1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/client/RequestWrapper.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,166 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.impl.client; 1.32 + 1.33 +import java.net.URI; 1.34 +import java.net.URISyntaxException; 1.35 + 1.36 +import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; 1.37 + 1.38 +import ch.boye.httpclientandroidlib.HttpRequest; 1.39 +import ch.boye.httpclientandroidlib.ProtocolException; 1.40 +import ch.boye.httpclientandroidlib.ProtocolVersion; 1.41 +import ch.boye.httpclientandroidlib.RequestLine; 1.42 +import ch.boye.httpclientandroidlib.client.methods.HttpUriRequest; 1.43 +import ch.boye.httpclientandroidlib.message.AbstractHttpMessage; 1.44 +import ch.boye.httpclientandroidlib.message.BasicRequestLine; 1.45 +import ch.boye.httpclientandroidlib.params.HttpProtocolParams; 1.46 + 1.47 +/** 1.48 + * A wrapper class for {@link HttpRequest}s that can be used to change 1.49 + * properties of the current request without modifying the original 1.50 + * object. 1.51 + * </p> 1.52 + * This class is also capable of resetting the request headers to 1.53 + * the state of the original request. 1.54 + * 1.55 + * 1.56 + * @since 4.0 1.57 + */ 1.58 +@NotThreadSafe 1.59 +public class RequestWrapper extends AbstractHttpMessage implements HttpUriRequest { 1.60 + 1.61 + private final HttpRequest original; 1.62 + 1.63 + private URI uri; 1.64 + private String method; 1.65 + private ProtocolVersion version; 1.66 + private int execCount; 1.67 + 1.68 + public RequestWrapper(final HttpRequest request) throws ProtocolException { 1.69 + super(); 1.70 + if (request == null) { 1.71 + throw new IllegalArgumentException("HTTP request may not be null"); 1.72 + } 1.73 + this.original = request; 1.74 + setParams(request.getParams()); 1.75 + setHeaders(request.getAllHeaders()); 1.76 + // Make a copy of the original URI 1.77 + if (request instanceof HttpUriRequest) { 1.78 + this.uri = ((HttpUriRequest) request).getURI(); 1.79 + this.method = ((HttpUriRequest) request).getMethod(); 1.80 + this.version = null; 1.81 + } else { 1.82 + RequestLine requestLine = request.getRequestLine(); 1.83 + try { 1.84 + this.uri = new URI(requestLine.getUri()); 1.85 + } catch (URISyntaxException ex) { 1.86 + throw new ProtocolException("Invalid request URI: " 1.87 + + requestLine.getUri(), ex); 1.88 + } 1.89 + this.method = requestLine.getMethod(); 1.90 + this.version = request.getProtocolVersion(); 1.91 + } 1.92 + this.execCount = 0; 1.93 + } 1.94 + 1.95 + public void resetHeaders() { 1.96 + // Make a copy of original headers 1.97 + this.headergroup.clear(); 1.98 + setHeaders(this.original.getAllHeaders()); 1.99 + } 1.100 + 1.101 + public String getMethod() { 1.102 + return this.method; 1.103 + } 1.104 + 1.105 + public void setMethod(final String method) { 1.106 + if (method == null) { 1.107 + throw new IllegalArgumentException("Method name may not be null"); 1.108 + } 1.109 + this.method = method; 1.110 + } 1.111 + 1.112 + public ProtocolVersion getProtocolVersion() { 1.113 + if (this.version == null) { 1.114 + this.version = HttpProtocolParams.getVersion(getParams()); 1.115 + } 1.116 + return this.version; 1.117 + } 1.118 + 1.119 + public void setProtocolVersion(final ProtocolVersion version) { 1.120 + this.version = version; 1.121 + } 1.122 + 1.123 + 1.124 + public URI getURI() { 1.125 + return this.uri; 1.126 + } 1.127 + 1.128 + public void setURI(final URI uri) { 1.129 + this.uri = uri; 1.130 + } 1.131 + 1.132 + public RequestLine getRequestLine() { 1.133 + String method = getMethod(); 1.134 + ProtocolVersion ver = getProtocolVersion(); 1.135 + String uritext = null; 1.136 + if (uri != null) { 1.137 + uritext = uri.toASCIIString(); 1.138 + } 1.139 + if (uritext == null || uritext.length() == 0) { 1.140 + uritext = "/"; 1.141 + } 1.142 + return new BasicRequestLine(method, uritext, ver); 1.143 + } 1.144 + 1.145 + public void abort() throws UnsupportedOperationException { 1.146 + throw new UnsupportedOperationException(); 1.147 + } 1.148 + 1.149 + public boolean isAborted() { 1.150 + return false; 1.151 + } 1.152 + 1.153 + public HttpRequest getOriginal() { 1.154 + return this.original; 1.155 + } 1.156 + 1.157 + public boolean isRepeatable() { 1.158 + return true; 1.159 + } 1.160 + 1.161 + public int getExecCount() { 1.162 + return this.execCount; 1.163 + } 1.164 + 1.165 + public void incrementExecCount() { 1.166 + this.execCount++; 1.167 + } 1.168 + 1.169 +}