mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/client/RequestWrapper.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial