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