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