• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/RequestWrapper.java $
3  * $Revision: 674186 $
4  * $Date: 2008-07-05 05:18:54 -0700 (Sat, 05 Jul 2008) $
5  *
6  * ====================================================================
7  * Licensed to the Apache Software Foundation (ASF) under one
8  * or more contributor license agreements.  See the NOTICE file
9  * distributed with this work for additional information
10  * regarding copyright ownership.  The ASF licenses this file
11  * to you under the Apache License, Version 2.0 (the
12  * "License"); you may not use this file except in compliance
13  * with the License.  You may obtain a copy of the License at
14  *
15  *   http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing,
18  * software distributed under the License is distributed on an
19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20  * KIND, either express or implied.  See the License for the
21  * specific language governing permissions and limitations
22  * under the License.
23  * ====================================================================
24  *
25  * This software consists of voluntary contributions made by many
26  * individuals on behalf of the Apache Software Foundation.  For more
27  * information on the Apache Software Foundation, please see
28  * <http://www.apache.org/>.
29  *
30  */
31 
32 package org.apache.http.impl.client;
33 
34 import java.net.URI;
35 import java.net.URISyntaxException;
36 
37 import org.apache.http.HttpRequest;
38 import org.apache.http.ProtocolException;
39 import org.apache.http.ProtocolVersion;
40 import org.apache.http.RequestLine;
41 import org.apache.http.client.methods.HttpUriRequest;
42 import org.apache.http.message.AbstractHttpMessage;
43 import org.apache.http.message.BasicRequestLine;
44 import org.apache.http.params.HttpProtocolParams;
45 
46 /**
47  * A wrapper class for {@link HttpRequest}s that can be used to change
48  * properties of the current request without modifying the original
49  * object.
50  * </p>
51  * This class is also capable of resetting the request headers to
52  * the state of the original request.
53  *
54  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
55  *
56  * @version $Revision: 674186 $
57  *
58  * @since 4.0
59  */
60 public class RequestWrapper extends AbstractHttpMessage implements HttpUriRequest {
61 
62     private final HttpRequest original;
63 
64     private URI uri;
65     private String method;
66     private ProtocolVersion version;
67     private int execCount;
68 
RequestWrapper(final HttpRequest request)69     public RequestWrapper(final HttpRequest request) throws ProtocolException {
70         super();
71         if (request == null) {
72             throw new IllegalArgumentException("HTTP request may not be null");
73         }
74         this.original = request;
75         setParams(request.getParams());
76         // Make a copy of the original URI
77         if (request instanceof HttpUriRequest) {
78             this.uri = ((HttpUriRequest) request).getURI();
79             this.method = ((HttpUriRequest) request).getMethod();
80             this.version = null;
81         } else {
82             RequestLine requestLine = request.getRequestLine();
83             try {
84                 this.uri = new URI(requestLine.getUri());
85             } catch (URISyntaxException ex) {
86                 throw new ProtocolException("Invalid request URI: "
87                         + requestLine.getUri(), ex);
88             }
89             this.method = requestLine.getMethod();
90             this.version = request.getProtocolVersion();
91         }
92         this.execCount = 0;
93     }
94 
resetHeaders()95     public void resetHeaders() {
96         // Make a copy of original headers
97         this.headergroup.clear();
98         setHeaders(this.original.getAllHeaders());
99     }
100 
getMethod()101     public String getMethod() {
102         return this.method;
103     }
104 
setMethod(final String method)105     public void setMethod(final String method) {
106         if (method == null) {
107             throw new IllegalArgumentException("Method name may not be null");
108         }
109         this.method = method;
110     }
111 
getProtocolVersion()112     public ProtocolVersion getProtocolVersion() {
113         if (this.version != null) {
114             return this.version;
115         } else {
116             return HttpProtocolParams.getVersion(getParams());
117         }
118     }
119 
setProtocolVersion(final ProtocolVersion version)120     public void setProtocolVersion(final ProtocolVersion version) {
121         this.version = version;
122     }
123 
124 
getURI()125     public URI getURI() {
126         return this.uri;
127     }
128 
setURI(final URI uri)129     public void setURI(final URI uri) {
130         this.uri = uri;
131     }
132 
getRequestLine()133     public RequestLine getRequestLine() {
134         String method = getMethod();
135         ProtocolVersion ver = getProtocolVersion();
136         String uritext = null;
137         if (uri != null) {
138             uritext = uri.toASCIIString();
139         }
140         if (uritext == null || uritext.length() == 0) {
141             uritext = "/";
142         }
143         return new BasicRequestLine(method, uritext, ver);
144     }
145 
abort()146     public void abort() throws UnsupportedOperationException {
147         throw new UnsupportedOperationException();
148     }
149 
isAborted()150     public boolean isAborted() {
151         return false;
152     }
153 
getOriginal()154     public HttpRequest getOriginal() {
155         return this.original;
156     }
157 
isRepeatable()158     public boolean isRepeatable() {
159         return true;
160     }
161 
getExecCount()162     public int getExecCount() {
163         return this.execCount;
164     }
165 
incrementExecCount()166     public void incrementExecCount() {
167         this.execCount++;
168     }
169 
170 }
171