michael@0: /* michael@0: * ==================================================================== michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.client.methods; michael@0: michael@0: import java.io.IOException; michael@0: import java.net.URI; michael@0: import java.util.concurrent.locks.Lock; michael@0: import java.util.concurrent.locks.ReentrantLock; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; michael@0: michael@0: import ch.boye.httpclientandroidlib.ProtocolVersion; michael@0: import ch.boye.httpclientandroidlib.RequestLine; michael@0: import ch.boye.httpclientandroidlib.client.utils.CloneUtils; michael@0: import ch.boye.httpclientandroidlib.conn.ClientConnectionRequest; michael@0: import ch.boye.httpclientandroidlib.conn.ConnectionReleaseTrigger; michael@0: import ch.boye.httpclientandroidlib.message.AbstractHttpMessage; michael@0: import ch.boye.httpclientandroidlib.message.BasicRequestLine; michael@0: import ch.boye.httpclientandroidlib.message.HeaderGroup; michael@0: import ch.boye.httpclientandroidlib.params.HttpParams; michael@0: import ch.boye.httpclientandroidlib.params.HttpProtocolParams; michael@0: michael@0: /** michael@0: * Basic implementation of an HTTP request that can be modified. michael@0: * michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @NotThreadSafe michael@0: public abstract class HttpRequestBase extends AbstractHttpMessage michael@0: implements HttpUriRequest, AbortableHttpRequest, Cloneable { michael@0: michael@0: private Lock abortLock; michael@0: michael@0: private boolean aborted; michael@0: michael@0: private URI uri; michael@0: private ClientConnectionRequest connRequest; michael@0: private ConnectionReleaseTrigger releaseTrigger; michael@0: michael@0: public HttpRequestBase() { michael@0: super(); michael@0: this.abortLock = new ReentrantLock(); michael@0: } michael@0: michael@0: public abstract String getMethod(); michael@0: michael@0: public ProtocolVersion getProtocolVersion() { michael@0: return HttpProtocolParams.getVersion(getParams()); michael@0: } michael@0: michael@0: /** michael@0: * Returns the original request URI. michael@0: *

michael@0: * Please note URI remains unchanged in the course of request execution and michael@0: * is not updated if the request is redirected to another location. michael@0: */ michael@0: public URI getURI() { michael@0: return this.uri; michael@0: } michael@0: michael@0: public RequestLine getRequestLine() { michael@0: String method = getMethod(); michael@0: ProtocolVersion ver = getProtocolVersion(); michael@0: URI uri = getURI(); michael@0: String uritext = null; michael@0: if (uri != null) { michael@0: uritext = uri.toASCIIString(); michael@0: } michael@0: if (uritext == null || uritext.length() == 0) { michael@0: uritext = "/"; michael@0: } michael@0: return new BasicRequestLine(method, uritext, ver); michael@0: } michael@0: michael@0: public void setURI(final URI uri) { michael@0: this.uri = uri; michael@0: } michael@0: michael@0: public void setConnectionRequest(final ClientConnectionRequest connRequest) michael@0: throws IOException { michael@0: this.abortLock.lock(); michael@0: try { michael@0: if (this.aborted) { michael@0: throw new IOException("Request already aborted"); michael@0: } michael@0: michael@0: this.releaseTrigger = null; michael@0: this.connRequest = connRequest; michael@0: } finally { michael@0: this.abortLock.unlock(); michael@0: } michael@0: } michael@0: michael@0: public void setReleaseTrigger(final ConnectionReleaseTrigger releaseTrigger) michael@0: throws IOException { michael@0: this.abortLock.lock(); michael@0: try { michael@0: if (this.aborted) { michael@0: throw new IOException("Request already aborted"); michael@0: } michael@0: michael@0: this.connRequest = null; michael@0: this.releaseTrigger = releaseTrigger; michael@0: } finally { michael@0: this.abortLock.unlock(); michael@0: } michael@0: } michael@0: michael@0: public void abort() { michael@0: ClientConnectionRequest localRequest; michael@0: ConnectionReleaseTrigger localTrigger; michael@0: michael@0: this.abortLock.lock(); michael@0: try { michael@0: if (this.aborted) { michael@0: return; michael@0: } michael@0: this.aborted = true; michael@0: michael@0: localRequest = connRequest; michael@0: localTrigger = releaseTrigger; michael@0: } finally { michael@0: this.abortLock.unlock(); michael@0: } michael@0: michael@0: // Trigger the callbacks outside of the lock, to prevent michael@0: // deadlocks in the scenario where the callbacks have michael@0: // their own locks that may be used while calling michael@0: // setReleaseTrigger or setConnectionRequest. michael@0: if (localRequest != null) { michael@0: localRequest.abortRequest(); michael@0: } michael@0: if (localTrigger != null) { michael@0: try { michael@0: localTrigger.abortConnection(); michael@0: } catch (IOException ex) { michael@0: // ignore michael@0: } michael@0: } michael@0: } michael@0: michael@0: public boolean isAborted() { michael@0: return this.aborted; michael@0: } michael@0: michael@0: @Override michael@0: public Object clone() throws CloneNotSupportedException { michael@0: HttpRequestBase clone = (HttpRequestBase) super.clone(); michael@0: clone.abortLock = new ReentrantLock(); michael@0: clone.aborted = false; michael@0: clone.releaseTrigger = null; michael@0: clone.connRequest = null; michael@0: clone.headergroup = (HeaderGroup) CloneUtils.clone(this.headergroup); michael@0: clone.params = (HttpParams) CloneUtils.clone(this.params); michael@0: return clone; michael@0: } michael@0: michael@0: }