You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by ff...@apache.org on 2022/01/27 21:12:00 UTC

[cxf] branch 3.5.x-fixes updated: [CXF-8647]add DisallowedMethodsHandler for cxf http-undertow transport

This is an automated email from the ASF dual-hosted git repository.

ffang pushed a commit to branch 3.5.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/3.5.x-fixes by this push:
     new d725043  [CXF-8647]add DisallowedMethodsHandler for cxf http-undertow transport
d725043 is described below

commit d72504360419f605619fcf7d62cb7b5ea3e19fa0
Author: Freeman Fang <fr...@gmail.com>
AuthorDate: Thu Jan 27 16:08:23 2022 -0500

    [CXF-8647]add DisallowedMethodsHandler for cxf http-undertow transport
    
    (cherry picked from commit 445fd07abc572f110d08c1172a5bda5e305f942c)
---
 .../handlers/CxfDisallowedMethodsHandler.java      | 80 ++++++++++++++++++++++
 .../http_undertow/UndertowBasicAuthServer.java     | 10 +++
 .../http_undertow/UndertowBasicAuthTest.java       | 22 ++++++
 .../http_undertow/undertowBasicAuthServer.xml      | 12 ++++
 4 files changed, 124 insertions(+)

diff --git a/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/handlers/CxfDisallowedMethodsHandler.java b/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/handlers/CxfDisallowedMethodsHandler.java
new file mode 100644
index 0000000..77620f1
--- /dev/null
+++ b/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/handlers/CxfDisallowedMethodsHandler.java
@@ -0,0 +1,80 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.transport.http_undertow.handlers;
+
+import org.apache.cxf.transport.http_undertow.CXFUndertowHttpHandler;
+
+import io.undertow.server.HttpHandler;
+import io.undertow.server.HttpServerExchange;
+import io.undertow.server.handlers.DisallowedMethodsHandler;
+import io.undertow.util.HttpString;
+
+public class CxfDisallowedMethodsHandler implements CXFUndertowHttpHandler {
+    
+    private HttpHandler next;
+    private DisallowedMethodsHandler buildDisallowedMethodHandler;
+    private String[] methods; 
+    
+    
+    public CxfDisallowedMethodsHandler(String[] methods) {
+        this.setMethods(methods);
+    }
+    
+    
+    @Override
+    public void handleRequest(HttpServerExchange exchange) throws Exception {
+        
+        if (buildDisallowedMethodHandler == null) {
+            buildDisallowedMethodHandler();
+        }
+        this.buildDisallowedMethodHandler.handleRequest(exchange);
+    }
+
+    @Override
+    public void setNext(HttpHandler nextHandler) {
+        this.next = nextHandler;
+        
+    }
+    
+    private void buildDisallowedMethodHandler() {
+        HttpString[] strings = new HttpString[methods.length];
+        for (int i = 0; i < methods.length; ++i) {
+            strings[i] = new HttpString(methods[i]);
+        }
+
+        HttpHandler handler = this.next;
+        this.buildDisallowedMethodHandler = 
+            new DisallowedMethodsHandler(handler, strings);
+
+    }
+
+
+    public String[] getMethods() {
+        return methods;
+    }
+
+
+    public void setMethods(String[] methods) {
+        this.methods = methods;
+    }
+
+    
+    
+}
+
diff --git a/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthServer.java b/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthServer.java
index 7db5b5f..7254027 100644
--- a/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthServer.java
+++ b/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthServer.java
@@ -30,13 +30,18 @@ import org.apache.cxf.bus.spring.SpringBusFactory;
 import org.apache.cxf.ext.logging.LoggingInInterceptor;
 import org.apache.cxf.ext.logging.LoggingOutInterceptor;
 import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+import org.apache.cxf.testutil.common.TestUtil;
 
 
 public class UndertowBasicAuthServer extends AbstractBusTestServerBase  {
     static final String PORT = allocatePort(UndertowBasicAuthServer.class);
     static final String ADDRESS = "http://localhost:" + PORT + "/SoapContext/SoapPort";
+    
+    static final String PORT1 = TestUtil.getPortNumber("UndertowBasicAuthServer1");
+    static final String ADDRESS1 = "http://localhost:" + PORT1 + "/SoapContext/SoapPort";
 
     Endpoint ep;
+    Endpoint ep1;
 
     protected void run()  {
         String configurationFile = "undertowBasicAuthServer.xml";
@@ -50,6 +55,7 @@ public class UndertowBasicAuthServer extends AbstractBusTestServerBase  {
 
         GreeterImpl implementor = new GreeterImpl();
         ep = Endpoint.publish(ADDRESS, implementor);
+        ep1 = Endpoint.publish(ADDRESS1, implementor);
     }
 
     public void tearDown() throws Exception {
@@ -57,6 +63,10 @@ public class UndertowBasicAuthServer extends AbstractBusTestServerBase  {
             ep.stop();
             ep = null;
         }
+        if (ep1 != null) {
+            ep1.stop();
+            ep1 = null;
+        }
     }
 
     public static void main(String[] args) {
diff --git a/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthTest.java b/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthTest.java
index 5919ce1..b9483a5 100644
--- a/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthTest.java
+++ b/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthTest.java
@@ -46,6 +46,7 @@ import org.junit.BeforeClass;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 /**
  * Tests thread pool config.
@@ -53,10 +54,12 @@ import static org.junit.Assert.assertTrue;
 
 public class UndertowBasicAuthTest extends AbstractClientServerTestBase {
     private static final String ADDRESS = UndertowBasicAuthServer.ADDRESS;
+    private static final String ADDRESS1 = UndertowBasicAuthServer.ADDRESS1;
     private static final QName SERVICE_NAME =
         new QName("http://apache.org/hello_world_soap_http", "SOAPServiceAddressing");
 
     private Greeter greeter;
+    private Greeter greeter1;
 
     @BeforeClass
     public static void startServers() throws Exception {
@@ -75,6 +78,15 @@ public class UndertowBasicAuthTest extends AbstractClientServerTestBase {
                                    ADDRESS);
         bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "ffang");
         bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "pswd");
+        
+        greeter1 = new SOAPService(wsdl, SERVICE_NAME).getPort(Greeter.class);
+        bp = (BindingProvider)greeter1;
+        ClientProxy.getClient(greeter1).getInInterceptors().add(new LoggingInInterceptor());
+        ClientProxy.getClient(greeter1).getOutInterceptors().add(new LoggingOutInterceptor());
+        bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
+                                   ADDRESS1);
+        bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "ffang");
+        bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "pswd");
     }
 
     @org.junit.Test
@@ -83,6 +95,16 @@ public class UndertowBasicAuthTest extends AbstractClientServerTestBase {
     }
     
     @org.junit.Test
+    public void testDisalloowMethodHandler() throws Exception {
+        try {
+            greeter1.greetMe("Alice");
+            fail("should catch '405: Method Not Allowed' exception");
+        } catch (Exception ex) {
+            assertTrue(ex.getCause().getMessage().contains("405: Method Not Allowed"));
+        }
+    }
+    
+    @org.junit.Test
     public void testRequestLog() throws Exception {
         assertEquals("Hello Log", greeter.greetMe("Log"));
         File logFile = new File("target/request.log");
diff --git a/systests/transport-undertow/src/test/resources/org/apache/cxf/systest/http_undertow/undertowBasicAuthServer.xml b/systests/transport-undertow/src/test/resources/org/apache/cxf/systest/http_undertow/undertowBasicAuthServer.xml
index 47c19db..4114144 100644
--- a/systests/transport-undertow/src/test/resources/org/apache/cxf/systest/http_undertow/undertowBasicAuthServer.xml
+++ b/systests/transport-undertow/src/test/resources/org/apache/cxf/systest/http_undertow/undertowBasicAuthServer.xml
@@ -42,5 +42,17 @@
                 </bean>
             </httpu:handlers>
         </httpu:engine>
+        <httpu:engine port="${testutil.ports.UndertowBasicAuthServer1}">
+            <httpu:handlers>
+                <bean class="org.apache.cxf.transport.http_undertow.handlers.CxfDisallowedMethodsHandler">
+                    <constructor-arg>
+                        <array>
+                            <value>POST</value>
+                            <value>GET</value>
+                        </array>
+                    </constructor-arg>
+                </bean>
+            </httpu:handlers>
+        </httpu:engine>
     </httpu:engine-factory>
 </beans>