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