1 package localjava.client.dynamic; 2 3 import javax.xml.namespace.QName ; 4 5 import org.apache.wsif.WSIFMessage; 6 import org.apache.wsif.WSIFException; 7 import org.apache.wsif.WSIFOperation; 8 import org.apache.wsif.WSIFPort; 9 import org.apache.wsif.WSIFService; 10 import org.apache.wsif.WSIFServiceFactory; 11 12 import localjava.client.stub.addressbook.wsiftypes.Address; 13 import localjava.client.stub.addressbook.wsiftypes.Phone; 14 15 public class Run { 16 17 private static void addFirstAddress(WSIFPort port) { 18 try { 19 WSIFOperation operation = 24 port.createOperation( 25 "addEntry", 26 "AddEntryWholeNameRequest", 27 null); 28 29 WSIFMessage input = operation.createInputMessage(); 31 32 input.setObjectPart("name", "John Smith"); 34 35 Address address = new Address(); 37 address.setStreetNum(25); 38 address.setStreetName("Willow Road"); 39 address.setCity("MyTown"); 40 address.setState("PA"); 41 address.setZip(28382); 42 Phone phone = new Phone(); 43 phone.setAreaCode(288); 44 phone.setExchange("555"); 45 phone.setNumber("9891"); 46 address.setPhoneNumber(phone); 47 48 input.setObjectPart("address", address); 49 50 System.out.println("Adding address for John Smith..."); 52 operation.executeInputOnlyOperation(input); 53 54 } catch (WSIFException we) { 55 System.out.println("Got exception from WSIF, details:"); 56 we.printStackTrace(); 57 } 58 } 59 60 private static void addSecondAddress(WSIFPort port) { 61 try { 62 63 WSIFOperation operation = 67 port.createOperation( 68 "addEntry", 69 "AddEntryFirstAndLastNamesRequest", 70 null); 71 72 WSIFMessage input = operation.createInputMessage(); 74 75 input.setObjectPart("firstName", "Jane"); 77 input.setObjectPart("lastName", "White"); 78 79 Address address = new Address(); 81 address.setStreetNum(20); 82 address.setStreetName("Peachtree Avenue"); 83 address.setCity("Atlanta"); 84 address.setState("GA"); 85 address.setZip(39892); 86 Phone phone = new Phone(); 87 phone.setAreaCode(701); 88 phone.setExchange("555"); 89 phone.setNumber("8721"); 90 address.setPhoneNumber(phone); 91 92 input.setObjectPart("address", address); 93 94 System.out.println("Adding address for Jane White..."); 96 operation.executeInputOnlyOperation(input); 97 98 } catch (WSIFException we) { 99 System.out.println("Got exception from WSIF, details:"); 100 we.printStackTrace(); 101 } 102 } 103 104 private static void queryAddresses(WSIFPort port) { 105 try { 106 WSIFOperation operation = 108 port.createOperation("getAddressFromName"); 109 110 WSIFMessage input = operation.createInputMessage(); 112 WSIFMessage output = operation.createOutputMessage(); 113 WSIFMessage fault = operation.createFaultMessage(); 114 115 input.setObjectPart("name", "John Smith"); 117 118 System.out.println("Querying address for John Smith..."); 120 if (operation.executeRequestResponseOperation(input, output, fault)) { 121 Address address = (Address) output.getObjectPart("address"); 124 System.out.println("Service returned the following address:"); 125 System.out.println( 126 address.getStreetNum() 127 + " " 128 + address.getStreetName() 129 + ", " 130 + address.getCity() 131 + " " 132 + address.getState() 133 + " " 134 + address.getZip() 135 + "; Phone: (" 136 + address.getPhoneNumber().getAreaCode() 137 + ") " 138 + address.getPhoneNumber().getExchange() 139 + "-" 140 + address.getPhoneNumber().getNumber()); 141 } else { 142 } 144 145 operation = port.createOperation("getAddressFromName"); 147 148 input = operation.createInputMessage(); 150 output = operation.createOutputMessage(); 151 fault = operation.createFaultMessage(); 152 153 input.setObjectPart("name", "Jane White"); 155 156 System.out.println("Querying address for Jane White..."); 158 if (operation.executeRequestResponseOperation(input, output, fault)) { 159 Address address = (Address) output.getObjectPart("address"); 162 System.out.println("Service returned the following address:"); 163 System.out.println( 164 address.getStreetNum() 165 + " " 166 + address.getStreetName() 167 + ", " 168 + address.getCity() 169 + " " 170 + address.getState() 171 + " " 172 + address.getZip() 173 + "; Phone: (" 174 + address.getPhoneNumber().getAreaCode() 175 + ") " 176 + address.getPhoneNumber().getExchange() 177 + "-" 178 + address.getPhoneNumber().getNumber()); 179 } else { 180 } 182 } catch (WSIFException we) { 183 System.out.println("Got exception from WSIF, details:"); 184 we.printStackTrace(); 185 } 186 } 187 188 public static void main(String [] args) throws Exception { 189 if (args.length != 1) { 190 System.out.println( 191 "Usage: java samples.java.client.dynamic.Run <wsdl location>"); 192 System.exit(1); 193 } 194 195 WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); 197 WSIFService service = 198 factory.getService( 199 args[0], 200 null, 201 null, 202 "http://wsifservice.addressbook/", 203 "AddressBook"); 204 205 service.mapType( 207 new QName ("http://wsiftypes.addressbook/", "Address"), 208 Class.forName("localjava.client.stub.addressbook.wsiftypes.Address")); 209 service.mapType( 210 new QName ("http://wsiftypes.addressbook/", "Phone"), 211 Class.forName("localjava.client.stub.addressbook.wsiftypes.Phone")); 212 213 WSIFPort port = service.getPort(); 215 216 addFirstAddress(port); 218 219 addSecondAddress(port); 221 222 queryAddresses(port); 224 } 225 } 226 | Popular Tags |