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