camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rom...@apache.org
Subject svn commit: r718242 - in /activemq/camel/trunk/components/camel-jetty/src: main/java/org/apache/camel/component/jetty/ test/java/org/apache/camel/component/jetty/
Date Mon, 17 Nov 2008 13:18:58 GMT
Author: romkal
Date: Mon Nov 17 05:18:58 2008
New Revision: 718242

URL: http://svn.apache.org/viewvc?rev=718242&view=rev
Log:
CAMEL-1091 : camel jetty looks at host specified in URL now

Added:
    activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/InterfacesTest.java
  (with props)
Modified:
    activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
    activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java

Modified: activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java?rev=718242&r1=718241&r2=718242&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
(original)
+++ activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
Mon Nov 17 05:18:58 2008
@@ -26,7 +26,6 @@
 import org.apache.camel.component.http.CamelServlet;
 import org.apache.camel.component.http.HttpConsumer;
 import org.apache.camel.component.http.HttpExchange;
-
 import org.mortbay.util.ajax.Continuation;
 import org.mortbay.util.ajax.ContinuationSupport;
 
@@ -35,6 +34,8 @@
  */
 public class CamelContinuationServlet extends CamelServlet {
 
+    private static final long serialVersionUID = 1L;
+
     @Override
     protected void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
         try {

Modified: activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java?rev=718242&r1=718241&r2=718242&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
(original)
+++ activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
Mon Nov 17 05:18:58 2008
@@ -25,9 +25,12 @@
 import org.apache.camel.component.http.HttpComponent;
 import org.apache.camel.component.http.HttpConsumer;
 import org.apache.camel.component.http.HttpEndpoint;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.mortbay.jetty.Connector;
 import org.mortbay.jetty.Server;
 import org.mortbay.jetty.client.HttpClient;
+import org.mortbay.jetty.handler.ContextHandlerCollection;
 import org.mortbay.jetty.nio.SelectChannelConnector;
 import org.mortbay.jetty.security.SslSocketConnector;
 import org.mortbay.jetty.servlet.Context;
@@ -44,10 +47,12 @@
 
     class ConnectorRef {
         Connector connector;
+        CamelServlet servlet;
         int refCount;
 
-        public ConnectorRef(Connector connector) {
+        public ConnectorRef(Connector connector, CamelServlet servlet) {
             this.connector = connector;
+            this.servlet = servlet;
             increment();
         }
 
@@ -59,10 +64,11 @@
             return --refCount;
         }
     }
-
-    private CamelServlet camelServlet;
+    
+    private static final Log LOGGER = LogFactory.getLog(JettyHttpComponent.class);
+    
     private Server server;
-    private final HashMap<String, ConnectorRef> connectors = new HashMap<String,
ConnectorRef>();
+    private HashMap<String, ConnectorRef> connectors = new HashMap<String, ConnectorRef>();
     private HttpClient httpClient;
     private String sslKeyPassword;
     private String sslPassword;
@@ -87,7 +93,7 @@
 
         // Make sure that there is a connector for the requested endpoint.
         JettyHttpEndpoint endpoint = (JettyHttpEndpoint)consumer.getEndpoint();
-        String connectorKey = endpoint.getProtocol() + ":" + endpoint.getPort();
+        String connectorKey = endpoint.getProtocol() + ":" + endpoint.getHttpUri().getHost()
+ ":" + endpoint.getPort();
 
         synchronized (connectors) {
             ConnectorRef connectorRef = connectors.get(connectorKey);
@@ -99,26 +105,27 @@
                     connector = new SelectChannelConnector();
                 }
                 connector.setPort(endpoint.getPort());
-                getServer().addConnector(connector);
-                // check the session support
-                if (endpoint.isSessionSupport()) {
-                    enableSessionSupport();
+                connector.setHost(endpoint.getHttpUri().getHost());
+                if ("localhost".equalsIgnoreCase(endpoint.getHttpUri().getHost())) {
+                    LOGGER.warn("You use localhost interface! It means that no external connections
will be available. Don't you want to use 0.0.0.0 instead (all network interfaces)?");
                 }
+                getServer().addConnector(connector);
+
+                connectorRef = new ConnectorRef(connector, createServletForConnector(connector));
                 connector.start();
-                connectorRef = new ConnectorRef(connector);
+                
                 connectors.put(connectorKey, connectorRef);
+                
             } else {
                 // ref track the connector
                 connectorRef.increment();
-                // check the session support
-                if (endpoint.isSessionSupport()) {
-                    enableSessionSupport();
-                }
             }
-
+            // check the session support
+            if (endpoint.isSessionSupport()) {
+                enableSessionSupport();
+            }
+            connectorRef.servlet.connect(consumer);
         }
-
-        camelServlet.connect(consumer);
     }
 
     private void enableSessionSupport() throws Exception {
@@ -141,8 +148,6 @@
      */
     @Override
     public void disconnect(HttpConsumer consumer) throws Exception {
-        camelServlet.disconnect(consumer);
-
         // If the connector is not needed anymore then stop it
         HttpEndpoint endpoint = consumer.getEndpoint();
         String connectorKey = endpoint.getProtocol() + ":" + endpoint.getPort();
@@ -150,6 +155,7 @@
         synchronized (connectors) {
             ConnectorRef connectorRef = connectors.get(connectorKey);
             if (connectorRef != null) {
+                connectorRef.servlet.disconnect(consumer);
                 if (connectorRef.decrement() == 0) {
                     getServer().removeConnector(connectorRef.connector);
                     connectorRef.connector.stop();
@@ -215,22 +221,32 @@
         sslSocketConnector = connector;
     }
 
-    // Implementation methods
-    // -------------------------------------------------------------------------
+    protected CamelServlet createServletForConnector(Connector connector) throws Exception
{
+        CamelServlet camelServlet = new CamelContinuationServlet();
+        
+        Context context = new Context(server, "/", Context.NO_SECURITY | Context.NO_SESSIONS);
+        context.setConnectorNames(new String[] {connector.getName()});
 
-    protected Server createServer() throws Exception {
-        camelServlet = new CamelContinuationServlet();
-
-        Server server = new Server();
-        Context context = new Context(Context.NO_SECURITY | Context.NO_SESSIONS);
-
-        context.setContextPath("/");
         ServletHolder holder = new ServletHolder();
         holder.setServlet(camelServlet);
         context.addServlet(holder, "/*");
-        server.setHandler(context);
+        connector.start();
+        context.start();
+        
 
+        return camelServlet;
+    }
+    
+    // Implementation methods
+    // -------------------------------------------------------------------------
+
+    protected Server createServer() throws Exception {
+        Server server = new Server();
+        ContextHandlerCollection collection = new ContextHandlerCollection();
+        collection.setServer(server);
+        server.addHandler(collection);
         server.start();
+
         return server;
     }
 

Added: activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/InterfacesTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/InterfacesTest.java?rev=718242&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/InterfacesTest.java
(added)
+++ activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/InterfacesTest.java
Mon Nov 17 05:18:58 2008
@@ -0,0 +1,74 @@
+package org.apache.camel.component.jetty;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.net.URL;
+import java.util.Enumeration;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.commons.io.IOUtils;
+
+public class InterfacesTest extends ContextTestSupport {
+    
+    private String remoteInterfaceAddress;
+
+    public InterfacesTest() throws SocketException {
+        // retirieve an address of some remote network interface
+        Enumeration<NetworkInterface> interfaces =  NetworkInterface.getNetworkInterfaces();
+        
+        while(interfaces.hasMoreElements()) {
+            NetworkInterface interfaze = interfaces.nextElement();
+            if (!interfaze.isUp() || interfaze.isLoopback()) {
+                continue;
+            }
+            Enumeration<InetAddress> addresses =  interfaze.getInetAddresses();
+            if(addresses.hasMoreElements()) {
+                remoteInterfaceAddress = addresses.nextElement().getHostAddress();
+            }
+        };
+        
+    }
+    
+    public void testLocalInterfaceHandled() throws IOException, InterruptedException {
+        getMockEndpoint("mock:endpoint").expectedMessageCount(3);
+        
+        URL localUrl = new URL("http://localhost:4567/testRoute");
+        String localResponse = IOUtils.toString(localUrl.openStream());
+        assertEquals("local", localResponse);
+
+        // 127.0.0.1 is an alias of localhost so should work
+        localUrl = new URL("http://127.0.0.1:4568/testRoute");
+        localResponse = IOUtils.toString(localUrl.openStream());
+        assertEquals("local-differentPort", localResponse);
+        
+        URL url = new URL("http://" + remoteInterfaceAddress + ":4567/testRoute");
+        String remoteResponse = IOUtils.toString(url.openStream());
+        assertEquals("remote", remoteResponse);
+        
+        assertMockEndpointsSatisfied();
+    }
+    
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+        
+            @Override
+            public void configure() throws Exception {
+                from("jetty:http://localhost:4567/testRoute")
+                    .setBody().constant("local")
+                    .to("mock:endpoint");
+                
+                from("jetty:http://localhost:4568/testRoute")
+                    .setBody().constant("local-differentPort")
+                    .to("mock:endpoint");
+                
+                from("jetty:http://" + remoteInterfaceAddress + ":4567/testRoute")
+                    .setBody().constant("remote")
+                    .to("mock:endpoint");
+            }
+        };
+    }
+}

Propchange: activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/InterfacesTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message