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
32package org.apache.http.impl.client;
33
34import java.net.URI;
35import java.net.URISyntaxException;
36
37import org.apache.http.HttpRequest;
38import org.apache.http.ProtocolException;
39import org.apache.http.ProtocolVersion;
40import org.apache.http.RequestLine;
41import org.apache.http.client.methods.HttpUriRequest;
42import org.apache.http.message.AbstractHttpMessage;
43import org.apache.http.message.BasicRequestLine;
44import 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 * @deprecated Please use {@link java.net.URL#openConnection} instead.
61 *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
62 *     for further details.
63 */
64@Deprecated
65public class RequestWrapper extends AbstractHttpMessage implements HttpUriRequest {
66
67    private final HttpRequest original;
68
69    private URI uri;
70    private String method;
71    private ProtocolVersion version;
72    private int execCount;
73
74    public RequestWrapper(final HttpRequest request) throws ProtocolException {
75        super();
76        if (request == null) {
77            throw new IllegalArgumentException("HTTP request may not be null");
78        }
79        this.original = request;
80        setParams(request.getParams());
81        // Make a copy of the original URI
82        if (request instanceof HttpUriRequest) {
83            this.uri = ((HttpUriRequest) request).getURI();
84            this.method = ((HttpUriRequest) request).getMethod();
85            this.version = null;
86        } else {
87            RequestLine requestLine = request.getRequestLine();
88            try {
89                this.uri = new URI(requestLine.getUri());
90            } catch (URISyntaxException ex) {
91                throw new ProtocolException("Invalid request URI: "
92                        + requestLine.getUri(), ex);
93            }
94            this.method = requestLine.getMethod();
95            this.version = request.getProtocolVersion();
96        }
97        this.execCount = 0;
98    }
99
100    public void resetHeaders() {
101        // Make a copy of original headers
102        this.headergroup.clear();
103        setHeaders(this.original.getAllHeaders());
104    }
105
106    public String getMethod() {
107        return this.method;
108    }
109
110    public void setMethod(final String method) {
111        if (method == null) {
112            throw new IllegalArgumentException("Method name may not be null");
113        }
114        this.method = method;
115    }
116
117    public ProtocolVersion getProtocolVersion() {
118        if (this.version != null) {
119            return this.version;
120        } else {
121            return HttpProtocolParams.getVersion(getParams());
122        }
123    }
124
125    public void setProtocolVersion(final ProtocolVersion version) {
126        this.version = version;
127    }
128
129
130    public URI getURI() {
131        return this.uri;
132    }
133
134    public void setURI(final URI uri) {
135        this.uri = uri;
136    }
137
138    public RequestLine getRequestLine() {
139        String method = getMethod();
140        ProtocolVersion ver = getProtocolVersion();
141        String uritext = null;
142        if (uri != null) {
143            uritext = uri.toASCIIString();
144        }
145        if (uritext == null || uritext.length() == 0) {
146            uritext = "/";
147        }
148        return new BasicRequestLine(method, uritext, ver);
149    }
150
151    public void abort() throws UnsupportedOperationException {
152        throw new UnsupportedOperationException();
153    }
154
155    public boolean isAborted() {
156        return false;
157    }
158
159    public HttpRequest getOriginal() {
160        return this.original;
161    }
162
163    public boolean isRepeatable() {
164        return true;
165    }
166
167    public int getExecCount() {
168        return this.execCount;
169    }
170
171    public void incrementExecCount() {
172        this.execCount++;
173    }
174
175}
176