Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * $Revision $ |
michael@0 | 3 | * ==================================================================== |
michael@0 | 4 | * |
michael@0 | 5 | * Licensed to the Apache Software Foundation (ASF) under one or more |
michael@0 | 6 | * contributor license agreements. See the NOTICE file distributed with |
michael@0 | 7 | * this work for additional information regarding copyright ownership. |
michael@0 | 8 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
michael@0 | 9 | * (the "License"); you may not use this file except in compliance with |
michael@0 | 10 | * the License. You may obtain a copy of the License at |
michael@0 | 11 | * |
michael@0 | 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 13 | * |
michael@0 | 14 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 17 | * See the License for the specific language governing permissions and |
michael@0 | 18 | * limitations under the License. |
michael@0 | 19 | * ==================================================================== |
michael@0 | 20 | * |
michael@0 | 21 | * This software consists of voluntary contributions made by many |
michael@0 | 22 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 23 | * information on the Apache Software Foundation, please see |
michael@0 | 24 | * <http://www.apache.org/>. |
michael@0 | 25 | * |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | package ch.boye.httpclientandroidlib.conn; |
michael@0 | 29 | |
michael@0 | 30 | import java.io.IOException; |
michael@0 | 31 | import java.io.InputStream; |
michael@0 | 32 | import java.io.OutputStream; |
michael@0 | 33 | |
michael@0 | 34 | import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; |
michael@0 | 35 | |
michael@0 | 36 | import ch.boye.httpclientandroidlib.HttpEntity; |
michael@0 | 37 | import ch.boye.httpclientandroidlib.entity.HttpEntityWrapper; |
michael@0 | 38 | import ch.boye.httpclientandroidlib.util.EntityUtils; |
michael@0 | 39 | |
michael@0 | 40 | /** |
michael@0 | 41 | * An entity that releases a {@link ManagedClientConnection connection}. |
michael@0 | 42 | * A {@link ManagedClientConnection} will |
michael@0 | 43 | * typically <i>not</i> return a managed entity, but you can replace |
michael@0 | 44 | * the unmanaged entity in the response with a managed one. |
michael@0 | 45 | * |
michael@0 | 46 | * @since 4.0 |
michael@0 | 47 | */ |
michael@0 | 48 | @NotThreadSafe |
michael@0 | 49 | public class BasicManagedEntity extends HttpEntityWrapper |
michael@0 | 50 | implements ConnectionReleaseTrigger, EofSensorWatcher { |
michael@0 | 51 | |
michael@0 | 52 | /** The connection to release. */ |
michael@0 | 53 | protected ManagedClientConnection managedConn; |
michael@0 | 54 | |
michael@0 | 55 | /** Whether to keep the connection alive. */ |
michael@0 | 56 | protected final boolean attemptReuse; |
michael@0 | 57 | |
michael@0 | 58 | /** |
michael@0 | 59 | * Creates a new managed entity that can release a connection. |
michael@0 | 60 | * |
michael@0 | 61 | * @param entity the entity of which to wrap the content. |
michael@0 | 62 | * Note that the argument entity can no longer be used |
michael@0 | 63 | * afterwards, since the content will be taken by this |
michael@0 | 64 | * managed entity. |
michael@0 | 65 | * @param conn the connection to release |
michael@0 | 66 | * @param reuse whether the connection should be re-used |
michael@0 | 67 | */ |
michael@0 | 68 | public BasicManagedEntity(HttpEntity entity, |
michael@0 | 69 | ManagedClientConnection conn, |
michael@0 | 70 | boolean reuse) { |
michael@0 | 71 | super(entity); |
michael@0 | 72 | |
michael@0 | 73 | if (conn == null) |
michael@0 | 74 | throw new IllegalArgumentException |
michael@0 | 75 | ("Connection may not be null."); |
michael@0 | 76 | |
michael@0 | 77 | this.managedConn = conn; |
michael@0 | 78 | this.attemptReuse = reuse; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | @Override |
michael@0 | 82 | public boolean isRepeatable() { |
michael@0 | 83 | return false; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | @Override |
michael@0 | 87 | public InputStream getContent() throws IOException { |
michael@0 | 88 | return new EofSensorInputStream(wrappedEntity.getContent(), this); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | private void ensureConsumed() throws IOException { |
michael@0 | 92 | if (managedConn == null) |
michael@0 | 93 | return; |
michael@0 | 94 | |
michael@0 | 95 | try { |
michael@0 | 96 | if (attemptReuse) { |
michael@0 | 97 | // this will not trigger a callback from EofSensorInputStream |
michael@0 | 98 | EntityUtils.consume(wrappedEntity); |
michael@0 | 99 | managedConn.markReusable(); |
michael@0 | 100 | } |
michael@0 | 101 | } finally { |
michael@0 | 102 | releaseManagedConnection(); |
michael@0 | 103 | } |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | @Deprecated |
michael@0 | 107 | @Override |
michael@0 | 108 | public void consumeContent() throws IOException { |
michael@0 | 109 | ensureConsumed(); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | @Override |
michael@0 | 113 | public void writeTo(final OutputStream outstream) throws IOException { |
michael@0 | 114 | super.writeTo(outstream); |
michael@0 | 115 | ensureConsumed(); |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | public void releaseConnection() throws IOException { |
michael@0 | 119 | ensureConsumed(); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | public void abortConnection() throws IOException { |
michael@0 | 123 | |
michael@0 | 124 | if (managedConn != null) { |
michael@0 | 125 | try { |
michael@0 | 126 | managedConn.abortConnection(); |
michael@0 | 127 | } finally { |
michael@0 | 128 | managedConn = null; |
michael@0 | 129 | } |
michael@0 | 130 | } |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | public boolean eofDetected(InputStream wrapped) throws IOException { |
michael@0 | 134 | try { |
michael@0 | 135 | if (attemptReuse && (managedConn != null)) { |
michael@0 | 136 | // there may be some cleanup required, such as |
michael@0 | 137 | // reading trailers after the response body: |
michael@0 | 138 | wrapped.close(); |
michael@0 | 139 | managedConn.markReusable(); |
michael@0 | 140 | } |
michael@0 | 141 | } finally { |
michael@0 | 142 | releaseManagedConnection(); |
michael@0 | 143 | } |
michael@0 | 144 | return false; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | public boolean streamClosed(InputStream wrapped) throws IOException { |
michael@0 | 148 | try { |
michael@0 | 149 | if (attemptReuse && (managedConn != null)) { |
michael@0 | 150 | // this assumes that closing the stream will |
michael@0 | 151 | // consume the remainder of the response body: |
michael@0 | 152 | wrapped.close(); |
michael@0 | 153 | managedConn.markReusable(); |
michael@0 | 154 | } |
michael@0 | 155 | } finally { |
michael@0 | 156 | releaseManagedConnection(); |
michael@0 | 157 | } |
michael@0 | 158 | return false; |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | public boolean streamAbort(InputStream wrapped) throws IOException { |
michael@0 | 162 | if (managedConn != null) { |
michael@0 | 163 | managedConn.abortConnection(); |
michael@0 | 164 | } |
michael@0 | 165 | return false; |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | /** |
michael@0 | 169 | * Releases the connection gracefully. |
michael@0 | 170 | * The connection attribute will be nullified. |
michael@0 | 171 | * Subsequent invocations are no-ops. |
michael@0 | 172 | * |
michael@0 | 173 | * @throws IOException in case of an IO problem. |
michael@0 | 174 | * The connection attribute will be nullified anyway. |
michael@0 | 175 | */ |
michael@0 | 176 | protected void releaseManagedConnection() |
michael@0 | 177 | throws IOException { |
michael@0 | 178 | |
michael@0 | 179 | if (managedConn != null) { |
michael@0 | 180 | try { |
michael@0 | 181 | managedConn.releaseConnection(); |
michael@0 | 182 | } finally { |
michael@0 | 183 | managedConn = null; |
michael@0 | 184 | } |
michael@0 | 185 | } |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | } |