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     @Override
109     public boolean eofDetected(final InputStream wrapped) throws IOException {
110         try {
111             // there may be some cleanup required, such as
112             // reading trailers after the response body:
113             wrapped.close();
114             releaseConnection();
115         } finally {
116             cleanup();
117         }
118         return false;
119     }
120 
121     @Override
122     public boolean streamClosed(final InputStream wrapped) throws IOException {
123         try {
124             final boolean open = connHolder != null && !connHolder.isReleased();
125             // this assumes that closing the stream will
126             // consume the remainder of the response body:
127             try {
128                 wrapped.close();
129                 releaseConnection();
130             } catch (final SocketException ex) {
131                 if (open) {
132                     throw ex;
133                 }
134             }
135         } finally {
136             cleanup();
137         }
138         return false;
139     }
140 
141     @Override
142     public boolean streamAbort(final InputStream wrapped) throws IOException {
143         cleanup();
144         return false;
145     }
146 
147     @Override
148     public String toString() {
149         final StringBuilder sb = new StringBuilder("ResponseEntityProxy{");
150         sb.append(wrappedEntity);
151         sb.append('}');
152         return sb.toString();
153     }
154 
155 }