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.impl.client;
29  
30  import java.net.URI;
31  import java.net.URISyntaxException;
32  
33  import org.apache.http.HttpRequest;
34  import org.apache.http.ProtocolException;
35  import org.apache.http.ProtocolVersion;
36  import org.apache.http.RequestLine;
37  import org.apache.http.client.methods.HttpUriRequest;
38  import org.apache.http.message.AbstractHttpMessage;
39  import org.apache.http.message.BasicRequestLine;
40  import org.apache.http.params.HttpProtocolParams;
41  import org.apache.http.util.Args;
42  
43  /**
44   * A wrapper class for {@link HttpRequest}s that can be used to change
45   * properties of the current request without modifying the original
46   * object.
47   * <p>
48   * This class is also capable of resetting the request headers to
49   * the state of the original request.
50   * </p>
51   *
52   * @since 4.0
53   *
54   * @deprecated (4.3) do not use.
55   */
56  @Deprecated
57  public class RequestWrapper extends AbstractHttpMessage implements HttpUriRequest {
58  
59      private final HttpRequest original;
60  
61      private URI uri;
62      private String method;
63      private ProtocolVersion version;
64      private int execCount;
65  
66      public RequestWrapper(final HttpRequest request) throws ProtocolException {
67          super();
68          Args.notNull(request, "HTTP request");
69          this.original = request;
70          setParams(request.getParams());
71          setHeaders(request.getAllHeaders());
72          // Make a copy of the original URI
73          if (request instanceof HttpUriRequest) {
74              this.uri = ((HttpUriRequest) request).getURI();
75              this.method = ((HttpUriRequest) request).getMethod();
76              this.version = null;
77          } else {
78              final RequestLine requestLine = request.getRequestLine();
79              try {
80                  this.uri = new URI(requestLine.getUri());
81              } catch (final URISyntaxException ex) {
82                  throw new ProtocolException("Invalid request URI: "
83                          + requestLine.getUri(), ex);
84              }
85              this.method = requestLine.getMethod();
86              this.version = request.getProtocolVersion();
87          }
88          this.execCount = 0;
89      }
90  
91      public void resetHeaders() {
92          // Make a copy of original headers
93          this.headergroup.clear();
94          setHeaders(this.original.getAllHeaders());
95      }
96  
97      @Override
98      public String getMethod() {
99          return this.method;
100     }
101 
102     public void setMethod(final String method) {
103         Args.notNull(method, "Method name");
104         this.method = method;
105     }
106 
107     @Override
108     public ProtocolVersion getProtocolVersion() {
109         if (this.version == null) {
110             this.version = HttpProtocolParams.getVersion(getParams());
111         }
112         return this.version;
113     }
114 
115     public void setProtocolVersion(final ProtocolVersion version) {
116         this.version = version;
117     }
118 
119 
120     @Override
121     public URI getURI() {
122         return this.uri;
123     }
124 
125     public void setURI(final URI uri) {
126         this.uri = uri;
127     }
128 
129     @Override
130     public RequestLine getRequestLine() {
131         final ProtocolVersion ver = getProtocolVersion();
132         String uritext = null;
133         if (uri != null) {
134             uritext = uri.toASCIIString();
135         }
136         if (uritext == null || uritext.isEmpty()) {
137             uritext = "/";
138         }
139         return new BasicRequestLine(getMethod(), uritext, ver);
140     }
141 
142     @Override
143     public void abort() throws UnsupportedOperationException {
144         throw new UnsupportedOperationException();
145     }
146 
147     @Override
148     public boolean isAborted() {
149         return false;
150     }
151 
152     public HttpRequest getOriginal() {
153         return this.original;
154     }
155 
156     public boolean isRepeatable() {
157         return true;
158     }
159 
160     public int getExecCount() {
161         return this.execCount;
162     }
163 
164     public void incrementExecCount() {
165         this.execCount++;
166     }
167 
168 }