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() throws IOException {
65          if (this.connHolder != null) {
66              this.connHolder.close();
67          }
68      }
69  
70      private void abortConnection() throws IOException {
71          if (this.connHolder != null) {
72              this.connHolder.abortConnection();
73          }
74      }
75  
76      public void releaseConnection() throws IOException {
77          if (this.connHolder != null) {
78              this.connHolder.releaseConnection();
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         } catch (IOException ex) {
104             abortConnection();
105             throw ex;
106         } catch (RuntimeException ex) {
107             abortConnection();
108             throw ex;
109         } finally {
110             cleanup();
111         }
112     }
113 
114     @Override
115     public boolean eofDetected(final InputStream wrapped) throws IOException {
116         try {
117             // there may be some cleanup required, such as
118             // reading trailers after the response body:
119             wrapped.close();
120             releaseConnection();
121         } catch (IOException ex) {
122             abortConnection();
123             throw ex;
124         } catch (RuntimeException ex) {
125             abortConnection();
126             throw ex;
127         } finally {
128             cleanup();
129         }
130         return false;
131     }
132 
133     @Override
134     public boolean streamClosed(final InputStream wrapped) throws IOException {
135         try {
136             final boolean open = connHolder != null && !connHolder.isReleased();
137             // this assumes that closing the stream will
138             // consume the remainder of the response body:
139             try {
140                 wrapped.close();
141                 releaseConnection();
142             } catch (final SocketException ex) {
143                 if (open) {
144                     throw ex;
145                 }
146             }
147         } catch (IOException ex) {
148             abortConnection();
149             throw ex;
150         } catch (RuntimeException ex) {
151             abortConnection();
152             throw ex;
153         } finally {
154             cleanup();
155         }
156         return false;
157     }
158 
159     @Override
160     public boolean streamAbort(final InputStream wrapped) throws IOException {
161         cleanup();
162         return false;
163     }
164 
165     @Override
166     public String toString() {
167         final StringBuilder sb = new StringBuilder("ResponseEntityProxy{");
168         sb.append(wrappedEntity);
169         sb.append('}');
170         return sb.toString();
171     }
172 
173 }