mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/methods/HttpRequestBase.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/methods/HttpRequestBase.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,184 @@
     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.client.methods;
    1.32 +
    1.33 +import java.io.IOException;
    1.34 +import java.net.URI;
    1.35 +import java.util.concurrent.locks.Lock;
    1.36 +import java.util.concurrent.locks.ReentrantLock;
    1.37 +
    1.38 +import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
    1.39 +
    1.40 +import ch.boye.httpclientandroidlib.ProtocolVersion;
    1.41 +import ch.boye.httpclientandroidlib.RequestLine;
    1.42 +import ch.boye.httpclientandroidlib.client.utils.CloneUtils;
    1.43 +import ch.boye.httpclientandroidlib.conn.ClientConnectionRequest;
    1.44 +import ch.boye.httpclientandroidlib.conn.ConnectionReleaseTrigger;
    1.45 +import ch.boye.httpclientandroidlib.message.AbstractHttpMessage;
    1.46 +import ch.boye.httpclientandroidlib.message.BasicRequestLine;
    1.47 +import ch.boye.httpclientandroidlib.message.HeaderGroup;
    1.48 +import ch.boye.httpclientandroidlib.params.HttpParams;
    1.49 +import ch.boye.httpclientandroidlib.params.HttpProtocolParams;
    1.50 +
    1.51 +/**
    1.52 + * Basic implementation of an HTTP request that can be modified.
    1.53 + *
    1.54 + *
    1.55 + * @since 4.0
    1.56 + */
    1.57 +@NotThreadSafe
    1.58 +public abstract class HttpRequestBase extends AbstractHttpMessage
    1.59 +    implements HttpUriRequest, AbortableHttpRequest, Cloneable {
    1.60 +
    1.61 +    private Lock abortLock;
    1.62 +
    1.63 +    private boolean aborted;
    1.64 +
    1.65 +    private URI uri;
    1.66 +    private ClientConnectionRequest connRequest;
    1.67 +    private ConnectionReleaseTrigger releaseTrigger;
    1.68 +
    1.69 +    public HttpRequestBase() {
    1.70 +        super();
    1.71 +        this.abortLock = new ReentrantLock();
    1.72 +    }
    1.73 +
    1.74 +    public abstract String getMethod();
    1.75 +
    1.76 +    public ProtocolVersion getProtocolVersion() {
    1.77 +        return HttpProtocolParams.getVersion(getParams());
    1.78 +    }
    1.79 +
    1.80 +    /**
    1.81 +     * Returns the original request URI.
    1.82 +     * <p>
    1.83 +     * Please note URI remains unchanged in the course of request execution and
    1.84 +     * is not updated if the request is redirected to another location.
    1.85 +     */
    1.86 +    public URI getURI() {
    1.87 +        return this.uri;
    1.88 +    }
    1.89 +
    1.90 +    public RequestLine getRequestLine() {
    1.91 +        String method = getMethod();
    1.92 +        ProtocolVersion ver = getProtocolVersion();
    1.93 +        URI uri = getURI();
    1.94 +        String uritext = null;
    1.95 +        if (uri != null) {
    1.96 +            uritext = uri.toASCIIString();
    1.97 +        }
    1.98 +        if (uritext == null || uritext.length() == 0) {
    1.99 +            uritext = "/";
   1.100 +        }
   1.101 +        return new BasicRequestLine(method, uritext, ver);
   1.102 +    }
   1.103 +
   1.104 +    public void setURI(final URI uri) {
   1.105 +        this.uri = uri;
   1.106 +    }
   1.107 +
   1.108 +    public void setConnectionRequest(final ClientConnectionRequest connRequest)
   1.109 +            throws IOException {
   1.110 +        this.abortLock.lock();
   1.111 +        try {
   1.112 +            if (this.aborted) {
   1.113 +                throw new IOException("Request already aborted");
   1.114 +            }
   1.115 +
   1.116 +            this.releaseTrigger = null;
   1.117 +            this.connRequest = connRequest;
   1.118 +        } finally {
   1.119 +            this.abortLock.unlock();
   1.120 +        }
   1.121 +    }
   1.122 +
   1.123 +    public void setReleaseTrigger(final ConnectionReleaseTrigger releaseTrigger)
   1.124 +            throws IOException {
   1.125 +        this.abortLock.lock();
   1.126 +        try {
   1.127 +            if (this.aborted) {
   1.128 +                throw new IOException("Request already aborted");
   1.129 +            }
   1.130 +
   1.131 +            this.connRequest = null;
   1.132 +            this.releaseTrigger = releaseTrigger;
   1.133 +        } finally {
   1.134 +            this.abortLock.unlock();
   1.135 +        }
   1.136 +    }
   1.137 +
   1.138 +    public void abort() {
   1.139 +        ClientConnectionRequest localRequest;
   1.140 +        ConnectionReleaseTrigger localTrigger;
   1.141 +
   1.142 +        this.abortLock.lock();
   1.143 +        try {
   1.144 +            if (this.aborted) {
   1.145 +                return;
   1.146 +            }
   1.147 +            this.aborted = true;
   1.148 +
   1.149 +            localRequest = connRequest;
   1.150 +            localTrigger = releaseTrigger;
   1.151 +        } finally {
   1.152 +            this.abortLock.unlock();
   1.153 +        }
   1.154 +
   1.155 +        // Trigger the callbacks outside of the lock, to prevent
   1.156 +        // deadlocks in the scenario where the callbacks have
   1.157 +        // their own locks that may be used while calling
   1.158 +        // setReleaseTrigger or setConnectionRequest.
   1.159 +        if (localRequest != null) {
   1.160 +            localRequest.abortRequest();
   1.161 +        }
   1.162 +        if (localTrigger != null) {
   1.163 +            try {
   1.164 +                localTrigger.abortConnection();
   1.165 +            } catch (IOException ex) {
   1.166 +                // ignore
   1.167 +            }
   1.168 +        }
   1.169 +    }
   1.170 +
   1.171 +    public boolean isAborted() {
   1.172 +        return this.aborted;
   1.173 +    }
   1.174 +
   1.175 +    @Override
   1.176 +    public Object clone() throws CloneNotSupportedException {
   1.177 +        HttpRequestBase clone = (HttpRequestBase) super.clone();
   1.178 +        clone.abortLock = new ReentrantLock();
   1.179 +        clone.aborted = false;
   1.180 +        clone.releaseTrigger = null;
   1.181 +        clone.connRequest = null;
   1.182 +        clone.headergroup = (HeaderGroup) CloneUtils.clone(this.headergroup);
   1.183 +        clone.params = (HttpParams) CloneUtils.clone(this.params);
   1.184 +        return clone;
   1.185 +    }
   1.186 +
   1.187 +}

mercurial