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

Commit

Permalink
Grizzly SSL example test fix.
Browse files Browse the repository at this point in the history
Change-Id: I5b032d627aeadd464569966689aecf8578763681
Signed-off-by: Michal Gajdos <[email protected]>
  • Loading branch information
Michal Gajdos committed Jun 3, 2015
1 parent ae1989b commit 5a94b0f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 35 deletions.
4 changes: 2 additions & 2 deletions examples/https-clientserver-grizzly/README.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
Copyright (c) 2010-2012 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2010-2015 Oracle and/or its affiliates. All rights reserved.
The contents of this file are subject to the terms of either the GNU
General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -110,7 +110,7 @@ <h2>Running the Example</h2>
<blockquote><code>mvn compile exec:java</code></blockquote></p>
<p>From a web browser, visit:<br />
<b style="color: red">This won't work! *</b>
<blockquote><code><a href="https://localhost:4463/">https://localhost:4463/</a></code></blockquote>
<blockquote><code><a href="https://localhost:8463/">https://localhost:8463/</a></code></blockquote>
<b style="color: red">[*]</b> Your web browser needs have and use generated
client keys. Or you have to disable server side client
authentication - set NeedClientAuth to false: <b>new SSLEngineConfigurator(sslContext).setClientMode(false).setNeedClientAuth(<span style="color:red">false</span>)</b>
Expand Down
12 changes: 6 additions & 6 deletions examples/https-clientserver-grizzly/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,25 @@
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-grizzly-connector</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.io.IOException;
import java.net.URI;
import java.security.AccessController;
import java.util.logging.Logger;

import javax.ws.rs.core.UriBuilder;

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

/**
* A simple SSL-secured HTTP server.
*
* @author Pavel Bucek (pavel.bucek at oracle.com)
*/
public class Server {

private static final Logger LOGGER = Logger.getLogger(Server.class.getName());

private static final String KEYSTORE_SERVER_FILE = "./keystore_server";
private static final String KEYSTORE_SERVER_PWD = "asdfgh";
private static final String TRUSTORE_SERVER_FILE = "./truststore_server";
private static final String TRUSTORE_SERVER_PWD = "asdfgh";
private static HttpServer webServer;

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

private final HttpServer webServer;

private Server(final HttpServer webServer) {
this.webServer = webServer;
}

private static URI getBaseURI() {
return UriBuilder.fromUri("https://localhost/").port(getPort(4463)).build();
return UriBuilder.fromUri("https://localhost/").port(getPort(8463)).build();
}

private static int getPort(int defaultPort) {
Expand All @@ -77,16 +88,21 @@ private static int getPort(int defaultPort) {
try {
return Integer.parseInt(port);
} catch (NumberFormatException e) {
System.out.println("Value of jersey.config.test.container.port property"
LOGGER.warning("Value of jersey.config.test.container.port property"
+ " is not a valid positive integer [" + port + "]."
+ " Reverting to default [" + defaultPort + "].");
}
}
return defaultPort;
}

protected static void startServer() {

/**
* Start SSL-secured HTTP test server.
*
* @throws IOException in case there is an error while reading server key store or trust store.
* @return an instance of the started SSL-secured HTTP test server.
*/
public static Server start() throws IOException {
// Grizzly ssl configuration
SSLContextConfigurator sslContext = new SSLContextConfigurator();

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

try {
webServer = GrizzlyHttpServerFactory.createHttpServer(
getBaseURI(),
rc,
true,
new SSLEngineConfigurator(sslContext).setClientMode(false).setNeedClientAuth(true)
);

// start Grizzly embedded server //
System.out.println("Jersey app started. Try out " + BASE_URI + "\nHit CTRL + C to stop it...");
webServer.start();

} catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex.getMessage());
}
final HttpServer grizzlyServer = GrizzlyHttpServerFactory.createHttpServer(
getBaseURI(),
rc,
true,
new SSLEngineConfigurator(sslContext).setClientMode(false).setNeedClientAuth(true)
);

// start Grizzly embedded server //
LOGGER.info("Jersey app started. Try out " + BASE_URI + "\nHit CTRL + C to stop it...");
grizzlyServer.start();

return new Server(grizzlyServer);
}

protected static void stopServer() {
/**
* Stop SSL-secured HTTP test server.
*/
public void stop() {
webServer.shutdownNow();
}

@SuppressWarnings("ResultOfMethodCallIgnored")
public static void main(String[] args) throws InterruptedException, IOException {
startServer();
start();

System.in.read();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2010-2013 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010-2015 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -68,19 +68,35 @@
* @author Pavel Bucek (pavel.bucek at oracle.com)
*/
public class MainTest {

private static final String TRUSTORE_CLIENT_FILE = "./truststore_client";
private static final String TRUSTSTORE_CLIENT_PWD = "asdfgh";
private static final String KEYSTORE_CLIENT_FILE = "./keystore_client";
private static final String KEYSTORE_CLIENT_PWD = "asdfgh";

private final Object serverGuard = new Object();
private Server server = null;

@Before
public void setUp() throws Exception {
Server.startServer();
synchronized (serverGuard) {
if (server != null) {
throw new IllegalStateException(
"Test run sync issue: Another instance of the SSL-secured HTTP test server has been already started.");
}
server = Server.start();
}
}

@After
public void tearDown() throws Exception {
Server.stopServer();
synchronized (serverGuard) {
if (server == null) {
throw new IllegalStateException("Test run sync issue: There is no SSL-secured HTTP test server to stop.");
}
server.stop();
server = null;
}
}

@Test
Expand Down Expand Up @@ -159,7 +175,7 @@ private void _testWithoutBasicAuth(ClientConfig clientConfig) {
WebTarget target = client.target(Server.BASE_URI);
target.register(new LoggingFilter());

Response response = null;
Response response;

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

0 comments on commit 5a94b0f

Please sign in to comment.