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-7010] Prototyping the code to support auto-linking to SwaggerUI in OSGI
Date Mon, 15 Aug 2016 14:22:42 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 348095868 -> 276017a4f


[CXF-7010] Prototyping the code to support auto-linking to SwaggerUI in OSGI


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

Branch: refs/heads/master
Commit: 276017a4f260c7dd9987fe320fe312485aaa88d8
Parents: 3480958
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Mon Aug 15 15:22:23 2016 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Mon Aug 15 15:22:23 2016 +0100

----------------------------------------------------------------------
 rt/rs/description-swagger/pom.xml               |  6 ++
 .../jaxrs/swagger/AbstractSwaggerFeature.java   | 27 +-------
 .../jaxrs/swagger/OsgiSwaggerUiResolver.java    | 60 +++++++++++++++++
 .../cxf/jaxrs/swagger/Swagger2Feature.java      | 32 +++++++--
 .../cxf/jaxrs/swagger/SwaggerUiResolver.java    | 68 ++++++++++++++++++++
 5 files changed, 162 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/276017a4/rt/rs/description-swagger/pom.xml
----------------------------------------------------------------------
diff --git a/rt/rs/description-swagger/pom.xml b/rt/rs/description-swagger/pom.xml
index 97ceb4d..16014e6 100644
--- a/rt/rs/description-swagger/pom.xml
+++ b/rt/rs/description-swagger/pom.xml
@@ -81,6 +81,12 @@
             <optional>true</optional>
         </dependency>
         <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+            <scope>provided</scope>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
             <groupId>org.easymock</groupId>
             <artifactId>easymock</artifactId>
             <scope>test</scope>

http://git-wip-us.apache.org/repos/asf/cxf/blob/276017a4/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/AbstractSwaggerFeature.java
----------------------------------------------------------------------
diff --git a/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/AbstractSwaggerFeature.java
b/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/AbstractSwaggerFeature.java
index 92ff8b6..0a95b32 100644
--- a/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/AbstractSwaggerFeature.java
+++ b/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/AbstractSwaggerFeature.java
@@ -18,8 +18,6 @@
  */
 package org.apache.cxf.jaxrs.swagger;
 
-import java.net.URL;
-import java.net.URLClassLoader;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -33,12 +31,10 @@ import org.apache.cxf.jaxrs.model.ClassResourceInfo;
 
 public abstract class AbstractSwaggerFeature extends AbstractFeature {
 
-    protected static final String SWAGGER_UI_RESOURCE_ROOT;
     private static final boolean SWAGGER_JAXRS_AVAILABLE;
     
     static {
         SWAGGER_JAXRS_AVAILABLE = isSwaggerJaxRsAvailable();
-        SWAGGER_UI_RESOURCE_ROOT = checkSwaggerUiResourceRoot();
     }
 
     protected boolean scan = true;
@@ -64,28 +60,7 @@ public abstract class AbstractSwaggerFeature extends AbstractFeature {
             return false;
         }    
     }
-    private static String checkSwaggerUiResourceRoot() {
-        try {
-            ClassLoader cl = AbstractSwaggerFeature.class.getClassLoader();
-            if (cl instanceof URLClassLoader) {
-                final String resourcesRootStart = "META-INF/resources/webjars/swagger-ui/";
-                for (URL url : ((URLClassLoader)cl).getURLs()) {
-                    String urlStr = url.toString();
-                    if (urlStr.contains("/swagger-ui") && urlStr.toString().endsWith(".jar"))
{
-                        urlStr = urlStr.substring(0, urlStr.length() - 4);
-                        String version = urlStr.substring(urlStr.lastIndexOf("/swagger-ui")
+ 12);
-                        return "jar:" + url.toString() + "!/"
-                                 +  resourcesRootStart + version + "/";
-                    }
-                }
-                
-            }
-        } catch (Throwable ex) {
-            // ignore
-        }   
-        return null;
-    }
-
+    
     @Override
     public void initialize(Server server, Bus bus) {
         if (!activateOnlyIfJaxrsSupported || SWAGGER_JAXRS_AVAILABLE) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/276017a4/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/OsgiSwaggerUiResolver.java
----------------------------------------------------------------------
diff --git a/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/OsgiSwaggerUiResolver.java
b/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/OsgiSwaggerUiResolver.java
new file mode 100644
index 0000000..4435a87
--- /dev/null
+++ b/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/OsgiSwaggerUiResolver.java
@@ -0,0 +1,60 @@
+/**
+ * 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.net.URL;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.FrameworkUtil;
+
+import io.swagger.annotations.Api;
+
+public class OsgiSwaggerUiResolver extends SwaggerUiResolver {
+    private static final String LOCATION = "mvn:org.webjars/swagger-ui/";
+    OsgiSwaggerUiResolver() throws Exception {
+        Class.forName("org.osgi.framework.FrameworkUtil");
+    }
+    
+    protected String findSwaggerUiRootInternal(String swaggerUiVersion) {
+        try {
+            Bundle bundle = FrameworkUtil.getBundle(Api.class);
+            if (bundle == null) {
+                return null;
+            }
+            for (Bundle b : bundle.getBundleContext().getBundles()) {
+                String location = b.getLocation();
+                if (location.equals(LOCATION + swaggerUiVersion)) {
+                    return getSwaggerUiRoot(b, swaggerUiVersion);
+                }
+            }
+        } catch (Throwable ex) {
+            // ignore
+        }   
+        return null;
+    }
+
+    private String getSwaggerUiRoot(Bundle b, String swaggerUiVersion) {
+        final String resourcesRootStart = "META-INF/resources/webjars/swagger-ui/";
+        URL entry = b.getEntry(resourcesRootStart + swaggerUiVersion);
+        if (entry != null) {
+            return entry.toString() + "/";
+        }
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/276017a4/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java
----------------------------------------------------------------------
diff --git a/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java
b/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java
index 8bc8afa..ebbb08f 100644
--- a/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java
+++ b/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java
@@ -82,16 +82,24 @@ public class Swagger2Feature extends AbstractSwaggerFeature {
     private String ignoreRoutes;
     
     private boolean supportSwaggerUi = true;
+    private String swaggerUiVersion;
 
     @Override
     protected void addSwaggerResource(Server server, Bus bus) {
         List<Object> swaggerResources = new LinkedList<Object>();
         ApiListingResource apiListingResource = new ApiListingResource();
         swaggerResources.add(apiListingResource);
-        if (SWAGGER_UI_RESOURCE_ROOT != null && supportSwaggerUi) {
-            swaggerResources.add(new SwaggerUIService());
-            bus.setProperty("swagger.service.ui.available", "true");
+        
+        SwaggerUIService swaggerUiService = null;
+        if (supportSwaggerUi) {
+            String swaggerUiRoot = SwaggerUiResolver.findSwaggerUiRoot(swaggerUiVersion);
+            if (swaggerUiRoot != null) {
+                swaggerUiService = new SwaggerUIService(swaggerUiRoot);
+                swaggerResources.add(swaggerUiService);
+                bus.setProperty("swagger.service.ui.available", "true");
+            }
         }
+        
         JAXRSServiceFactoryBean sfb =
                 (JAXRSServiceFactoryBean) server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName());
         sfb.setResourceClassesFromBeans(swaggerResources);
@@ -108,7 +116,7 @@ public class Swagger2Feature extends AbstractSwaggerFeature {
                 }
             }
         }
-        if (SWAGGER_UI_RESOURCE_ROOT != null && supportSwaggerUi) {
+        if (swaggerUiService != null) {
             providers.add(new SwaggerUIFilter());
         }
         providers.add(new Swagger2Serializers(dynamicBasePath, replaceTags, javadocProvider,
cris));
@@ -280,6 +288,11 @@ public class Swagger2Feature extends AbstractSwaggerFeature {
     @Path("api-docs")
     public static class SwaggerUIService {
         private static final String FAVICON = "favicon";
+        private String swaggerUiRoot;
+        public SwaggerUIService(String swaggerUiRoot) {
+            this.swaggerUiRoot = swaggerUiRoot;
+        }
+        
         @GET
         @Path("{resource:.*}")
         public Response getResource(@Context UriInfo uriInfo, @PathParam("resource") String
resourcePath) {
@@ -294,7 +307,7 @@ public class Swagger2Feature extends AbstractSwaggerFeature {
             }
             
             try {
-                URL resourceURL = URI.create(SWAGGER_UI_RESOURCE_ROOT + resourcePath).toURL();
+                URL resourceURL = URI.create(swaggerUiRoot + resourcePath).toURL();
                 return Response.ok(resourceURL.openStream()).build();
             } catch (IOException ex) {
                 throw new NotFoundException(ex);
@@ -317,4 +330,13 @@ public class Swagger2Feature extends AbstractSwaggerFeature {
             }
         }
     }
+    
+    public void setSwaggerUiVersion(String swaggerUiVersion) {
+        this.swaggerUiVersion = swaggerUiVersion;
+    }
+
+    public void setSupportSwaggerUi(boolean supportSwaggerUi) {
+        this.supportSwaggerUi = supportSwaggerUi;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/276017a4/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerUiResolver.java
----------------------------------------------------------------------
diff --git a/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerUiResolver.java
b/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerUiResolver.java
new file mode 100644
index 0000000..1df1119
--- /dev/null
+++ b/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerUiResolver.java
@@ -0,0 +1,68 @@
+/**
+ * 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.net.URL;
+import java.net.URLClassLoader;
+
+public class SwaggerUiResolver {
+    static final SwaggerUiResolver HELPER;
+    static {
+        SwaggerUiResolver theHelper = null;
+        try {
+            theHelper = new OsgiSwaggerUiResolver();
+        } catch (Throwable ex) {
+            theHelper = new SwaggerUiResolver();
+        }
+        HELPER = theHelper;
+    }
+    
+    
+    protected SwaggerUiResolver() {
+    }
+    
+    protected String findSwaggerUiRootInternal(String swaggerUiVersion) {
+        try {
+            ClassLoader cl = AbstractSwaggerFeature.class.getClassLoader();
+            if (cl instanceof URLClassLoader) {
+                final String resourcesRootStart = "META-INF/resources/webjars/swagger-ui/";
+                for (URL url : ((URLClassLoader)cl).getURLs()) {
+                    String urlStr = url.toString();
+                    if (urlStr.contains("/swagger-ui") && urlStr.toString().endsWith(".jar"))
{
+                        urlStr = urlStr.substring(0, urlStr.length() - 4);
+                        String version = urlStr.substring(urlStr.lastIndexOf("/swagger-ui")
+ 12);
+                        if (swaggerUiVersion != null && !swaggerUiVersion.equals(version))
{
+                            continue;
+                        }
+                        return "jar:" + url.toString() + "!/"
+                                 +  resourcesRootStart + version + "/";
+                    }
+                }
+                
+            }
+        } catch (Throwable ex) {
+            // ignore
+        }   
+        return null;
+    }
+
+    public static String findSwaggerUiRoot(String swaggerUiVersion) {
+        return HELPER.findSwaggerUiRootInternal(swaggerUiVersion);
+    }
+}


Mime
View raw message