Skip to content

Commit b75a663

Browse files
committed
Converted to new web client.
1 parent 4febf9d commit b75a663

File tree

2 files changed

+122
-47
lines changed

2 files changed

+122
-47
lines changed
Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
package org.javaee8.servlet.http2;
22

3-
import static org.eclipse.jetty.http.HttpVersion.HTTP_2;
43
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
5-
import static org.junit.Assert.assertFalse;
6-
import static org.junit.Assert.assertTrue;
7-
import static org.junit.Assert.fail;
4+
import static org.junit.Assert.assertNotNull;
85

96
import java.io.File;
7+
import java.net.URISyntaxException;
108
import java.net.URL;
119
import java.util.concurrent.ExecutionException;
1210
import java.util.concurrent.TimeoutException;
11+
import java.util.logging.Level;
1312

14-
import org.eclipse.jetty.client.HttpClient;
15-
import org.eclipse.jetty.client.api.ContentResponse;
16-
import org.eclipse.jetty.http.HttpVersion;
17-
import org.eclipse.jetty.http2.client.HTTP2Client;
18-
import org.eclipse.jetty.http2.client.http.HttpClientTransportOverHTTP2;
19-
import org.eclipse.jetty.util.ssl.SslContextFactory;
2013
import org.jboss.arquillian.container.test.api.Deployment;
2114
import org.jboss.arquillian.container.test.api.RunAsClient;
2215
import org.jboss.arquillian.junit.Arquillian;
@@ -30,64 +23,39 @@
3023
@RunWith(Arquillian.class)
3124
public class Http2Test {
3225

33-
private HttpClient httpClient;
26+
private WebClient client;
3427

3528
@Deployment
3629
public static WebArchive createDeployment() {
37-
return create(WebArchive.class)
38-
.addPackages(true, "org.javaee8.servlet.http2")
30+
return create(WebArchive.class).addPackages(true, "org.javaee8.servlet.http2")
3931
.addAsWebResource(new File("src/main/webapp/images/payara-logo.jpg"), "images/payara-logo.jpg")
4032
.addAsWebInfResource(new File("src/main/webapp/WEB-INF/web.xml"));
4133
}
4234

4335
@Test
4436
@RunAsClient
45-
public void testHttp2ControlGroup() {
46-
String url = "https://javaee.github.io/";
47-
testUrl(url);
37+
public void testHttp2ControlGroup()
38+
throws InterruptedException, ExecutionException, TimeoutException, URISyntaxException {
39+
String url = "https://http2.akamai.com/";
40+
assertNotNull(client.getResponse(url));
4841
}
4942

5043
@Test
5144
@RunAsClient
52-
public void testServerHttp2(@ArquillianResource URL base) {
53-
String url = String.format("https://%s:%d%s", base.getHost(), 8181, base.getPath());
54-
testUrl(url);
55-
}
56-
57-
private void testUrl(String url) {
58-
System.out.println("Testing url: " + url);
59-
ContentResponse response = null;
60-
try {
61-
response = httpClient.GET(url);
62-
} catch (InterruptedException ex) {
63-
fail("Request was interruped with exception: " + ex.getMessage());
64-
} catch (ExecutionException ex) {
65-
fail("Exception whilst executing request: " + ex.getMessage());
66-
} catch (TimeoutException ex) {
67-
fail("Request timed out with exception: " + ex.getMessage());
68-
}
69-
70-
assertFalse("Error getting the response.", response == null);
71-
72-
HttpVersion protocol = response.getVersion();
73-
74-
assertTrue(String.format("The page was delivered over %s instead of HTTP 2.", protocol.asString()),
75-
protocol.equals(HTTP_2));
45+
public void testServerHttp2(@ArquillianResource URL url)
46+
throws InterruptedException, ExecutionException, TimeoutException, URISyntaxException {
47+
assertNotNull(client.getResponse(url.toString()));
7648
}
7749

7850
@Before
7951
public void setup() throws Exception {
80-
//System.setProperty("org.eclipse.jetty.client.LEVEL", "DEBUG");
81-
SslContextFactory sslContextFactory = new SslContextFactory(true);
82-
HTTP2Client http2Client = new HTTP2Client();
83-
HttpClientTransportOverHTTP2 http2Transport = new HttpClientTransportOverHTTP2(http2Client);
84-
httpClient = new HttpClient(http2Transport, sslContextFactory);
85-
httpClient.start();
52+
client = new WebClient(Level.INFO);
53+
client.start();
8654
}
8755

8856
@After
8957
public void cleanUp() throws Exception {
90-
httpClient.stop();
58+
client.stop();
9159
}
9260

9361
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

Comments
 (0)