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.Closeable;
31  import java.io.IOException;
32  import java.lang.reflect.InvocationHandler;
33  import java.lang.reflect.InvocationTargetException;
34  import java.lang.reflect.Method;
35  
36  import org.apache.http.HttpEntity;
37  import org.apache.http.HttpResponse;
38  import org.apache.http.annotation.NotThreadSafe;
39  
40  /**
41   * A proxy class for {@link HttpResponse} that can be used to release client connection
42   * associated with the original response.
43   *
44   * @since 4.3
45   */
46  @NotThreadSafe
47  class ResponseProxyHandler implements InvocationHandler {
48  
49      private static final Method CLOSE_METHOD;
50  
51      static {
52          try {
53              CLOSE_METHOD = Closeable.class.getMethod("close");
54          } catch (final NoSuchMethodException ex) {
55              throw new Error(ex);
56          }
57      }
58  
59      private final HttpResponse original;
60      private final ConnectionHolder connHolder;
61  
62      ResponseProxyHandler(
63              final HttpResponse original,
64              final ConnectionHolder connHolder) {
65          super();
66          this.original = original;
67          this.connHolder = connHolder;
68          final HttpEntity entity = original.getEntity();
69          if (entity != null && entity.isStreaming() && connHolder != null) {
70              this.original.setEntity(new ResponseEntityWrapper(entity, connHolder));
71          }
72      }
73  
74      public void close() throws IOException {
75          if (this.connHolder != null) {
76              this.connHolder.abortConnection();
77          }
78      }
79  
80      public Object invoke(
81              final Object proxy, final Method method, final Object[] args) throws Throwable {
82          if (method.equals(CLOSE_METHOD)) {
83              close();
84              return null;
85          } else {
86              try {
87                  return method.invoke(original, args);
88              } catch (final InvocationTargetException ex) {
89                  final Throwable cause = ex.getCause();
90                  if (cause != null) {
91                      throw cause;
92                  } else {
93                      throw ex;
94                  }
95              }
96          }
97      }
98  
99  }