hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1057360 - in /httpcomponents/httpasyncclient/trunk/httpasyncclient/src: examples/org/apache/http/examples/nio/client/ main/java/org/apache/http/impl/nio/client/ main/java/org/apache/http/impl/nio/conn/ test/java/org/apache/http/impl/nio/cl...
Date Mon, 10 Jan 2011 21:06:10 GMT
Author: olegk
Date: Mon Jan 10 21:06:10 2011
New Revision: 1057360

URL: http://svn.apache.org/viewvc?rev=1057360&view=rev
Log:
Improved exception logging; added missing AL headers; added an example of an asynchronous
HTTP exchange with content streaming

Added:
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientHttpExchange.java
      - copied, changed from r1057339, httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientRequest.java
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientHttpExchangeStreaming.java
  (with props)
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/InternalIOReactorExceptionHandler.java
  (with props)
Removed:
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientRequest.java
Modified:
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/AbstractHttpAsyncClient.java
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/Wire.java
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestHttpAsync.java
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestHttpsAsync.java

Copied: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientHttpExchange.java
(from r1057339, httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientRequest.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientHttpExchange.java?p2=httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientHttpExchange.java&p1=httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientRequest.java&r1=1057339&r2=1057360&rev=1057360&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientRequest.java
(original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientHttpExchange.java
Mon Jan 10 21:06:10 2011
@@ -26,8 +26,6 @@
  */
 package org.apache.http.examples.nio.client;
 
-import java.util.LinkedList;
-import java.util.Queue;
 import java.util.concurrent.Future;
 
 import org.apache.http.HttpResponse;
@@ -35,23 +33,16 @@ import org.apache.http.client.methods.Ht
 import org.apache.http.impl.nio.client.DefaultHttpAsyncClient;
 import org.apache.http.nio.client.HttpAsyncClient;
 
-public class AsyncClientRequest {
+public class AsyncClientHttpExchange {
 
     public static void main(String[] args) throws Exception {
         HttpAsyncClient httpclient = new DefaultHttpAsyncClient();
         httpclient.start();
         try {
-            Queue<Future<HttpResponse>> queue = new LinkedList<Future<HttpResponse>>();
-            for (int i = 0; i < 10; i++) {
-                HttpGet request = new HttpGet("http://www.apache.org/");
-                queue.add(httpclient.execute(request, null));
-            }
-            while (!queue.isEmpty()) {
-                Future<HttpResponse> future = queue.remove();
-                HttpResponse response = future.get();
-                System.out.println("Response: " + response.getStatusLine());
-            }
-
+            HttpGet request = new HttpGet("http://www.apache.org/");
+            Future<HttpResponse> future = httpclient.execute(request, null);
+            HttpResponse response = future.get();
+            System.out.println("Response: " + response.getStatusLine());
             System.out.println("Shutting down");
         } finally {
             httpclient.shutdown();

Added: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientHttpExchangeStreaming.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientHttpExchangeStreaming.java?rev=1057360&view=auto
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientHttpExchangeStreaming.java
(added)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientHttpExchangeStreaming.java
Mon Jan 10 21:06:10 2011
@@ -0,0 +1,166 @@
+/*
+ * ====================================================================
+ * 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.nio.client;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.CoderResult;
+import java.util.concurrent.Future;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpException;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.impl.nio.client.DefaultHttpAsyncClient;
+import org.apache.http.message.BasicHttpRequest;
+import org.apache.http.nio.ContentDecoder;
+import org.apache.http.nio.ContentEncoder;
+import org.apache.http.nio.IOControl;
+import org.apache.http.nio.client.HttpAsyncClient;
+import org.apache.http.nio.client.HttpAsyncRequestProducer;
+import org.apache.http.nio.client.HttpAsyncResponseConsumer;
+import org.apache.http.protocol.HTTP;
+import org.apache.http.util.EntityUtils;
+
+public class AsyncClientHttpExchangeStreaming {
+
+    public static void main(String[] args) throws Exception {
+        HttpAsyncClient httpclient = new DefaultHttpAsyncClient();
+        httpclient.start();
+        try {
+            Future<Boolean> future = httpclient.execute(
+                    new MyRequestProducer(), new MyResponseConsumer(), null);
+            Boolean result = future.get();
+            if (result != null && result.booleanValue()) {
+                System.out.println("Request successfully executed");
+            } else {
+                System.out.println("Request failed");
+            }
+            System.out.println("Shutting down");
+        } finally {
+            httpclient.shutdown();
+        }
+        System.out.println("Done");
+    }
+
+    static class MyRequestProducer implements HttpAsyncRequestProducer {
+
+        public HttpRequest generateRequest() throws IOException, HttpException {
+            BasicHttpRequest request = new BasicHttpRequest("GET", "/");
+            return request;
+        }
+
+        public HttpHost getTarget() {
+            return new HttpHost("www.apache.org");
+        }
+
+        public boolean isRepeatable() {
+            return true;
+        }
+
+        public void produceContent(
+                final ContentEncoder encoder, final IOControl ioctrl) throws IOException
{
+            // Should never be called for non entity enclosing requests
+        }
+
+        public void resetRequest() {
+        }
+
+    }
+
+    static class MyResponseConsumer implements HttpAsyncResponseConsumer<Boolean> {
+
+        private Charset charset;
+        private CharsetDecoder decoder;
+        private ByteBuffer bbuf;
+        private CharBuffer cbuf;
+
+        private volatile Boolean result;
+
+        public synchronized void responseReceived(
+                final HttpResponse response) throws IOException, HttpException {
+            System.out.println("HTTP response: " + response.getStatusLine());
+            HttpEntity entity = response.getEntity();
+            if (entity != null) {
+                String s = EntityUtils.getContentCharSet(entity);
+                if (s == null) {
+                    s = HTTP.DEFAULT_CONTENT_CHARSET;
+                }
+                this.charset = Charset.forName(s);
+                this.decoder = this.charset.newDecoder();
+                this.bbuf = ByteBuffer.allocate(1024);
+                this.cbuf = CharBuffer.allocate(1024);
+            }
+        }
+
+        public synchronized void consumeContent(
+                final ContentDecoder decoder, final IOControl ioctrl) throws IOException
{
+            int bytesRead;
+            do {
+                bytesRead = decoder.read(this.bbuf);
+                this.bbuf.flip();
+                CoderResult result = this.decoder.decode(this.bbuf, this.cbuf, decoder.isCompleted());
+                if (result.isError()) {
+                    result.throwException();
+                }
+                this.bbuf.compact();
+                this.cbuf.flip();
+                while (this.cbuf.hasRemaining()) {
+                    System.out.print(this.cbuf.get());
+                }
+                this.cbuf.compact();
+            } while (bytesRead > 0);
+        }
+
+        public void responseCompleted() {
+            this.result = Boolean.TRUE;
+        }
+
+        public void cancel() {
+            this.result = Boolean.FALSE;
+        }
+
+        public void failed(final Exception ex) {
+            this.result = Boolean.FALSE;
+            ex.printStackTrace();
+        }
+
+        public Boolean getResult() {
+            return this.result;
+        }
+
+        public boolean isDone() {
+            return this.result != null;
+        }
+
+    }
+
+}

Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientHttpExchangeStreaming.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientHttpExchangeStreaming.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/examples/org/apache/http/examples/nio/client/AsyncClientHttpExchangeStreaming.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/AbstractHttpAsyncClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/AbstractHttpAsyncClient.java?rev=1057360&r1=1057359&r2=1057360&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/AbstractHttpAsyncClient.java
(original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/AbstractHttpAsyncClient.java
Mon Jan 10 21:06:10 2011
@@ -95,10 +95,14 @@ public abstract class AbstractHttpAsyncC
         this.params = params;
     }
 
-    protected AbstractHttpAsyncClient(
-            final HttpParams params) throws IOReactorException {
+    protected AbstractHttpAsyncClient(HttpParams params) throws IOReactorException {
         super();
-        this.ioReactor = new DefaultConnectingIOReactor(2, params);
+        if (params == null) {
+            params = createHttpParams();
+        }
+        DefaultConnectingIOReactor defaultioreactor = new DefaultConnectingIOReactor(2, params);
+        defaultioreactor.setExceptionHandler(new InternalIOReactorExceptionHandler(this.log));
+        this.ioReactor = defaultioreactor;
         this.connmgr = new PoolingClientConnectionManager(this.ioReactor);
         this.pendingResponses = new HttpAsyncResponseSet();
         this.params = params;

Added: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/InternalIOReactorExceptionHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/InternalIOReactorExceptionHandler.java?rev=1057360&view=auto
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/InternalIOReactorExceptionHandler.java
(added)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/InternalIOReactorExceptionHandler.java
Mon Jan 10 21:06:10 2011
@@ -0,0 +1,53 @@
+/*
+ * ====================================================================
+ * 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.nio.client;
+
+import java.io.IOException;
+
+import org.apache.commons.logging.Log;
+import org.apache.http.nio.reactor.IOReactorExceptionHandler;
+
+class InternalIOReactorExceptionHandler implements IOReactorExceptionHandler {
+
+    private final Log log;
+
+    InternalIOReactorExceptionHandler(final Log log) {
+        super();
+        this.log = log;
+    }
+
+    public boolean handle(final IOException ex) {
+        this.log.error("Fatal I/O error", ex);
+        return false;
+    }
+
+    public boolean handle(final RuntimeException ex) {
+        this.log.error("Fatal runtime error", ex);
+        return false;
+    }
+
+}
\ No newline at end of file

Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/InternalIOReactorExceptionHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/InternalIOReactorExceptionHandler.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/InternalIOReactorExceptionHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/Wire.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/Wire.java?rev=1057360&r1=1057359&r2=1057360&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/Wire.java
(original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/conn/Wire.java
Mon Jan 10 21:06:10 2011
@@ -1,3 +1,29 @@
+/*
+ * ====================================================================
+ * 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.nio.conn;
 
 import java.nio.ByteBuffer;

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestHttpAsync.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestHttpAsync.java?rev=1057360&r1=1057359&r2=1057360&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestHttpAsync.java
(original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestHttpAsync.java
Mon Jan 10 21:06:10 2011
@@ -1,3 +1,29 @@
+/*
+ * ====================================================================
+ * 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.nio.client;
 
 import java.io.IOException;

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestHttpsAsync.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestHttpsAsync.java?rev=1057360&r1=1057359&r2=1057360&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestHttpsAsync.java
(original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestHttpsAsync.java
Mon Jan 10 21:06:10 2011
@@ -1,3 +1,29 @@
+/*
+ * ====================================================================
+ * 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.nio.client;
 
 import java.io.IOException;



Mime
View raw message