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

reta pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/master by this push:
     new ef562f6  CXF-7581: SwaggerUIResourceFilter doesn't allow call to 
service endpoint
ef562f6 is described below

commit ef562f6c17a417081651d55364c4dc17191aa741
Author: reta <[email protected]>
AuthorDate: Tue Dec 12 22:10:48 2017 -0500

    CXF-7581: SwaggerUIResourceFilter doesn't allow call to service endpoint
---
 .../cxf/jaxrs/swagger/SwaggerUiResourceFilter.java |  8 ++-
 .../jaxrs/swagger/SwaggerUiResourceLocator.java    | 72 ++++++++++++++++++++++
 .../apache/cxf/jaxrs/swagger/SwaggerUiService.java | 22 ++-----
 .../apache/cxf/jaxrs/swagger/SwaggerUiSupport.java |  5 +-
 .../AbstractSwagger2ServiceDescriptionTest.java    | 26 ++++++++
 .../jaxrs/description/BookStoreSwagger2.java       |  8 +++
 6 files changed, 122 insertions(+), 19 deletions(-)

diff --git 
a/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerUiResourceFilter.java
 
b/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerUiResourceFilter.java
index 81f6f6c..dc97cfe 100644
--- 
a/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerUiResourceFilter.java
+++ 
b/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerUiResourceFilter.java
@@ -39,12 +39,18 @@ class SwaggerUiResourceFilter implements 
ContainerRequestFilter {
               + "/css/.*|/images/.*|/lib/.*|/fonts/.*"
         );
 
+    private final SwaggerUiResourceLocator locator;
+    
+    SwaggerUiResourceFilter(SwaggerUiResourceLocator locator) {
+        this.locator = locator;
+    }
+    
     @Override
     public void filter(ContainerRequestContext rc) throws IOException {
         if (HttpMethod.GET.equals(rc.getRequest().getMethod())) {
             UriInfo ui = rc.getUriInfo();
             String path = "/" + ui.getPath();
-            if (PATTERN.matcher(path).matches()) {
+            if (PATTERN.matcher(path).matches() && locator.exists(path)) {
                 rc.setRequestUri(URI.create("api-docs" + path));
             }
         }
diff --git 
a/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerUiResourceLocator.java
 
b/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerUiResourceLocator.java
new file mode 100644
index 0000000..7b2f081
--- /dev/null
+++ 
b/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerUiResourceLocator.java
@@ -0,0 +1,72 @@
+/**
+ * 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.jaxrs.swagger;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
+
+import org.apache.cxf.common.util.StringUtils;
+
+/**
+ * Swagger UI resource locator
+ */
+class SwaggerUiResourceLocator {
+    private final String swaggerUiRoot;
+    
+    SwaggerUiResourceLocator(String swaggerUiRoot) {
+        this.swaggerUiRoot = swaggerUiRoot;
+    }
+
+    /**
+     * Locate Swagger UI resource corresponding to resource path
+     * @param resourcePath resource path
+     * @return Swagger UI resource URL
+     * @throws MalformedURLException
+     */
+    URL locate(String resourcePath) throws MalformedURLException {
+        if (StringUtils.isEmpty(resourcePath) || "/".equals(resourcePath)) {
+            resourcePath = "index.html";
+        }
+    
+        if (resourcePath.startsWith("/")) {
+            resourcePath = resourcePath.substring(1);
+        }
+        
+        return URI.create(swaggerUiRoot + resourcePath).toURL();
+    }
+    
+    /**
+     * Checks the existence of the Swagger UI resource corresponding to 
resource path
+     * @param resourcePath resource path
+     * @return "true" if Swagger UI resource exists, "false" otherwise
+     */
+    boolean exists(String resourcePath) {
+        try {
+            // The connect() will try to locate the entry (jar file, classpath 
resource) 
+            // and fail with FileNotFoundException /IOException if there is 
none.
+            locate(resourcePath).openConnection().connect();
+            return true;
+        } catch (IOException ex) {
+            return false;
+        }
+    }
+}
diff --git 
a/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerUiService.java
 
b/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerUiService.java
index af6f3bb..49a939d 100644
--- 
a/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerUiService.java
+++ 
b/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerUiService.java
@@ -20,7 +20,6 @@
 package org.apache.cxf.jaxrs.swagger;
 
 import java.io.IOException;
-import java.net.URI;
 import java.net.URL;
 import java.util.HashMap;
 import java.util.Map;
@@ -34,9 +33,6 @@ import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.ResponseBuilder;
 import javax.ws.rs.core.UriInfo;
 
-import org.apache.cxf.common.util.StringUtils;
-
-
 @Path("api-docs")
 public class SwaggerUiService {
     private static final String FAVICON = "favicon";
@@ -56,30 +52,24 @@ public class SwaggerUiService {
         DEFAULT_MEDIA_TYPES.put("woff2", "application/font-woff2");
     }
 
-    private final String swaggerUiRoot;
-
+    
+    private final SwaggerUiResourceLocator locator;
     private final Map<String, String> mediaTypes;
 
-    public SwaggerUiService(String swaggerUiRoot, Map<String, String> 
mediaTypes) {
-        this.swaggerUiRoot = swaggerUiRoot;
+    public SwaggerUiService(SwaggerUiResourceLocator locator, Map<String, 
String> mediaTypes) {
+        this.locator = locator;
         this.mediaTypes = mediaTypes;
     }
 
     @GET
     @Path("{resource:.*}")
     public Response getResource(@Context UriInfo uriInfo, 
@PathParam("resource") String resourcePath) {
-        if (StringUtils.isEmpty(resourcePath) || "/".equals(resourcePath)) {
-            resourcePath = "index.html";
-        }
         if (resourcePath.contains(FAVICON)) {
             return Response.status(404).build();
         }
-        if (resourcePath.startsWith("/")) {
-            resourcePath = resourcePath.substring(1);
-        }
-
+        
         try {
-            URL resourceURL = URI.create(swaggerUiRoot + resourcePath).toURL();
+            final URL resourceURL = locator.locate(resourcePath);
 
             String mediaType = null;
             int ind = resourcePath.lastIndexOf('.');
diff --git 
a/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerUiSupport.java
 
b/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerUiSupport.java
index 051e54c..652bf3c 100644
--- 
a/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerUiSupport.java
+++ 
b/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerUiSupport.java
@@ -65,7 +65,8 @@ public interface SwaggerUiSupport {
             String swaggerUiRoot = findSwaggerUiRoot();
             
             if (swaggerUiRoot != null) {
-                SwaggerUiService swaggerUiService = new 
SwaggerUiService(swaggerUiRoot, getSwaggerUiMediaTypes());
+                final SwaggerUiResourceLocator locator = new 
SwaggerUiResourceLocator(swaggerUiRoot);
+                SwaggerUiService swaggerUiService = new 
SwaggerUiService(locator, getSwaggerUiMediaTypes());
                 
                 if (!runAsFilter) {
                     registration.resources.add(swaggerUiService);
@@ -73,7 +74,7 @@ public interface SwaggerUiSupport {
                     registration.providers.add(new 
SwaggerUiServiceFilter(swaggerUiService));
                 }
 
-                registration.providers.add(new SwaggerUiResourceFilter());
+                registration.providers.add(new 
SwaggerUiResourceFilter(locator));
                 bus.setProperty("swagger.service.ui.available", "true");
             }
         }
diff --git 
a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/AbstractSwagger2ServiceDescriptionTest.java
 
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/AbstractSwagger2ServiceDescriptionTest.java
index 6667aeb..c76fabf 100644
--- 
a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/AbstractSwagger2ServiceDescriptionTest.java
+++ 
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/AbstractSwagger2ServiceDescriptionTest.java
@@ -55,6 +55,10 @@ import org.junit.Test;
 
 import org.yaml.snakeyaml.Yaml;
 
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.equalTo;
+
+
 public abstract class AbstractSwagger2ServiceDescriptionTest extends 
AbstractBusClientServerTestBase {
     static final String SECURITY_DEFINITION_NAME = "basicAuth";
     
@@ -184,6 +188,28 @@ public abstract class 
AbstractSwagger2ServiceDescriptionTest extends AbstractBus
     }
 
     @Test
+    public void testNonUiResource() {
+        // Test that Swagger UI resources do not interfere with 
+        // application-specific ones.
+        WebClient uiClient = WebClient
+            .create("http://localhost:"; + getPort() + 
"/bookstore/css/book.css")
+            .accept("text/css");
+        String css = uiClient.get(String.class);
+        assertThat(css, equalTo("body { background-color: lightblue; }"));
+    }
+    
+    @Test
+    public void testUiResource() {
+        // Test that Swagger UI resources do not interfere with 
+        // application-specific ones and are accessible.
+        WebClient uiClient = WebClient
+            .create("http://localhost:"; + getPort() + "/swagger-ui.css")
+            .accept("text/css");
+        String css = uiClient.get(String.class);
+        assertThat(css, containsString(".swagger-ui{font"));
+    }
+    
+    @Test
     @Ignore
     public void testApiListingIsProperlyReturnedYAML() throws Exception {
         final WebClient client = createWebClient("/swagger.yaml");
diff --git 
a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/BookStoreSwagger2.java
 
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/BookStoreSwagger2.java
index 871f82f..46336a6 100644
--- 
a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/BookStoreSwagger2.java
+++ 
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/BookStoreSwagger2.java
@@ -79,4 +79,12 @@ public class BookStoreSwagger2 {
     public Response delete(@ApiParam(value = "id", required = true) 
@PathParam("id") String id) {
         return Response.ok().build();
     }
+    
+    @ApiOperation(hidden = true, value = "css")
+    @Produces({ "text/css" })
+    @Path("/css/book.css")
+    @GET
+    public String getCss() {
+        return "body { background-color: lightblue; }"; 
+    }
 }

-- 
To stop receiving notification emails like this one, please contact
['"[email protected]" <[email protected]>'].

Reply via email to