Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 5a94b0f

Browse files
author
Michal Gajdos
committed
Grizzly SSL example test fix.
Change-Id: I5b032d627aeadd464569966689aecf8578763681 Signed-off-by: Michal Gajdos <[email protected]>
1 parent ae1989b commit 5a94b0f

File tree

4 files changed

+66
-35
lines changed

4 files changed

+66
-35
lines changed

examples/https-clientserver-grizzly/README.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
55
6-
Copyright (c) 2010-2012 Oracle and/or its affiliates. All rights reserved.
6+
Copyright (c) 2010-2015 Oracle and/or its affiliates. All rights reserved.
77
88
The contents of this file are subject to the terms of either the GNU
99
General Public License Version 2 only ("GPL") or the Common Development
@@ -110,7 +110,7 @@ <h2>Running the Example</h2>
110110
<blockquote><code>mvn compile exec:java</code></blockquote></p>
111111
<p>From a web browser, visit:<br />
112112
<b style="color: red">This won't work! *</b>
113-
<blockquote><code><a href="https://localhost:4463/">https://localhost:4463/</a></code></blockquote>
113+
<blockquote><code><a href="https://localhost:8463/">https://localhost:8463/</a></code></blockquote>
114114
<b style="color: red">[*]</b> Your web browser needs have and use generated
115115
client keys. Or you have to disable server side client
116116
authentication - set NeedClientAuth to false: <b>new SSLEngineConfigurator(sslContext).setClientMode(false).setNeedClientAuth(<span style="color:red">false</span>)</b>

examples/https-clientserver-grizzly/pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,25 @@
6161
<groupId>javax.annotation</groupId>
6262
<artifactId>javax.annotation-api</artifactId>
6363
</dependency>
64-
<dependency>
65-
<groupId>junit</groupId>
66-
<artifactId>junit</artifactId>
67-
<scope>test</scope>
68-
</dependency>
6964
<dependency>
7065
<groupId>org.glassfish.jersey.containers</groupId>
7166
<artifactId>jersey-container-grizzly2-servlet</artifactId>
7267
</dependency>
7368
<dependency>
7469
<groupId>org.glassfish.jersey.core</groupId>
7570
<artifactId>jersey-client</artifactId>
76-
<scope>test</scope>
7771
</dependency>
7872
<dependency>
7973
<groupId>org.glassfish.jersey.connectors</groupId>
8074
<artifactId>jersey-grizzly-connector</artifactId>
8175
<version>${project.version}</version>
8276
</dependency>
77+
78+
<dependency>
79+
<groupId>junit</groupId>
80+
<artifactId>junit</artifactId>
81+
<scope>test</scope>
82+
</dependency>
8383
</dependencies>
8484

8585
<build>

examples/https-clientserver-grizzly/src/main/java/org/glassfish/jersey/examples/httpsclientservergrizzly/Server.java

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.io.IOException;
4343
import java.net.URI;
4444
import java.security.AccessController;
45+
import java.util.logging.Logger;
4546

4647
import javax.ws.rs.core.UriBuilder;
4748

@@ -54,20 +55,30 @@
5455
import org.glassfish.grizzly.ssl.SSLEngineConfigurator;
5556

5657
/**
58+
* A simple SSL-secured HTTP server.
59+
*
5760
* @author Pavel Bucek (pavel.bucek at oracle.com)
5861
*/
5962
public class Server {
63+
64+
private static final Logger LOGGER = Logger.getLogger(Server.class.getName());
65+
6066
private static final String KEYSTORE_SERVER_FILE = "./keystore_server";
6167
private static final String KEYSTORE_SERVER_PWD = "asdfgh";
6268
private static final String TRUSTORE_SERVER_FILE = "./truststore_server";
6369
private static final String TRUSTORE_SERVER_PWD = "asdfgh";
64-
private static HttpServer webServer;
6570

6671
public static final URI BASE_URI = getBaseURI();
6772
public static final String CONTENT = "JERSEY HTTPS EXAMPLE\n";
6873

74+
private final HttpServer webServer;
75+
76+
private Server(final HttpServer webServer) {
77+
this.webServer = webServer;
78+
}
79+
6980
private static URI getBaseURI() {
70-
return UriBuilder.fromUri("https://localhost/").port(getPort(4463)).build();
81+
return UriBuilder.fromUri("https://localhost/").port(getPort(8463)).build();
7182
}
7283

7384
private static int getPort(int defaultPort) {
@@ -77,16 +88,21 @@ private static int getPort(int defaultPort) {
7788
try {
7889
return Integer.parseInt(port);
7990
} catch (NumberFormatException e) {
80-
System.out.println("Value of jersey.config.test.container.port property"
91+
LOGGER.warning("Value of jersey.config.test.container.port property"
8192
+ " is not a valid positive integer [" + port + "]."
8293
+ " Reverting to default [" + defaultPort + "].");
8394
}
8495
}
8596
return defaultPort;
8697
}
8798

88-
protected static void startServer() {
89-
99+
/**
100+
* Start SSL-secured HTTP test server.
101+
*
102+
* @throws IOException in case there is an error while reading server key store or trust store.
103+
* @return an instance of the started SSL-secured HTTP test server.
104+
*/
105+
public static Server start() throws IOException {
90106
// Grizzly ssl configuration
91107
SSLContextConfigurator sslContext = new SSLContextConfigurator();
92108

@@ -99,31 +115,30 @@ protected static void startServer() {
99115
ResourceConfig rc = new ResourceConfig();
100116
rc.registerClasses(RootResource.class, SecurityFilter.class, AuthenticationExceptionMapper.class);
101117

102-
try {
103-
webServer = GrizzlyHttpServerFactory.createHttpServer(
104-
getBaseURI(),
105-
rc,
106-
true,
107-
new SSLEngineConfigurator(sslContext).setClientMode(false).setNeedClientAuth(true)
108-
);
109-
110-
// start Grizzly embedded server //
111-
System.out.println("Jersey app started. Try out " + BASE_URI + "\nHit CTRL + C to stop it...");
112-
webServer.start();
113-
114-
} catch (Exception ex) {
115-
ex.printStackTrace();
116-
System.out.println(ex.getMessage());
117-
}
118+
final HttpServer grizzlyServer = GrizzlyHttpServerFactory.createHttpServer(
119+
getBaseURI(),
120+
rc,
121+
true,
122+
new SSLEngineConfigurator(sslContext).setClientMode(false).setNeedClientAuth(true)
123+
);
124+
125+
// start Grizzly embedded server //
126+
LOGGER.info("Jersey app started. Try out " + BASE_URI + "\nHit CTRL + C to stop it...");
127+
grizzlyServer.start();
128+
129+
return new Server(grizzlyServer);
118130
}
119131

120-
protected static void stopServer() {
132+
/**
133+
* Stop SSL-secured HTTP test server.
134+
*/
135+
public void stop() {
121136
webServer.shutdownNow();
122137
}
123138

124139
@SuppressWarnings("ResultOfMethodCallIgnored")
125140
public static void main(String[] args) throws InterruptedException, IOException {
126-
startServer();
141+
start();
127142

128143
System.in.read();
129144
}

examples/https-clientserver-grizzly/src/test/java/org/glassfish/jersey/examples/httpsclientservergrizzly/MainTest.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2010-2013 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2010-2015 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -68,19 +68,35 @@
6868
* @author Pavel Bucek (pavel.bucek at oracle.com)
6969
*/
7070
public class MainTest {
71+
7172
private static final String TRUSTORE_CLIENT_FILE = "./truststore_client";
7273
private static final String TRUSTSTORE_CLIENT_PWD = "asdfgh";
7374
private static final String KEYSTORE_CLIENT_FILE = "./keystore_client";
7475
private static final String KEYSTORE_CLIENT_PWD = "asdfgh";
7576

77+
private final Object serverGuard = new Object();
78+
private Server server = null;
79+
7680
@Before
7781
public void setUp() throws Exception {
78-
Server.startServer();
82+
synchronized (serverGuard) {
83+
if (server != null) {
84+
throw new IllegalStateException(
85+
"Test run sync issue: Another instance of the SSL-secured HTTP test server has been already started.");
86+
}
87+
server = Server.start();
88+
}
7989
}
8090

8191
@After
8292
public void tearDown() throws Exception {
83-
Server.stopServer();
93+
synchronized (serverGuard) {
94+
if (server == null) {
95+
throw new IllegalStateException("Test run sync issue: There is no SSL-secured HTTP test server to stop.");
96+
}
97+
server.stop();
98+
server = null;
99+
}
84100
}
85101

86102
@Test
@@ -159,7 +175,7 @@ private void _testWithoutBasicAuth(ClientConfig clientConfig) {
159175
WebTarget target = client.target(Server.BASE_URI);
160176
target.register(new LoggingFilter());
161177

162-
Response response = null;
178+
Response response;
163179

164180
try {
165181
response = target.path("/").request().get(Response.class);

0 commit comments

Comments
 (0)