View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * 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   */
27  
28  package org.apache.http.impl.execchain;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  import java.net.SocketException;
34  
35  import org.apache.http.HttpEntity;
36  import org.apache.http.HttpResponse;
37  import org.apache.http.annotation.NotThreadSafe;
38  import org.apache.http.conn.EofSensorInputStream;
39  import org.apache.http.conn.EofSensorWatcher;
40  import org.apache.http.entity.HttpEntityWrapper;
41  
42  /**
43   * A wrapper class for {@link HttpEntity} enclosed in a response message.
44   *
45   * @since 4.3
46   */
47  @NotThreadSafe
48  class ResponseEntityProxy extends HttpEntityWrapper implements EofSensorWatcher {
49  
50      private final ConnectionHolder connHolder;
51  
52      public static void enchance(final HttpResponse response, final ConnectionHolder connHolder) {
53          final HttpEntity entity = response.getEntity();
54          if (entity != null && entity.isStreaming() && connHolder != null) {
55              response.setEntity(new ResponseEntityProxy(entity, connHolder));
56          }
57      }
58  
59      ResponseEntityProxy(final HttpEntity entity, final ConnectionHolder connHolder) {
60          super(entity);
61          this.connHolder = connHolder;
62      }
63  
64      private void cleanup() {
65          if (this.connHolder != null) {
66              this.connHolder.abortConnection();
67          }
68      }
69  
70      public void releaseConnection() throws IOException {
71          if (this.connHolder != null) {
72              try {
73                  if (this.connHolder.isReusable()) {
74                      this.connHolder.releaseConnection();
75                  }
76              } finally {
77                  cleanup();
78              }
79          }
80      }
81  
82      @Override
83      public boolean isRepeatable() {
84          return false;
85      }
86  
87      @Override
88      public InputStream getContent() throws IOException {
89          return new EofSensorInputStream(this.wrappedEntity.getContent(), this);
90      }
91  
92      @Deprecated
93      @Override
94      public void consumeContent() throws IOException {
95          releaseConnection();
96      }
97  
98      @Override
99      public void writeTo(final OutputStream outstream) throws IOException {
100         try {
101             this.wrappedEntity.writeTo(outstream);
102             releaseConnection();
103         } finally {
104             cleanup();
105         }
106     }
107 
108     public boolean eofDetected(final InputStream wrapped) throws IOException {
109         try {
110             // there may be some cleanup required, such as
111             // reading trailers after the response body:
112             wrapped.close();
113             releaseConnection();
114         } finally {
115             cleanup();
116         }
117         return false;
118     }
119 
120     public boolean streamClosed(final InputStream wrapped) throws IOException {
121         try {
122             final boolean open = connHolder != null && !connHolder.isReleased();
123             // this assumes that closing the stream will
124             // consume the remainder of the response body:
125             try {
126                 wrapped.close();
127                 releaseConnection();
128             } catch (final SocketException ex) {
129                 if (open) {
130                     throw ex;
131                 }
132             }
133         } finally {
134             cleanup();
135         }
136         return false;
137     }
138 
139     public boolean streamAbort(final InputStream wrapped) throws IOException {
140         cleanup();
141         return false;
142     }
143 
144     @Override
145     public String toString() {
146         final StringBuilder sb = new StringBuilder("ResponseEntityProxy{");
147         sb.append(wrappedEntity);
148         sb.append('}');
149         return sb.toString();
150     }
151 
152 }