Added ClientResponseHandler interface and new methods for the HttpClient interface as suggested by Bob Lee <crazybob at crazybob.org>

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@672425 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2008-06-27 23:33:05 +00:00
parent dee6af01ad
commit 96335cd101
12 changed files with 356 additions and 12 deletions

View File

@ -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("----------------------------------------");

View File

@ -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("----------------------------------------");

View File

@ -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("----------------------------------------");

View File

@ -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
* <http://www.apache.org/>.
*
*/
package org.apache.http.client;
import java.io.IOException;

View File

@ -89,5 +89,4 @@ public interface ClientRequestDirector {
throws HttpException, IOException
;
} // class ClientRequestDirector

View File

@ -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
* <http://www.apache.org/>.
*
*/
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 <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
* @since 4.0
*/
public interface ClientResponseHandler<T> {
T handleResponse(HttpResponse response) throws ClientProtocolException, IOException;
}

View File

@ -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 <code>null</code>
@ -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 <code>null</code>
@ -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> T execute(
HttpUriRequest request,
ClientResponseHandler<? extends T> 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> T execute(
HttpUriRequest request,
ClientResponseHandler<? extends T> 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 <code>null</code>
* 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> T execute(
HttpHost target,
HttpRequest request,
ClientResponseHandler<? extends T> 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 <code>null</code>
* 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
* <code>null</code> 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> T execute(
HttpHost target,
HttpRequest request,
ClientResponseHandler<? extends T> responseHandler,
HttpContext context)
throws IOException, ClientProtocolException
;
} // interface HttpClient

View File

@ -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
* <http://www.apache.org/>.
*
*/
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;
}
}

View File

@ -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> T execute(
final HttpUriRequest request,
final ClientResponseHandler<? extends T> 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> T execute(
final HttpUriRequest request,
final ClientResponseHandler<? extends T> 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> T execute(
final HttpHost target,
final HttpRequest request,
final ClientResponseHandler<? extends T> 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> T execute(
final HttpHost target,
final HttpRequest request,
final ClientResponseHandler<? extends T> 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

View File

@ -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
* <http://www.apache.org/>.
*
*/
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 <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
* @version $Revision: $
*
* @since 4.0
*/
public class BasicResponseHandler implements ClientResponseHandler<String> {
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;
}
}
}

View File

@ -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}.
* <br/>
* This class replaces the <code>HttpMethodDirector</code> in HttpClient 3.
*

View File

@ -52,7 +52,7 @@ import org.apache.http.protocol.ExecutionContext;
/**
* Default implementation of a redirect handler.
* Default implementation of {@link RedirectHandler}.
*
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*