From be88a53b3bfadaaf216502cc62eb57d30817cd14 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Wed, 26 Nov 2008 08:39:32 +0000 Subject: [PATCH] Replaced HttpClient example with more generic wget style one --- .../jboss/netty/example/http/HttpClient.java | 92 ------------------- ...ry.java => HttpClientPipelineFactory.java} | 4 +- .../example/http/HttpWebserverClient.java | 34 +++++-- 3 files changed, 28 insertions(+), 102 deletions(-) delete mode 100644 src/main/java/org/jboss/netty/example/http/HttpClient.java rename src/main/java/org/jboss/netty/example/http/{HttpPipelineFactory.java => HttpClientPipelineFactory.java} (93%) diff --git a/src/main/java/org/jboss/netty/example/http/HttpClient.java b/src/main/java/org/jboss/netty/example/http/HttpClient.java deleted file mode 100644 index 71a423c64d..0000000000 --- a/src/main/java/org/jboss/netty/example/http/HttpClient.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2005-2008, Red Hat Middleware LLC, and individual contributors - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * This is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This software is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this software; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA, or see the FSF site: http://www.fsf.org. - */ -package org.jboss.netty.example.http; - -import java.net.InetSocketAddress; -import java.net.URI; -import java.util.concurrent.Executors; - -import org.jboss.netty.bootstrap.ClientBootstrap; -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBuffers; -import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.ChannelFactory; -import org.jboss.netty.channel.ChannelFuture; -import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; -import org.jboss.netty.handler.codec.http.DefaultHttpRequest; -import org.jboss.netty.handler.codec.http.HttpHeaders; -import org.jboss.netty.handler.codec.http.HttpMethod; -import org.jboss.netty.handler.codec.http.HttpRequest; -import org.jboss.netty.handler.codec.http.HttpVersion; -import org.jboss.netty.handler.codec.http.QueryStringEncoder; - -/** - * - * @author The Netty Project (netty-dev@lists.jboss.org) - * @author Andy Taylor (andy.taylor@jboss.org) - */ -public class HttpClient { - public static void main(String[] args) throws Exception { - - // Parse options. - String host = "localhost"; - int port = 8080; - - // Configure the client. - ChannelFactory factory = - new NioClientSocketChannelFactory( - Executors.newCachedThreadPool(), - Executors.newCachedThreadPool()); - - ClientBootstrap bootstrap = new ClientBootstrap(factory); - HttpPipelineFactory handler = new HttpPipelineFactory(new HttpResponseHandler()); - bootstrap.setPipelineFactory(handler); - bootstrap.setOption("tcpNoDelay", true); - bootstrap.setOption("keepAlive", true); - // Start the connection attempt. - ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); - - // Wait until the connection attempt succeeds or fails. - Channel channel = future.awaitUninterruptibly().getChannel(); - if (!future.isSuccess()) { - future.getCause().printStackTrace(); - System.exit(0); - } - String message = "It's Hello From me"; - ChannelBuffer buf = ChannelBuffers.wrappedBuffer(message.getBytes()); - HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, new URI("/netty/")); - request.addHeader(HttpHeaders.HOST, host); - request.addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(buf.writerIndex())); - request.setContent(buf); - ChannelFuture lastWriteFuture = channel.write(request); - buf = ChannelBuffers.wrappedBuffer(message.getBytes()); - QueryStringEncoder queryStringEncoder = new QueryStringEncoder("/netty/"); - queryStringEncoder.addParam("testparam", "hey ho"); - queryStringEncoder.addParam("testparam2", "hey ho again"); - request = new DefaultHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, queryStringEncoder.toUri()); - request.addHeader(HttpHeaders.HOST, host); - request.addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(buf.writerIndex())); - request.setContent(buf); - lastWriteFuture = channel.write(request); - lastWriteFuture.awaitUninterruptibly(); - } -} diff --git a/src/main/java/org/jboss/netty/example/http/HttpPipelineFactory.java b/src/main/java/org/jboss/netty/example/http/HttpClientPipelineFactory.java similarity index 93% rename from src/main/java/org/jboss/netty/example/http/HttpPipelineFactory.java rename to src/main/java/org/jboss/netty/example/http/HttpClientPipelineFactory.java index 2e4427c8be..e7e0c28fb2 100644 --- a/src/main/java/org/jboss/netty/example/http/HttpPipelineFactory.java +++ b/src/main/java/org/jboss/netty/example/http/HttpClientPipelineFactory.java @@ -33,10 +33,10 @@ import org.jboss.netty.handler.codec.http.HttpResponseDecoder; * @author The Netty Project (netty-dev@lists.jboss.org) * @author Andy Taylor (andy.taylor@jboss.org) */ -public class HttpPipelineFactory implements ChannelPipelineFactory { +public class HttpClientPipelineFactory implements ChannelPipelineFactory { private final ChannelHandler handler; - public HttpPipelineFactory(HttpResponseHandler handler) { + public HttpClientPipelineFactory(HttpResponseHandler handler) { this.handler = handler; } diff --git a/src/main/java/org/jboss/netty/example/http/HttpWebserverClient.java b/src/main/java/org/jboss/netty/example/http/HttpWebserverClient.java index 154cf81470..2a6f93cef6 100644 --- a/src/main/java/org/jboss/netty/example/http/HttpWebserverClient.java +++ b/src/main/java/org/jboss/netty/example/http/HttpWebserverClient.java @@ -22,6 +22,7 @@ package org.jboss.netty.example.http; import java.net.InetSocketAddress; +import java.net.URI; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ClientBootstrap; @@ -34,18 +35,33 @@ import org.jboss.netty.handler.codec.http.HttpHeaders; import org.jboss.netty.handler.codec.http.HttpMethod; import org.jboss.netty.handler.codec.http.HttpRequest; import org.jboss.netty.handler.codec.http.HttpVersion; -import org.jboss.netty.handler.codec.http.QueryStringEncoder; /** * @author The Netty Project (netty-dev@lists.jboss.org) * @author Andy Taylor (andy.taylor@jboss.org) + * @author Trustin Lee (tlee@redhat.com) */ public class HttpWebserverClient { - public static void main(String[] args) throws Exception { - // Parse options. - String host = "www.jboss.org"; - int port = 80; + public static void main(String[] args) throws Exception { + if (args.length != 1) { + System.err.println( + "Usage: " + HttpWebserverClient.class.getSimpleName() + + " "); + return; + } + + URI uri = new URI(args[0]); + String scheme = uri.getScheme() == null? "http" : uri.getScheme(); + String host = uri.getHost() == null? "localhost" : uri.getHost(); + int port = uri.getPort() == -1? 80 : uri.getPort(); + + if (!scheme.equals("http")) { + // We can actually support HTTPS fairly easily by inserting + // an SslHandler to the pipeline - left as an exercise. + System.err.println("Only HTTP is supported."); + return; + } // Configure the client. ChannelFactory factory = @@ -54,8 +70,9 @@ public class HttpWebserverClient { Executors.newCachedThreadPool()); ClientBootstrap bootstrap = new ClientBootstrap(factory); - HttpPipelineFactory handler = new HttpPipelineFactory(new HttpResponseHandler()); + HttpClientPipelineFactory handler = new HttpClientPipelineFactory(new HttpResponseHandler()); bootstrap.setPipelineFactory(handler); + // Start the connection attempt. ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); @@ -65,8 +82,9 @@ public class HttpWebserverClient { future.getCause().printStackTrace(); System.exit(0); } - QueryStringEncoder uriBuilder = new QueryStringEncoder("/netty/"); - HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, uriBuilder.toUri()); + + // Send the HTTP request. + HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, uri); request.addHeader(HttpHeaders.HOST, host); ChannelFuture lastWriteFuture = channel.write(request); lastWriteFuture.awaitUninterruptibly();