Hi, in this Posts I will be explaining you how to develop the client to access the AXIOM based AXIS2 web service, what we created in part 1 and tested in part 2.
Ok So lets get started
Ok So lets get started
Prerequisites
The service should be developped and deployed in Axis2, this is explained in part 1 of this post series.
I will be using eclipse to create my client.
Architecture of the client
Customers items will be added to the itemsList and all the items will be processes when buying the items (Final Checkout). There won't be any user inputs or interfaces, this is a simple client side application to demonstrate how a service can be accessed in Axis2 via AXIOM So all the inouts will be hard coded in the client application.
Creating the Client
Step 1
- Create a new Java Project and lets name it "Axis2_Webservice".
- Within "src" directory let's create a package as follows, "televisionshop.client"
- To resolve dependency issues add all the jars in the <AXIS2_HOME>/lib directory into your class path. All the jars may not be required, doing this to make things easy :)
- Now within the created package create a new java class named "TelevisionShopClient.java" with the following code snippets.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | package televisionshop.client; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import javax.xml.namespace.QName; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.axis2.AxisFault; import org.apache.axis2.Constants; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; public class TelevisionShopClient { private static String namespace = "http://myshop.com/xsd"; // Customer Items will be added to the itemList static HashMap<String, String> itemList = new HashMap<String, String>(); private static EndpointReference endPointRef = new EndpointReference( "http://localhost:8080/axis2/services/TelevisionShopService"); public static OMElement getProductAXIOMXML(String id) { OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac .createOMNamespace("http://myshop.com/xsd", "tns"); OMElement method = fac.createOMElement("getProduct", omNs); OMElement value = fac.createOMElement("id", omNs); value.addChild(fac.createOMText(value, id)); method.addChild(value); return method; } public static OMElement setProductAXIOMXML(String id, String model, String price, String qty) { OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac .createOMNamespace("http://myshop.com/xsd", "tns"); OMElement method = fac.createOMElement("setProduct", omNs); OMElement value1 = fac.createOMElement("id", omNs); value1.addChild(fac.createOMText(value1, id)); method.addChild(value1); OMElement value2 = fac.createOMElement("model", omNs); value2.addChild(fac.createOMText(value2, model)); method.addChild(value2); OMElement value3 = fac.createOMElement("price", omNs); value3.addChild(fac.createOMText(value3, price)); method.addChild(value3); OMElement value4 = fac.createOMElement("qty", omNs); value4.addChild(fac.createOMText(value4, qty)); method.addChild(value4); return method; } public static void getProductDetails(String id) throws AxisFault { OMElement getPhonePayload = getProductAXIOMXML(id); Options options = new Options(); options.setTo(endPointRef); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); ServiceClient sender = new ServiceClient(); sender.setOptions(options); OMElement result = sender.sendReceive(getPhonePayload); System.out.println("Model : " + result.getFirstChildWithName(new QName(namespace, "model")) .getText()); System.out.println("Price : " + result.getFirstChildWithName(new QName(namespace, "price")) .getText()); System.out.println("QTY On Stocks : " + result.getFirstChildWithName(new QName(namespace, "qty")) .getText()); System.out.println(); } public static void updateProductDetails(String id, String model, String price, String qty) { try { OMElement updatePhonePayload = setProductAXIOMXML(id, model, price, qty); Options options = new Options(); options.setTo(endPointRef); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); ServiceClient sender = new ServiceClient(); sender.setOptions(options); sender.fireAndForget(updatePhonePayload); System.out.println("Updated Product Details of Product : " + model); } catch (Exception e) { e.printStackTrace(); } } public static void buyItems(HashMap itemList) throws AxisFault { Iterator it = itemList.entrySet().iterator(); float total = 0; System.out.println("================================================="); System.out.println("===========YOUR ORDER DETAILS===================="); System.out.println("================================================="); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); String key = (String) entry.getKey(); String val = (String) entry.getValue(); OMElement getPhonePayload = getProductAXIOMXML(key); Options options = new Options(); options.setTo(endPointRef); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); ServiceClient sender = new ServiceClient(); sender.setOptions(options); OMElement result = sender.sendReceive(getPhonePayload); String model = result.getFirstChildWithName( new QName(namespace, "model")).getText(); if (Integer.parseInt(result.getFirstChildWithName( new QName(namespace, "qty")).getText()) <= Integer .parseInt(val)) { System.out.println("Not Enough Stock for : " + model); System.out.println(); continue; } float price = Float.parseFloat(result.getFirstChildWithName( new QName(namespace, "price")).getText()); total += price * Float.parseFloat(val); System.out.println("Value of your Items : " + model + " Television : (No of Units : " + val + " ) = " + price * Float.parseFloat(val)); } System.out.println(); System.out.println("The Total Value of your Items = " + total); System.out.println(); } public static void main(String[] args) throws AxisFault { // Adding Products to the Service updateProductDetails("001", "Samsung", "100", "6"); updateProductDetails("002", "Philipse", "200", "15"); // Retrieving information about a product getProductDetails("1"); // Customer adding items to the List --> The first arguement is product // ID and Second is no of units itemList.put("001", "5"); itemList.put("002", "3"); // Checking out buyItems(itemList); } } |
- The final file structure will look like following,
- Now right click on the "TelevisionShopClient.java" file and select Run As >>> Java Application.
0 comments:
Post a Comment