diff --git a/module-client/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java b/module-client/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java index fdc7e1a93..6fe6656b1 100644 --- a/module-client/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java +++ b/module-client/src/examples/org/apache/http/examples/client/ClientExecuteDirect.java @@ -99,7 +99,7 @@ public class ClientExecuteDirect { System.out.println("executing request to " + target); HttpEntity entity = null; try { - HttpResponse rsp = client.execute(target, req, null); + HttpResponse rsp = client.execute(target, req); entity = rsp.getEntity(); System.out.println("----------------------------------------"); diff --git a/module-client/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java b/module-client/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java index 28d3fd0fc..07d04c772 100644 --- a/module-client/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java +++ b/module-client/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java @@ -107,7 +107,7 @@ public class ClientExecuteProxy { System.out.println("executing request to " + target + " via " + proxy); HttpEntity entity = null; try { - HttpResponse rsp = client.execute(target, req, null); + HttpResponse rsp = client.execute(target, req); entity = rsp.getEntity(); System.out.println("----------------------------------------"); diff --git a/module-client/src/examples/org/apache/http/examples/client/ClientProxyAuthentication.java b/module-client/src/examples/org/apache/http/examples/client/ClientProxyAuthentication.java index 378dcd4fd..56a718ba1 100644 --- a/module-client/src/examples/org/apache/http/examples/client/ClientProxyAuthentication.java +++ b/module-client/src/examples/org/apache/http/examples/client/ClientProxyAuthentication.java @@ -63,7 +63,7 @@ public class ClientProxyAuthentication { System.out.println("via proxy: " + proxy); System.out.println("to target: " + targetHost); - HttpResponse response = httpclient.execute(targetHost, httpget, null); + HttpResponse response = httpclient.execute(targetHost, httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); diff --git a/module-client/src/main/java/org/apache/http/client/ClientProtocolException.java b/module-client/src/main/java/org/apache/http/client/ClientProtocolException.java index f337654e3..b5a991a9d 100644 --- a/module-client/src/main/java/org/apache/http/client/ClientProtocolException.java +++ b/module-client/src/main/java/org/apache/http/client/ClientProtocolException.java @@ -1,3 +1,33 @@ +/* + * $HeadURL: $ + * $Revision: $ + * $Date: $ + * + * ==================================================================== + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + package org.apache.http.client; import java.io.IOException; diff --git a/module-client/src/main/java/org/apache/http/client/ClientRequestDirector.java b/module-client/src/main/java/org/apache/http/client/ClientRequestDirector.java index 60ec78d77..c4726d0c4 100644 --- a/module-client/src/main/java/org/apache/http/client/ClientRequestDirector.java +++ b/module-client/src/main/java/org/apache/http/client/ClientRequestDirector.java @@ -89,5 +89,4 @@ public interface ClientRequestDirector { throws HttpException, IOException ; - } // class ClientRequestDirector diff --git a/module-client/src/main/java/org/apache/http/client/ClientResponseHandler.java b/module-client/src/main/java/org/apache/http/client/ClientResponseHandler.java new file mode 100644 index 000000000..94fe118bb --- /dev/null +++ b/module-client/src/main/java/org/apache/http/client/ClientResponseHandler.java @@ -0,0 +1,49 @@ +/* + * $HeadURL: $ + * $Revision: $ + * $Date: $ + * + * ==================================================================== + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.client; + +import java.io.IOException; + +import org.apache.http.HttpResponse; + +/** + * Handler that incapsulates the process of generating a response object + * from a {@link HttpResponse}. + * + * @author Oleg Kalnichevski + * + * @since 4.0 + */ +public interface ClientResponseHandler { + + T handleResponse(HttpResponse response) throws ClientProtocolException, IOException; + +} diff --git a/module-client/src/main/java/org/apache/http/client/HttpClient.java b/module-client/src/main/java/org/apache/http/client/HttpClient.java index 514a2c7a5..18949b82d 100644 --- a/module-client/src/main/java/org/apache/http/client/HttpClient.java +++ b/module-client/src/main/java/org/apache/http/client/HttpClient.java @@ -85,7 +85,11 @@ public interface HttpClient { * * @param request the request to execute * - * @return the response to the request + * @return the response to the request. This is always a final response, + * never an intermediate response with an 1xx status code. + * Whether redirects or authentication challenges will be returned + * or handled automatically depends on the implementation and + * configuration of this client. * @throws IOException in case of a problem or the connection was aborted * @throws ClientProtocolException in case of an http protocol error */ @@ -117,7 +121,6 @@ public interface HttpClient { /** * Executes a request to the target using the default context. - * See there for details. * * @param target the target host for the request. * Implementations may accept null @@ -137,9 +140,8 @@ public interface HttpClient { throws IOException, ClientProtocolException ; - /** - * Executes a request to the target with the given context. + * Executes a request to the target using the given context. * * @param target the target host for the request. * Implementations may accept null @@ -162,5 +164,86 @@ public interface HttpClient { throws IOException, ClientProtocolException ; + /** + * Executes a request using the default context and processes the + * response using the given response handler. + * + * @param request the request to execute + * @param responseHandler the response handler + * + * @return the response object as generated by the response handler. + * @throws IOException in case of a problem or the connection was aborted + * @throws ClientProtocolException in case of an http protocol error + */ + T execute( + HttpUriRequest request, + ClientResponseHandler responseHandler) + throws IOException, ClientProtocolException + ; + /** + * Executes a request using the given context and processes the + * response using the given response handler. + * + * @param request the request to execute + * @param responseHandler the response handler + * + * @return the response object as generated by the response handler. + * @throws IOException in case of a problem or the connection was aborted + * @throws ClientProtocolException in case of an http protocol error + */ + T execute( + HttpUriRequest request, + ClientResponseHandler responseHandler, + HttpContext context) + throws IOException, ClientProtocolException + ; + + /** + * Executes a request to the target using the default context and + * processes the response using the given response handler. + * + * @param target the target host for the request. + * Implementations may accept null + * if they can still determine a route, for example + * to a default target or by inspecting the request. + * @param request the request to execute + * @param responseHandler the response handler + * + * @return the response object as generated by the response handler. + * @throws IOException in case of a problem or the connection was aborted + * @throws ClientProtocolException in case of an http protocol error + */ + T execute( + HttpHost target, + HttpRequest request, + ClientResponseHandler responseHandler) + throws IOException, ClientProtocolException + ; + + /** + * Executes a request to the target using the given context and + * processes the response using the given response handler. + * + * @param target the target host for the request. + * Implementations may accept null + * if they can still determine a route, for example + * to a default target or by inspecting the request. + * @param request the request to execute + * @param responseHandler the response handler + * @param context the context to use for the execution, or + * null to use the default context + * + * @return the response object as generated by the response handler. + * @throws IOException in case of a problem or the connection was aborted + * @throws ClientProtocolException in case of an http protocol error + */ + T execute( + HttpHost target, + HttpRequest request, + ClientResponseHandler responseHandler, + HttpContext context) + throws IOException, ClientProtocolException + ; + } // interface HttpClient diff --git a/module-client/src/main/java/org/apache/http/client/HttpResponseException.java b/module-client/src/main/java/org/apache/http/client/HttpResponseException.java new file mode 100644 index 000000000..f37fb9af1 --- /dev/null +++ b/module-client/src/main/java/org/apache/http/client/HttpResponseException.java @@ -0,0 +1,51 @@ +/* + * $HeadURL: $ + * $Revision: $ + * $Date: $ + * + * ==================================================================== + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.client; + +/** + * Signals a non 2xx HTTP response. + */ +public class HttpResponseException extends ClientProtocolException { + + private static final long serialVersionUID = -7186627969477257933L; + + private final int statusCode; + + public HttpResponseException(int statusCode, final String s) { + super(s); + this.statusCode = statusCode; + } + + public int getStatusCode() { + return this.statusCode; + } + +} diff --git a/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java b/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java index 461c82618..09fca5e59 100644 --- a/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java +++ b/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java @@ -45,6 +45,7 @@ import org.apache.http.auth.AuthSchemeRegistry; import org.apache.http.client.AuthenticationHandler; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientRequestDirector; +import org.apache.http.client.ClientResponseHandler; import org.apache.http.client.CookieStore; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.HttpClient; @@ -456,7 +457,7 @@ public abstract class AbstractHttpClient implements HttpClient { public final HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException { - return execute(request, null); + return execute(request, (HttpContext) null); } @@ -498,7 +499,7 @@ public abstract class AbstractHttpClient implements HttpClient { public final HttpResponse execute(HttpHost target, HttpRequest request) throws IOException, ClientProtocolException { - return execute(target, request, null); + return execute(target, request, (HttpContext) null); } @@ -613,4 +614,64 @@ public abstract class AbstractHttpClient implements HttpClient { } + // non-javadoc, see interface HttpClient + public T execute( + final HttpUriRequest request, + final ClientResponseHandler responseHandler) + throws IOException, ClientProtocolException { + if (responseHandler == null) { + throw new IllegalArgumentException + ("Response handler must not be null."); + } + HttpResponse response = execute(request); + return responseHandler.handleResponse(response); + } + + + // non-javadoc, see interface HttpClient + public T execute( + final HttpUriRequest request, + final ClientResponseHandler responseHandler, + final HttpContext context) + throws IOException, ClientProtocolException { + if (responseHandler == null) { + throw new IllegalArgumentException + ("Response handler must not be null."); + } + HttpResponse response = execute(request, context); + return responseHandler.handleResponse(response); + } + + + // non-javadoc, see interface HttpClient + public T execute( + final HttpHost target, + final HttpRequest request, + final ClientResponseHandler responseHandler) + throws IOException, ClientProtocolException { + if (responseHandler == null) { + throw new IllegalArgumentException + ("Response handler must not be null."); + } + HttpResponse response = execute(target, request); + return responseHandler.handleResponse(response); + } + + + // non-javadoc, see interface HttpClient + public T execute( + final HttpHost target, + final HttpRequest request, + final ClientResponseHandler responseHandler, + final HttpContext context) + throws IOException, ClientProtocolException { + if (responseHandler == null) { + throw new IllegalArgumentException + ("Response handler must not be null."); + } + HttpResponse response = execute(target, request, context); + return responseHandler.handleResponse(response); + } + + } // class AbstractHttpClient diff --git a/module-client/src/main/java/org/apache/http/impl/client/BasicResponseHandler.java b/module-client/src/main/java/org/apache/http/impl/client/BasicResponseHandler.java new file mode 100644 index 000000000..375045f5f --- /dev/null +++ b/module-client/src/main/java/org/apache/http/impl/client/BasicResponseHandler.java @@ -0,0 +1,71 @@ +/* + * $HeadURL: $ + * $Revision: $ + * $Date: $ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.impl.client; + +import java.io.IOException; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.StatusLine; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.ClientResponseHandler; +import org.apache.http.client.HttpResponseException; +import org.apache.http.util.EntityUtils; + +/** + * Default implementation of {@link ClientResponseHandler}. + * + * @author Oleg Kalnichevski + * + * @version $Revision: $ + * + * @since 4.0 + */ +public class BasicResponseHandler implements ClientResponseHandler { + + public String handleResponse( + final HttpResponse response) throws ClientProtocolException, IOException { + HttpEntity entity = response.getEntity(); + if (entity != null) { + StatusLine statusLine = response.getStatusLine(); + if (statusLine.getStatusCode() >= 300) { + entity.consumeContent(); + throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); + } else { + return EntityUtils.toString(entity); + } + } else { + return null; + } + } + +} diff --git a/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java b/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java index 868d6675d..4911e65e7 100644 --- a/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java +++ b/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java @@ -94,7 +94,7 @@ import org.apache.http.protocol.HttpProcessor; import org.apache.http.protocol.HttpRequestExecutor; /** - * Default implementation of a client-side request director. + * Default implementation of {@link ClientRequestDirector}. *
* This class replaces the HttpMethodDirector in HttpClient 3. * diff --git a/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java b/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java index 8fbdb0360..825e86f3a 100644 --- a/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java +++ b/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java @@ -52,7 +52,7 @@ import org.apache.http.protocol.ExecutionContext; /** - * Default implementation of a redirect handler. + * Default implementation of {@link RedirectHandler}. * * @author Oleg Kalnichevski *