001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/NoHostHttpConnectionManager.java,v 1.9 2004/10/31 13:53:03 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: * [Additional notices, if required by prior licensing conditions]
030: *
031: */
032:
033: package org.apache.commons.httpclient;
034:
035: import java.io.IOException;
036: import java.io.InputStream;
037:
038: import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
039:
040: /**
041: */
042: public class NoHostHttpConnectionManager implements
043: HttpConnectionManager {
044:
045: private HttpConnection connection;
046:
047: private boolean connectionReleased = false;
048:
049: private HttpConnectionManagerParams params = new HttpConnectionManagerParams();
050:
051: public NoHostHttpConnectionManager() {
052: super ();
053: }
054:
055: /**
056: * This method currently does nothing.
057: */
058: public void closeIdleConnections(long idleTimeout) {
059: }
060:
061: /**
062: * @return
063: */
064: public boolean isConnectionReleased() {
065: return connectionReleased;
066: }
067:
068: /**
069: * @param connection
070: */
071: public void setConnection(HttpConnection connection) {
072: this .connection = connection;
073: connection.setHttpConnectionManager(this );
074: connection.getParams().setDefaults(this .params);
075: }
076:
077: public HttpConnection getConnection(
078: HostConfiguration hostConfiguration) {
079:
080: // make sure the host and proxy are correct for this connection
081: // close it and set the values if they are not
082: if (!hostConfiguration.hostEquals(connection)
083: || !hostConfiguration.proxyEquals(connection)) {
084:
085: if (connection.isOpen()) {
086: connection.close();
087: }
088:
089: connection.setHost(hostConfiguration.getHost());
090: connection.setPort(hostConfiguration.getPort());
091: connection.setProtocol(hostConfiguration.getProtocol());
092: connection.setLocalAddress(hostConfiguration
093: .getLocalAddress());
094:
095: connection.setProxyHost(hostConfiguration.getProxyHost());
096: connection.setProxyPort(hostConfiguration.getProxyPort());
097: } else {
098: finishLastResponse(connection);
099: }
100:
101: connectionReleased = false;
102: return connection;
103: }
104:
105: /**
106: * @deprecated
107: */
108: public HttpConnection getConnection(
109: HostConfiguration hostConfiguration, long timeout)
110: throws HttpException {
111: return getConnection(hostConfiguration);
112: }
113:
114: public HttpConnection getConnectionWithTimeout(
115: HostConfiguration hostConfiguration, long timeout)
116: throws ConnectionPoolTimeoutException {
117: return getConnection(hostConfiguration);
118: }
119:
120: public void releaseConnection(HttpConnection conn) {
121: if (conn != connection) {
122: throw new IllegalStateException(
123: "Unexpected close on a different connection.");
124: }
125:
126: connectionReleased = true;
127: finishLastResponse(connection);
128: }
129:
130: /**
131: * Since the same connection is about to be reused, make sure the
132: * previous request was completely processed, and if not
133: * consume it now.
134: * @param conn The connection
135: */
136: static void finishLastResponse(HttpConnection conn) {
137: InputStream lastResponse = conn.getLastResponseInputStream();
138: if (lastResponse != null) {
139: conn.setLastResponseInputStream(null);
140: try {
141: lastResponse.close();
142: } catch (IOException ioe) {
143: //FIXME: badness - close to force reconnect.
144: conn.close();
145: }
146: }
147: }
148:
149: public HttpConnectionManagerParams getParams() {
150: return this .params;
151: }
152:
153: public void setParams(final HttpConnectionManagerParams params) {
154: if (params == null) {
155: throw new IllegalArgumentException(
156: "Parameters may not be null");
157: }
158: this.params = params;
159: }
160:
161: }
|