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  @Deprecated
47  @NotThreadSafe
48  class CloseableHttpResponseProxy implements InvocationHandler {
49  
50      private final static Constructor<?> CONSTRUCTOR;
51  
52      static {
53          try {
54              CONSTRUCTOR = Proxy.getProxyClass(CloseableHttpResponseProxy.class.getClassLoader(),
55                      new Class<?>[] { CloseableHttpResponse.class }).getConstructor(new Class[] { InvocationHandler.class });
56          } catch (NoSuchMethodException ex) {
57              throw new IllegalStateException(ex);
58          }
59      }
60  
61      private final HttpResponse original;
62  
63      CloseableHttpResponseProxy(final HttpResponse original) {
64          super();
65          this.original = original;
66      }
67  
68      public void close() throws IOException {
69          final HttpEntity entity = this.original.getEntity();
70          EntityUtils.consume(entity);
71      }
72  
73      @Override
74      public Object invoke(
75              final Object proxy, final Method method, final Object[] args) throws Throwable {
76          final String mname = method.getName();
77          if (mname.equals("close")) {
78              close();
79              return null;
80          } else {
81              try {
82                  return method.invoke(original, args);
83              } catch (final InvocationTargetException ex) {
84                  final Throwable cause = ex.getCause();
85                  if (cause != null) {
86                      throw cause;
87                  } else {
88                      throw ex;
89                  }
90              }
91          }
92      }
93  
94      public static CloseableHttpResponse newProxy(final HttpResponse original) {
95          try {
96              return (CloseableHttpResponse) CONSTRUCTOR.newInstance(new CloseableHttpResponseProxy(original));
97          } catch (InstantiationException ex) {
98              throw new IllegalStateException(ex);
99          } catch (InvocationTargetException ex) {
100             throw new IllegalStateException(ex);
101         } catch (IllegalAccessException ex) {
102             throw new IllegalStateException(ex);
103         }
104     }
105 
106 }