1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2004-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: HTTPEndpoint.java,v 1.4 2005/04/17 13:41:49 tanderson Exp $
44 */
45 package org.exolab.jms.net.http;
46
47 import java.io.BufferedReader;
48 import java.io.IOException;
49 import java.io.InputStream;
50 import java.io.InputStreamReader;
51 import java.io.OutputStream;
52 import java.net.HttpURLConnection;
53 import java.net.URL;
54 import java.net.ConnectException;
55
56 import org.exolab.jms.net.multiplexer.Endpoint;
57 import org.exolab.jms.net.uri.URI;
58 import org.exolab.jms.net.util.SSLHelper;
59
60
61 /***
62 * HTTP implementation of the {@link Endpoint} interface.
63 *
64 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
65 * @version $Revision: 1.4 $ $Date: 2005/04/17 13:41:49 $
66 */
67 class HTTPEndpoint implements Endpoint {
68
69 /***
70 * The connection to the servlet.
71 */
72 private final URL _url;
73
74 /***
75 * The connection identifier.
76 */
77 private final String _id;
78
79 /***
80 * The http URI.
81 */
82 private final URI _uri;
83
84 /***
85 * The input stream.
86 */
87 private HTTPInputStream _in;
88
89 /***
90 * The output stream.
91 */
92 private HTTPOutputStream _out;
93
94 /***
95 * The request info.
96 */
97 private HTTPRequestInfo _info;
98
99 /***
100 * The response prefix to a successfull open request.
101 */
102 private static final String OPEN_RESPONSE = "OPEN ";
103
104 /***
105 * System property to indicate the proxy server that the http protocol
106 * handler will use.
107 */
108 private static final String HTTP_PROXY_HOST = "http.proxyHost";
109
110 /***
111 * System property to indicate the proxy port that the http protocol
112 * handler will use.
113 */
114 private static final String HTTP_PROXY_PORT = "http.proxyPort";
115
116 /***
117 * System property to indicate the proxy server that the https protocol
118 * handler will use.
119 */
120 private static final String HTTPS_PROXY_HOST = "https.proxyHost";
121
122 /***
123 * System property to indicate the proxy port that the https protocol
124 * handler will use.
125 */
126 private static final String HTTPS_PROXY_PORT = "https.proxyPort";
127
128
129 /***
130 * Construct a new <code>HTTPEndpoint</code>.
131 *
132 * @param info the connection request info
133 * @param info the http request information
134 * @throws IOException for any I/O error
135 */
136 public HTTPEndpoint(HTTPRequestInfo info) throws IOException {
137 final int bufferSize = 2048;
138
139 _uri = info.getURI();
140 _url = new URL(_uri.toString());
141 _info = info;
142
143 boolean isSSL = _uri.getScheme().equals("https");
144
145 if (isSSL && info.getSSLProperties() != null) {
146 SSLHelper.configure(info.getSSLProperties());
147 }
148
149 if (_info.getProxyHost() != null) {
150
151 System.setProperty("proxySet", "true");
152
153 String hostProp = (isSSL) ? HTTPS_PROXY_HOST : HTTP_PROXY_HOST;
154 String portProp = (isSSL) ? HTTPS_PROXY_PORT : HTTP_PROXY_PORT;
155
156 try {
157 System.setProperty(hostProp, _info.getProxyHost());
158 if (_info.getProxyPort() != 0) {
159 System.setProperty(portProp, "" + _info.getProxyPort());
160 }
161 } catch (SecurityException exception) {
162 throw new ConnectException(
163 "Failed to set proxy system properties: "
164 + exception.getMessage());
165 }
166 }
167
168 HttpURLConnection connection = getConnection("open");
169 BufferedReader reader = null;
170 try {
171 reader = new BufferedReader(new InputStreamReader(
172 connection.getInputStream()));
173 String line = reader.readLine();
174 if (line == null || !line.startsWith(OPEN_RESPONSE)) {
175 throw new IOException("Invalid response returned from URL="
176 + _url + ": " + line);
177
178 }
179 _id = line.substring(line.indexOf(OPEN_RESPONSE)
180 + OPEN_RESPONSE.length());
181
182 } finally {
183 reader.close();
184 }
185 _in = new HTTPInputStream(_id, _url, info);
186 _out = new HTTPOutputStream(_id, _url, bufferSize, info);
187 }
188
189 /***
190 * Returns the URI that the endpoint is connected to.
191 *
192 * @return the URI that the endpoint is connected to
193 */
194 public URI getURI() {
195 return _uri;
196 }
197
198 /***
199 * Returns an input stream that reads from this endpoint.
200 *
201 * @return an input stream that reads from this endpoint
202 * @throws IOException if an I/O error occurs while creating the input
203 * stream.
204 */
205 public InputStream getInputStream() throws IOException {
206 return _in;
207 }
208
209 /***
210 * Returns an output stream that writes to this endpoint.
211 *
212 * @return an output stream that writes to this endpoint
213 * @throws IOException if an I/O error occurs while creating the output
214 * stream.
215 */
216 public OutputStream getOutputStream() throws IOException {
217 return _out;
218 }
219
220 /***
221 * Closes the endpoint.
222 *
223 * @throws IOException if an I/O error occurs while closing the endpoint
224 */
225 public void close() throws IOException {
226 try {
227 getConnection("close");
228 } finally {
229 _in.close();
230 _out.close();
231 }
232 }
233
234 /***
235 * Opens a connection to the tunnel servlet.
236 *
237 * @param action the action to invoke
238 * @return a connection to the tunnel servlet
239 * @throws IOException for any I/O error
240 */
241 private HttpURLConnection getConnection(String action) throws IOException {
242 return TunnelHelper.connect(_url, _id, action, _info);
243 }
244
245 }