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 27057bc Revert "CXF-7581: SwaggerUIResourceFilter doesn't allow call
to service endpoint. Reverting the initial solution (not generic enough)"
27057bc is described below
commit 27057bcdfe2e2a90100a75413fc22ed4bc58ebae
Author: reta <[email protected]>
AuthorDate: Wed Dec 13 12:17:01 2017 -0500
Revert "CXF-7581: SwaggerUIResourceFilter doesn't allow call to service
endpoint. Reverting the initial solution (not generic enough)"
This reverts commit 5b90271e05b62fb8b576d3a48bd224abd4871af0.
---
.../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 | 1 -
5 files changed, 88 insertions(+), 20 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 a204bb1..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
@@ -188,7 +188,6 @@ public abstract class
AbstractSwagger2ServiceDescriptionTest extends AbstractBus
}
@Test
- @Ignore
public void testNonUiResource() {
// Test that Swagger UI resources do not interfere with
// application-specific ones.
--
To stop receiving notification emails like this one, please contact
['"[email protected]" <[email protected]>'].