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