mobile/android/thirdparty/ch/boye/httpclientandroidlib/conn/BasicManagedEntity.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/conn/BasicManagedEntity.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,188 @@
     1.4 +/*
     1.5 + * $Revision $
     1.6 + * ====================================================================
     1.7 + *
     1.8 + *  Licensed to the Apache Software Foundation (ASF) under one or more
     1.9 + *  contributor license agreements.  See the NOTICE file distributed with
    1.10 + *  this work for additional information regarding copyright ownership.
    1.11 + *  The ASF licenses this file to You under the Apache License, Version 2.0
    1.12 + *  (the "License"); you may not use this file except in compliance with
    1.13 + *  the License.  You may obtain a copy of the License at
    1.14 + *
    1.15 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.16 + *
    1.17 + *  Unless required by applicable law or agreed to in writing, software
    1.18 + *  distributed under the License is distributed on an "AS IS" BASIS,
    1.19 + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.20 + *  See the License for the specific language governing permissions and
    1.21 + *  limitations under the License.
    1.22 + * ====================================================================
    1.23 + *
    1.24 + * This software consists of voluntary contributions made by many
    1.25 + * individuals on behalf of the Apache Software Foundation.  For more
    1.26 + * information on the Apache Software Foundation, please see
    1.27 + * <http://www.apache.org/>.
    1.28 + *
    1.29 + */
    1.30 +
    1.31 +package ch.boye.httpclientandroidlib.conn;
    1.32 +
    1.33 +import java.io.IOException;
    1.34 +import java.io.InputStream;
    1.35 +import java.io.OutputStream;
    1.36 +
    1.37 +import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
    1.38 +
    1.39 +import ch.boye.httpclientandroidlib.HttpEntity;
    1.40 +import ch.boye.httpclientandroidlib.entity.HttpEntityWrapper;
    1.41 +import ch.boye.httpclientandroidlib.util.EntityUtils;
    1.42 +
    1.43 +/**
    1.44 + * An entity that releases a {@link ManagedClientConnection connection}.
    1.45 + * A {@link ManagedClientConnection} will
    1.46 + * typically <i>not</i> return a managed entity, but you can replace
    1.47 + * the unmanaged entity in the response with a managed one.
    1.48 + *
    1.49 + * @since 4.0
    1.50 + */
    1.51 +@NotThreadSafe
    1.52 +public class BasicManagedEntity extends HttpEntityWrapper
    1.53 +    implements ConnectionReleaseTrigger, EofSensorWatcher {
    1.54 +
    1.55 +    /** The connection to release. */
    1.56 +    protected ManagedClientConnection managedConn;
    1.57 +
    1.58 +    /** Whether to keep the connection alive. */
    1.59 +    protected final boolean attemptReuse;
    1.60 +
    1.61 +    /**
    1.62 +     * Creates a new managed entity that can release a connection.
    1.63 +     *
    1.64 +     * @param entity    the entity of which to wrap the content.
    1.65 +     *                  Note that the argument entity can no longer be used
    1.66 +     *                  afterwards, since the content will be taken by this
    1.67 +     *                  managed entity.
    1.68 +     * @param conn      the connection to release
    1.69 +     * @param reuse     whether the connection should be re-used
    1.70 +     */
    1.71 +    public BasicManagedEntity(HttpEntity entity,
    1.72 +                              ManagedClientConnection conn,
    1.73 +                              boolean reuse) {
    1.74 +        super(entity);
    1.75 +
    1.76 +        if (conn == null)
    1.77 +            throw new IllegalArgumentException
    1.78 +                ("Connection may not be null.");
    1.79 +
    1.80 +        this.managedConn = conn;
    1.81 +        this.attemptReuse = reuse;
    1.82 +    }
    1.83 +
    1.84 +    @Override
    1.85 +    public boolean isRepeatable() {
    1.86 +        return false;
    1.87 +    }
    1.88 +
    1.89 +    @Override
    1.90 +    public InputStream getContent() throws IOException {
    1.91 +        return new EofSensorInputStream(wrappedEntity.getContent(), this);
    1.92 +    }
    1.93 +
    1.94 +    private void ensureConsumed() throws IOException {
    1.95 +        if (managedConn == null)
    1.96 +            return;
    1.97 +
    1.98 +        try {
    1.99 +            if (attemptReuse) {
   1.100 +                // this will not trigger a callback from EofSensorInputStream
   1.101 +                EntityUtils.consume(wrappedEntity);
   1.102 +                managedConn.markReusable();
   1.103 +            }
   1.104 +        } finally {
   1.105 +            releaseManagedConnection();
   1.106 +        }
   1.107 +    }
   1.108 +
   1.109 +    @Deprecated
   1.110 +    @Override
   1.111 +    public void consumeContent() throws IOException {
   1.112 +        ensureConsumed();
   1.113 +    }
   1.114 +
   1.115 +    @Override
   1.116 +    public void writeTo(final OutputStream outstream) throws IOException {
   1.117 +        super.writeTo(outstream);
   1.118 +        ensureConsumed();
   1.119 +    }
   1.120 +
   1.121 +    public void releaseConnection() throws IOException {
   1.122 +        ensureConsumed();
   1.123 +    }
   1.124 +
   1.125 +    public void abortConnection() throws IOException {
   1.126 +
   1.127 +        if (managedConn != null) {
   1.128 +            try {
   1.129 +                managedConn.abortConnection();
   1.130 +            } finally {
   1.131 +                managedConn = null;
   1.132 +            }
   1.133 +        }
   1.134 +    }
   1.135 +
   1.136 +    public boolean eofDetected(InputStream wrapped) throws IOException {
   1.137 +        try {
   1.138 +            if (attemptReuse && (managedConn != null)) {
   1.139 +                // there may be some cleanup required, such as
   1.140 +                // reading trailers after the response body:
   1.141 +                wrapped.close();
   1.142 +                managedConn.markReusable();
   1.143 +            }
   1.144 +        } finally {
   1.145 +            releaseManagedConnection();
   1.146 +        }
   1.147 +        return false;
   1.148 +    }
   1.149 +
   1.150 +    public boolean streamClosed(InputStream wrapped) throws IOException {
   1.151 +        try {
   1.152 +            if (attemptReuse && (managedConn != null)) {
   1.153 +                // this assumes that closing the stream will
   1.154 +                // consume the remainder of the response body:
   1.155 +                wrapped.close();
   1.156 +                managedConn.markReusable();
   1.157 +            }
   1.158 +        } finally {
   1.159 +            releaseManagedConnection();
   1.160 +        }
   1.161 +        return false;
   1.162 +    }
   1.163 +
   1.164 +    public boolean streamAbort(InputStream wrapped) throws IOException {
   1.165 +        if (managedConn != null) {
   1.166 +            managedConn.abortConnection();
   1.167 +        }
   1.168 +        return false;
   1.169 +    }
   1.170 +
   1.171 +    /**
   1.172 +     * Releases the connection gracefully.
   1.173 +     * The connection attribute will be nullified.
   1.174 +     * Subsequent invocations are no-ops.
   1.175 +     *
   1.176 +     * @throws IOException      in case of an IO problem.
   1.177 +     *         The connection attribute will be nullified anyway.
   1.178 +     */
   1.179 +    protected void releaseManagedConnection()
   1.180 +        throws IOException {
   1.181 +
   1.182 +        if (managedConn != null) {
   1.183 +            try {
   1.184 +                managedConn.releaseConnection();
   1.185 +            } finally {
   1.186 +                managedConn = null;
   1.187 +            }
   1.188 +        }
   1.189 +    }
   1.190 +
   1.191 +}

mercurial