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