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