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; michael@0: michael@0: import java.io.IOException; michael@0: michael@0: import ch.boye.httpclientandroidlib.HttpHost; michael@0: import ch.boye.httpclientandroidlib.HttpRequest; michael@0: import ch.boye.httpclientandroidlib.HttpResponse; michael@0: import ch.boye.httpclientandroidlib.client.methods.HttpUriRequest; michael@0: import ch.boye.httpclientandroidlib.conn.ClientConnectionManager; michael@0: import ch.boye.httpclientandroidlib.params.HttpParams; michael@0: import ch.boye.httpclientandroidlib.protocol.HttpContext; michael@0: michael@0: /** michael@0: * This interface represents only the most basic contract for HTTP request michael@0: * execution. It imposes no restrictions or particular details on the request michael@0: * execution process and leaves the specifics of state management, michael@0: * authentication and redirect handling up to individual implementations. michael@0: * This should make it easier to decorate the interface with additional michael@0: * functionality such as response content caching. michael@0: *

michael@0: * The usual execution flow can be demonstrated by the code snippet below: michael@0: *

michael@0:  * HttpClient httpclient = new DefaultHttpClient();
michael@0:  *
michael@0:  * // Prepare a request object
michael@0:  * HttpGet httpget = new HttpGet("http://www.apache.org/");
michael@0:  *
michael@0:  * // Execute the request
michael@0:  * HttpResponse response = httpclient.execute(httpget);
michael@0:  *
michael@0:  * // Examine the response status
michael@0:  * System.out.println(response.getStatusLine());
michael@0:  *
michael@0:  * // Get hold of the response entity
michael@0:  * HttpEntity entity = response.getEntity();
michael@0:  *
michael@0:  * // If the response does not enclose an entity, there is no need
michael@0:  * // to worry about connection release
michael@0:  * if (entity != null) {
michael@0:  *     InputStream instream = entity.getContent();
michael@0:  *     try {
michael@0:  *
michael@0:  *         BufferedReader reader = new BufferedReader(
michael@0:  *                 new InputStreamReader(instream));
michael@0:  *         // do something useful with the response
michael@0:  *         System.out.println(reader.readLine());
michael@0:  *
michael@0:  *     } catch (IOException ex) {
michael@0:  *
michael@0:  *         // In case of an IOException the connection will be released
michael@0:  *         // back to the connection manager automatically
michael@0:  *         throw ex;
michael@0:  *
michael@0:  *     } catch (RuntimeException ex) {
michael@0:  *
michael@0:  *         // In case of an unexpected exception you may want to abort
michael@0:  *         // the HTTP request in order to shut down the underlying
michael@0:  *         // connection and release it back to the connection manager.
michael@0:  *         httpget.abort();
michael@0:  *         throw ex;
michael@0:  *
michael@0:  *     } finally {
michael@0:  *
michael@0:  *         // Closing the input stream will trigger connection release
michael@0:  *         instream.close();
michael@0:  *
michael@0:  *     }
michael@0:  *
michael@0:  *     // When HttpClient instance is no longer needed,
michael@0:  *     // shut down the connection manager to ensure
michael@0:  *     // immediate deallocation of all system resources
michael@0:  *     httpclient.getConnectionManager().shutdown();
michael@0:  * }
michael@0:  * 
michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: public interface HttpClient { michael@0: michael@0: michael@0: /** michael@0: * Obtains the parameters for this client. michael@0: * These parameters will become defaults for all requests being michael@0: * executed with this client, and for the parameters of michael@0: * dependent objects in this client. michael@0: * michael@0: * @return the default parameters michael@0: */ michael@0: HttpParams getParams(); michael@0: michael@0: /** michael@0: * Obtains the connection manager used by this client. michael@0: * michael@0: * @return the connection manager michael@0: */ michael@0: ClientConnectionManager getConnectionManager(); michael@0: michael@0: /** michael@0: * Executes a request using the default context. michael@0: * michael@0: * @param request the request to execute michael@0: * michael@0: * @return the response to the request. This is always a final response, michael@0: * never an intermediate response with an 1xx status code. michael@0: * Whether redirects or authentication challenges will be returned michael@0: * or handled automatically depends on the implementation and michael@0: * configuration of this client. michael@0: * @throws IOException in case of a problem or the connection was aborted michael@0: * @throws ClientProtocolException in case of an http protocol error michael@0: */ michael@0: HttpResponse execute(HttpUriRequest request) michael@0: throws IOException, ClientProtocolException; michael@0: michael@0: /** michael@0: * Executes a request using the given context. michael@0: * The route to the target will be determined by the HTTP client. michael@0: * michael@0: * @param request the request to execute michael@0: * @param context the context to use for the execution, or michael@0: * null to use the default context michael@0: * michael@0: * @return the response to the request. This is always a final response, michael@0: * never an intermediate response with an 1xx status code. michael@0: * Whether redirects or authentication challenges will be returned michael@0: * or handled automatically depends on the implementation and michael@0: * configuration of this client. michael@0: * @throws IOException in case of a problem or the connection was aborted michael@0: * @throws ClientProtocolException in case of an http protocol error michael@0: */ michael@0: HttpResponse execute(HttpUriRequest request, HttpContext context) michael@0: throws IOException, ClientProtocolException; michael@0: michael@0: /** michael@0: * Executes a request to the target using the default context. michael@0: * michael@0: * @param target the target host for the request. michael@0: * Implementations may accept null michael@0: * if they can still determine a route, for example michael@0: * to a default target or by inspecting the request. michael@0: * @param request the request to execute michael@0: * michael@0: * @return the response to the request. This is always a final response, michael@0: * never an intermediate response with an 1xx status code. michael@0: * Whether redirects or authentication challenges will be returned michael@0: * or handled automatically depends on the implementation and michael@0: * configuration of this client. michael@0: * @throws IOException in case of a problem or the connection was aborted michael@0: * @throws ClientProtocolException in case of an http protocol error michael@0: */ michael@0: HttpResponse execute(HttpHost target, HttpRequest request) michael@0: throws IOException, ClientProtocolException; michael@0: michael@0: /** michael@0: * Executes a request to the target using the given context. michael@0: * michael@0: * @param target the target host for the request. michael@0: * Implementations may accept null michael@0: * if they can still determine a route, for example michael@0: * to a default target or by inspecting the request. michael@0: * @param request the request to execute michael@0: * @param context the context to use for the execution, or michael@0: * null to use the default context michael@0: * michael@0: * @return the response to the request. This is always a final response, michael@0: * never an intermediate response with an 1xx status code. michael@0: * Whether redirects or authentication challenges will be returned michael@0: * or handled automatically depends on the implementation and michael@0: * configuration of this client. michael@0: * @throws IOException in case of a problem or the connection was aborted michael@0: * @throws ClientProtocolException in case of an http protocol error michael@0: */ michael@0: HttpResponse execute(HttpHost target, HttpRequest request, michael@0: HttpContext context) michael@0: throws IOException, ClientProtocolException; michael@0: michael@0: /** michael@0: * Executes a request using the default context and processes the michael@0: * response using the given response handler. michael@0: * michael@0: * @param request the request to execute michael@0: * @param responseHandler the response handler michael@0: * michael@0: * @return the response object as generated by the response handler. michael@0: * @throws IOException in case of a problem or the connection was aborted michael@0: * @throws ClientProtocolException in case of an http protocol error michael@0: */ michael@0: T execute( michael@0: HttpUriRequest request, michael@0: ResponseHandler responseHandler) michael@0: throws IOException, ClientProtocolException; michael@0: michael@0: /** michael@0: * Executes a request using the given context and processes the michael@0: * response using the given response handler. michael@0: * michael@0: * @param request the request to execute michael@0: * @param responseHandler the response handler michael@0: * michael@0: * @return the response object as generated by the response handler. michael@0: * @throws IOException in case of a problem or the connection was aborted michael@0: * @throws ClientProtocolException in case of an http protocol error michael@0: */ michael@0: T execute( michael@0: HttpUriRequest request, michael@0: ResponseHandler responseHandler, michael@0: HttpContext context) michael@0: throws IOException, ClientProtocolException; michael@0: michael@0: /** michael@0: * Executes a request to the target using the default context and michael@0: * processes the response using the given response handler. michael@0: * michael@0: * @param target the target host for the request. michael@0: * Implementations may accept null michael@0: * if they can still determine a route, for example michael@0: * to a default target or by inspecting the request. michael@0: * @param request the request to execute michael@0: * @param responseHandler the response handler michael@0: * michael@0: * @return the response object as generated by the response handler. michael@0: * @throws IOException in case of a problem or the connection was aborted michael@0: * @throws ClientProtocolException in case of an http protocol error michael@0: */ michael@0: T execute( michael@0: HttpHost target, michael@0: HttpRequest request, michael@0: ResponseHandler responseHandler) michael@0: throws IOException, ClientProtocolException; michael@0: michael@0: /** michael@0: * Executes a request to the target using the given context and michael@0: * processes the response using the given response handler. michael@0: * michael@0: * @param target the target host for the request. michael@0: * Implementations may accept null michael@0: * if they can still determine a route, for example michael@0: * to a default target or by inspecting the request. michael@0: * @param request the request to execute michael@0: * @param responseHandler the response handler michael@0: * @param context the context to use for the execution, or michael@0: * null to use the default context michael@0: * michael@0: * @return the response object as generated by the response handler. michael@0: * @throws IOException in case of a problem or the connection was aborted michael@0: * @throws ClientProtocolException in case of an http protocol error michael@0: */ michael@0: T execute( michael@0: HttpHost target, michael@0: HttpRequest request, michael@0: ResponseHandler responseHandler, michael@0: HttpContext context) michael@0: throws IOException, ClientProtocolException; michael@0: michael@0: }