hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r528462 - in /jakarta/httpcomponents/httpclient/trunk/src: examples/org/apache/http/examples/client/ java/org/apache/http/client/methods/ java/org/apache/http/impl/client/
Date Fri, 13 Apr 2007 12:25:16 GMT
Author: olegk
Date: Fri Apr 13 05:25:11 2007
New Revision: 528462

URL: http://svn.apache.org/viewvc?view=rev&rev=528462
Log:
Added ability to abort HTTP methods

Added:
    jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/MethodAbort.java
  (with props)
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/AbortableHttpRequest.java
  (with props)
Modified:
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/HttpRequestBase.java
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java

Added: jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/MethodAbort.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/MethodAbort.java?view=auto&rev=528462
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/MethodAbort.java
(added)
+++ jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/MethodAbort.java
Fri Apr 13 05:25:11 2007
@@ -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();
+    }
+
+}
+

Propchange: jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/MethodAbort.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/MethodAbort.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpclient/trunk/src/examples/org/apache/http/examples/client/MethodAbort.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/AbortableHttpRequest.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/AbortableHttpRequest.java?view=auto&rev=528462
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/AbortableHttpRequest.java
(added)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/AbortableHttpRequest.java
Fri Apr 13 05:25:11 2007
@@ -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();
+
+}

Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/AbortableHttpRequest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/AbortableHttpRequest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/AbortableHttpRequest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/HttpRequestBase.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/HttpRequestBase.java?view=diff&rev=528462&r1=528461&r2=528462
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/HttpRequestBase.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/methods/HttpRequestBase.java
Fri Apr 13 05:25:11 2007
@@ -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 @@
  * 
  * @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 @@
     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
+            }
+        }
+    }
+
 }

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java?view=diff&rev=528462&r1=528461&r2=528462
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java
Fri Apr 13 05:25:11 2007
@@ -41,6 +41,7 @@
 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 @@
 
                 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());



Mime
View raw message