View Javadoc

1   package org.codehaus.xfire.client.http;
2   
3   import java.io.IOException;
4   import java.io.InputStreamReader;
5   import java.io.OutputStream;
6   import java.io.Reader;
7   import java.net.HttpURLConnection;
8   import java.net.MalformedURLException;
9   import java.net.URL;
10  import java.net.URLConnection;
11  
12  import javax.xml.stream.XMLInputFactory;
13  import javax.xml.stream.XMLOutputFactory;
14  import javax.xml.stream.XMLStreamException;
15  import javax.xml.stream.XMLStreamReader;
16  import javax.xml.stream.XMLStreamWriter;
17  
18  import org.codehaus.xfire.XFireRuntimeException;
19  import org.codehaus.xfire.fault.XFireFault;
20  
21  /***
22   * Common functionality for the SOAP and Rest HTTP clients.
23   *
24   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
25   * @since Oct 26, 2004
26   */
27  public abstract class AbstractHttpClient
28  {
29      public final static String SOAP11_ENVELOPE_NS = "http://schemas.xmlsoap.org/soap/envelope/";
30  
31      public final static String SOAP12_ENVELOPE_NS = "http://www.w3.org/2003/05/soap-envelope";
32  
33      private String username;
34      private String password;
35      private String encoding = "UTF-8";
36      private String urlString;
37  
38      public void invoke()
39              throws IOException, XFireFault
40      {
41          URL url = new URL(urlString);
42          HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
43  
44          try
45          {
46  
47              urlConn.setDoInput(true);
48              urlConn.setDoOutput(true);
49              urlConn.setUseCaches(false);
50              urlConn.setRequestMethod("POST");
51              
52              // Specify the content type.
53              urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
54              
55              // Specify content type and encoding
56              // If content encoding is not explicitly specified
57              // ISO-8859-1 is assumed
58              urlConn.setRequestProperty("Content-type", "text/xml; charset=" + encoding);
59  
60              urlConn.setRequestProperty("User-Agent", "XFire Client +http://xfire.codehaus.org");
61              urlConn.setRequestProperty("Accept", "text/xml; text/html");
62  
63              writeHeaders(urlConn);
64  
65              OutputStream out = urlConn.getOutputStream();
66              writeRequest(out);
67              out.write('\n');
68  
69              out.flush();
70              out.close();
71  
72              Reader reader = null;
73              try
74              {
75                  reader = new InputStreamReader(urlConn.getInputStream());
76                  readResponse(reader);
77              }
78              catch (IOException ioe)
79              {
80                  if (urlConn.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR)
81                  {
82                      reader = new InputStreamReader(urlConn.getErrorStream());
83                      readResponse(reader);
84                  }
85              }
86              finally
87              {
88                  if (reader != null)
89                      reader.close();
90              }
91  
92          }
93          catch (MalformedURLException me)
94          {
95              throw new XFireRuntimeException("Bad URL.", me);
96          }
97          finally
98          {
99              urlConn.disconnect();
100         }
101     }
102 
103     protected void writeHeaders(URLConnection urlConn)
104     {
105 
106     }
107 
108     /***
109      * @return
110      */
111     protected void writeRequest(OutputStream out)
112     {
113         XMLOutputFactory factory = XMLOutputFactory.newInstance();
114 
115         try
116         {
117             XMLStreamWriter writer = factory.createXMLStreamWriter(out);
118 
119             writeRequest(writer);
120             writer.close();
121         }
122         catch (XMLStreamException e)
123         {
124             throw new XFireRuntimeException("Couldn't parse stream.", e);
125         }
126     }
127 
128     protected abstract void writeRequest(XMLStreamWriter writer)
129             throws XMLStreamException;
130 
131     /***
132      * @param reader
133      */
134     protected void readResponse(Reader is)
135             throws XFireFault
136     {
137         // Read in Envelope and then delegate header and Body
138         XMLInputFactory factory = XMLInputFactory.newInstance();
139 
140         try
141         {
142             readResponse(factory.createXMLStreamReader(is));
143         }
144         catch (XMLStreamException e)
145         {
146             throw new XFireRuntimeException("Couldn't parse stream.", e);
147         }
148     }
149 
150     protected abstract void readResponse(XMLStreamReader reader)
151             throws XMLStreamException, XFireFault;
152 
153     /***
154      * @return Returns the url.
155      */
156     public String getUrl()
157     {
158         return urlString;
159     }
160 
161     /***
162      * @param url The url to set.
163      */
164     public void setUrl(String url)
165     {
166         this.urlString = url;
167     }
168 
169     /***
170      * @return Returns the charset.
171      */
172     public String getEncoding()
173     {
174         return encoding;
175     }
176 
177     /***
178      * @param charset The charset to set.
179      */
180     public void setEncoding(String charset)
181     {
182         this.encoding = charset;
183     }
184 
185     /***
186      * @return Returns the password.
187      */
188     public String getPassword()
189     {
190         return password;
191     }
192 
193     /***
194      * @param password The password to set.
195      */
196     public void setPassword(String password)
197     {
198         this.password = password;
199     }
200 
201     /***
202      * @return Returns the username.
203      */
204     public String getUsername()
205     {
206         return username;
207     }
208 
209     /***
210      * @param username The username to set.
211      */
212     public void setUsername(String username)
213     {
214         this.username = username;
215     }
216 }