View Javadoc

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: AbstractHTTPManagedConnection.java,v 1.7 2006/12/16 12:37:17 tanderson Exp $
44   */
45  package org.exolab.jms.net.http;
46  
47  import java.io.IOException;
48  import java.net.Socket;
49  import java.security.Principal;
50  
51  import org.exolab.jms.common.uuid.UUIDGenerator;
52  import org.exolab.jms.net.connector.Authenticator;
53  import org.exolab.jms.net.connector.ConnectException;
54  import org.exolab.jms.net.connector.ResourceException;
55  import org.exolab.jms.net.connector.SecurityException;
56  import org.exolab.jms.net.multiplexer.Endpoint;
57  import org.exolab.jms.net.multiplexer.MultiplexedManagedConnection;
58  import org.exolab.jms.net.multiplexer.Multiplexer;
59  import org.exolab.jms.net.socket.SocketEndpoint;
60  import org.exolab.jms.net.uri.InvalidURIException;
61  import org.exolab.jms.net.uri.URI;
62  import org.exolab.jms.net.uri.URIHelper;
63  import org.apache.commons.logging.Log;
64  import org.apache.commons.logging.LogFactory;
65  
66  
67  /***
68   * <code>AbstractHTTPManagedConnection</code> manages multiple
69   * <code>Connection</code> instances over a single HTTP connection.
70   *
71   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
72   * @version $Revision: 1.7 $ $Date: 2006/12/16 12:37:17 $
73   */
74  abstract class AbstractHTTPManagedConnection
75          extends MultiplexedManagedConnection {
76  
77      /***
78       * The endpoint.
79       */
80      protected Endpoint _endpoint;
81  
82      /***
83       * The remote address to which this is connected.
84       */
85      private URI _remoteURI;
86  
87      /***
88       * The local address that this connection is bound to.
89       */
90      private URI _localURI;
91  
92      /***
93       * The logger.
94       */
95      private static final Log _log = LogFactory.getLog(
96              AbstractHTTPManagedConnection.class);
97  
98  
99      /***
100      * Construct a new client <code>HTTPManagedConnection</code>.
101      *
102      * @param principal the security principal.
103      * @param info      the connection request info
104      * @throws ResourceException if a socket cannot be created
105      */
106     public AbstractHTTPManagedConnection(Principal principal,
107                                          HTTPRequestInfo info)
108             throws ResourceException {
109         super(principal);
110         if (info == null) {
111             throw new IllegalArgumentException("Argument 'info' is null");
112         }
113         final URI uri = info.getURI();
114         try {
115             _endpoint = new HTTPEndpoint(info);
116         } catch (IOException exception) {
117             _log.debug(exception, exception);
118             throw new ConnectException("Failed to connect to URI="
119                                        + info.getURI(), exception);
120         }
121         _remoteURI = URIHelper.convertHostToAddress(uri);
122 
123         try {
124             _localURI = URIHelper.create(uri.getScheme(), null, -1,
125                                          UUIDGenerator.create());
126         } catch (InvalidURIException exception) {
127             _log.debug(exception, exception);
128              throw new ResourceException("Failed to generate local URI",
129                                         exception);
130         }
131 
132     }
133 
134     /***
135      * Construct a new server <code>HTTPManagedConnection</code>.
136      *
137      * @param uri           the URI the acceptor was listening on
138      * @param socket        the socket
139      * @param authenticator the connection authenticator
140      * @throws ResourceException if an error occurs accessing the socket
141      */
142     public AbstractHTTPManagedConnection(URI uri, Socket socket,
143                                          Authenticator authenticator)
144         throws ResourceException {
145         super(authenticator);
146         if (uri == null) {
147             throw new IllegalArgumentException("Argument 'uri' is null");
148         }
149         if (socket == null) {
150             throw new IllegalArgumentException("Argument 'socket' is null");
151         }
152         if (authenticator == null) {
153             throw new IllegalArgumentException(
154                     "Argument 'authenticator' is null");
155         }
156         String scheme = uri.getScheme();
157         int localPort = socket.getLocalPort();
158 
159         try {
160             _localURI = URIHelper.create(scheme, uri.getHost(), localPort);
161         } catch (InvalidURIException exception) {
162             _log.debug(exception, exception);
163              throw new ResourceException("Failed to generate local URI",
164                                         exception);
165         }
166         try {
167             _endpoint = new SocketEndpoint(scheme, socket);
168         } catch (IOException exception) {
169             _log.debug(exception, exception);
170              throw new ResourceException("Failed to create endpoint", exception);
171         }
172     }
173 
174     /***
175      * Returns the remote address to which this is connected.
176      *
177      * @return the remote address to which this is connected
178      */
179     public URI getRemoteURI() {
180         return _remoteURI;
181     }
182 
183     /***
184      * Returns the local address that this connection is bound to.
185      *
186      * @return the local address that this connection is bound to
187      */
188     public URI getLocalURI() {
189         return _localURI;
190     }
191 
192     /***
193      * Returns the endpoint to multiplex data over.
194      *
195      * @return the endpoint to multiplex data over
196      * @throws IOException for any I/O error
197      */
198     protected Endpoint createEndpoint() throws IOException {
199         return _endpoint;
200     }
201 
202     /***
203      * Create a new client-side multiplexer.
204      *
205      * @param endpoint  the endpoint to multiplex messages over
206      * @param principal the security principal
207      * @return a new client-side multiplexer
208      * @throws IOException       if an I/O error occurs
209      * @throws SecurityException if connection is refused by the server
210      */
211     protected Multiplexer createMultiplexer(Endpoint endpoint,
212                                             Principal principal)
213             throws IOException, SecurityException {
214         return new HTTPMultiplexer(this, endpoint, _localURI, principal);
215     }
216 
217     /***
218      * Create a new server-side multiplexer.
219      *
220      * @param endpoint      the endpoint to multiplex messages over
221      * @param authenticator the connection authetnicator
222      * @throws IOException       if an I/O error occurs
223      * @throws ResourceException if the authenticator cannot authenticate
224      * @return a new server-side multiplexer
225      */
226     protected Multiplexer createMultiplexer(Endpoint endpoint,
227                                             Authenticator authenticator)
228             throws IOException, ResourceException {
229         HTTPMultiplexer result = new HTTPMultiplexer(this, endpoint,
230                                                      authenticator);
231         _remoteURI = result.getClientURI();
232         return result;
233     }
234 
235 }