1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/NoHostHttpConnectionManager.java,v 1.6 2004/04/25 21:49:35 mbecke Exp $
3    * $Revision: 1.6 $
4    * $Date: 2004/04/25 21:49:35 $
5    *
6    * ====================================================================
7    *
8    *  Copyright 2002-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   * [Additional notices, if required by prior licensing conditions]
29   *
30   */
31   
32  package org.apache.commons.httpclient;
33  
34  import java.io.IOException;
35  import java.io.InputStream;
36  
37  import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
38  
39  /***
40   */
41  public class NoHostHttpConnectionManager implements HttpConnectionManager {
42  
43      private HttpConnection connection;
44      
45      private boolean connectionReleased = false;
46      
47      private HttpConnectionManagerParams params = new HttpConnectionManagerParams(); 
48      
49      public NoHostHttpConnectionManager() { 
50          setConnection(new SimpleHttpConnection());
51      }
52  
53      /***
54       * This method currently does nothing.
55       */
56      public void closeIdleConnections(long idleTimeout) {
57      }
58  
59      /***
60       * @return
61       */
62      public boolean isConnectionReleased() {
63          return connectionReleased;
64      }
65      
66      /***
67       * @param connection
68       */
69      public void setConnection(HttpConnection connection) {
70          this.connection = connection;
71          connection.setHttpConnectionManager(this);
72          connection.getParams().setDefaults(this.params);
73      }
74  
75      public HttpConnection getConnection(HostConfiguration hostConfiguration) {
76          
77          // make sure the host and proxy are correct for this connection
78          // close it and set the values if they are not
79          if (!hostConfiguration.hostEquals(connection)
80              || !hostConfiguration.proxyEquals(connection)) {
81                  
82              if (connection.isOpen()) {
83                  connection.close();
84              }
85  
86              connection.setHost(hostConfiguration.getHost());
87              connection.setVirtualHost(hostConfiguration.getVirtualHost());
88              connection.setPort(hostConfiguration.getPort());
89              connection.setProtocol(hostConfiguration.getProtocol());
90              connection.setLocalAddress(hostConfiguration.getLocalAddress());
91  
92              connection.setProxyHost(hostConfiguration.getProxyHost());
93              connection.setProxyPort(hostConfiguration.getProxyPort());
94          } else {
95              finishLastResponse(connection);
96          }
97          
98          connectionReleased = false;
99          return connection;
100     }
101 
102     /***
103      * @deprecated
104      */
105     public HttpConnection getConnection(HostConfiguration hostConfiguration, long timeout)
106         throws HttpException {
107         return getConnection(hostConfiguration);
108     }
109 
110     public HttpConnection getConnectionWithTimeout(
111         HostConfiguration hostConfiguration,
112         long timeout)
113         throws ConnectTimeoutException {
114         return getConnection(hostConfiguration);
115     }
116 
117     public void releaseConnection(HttpConnection conn) {
118         if (conn != connection) {
119             throw new IllegalStateException("Unexpected close on a different connection.");
120         }
121         
122         connectionReleased = true;
123         finishLastResponse(connection);
124     }
125 
126     /***
127      * Since the same connection is about to be reused, make sure the
128      * previous request was completely processed, and if not
129      * consume it now.
130      * @param conn The connection
131      */
132     static void finishLastResponse(HttpConnection conn) {
133         InputStream lastResponse = conn.getLastResponseInputStream();
134         if (lastResponse != null) {
135             conn.setLastResponseInputStream(null);
136             try {
137                 lastResponse.close();
138             } catch (IOException ioe) {
139                 //FIXME: badness - close to force reconnect.
140                 conn.close();
141             }
142         }
143     }
144 
145     public HttpConnectionManagerParams getParams() {
146         return this.params;
147     }
148 
149     public void setParams(final HttpConnectionManagerParams params) {
150         if (params == null) {
151             throw new IllegalArgumentException("Parameters may not be null");
152         }
153         this.params = params;
154     }
155 
156 }