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