View Javadoc

1   package org.codehaus.xfire.client.http;
2   
3   import java.net.URLConnection;
4   
5   import javax.xml.stream.XMLStreamException;
6   import javax.xml.stream.XMLStreamReader;
7   import javax.xml.stream.XMLStreamWriter;
8   
9   import org.codehaus.xfire.client.ClientHandler;
10  
11  /***
12   * A SOAP client for the HTTP transport.
13   * 
14   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
15   * @since Oct 26, 2004
16   */
17  public class SoapHttpClient 
18      extends AbstractHttpClient
19  {
20      private ClientHandler bodyHandler;
21      private ClientHandler headerHandler;
22      private String soapVersion;
23      private String action = "";
24      
25      /***
26       * Create the SoapHttpClient. It will default to generating a
27       * SOAP 1.1 Envelope.
28       * 
29       * @param bodyHandler A handler which will generate and process
30       * the SOAP Body.
31       * @param url The URL to invoke.
32       */
33      public SoapHttpClient(ClientHandler bodyHandler, String url)
34      {
35          super();
36          this.bodyHandler = bodyHandler;
37          setUrl( url );
38          setSoapVersion( SOAP11_ENVELOPE_NS );
39      }    
40      
41      /***
42       * Create the SoapHttpClient. It will default to generating a
43       * SOAP 1.1 Envelope.
44       * 
45       * @param bodyHandler A handler which will generate and process
46       * the SOAP Body.
47       * @param headerHandler A handler which will generate and process
48       * the SOAP Header.
49       * @param url The URL to invoke.
50       */
51      public SoapHttpClient( ClientHandler bodyHandler, 
52                             ClientHandler headerHandler, 
53                             String url )
54      {
55          super();
56          this.bodyHandler = bodyHandler;
57          this.headerHandler = headerHandler;
58          setUrl( url );
59          setSoapVersion( SOAP11_ENVELOPE_NS );
60      }
61      /***
62       * Create the SoapHttpClient.
63       * 
64       * @param bodyHandler A handler which will generate and process
65       * the SOAP Body.
66       * @param headerHandler A handler which will generate and process
67       * the SOAP Header.
68       * @param url The URL to invoke.
69       * @param soapVersion The soap version.  See SOAPConstants.
70       */
71      public SoapHttpClient( ClientHandler bodyHandler, 
72                             ClientHandler headerHandler, 
73                             String url,
74                             String soapVersion )
75      {
76          super();
77          this.bodyHandler = bodyHandler;
78          this.headerHandler = headerHandler;
79          this.soapVersion = soapVersion;
80          setUrl( url );
81      }
82      
83      protected void writeRequest(XMLStreamWriter writer) 
84          throws XMLStreamException
85      {
86          writer.writeStartDocument(getEncoding(), "1.0");
87  
88          writer.setPrefix("soap", getSoapVersion());
89          writer.writeStartElement("soap", "Envelope", soapVersion);
90          writer.writeNamespace("soap", getSoapVersion());
91          
92          if ( headerHandler != null )
93          {
94              writer.writeStartElement("soap", "Header", soapVersion);
95              headerHandler.writeRequest(writer);
96              writer.writeEndElement();
97          }
98          
99          writer.writeStartElement("soap", "Body", soapVersion);
100         
101         bodyHandler.writeRequest(writer);
102         
103         writer.writeEndElement(); // Body
104 
105         writer.writeEndElement();  // Envelope
106 
107         writer.writeEndDocument();
108         writer.close();
109     }
110 
111     protected void readResponse(XMLStreamReader reader) 
112         throws XMLStreamException
113     {
114         while ( true )
115         {
116             int event = reader.next();
117             switch( event )
118             {
119                 case XMLStreamReader.END_DOCUMENT:
120                     return;
121                 case XMLStreamReader.START_ELEMENT:
122                     if( reader.getLocalName().equals("Header") && headerHandler != null )
123                     {
124                         reader.nextTag();
125                         headerHandler.handleResponse(reader);
126                     }
127                     else if ( reader.getLocalName().equals("Body") )
128                     {
129                         reader.nextTag();
130                         bodyHandler.handleResponse(reader);
131                     }
132                     break;
133                 default:
134                     break;
135             }
136         }
137     } 
138     
139     protected void writeHeaders(URLConnection urlConn)
140     {
141         // Set the SOAPAction header
142         urlConn.setRequestProperty( "SOAPAction", getAction() );
143     }
144     
145     /***
146      * @return Returns the bodyHandler.
147      */
148     public ClientHandler getBodyHandler()
149     {
150         return bodyHandler;
151     }
152     
153     /***
154      * @return Returns the headerHandler.
155      */
156     public ClientHandler getHeaderHandler()
157     {
158         return headerHandler;
159     }
160     
161     /***
162      * @return Returns the soapVersion.
163      */
164     public String getSoapVersion()
165     {
166         return soapVersion;
167     }
168     
169     /***
170      * @param soapVersion The soapVersion to set.
171      */
172     public void setSoapVersion(String soapVersion)
173     {
174         this.soapVersion = soapVersion;
175     }
176     /***
177      * @param action The action to set.
178      */
179     public void setAction(String action)
180     {
181         this.action = action;
182     }
183     
184     public String getAction()
185     {
186         return action;
187     }
188 }