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.client.methods;
29  
30  import java.net.URI;
31  
32  import org.apache.http.Header;
33  import org.apache.http.HttpEntity;
34  import org.apache.http.HttpEntityEnclosingRequest;
35  import org.apache.http.HttpHost;
36  import org.apache.http.HttpRequest;
37  import org.apache.http.ProtocolVersion;
38  import org.apache.http.RequestLine;
39  import org.apache.http.annotation.NotThreadSafe;
40  import org.apache.http.message.AbstractHttpMessage;
41  import org.apache.http.message.BasicRequestLine;
42  import org.apache.http.params.HttpParams;
43  import org.apache.http.protocol.HTTP;
44  import org.apache.http.util.Args;
45  
46  /**
47   * A wrapper class for {@link HttpRequest} that can be used to change properties of the current
48   * request without modifying the original object.
49   *
50   * @since 4.3
51   */
52  @SuppressWarnings("deprecation")
53  @NotThreadSafe
54  public class HttpRequestWrapper extends AbstractHttpMessage implements HttpUriRequest {
55  
56      private final HttpRequest original;
57      private final HttpHost target;
58      private final String method;
59      private RequestLine requestLine;
60      private ProtocolVersion version;
61      private URI uri;
62  
63      private HttpRequestWrapper(final HttpRequest request, final HttpHost target) {
64          super();
65          this.original = Args.notNull(request, "HTTP request");
66          this.target = target;
67          this.version = this.original.getRequestLine().getProtocolVersion();
68          this.method = this.original.getRequestLine().getMethod();
69          if (request instanceof HttpUriRequest) {
70              this.uri = ((HttpUriRequest) request).getURI();
71          } else {
72              this.uri = null;
73          }
74          setHeaders(request.getAllHeaders());
75      }
76  
77      @Override
78      public ProtocolVersion getProtocolVersion() {
79          return this.version != null ? this.version : this.original.getProtocolVersion();
80      }
81  
82      public void setProtocolVersion(final ProtocolVersion version) {
83          this.version = version;
84          this.requestLine = null;
85      }
86  
87      @Override
88      public URI getURI() {
89          return this.uri;
90      }
91  
92      public void setURI(final URI uri) {
93          this.uri = uri;
94          this.requestLine = null;
95      }
96  
97      @Override
98      public String getMethod() {
99          return method;
100     }
101 
102     @Override
103     public void abort() throws UnsupportedOperationException {
104         throw new UnsupportedOperationException();
105     }
106 
107     @Override
108     public boolean isAborted() {
109         return false;
110     }
111 
112     @Override
113     public RequestLine getRequestLine() {
114         if (this.requestLine == null) {
115             String requestUri;
116             if (this.uri != null) {
117                 requestUri = this.uri.toASCIIString();
118             } else {
119                 requestUri = this.original.getRequestLine().getUri();
120             }
121             if (requestUri == null || requestUri.isEmpty()) {
122                 requestUri = "/";
123             }
124             this.requestLine = new BasicRequestLine(this.method, requestUri, getProtocolVersion());
125         }
126         return this.requestLine;
127     }
128 
129     public HttpRequest getOriginal() {
130         return this.original;
131     }
132 
133     /**
134      * @since 4.4
135      */
136     public HttpHost getTarget() {
137         return target;
138     }
139 
140     @Override
141     public String toString() {
142         return getRequestLine() + " " + this.headergroup;
143     }
144 
145     static class HttpEntityEnclosingRequestWrapper extends HttpRequestWrapper
146         implements HttpEntityEnclosingRequest {
147 
148         private HttpEntity entity;
149 
150         HttpEntityEnclosingRequestWrapper(final HttpEntityEnclosingRequest request, final HttpHost target) {
151             super(request, target);
152             this.entity = request.getEntity();
153         }
154 
155         @Override
156         public HttpEntity getEntity() {
157             return this.entity;
158         }
159 
160         @Override
161         public void setEntity(final HttpEntity entity) {
162             this.entity = entity;
163         }
164 
165         @Override
166         public boolean expectContinue() {
167             final Header expect = getFirstHeader(HTTP.EXPECT_DIRECTIVE);
168             return expect != null && HTTP.EXPECT_CONTINUE.equalsIgnoreCase(expect.getValue());
169         }
170 
171     }
172 
173     /**
174      * Creates a mutable wrapper of the original request.
175      *
176      * @param request original request
177      * @return mutable request wrappering the original one
178      */
179     public static HttpRequestWrapper wrap(final HttpRequest request) {
180         return wrap(request, null);
181     }
182 
183 
184     /**
185      * Creates a mutable wrapper of the original request.
186      *
187      * @param request original request
188      * @param target original target, if explicitly specified
189      * @return mutable request wrappering the original one
190      * @since 4.4
191      */
192     public static HttpRequestWrapper wrap(final HttpRequest request, final HttpHost target) {
193         Args.notNull(request, "HTTP request");
194         if (request instanceof HttpEntityEnclosingRequest) {
195             return new HttpEntityEnclosingRequestWrapper((HttpEntityEnclosingRequest) request, target);
196         } else {
197             return new HttpRequestWrapper(request, target);
198         }
199     }
200 
201     /**
202      * @deprecated (4.3) use
203      *   {@link org.apache.http.client.config.RequestConfig}.
204      */
205     @Override
206     @Deprecated
207     public HttpParams getParams() {
208         if (this.params == null) {
209             this.params = original.getParams().copy();
210         }
211         return this.params;
212     }
213 
214 }