View Javadoc

    1   /*
    2    * Copyright 2012 The Netty Project
    3    *
    4    * The Netty Project licenses this file to you under the Apache License,
    5    * version 2.0 (the "License"); you may not use this file except in compliance
    6    * with the License. You may obtain a copy of the License at:
    7    *
    8    *   http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    13   * License for the specific language governing permissions and limitations
    14   * under the License.
    15   */
    16  package org.jboss.netty.example.http.snoop;
    17
    18  import java.net.InetSocketAddress;
    19  import java.net.URI;
    20  import java.util.concurrent.Executors;
    21
    22  import org.jboss.netty.bootstrap.ClientBootstrap;
    23  import org.jboss.netty.channel.Channel;
    24  import org.jboss.netty.channel.ChannelFuture;
    25  import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
    26  import org.jboss.netty.handler.codec.http.CookieEncoder;
    27  import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
    28  import org.jboss.netty.handler.codec.http.HttpHeaders;
    29  import org.jboss.netty.handler.codec.http.HttpMethod;
    30  import org.jboss.netty.handler.codec.http.HttpRequest;
    31  import org.jboss.netty.handler.codec.http.HttpVersion;
    32
    33  /**
    34   * A simple HTTP client that prints out the content of the HTTP response to
    35   * {@link System#out} to test {@link HttpSnoopServer}.
    36   */
    37  public class HttpSnoopClient {
    38
    39      private final URI uri;
    40
    41      public HttpSnoopClient(URI uri) {
    42          this.uri = uri;
    43      }
    44
    45      public void run() {
    46          String scheme = uri.getScheme() == null? "http" : uri.getScheme();
    47          String host = uri.getHost() == null? "localhost" : uri.getHost();
    48          int port = uri.getPort();
    49          if (port == -1) {
    50              if (scheme.equalsIgnoreCase("http")) {
    51                  port = 80;
    52              } else if (scheme.equalsIgnoreCase("https")) {
    53                  port = 443;
    54              }
    55          }
    56
    57          if (!scheme.equalsIgnoreCase("http") && !scheme.equalsIgnoreCase("https")) {
    58              System.err.println("Only HTTP(S) is supported.");
    59              return;
    60          }
    61
    62          boolean ssl = scheme.equalsIgnoreCase("https");
    63
    64          // Configure the client.
    65          ClientBootstrap bootstrap = new ClientBootstrap(
    66                  new NioClientSocketChannelFactory(
    67                          Executors.newCachedThreadPool(),
    68                          Executors.newCachedThreadPool()));
    69
    70          // Set up the event pipeline factory.
    71          bootstrap.setPipelineFactory(new HttpSnoopClientPipelineFactory(ssl));
    72
    73          // Start the connection attempt.
    74          ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
    75
    76          // Wait until the connection attempt succeeds or fails.
    77          Channel channel = future.awaitUninterruptibly().getChannel();
    78          if (!future.isSuccess()) {
    79              future.getCause().printStackTrace();
    80              bootstrap.releaseExternalResources();
    81              return;
    82          }
    83
    84          // Prepare the HTTP request.
    85          HttpRequest request = new DefaultHttpRequest(
    86                  HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
    87          request.setHeader(HttpHeaders.Names.HOST, host);
    88          request.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
    89          request.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
    90
    91          // Set some example cookies.
    92          CookieEncoder httpCookieEncoder = new CookieEncoder(false);
    93          httpCookieEncoder.addCookie("my-cookie", "foo");
    94          httpCookieEncoder.addCookie("another-cookie", "bar");
    95          request.setHeader(HttpHeaders.Names.COOKIE, httpCookieEncoder.encode());
    96
    97          // Send the HTTP request.
    98          channel.write(request);
    99
    100         // Wait for the server to close the connection.
    101         channel.getCloseFuture().awaitUninterruptibly();
    102
    103         // Shut down executor threads to exit.
    104         bootstrap.releaseExternalResources();
    105     }
    106
    107     public static void main(String[] args) throws Exception {
    108         if (args.length != 1) {
    109             System.err.println(
    110                     "Usage: " + HttpSnoopClient.class.getSimpleName() +
    111                     " <URL>");
    112             return;
    113         }
    114
    115         URI uri = new URI(args[0]);
    116         new HttpSnoopClient(uri).run();
    117     }
    118 }