Home | History | Annotate | Download | only in conn
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/BasicManagedEntity.java $
      3  * $Revision $
      4  * $Date: 2008-06-27 12:49:20 -0700 (Fri, 27 Jun 2008) $
      5  *
      6  * ====================================================================
      7  *
      8  *  Licensed to the Apache Software Foundation (ASF) under one or more
      9  *  contributor license agreements.  See the NOTICE file distributed with
     10  *  this work for additional information regarding copyright ownership.
     11  *  The ASF licenses this file to You under the Apache License, Version 2.0
     12  *  (the "License"); you may not use this file except in compliance with
     13  *  the License.  You may obtain a copy of the License at
     14  *
     15  *      http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  *  Unless required by applicable law or agreed to in writing, software
     18  *  distributed under the License is distributed on an "AS IS" BASIS,
     19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     20  *  See the License for the specific language governing permissions and
     21  *  limitations under the License.
     22  * ====================================================================
     23  *
     24  * This software consists of voluntary contributions made by many
     25  * individuals on behalf of the Apache Software Foundation.  For more
     26  * information on the Apache Software Foundation, please see
     27  * <http://www.apache.org/>.
     28  *
     29  */
     30 
     31 package org.apache.http.conn;
     32 
     33 import java.io.IOException;
     34 import java.io.InputStream;
     35 import java.io.OutputStream;
     36 
     37 import org.apache.http.HttpEntity;
     38 import org.apache.http.entity.HttpEntityWrapper;
     39 
     40 
     41 /**
     42  * An entity that releases a {@link ManagedClientConnection connection}.
     43  * A {@link ManagedClientConnection} will
     44  * typically <i>not</i> return a managed entity, but you can replace
     45  * the unmanaged entity in the response with a managed one.
     46  *
     47  * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
     48  *
     49  *
     50  * <!-- empty lines to avoid svn diff problems -->
     51  * @version $Revision: 672367 $
     52  *
     53  * @since 4.0
     54  *
     55  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     56  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     57  *     for further details.
     58  */
     59 @Deprecated
     60 public class BasicManagedEntity extends HttpEntityWrapper
     61     implements ConnectionReleaseTrigger, EofSensorWatcher {
     62 
     63     /** The connection to release. */
     64     protected ManagedClientConnection managedConn;
     65 
     66     /** Whether to keep the connection alive. */
     67     protected final boolean attemptReuse;
     68 
     69 
     70     /**
     71      * Creates a new managed entity that can release a connection.
     72      *
     73      * @param entity    the entity of which to wrap the content.
     74      *                  Note that the argument entity can no longer be used
     75      *                  afterwards, since the content will be taken by this
     76      *                  managed entity.
     77      * @param conn      the connection to release
     78      * @param reuse     whether the connection should be re-used
     79      */
     80     public BasicManagedEntity(HttpEntity entity,
     81                               ManagedClientConnection conn,
     82                               boolean reuse) {
     83         super(entity);
     84 
     85         if (conn == null)
     86             throw new IllegalArgumentException
     87                 ("Connection may not be null.");
     88 
     89         this.managedConn = conn;
     90         this.attemptReuse = reuse;
     91     }
     92 
     93 
     94     // non-javadoc, see interface HttpEntity
     95     @Override
     96     public boolean isRepeatable() {
     97         return false;
     98     }
     99 
    100 
    101     // non-javadoc, see interface HttpEntity
    102     @Override
    103     public InputStream getContent() throws IOException {
    104 
    105         return new EofSensorInputStream(wrappedEntity.getContent(), this);
    106     }
    107 
    108 
    109     // non-javadoc, see interface HttpEntity
    110     @Override
    111     public void consumeContent() throws IOException {
    112 
    113         if (managedConn == null)
    114             return;
    115 
    116         try {
    117             if (attemptReuse) {
    118                 // this will not trigger a callback from EofSensorInputStream
    119                 wrappedEntity.consumeContent();
    120                 managedConn.markReusable();
    121             }
    122         } finally {
    123             releaseManagedConnection();
    124         }
    125     }
    126 
    127 
    128     // non-javadoc, see interface HttpEntity
    129     @Override
    130     public void writeTo(final OutputStream outstream) throws IOException {
    131         super.writeTo(outstream);
    132         consumeContent();
    133     }
    134 
    135 
    136     // non-javadoc, see interface ConnectionReleaseTrigger
    137     public void releaseConnection()
    138         throws IOException {
    139 
    140         this.consumeContent();
    141     }
    142 
    143 
    144     // non-javadoc, see interface ConnectionReleaseTrigger
    145     public void abortConnection()
    146         throws IOException {
    147 
    148         if (managedConn != null) {
    149             try {
    150                 managedConn.abortConnection();
    151             } finally {
    152                 managedConn = null;
    153             }
    154         }
    155     }
    156 
    157 
    158     // non-javadoc, see interface EofSensorWatcher
    159     public boolean eofDetected(InputStream wrapped)
    160         throws IOException {
    161 
    162         try {
    163             if (attemptReuse && (managedConn != null)) {
    164                 // there may be some cleanup required, such as
    165                 // reading trailers after the response body:
    166                 wrapped.close();
    167                 managedConn.markReusable();
    168             }
    169         } finally {
    170             releaseManagedConnection();
    171         }
    172         return false;
    173     }
    174 
    175 
    176     // non-javadoc, see interface EofSensorWatcher
    177     public boolean streamClosed(InputStream wrapped)
    178         throws IOException {
    179 
    180         try {
    181             if (attemptReuse && (managedConn != null)) {
    182                 // this assumes that closing the stream will
    183                 // consume the remainder of the response body:
    184                 wrapped.close();
    185                 managedConn.markReusable();
    186             }
    187         } finally {
    188             releaseManagedConnection();
    189         }
    190         return false;
    191     }
    192 
    193 
    194     // non-javadoc, see interface EofSensorWatcher
    195     public boolean streamAbort(InputStream wrapped)
    196         throws IOException {
    197 
    198         if (managedConn != null) {
    199             managedConn.abortConnection();
    200         }
    201         return false;
    202     }
    203 
    204 
    205     /**
    206      * Releases the connection gracefully.
    207      * The connection attribute will be nullified.
    208      * Subsequent invocations are no-ops.
    209      *
    210      * @throws IOException      in case of an IO problem.
    211      *         The connection attribute will be nullified anyway.
    212      */
    213     protected void releaseManagedConnection()
    214         throws IOException {
    215 
    216         if (managedConn != null) {
    217             try {
    218                 managedConn.releaseConnection();
    219             } finally {
    220                 managedConn = null;
    221             }
    222         }
    223     }
    224 
    225 } // class BasicManagedEntity
    226