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.hc.client5.http.impl.classic;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.OutputStream;
32  import java.util.List;
33  import java.util.Set;
34  
35  import org.apache.hc.core5.function.Supplier;
36  import org.apache.hc.core5.http.ClassicHttpRequest;
37  import org.apache.hc.core5.http.Header;
38  import org.apache.hc.core5.http.HttpEntity;
39  
40  class RequestEntityProxy implements HttpEntity  {
41  
42      static void enhance(final ClassicHttpRequest request) {
43          final HttpEntity entity = request.getEntity();
44          if (entity != null && !entity.isRepeatable() && !isEnhanced(entity)) {
45              request.setEntity(new RequestEntityProxy(entity));
46          }
47      }
48  
49      static boolean isEnhanced(final HttpEntity entity) {
50          return entity instanceof RequestEntityProxy;
51      }
52  
53      private final HttpEntity original;
54      private boolean consumed;
55  
56      RequestEntityProxy(final HttpEntity original) {
57          super();
58          this.original = original;
59      }
60  
61      public HttpEntity getOriginal() {
62          return original;
63      }
64  
65      public boolean isConsumed() {
66          return consumed;
67      }
68  
69      @Override
70      public boolean isRepeatable() {
71          if (!consumed) {
72              return true;
73          } else {
74              return original.isRepeatable();
75          }
76      }
77  
78      @Override
79      public boolean isChunked() {
80          return original.isChunked();
81      }
82  
83      @Override
84      public long getContentLength() {
85          return original.getContentLength();
86      }
87  
88      @Override
89      public String getContentType() {
90          return original.getContentType();
91      }
92  
93      @Override
94      public String getContentEncoding() {
95          return original.getContentEncoding();
96      }
97  
98      @Override
99      public InputStream getContent() throws IOException, IllegalStateException {
100         return original.getContent();
101     }
102 
103     @Override
104     public void writeTo(final OutputStream outStream) throws IOException {
105         consumed = true;
106         original.writeTo(outStream);
107     }
108 
109     @Override
110     public boolean isStreaming() {
111         return original.isStreaming();
112     }
113 
114     @Override
115     public Supplier<List<? extends Header>> getTrailers() {
116         return original.getTrailers();
117     }
118 
119     @Override
120     public Set<String> getTrailerNames() {
121         return original.getTrailerNames();
122     }
123 
124     @Override
125     public void close() throws IOException {
126         original.close();
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 }