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