cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: Prototyping some code to get SwaggerFeature run as a filter
Date Fri, 12 Dec 2014 13:22:02 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 9aace1d5e -> 3834ba2ea


Prototyping some code to get SwaggerFeature run as a filter


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

Branch: refs/heads/master
Commit: 3834ba2ea50a164cf2b5ffe15229dcf512e110c6
Parents: 9aace1d
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Fri Dec 12 13:21:46 2014 +0000
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Fri Dec 12 13:21:46 2014 +0000

----------------------------------------------------------------------
 .../cxf/jaxrs/swagger/SwaggerFeature.java       | 64 +++++++++++++++++---
 1 file changed, 56 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/3834ba2e/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerFeature.java
----------------------------------------------------------------------
diff --git a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerFeature.java
b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerFeature.java
index 3630415..f63a2a0 100644
--- a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerFeature.java
+++ b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerFeature.java
@@ -18,13 +18,27 @@
  */
 package org.apache.cxf.jaxrs.swagger;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
+import javax.ws.rs.container.ContainerRequestContext;
+import javax.ws.rs.container.ContainerRequestFilter;
+import javax.ws.rs.container.PreMatching;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+
+import com.wordnik.swagger.jaxrs.config.BeanConfig;
+import com.wordnik.swagger.jaxrs.listing.ApiDeclarationProvider;
+import com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON;
+import com.wordnik.swagger.jaxrs.listing.ResourceListingProvider;
+
 import org.apache.cxf.Bus;
 import org.apache.cxf.endpoint.Server;
 import org.apache.cxf.feature.AbstractFeature;
 import org.apache.cxf.jaxrs.JAXRSServiceFactoryBean;
+import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
 import org.apache.cxf.jaxrs.provider.ServerProviderFactory;
 
@@ -39,22 +53,29 @@ public class SwaggerFeature extends AbstractFeature {
     private String license = "Apache 2.0 License";
     private String licenseUrl = "http://www.apache.org/licenses/LICENSE-2.0.html";
     private boolean scan = true;
-    
+    private boolean runAsFilter;
     
     @Override
     public void initialize(Server server, Bus bus) {
-        List<Object> serviceBeans = new ArrayList<Object>();
-        serviceBeans.add(new com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON());
         calulateDefaultResourcePackage(server);
         calulateDefaultBasePath(server);
-        ((JAXRSServiceFactoryBean)server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName())).
-            setResourceClassesFromBeans(serviceBeans);
+        ApiListingResourceJSON apiListingResource = new ApiListingResourceJSON();
+        if (!runAsFilter) {
+            List<Object> serviceBeans = new ArrayList<Object>();
+            serviceBeans.add(apiListingResource);
+            ((JAXRSServiceFactoryBean)server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName())).
+                setResourceClassesFromBeans(serviceBeans);
+        }
         List<Object> providers = new ArrayList<Object>();
-        providers.add(new com.wordnik.swagger.jaxrs.listing.ResourceListingProvider());
-        providers.add(new com.wordnik.swagger.jaxrs.listing.ApiDeclarationProvider());
+        if (runAsFilter) {
+            providers.add(new SwaggerContainerRequestFilter(apiListingResource));
+        }
+        providers.add(new ResourceListingProvider());
+        providers.add(new ApiDeclarationProvider());
         ((ServerProviderFactory)server.getEndpoint().get(
                 ServerProviderFactory.class.getName())).setUserProviders(providers);
-        com.wordnik.swagger.jaxrs.config.BeanConfig beanConfig = new com.wordnik.swagger.jaxrs.config.BeanConfig();
+        
+        BeanConfig beanConfig = new BeanConfig();
         beanConfig.setResourcePackage(getResourcePackage());
         beanConfig.setVersion(getVersion());
         beanConfig.setBasePath(getBasePath());
@@ -138,4 +159,31 @@ public class SwaggerFeature extends AbstractFeature {
         this.scan = scan;
     }
 
+    public boolean isRunAsFilter() {
+        return runAsFilter;
+    }
+    public void setRunAsFilter(boolean runAsFilter) {
+        this.runAsFilter = runAsFilter;
+    }
+
+    @PreMatching
+    private static class SwaggerContainerRequestFilter implements ContainerRequestFilter
{
+        private ApiListingResourceJSON apiListingResource;
+        @Context
+        private MessageContext mc;
+        public SwaggerContainerRequestFilter(ApiListingResourceJSON apiListingResource) {
+            this.apiListingResource = apiListingResource;
+        }
+
+        @Override
+        public void filter(ContainerRequestContext requestContext) throws IOException {
+            UriInfo ui = mc.getUriInfo();
+            if (ui.getPath().endsWith("api-docs")) {
+                Response r = 
+                    apiListingResource.apiDeclaration("", null, mc.getServletConfig(), mc.getHttpHeaders(),
ui);
+                requestContext.abortWith(r);
+            }
+        }
+        
+    }
 }


Mime
View raw message