View Javadoc
1   /**
2    * Copyright 2014 Internet2
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /*
17   * $Header: /home/hagleyj/i2mi/grouper-misc/grouperClient/src/ext/edu/internet2/middleware/grouperClientExt/org/apache/commons/httpclient/ProxyClient.java,v 1.1 2008-11-30 10:57:19 mchyzer Exp $
18   * $Revision: 1.1 $
19   * $Date: 2008-11-30 10:57:19 $
20   *
21   * ====================================================================
22   *
23   *  Licensed to the Apache Software Foundation (ASF) under one or more
24   *  contributor license agreements.  See the NOTICE file distributed with
25   *  this work for additional information regarding copyright ownership.
26   *  The ASF licenses this file to You under the Apache License, Version 2.0
27   *  (the "License"); you may not use this file except in compliance with
28   *  the License.  You may obtain a copy of the License at
29   *
30   *      http://www.apache.org/licenses/LICENSE-2.0
31   *
32   *  Unless required by applicable law or agreed to in writing, software
33   *  distributed under the License is distributed on an "AS IS" BASIS,
34   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35   *  See the License for the specific language governing permissions and
36   *  limitations under the License.
37   * ====================================================================
38   *
39   * This software consists of voluntary contributions made by many
40   * individuals on behalf of the Apache Software Foundation.  For more
41   * information on the Apache Software Foundation, please see
42   * <http://www.apache.org/>.
43   *
44   */
45  
46  package edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient;
47  
48  import java.io.IOException;
49  import java.net.Socket;
50  
51  import edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.params.HttpClientParams;
52  import edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.params.HttpConnectionManagerParams;
53  import edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.params.HttpParams;
54  
55  /**
56   * A client that provides {@link java.net.Socket sockets} for communicating through HTTP proxies
57   * via the HTTP CONNECT method.  This is primarily needed for non-HTTP protocols that wish to 
58   * communicate via an HTTP proxy.
59   * 
60   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
61   * @author Michael Becke
62   * 
63   * @since 3.0
64   * 
65   * @version $Revision: 1.1 $
66   */
67  public class ProxyClient {
68  
69      // ----------------------------------------------------- Instance Variables
70  
71      /**
72       * The {@link HttpState HTTP state} associated with this ProxyClient.
73       */
74      private HttpStateleware/grouperClientExt/org/apache/commons/httpclient/HttpState.html#HttpState">HttpState state = new HttpState();
75      
76      /**
77       * The {@link HttpClientParams collection of parameters} associated with this ProxyClient.
78       */
79      private HttpClientParams params = null; 
80  
81      /** 
82       * The {@link HostConfiguration host configuration} associated with
83       * the ProxyClient
84       */
85      private HostConfigurationExt/org/apache/commons/httpclient/HostConfiguration.html#HostConfiguration">HostConfiguration hostConfiguration = new HostConfiguration();
86      
87      /**
88       * Creates an instance of ProxyClient using default {@link HttpClientParams parameter set}.
89       * 
90       * @see HttpClientParams
91       */
92      public ProxyClient() {
93          this(new HttpClientParams());
94      }
95  
96      /**
97       * Creates an instance of ProxyClient using the given 
98       * {@link HttpClientParams parameter set}.
99       * 
100      * @param params The {@link HttpClientParams parameters} to use.
101      * 
102      * @see HttpClientParams
103      */
104     public ProxyClient(HttpClientParams params) {
105         super();
106         if (params == null) {
107             throw new IllegalArgumentException("Params may not be null");  
108         }
109         this.params = params;
110     }
111 
112     // ------------------------------------------------------------- Properties
113 
114     /**
115      * Returns {@link HttpState HTTP state} associated with the ProxyClient.
116      *
117      * @see #setState(HttpState)
118      * @return the shared client state
119      */
120     public synchronized HttpState getState() {
121         return state;
122     }
123 
124     /**
125      * Assigns {@link HttpState HTTP state} for the ProxyClient.
126      *
127      * @see #getState()
128      * @param state the new {@link HttpState HTTP state} for the client
129      */
130     public synchronized void setState(HttpState state) {
131         this.state = state;
132     }
133 
134     /**
135      * Returns the {@link HostConfiguration host configuration} associated with the 
136      * ProxyClient.
137      * 
138      * @return {@link HostConfiguration host configuration}
139      */
140     public synchronized HostConfiguration getHostConfiguration() {
141         return hostConfiguration;
142     }
143 
144     /**
145      * Assigns the {@link HostConfiguration host configuration} to use with the
146      * ProxyClient.
147      * 
148      * @param hostConfiguration The {@link HostConfiguration host configuration} to set
149      */
150     public synchronized void setHostConfiguration(HostConfiguration hostConfiguration) {
151         this.hostConfiguration = hostConfiguration;
152     }
153 
154     /**
155      * Returns {@link HttpClientParams HTTP protocol parameters} associated with this ProxyClient.
156      * 
157      * @see HttpClientParams
158      */
159     public synchronized HttpClientParams getParams() {
160         return this.params;
161     }
162 
163     /**
164      * Assigns {@link HttpClientParams HTTP protocol parameters} for this ProxyClient.
165      * 
166      * @see HttpClientParams
167      */
168     public synchronized void setParams(final HttpClientParams params) {
169         if (params == null) {
170             throw new IllegalArgumentException("Parameters may not be null");
171         }
172         this.params = params;
173     }
174 
175     /**
176      * Creates a socket that is connected, via the HTTP CONNECT method, to a proxy.
177      * 
178      * <p>
179      * Even though HTTP CONNECT proxying is generally used for HTTPS tunneling, the returned
180      * socket will not have been wrapped in an SSL socket.
181      * </p>
182      * 
183      * <p>
184      * Both the proxy and destination hosts must be set via the 
185      * {@link #getHostConfiguration() host configuration} prior to calling this method.
186      * </p>
187      * 
188      * @return the connect response
189      * 
190      * @throws IOException
191      * @throws HttpException
192      * 
193      * @see #getHostConfiguration()
194      */
195     public ConnectResponse connect() throws IOException, HttpException {
196         
197         HostConfiguration hostconf = getHostConfiguration();
198         if (hostconf.getProxyHost() == null) {
199             throw new IllegalStateException("proxy host must be configured");
200         }
201         if (hostconf.getHost() == null) {
202             throw new IllegalStateException("destination host must be configured");
203         }
204         if (hostconf.getProtocol().isSecure()) {
205             throw new IllegalStateException("secure protocol socket factory may not be used");
206         }
207         
208         ConnectMethode/grouperClientExt/org/apache/commons/httpclient/ConnectMethod.html#ConnectMethod">ConnectMethod method = new ConnectMethod(getHostConfiguration());
209         method.getParams().setDefaults(getParams());
210         
211         DummyConnectionManager connectionManager = new DummyConnectionManager();
212         connectionManager.setConnectionParams(getParams());
213         
214         HttpMethodDirectorerClientExt/org/apache/commons/httpclient/HttpMethodDirector.html#HttpMethodDirector">HttpMethodDirector director = new HttpMethodDirector(
215             connectionManager,
216             hostconf,
217             getParams(),
218             getState()
219         );
220         
221         director.executeMethod(method);
222         
223         ConnectResponse response = new ConnectResponse();
224         response.setConnectMethod(method);
225         
226         // only set the socket if the connect was successful
227         if (method.getStatusCode() == HttpStatus.SC_OK) {
228             response.setSocket(connectionManager.getConnection().getSocket());
229         } else {
230             connectionManager.getConnection().close();
231         }
232         
233         return response;
234     }
235 
236     /**
237      * Contains the method used to execute the connect along with the created socket.
238      */
239     public static class ConnectResponse {
240         
241         private ConnectMethod connectMethod;
242         
243         private Socket socket;
244         
245         private ConnectResponse() {}
246         
247         /**
248          * Gets the method that was used to execute the connect.  This method is useful for 
249          * analyzing the proxy's response when a connect fails.
250          * 
251          * @return the connectMethod.
252          */
253         public ConnectMethod getConnectMethod() {
254             return connectMethod;
255         }
256         /**
257          * @param connectMethod The connectMethod to set.
258          */
259         private void setConnectMethod(ConnectMethod connectMethod) {
260             this.connectMethod = connectMethod;
261         }
262         /**
263          * Gets the socket connected and authenticated (if appropriate) to the configured
264          * HTTP proxy, or <code>null</code> if a connection could not be made.  It is the
265          * responsibility of the user to close this socket when it is no longer needed.
266          * 
267          * @return the socket.
268          */
269         public Socket getSocket() {
270             return socket;
271         }
272         /**
273          * @param socket The socket to set.
274          */
275         private void setSocket(Socket socket) {
276             this.socket = socket;
277         }
278     }
279     
280     /**
281      * A connection manager that creates a single connection.  Meant to be used only once.
282      */
283     static class DummyConnectionManager implements HttpConnectionManager {
284 
285         private HttpConnection httpConnection;
286         
287         private HttpParams connectionParams;
288         
289         public void closeIdleConnections(long idleTimeout) {
290         }
291 
292         public HttpConnection getConnection() {
293             return httpConnection;
294         }
295 
296         public void setConnectionParams(HttpParams httpParams) {
297             this.connectionParams = httpParams;
298         }
299 
300         public HttpConnection getConnectionWithTimeout(
301             HostConfiguration hostConfiguration, long timeout) {
302 
303             httpConnection = new HttpConnection(hostConfiguration);
304             httpConnection.setHttpConnectionManager(this);
305             httpConnection.getParams().setDefaults(connectionParams);
306             return httpConnection;
307         }        
308         
309         /**
310          * @deprecated
311          */
312         public HttpConnection getConnection(HostConfiguration hostConfiguration, long timeout)
313             throws HttpException {
314             return getConnectionWithTimeout(hostConfiguration, timeout);
315         }
316         
317         public HttpConnection getConnection(HostConfiguration hostConfiguration) {
318             return getConnectionWithTimeout(hostConfiguration, -1);
319         }
320     
321         public void releaseConnection(HttpConnection conn) {
322         }
323 
324         public HttpConnectionManagerParams getParams() {
325             return null;
326         }
327 
328         public void setParams(HttpConnectionManagerParams params) {
329         }
330     }
331 }