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: HTTPRequestInfo.java,v 1.5 2005/05/03 13:45:58 tanderson Exp $
44 */
45 package org.exolab.jms.net.http;
46
47 import org.exolab.jms.net.connector.ConnectionRequestInfo;
48 import org.exolab.jms.net.connector.URIRequestInfo;
49 import org.exolab.jms.net.connector.ResourceException;
50 import org.exolab.jms.net.uri.URI;
51 import org.exolab.jms.net.util.SSLProperties;
52 import org.exolab.jms.net.util.Properties;
53
54
55 /***
56 * Implementation of the {@link ConnectionRequestInfo} interface that enables
57 * the HTTP connector to pass data across the connection request flow.
58 *
59 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
60 * @version $Revision: 1.5 $ $Date: 2005/05/03 13:45:58 $
61 */
62 public class HTTPRequestInfo extends URIRequestInfo {
63
64 /***
65 * The proxy host, if the client needs to connect via a proxy.
66 */
67 private String _proxyHost;
68
69 /***
70 * The proxy port, if the client needs to connect via a proxy.
71 */
72 private int _proxyPort;
73
74 /***
75 * The proxy user, if the client needs to log in to the proxy.
76 */
77 private String _proxyUser;
78
79 /***
80 * The proxy password, if the client needs to log in to the proxy.
81 */
82 private String _proxyPassword;
83
84 /***
85 * Properties to configure the secure socket layer. May be
86 * <code>null</code>.
87 */
88 private SSLProperties _sslProperties;
89
90 /***
91 * Connection property to indicate the proxy server to use.
92 */
93 private static final String PROXY_HOST = "proxyHost";
94
95 /***
96 * Connection property to indicate the proxy port to use.
97 */
98 private static final String PROXY_PORT = "proxyPort";
99
100 /***
101 * Connection property to indicate the proxy user to use.
102 */
103 private static final String PROXY_USER = "proxyUser";
104
105 /***
106 * Connection property to indicate the proxy password to use.
107 */
108 private static final String PROXY_PASSWORD = "proxyPassword";
109
110
111 /***
112 * Construct a new <code>HTTPRequestInfo</code>.
113 *
114 * @param uri the URI
115 */
116 public HTTPRequestInfo(URI uri) {
117 super(uri);
118 }
119
120 /***
121 * Construct a new <code>HTTPRequestInfo</code>.
122 *
123 * @param uri the URI
124 * @param properties the properties to populate this from
125 * @throws ResourceException if any of the properties are invalid
126 */
127 public HTTPRequestInfo(URI uri, Properties properties)
128 throws ResourceException {
129 super(uri);
130 setProxyHost(properties.get(PROXY_HOST));
131 setProxyPort(properties.getInt(PROXY_PORT, 0));
132 setProxyUser(properties.get(PROXY_USER));
133 setProxyPassword(properties.get(PROXY_PASSWORD));
134
135 SSLProperties ssl = new SSLProperties(properties);
136 if (!ssl.isEmpty()) {
137 setSSLProperties(ssl);
138 }
139 }
140
141 /***
142 * Sets the proxy host.
143 *
144 * @param host the proxy host
145 */
146 public void setProxyHost(String host) {
147 _proxyHost = host;
148 }
149
150 /***
151 * Returns the proxy host.
152 *
153 * @return the proxy host, or <code>null</code> if none is set
154 */
155 public String getProxyHost() {
156 return _proxyHost;
157 }
158
159 /***
160 * Sets the proxy port.
161 *
162 * @param port the proxy port
163 */
164 public void setProxyPort(int port) {
165 _proxyPort = port;
166 }
167
168 /***
169 * Returns the proxy port.
170 *
171 * @return the proxy port, or <code>0</code> if none is set
172 */
173 public int getProxyPort() {
174 return _proxyPort;
175 }
176
177 /***
178 * Sets the proxy user.
179 *
180 * @param user the proxy user
181 */
182 public void setProxyUser(String user) {
183 _proxyUser = user;
184 }
185
186 /***
187 * Returns the proxy user.
188 *
189 * @return the proxy user, or <code>null</code> if none is set
190 */
191 public String getProxyUser() {
192 return _proxyUser;
193 }
194
195 /***
196 * Sets the proxy password.
197 *
198 * @param pwd the proxy password
199 */
200 public void setProxyPassword(String pwd) {
201 _proxyPassword = pwd;
202 }
203
204 /***
205 * Returns the proxy password.
206 *
207 * @return the proxy password, or <code>null</code> if none is set
208 */
209 public String getProxyPassword() {
210 return _proxyPassword;
211 }
212
213 /***
214 * Returns the properties used to configure the secure socket layer (SSL).
215 *
216 * @return the SSL configuration properties, or <code>null</code> if unset
217 */
218 public SSLProperties getSSLProperties() {
219 return _sslProperties;
220 }
221
222 /***
223 * Sets the properties used to configure the secure socket layer (SSL).
224 *
225 * @param properties the SSL configuration properties
226 */
227 public void setSSLProperties(SSLProperties properties) {
228 _sslProperties = properties;
229 }
230
231 /***
232 * Helper to export this to a {@link Properties} instance.
233 *
234 * @param properties the properties to export to.
235 */
236 public void export(Properties properties) {
237 super.export(properties);
238 properties.setNonNull(PROXY_HOST, getProxyHost());
239 properties.set(PROXY_PORT, getProxyPort());
240 properties.setNonNull(PROXY_USER, getProxyUser());
241 properties.setNonNull(PROXY_PASSWORD, getProxyPassword());
242
243 SSLProperties ssl = getSSLProperties();
244 if (ssl != null) {
245 ssl.export(properties);
246 }
247 }
248
249 /***
250 * Checks whether this instance is equal to another.
251 *
252 * @param other the object to compare
253 * @return <code>true</code> if the two instances are equal; otherwise
254 * <code>false</code>
255 */
256 public boolean equals(Object other) {
257 boolean equal = false;
258 if (other instanceof HTTPRequestInfo && super.equals(other)) {
259 HTTPRequestInfo info = (HTTPRequestInfo) other;
260 if (equals(_proxyHost, info._proxyHost)
261 && (_proxyPort == info._proxyPort)
262 && equals(_proxyUser, info._proxyUser)
263 && equals(_proxyPassword, info._proxyPassword)
264 && equals(_sslProperties, info._sslProperties)) {
265 equal = true;
266 }
267 }
268 return equal;
269 }
270
271 /***
272 * Helper to compare two objects for equality.
273 *
274 * @param o1 the first object to compare
275 * @param o2 the second object to compare
276 * @return <code>true</code> if the objects are equal, otherwise
277 * <code>false</code>
278 */
279 private boolean equals(Object o1, Object o2) {
280 boolean equal = (o1 == null && o2 == null);
281 if (!equal) {
282 if (o1 != null && o1.equals(o2)) {
283 equal = true;
284 }
285 }
286 return equal;
287 }
288
289 }