You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-commits@axis.apache.org by ve...@apache.org on 2017/01/14 18:30:34 UTC

svn commit: r1778814 - in /axis/axis2/java/rampart/trunk/modules/rampart-integration/src: main/java/org/apache/axis2/integration/JettyServer.java main/java/org/apache/rahas/TestClient.java test/java/org/apache/rampart/RampartTest.java

Author: veithen
Date: Sat Jan 14 18:30:34 2017
New Revision: 1778814

URL: http://svn.apache.org/viewvc?rev=1778814&view=rev
Log:
Support either HTTP or HTTPS in JettyServer, but not both at the same time, so that we can converge towards the same interface as Axis2Server.

Modified:
    axis/axis2/java/rampart/trunk/modules/rampart-integration/src/main/java/org/apache/axis2/integration/JettyServer.java
    axis/axis2/java/rampart/trunk/modules/rampart-integration/src/main/java/org/apache/rahas/TestClient.java
    axis/axis2/java/rampart/trunk/modules/rampart-integration/src/test/java/org/apache/rampart/RampartTest.java

Modified: axis/axis2/java/rampart/trunk/modules/rampart-integration/src/main/java/org/apache/axis2/integration/JettyServer.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/rampart/trunk/modules/rampart-integration/src/main/java/org/apache/axis2/integration/JettyServer.java?rev=1778814&r1=1778813&r2=1778814&view=diff
==============================================================================
--- axis/axis2/java/rampart/trunk/modules/rampart-integration/src/main/java/org/apache/axis2/integration/JettyServer.java (original)
+++ axis/axis2/java/rampart/trunk/modules/rampart-integration/src/main/java/org/apache/axis2/integration/JettyServer.java Sat Jan 14 18:30:34 2017
@@ -73,8 +73,8 @@ public class JettyServer extends Externa
     private static final Logger logger = LoggerFactory.getLogger(JettyServer.class);
     
     private final String repository;
-    private final int httpPort;
-    private final int httpsPort;
+    private final int port;
+    private final boolean secure;
     private Server server;
     private boolean systemPropertiesSet;
     private String savedTrustStore;
@@ -86,49 +86,32 @@ public class JettyServer extends Externa
      * 
      * @param repository
      *            The path to the Axis2 repository to use. Must not be null or empty.
-     * @param httpPort
-     *            The http port to use. Set to <code>-1</code> to disable http connector. Set to
-     *            <code>0</code> to enable dynamic port allocation.
-     * @param httpsPort
-     *            The https port to use. Set to <code>-1</code> to disable https connector. Set to
-     *            <code>0</code> to enable dynamic port allocation.
-     * @throws IllegalArgumentException If both ports are set to <code>-1</code>
+     * @param port
+     *            The port to use. Set to <code>0</code> to enable dynamic port allocation.
+     * @param secure
+     *            Whether to enable HTTPS.
      */
-    public JettyServer(String repository, int httpPort, int httpsPort) {
+    public JettyServer(String repository, int port, boolean secure) {
         if (repository == null || repository.trim().length() == 0) {
             throw new IllegalArgumentException("Axis2 repository must not be null or empty");
         }
-        if (httpPort == -1 && httpsPort == -1) {
-            throw new IllegalArgumentException("At least one port must be specified.");
-        }
         this.repository = repository;
-        this.httpPort = httpPort;
-        this.httpsPort = httpsPort;
+        this.port = port;
+        this.secure = secure;
     }
     
     @Override
     protected void before() throws Throwable {
-        int httpPort = this.httpPort == 0 ? PortAllocator.allocatePort() : this.httpPort;
-        int httpsPort = this.httpsPort == 0 ? PortAllocator.allocatePort() : this.httpsPort;
+        int port = this.port == 0 ? PortAllocator.allocatePort() : this.port;
         
         server = new Server();
         
-        SelectChannelConnector connector = null;
-        if (httpPort == -1) {
-            logger.debug("Http connector is disabled");
-        }
-        else {
-            logger.info("Starting http connector on port: " + httpPort);
-            
-            connector = new SelectChannelConnector();
-            connector.setPort(httpPort);
+        logger.info("Starting server on port: " + port);
+        if (!secure) {
+            SelectChannelConnector connector = new SelectChannelConnector();
+            connector.setPort(port);
             server.addConnector(connector);
-        }
-        
-        if (httpsPort == -1) {
-            logger.debug("Https connector is disabled");
-        }
-        else {
+        } else {
             SslContextFactory sslContextFactory = new SslContextFactory();
             sslContextFactory.setKeyStorePath(KEYSTORE);
             sslContextFactory.setKeyStorePassword(KEYSTORE_PASSWORD);
@@ -138,15 +121,9 @@ public class JettyServer extends Externa
             sslContextFactory.setCertAlias(CERT_ALIAS);
             SslSelectChannelConnector sslConnector = new SslSelectChannelConnector(sslContextFactory);
             
-            logger.info("Starting https connector on port: " + httpsPort);
-            
-            sslConnector.setPort(httpsPort);
+            sslConnector.setPort(port);
             server.addConnector(sslConnector);
             
-            if (connector != null) {
-                connector.setConfidentialPort(httpsPort);
-            }
-            
             savedTrustStore = System.getProperty("javax.net.ssl.trustStore");
             System.setProperty("javax.net.ssl.trustStore", CLIENT_KEYSTORE);
             savedTrustStorePassword = System.getProperty("javax.net.ssl.trustStorePassword");
@@ -231,53 +208,26 @@ public class JettyServer extends Externa
      * @return Jetty's http connector port. 
      * @throws IllegalStateException If Jetty is not running or the http connector cannot be found.
      */
-    public int getHttpPort() throws IllegalStateException {
-        assertStarted();
-        
-        Connector[] connectors = server.getConnectors();
-        if (connectors.length == 0) {
-            throw new IllegalStateException("Jetty server is not configured with any connectors");
+    public int getPort() throws IllegalStateException {
+        if (server == null) {
+            throw new IllegalStateException("Jetty server is not initialized");
         }
-        
-        for (Connector connector : connectors) {
-            if ((connector instanceof SelectChannelConnector) &&
-                !(connector instanceof SslSelectChannelConnector)) {
-                //must be the http connector
-                return connector.getPort();
-            }
+        if (!server.isStarted()) {
+            throw new IllegalStateException("Jetty server is not started");
         }
         
-        throw new IllegalStateException("Could not find Jetty http connector");
-    }
-    
-    /**
-     * @return Jetty's ssl connector port. 
-     * @throws IllegalStateException If Jetty is not running or the ssl connector cannot be found.
-     */
-    public int getHttpsPort() throws IllegalStateException {
-        assertStarted();
-        
         Connector[] connectors = server.getConnectors();
         if (connectors.length == 0) {
             throw new IllegalStateException("Jetty server is not configured with any connectors");
         }
         
         for (Connector connector : connectors) {
-            if (connector instanceof SslSelectChannelConnector) {
-                //must be the https connector
+            if (connector instanceof SelectChannelConnector) {
+                //must be the http connector
                 return connector.getPort();
             }
         }
         
-        throw new IllegalStateException("Could not find Jetty https connector");
-    }
-    
-    private void assertStarted() throws IllegalStateException {
-        if (server == null) {
-            throw new IllegalStateException("Jetty server is not initialized");
-        }
-        else if (!server.isStarted()) {
-            throw new IllegalStateException("Jetty server is not started");
-        }
+        throw new IllegalStateException("Could not find Jetty http connector");
     }
 }

Modified: axis/axis2/java/rampart/trunk/modules/rampart-integration/src/main/java/org/apache/rahas/TestClient.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/rampart/trunk/modules/rampart-integration/src/main/java/org/apache/rahas/TestClient.java?rev=1778814&r1=1778813&r2=1778814&view=diff
==============================================================================
--- axis/axis2/java/rampart/trunk/modules/rampart-integration/src/main/java/org/apache/rahas/TestClient.java (original)
+++ axis/axis2/java/rampart/trunk/modules/rampart-integration/src/main/java/org/apache/rahas/TestClient.java Sat Jan 14 18:30:34 2017
@@ -42,7 +42,7 @@ public abstract class TestClient {
     protected int port = 5555;
 
     @Rule
-    public final JettyServer server = new JettyServer(Constants.TESTING_PATH + getServiceRepo(), port, -1);
+    public final JettyServer server = new JettyServer(Constants.TESTING_PATH + getServiceRepo(), port, false);
 
     /**
      */

Modified: axis/axis2/java/rampart/trunk/modules/rampart-integration/src/test/java/org/apache/rampart/RampartTest.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/rampart/trunk/modules/rampart-integration/src/test/java/org/apache/rampart/RampartTest.java?rev=1778814&r1=1778813&r2=1778814&view=diff
==============================================================================
--- axis/axis2/java/rampart/trunk/modules/rampart-integration/src/test/java/org/apache/rampart/RampartTest.java (original)
+++ axis/axis2/java/rampart/trunk/modules/rampart-integration/src/test/java/org/apache/rampart/RampartTest.java Sat Jan 14 18:30:34 2017
@@ -47,8 +47,10 @@ public class RampartTest {
     private static ResourceBundle resources;
     
     @Rule
-    public final JettyServer server = new JettyServer(Constants.TESTING_PATH + "rampart_service_repo", 0, 0);
+    public final JettyServer server = new JettyServer(Constants.TESTING_PATH + "rampart_service_repo", 0, false);
     
+    @Rule
+    public final JettyServer secureServer = new JettyServer(Constants.TESTING_PATH + "rampart_service_repo", 0, true);
     
     static {
         try {
@@ -103,7 +105,7 @@ public class RampartTest {
                 
                 if( i == 13 ) {
                     options.setTo(new EndpointReference("https://localhost:" +
-                                    server.getHttpsPort() +  
+                                    secureServer.getPort() +  
                                     "/axis2/services/SecureService" + i));
                     //Username token created with user/pass from options
                     options.setUserName("alice");
@@ -111,7 +113,7 @@ public class RampartTest {
                 }
                 else {
                     options.setTo(new EndpointReference("http://localhost:" +
-                                    server.getHttpPort() +  
+                                    server.getPort() +  
                                     "/axis2/services/SecureService" + i));
                 }
                 
@@ -179,7 +181,7 @@ public class RampartTest {
 
                 if (i == 13) {
                     options.setTo(new EndpointReference("https://localhost:" +
-                                    server.getHttpsPort() +
+                                    secureServer.getPort() +
                                     "/axis2/services/SecureService" + i));
                     //Username token created with user/pass from options
                     options.setUserName("alice");
@@ -187,7 +189,7 @@ public class RampartTest {
                 }
                 else {
                     options.setTo(new EndpointReference("http://localhost:" +
-                                    server.getHttpPort() +
+                                    server.getPort() +
                                     "/axis2/services/SecureService" + i));
                 }
                 System.out.println("Testing WS-Sec: negative scenario " + i);
@@ -213,10 +215,10 @@ public class RampartTest {
                 Options options = new Options();
                 
                 if (i == 3 || i == 6) {
-                    options.setTo(new EndpointReference("https://localhost:" + server.getHttpsPort() + "/axis2/services/SecureServiceSC" + i));
+                    options.setTo(new EndpointReference("https://localhost:" + secureServer.getPort() + "/axis2/services/SecureServiceSC" + i));
                 }
                 else {
-                    options.setTo(new EndpointReference("http://localhost:" + server.getHttpPort() + "/axis2/services/SecureServiceSC" + i));
+                    options.setTo(new EndpointReference("http://localhost:" + server.getPort() + "/axis2/services/SecureServiceSC" + i));
                 }
 
                 System.out.println("Testing WS-SecConv: custom scenario " + i);