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      @Override
91      public boolean isRepeatable() {
92          return original.isRepeatable();
93      }
94  
95      @Override
96      public boolean isChunked() {
97          return original.isChunked();
98      }
99  
100     @Override
101     public long getContentLength() {
102         return original.getContentLength();
103     }
104 
105     @Override
106     public Header getContentType() {
107         return original.getContentType();
108     }
109 
110     @Override
111     public Header getContentEncoding() {
112         return original.getContentEncoding();
113     }
114 
115     @Override
116     public InputStream getContent() throws IOException, IllegalStateException {
117         return original.getContent();
118     }
119 
120     @Override
121     public void writeTo(final OutputStream outstream) throws IOException {
122         consumed = true;
123         original.writeTo(outstream);
124     }
125 
126     @Override
127     public boolean isStreaming() {
128         return original.isStreaming();
129     }
130 
131     @Override
132     @Deprecated
133     public void consumeContent() throws IOException {
134         consumed = true;
135         original.consumeContent();
136     }
137 
138     @Override
139     public String toString() {
140         final StringBuilder sb = new StringBuilder("RequestEntityProxy{");
141         sb.append(original);
142         sb.append('}');
143         return sb.toString();
144     }
145 
146 }