cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-7351, CXF-7352] UriInfo.getMatchedUris does not work if the root-class templates are used or when a CORS preflight method is checked
Date Tue, 02 May 2017 21:19:26 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.1.x-fixes df104ce05 -> 653a93f6e


[CXF-7351, CXF-7352] UriInfo.getMatchedUris does not work if the root-class templates are
used or when a CORS preflight method is checked


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

Branch: refs/heads/3.1.x-fixes
Commit: 653a93f6e3321ed558c5edd80603b79e18d36a12
Parents: df104ce
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Tue May 2 17:45:31 2017 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Tue May 2 22:19:07 2017 +0100

----------------------------------------------------------------------
 .../jaxrs/interceptor/JAXRSInInterceptor.java   |  12 +--
 .../org/apache/cxf/jaxrs/utils/JAXRSUtils.java  |  38 +++++--
 .../cors/CrossOriginResourceSharingFilter.java  |  10 +-
 .../systest/jaxrs/JAXRSUriInfoMatchTest.java    | 107 +++++++++++++++++++
 .../jaxrs_uriinfo_match/WEB-INF/beans.xml       |  35 ++++++
 .../jaxrs_uriinfo_match/WEB-INF/web.xml         |  45 ++++++++
 6 files changed, 226 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/653a93f6/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java
index 6b70283..a32731c 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSInInterceptor.java
@@ -170,12 +170,12 @@ public class JAXRSInInterceptor extends AbstractPhaseInterceptor<Message>
{
         }
 
         MultivaluedMap<String, String> matchedValues = new MetadataMap<String, String>();
-                
-        OperationResourceInfo ori = null;     
-        
-        try {                
-            ori = JAXRSUtils.findTargetMethod(matchedResources, message, 
-                      httpMethod, matchedValues, requestContentType, acceptContentTypes,
true);
+
+        OperationResourceInfo ori = null;
+
+        try {
+            ori = JAXRSUtils.findTargetMethod(matchedResources, message,
+                      httpMethod, matchedValues, requestContentType, acceptContentTypes,
true, true);
             setExchangeProperties(message, exchange, ori, matchedValues, resources.size());
         } catch (WebApplicationException ex) {
             if (JAXRSUtils.noResourceMethodForOptions(ex.getResponse(), httpMethod)) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/653a93f6/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
index 2024b1d..f397dbd 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
@@ -366,8 +366,9 @@ public final class JAXRSUtils {
         String requestContentType, 
         List<MediaType> acceptContentTypes) {
         return findTargetMethod(matchedResources, message, httpMethod, matchedValues,
-                                requestContentType, acceptContentTypes, true);
+                                requestContentType, acceptContentTypes, true, true);
     }
+    //CHECKSTYLE:OFF
     public static OperationResourceInfo findTargetMethod(
         Map<ClassResourceInfo, MultivaluedMap<String, String>> matchedResources,
         Message message,
@@ -375,9 +376,10 @@ public final class JAXRSUtils {
         MultivaluedMap<String, String> matchedValues,
         String requestContentType, 
         List<MediaType> acceptContentTypes,
-        boolean throwException) {
-        
-        final boolean isFineLevelLoggable = LOG.isLoggable(Level.FINE); 
+        boolean throwException,
+        boolean recordMatchedUri) {
+    //CHECKSTYLE:ON
+        final boolean isFineLevelLoggable = LOG.isLoggable(Level.FINE);
         final boolean getMethod = HttpMethod.GET.equals(httpMethod);
         
         MediaType requestType;
@@ -489,7 +491,9 @@ public final class JAXRSUtils {
                                                                                   MEDIA_TYPE_Q_PARAM,

                                                                                   MEDIA_TYPE_QS_PARAM));
             }
-            pushOntoStack(ori, matchedValues, message);
+            if (recordMatchedUri) {
+                pushOntoStack(ori, matchedValues, message);
+            }
             return ori;
         }
         
@@ -1772,20 +1776,32 @@ public final class JAXRSUtils {
             values = Collections.emptyList();
         } else {
             values = new ArrayList<String>(params.size() - 1);
+            URITemplate rootTemplate = ori.getClassResourceInfo().getURITemplate();
+            if (rootTemplate != null) {
+                for (String var : rootTemplate.getVariables()) {
+                    addTemplateVarValues(values, params, var);
+                }
+            }
             for (Parameter pm : ori.getParameters()) {
                 if (pm.getType() == ParameterType.PATH) {
-                    List<String> paramValues = params.get(pm.getName());
-                    if (paramValues != null) {
-                        values.addAll(paramValues);
-                    }
-                    
+                    addTemplateVarValues(values, params, pm.getName());
                 }
             }
         }
         Class<?> realClass = ori.getClassResourceInfo().getServiceClass();
         stack.push(new MethodInvocationInfo(ori, realClass, values));
     }
-    
+
+    private static void addTemplateVarValues(List<String> values, 
+                                             MultivaluedMap<String, String> params,
+                                             String var) {
+        List<String> paramValues = params.get(var);
+        if (paramValues != null) {
+            values.addAll(paramValues);
+        }
+        
+    }
+
     public static String logMessageHandlerProblem(String name, Class<?> cls, MediaType
ct) {
         org.apache.cxf.common.i18n.Message errorMsg = 
             new org.apache.cxf.common.i18n.Message(name, BUNDLE, cls.getName(), mediaTypeToString(ct));

http://git-wip-us.apache.org/repos/asf/cxf/blob/653a93f6/rt/rs/security/cors/src/main/java/org/apache/cxf/rs/security/cors/CrossOriginResourceSharingFilter.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/cors/src/main/java/org/apache/cxf/rs/security/cors/CrossOriginResourceSharingFilter.java
b/rt/rs/security/cors/src/main/java/org/apache/cxf/rs/security/cors/CrossOriginResourceSharingFilter.java
index 990c089..92e4ee6 100644
--- a/rt/rs/security/cors/src/main/java/org/apache/cxf/rs/security/cors/CrossOriginResourceSharingFilter.java
+++ b/rt/rs/security/cors/src/main/java/org/apache/cxf/rs/security/cors/CrossOriginResourceSharingFilter.java
@@ -293,10 +293,12 @@ public class CrossOriginResourceSharingFilter implements ContainerRequestFilter,
                                                       Message m) {
         final String contentType = MediaType.WILDCARD;
         final MediaType acceptType = MediaType.WILDCARD_TYPE;
-        OperationResourceInfo ori = JAXRSUtils.findTargetMethod(matchedResources, 
-                                    m, httpMethod, values, 
-                                    contentType, 
-                                    Collections.singletonList(acceptType), false); 
+        OperationResourceInfo ori = JAXRSUtils.findTargetMethod(matchedResources,
+                                    m, httpMethod, values,
+                                    contentType,
+                                    Collections.singletonList(acceptType), 
+                                    false,
+                                    false);
         if (ori == null) {
             return null;
         }

http://git-wip-us.apache.org/repos/asf/cxf/blob/653a93f6/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSUriInfoMatchTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSUriInfoMatchTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSUriInfoMatchTest.java
new file mode 100644
index 0000000..ee07ac0
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSUriInfoMatchTest.java
@@ -0,0 +1,107 @@
+/**
+ * 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.systest.jaxrs;
+
+import java.util.Iterator;
+import java.util.List;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.UriInfo;
+
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.cxf.testutil.common.AbstractClientServerTestBase;
+
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+
+public class JAXRSUriInfoMatchTest extends AbstractClientServerTestBase {
+    public static final int PORT = SpringServer.PORT;
+    @BeforeClass
+    public static void beforeClass() throws Exception {
+        // must be 'in-process' to communicate with inner class in single JVM
+        // and to spawn class SpringServer w/o using main() method
+        launchServer(SpringServer.class, true);
+    }
+
+    @Ignore
+    public static class SpringServer extends AbstractSpringServer {
+        public static final int PORT = allocatePortAsInt(SpringServer.class);
+        public SpringServer() {
+            super("/jaxrs_uriinfo_match", "/match", PORT);
+        }
+    }
+
+    
+    @Test
+    public void testMatchedUris() throws Exception {
+        WebClient wc = WebClient.create("http://localhost:" + PORT + "/match/my/resource/1/matched/uris");
+        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(100000000L);
+        wc.accept("text/plain");
+        String data = wc.get(String.class);
+        assertEquals("/my/resource/1/matched/uris,/my/resource/1", data);
+    }
+    @Test
+    public void testMatchedResources() throws Exception {
+        WebClient wc = WebClient.create("http://localhost:" + PORT + "/match/my/resource/1/matched/resources");
+        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(100000000L);
+        wc.accept("text/plain");
+        String data = wc.get(String.class);
+        assertEquals("class org.apache.cxf.systest.jaxrs.JAXRSUriInfoMatchTest$Resource",
data);
+    }
+
+    @Ignore
+    @Path("my/resource/{param}")
+    @Produces(MediaType.TEXT_PLAIN)
+    public static class Resource {
+
+        @Context
+        private UriInfo uriInfo;
+
+        @GET
+        @Path("matched/uris")
+        public Object getMatchedUris() {
+            return concat(uriInfo.getMatchedURIs());
+        }
+        @GET
+        @Path("matched/resources")
+        public Object getMatchedResources() {
+            return concat(uriInfo.getMatchedResources());
+        }
+        
+    }
+    
+    private static String concat(List<?> data) {
+        StringBuilder builder = new StringBuilder();
+        Iterator<?> iterator = data.iterator();
+        while (iterator.hasNext()) {
+            builder.append(iterator.next().toString());
+            if (iterator.hasNext()) {
+                builder.append(",");
+            }
+        }
+        return builder.toString();
+    }
+
+    
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/653a93f6/systests/jaxrs/src/test/resources/jaxrs_uriinfo_match/WEB-INF/beans.xml
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/resources/jaxrs_uriinfo_match/WEB-INF/beans.xml b/systests/jaxrs/src/test/resources/jaxrs_uriinfo_match/WEB-INF/beans.xml
new file mode 100644
index 0000000..06e577c
--- /dev/null
+++ b/systests/jaxrs/src/test/resources/jaxrs_uriinfo_match/WEB-INF/beans.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+                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.
+        -->
+<!-- START SNIPPET: beans -->
+<!--
+                beans xmlns="http://www.springframework.org/schema/beans"
+                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+                xmlns:simple="http://cxf.apache.org/simple" xsi:schemaLocation="
+                http://www.springframework.org/schema/beans
+                http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
+                http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd"
+        -->
+<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation=" http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://cxf.apache.org/jaxrs  http://cxf.apache.org/schemas/jaxrs.xsd http://www.springframework.org/schema/util
 http://www.springframework.org/schema/util/spring-util-4.2.xsd">
+    <import resource="classpath:META-INF/cxf/cxf.xml"/>
+    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
+    <jaxrs:server address="/">
+        <jaxrs:serviceBeans>
+            <bean class="org.apache.cxf.systest.jaxrs.JAXRSUriInfoMatchTest$Resource"/>
+        </jaxrs:serviceBeans>
+    </jaxrs:server>
+</beans>
+<!-- END SNIPPET: beans -->

http://git-wip-us.apache.org/repos/asf/cxf/blob/653a93f6/systests/jaxrs/src/test/resources/jaxrs_uriinfo_match/WEB-INF/web.xml
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/resources/jaxrs_uriinfo_match/WEB-INF/web.xml b/systests/jaxrs/src/test/resources/jaxrs_uriinfo_match/WEB-INF/web.xml
new file mode 100644
index 0000000..a8cae3f
--- /dev/null
+++ b/systests/jaxrs/src/test/resources/jaxrs_uriinfo_match/WEB-INF/web.xml
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
+<!--
+        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.
+-->
+<!-- START SNIPPET: webxml -->
+<web-app>
+    <context-param>
+        <param-name>contextConfigLocation</param-name>
+        <param-value>WEB-INF/beans.xml</param-value>
+    </context-param>
+    <listener>
+        <listener-class>
+                        org.springframework.web.context.ContextLoaderListener
+                </listener-class>
+    </listener>
+    <servlet>
+        <servlet-name>CXFServlet</servlet-name>
+        <display-name>CXF Servlet</display-name>
+        <servlet-class>
+                        org.apache.cxf.transport.servlet.CXFServlet
+                </servlet-class>
+        <load-on-startup>1</load-on-startup>
+    </servlet>
+    <servlet-mapping>
+        <servlet-name>CXFServlet</servlet-name>
+        <url-pattern>/*</url-pattern>
+    </servlet-mapping>
+</web-app>
+<!-- END SNIPPET: webxml -->


Mime
View raw message