KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > soap > SOAPConnectionImpl


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis.soap;
17
18 import org.apache.axis.Message;
19 import org.apache.axis.attachments.Attachments;
20 import org.apache.axis.client.Call;
21 import org.apache.axis.utils.Messages;
22
23 import javax.xml.soap.SOAPException JavaDoc;
24 import javax.xml.soap.SOAPMessage JavaDoc;
25 import javax.xml.soap.MimeHeaders JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 /**
29  * SOAP Connection implementation
30  *
31  * @author Davanum Srinivas (dims@yahoo.com)
32  */

33 public class SOAPConnectionImpl extends javax.xml.soap.SOAPConnection JavaDoc {
34     private boolean closed = false;
35     private Integer JavaDoc timeout = null;
36
37     /**
38      * get the timeout value
39      * @return
40      */

41     public Integer JavaDoc getTimeout() {
42         return timeout;
43     }
44
45     /**
46      * set the timeout value
47      * @param timeout
48      */

49     public void setTimeout(Integer JavaDoc timeout) {
50         this.timeout = timeout;
51     }
52
53     /**
54      * Sends the given message to the specified endpoint and
55      * blocks until it has returned the response.
56      * @param request the <CODE>SOAPMessage</CODE>
57      * object to be sent
58      * @param endpoint a <CODE>URLEndpoint</CODE>
59      * object giving the URL to which the message should be
60      * sent
61      * @return the <CODE>SOAPMessage</CODE> object that is the
62      * response to the message that was sent
63      * @throws SOAPException if there is a SOAP error
64      */

65     public SOAPMessage JavaDoc call(SOAPMessage JavaDoc request, Object JavaDoc endpoint)
66         throws SOAPException JavaDoc {
67         if(closed){
68             throw new SOAPException JavaDoc(Messages.getMessage("connectionClosed00"));
69         }
70         try {
71             Call call = new Call(endpoint.toString());
72             ((org.apache.axis.Message)request).setMessageContext(call.getMessageContext());
73             Attachments attachments = ((org.apache.axis.Message)
74                     request).getAttachmentsImpl();
75             if (attachments != null) {
76                 Iterator JavaDoc iterator = attachments.getAttachments().iterator();
77                 while (iterator.hasNext()) {
78                     Object JavaDoc attachment = iterator.next();
79                     call.addAttachmentPart(attachment);
80                 }
81             }
82             
83             String JavaDoc soapActionURI = checkForSOAPActionHeader(request);
84             if (soapActionURI != null)
85                 call.setSOAPActionURI(soapActionURI);
86             
87             call.setTimeout(timeout);
88             call.setReturnClass(SOAPMessage JavaDoc.class);
89             call.setProperty(Call.CHECK_MUST_UNDERSTAND,Boolean.FALSE);
90             call.invoke((Message) request);
91             return call.getResponseMessage();
92         } catch (java.net.MalformedURLException JavaDoc mue){
93             throw new SOAPException JavaDoc(mue);
94         } catch (org.apache.axis.AxisFault af){
95             throw new SOAPException JavaDoc(af);
96         }
97     }
98
99     /**
100      * Checks whether the request has an associated SOAPAction MIME header
101      * and returns its value.
102      * @param request the message to check
103      * @return the value of any associated SOAPAction MIME header or null
104      * if there is no such header.
105      */

106     private String JavaDoc checkForSOAPActionHeader(SOAPMessage JavaDoc request) {
107         MimeHeaders JavaDoc hdrs = request.getMimeHeaders();
108         if (hdrs != null) {
109             String JavaDoc[] saHdrs = hdrs.getHeader("SOAPAction");
110             if (saHdrs != null && saHdrs.length > 0)
111                 return saHdrs[0];
112         }
113         return null;
114     }
115     
116     /**
117      * Closes this <CODE>SOAPConnection</CODE> object.
118      * @throws SOAPException if there is a SOAP error
119      */

120     public void close() throws SOAPException JavaDoc {
121         if(closed){
122             throw new SOAPException JavaDoc(Messages.getMessage("connectionClosed00"));
123         }
124         closed = true;
125     }
126 }
127
Popular Tags