michael@0: /* michael@0: * ==================================================================== michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.impl.client; michael@0: michael@0: import java.net.URI; michael@0: import java.net.URISyntaxException; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; michael@0: michael@0: import ch.boye.httpclientandroidlib.HttpRequest; michael@0: import ch.boye.httpclientandroidlib.ProtocolException; michael@0: import ch.boye.httpclientandroidlib.ProtocolVersion; michael@0: import ch.boye.httpclientandroidlib.RequestLine; michael@0: import ch.boye.httpclientandroidlib.client.methods.HttpUriRequest; michael@0: import ch.boye.httpclientandroidlib.message.AbstractHttpMessage; michael@0: import ch.boye.httpclientandroidlib.message.BasicRequestLine; michael@0: import ch.boye.httpclientandroidlib.params.HttpProtocolParams; michael@0: michael@0: /** michael@0: * A wrapper class for {@link HttpRequest}s that can be used to change michael@0: * properties of the current request without modifying the original michael@0: * object. michael@0: *

michael@0: * This class is also capable of resetting the request headers to michael@0: * the state of the original request. michael@0: * michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @NotThreadSafe michael@0: public class RequestWrapper extends AbstractHttpMessage implements HttpUriRequest { michael@0: michael@0: private final HttpRequest original; michael@0: michael@0: private URI uri; michael@0: private String method; michael@0: private ProtocolVersion version; michael@0: private int execCount; michael@0: michael@0: public RequestWrapper(final HttpRequest request) throws ProtocolException { michael@0: super(); michael@0: if (request == null) { michael@0: throw new IllegalArgumentException("HTTP request may not be null"); michael@0: } michael@0: this.original = request; michael@0: setParams(request.getParams()); michael@0: setHeaders(request.getAllHeaders()); michael@0: // Make a copy of the original URI michael@0: if (request instanceof HttpUriRequest) { michael@0: this.uri = ((HttpUriRequest) request).getURI(); michael@0: this.method = ((HttpUriRequest) request).getMethod(); michael@0: this.version = null; michael@0: } else { michael@0: RequestLine requestLine = request.getRequestLine(); michael@0: try { michael@0: this.uri = new URI(requestLine.getUri()); michael@0: } catch (URISyntaxException ex) { michael@0: throw new ProtocolException("Invalid request URI: " michael@0: + requestLine.getUri(), ex); michael@0: } michael@0: this.method = requestLine.getMethod(); michael@0: this.version = request.getProtocolVersion(); michael@0: } michael@0: this.execCount = 0; michael@0: } michael@0: michael@0: public void resetHeaders() { michael@0: // Make a copy of original headers michael@0: this.headergroup.clear(); michael@0: setHeaders(this.original.getAllHeaders()); michael@0: } michael@0: michael@0: public String getMethod() { michael@0: return this.method; michael@0: } michael@0: michael@0: public void setMethod(final String method) { michael@0: if (method == null) { michael@0: throw new IllegalArgumentException("Method name may not be null"); michael@0: } michael@0: this.method = method; michael@0: } michael@0: michael@0: public ProtocolVersion getProtocolVersion() { michael@0: if (this.version == null) { michael@0: this.version = HttpProtocolParams.getVersion(getParams()); michael@0: } michael@0: return this.version; michael@0: } michael@0: michael@0: public void setProtocolVersion(final ProtocolVersion version) { michael@0: this.version = version; michael@0: } michael@0: michael@0: michael@0: public URI getURI() { michael@0: return this.uri; michael@0: } michael@0: michael@0: public void setURI(final URI uri) { michael@0: this.uri = uri; michael@0: } michael@0: michael@0: public RequestLine getRequestLine() { michael@0: String method = getMethod(); michael@0: ProtocolVersion ver = getProtocolVersion(); michael@0: String uritext = null; michael@0: if (uri != null) { michael@0: uritext = uri.toASCIIString(); michael@0: } michael@0: if (uritext == null || uritext.length() == 0) { michael@0: uritext = "/"; michael@0: } michael@0: return new BasicRequestLine(method, uritext, ver); michael@0: } michael@0: michael@0: public void abort() throws UnsupportedOperationException { michael@0: throw new UnsupportedOperationException(); michael@0: } michael@0: michael@0: public boolean isAborted() { michael@0: return false; michael@0: } michael@0: michael@0: public HttpRequest getOriginal() { michael@0: return this.original; michael@0: } michael@0: michael@0: public boolean isRepeatable() { michael@0: return true; michael@0: } michael@0: michael@0: public int getExecCount() { michael@0: return this.execCount; michael@0: } michael@0: michael@0: public void incrementExecCount() { michael@0: this.execCount++; michael@0: } michael@0: michael@0: }