Added ability to abort HTTP methods

git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@528462 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2007-04-13 12:25:11 +00:00
parent 254de778a5
commit 58113ab702
4 changed files with 190 additions and 1 deletions

View File

@ -0,0 +1,113 @@
/*
* $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.examples.client;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.PlainSocketFactory;
import org.apache.http.conn.Scheme;
import org.apache.http.conn.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.RequestConnControl;
import org.apache.http.protocol.RequestContent;
import org.apache.http.protocol.RequestExpectContinue;
import org.apache.http.protocol.RequestTargetHost;
import org.apache.http.protocol.RequestUserAgent;
/**
* How to abort an HTTP method before its normal completion.
*
* <!-- empty lines above to avoid 'svn diff' context problems -->
* @version $Revision$
*
* @since 4.0
*/
public class MethodAbort {
public final static void main(String[] args) throws Exception {
// Create a registry of available protocol schemes
SchemeRegistry supportedSchemes = new SchemeRegistry();
supportedSchemes.register(
new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
// Initialize default parameters
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUserAgent(params, "Jakarta-HttpClient/4.0");
DefaultHttpClient httpclient = new DefaultHttpClient(
params,
new SingleClientConnManager(params, supportedSchemes),
supportedSchemes);
// Required protocol interceptors
httpclient.getProcessor().addInterceptor(new RequestContent());
httpclient.getProcessor().addInterceptor(new RequestTargetHost());
// Recommended protocol interceptors
httpclient.getProcessor().addInterceptor(new RequestConnControl());
httpclient.getProcessor().addInterceptor(new RequestUserAgent());
httpclient.getProcessor().addInterceptor(new RequestExpectContinue());
HttpHost target = new HttpHost("www.apache.org", 80, "http");
HttpGet httpget = new HttpGet("/");
System.out.println("executing request to " + target);
HttpResponse response = httpclient.execute(target, httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
System.out.println("Chunked?: " + entity.isChunked());
}
System.out.println("----------------------------------------");
// Do not feel like reading the response body
// Call abort on the request object
httpget.abort();
}
}

View File

@ -0,0 +1,53 @@
/*
* $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.methods;
import org.apache.http.conn.ConnectionReleaseTrigger;
/**
* Interface representing an HTTP request that can be aborted by shutting
* donw the underying HTTP connection.
*
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
* <!-- empty lines to avoid svn diff problems -->
* @version $Revision$
*
* @since 4.0
*/
public interface AbortableHttpRequest {
void setReleaseTrigger(ConnectionReleaseTrigger releaseTrigger);
void abort();
}

View File

@ -31,11 +31,13 @@
package org.apache.http.client.methods;
import java.io.IOException;
import java.net.URI;
import org.apache.http.HttpRequest;
import org.apache.http.HttpVersion;
import org.apache.http.RequestLine;
import org.apache.http.conn.ConnectionReleaseTrigger;
import org.apache.http.message.AbstractHttpMessage;
import org.apache.http.message.BasicRequestLine;
import org.apache.http.params.HttpProtocolParams;
@ -49,9 +51,11 @@ import org.apache.http.params.HttpProtocolParams;
*
* @since 4.0
*/
abstract class HttpRequestBase extends AbstractHttpMessage implements HttpRequest {
abstract class HttpRequestBase extends AbstractHttpMessage
implements HttpRequest, AbortableHttpRequest {
private URI uri;
private ConnectionReleaseTrigger releaseTrigger;
public HttpRequestBase() {
super();
@ -83,5 +87,19 @@ abstract class HttpRequestBase extends AbstractHttpMessage implements HttpReques
public void setURI(final URI uri) {
this.uri = uri;
}
public void setReleaseTrigger(final ConnectionReleaseTrigger releaseTrigger) {
this.releaseTrigger = releaseTrigger;
}
public void abort() {
if (this.releaseTrigger != null) {
try {
this.releaseTrigger.abortConnection();
} catch (IOException ex) {
// ignore
}
}
}
}

View File

@ -41,6 +41,7 @@ import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientRequestDirector;
import org.apache.http.client.RoutedRequest;
import org.apache.http.client.methods.AbortableHttpRequest;
import org.apache.http.conn.BasicManagedEntity;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.HttpRoute;
@ -131,6 +132,10 @@ public class DefaultClientRequestDirector
HttpRequest prepreq = prepareRequest(roureq, context);
//@@@ handle authentication here or via interceptor?
if (prepreq instanceof AbortableHttpRequest) {
((AbortableHttpRequest) prepreq).setReleaseTrigger(managedConn);
}
context.setAttribute(HttpExecutionContext.HTTP_TARGET_HOST,
roureq.getRoute().getTargetHost());