001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.impl.client;
029
030import java.net.URI;
031import java.net.URISyntaxException;
032
033import org.apache.http.HttpRequest;
034import org.apache.http.ProtocolException;
035import org.apache.http.ProtocolVersion;
036import org.apache.http.RequestLine;
037import org.apache.http.client.methods.HttpUriRequest;
038import org.apache.http.message.AbstractHttpMessage;
039import org.apache.http.message.BasicRequestLine;
040import org.apache.http.params.HttpProtocolParams;
041import org.apache.http.util.Args;
042
043/**
044 * A wrapper class for {@link HttpRequest}s that can be used to change
045 * properties of the current request without modifying the original
046 * object.
047 * <p>
048 * This class is also capable of resetting the request headers to
049 * the state of the original request.
050 * </p>
051 *
052 * @since 4.0
053 *
054 * @deprecated (4.3) do not use.
055 */
056@Deprecated
057public class RequestWrapper extends AbstractHttpMessage implements HttpUriRequest {
058
059    private final HttpRequest original;
060
061    private URI uri;
062    private String method;
063    private ProtocolVersion version;
064    private int execCount;
065
066    public RequestWrapper(final HttpRequest request) throws ProtocolException {
067        super();
068        Args.notNull(request, "HTTP request");
069        this.original = request;
070        setParams(request.getParams());
071        setHeaders(request.getAllHeaders());
072        // Make a copy of the original URI
073        if (request instanceof HttpUriRequest) {
074            this.uri = ((HttpUriRequest) request).getURI();
075            this.method = ((HttpUriRequest) request).getMethod();
076            this.version = null;
077        } else {
078            final RequestLine requestLine = request.getRequestLine();
079            try {
080                this.uri = new URI(requestLine.getUri());
081            } catch (final URISyntaxException ex) {
082                throw new ProtocolException("Invalid request URI: "
083                        + requestLine.getUri(), ex);
084            }
085            this.method = requestLine.getMethod();
086            this.version = request.getProtocolVersion();
087        }
088        this.execCount = 0;
089    }
090
091    public void resetHeaders() {
092        // Make a copy of original headers
093        this.headergroup.clear();
094        setHeaders(this.original.getAllHeaders());
095    }
096
097    @Override
098    public String getMethod() {
099        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}