diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-03-14 00:50:38 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2010-03-14 00:50:38 +0000 |
commit | b4f48f407a8fefba16395eceb34de6bd48e74d12 (patch) | |
tree | 7e1bcf4456ef1dfd3b4d337ed7ea262f1259ffef /sca-java-2.x/trunk/modules/host-jetty/src | |
parent | 014971ccb5b2041147cf9d84d498cce64ea0493e (diff) |
Improve the IP address binding to be based on the host from the uri
Having servlet host to return deployed uri
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@922701 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules/host-jetty/src')
2 files changed, 62 insertions, 13 deletions
diff --git a/sca-java-2.x/trunk/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java b/sca-java-2.x/trunk/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java index 5b722528e5..ba131e0e94 100644 --- a/sca-java-2.x/trunk/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java +++ b/sca-java-2.x/trunk/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java @@ -118,6 +118,7 @@ public class JettyServer implements ServletHost, LifeCycleListener { } protected JettyServer(WorkScheduler workScheduler) { + this.defaultPort = portDefault; this.workScheduler = workScheduler; AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { @@ -203,11 +204,11 @@ public class JettyServer implements ServletHost, LifeCycleListener { } - public void addServletMapping(String suri, Servlet servlet) throws ServletMappingException { - addServletMapping(suri, servlet, null); + public String addServletMapping(String suri, Servlet servlet) throws ServletMappingException { + return addServletMapping(suri, servlet, null); } - public void addServletMapping(String suri, Servlet servlet, final SecurityContext securityContext) throws ServletMappingException { + public String addServletMapping(String suri, Servlet servlet, final SecurityContext securityContext) throws ServletMappingException { URI uri = URI.create(suri); // Get the URI scheme and port @@ -221,6 +222,11 @@ public class JettyServer implements ServletHost, LifeCycleListener { } } + String host = uri.getHost(); + if ("0.0.0.0".equals(host)) { + host = null; + } + int portNumber = uri.getPort(); if (portNumber == -1) { if ("http".equals(scheme)) { @@ -243,11 +249,17 @@ public class JettyServer implements ServletHost, LifeCycleListener { // httpConnector.setPort(portNumber); SslSocketConnector sslConnector = new SslSocketConnector(); sslConnector.setPort(portNumber); + // FIXME: [rfeng] We should set the host to be bound but binding-ws-axis2 is passing + // in an absolute URI with host set to one of the ip addresses + sslConnector.setHost(host); configureSSL(sslConnector, securityContext); server.setConnectors(new Connector[] {sslConnector}); } else { SelectChannelConnector selectConnector = new SelectChannelConnector(); selectConnector.setPort(portNumber); + // FIXME: [rfeng] We should set the host to be bound but binding-ws-axis2 is passing + // in an absolute URI with host set to one of the ip addresses + selectConnector.setHost(host); server.setConnectors(new Connector[] {selectConnector}); } @@ -318,11 +330,12 @@ public class JettyServer implements ServletHost, LifeCycleListener { servletHandler.addServletMapping(mapping); // Compute the complete URL - String host; - try { - host = InetAddress.getLocalHost().getHostName(); - } catch (UnknownHostException e) { - host = "localhost"; + if (host == null) { + try { + host = InetAddress.getLocalHost().getHostAddress(); + } catch (UnknownHostException e) { + host = "localhost"; + } } URL addedURL; try { @@ -331,9 +344,14 @@ public class JettyServer implements ServletHost, LifeCycleListener { throw new ServletMappingException(e); } logger.info("Added Servlet mapping: " + addedURL); + return addedURL.toString(); } public URL getURLMapping(String suri, SecurityContext securityContext) throws ServletMappingException { + return map(suri, securityContext, true); + } + + private URL map(String suri, SecurityContext securityContext, boolean resolve) throws ServletMappingException { URI uri = URI.create(suri); // Get the URI scheme and port @@ -357,11 +375,16 @@ public class JettyServer implements ServletHost, LifeCycleListener { } // Get the host - String host; - try { - host = InetAddress.getLocalHost().getHostName(); - } catch (UnknownHostException e) { - host = "localhost"; + String host = uri.getHost(); + if (host == null) { + host = "0.0.0.0"; + if (resolve) { + try { + host = InetAddress.getLocalHost().getHostAddress(); + } catch (UnknownHostException e) { + host = "localhost"; + } + } } // Construct the URL diff --git a/sca-java-2.x/trunk/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java b/sca-java-2.x/trunk/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java index 2ab9e46b10..a3199cf4d1 100644 --- a/sca-java-2.x/trunk/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java +++ b/sca-java-2.x/trunk/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java @@ -24,6 +24,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.ConnectException; +import java.net.InetAddress; import java.net.Socket; import java.net.URL; @@ -41,6 +42,7 @@ import junit.framework.TestCase; import org.apache.tuscany.sca.host.http.DefaultResourceServlet; import org.apache.tuscany.sca.work.NotificationListener; import org.apache.tuscany.sca.work.WorkScheduler; +import org.junit.Assert; /** * @version $Rev$ $Date$ @@ -98,6 +100,30 @@ public class JettyServerTestCase extends TestCase { service.stop(); assertTrue(servlet.invoked); } + + /** + * Verifies requests are properly routed according to the Servlet mapping + */ + public void testDeployedURI() throws Exception { + JettyServer service = new JettyServer(workScheduler); + service.setDefaultPort(8085); + service.start(); + TestServlet servlet = new TestServlet(); + String host = InetAddress.getLocalHost().getHostAddress(); + String hostName = InetAddress.getLocalHost().getHostName(); + String url1 = service.addServletMapping("/MyService", servlet); + Assert.assertEquals("http://" + host + ":8085/MyService", url1); + String url2 = service.addServletMapping("http://localhost:8086/MyService", servlet); + Assert.assertEquals("http://localhost:8086/MyService", url2); + String url3 = service.addServletMapping("http://" + host + ":8087/MyService", servlet); + Assert.assertEquals("http://" + host + ":8087/MyService", url3); + String url4 = service.addServletMapping("http://0.0.0.0:8088/MyService", servlet); + Assert.assertEquals("http://" + host + ":8088/MyService", url4); + String url5 = service.addServletMapping("http://" + hostName + ":8089/MyService", servlet); + Assert.assertEquals("http://" + hostName + ":8089/MyService", url5); + + service.stop(); + } public void testRegisterServletMappingSSL() throws Exception { System.setProperty("javax.net.ssl.keyStore", "target/test-classes/tuscany.keyStore"); |