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.HttpRequest;
36  import org.apache.http.ProtocolVersion;
37  import org.apache.http.RequestLine;
38  import org.apache.http.annotation.NotThreadSafe;
39  import org.apache.http.client.config.RequestConfig;
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  
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  @NotThreadSafe
53  public class HttpRequestWrapper extends AbstractHttpMessage implements HttpRequest {
54  
55      private final HttpRequest original;
56      private final String method;
57      private ProtocolVersion version;
58      private URI uri;
59  
60      private HttpRequestWrapper(final HttpRequest request) {
61          super();
62          this.original = request;
63          this.version = this.original.getRequestLine().getProtocolVersion();
64          this.method = this.original.getRequestLine().getMethod();
65          if (request instanceof HttpUriRequest) {
66              this.uri = ((HttpUriRequest) request).getURI();
67          } else {
68              this.uri = null;
69          }
70          setHeaders(request.getAllHeaders());
71      }
72  
73      public ProtocolVersion getProtocolVersion() {
74          return this.version != null ? this.version : this.original.getProtocolVersion();
75      }
76  
77      public void setProtocolVersion(final ProtocolVersion version) {
78          this.version = version;
79      }
80  
81      public URI getURI() {
82          return this.uri;
83      }
84  
85      public void setURI(final URI uri) {
86          this.uri = uri;
87      }
88  
89      public RequestLine getRequestLine() {
90          String requestUri = null;
91          if (this.uri != null) {
92              requestUri = this.uri.toASCIIString();
93          } else {
94              requestUri = this.original.getRequestLine().getUri();
95          }
96          if (requestUri == null || requestUri.length() == 0) {
97              requestUri = "/";
98          }
99          return new BasicRequestLine(this.method, requestUri, getProtocolVersion());
100     }
101 
102     public HttpRequest getOriginal() {
103         return this.original;
104     }
105 
106     @Override
107     public String toString() {
108         return getRequestLine() + " " + this.headergroup;
109     }
110 
111     static class HttpEntityEnclosingRequestWrapper extends HttpRequestWrapper
112         implements HttpEntityEnclosingRequest {
113 
114         private HttpEntity entity;
115 
116         public HttpEntityEnclosingRequestWrapper(final HttpEntityEnclosingRequest request) {
117             super(request);
118             this.entity = request.getEntity();
119         }
120 
121         public HttpEntity getEntity() {
122             return this.entity;
123         }
124 
125         public void setEntity(final HttpEntity entity) {
126             this.entity = entity;
127         }
128 
129         public boolean expectContinue() {
130             final Header expect = getFirstHeader(HTTP.EXPECT_DIRECTIVE);
131             return expect != null && HTTP.EXPECT_CONTINUE.equalsIgnoreCase(expect.getValue());
132         }
133 
134     }
135 
136     public static HttpRequestWrapper wrap(final HttpRequest request) {
137         if (request == null) {
138             return null;
139         }
140         if (request instanceof HttpEntityEnclosingRequest) {
141             return new HttpEntityEnclosingRequestWrapper((HttpEntityEnclosingRequest) request);
142         } else {
143             return new HttpRequestWrapper(request);
144         }
145     }
146 
147     /**
148      * @deprecated (4.3) use {@link RequestConfig}.
149      */
150     @Override
151     @Deprecated
152     public HttpParams getParams() {
153         if (this.params == null) {
154             this.params = original.getParams().copy();
155         }
156         return this.params;
157     }
158 
159 }