KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > rpc > Call


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "SOAP" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2000, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.soap.rpc;
59
60 import java.io.*;
61 import java.net.*;
62 import java.util.*;
63 import java.lang.reflect.*;
64 import javax.xml.parsers.*;
65 import org.w3c.dom.*;
66 import org.xml.sax.*;
67 import org.apache.soap.util.xml.*;
68 import org.apache.soap.util.IOUtils;
69 import org.apache.soap.*;
70 import org.apache.soap.encoding.*;
71 import org.apache.soap.transport.*;
72 import org.apache.soap.transport.http.*;
73 import org.apache.soap.server.*;
74 import javax.mail.MessagingException JavaDoc;
75 import javax.mail.internet.MimeBodyPart JavaDoc;
76
77 /**
78  * A <code>Call</code> object represents an <em>RPC</em> call. Both the
79  * client and the server use <code>Call</code> objects to invoke the
80  * method.
81  *
82  * @author Matthew J. Duftler (duftler@us.ibm.com)
83  * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
84  */

85 public class Call extends RPCMessage
86 {
87   private DocumentBuilder xdb = XMLParserUtils.getXMLDocBuilder();
88   private SOAPMappingRegistry smr = null;
89   private SOAPTransport st = null;;
90   private int to = 0;
91
92   public Call()
93   {
94     this(null, null, null, null, null);
95   }
96
97   public Call(String JavaDoc targetObjectURI, String JavaDoc methodName, Vector params,
98               Header header, String JavaDoc encodingStyleURI)
99   {
100     this(targetObjectURI, methodName, params, header, encodingStyleURI,
101          new SOAPContext());
102   }
103
104   public Call(String JavaDoc targetObjectURI, String JavaDoc methodName, Vector params,
105               Header header, String JavaDoc encodingStyleURI, SOAPContext ctx)
106   {
107     super(targetObjectURI, methodName, params, header, encodingStyleURI, ctx);
108   }
109
110   public void setSOAPMappingRegistry(SOAPMappingRegistry smr)
111   {
112     this.smr = smr;
113   }
114
115   public SOAPMappingRegistry getSOAPMappingRegistry()
116   {
117     // if the smr hasn't been created yet, do it now
118
if (smr == null)
119     {
120       smr = new SOAPMappingRegistry();
121     }
122
123     return smr;
124   }
125
126   public void setSOAPTransport(SOAPTransport st)
127   {
128     this.st = st;
129   }
130    
131   public SOAPTransport getSOAPTransport()
132   {
133     return st;
134   }
135
136   /**
137    * Set timeout in our MessageContext.
138    *
139    * @param value the maximum amount of time, in milliseconds
140    */

141   public void setTimeout (int value) {
142     to = value;
143   }
144     
145   /**
146    * Get timeout from our MessageContext.
147    *
148    * @return value the maximum amount of time, in milliseconds
149    */

150   public int getTimeout () {
151     return to;
152   }
153     
154   /**
155    * Add a MIME BodyPart.
156    *
157    * @param part The Part to be appended
158    * @exception MessagingException
159    */

160   public void addBodyPart(MimeBodyPart JavaDoc part) throws MessagingException JavaDoc
161   {
162     ctx.addBodyPart(part);
163   }
164
165   /**
166    * Remove a MIME BodyPart.
167    */

168   public void removeBodyPart(MimeBodyPart JavaDoc part) throws MessagingException JavaDoc
169   {
170     ctx.removeBodyPart(part);
171   }
172
173   public Envelope buildEnvelope()
174   {
175     return super.buildEnvelope(false);
176   }
177
178   public static Call extractFromEnvelope(Envelope env, ServiceManager svcMgr,
179                                          SOAPContext ctx)
180     throws IllegalArgumentException JavaDoc
181   {
182     return (Call)RPCMessage.extractFromEnvelope(env, svcMgr, false, null, ctx);
183   }
184
185   /**
186    * Check if response root part is text/xml and return it as a String.
187    * Temporarily placing this method here - I think it will be moved to
188    * SOAPContext after a redesign of SOAPTransport interaction.
189    *
190    * @author Wouter Cloetens <wcloeten@raleigh.ibm.com>
191    */

192   public static String JavaDoc getEnvelopeString(SOAPTransport st)
193     throws SOAPException, MessagingException JavaDoc, IOException {
194     SOAPContext respCtx = st.getResponseSOAPContext();
195     BufferedReader in = null;
196     String JavaDoc payloadStr = null;
197
198     MimeBodyPart JavaDoc rootPart = respCtx.getRootPart();
199     if (rootPart.isMimeType("text/*")) {
200       // Get the input stream to read the response envelope from.
201
in = st.receive();
202       payloadStr = IOUtils.getStringFromReader(in);
203     }
204
205     // Check Content-Type of root part of response to see if it's
206
// consistent with a SOAP envelope (text/xml).
207
if (!rootPart.isMimeType(Constants.HEADERVAL_CONTENT_TYPE)) {
208       throw new SOAPException(Constants.FAULT_CODE_PROTOCOL,
209         "Unsupported response content type \"" +
210         rootPart.getContentType() + "\", must be: \"" +
211         Constants.HEADERVAL_CONTENT_TYPE + "\"." +
212         (payloadStr == null ? "" : " Response was:\n" + payloadStr));
213     }
214
215     return payloadStr;
216   }
217
218   /**
219    * Invoke this call at the specified URL. Valid only on the client side.
220    */

221   public Response invoke(URL url, String JavaDoc SOAPActionURI) throws SOAPException
222   {
223     if (SOAPActionURI == null)
224     {
225       SOAPActionURI = "";
226     }
227
228     // if the smr hasn't been created yet, do it now
229
if (smr == null)
230     {
231       smr = new SOAPMappingRegistry();
232     }
233
234     try
235     {
236       // Build an envelope containing the call.
237
Envelope callEnv = buildEnvelope();
238
239       // Construct default HTTP transport if not specified.
240
if (st == null)
241         st = new SOAPHTTPConnection();
242
243       // set the timeout
244
if (to != 0 && st instanceof SOAPHTTPConnection)
245         ((SOAPHTTPConnection)st).setTimeout(to);
246
247       // Post the call envelope.
248
st.send(url, SOAPActionURI, null, callEnv, smr, ctx);
249
250       // Get the response context.
251
SOAPContext respCtx = st.getResponseSOAPContext();
252
253       // Pre-read the response root part as a String to be able to
254
// log it in an exception if parsing fails.
255
String JavaDoc payloadStr = getEnvelopeString(st);
256
257       // Parse the incoming response stream.
258
Document respDoc =
259         xdb.parse(new InputSource(new StringReader(payloadStr)));
260       Element payload = null;
261
262       if (respDoc != null)
263       {
264         payload = respDoc.getDocumentElement();
265       }
266       else //probably does not happen
267
{
268         throw new SOAPException (Constants.FAULT_CODE_CLIENT,
269           "Parsing error, response was:\n" + payloadStr);
270       }
271
272       // Unmarshall the response envelope.
273
Envelope respEnv = Envelope.unmarshall(payload, respCtx);
274
275       // Extract the response from the response envelope.
276
Response resp = Response.extractFromEnvelope(respEnv, smr, respCtx);
277
278       // Reset the targetObjectURI in case it was changed by the server
279
String JavaDoc fullTargetObjectURI = resp.getFullTargetObjectURI();
280
281       if (fullTargetObjectURI != null)
282       {
283         setTargetObjectURI(fullTargetObjectURI);
284       }
285
286       return resp;
287     }
288     catch (MessagingException JavaDoc me)
289     {
290       throw new SOAPException(Constants.FAULT_CODE_CLIENT, me.getMessage(), me);
291     }
292     catch (IllegalArgumentException JavaDoc e)
293     {
294       throw new SOAPException(Constants.FAULT_CODE_CLIENT, e.getMessage(), e);
295     }
296     catch (SAXException e)
297     {
298       throw new SOAPException(Constants.FAULT_CODE_CLIENT,
299                               "Parsing error, response was:\n" +e.getMessage(),
300                   e);
301     }
302     catch (IOException e)
303     {
304       throw new SOAPException(Constants.FAULT_CODE_PROTOCOL,
305                               e.getMessage(), e);
306     }
307   }
308 }
309
Popular Tags