diff --git a/examples/https-clientserver-grizzly/README.html b/examples/https-clientserver-grizzly/README.html index 7ff9fb9065..ad7232b202 100644 --- a/examples/https-clientserver-grizzly/README.html +++ b/examples/https-clientserver-grizzly/README.html @@ -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 @@ -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> diff --git a/examples/https-clientserver-grizzly/pom.xml b/examples/https-clientserver-grizzly/pom.xml index 3d664f13a3..5b721f0ecf 100644 --- a/examples/https-clientserver-grizzly/pom.xml +++ b/examples/https-clientserver-grizzly/pom.xml @@ -61,11 +61,6 @@ <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> @@ -73,13 +68,18 @@ <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> diff --git a/examples/https-clientserver-grizzly/src/main/java/org/glassfish/jersey/examples/httpsclientservergrizzly/Server.java b/examples/https-clientserver-grizzly/src/main/java/org/glassfish/jersey/examples/httpsclientservergrizzly/Server.java index c3ccd11dc7..6740fcfc0c 100644 --- a/examples/https-clientserver-grizzly/src/main/java/org/glassfish/jersey/examples/httpsclientservergrizzly/Server.java +++ b/examples/https-clientserver-grizzly/src/main/java/org/glassfish/jersey/examples/httpsclientservergrizzly/Server.java @@ -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; @@ -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) { @@ -77,7 +88,7 @@ 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 + "]."); } @@ -85,8 +96,13 @@ private static int getPort(int 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(); @@ -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(); } diff --git a/examples/https-clientserver-grizzly/src/test/java/org/glassfish/jersey/examples/httpsclientservergrizzly/MainTest.java b/examples/https-clientserver-grizzly/src/test/java/org/glassfish/jersey/examples/httpsclientservergrizzly/MainTest.java index be5ef06386..800ea75b38 100644 --- a/examples/https-clientserver-grizzly/src/test/java/org/glassfish/jersey/examples/httpsclientservergrizzly/MainTest.java +++ b/examples/https-clientserver-grizzly/src/test/java/org/glassfish/jersey/examples/httpsclientservergrizzly/MainTest.java @@ -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 @@ -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 @@ -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);