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.client;
29  
30  import java.io.IOException;
31  import java.lang.reflect.Constructor;
32  import java.lang.reflect.InvocationHandler;
33  import java.lang.reflect.InvocationTargetException;
34  import java.lang.reflect.Method;
35  import java.lang.reflect.Proxy;
36  
37  import org.apache.http.HttpEntity;
38  import org.apache.http.HttpResponse;
39  import org.apache.http.annotation.NotThreadSafe;
40  import org.apache.http.client.methods.CloseableHttpResponse;
41  import org.apache.http.util.EntityUtils;
42  
43  /**
44   * @since 4.3
45   */
46  @NotThreadSafe
47  class CloseableHttpResponseProxy implements InvocationHandler {
48  
49      private final static Constructor<?> CONSTRUCTOR;
50  
51      static {
52          try {
53              CONSTRUCTOR = Proxy.getProxyClass(CloseableHttpResponseProxy.class.getClassLoader(),
54                      new Class<?>[] { CloseableHttpResponse.class }).getConstructor(new Class[] { InvocationHandler.class });
55          } catch (NoSuchMethodException ex) {
56              throw new IllegalStateException(ex);
57          }
58      }
59  
60      private final HttpResponse original;
61  
62      CloseableHttpResponseProxy(final HttpResponse original) {
63          super();
64          this.original = original;
65      }
66  
67      public void close() throws IOException {
68          final HttpEntity entity = this.original.getEntity();
69          EntityUtils.consume(entity);
70      }
71  
72      public Object invoke(
73              final Object proxy, final Method method, final Object[] args) throws Throwable {
74          final String mname = method.getName();
75          if (mname.equals("close")) {
76              close();
77              return null;
78          } else {
79              try {
80                  return method.invoke(original, args);
81              } catch (final InvocationTargetException ex) {
82                  final Throwable cause = ex.getCause();
83                  if (cause != null) {
84                      throw cause;
85                  } else {
86                      throw ex;
87                  }
88              }
89          }
90      }
91  
92      public static CloseableHttpResponse newProxy(final HttpResponse original) {
93          try {
94              return (CloseableHttpResponse) CONSTRUCTOR.newInstance(new CloseableHttpResponseProxy(original));
95          } catch (InstantiationException ex) {
96              throw new IllegalStateException(ex);
97          } catch (InvocationTargetException ex) {
98              throw new IllegalStateException(ex);
99          } catch (IllegalAccessException ex) {
100             throw new IllegalStateException(ex);
101         }
102     }
103 
104 }