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  package org.apache.http.impl.execchain;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.OutputStream;
32  
33  import org.apache.http.Header;
34  import org.apache.http.HttpEntity;
35  import org.apache.http.HttpEntityEnclosingRequest;
36  import org.apache.http.HttpRequest;
37  
38  /**
39   * A Proxy class for {@link org.apache.http.HttpEntity} enclosed in a request message.
40   *
41   * @since 4.3
42   */
43  class RequestEntityProxy implements HttpEntity  {
44  
45      static void enhance(final HttpEntityEnclosingRequest request) {
46          final HttpEntity entity = request.getEntity();
47          if (entity != null && !entity.isRepeatable() && !isEnhanced(entity)) {
48              request.setEntity(new RequestEntityProxy(entity));
49          }
50      }
51  
52      static boolean isEnhanced(final HttpEntity entity) {
53          return entity instanceof RequestEntityProxy;
54      }
55  
56      static boolean isRepeatable(final HttpRequest request) {
57          if (request instanceof HttpEntityEnclosingRequest) {
58              final HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
59              if (entity != null) {
60                  if (isEnhanced(entity)) {
61                      final RequestEntityProxy proxy = (RequestEntityProxy) entity;
62                      if (!proxy.isConsumed()) {
63                          return true;
64                      }
65                  }
66                  return entity.isRepeatable();
67              }
68          }
69          return true;
70      }
71  
72      private final HttpEntity original;
73      private boolean consumed = false;
74  
75      RequestEntityProxy(final HttpEntity original) {
76          super();
77          this.original = original;
78      }
79  
80      public HttpEntity getOriginal() {
81          return original;
82      }
83  
84      public boolean isConsumed() {
85          return consumed;
86      }
87  
88      @Override
89      public boolean isRepeatable() {
90          return original.isRepeatable();
91      }
92  
93      @Override
94      public boolean isChunked() {
95          return original.isChunked();
96      }
97  
98      @Override
99      public long getContentLength() {
100         return original.getContentLength();
101     }
102 
103     @Override
104     public Header getContentType() {
105         return original.getContentType();
106     }
107 
108     @Override
109     public Header getContentEncoding() {
110         return original.getContentEncoding();
111     }
112 
113     @Override
114     public InputStream getContent() throws IOException, IllegalStateException {
115         return original.getContent();
116     }
117 
118     @Override
119     public void writeTo(final OutputStream outstream) throws IOException {
120         consumed = true;
121         original.writeTo(outstream);
122     }
123 
124     @Override
125     public boolean isStreaming() {
126         return original.isStreaming();
127     }
128 
129     @Override
130     @Deprecated
131     public void consumeContent() throws IOException {
132         consumed = true;
133         original.consumeContent();
134     }
135 
136     @Override
137     public String toString() {
138         final StringBuilder sb = new StringBuilder("RequestEntityProxy{");
139         sb.append(original);
140         sb.append('}');
141         return sb.toString();
142     }
143 
144 }