001: package localjava.client.dynamic;
002:
003: import javax.xml.namespace.QName;
004:
005: import org.apache.wsif.WSIFMessage;
006: import org.apache.wsif.WSIFException;
007: import org.apache.wsif.WSIFOperation;
008: import org.apache.wsif.WSIFPort;
009: import org.apache.wsif.WSIFService;
010: import org.apache.wsif.WSIFServiceFactory;
011:
012: import localjava.client.stub.addressbook.wsiftypes.Address;
013: import localjava.client.stub.addressbook.wsiftypes.Phone;
014:
015: public class Run {
016:
017: private static void addFirstAddress(WSIFPort port) {
018: try {
019: // create the operation
020: // note that we have two operations with the same name,
021: // so we need to specify the name of the input and output
022: // messages as well
023: WSIFOperation operation = port.createOperation("addEntry",
024: "AddEntryWholeNameRequest", null);
025:
026: // create the input message associated with this operation
027: WSIFMessage input = operation.createInputMessage();
028:
029: // populate the input message
030: input.setObjectPart("name", "John Smith");
031:
032: // create an address object to populate the input
033: Address address = new Address();
034: address.setStreetNum(25);
035: address.setStreetName("Willow Road");
036: address.setCity("MyTown");
037: address.setState("PA");
038: address.setZip(28382);
039: Phone phone = new Phone();
040: phone.setAreaCode(288);
041: phone.setExchange("555");
042: phone.setNumber("9891");
043: address.setPhoneNumber(phone);
044:
045: input.setObjectPart("address", address);
046:
047: // do the invocation
048: System.out.println("Adding address for John Smith...");
049: operation.executeInputOnlyOperation(input);
050:
051: } catch (WSIFException we) {
052: System.out.println("Got exception from WSIF, details:");
053: we.printStackTrace();
054: }
055: }
056:
057: private static void addSecondAddress(WSIFPort port) {
058: try {
059:
060: // create the operation
061: // note that we have two operations with the same name, so we need to specify the
062: // name of the input and output messages as well
063: WSIFOperation operation = port.createOperation("addEntry",
064: "AddEntryFirstAndLastNamesRequest", null);
065:
066: // create the input message associated with this operation
067: WSIFMessage input = operation.createInputMessage();
068:
069: // populate the input message
070: input.setObjectPart("firstName", "Jane");
071: input.setObjectPart("lastName", "White");
072:
073: // create an address object to populate the input
074: Address address = new Address();
075: address.setStreetNum(20);
076: address.setStreetName("Peachtree Avenue");
077: address.setCity("Atlanta");
078: address.setState("GA");
079: address.setZip(39892);
080: Phone phone = new Phone();
081: phone.setAreaCode(701);
082: phone.setExchange("555");
083: phone.setNumber("8721");
084: address.setPhoneNumber(phone);
085:
086: input.setObjectPart("address", address);
087:
088: // do the invocation
089: System.out.println("Adding address for Jane White...");
090: operation.executeInputOnlyOperation(input);
091:
092: } catch (WSIFException we) {
093: System.out.println("Got exception from WSIF, details:");
094: we.printStackTrace();
095: }
096: }
097:
098: private static void queryAddresses(WSIFPort port) {
099: try {
100: // create the operation
101: WSIFOperation operation = port
102: .createOperation("getAddressFromName");
103:
104: // create the input message associated with this operation
105: WSIFMessage input = operation.createInputMessage();
106: WSIFMessage output = operation.createOutputMessage();
107: WSIFMessage fault = operation.createFaultMessage();
108:
109: // populate the input message
110: input.setObjectPart("name", "John Smith");
111:
112: // do the invocation
113: System.out.println("Querying address for John Smith...");
114: if (operation.executeRequestResponseOperation(input,
115: output, fault)) {
116: // invocation succeeded
117: // extract the address from the output message
118: Address address = (Address) output
119: .getObjectPart("address");
120: System.out
121: .println("Service returned the following address:");
122: System.out.println(address.getStreetNum() + " "
123: + address.getStreetName() + ", "
124: + address.getCity() + " " + address.getState()
125: + " " + address.getZip() + "; Phone: ("
126: + address.getPhoneNumber().getAreaCode() + ") "
127: + address.getPhoneNumber().getExchange() + "-"
128: + address.getPhoneNumber().getNumber());
129: } else {
130: // invocation failed, check fault message
131: }
132:
133: // create the operation
134: operation = port.createOperation("getAddressFromName");
135:
136: // create the input message associated with this operation
137: input = operation.createInputMessage();
138: output = operation.createOutputMessage();
139: fault = operation.createFaultMessage();
140:
141: // populate the input message
142: input.setObjectPart("name", "Jane White");
143:
144: // do the invocation
145: System.out.println("Querying address for Jane White...");
146: if (operation.executeRequestResponseOperation(input,
147: output, fault)) {
148: // invocation succeeded
149: // extract the address from the output message
150: Address address = (Address) output
151: .getObjectPart("address");
152: System.out
153: .println("Service returned the following address:");
154: System.out.println(address.getStreetNum() + " "
155: + address.getStreetName() + ", "
156: + address.getCity() + " " + address.getState()
157: + " " + address.getZip() + "; Phone: ("
158: + address.getPhoneNumber().getAreaCode() + ") "
159: + address.getPhoneNumber().getExchange() + "-"
160: + address.getPhoneNumber().getNumber());
161: } else {
162: // invocation failed, check fault message
163: }
164: } catch (WSIFException we) {
165: System.out.println("Got exception from WSIF, details:");
166: we.printStackTrace();
167: }
168: }
169:
170: public static void main(String[] args) throws Exception {
171: if (args.length != 1) {
172: System.out
173: .println("Usage: java samples.java.client.dynamic.Run <wsdl location>");
174: System.exit(1);
175: }
176:
177: // create a service factory
178: WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
179: WSIFService service = factory.getService(args[0], null, null,
180: "http://wsifservice.addressbook/", "AddressBook");
181:
182: // map types
183: service
184: .mapType(
185: new QName("http://wsiftypes.addressbook/",
186: "Address"),
187: Class
188: .forName("localjava.client.stub.addressbook.wsiftypes.Address"));
189: service
190: .mapType(
191: new QName("http://wsiftypes.addressbook/",
192: "Phone"),
193: Class
194: .forName("localjava.client.stub.addressbook.wsiftypes.Phone"));
195:
196: // get the port
197: WSIFPort port = service.getPort();
198:
199: // add the first address
200: addFirstAddress(port);
201:
202: // add the second address
203: addSecondAddress(port);
204:
205: // query addresses
206: queryAddresses(port);
207: }
208: }
|