CXF-5616 Netty transport should warn the user if the endpoint address conflicts 
with published service


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/eaf416c0
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/eaf416c0
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/eaf416c0

Branch: refs/heads/master
Commit: eaf416c0d51f89dd7bfc028d2f718547453466c9
Parents: 38d1f49
Author: Willem Jiang <willem.ji...@gmail.com>
Authored: Fri Mar 14 20:15:49 2014 +0800
Committer: Willem Jiang <willem.ji...@gmail.com>
Committed: Fri Mar 14 20:16:47 2014 +0800

----------------------------------------------------------------------
 .../http/netty/server/Messages.properties       |  2 +
 .../http/netty/server/NettyHttpHandler.java     | 10 +----
 .../netty/server/NettyHttpServerEngine.java     | 43 ++++++++++++++++++--
 .../netty/server/NettyHttpServerEngineTest.java | 25 ++++++++++--
 .../http/netty/server/NettyHttpTestHandler.java |  3 +-
 5 files changed, 67 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/eaf416c0/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/Messages.properties
----------------------------------------------------------------------
diff --git 
a/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/Messages.properties
 
b/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/Messages.properties
index 82551e2..18632b1 100644
--- 
a/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/Messages.properties
+++ 
b/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/Messages.properties
@@ -18,6 +18,8 @@
 #    under the License.
 #
 #
+ADD_HANDLER_CONTEXT_IS_USED_MSG = Could not add cxf Netty handler for url {0} 
to Netty server, as the path {1} is still in use.
+ADD_HANDLER_CONTEXT_CONFILICT_MSG = Could not add cxf Netty handler for url 
{0} to Netty server, as it conflicts with the registered path {1}.
 UNEXPECTED_EXCEPCTION_IN_NETTY_SERVLET_HANDLER = Unexpected exception from 
downstream in Netty servlet handler, due to: {0}.
 NO_NETTY_SERVLET_HANDLER_FOUND = No Netty servlet handler found for url: {0}.
 CREATING_NETTY_SERVER_ENGINE = Creating Netty HTTP Server Engine for port: {0}.

http://git-wip-us.apache.org/repos/asf/cxf/blob/eaf416c0/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/NettyHttpHandler.java
----------------------------------------------------------------------
diff --git 
a/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/NettyHttpHandler.java
 
b/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/NettyHttpHandler.java
index aa73f63..95fbdaa 100644
--- 
a/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/NettyHttpHandler.java
+++ 
b/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/NettyHttpHandler.java
@@ -24,6 +24,7 @@ import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import org.apache.cxf.transport.http.HttpUrlUtil;
 
 public class NettyHttpHandler {
     private String urlName;
@@ -51,13 +52,6 @@ public class NettyHttpHandler {
         return urlName;
     }
 
-    boolean checkContextPath(String target) {
-        String pathString = urlName;
-        if (!pathString.endsWith("/")) {
-            pathString = pathString + "/";
-        }
-        return target.startsWith(pathString);
-    }
 
     public void handle(String target, HttpServletRequest request,
                        HttpServletResponse response) throws IOException, 
ServletException {
@@ -66,7 +60,7 @@ public class NettyHttpHandler {
                 nettyHttpDestination.doService(servletContext, request, 
response);
             }
         } else {
-            if (target.equals(urlName) || checkContextPath(target)) {
+            if (target.equals(urlName) || 
HttpUrlUtil.checkContextPath(getName(), target)) {
                 nettyHttpDestination.doService(servletContext, request, 
response);
             }
         }

http://git-wip-us.apache.org/repos/asf/cxf/blob/eaf416c0/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/NettyHttpServerEngine.java
----------------------------------------------------------------------
diff --git 
a/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/NettyHttpServerEngine.java
 
b/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/NettyHttpServerEngine.java
index eb3435f..17fa9da 100644
--- 
a/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/NettyHttpServerEngine.java
+++ 
b/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/NettyHttpServerEngine.java
@@ -21,13 +21,20 @@ package org.apache.cxf.transport.http.netty.server;
 
 import java.net.InetSocketAddress;
 import java.net.URL;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.logging.Logger;
 import javax.annotation.PostConstruct;
 
+import org.apache.cxf.common.i18n.Message;
+import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.configuration.jsse.TLSServerParameters;
+import org.apache.cxf.interceptor.Fault;
 import org.apache.cxf.transport.HttpUriMapper;
+import org.apache.cxf.transport.http.HttpUrlUtil;
 import io.netty.bootstrap.ServerBootstrap;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelOption;
@@ -36,8 +43,12 @@ import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.nio.NioServerSocketChannel;
 import io.netty.handler.timeout.IdleStateHandler;
 
+
 public class NettyHttpServerEngine implements ServerEngine {
 
+    private static final Logger LOG =
+            LogUtils.getL7dLogger(NettyHttpServerEngine.class);
+
     /**
      * This is the network port for which this engine is allocated.
      */
@@ -67,8 +78,11 @@ public class NettyHttpServerEngine implements ServerEngine {
      */
     private TLSServerParameters tlsServerParameters;
     
-    private ThreadingParameters threadingParameters = new 
ThreadingParameters(); 
-    
+    private ThreadingParameters threadingParameters = new 
ThreadingParameters();
+
+    private List<String> registedPaths = new CopyOnWriteArrayList<String>();
+
+    // TODO need to setup configuration about them
     private int readIdleTime = 60;
     
     private int writeIdleTime = 30;
@@ -109,7 +123,6 @@ public class NettyHttpServerEngine implements ServerEngine {
     /**
      * This method is used to programmatically set the TLSServerParameters.
      * This method may only be called by the factory.
-     * @throws IOException 
      */
     public void setTlsServerParameters(TLSServerParameters params) {
         tlsServerParameters = params;
@@ -168,9 +181,26 @@ public class NettyHttpServerEngine implements ServerEngine 
{
         }
     }
 
+    protected void checkRegistedContext(URL url) {
+        String path = url.getPath();
+        for (String registedPath : registedPaths) {
+            if (path.equals(registedPath)
+                    || HttpUrlUtil.checkContextPath(registedPath, path)) {
+                // Throw the address is already used exception
+                throw new Fault(new Message("ADD_HANDLER_CONTEXT_IS_USED_MSG", 
LOG, url, registedPath));
+            }
+            if (HttpUrlUtil.checkContextPath(path, registedPath)) {
+                throw new Fault(new 
Message("ADD_HANDLER_CONTEXT_CONFILICT_MSG", LOG, url, registedPath));
+            }
+        }
+
+    }
+
 
     @Override
     public void addServant(URL url, NettyHttpHandler handler) {
+        checkRegistedContext(url);
+
         if (serverChannel == null) {
             serverChannel = startServer();
         }
@@ -184,6 +214,7 @@ public class NettyHttpServerEngine implements ServerEngine {
             handlerMap.put(contextName, contextHandler);
         }
         contextHandler.addNettyHttpHandler(handler);
+        registedPaths.add(url.getPath());
     }
 
     @Override
@@ -197,6 +228,8 @@ public class NettyHttpServerEngine implements ServerEngine {
                 handlerMap.remove(contextName);
             }
         }
+        registedPaths.remove(url.getPath());
+
     }
 
     @Override
@@ -211,6 +244,10 @@ public class NettyHttpServerEngine implements ServerEngine 
{
     }
 
     public void shutdown() {
+        // clean up the handler maps
+        handlerMap.clear();
+        registedPaths.clear();
+
         // just unbind the channel
         if (servletPipeline != null) {
             servletPipeline.shutdown();

http://git-wip-us.apache.org/repos/asf/cxf/blob/eaf416c0/rt/transports/http-netty/netty-server/src/test/java/org/apache/cxf/transport/http/netty/server/NettyHttpServerEngineTest.java
----------------------------------------------------------------------
diff --git 
a/rt/transports/http-netty/netty-server/src/test/java/org/apache/cxf/transport/http/netty/server/NettyHttpServerEngineTest.java
 
b/rt/transports/http-netty/netty-server/src/test/java/org/apache/cxf/transport/http/netty/server/NettyHttpServerEngineTest.java
index 3118a5f..aba3908 100644
--- 
a/rt/transports/http-netty/netty-server/src/test/java/org/apache/cxf/transport/http/netty/server/NettyHttpServerEngineTest.java
+++ 
b/rt/transports/http-netty/netty-server/src/test/java/org/apache/cxf/transport/http/netty/server/NettyHttpServerEngineTest.java
@@ -91,12 +91,29 @@ public class NettyHttpServerEngineTest extends Assert {
         response = getResponse(urlStr);
         assertEquals("The netty http handler did not take effect", response, 
"string1");
 
-        engine.addServant(new URL(urlStr), handler2);
-        response = getResponse(urlStr);
-        assertEquals("The netty http handler did not take effect", response, 
"string1string2");
+        try {
+            engine.addServant(new URL(urlStr), handler2);
+            fail("We don't support to publish the two service at the same 
context path");
+        } catch (Exception ex) {
+            assertTrue("Get a wrong exception message", 
ex.getMessage().indexOf("hello/test") > 0);
+        }
+
+        try {
+            engine.addServant(new URL(urlStr + "/test"), handler2);
+            fail("We don't support to publish the two service at the same 
context path");
+        } catch (Exception ex) {
+            assertTrue("Get a wrong exception message", 
ex.getMessage().indexOf("hello/test/test") > 0);
+        }
+
+        try {
+            engine.addServant(new URL("http://localhost:"; + PORT1 + "/hello"), 
handler2);
+            fail("We don't support to publish the two service at the same 
context path");
+        } catch (Exception ex) {
+            assertTrue("Get a wrong exception message", 
ex.getMessage().indexOf("hello") > 0);
+        }
+
         engine.addServant(new URL(urlStr2), handler2);
 
-        
         engine.removeServant(new URL(urlStr));
         response = getResponse(urlStr2);
         assertEquals("The netty http handler did not take effect", response, 
"string2");

http://git-wip-us.apache.org/repos/asf/cxf/blob/eaf416c0/rt/transports/http-netty/netty-server/src/test/java/org/apache/cxf/transport/http/netty/server/NettyHttpTestHandler.java
----------------------------------------------------------------------
diff --git 
a/rt/transports/http-netty/netty-server/src/test/java/org/apache/cxf/transport/http/netty/server/NettyHttpTestHandler.java
 
b/rt/transports/http-netty/netty-server/src/test/java/org/apache/cxf/transport/http/netty/server/NettyHttpTestHandler.java
index 925d0ed..1fb6e8b 100644
--- 
a/rt/transports/http-netty/netty-server/src/test/java/org/apache/cxf/transport/http/netty/server/NettyHttpTestHandler.java
+++ 
b/rt/transports/http-netty/netty-server/src/test/java/org/apache/cxf/transport/http/netty/server/NettyHttpTestHandler.java
@@ -24,6 +24,7 @@ import java.io.IOException;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import org.apache.cxf.transport.http.HttpUrlUtil;
 
 
 public class NettyHttpTestHandler extends NettyHttpHandler {
@@ -47,7 +48,7 @@ public class NettyHttpTestHandler extends NettyHttpHandler {
             resp.flushBuffer();
 
         } else {
-            if (target.equals(getName()) || checkContextPath(target)) {
+            if (target.equals(getName()) || 
HttpUrlUtil.checkContextPath(getName(), target)) {
                 resp.getOutputStream().write(response.getBytes());
                 resp.flushBuffer();
             }

Reply via email to