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.annotation.NotThreadSafe;
37  import org.apache.http.conn.EofSensorInputStream;
38  import org.apache.http.conn.EofSensorWatcher;
39  import org.apache.http.entity.HttpEntityWrapper;
40  
41  /**
42   * A wrapper class for {@link HttpEntity} enclosed in a response message.
43   *
44   * @since 4.3
45   */
46  @NotThreadSafe
47  class ResponseEntityWrapper extends HttpEntityWrapper implements EofSensorWatcher {
48  
49      private final ConnectionHolder connReleaseTrigger;
50  
51      public ResponseEntityWrapper(
52              final HttpEntity entity,
53              final ConnectionHolder connReleaseTrigger) {
54          super(entity);
55          this.connReleaseTrigger = connReleaseTrigger;
56      }
57  
58      private void cleanup() {
59          if (this.connReleaseTrigger != null) {
60              this.connReleaseTrigger.abortConnection();
61          }
62      }
63  
64      public void releaseConnection() throws IOException {
65          if (this.connReleaseTrigger != null) {
66              try {
67                  if (this.connReleaseTrigger.isReusable()) {
68                      this.connReleaseTrigger.releaseConnection();
69                  }
70              } finally {
71                  cleanup();
72              }
73          }
74      }
75  
76      @Override
77      public boolean isRepeatable() {
78          return false;
79      }
80  
81      @Override
82      public InputStream getContent() throws IOException {
83          return new EofSensorInputStream(this.wrappedEntity.getContent(), this);
84      }
85  
86      @Deprecated
87      @Override
88      public void consumeContent() throws IOException {
89          releaseConnection();
90      }
91  
92      @Override
93      public void writeTo(final OutputStream outstream) throws IOException {
94          try {
95              this.wrappedEntity.writeTo(outstream);
96              releaseConnection();
97          } finally {
98              cleanup();
99          }
100     }
101 
102     public boolean eofDetected(final InputStream wrapped) throws IOException {
103         try {
104             // there may be some cleanup required, such as
105             // reading trailers after the response body:
106             wrapped.close();
107             releaseConnection();
108         } finally {
109             cleanup();
110         }
111         return false;
112     }
113 
114     public boolean streamClosed(final InputStream wrapped) throws IOException {
115         try {
116             final boolean open = connReleaseTrigger != null && !connReleaseTrigger.isReleased();
117             // this assumes that closing the stream will
118             // consume the remainder of the response body:
119             try {
120                 wrapped.close();
121                 releaseConnection();
122             } catch (final SocketException ex) {
123                 if (open) {
124                     throw ex;
125                 }
126             }
127         } finally {
128             cleanup();
129         }
130         return false;
131     }
132 
133     public boolean streamAbort(final InputStream wrapped) throws IOException {
134         cleanup();
135         return false;
136     }
137 
138 }