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; |
michael@0 | 29 | |
michael@0 | 30 | import java.io.IOException; |
michael@0 | 31 | |
michael@0 | 32 | import ch.boye.httpclientandroidlib.HttpHost; |
michael@0 | 33 | import ch.boye.httpclientandroidlib.HttpRequest; |
michael@0 | 34 | import ch.boye.httpclientandroidlib.HttpResponse; |
michael@0 | 35 | import ch.boye.httpclientandroidlib.client.methods.HttpUriRequest; |
michael@0 | 36 | import ch.boye.httpclientandroidlib.conn.ClientConnectionManager; |
michael@0 | 37 | import ch.boye.httpclientandroidlib.params.HttpParams; |
michael@0 | 38 | import ch.boye.httpclientandroidlib.protocol.HttpContext; |
michael@0 | 39 | |
michael@0 | 40 | /** |
michael@0 | 41 | * This interface represents only the most basic contract for HTTP request |
michael@0 | 42 | * execution. It imposes no restrictions or particular details on the request |
michael@0 | 43 | * execution process and leaves the specifics of state management, |
michael@0 | 44 | * authentication and redirect handling up to individual implementations. |
michael@0 | 45 | * This should make it easier to decorate the interface with additional |
michael@0 | 46 | * functionality such as response content caching. |
michael@0 | 47 | * <p/> |
michael@0 | 48 | * The usual execution flow can be demonstrated by the code snippet below: |
michael@0 | 49 | * <PRE> |
michael@0 | 50 | * HttpClient httpclient = new DefaultHttpClient(); |
michael@0 | 51 | * |
michael@0 | 52 | * // Prepare a request object |
michael@0 | 53 | * HttpGet httpget = new HttpGet("http://www.apache.org/"); |
michael@0 | 54 | * |
michael@0 | 55 | * // Execute the request |
michael@0 | 56 | * HttpResponse response = httpclient.execute(httpget); |
michael@0 | 57 | * |
michael@0 | 58 | * // Examine the response status |
michael@0 | 59 | * System.out.println(response.getStatusLine()); |
michael@0 | 60 | * |
michael@0 | 61 | * // Get hold of the response entity |
michael@0 | 62 | * HttpEntity entity = response.getEntity(); |
michael@0 | 63 | * |
michael@0 | 64 | * // If the response does not enclose an entity, there is no need |
michael@0 | 65 | * // to worry about connection release |
michael@0 | 66 | * if (entity != null) { |
michael@0 | 67 | * InputStream instream = entity.getContent(); |
michael@0 | 68 | * try { |
michael@0 | 69 | * |
michael@0 | 70 | * BufferedReader reader = new BufferedReader( |
michael@0 | 71 | * new InputStreamReader(instream)); |
michael@0 | 72 | * // do something useful with the response |
michael@0 | 73 | * System.out.println(reader.readLine()); |
michael@0 | 74 | * |
michael@0 | 75 | * } catch (IOException ex) { |
michael@0 | 76 | * |
michael@0 | 77 | * // In case of an IOException the connection will be released |
michael@0 | 78 | * // back to the connection manager automatically |
michael@0 | 79 | * throw ex; |
michael@0 | 80 | * |
michael@0 | 81 | * } catch (RuntimeException ex) { |
michael@0 | 82 | * |
michael@0 | 83 | * // In case of an unexpected exception you may want to abort |
michael@0 | 84 | * // the HTTP request in order to shut down the underlying |
michael@0 | 85 | * // connection and release it back to the connection manager. |
michael@0 | 86 | * httpget.abort(); |
michael@0 | 87 | * throw ex; |
michael@0 | 88 | * |
michael@0 | 89 | * } finally { |
michael@0 | 90 | * |
michael@0 | 91 | * // Closing the input stream will trigger connection release |
michael@0 | 92 | * instream.close(); |
michael@0 | 93 | * |
michael@0 | 94 | * } |
michael@0 | 95 | * |
michael@0 | 96 | * // When HttpClient instance is no longer needed, |
michael@0 | 97 | * // shut down the connection manager to ensure |
michael@0 | 98 | * // immediate deallocation of all system resources |
michael@0 | 99 | * httpclient.getConnectionManager().shutdown(); |
michael@0 | 100 | * } |
michael@0 | 101 | * </PRE> |
michael@0 | 102 | * |
michael@0 | 103 | * @since 4.0 |
michael@0 | 104 | */ |
michael@0 | 105 | public interface HttpClient { |
michael@0 | 106 | |
michael@0 | 107 | |
michael@0 | 108 | /** |
michael@0 | 109 | * Obtains the parameters for this client. |
michael@0 | 110 | * These parameters will become defaults for all requests being |
michael@0 | 111 | * executed with this client, and for the parameters of |
michael@0 | 112 | * dependent objects in this client. |
michael@0 | 113 | * |
michael@0 | 114 | * @return the default parameters |
michael@0 | 115 | */ |
michael@0 | 116 | HttpParams getParams(); |
michael@0 | 117 | |
michael@0 | 118 | /** |
michael@0 | 119 | * Obtains the connection manager used by this client. |
michael@0 | 120 | * |
michael@0 | 121 | * @return the connection manager |
michael@0 | 122 | */ |
michael@0 | 123 | ClientConnectionManager getConnectionManager(); |
michael@0 | 124 | |
michael@0 | 125 | /** |
michael@0 | 126 | * Executes a request using the default context. |
michael@0 | 127 | * |
michael@0 | 128 | * @param request the request to execute |
michael@0 | 129 | * |
michael@0 | 130 | * @return the response to the request. This is always a final response, |
michael@0 | 131 | * never an intermediate response with an 1xx status code. |
michael@0 | 132 | * Whether redirects or authentication challenges will be returned |
michael@0 | 133 | * or handled automatically depends on the implementation and |
michael@0 | 134 | * configuration of this client. |
michael@0 | 135 | * @throws IOException in case of a problem or the connection was aborted |
michael@0 | 136 | * @throws ClientProtocolException in case of an http protocol error |
michael@0 | 137 | */ |
michael@0 | 138 | HttpResponse execute(HttpUriRequest request) |
michael@0 | 139 | throws IOException, ClientProtocolException; |
michael@0 | 140 | |
michael@0 | 141 | /** |
michael@0 | 142 | * Executes a request using the given context. |
michael@0 | 143 | * The route to the target will be determined by the HTTP client. |
michael@0 | 144 | * |
michael@0 | 145 | * @param request the request to execute |
michael@0 | 146 | * @param context the context to use for the execution, or |
michael@0 | 147 | * <code>null</code> to use the default context |
michael@0 | 148 | * |
michael@0 | 149 | * @return the response to the request. This is always a final response, |
michael@0 | 150 | * never an intermediate response with an 1xx status code. |
michael@0 | 151 | * Whether redirects or authentication challenges will be returned |
michael@0 | 152 | * or handled automatically depends on the implementation and |
michael@0 | 153 | * configuration of this client. |
michael@0 | 154 | * @throws IOException in case of a problem or the connection was aborted |
michael@0 | 155 | * @throws ClientProtocolException in case of an http protocol error |
michael@0 | 156 | */ |
michael@0 | 157 | HttpResponse execute(HttpUriRequest request, HttpContext context) |
michael@0 | 158 | throws IOException, ClientProtocolException; |
michael@0 | 159 | |
michael@0 | 160 | /** |
michael@0 | 161 | * Executes a request to the target using the default context. |
michael@0 | 162 | * |
michael@0 | 163 | * @param target the target host for the request. |
michael@0 | 164 | * Implementations may accept <code>null</code> |
michael@0 | 165 | * if they can still determine a route, for example |
michael@0 | 166 | * to a default target or by inspecting the request. |
michael@0 | 167 | * @param request the request to execute |
michael@0 | 168 | * |
michael@0 | 169 | * @return the response to the request. This is always a final response, |
michael@0 | 170 | * never an intermediate response with an 1xx status code. |
michael@0 | 171 | * Whether redirects or authentication challenges will be returned |
michael@0 | 172 | * or handled automatically depends on the implementation and |
michael@0 | 173 | * configuration of this client. |
michael@0 | 174 | * @throws IOException in case of a problem or the connection was aborted |
michael@0 | 175 | * @throws ClientProtocolException in case of an http protocol error |
michael@0 | 176 | */ |
michael@0 | 177 | HttpResponse execute(HttpHost target, HttpRequest request) |
michael@0 | 178 | throws IOException, ClientProtocolException; |
michael@0 | 179 | |
michael@0 | 180 | /** |
michael@0 | 181 | * Executes a request to the target using the given context. |
michael@0 | 182 | * |
michael@0 | 183 | * @param target the target host for the request. |
michael@0 | 184 | * Implementations may accept <code>null</code> |
michael@0 | 185 | * if they can still determine a route, for example |
michael@0 | 186 | * to a default target or by inspecting the request. |
michael@0 | 187 | * @param request the request to execute |
michael@0 | 188 | * @param context the context to use for the execution, or |
michael@0 | 189 | * <code>null</code> to use the default context |
michael@0 | 190 | * |
michael@0 | 191 | * @return the response to the request. This is always a final response, |
michael@0 | 192 | * never an intermediate response with an 1xx status code. |
michael@0 | 193 | * Whether redirects or authentication challenges will be returned |
michael@0 | 194 | * or handled automatically depends on the implementation and |
michael@0 | 195 | * configuration of this client. |
michael@0 | 196 | * @throws IOException in case of a problem or the connection was aborted |
michael@0 | 197 | * @throws ClientProtocolException in case of an http protocol error |
michael@0 | 198 | */ |
michael@0 | 199 | HttpResponse execute(HttpHost target, HttpRequest request, |
michael@0 | 200 | HttpContext context) |
michael@0 | 201 | throws IOException, ClientProtocolException; |
michael@0 | 202 | |
michael@0 | 203 | /** |
michael@0 | 204 | * Executes a request using the default context and processes the |
michael@0 | 205 | * response using the given response handler. |
michael@0 | 206 | * |
michael@0 | 207 | * @param request the request to execute |
michael@0 | 208 | * @param responseHandler the response handler |
michael@0 | 209 | * |
michael@0 | 210 | * @return the response object as generated by the response handler. |
michael@0 | 211 | * @throws IOException in case of a problem or the connection was aborted |
michael@0 | 212 | * @throws ClientProtocolException in case of an http protocol error |
michael@0 | 213 | */ |
michael@0 | 214 | <T> T execute( |
michael@0 | 215 | HttpUriRequest request, |
michael@0 | 216 | ResponseHandler<? extends T> responseHandler) |
michael@0 | 217 | throws IOException, ClientProtocolException; |
michael@0 | 218 | |
michael@0 | 219 | /** |
michael@0 | 220 | * Executes a request using the given context and processes the |
michael@0 | 221 | * response using the given response handler. |
michael@0 | 222 | * |
michael@0 | 223 | * @param request the request to execute |
michael@0 | 224 | * @param responseHandler the response handler |
michael@0 | 225 | * |
michael@0 | 226 | * @return the response object as generated by the response handler. |
michael@0 | 227 | * @throws IOException in case of a problem or the connection was aborted |
michael@0 | 228 | * @throws ClientProtocolException in case of an http protocol error |
michael@0 | 229 | */ |
michael@0 | 230 | <T> T execute( |
michael@0 | 231 | HttpUriRequest request, |
michael@0 | 232 | ResponseHandler<? extends T> responseHandler, |
michael@0 | 233 | HttpContext context) |
michael@0 | 234 | throws IOException, ClientProtocolException; |
michael@0 | 235 | |
michael@0 | 236 | /** |
michael@0 | 237 | * Executes a request to the target using the default context and |
michael@0 | 238 | * processes the response using the given response handler. |
michael@0 | 239 | * |
michael@0 | 240 | * @param target the target host for the request. |
michael@0 | 241 | * Implementations may accept <code>null</code> |
michael@0 | 242 | * if they can still determine a route, for example |
michael@0 | 243 | * to a default target or by inspecting the request. |
michael@0 | 244 | * @param request the request to execute |
michael@0 | 245 | * @param responseHandler the response handler |
michael@0 | 246 | * |
michael@0 | 247 | * @return the response object as generated by the response handler. |
michael@0 | 248 | * @throws IOException in case of a problem or the connection was aborted |
michael@0 | 249 | * @throws ClientProtocolException in case of an http protocol error |
michael@0 | 250 | */ |
michael@0 | 251 | <T> T execute( |
michael@0 | 252 | HttpHost target, |
michael@0 | 253 | HttpRequest request, |
michael@0 | 254 | ResponseHandler<? extends T> responseHandler) |
michael@0 | 255 | throws IOException, ClientProtocolException; |
michael@0 | 256 | |
michael@0 | 257 | /** |
michael@0 | 258 | * Executes a request to the target using the given context and |
michael@0 | 259 | * processes the response using the given response handler. |
michael@0 | 260 | * |
michael@0 | 261 | * @param target the target host for the request. |
michael@0 | 262 | * Implementations may accept <code>null</code> |
michael@0 | 263 | * if they can still determine a route, for example |
michael@0 | 264 | * to a default target or by inspecting the request. |
michael@0 | 265 | * @param request the request to execute |
michael@0 | 266 | * @param responseHandler the response handler |
michael@0 | 267 | * @param context the context to use for the execution, or |
michael@0 | 268 | * <code>null</code> to use the default context |
michael@0 | 269 | * |
michael@0 | 270 | * @return the response object as generated by the response handler. |
michael@0 | 271 | * @throws IOException in case of a problem or the connection was aborted |
michael@0 | 272 | * @throws ClientProtocolException in case of an http protocol error |
michael@0 | 273 | */ |
michael@0 | 274 | <T> T execute( |
michael@0 | 275 | HttpHost target, |
michael@0 | 276 | HttpRequest request, |
michael@0 | 277 | ResponseHandler<? extends T> responseHandler, |
michael@0 | 278 | HttpContext context) |
michael@0 | 279 | throws IOException, ClientProtocolException; |
michael@0 | 280 | |
michael@0 | 281 | } |