View Javadoc
1   /*
2    * Copyright 2014 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 io.netty.example.spdy.client;
17  
18  import io.netty.bootstrap.Bootstrap;
19  import io.netty.channel.Channel;
20  import io.netty.channel.ChannelOption;
21  import io.netty.channel.EventLoopGroup;
22  import io.netty.channel.nio.NioEventLoopGroup;
23  import io.netty.channel.socket.nio.NioSocketChannel;
24  import io.netty.handler.codec.http.DefaultFullHttpRequest;
25  import io.netty.handler.codec.http.HttpHeaderNames;
26  import io.netty.handler.codec.http.HttpHeaderValues;
27  import io.netty.handler.codec.http.HttpMethod;
28  import io.netty.handler.codec.http.HttpRequest;
29  import io.netty.handler.codec.http.HttpVersion;
30  import io.netty.handler.ssl.ApplicationProtocolConfig;
31  import io.netty.handler.ssl.ApplicationProtocolConfig.Protocol;
32  import io.netty.handler.ssl.ApplicationProtocolConfig.SelectedListenerFailureBehavior;
33  import io.netty.handler.ssl.ApplicationProtocolConfig.SelectorFailureBehavior;
34  import io.netty.handler.ssl.ApplicationProtocolNames;
35  import io.netty.handler.ssl.SslContext;
36  import io.netty.handler.ssl.SslContextBuilder;
37  import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
38  
39  /**
40   * An SPDY client that allows you to send HTTP GET to a SPDY server.
41   * <p>
42   * This class must be run with the JVM parameter: {@code java -Xbootclasspath/p:<path_to_npn_boot_jar> ...}. The
43   * "path_to_npn_boot_jar" is the path on the file system for the NPN Boot Jar file which can be downloaded from Maven at
44   * coordinates org.mortbay.jetty.npn:npn-boot. Different versions applies to different OpenJDK versions. See
45   * <a href="http://www.eclipse.org/jetty/documentation/current/npn-chapter.html">Jetty docs</a> for more information.
46   * <p>
47   * You may also use the {@code run-example.sh} script to start the client from the command line:
48   * <pre>
49   *     ./run-example.sh spdy-client
50   * </pre>
51   */
52  public final class SpdyClient {
53  
54      static final String HOST = System.getProperty("host", "127.0.0.1");
55      static final int PORT = Integer.parseInt(System.getProperty("port", "8443"));
56  
57      public static void main(String[] args) throws Exception {
58          // Configure SSL.
59          final SslContext sslCtx = SslContextBuilder.forClient()
60              .trustManager(InsecureTrustManagerFactory.INSTANCE)
61              .applicationProtocolConfig(new ApplicationProtocolConfig(
62                          Protocol.NPN,
63                          // NO_ADVERTISE is currently the only mode supported by both OpenSsl and JDK providers.
64                          SelectorFailureBehavior.NO_ADVERTISE,
65                          // ACCEPT is currently the only mode supported by both OpenSsl and JDK providers.
66                          SelectedListenerFailureBehavior.ACCEPT,
67                          ApplicationProtocolNames.SPDY_3_1,
68                          ApplicationProtocolNames.HTTP_1_1))
69              .build();
70  
71          HttpResponseClientHandler httpResponseHandler = new HttpResponseClientHandler();
72          EventLoopGroup workerGroup = new NioEventLoopGroup();
73  
74          try {
75              Bootstrap b = new Bootstrap();
76              b.group(workerGroup);
77              b.channel(NioSocketChannel.class);
78              b.option(ChannelOption.SO_KEEPALIVE, true);
79              b.remoteAddress(HOST, PORT);
80              b.handler(new SpdyClientInitializer(sslCtx, httpResponseHandler));
81  
82              // Start the client.
83              Channel channel = b.connect().syncUninterruptibly().channel();
84              System.out.println("Connected to " + HOST + ':' + PORT);
85  
86              // Create a GET request.
87              HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "");
88              request.headers().set(HttpHeaderNames.HOST, HOST);
89              request.headers().set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
90  
91              // Send the GET request.
92              channel.writeAndFlush(request).sync();
93  
94              // Waits for the complete HTTP response
95              httpResponseHandler.queue().take().sync();
96              System.out.println("Finished SPDY HTTP GET");
97  
98              // Wait until the connection is closed.
99              channel.close().syncUninterruptibly();
100         } finally {
101             workerGroup.shutdownGracefully();
102         }
103     }
104 }