1069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/* 2069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java $ 3069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Revision: 658775 $ 4069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Date: 2008-05-21 10:30:45 -0700 (Wed, 21 May 2008) $ 5069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 6069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ==================================================================== 7069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 8069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Licensed to the Apache Software Foundation (ASF) under one or more 9069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * contributor license agreements. See the NOTICE file distributed with 10069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * this work for additional information regarding copyright ownership. 11069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * The ASF licenses this file to You under the Apache License, Version 2.0 12069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * (the "License"); you may not use this file except in compliance with 13069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * the License. You may obtain a copy of the License at 14069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 15069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 16069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 17069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 18069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 19069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * See the License for the specific language governing permissions and 21069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * limitations under the License. 22069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ==================================================================== 23069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 24069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This software consists of voluntary contributions made by many 25069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * individuals on behalf of the Apache Software Foundation. For more 26069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * information on the Apache Software Foundation, please see 27069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <http://www.apache.org/>. 28069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 29069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 30069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 31069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpackage org.apache.http.impl.conn; 32069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 33069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 34069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.io.IOException; 35069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 36069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.HttpHost; 37069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.params.HttpParams; 38069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.protocol.HttpContext; 39069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.conn.routing.HttpRoute; 40069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.conn.ClientConnectionManager; 41069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.conn.OperatedClientConnection; 42069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 43069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 44069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 45069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/** 46069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Abstract adapter from pool {@link AbstractPoolEntry entries} to 47069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * {@link org.apache.http.conn.ManagedClientConnection managed} 48069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * client connections. 49069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * The connection in the pool entry is used to initialize the base class. 50069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * In addition, methods to establish a route are delegated to the 51069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * pool entry. {@link #shutdown shutdown} and {@link #close close} 52069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * will clear the tracked route in the pool entry and call the 53069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * respective method of the wrapped connection. 54069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 55069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:rolandw at apache.org">Roland Weber</a> 56069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 57069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 58069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <!-- empty lines to avoid svn diff problems --> 59069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @version $Revision: 658775 $ $Date: 2008-05-21 10:30:45 -0700 (Wed, 21 May 2008) $ 60069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 61069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @since 4.0 62d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath * 63d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath * @deprecated Please use {@link java.net.URL#openConnection} instead. 64d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath * Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a> 65d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath * for further details. 66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 67d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath@Deprecated 68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic abstract class AbstractPooledConnAdapter extends AbstractClientConnAdapter { 69069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 70069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** The wrapped pool entry. */ 71069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project protected volatile AbstractPoolEntry poolEntry; 72069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 74069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 75069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Creates a new connection adapter. 76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param manager the connection manager 78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param entry the pool entry for the connection being wrapped 79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project protected AbstractPooledConnAdapter(ClientConnectionManager manager, 81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project AbstractPoolEntry entry) { 82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project super(manager, entry.connection); 83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project this.poolEntry = entry; 84069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 85069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 86069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 87069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 88069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Asserts that this adapter is still attached. 89069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 90069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @throws IllegalStateException 91069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * if it is {@link #detach detach}ed 92069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 93069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project protected final void assertAttached() { 94069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (poolEntry == null) { 95069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project throw new IllegalStateException("Adapter is detached."); 96069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 97069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 98069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 99069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 100069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Detaches this adapter from the wrapped connection. 101069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This adapter becomes useless. 102069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 103069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project @Override 104069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project protected void detach() { 105069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project super.detach(); 106069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project poolEntry = null; 107069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 108069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 109069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 110069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // non-javadoc, see interface ManagedHttpConnection 111069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public HttpRoute getRoute() { 112069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 113069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project assertAttached(); 114069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return (poolEntry.tracker == null) ? 115069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project null : poolEntry.tracker.toRoute(); 116069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 117069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 118069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // non-javadoc, see interface ManagedHttpConnection 119069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public void open(HttpRoute route, 120069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project HttpContext context, HttpParams params) 121069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project throws IOException { 122069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 123069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project assertAttached(); 124069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project poolEntry.open(route, context, params); 125069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 126069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 127069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 128069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // non-javadoc, see interface ManagedHttpConnection 129069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public void tunnelTarget(boolean secure, HttpParams params) 130069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project throws IOException { 131069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 132069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project assertAttached(); 133069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project poolEntry.tunnelTarget(secure, params); 134069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 135069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 136069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 137069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // non-javadoc, see interface ManagedHttpConnection 138069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public void tunnelProxy(HttpHost next, boolean secure, HttpParams params) 139069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project throws IOException { 140069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 141069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project assertAttached(); 142069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project poolEntry.tunnelProxy(next, secure, params); 143069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 144069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 145069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 146069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // non-javadoc, see interface ManagedHttpConnection 147069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public void layerProtocol(HttpContext context, HttpParams params) 148069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project throws IOException { 149069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 150069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project assertAttached(); 151069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project poolEntry.layerProtocol(context, params); 152069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 153069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 154069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 155069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 156069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // non-javadoc, see interface HttpConnection 157069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public void close() throws IOException { 158069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (poolEntry != null) 159069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project poolEntry.shutdownEntry(); 160069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 161069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project OperatedClientConnection conn = getWrappedConnection(); 162069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (conn != null) { 163069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project conn.close(); 164069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 165069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 166069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 167069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // non-javadoc, see interface HttpConnection 168069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public void shutdown() throws IOException { 169069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (poolEntry != null) 170069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project poolEntry.shutdownEntry(); 171069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 172069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project OperatedClientConnection conn = getWrappedConnection(); 173069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (conn != null) { 174069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project conn.shutdown(); 175069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 176069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 177069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 178069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 179069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // non-javadoc, see interface ManagedClientConnection 180069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public Object getState() { 181069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project assertAttached(); 182069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return poolEntry.getState(); 183069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 184069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 185069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 186069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // non-javadoc, see interface ManagedClientConnection 187069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public void setState(final Object state) { 188069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project assertAttached(); 189069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project poolEntry.setState(state); 190069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 191069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 192069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 193069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project} // class AbstractPooledConnAdapter 194