1+ package org .javaee8 .servlet .http2 ;
2+
3+ import java .net .InetSocketAddress ;
4+ import java .net .URI ;
5+ import java .net .URISyntaxException ;
6+ import java .util .concurrent .CountDownLatch ;
7+ import java .util .concurrent .ExecutionException ;
8+ import java .util .concurrent .TimeUnit ;
9+ import java .util .concurrent .TimeoutException ;
10+ import java .util .logging .Level ;
11+
12+ import org .eclipse .jetty .http .HttpFields ;
13+ import org .eclipse .jetty .http .HttpURI ;
14+ import org .eclipse .jetty .http .HttpVersion ;
15+ import org .eclipse .jetty .http .MetaData ;
16+ import org .eclipse .jetty .http2 .api .Session ;
17+ import org .eclipse .jetty .http2 .api .Stream ;
18+ import org .eclipse .jetty .http2 .api .server .ServerSessionListener ;
19+ import org .eclipse .jetty .http2 .client .HTTP2Client ;
20+ import org .eclipse .jetty .http2 .frames .DataFrame ;
21+ import org .eclipse .jetty .http2 .frames .HeadersFrame ;
22+ import org .eclipse .jetty .util .Callback ;
23+ import org .eclipse .jetty .util .FuturePromise ;
24+ import org .eclipse .jetty .util .Jetty ;
25+ import org .eclipse .jetty .util .ssl .SslContextFactory ;
26+
27+ public class WebClient {
28+
29+ private HTTP2Client client ;
30+ private SslContextFactory sslContextFactory ;
31+
32+ public WebClient () {
33+ this (Level .INFO );
34+ }
35+
36+ public WebClient (Level logLevel ) {
37+ System .setProperty ("org.eclipse.jetty.client.LEVEL" , logLevel .getName ());
38+ }
39+
40+ //private Pattern urlMatcher = Pattern.compile("^(?<protocol>http[s]?):\\/\\/(?<host>[\\w.]+)(?:(?=:):(?<port>9000)|\\/)");
41+
42+ public String getResponse (String url )
43+ throws URISyntaxException , InterruptedException , ExecutionException , TimeoutException {
44+ URI uri = new URI (url );
45+
46+ String host = uri .getHost ();
47+ int port = uri .getPort ();
48+ if (port == -1 ) {
49+ port = 443 ;
50+ }
51+ String scheme = uri .getScheme ();
52+
53+ FuturePromise <Session > sessionPromise = new FuturePromise <>();
54+ if (scheme .contains ("https" )) {
55+ client .connect (sslContextFactory , new InetSocketAddress (host , port ), new ServerSessionListener .Adapter (),
56+ sessionPromise );
57+ } else {
58+ client .connect (new InetSocketAddress (host , port ), new ServerSessionListener .Adapter (), sessionPromise );
59+ }
60+
61+ Session session = sessionPromise .get (5 , TimeUnit .SECONDS );
62+
63+ HttpFields requestFields = new HttpFields ();
64+ requestFields .put ("User-Agent" , client .getClass ().getName () + "/" + Jetty .VERSION );
65+ requestFields .put ("Host" , host + ":" + port );
66+
67+ MetaData .Request request = new MetaData .Request ("GET" , new HttpURI (url ), HttpVersion .HTTP_2 , requestFields );
68+
69+ HeadersFrame headersFrame = new HeadersFrame (request , null , true );
70+
71+ CountDownLatch latch = new CountDownLatch (1 );
72+ final StringBuilder response = new StringBuilder ();
73+ Stream .Listener responseListener = new Stream .Listener .Adapter () {
74+ @ Override
75+ public void onData (Stream stream , DataFrame frame , Callback callback ) {
76+ byte [] bytes = new byte [frame .getData ().remaining ()];
77+ frame .getData ().get (bytes );
78+ response .append (new String (bytes ));
79+ latch .countDown ();
80+ callback .succeeded ();
81+ }
82+ };
83+
84+ session .newStream (headersFrame , new FuturePromise <>(), responseListener );
85+
86+ if (!latch .await (1 , TimeUnit .SECONDS )) {
87+ throw new RuntimeException ("The request timed out." );
88+ }
89+ return response .toString ();
90+ }
91+
92+ public void start () throws Exception {
93+ client = new HTTP2Client ();
94+
95+ // Configure SSL for test
96+ sslContextFactory = new SslContextFactory (true );
97+ client .addBean (sslContextFactory );
98+
99+ // Start client
100+ client .start ();
101+ }
102+
103+ public void stop () throws Exception {
104+ client .stop ();
105+ }
106+
107+ }
0 commit comments