From b4f48f407a8fefba16395eceb34de6bd48e74d12 Mon Sep 17 00:00:00 2001 From: rfeng Date: Sun, 14 Mar 2010 00:50:38 +0000 Subject: 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 --- .../apache/tuscany/sca/http/jetty/JettyServer.java | 49 ++++++++++++++++------ 1 file changed, 36 insertions(+), 13 deletions(-) (limited to 'sca-java-2.x/trunk/modules/host-jetty/src/main') 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() { 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 -- cgit v1.2.3