1 package org.codehaus.xfire.transport.http;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.net.HttpURLConnection;
7 import java.net.URL;
8
9 import org.codehaus.xfire.XFireException;
10 import org.codehaus.xfire.exchange.InMessage;
11 import org.codehaus.xfire.fault.XFireFault;
12 import org.codehaus.xfire.util.STAXUtils;
13
14 /***
15 * Http Sender
16 *
17 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
18 * @since Oct 26, 2004
19 */
20 public class HttpMessageSender
21 {
22 private String username;
23 private String password;
24 private String encoding;
25 private String urlString;
26 private HttpURLConnection urlConn;
27 private InputStream is;
28 private String action;
29
30 public HttpMessageSender(String urlString, String encoding)
31 {
32 this.urlString = urlString;
33 this.encoding = encoding;
34 }
35
36 public void open() throws IOException, XFireFault
37 {
38 URL url = new URL(urlString);
39 urlConn = createConnection(url);
40
41 urlConn.setDoInput(true);
42 urlConn.setDoOutput(true);
43 urlConn.setUseCaches(false);
44 urlConn.setRequestMethod("POST");
45
46
47 urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
48
49 urlConn.setRequestProperty("User-Agent", "XFire Client +http://xfire.codehaus.org");
50 urlConn.setRequestProperty("Accept", "text/xml; text/html");
51 urlConn.setRequestProperty("Content-type", "text/xml; charset=" + encoding);
52
53 String action = getAction();
54 if (action == null)
55 action = "";
56
57 urlConn.setRequestProperty( "SOAPAction", "\"" + action + "\"");
58 }
59
60 public OutputStream getOutputStream() throws IOException, XFireFault
61 {
62 return urlConn.getOutputStream();
63 }
64
65 public InMessage getInMessage() throws IOException
66 {
67 try
68 {
69 is = urlConn.getInputStream();
70 }
71 catch (IOException ioe)
72 {
73 if (urlConn.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR)
74 {
75 is = urlConn.getErrorStream();
76 }
77 }
78
79 return new InMessage(STAXUtils.createXMLStreamReader(is, encoding), urlString);
80 }
81
82 public void close() throws XFireException
83 {
84
85 try
86 {
87 if (is != null)
88 is.close();
89 }
90 catch (IOException e)
91 {
92 throw new XFireException("Couldn't close stream.", e);
93 }
94 finally
95 {
96 if (urlConn != null)
97 urlConn.disconnect();
98 }
99 }
100
101 private HttpURLConnection createConnection(URL url)
102 throws IOException
103 {
104 return (HttpURLConnection) url.openConnection();
105 }
106
107 /***
108 * @return Returns the url.
109 */
110 public String getUrl()
111 {
112 return urlString;
113 }
114
115 /***
116 * @param url The url to set.
117 */
118 public void setUrl(String url)
119 {
120 this.urlString = url;
121 }
122
123 /***
124 * @return Returns the password.
125 */
126 public String getPassword()
127 {
128 return password;
129 }
130
131 /***
132 * @param password The password to set.
133 */
134 public void setPassword(String password)
135 {
136 this.password = password;
137 }
138
139 /***
140 * @return Returns the username.
141 */
142 public String getUsername()
143 {
144 return username;
145 }
146
147 /***
148 * @param username The username to set.
149 */
150 public void setUsername(String username)
151 {
152 this.username = username;
153 }
154
155 public String getAction()
156 {
157 return action;
158 }
159
160 public void setAction(String action)
161 {
162 this.action = action;
163 }
164
165 }