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