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 public abstract class HttpRequestBase extends AbstractHttpMessage
     60     implements HttpUriRequest, AbortableHttpRequest, Cloneable {
     61 
     62     private Lock abortLock;
     63 
     64     private boolean aborted;
     65 
     66     private URI uri;
     67     private ClientConnectionRequest connRequest;
     68     private ConnectionReleaseTrigger releaseTrigger;
     69 
     70     public HttpRequestBase() {
     71         super();
     72         this.abortLock = new ReentrantLock();
     73     }
     74 
     75     public abstract String getMethod();
     76 
     77     public ProtocolVersion getProtocolVersion() {
     78         return HttpProtocolParams.getVersion(getParams());
     79     }
     80 
     81     public URI getURI() {
     82         return this.uri;
     83     }
     84 
     85     public RequestLine getRequestLine() {
     86         String method = getMethod();
     87         ProtocolVersion ver = getProtocolVersion();
     88         URI uri = getURI();
     89         String uritext = null;
     90         if (uri != null) {
     91             uritext = uri.toASCIIString();
     92         }
     93         if (uritext == null || uritext.length() == 0) {
     94             uritext = "/";
     95         }
     96         return new BasicRequestLine(method, uritext, ver);
     97     }
     98 
     99     public void setURI(final URI uri) {
    100         this.uri = uri;
    101     }
    102 
    103     public void setConnectionRequest(final ClientConnectionRequest connRequest)
    104             throws IOException {
    105         this.abortLock.lock();
    106         try {
    107             if (this.aborted) {
    108                 throw new IOException("Request already aborted");
    109             }
    110 
    111             this.releaseTrigger = null;
    112             this.connRequest = connRequest;
    113         } finally {
    114             this.abortLock.unlock();
    115         }
    116     }
    117 
    118     public void setReleaseTrigger(final ConnectionReleaseTrigger releaseTrigger)
    119             throws IOException {
    120         this.abortLock.lock();
    121         try {
    122             if (this.aborted) {
    123                 throw new IOException("Request already aborted");
    124             }
    125 
    126             this.connRequest = null;
    127             this.releaseTrigger = releaseTrigger;
    128         } finally {
    129             this.abortLock.unlock();
    130         }
    131     }
    132 
    133     public void abort() {
    134         ClientConnectionRequest localRequest;
    135         ConnectionReleaseTrigger localTrigger;
    136 
    137         this.abortLock.lock();
    138         try {
    139             if (this.aborted) {
    140                 return;
    141             }
    142             this.aborted = true;
    143 
    144             localRequest = connRequest;
    145             localTrigger = releaseTrigger;
    146         } finally {
    147             this.abortLock.unlock();
    148         }
    149 
    150         // Trigger the callbacks outside of the lock, to prevent
    151         // deadlocks in the scenario where the callbacks have
    152         // their own locks that may be used while calling
    153         // setReleaseTrigger or setConnectionRequest.
    154         if (localRequest != null) {
    155             localRequest.abortRequest();
    156         }
    157         if (localTrigger != null) {
    158             try {
    159                 localTrigger.abortConnection();
    160             } catch (IOException ex) {
    161                 // ignore
    162             }
    163         }
    164     }
    165 
    166     public boolean isAborted() {
    167         return this.aborted;
    168     }
    169 
    170     @Override
    171     public Object clone() throws CloneNotSupportedException {
    172         HttpRequestBase clone = (HttpRequestBase) super.clone();
    173         clone.abortLock = new ReentrantLock();
    174         clone.aborted = false;
    175         clone.releaseTrigger = null;
    176         clone.connRequest = null;
    177         clone.headergroup = (HeaderGroup) CloneUtils.clone(this.headergroup);
    178         clone.params = (HttpParams) CloneUtils.clone(this.params);
    179         return clone;
    180     }
    181 
    182 }
    183