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