58 package samples.addressbook;
59
60 import java.io.*;
61 import java.util.*;
62 import java.net.*;
63 import org.w3c.dom.*;
64 import org.apache.soap.util.xml.*;
65 import org.apache.soap.*;
66 import org.apache.soap.encoding.*;
67 import org.apache.soap.encoding.soapenc.*;
68 import org.apache.soap.rpc.*;
69
70 /**
71 * See \samples\addressbook\readme for info.
72 *
73 * @author Matthew J. Duftler (duftler@us.ibm.com)
74 */
75 public class GetAddress
76 {
77 public static void main(String[] args) throws Exception
78 {
79 if (args.length != 2
80 && (args.length != 3 || !args[0].startsWith("-")))
81 {
82 System.err.println("Usage:");
83 System.err.println(" java " + GetAddress.class.getName() +
84 " [-encodingStyleURI] SOAP-router-URL nameToLookup");
85 System.exit (1);
86 }
87
88 // Process the arguments.
89 int offset = 3 - args.length;
90 String encodingStyleURI = args.length == 3
91 ? args[0].substring(1)
92 : Constants.NS_URI_SOAP_ENC;
this is the URL of the soap service
93 URL url = new URL(args[1 - offset]);
94 String nameToLookup = args[2 - offset];
this creates a type registry and a standard serializer
95 SOAPMappingRegistry smr = new SOAPMappingRegistry();
96 BeanSerializer beanSer = new BeanSerializer();
97
98 // Map the types.
the statements below are to do with registering the type marshallers. Tricky!
99 smr.mapTypes(Constants.NS_URI_SOAP_ENC,
100 new QName("urn:xml-soap-address-demo", "address"),
101 Address.class, beanSer, beanSer);
102 smr.mapTypes(Constants.NS_URI_SOAP_ENC,
103 new QName("urn:xml-soap-address-demo", "phone"),
104 PhoneNumber.class, beanSer, beanSer);
105
106 // Build the call.
this prepares the call
107 Call call = new Call();
108
this sets the registry for this call
109 call.setSOAPMappingRegistry(smr);
this is the URN of the target service
110 call.setTargetObjectURI("urn:AddressFetcher");
this sets the particular service method
111 call.setMethodName("getAddressFromName");
this is always the same
112 call.setEncodingStyleURI(encodingStyleURI);
113
this prepares the parameter Vector
114 Vector params = new Vector();
115
this sets the single parameter for this call
116 params.addElement(new Parameter("nameToLookup", String.class,
117 nameToLookup, null));
118 call.setParams(params);
119
120 // Invoke the call.
121 Response resp;
122
123 try
124 {
Finally, we're there!
125 resp = call.invoke(url, "");
126 }
127 catch (SOAPException e)
128 {
129 System.err.println("Caught SOAPException (" +
130 e.getFaultCode() + "): " +
131 e.getMessage());
132 return;
133 }
134
135 // Check the response.
136 if (!resp.generatedFault())
137 {
138 Parameter ret = resp.getReturnValue();
139 Object value = ret.getValue();
140
141 System.out.println(value != null ? "\n" + value : "I don't know.");
142 }
143 else
144 {
This isthe classical error handling code
145 Fault fault = resp.getFault();
146
147 System.err.println("Generated fault: ");
148 System.out.println (" Fault Code = " + fault.getFaultCode());
149 System.out.println (" Fault String = " + fault.getFaultString());
150 }
151 }
152 }