Home | History | Annotate | Download | only in methods
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/methods/HttpRequestBase.java $
      3  * $Revision: 674186 $
      4  * $Date: 2008-07-05 05:18:54 -0700 (Sat, 05 Jul 2008) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 
     32 package org.apache.http.client.methods;
     33 
     34 import java.io.IOException;
     35 import java.net.URI;
     36 import java.util.concurrent.locks.Lock;
     37 import java.util.concurrent.locks.ReentrantLock;
     38 
     39 import org.apache.http.ProtocolVersion;
     40 import org.apache.http.RequestLine;
     41 import org.apache.http.client.utils.CloneUtils;
     42 import org.apache.http.conn.ClientConnectionRequest;
     43 import org.apache.http.conn.ConnectionReleaseTrigger;
     44 import org.apache.http.message.AbstractHttpMessage;
     45 import org.apache.http.message.BasicRequestLine;
     46 import org.apache.http.message.HeaderGroup;
     47 import org.apache.http.params.HttpParams;
     48 import org.apache.http.params.HttpProtocolParams;
     49 
     50 /**
     51  * Basic implementation of an HTTP request that can be modified.
     52  *
     53  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     54  *
     55  * @version $Revision: 674186 $
     56  *
     57  * @since 4.0
     58  *
     59  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     60  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     61  *     for further details.
     62  */
     63 @Deprecated
     64 public abstract class HttpRequestBase extends AbstractHttpMessage
     65     implements HttpUriRequest, AbortableHttpRequest, Cloneable {
     66 
     67     private Lock abortLock;
     68 
     69     private boolean aborted;
     70 
     71     private URI uri;
     72     private ClientConnectionRequest connRequest;
     73     private ConnectionReleaseTrigger releaseTrigger;
     74 
     75     public HttpRequestBase() {
     76         super();
     77         this.abortLock = new ReentrantLock();
     78     }
     79 
     80     public abstract String getMethod();
     81 
     82     public ProtocolVersion getProtocolVersion() {
     83         return HttpProtocolParams.getVersion(getParams());
     84     }
     85 
     86     public URI getURI() {
     87         return this.uri;
     88     }
     89 
     90     public RequestLine getRequestLine() {
     91         String method = getMethod();
     92         ProtocolVersion ver = getProtocolVersion();
     93         URI uri = getURI();
     94         String uritext = null;
     95         if (uri != null) {
     96             uritext = uri.toASCIIString();
     97         }
     98         if (uritext == null || uritext.length() == 0) {
     99             uritext = "/";
    100         }
    101         return new BasicRequestLine(method, uritext, ver);
    102     }
    103 
    104     public void setURI(final URI uri) {
    105         this.uri = uri;
    106     }
    107 
    108     public void setConnectionRequest(final ClientConnectionRequest connRequest)
    109             throws IOException {
    110         this.abortLock.lock();
    111         try {
    112             if (this.aborted) {
    113                 throw new IOException("Request already aborted");
    114             }
    115 
    116             this.releaseTrigger = null;
    117             this.connRequest = connRequest;
    118         } finally {
    119             this.abortLock.unlock();
    120         }
    121     }
    122 
    123     public void setReleaseTrigger(final ConnectionReleaseTrigger releaseTrigger)
    124             throws IOException {
    125         this.abortLock.lock();
    126         try {
    127             if (this.aborted) {
    128                 throw new IOException("Request already aborted");
    129             }
    130 
    131             this.connRequest = null;
    132             this.releaseTrigger = releaseTrigger;
    133         } finally {
    134             this.abortLock.unlock();
    135         }
    136     }
    137 
    138     public void abort() {
    139         ClientConnectionRequest localRequest;
    140         ConnectionReleaseTrigger localTrigger;
    141 
    142         this.abortLock.lock();
    143         try {
    144             if (this.aborted) {
    145                 return;
    146             }
    147             this.aborted = true;
    148 
    149             localRequest = connRequest;
    150             localTrigger = releaseTrigger;
    151         } finally {
    152             this.abortLock.unlock();
    153         }
    154 
    155         // Trigger the callbacks outside of the lock, to prevent
    156         // deadlocks in the scenario where the callbacks have
    157         // their own locks that may be used while calling
    158         // setReleaseTrigger or setConnectionRequest.
    159         if (localRequest != null) {
    160             localRequest.abortRequest();
    161         }
    162         if (localTrigger != null) {
    163             try {
    164                 localTrigger.abortConnection();
    165             } catch (IOException ex) {
    166                 // ignore
    167             }
    168         }
    169     }
    170 
    171     public boolean isAborted() {
    172         return this.aborted;
    173     }
    174 
    175     @Override
    176     public Object clone() throws CloneNotSupportedException {
    177         HttpRequestBase clone = (HttpRequestBase) super.clone();
    178         clone.abortLock = new ReentrantLock();
    179         clone.aborted = false;
    180         clone.releaseTrigger = null;
    181         clone.connRequest = null;
    182         clone.headergroup = (HeaderGroup) CloneUtils.clone(this.headergroup);
    183         clone.params = (HttpParams) CloneUtils.clone(this.params);
    184         return clone;
    185     }
    186 
    187 }
    188