cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ilgro...@apache.org
Subject cxf git commit: Several Swagger-related improvements: swagger-jaxrs upgraded to 1.5.8, now working properly even as filter, prepared Swagger2Feature for extension
Date Wed, 16 Mar 2016 15:40:41 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.1.x-fixes d7e2821f8 -> 7a35d3314


Several Swagger-related improvements: swagger-jaxrs upgraded to 1.5.8, now working properly
even as filter, prepared Swagger2Feature for extension


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

Branch: refs/heads/3.1.x-fixes
Commit: 7a35d33145a7d1f2d00d01df130f836127d65f62
Parents: d7e2821
Author: Francesco Chicchiriccò <ilgrosso@apache.org>
Authored: Wed Mar 16 16:09:17 2016 +0100
Committer: Francesco Chicchiriccò <ilgrosso@apache.org>
Committed: Wed Mar 16 16:40:33 2016 +0100

----------------------------------------------------------------------
 parent/pom.xml                                  |  2 +-
 .../cxf/jaxrs/swagger/Swagger2Feature.java      | 82 ++++++++++++--------
 .../cxf/jaxrs/swagger/Swagger2Serializers.java  | 10 +--
 3 files changed, 55 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/7a35d331/parent/pom.xml
----------------------------------------------------------------------
diff --git a/parent/pom.xml b/parent/pom.xml
index c50a51f..ccf6de9 100644
--- a/parent/pom.xml
+++ b/parent/pom.xml
@@ -146,7 +146,7 @@
         <cxf.spring.ldap.version>1.3.1.RELEASE</cxf.spring.ldap.version>
         <cxf.spring.mock>spring-test</cxf.spring.mock>
         <cxf.swagger.version>1.3.13</cxf.swagger.version>
-        <cxf.swagger2.version>1.5.4</cxf.swagger2.version>
+        <cxf.swagger2.version>1.5.8</cxf.swagger2.version>
         <cxf.velocity.version>1.7</cxf.velocity.version>
         <cxf.woodstox.core.version>4.4.1</cxf.woodstox.core.version>
         <cxf.woodstox.stax2-api.version>3.1.4</cxf.woodstox.stax2-api.version>

http://git-wip-us.apache.org/repos/asf/cxf/blob/7a35d331/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java
----------------------------------------------------------------------
diff --git a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java
b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java
index 5072104..bc9102f 100644
--- a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java
+++ b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java
@@ -32,6 +32,7 @@ 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.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 
@@ -46,6 +47,7 @@ import org.apache.cxf.jaxrs.model.doc.DocumentationProvider;
 import org.apache.cxf.jaxrs.model.doc.JavaDocProvider;
 import org.apache.cxf.jaxrs.provider.ServerProviderFactory;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
+import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 
 import io.swagger.jaxrs.config.BeanConfig;
 import io.swagger.jaxrs.config.DefaultReaderConfig;
@@ -55,6 +57,12 @@ import io.swagger.jaxrs.listing.ApiListingResource;
 @Provider(Type.Feature)
 public class Swagger2Feature extends AbstractSwaggerFeature {
 
+    protected boolean dynamicBasePath;
+
+    protected boolean replaceTags;
+
+    protected DocumentationProvider javadocProvider;
+
     private String host;
 
     private String[] schemes;
@@ -65,33 +73,24 @@ public class Swagger2Feature extends AbstractSwaggerFeature {
 
     private String ignoreRoutes;
 
-    private boolean dynamicBasePath;
-
-    private boolean replaceTags;
-
-    private DocumentationProvider javadocProvider;
-
     @Override
     protected void addSwaggerResource(Server server) {
-        List<ClassResourceInfo> cris = Collections.emptyList();
-        if (!runAsFilter) {
-            List<Object> serviceBeans = new ArrayList<>();
-            ApiListingResource apiListingResource = new ApiListingResource();
-            serviceBeans.add(apiListingResource);
-            JAXRSServiceFactoryBean sfb =
-                    (JAXRSServiceFactoryBean) server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName());
-            sfb.setResourceClassesFromBeans(serviceBeans);
-            cris = sfb.getClassResourceInfo();
+        ApiListingResource apiListingResource = new ApiListingResource();
+        JAXRSServiceFactoryBean sfb =
+                (JAXRSServiceFactoryBean) server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName());
+        sfb.setResourceClassesFromBeans(Collections.<Object>singletonList(apiListingResource));
+        List<ClassResourceInfo> cris = sfb.getClassResourceInfo();
+
+        List<Object> providers = new ArrayList<>();
+        if (runAsFilter) {
+            providers.add(new SwaggerContainerRequestFilter());
+        } else {
             for (ClassResourceInfo cri : cris) {
                 if (ApiListingResource.class == cri.getResourceClass()) {
-                    InjectionUtils.injectContextProxiesAndApplication(cri, apiListingResource,
null);
+                    InjectionUtils.injectContextProxies(cri, apiListingResource);
                 }
             }
         }
-        List<Object> providers = new ArrayList<>();
-        if (runAsFilter) {
-            providers.add(new SwaggerContainerRequestFilter());
-        }
         providers.add(new Swagger2Serializers(dynamicBasePath, replaceTags, javadocProvider,
cris));
         providers.add(new ReaderConfigFilter());
         ((ServerProviderFactory) server.getEndpoint().get(
@@ -187,32 +186,49 @@ public class Swagger2Feature extends AbstractSwaggerFeature {
     }
 
     @PreMatching
-    private static class SwaggerContainerRequestFilter extends ApiListingResource implements
ContainerRequestFilter {
+    protected static class SwaggerContainerRequestFilter extends ApiListingResource implements
ContainerRequestFilter {
+
+        protected static final MediaType APPLICATION_YAML_TYPE = JAXRSUtils.toMediaType("application/yaml");
+
+        protected static final String APIDOCS_LISTING_PATH = "swagger";
 
-        private static final String APIDOCS_LISTING_PATH_JSON = "swagger.json";
+        protected static final String APIDOCS_LISTING_PATH_JSON = APIDOCS_LISTING_PATH +
".json";
 
-        private static final String APIDOCS_LISTING_PATH_YAML = "swagger.yaml";
+        protected static final String APIDOCS_LISTING_PATH_YAML = APIDOCS_LISTING_PATH +
".yaml";
 
         @Context
-        private MessageContext mc;
+        protected MessageContext mc;
 
         @Override
         public void filter(ContainerRequestContext requestContext) throws IOException {
             UriInfo ui = mc.getUriInfo();
-            if (ui.getPath().endsWith(APIDOCS_LISTING_PATH_JSON)) {
-                Response r = getListingJson(null, mc.getServletConfig(), mc.getHttpHeaders(),
ui);
-                requestContext.abortWith(r);
-            } else if (ui.getPath().endsWith(APIDOCS_LISTING_PATH_YAML)) {
-                Response r = getListingYaml(null, mc.getServletConfig(), mc.getHttpHeaders(),
ui);
-                requestContext.abortWith(r);
+            List<MediaType> mediaTypes = mc.getHttpHeaders().getAcceptableMediaTypes();
+
+            Response response = null;
+            if ((ui.getPath().endsWith(APIDOCS_LISTING_PATH)
+                    && !JAXRSUtils.intersectMimeTypes(mediaTypes, MediaType.APPLICATION_JSON_TYPE).isEmpty())
+                    || ui.getPath().endsWith(APIDOCS_LISTING_PATH_JSON)) {
+
+                response = getListingJsonResponse(
+                        null, mc.getServletContext(), mc.getServletConfig(), mc.getHttpHeaders(),
ui);
+            } else if ((ui.getPath().endsWith(APIDOCS_LISTING_PATH)
+                    && !JAXRSUtils.intersectMimeTypes(mediaTypes, APPLICATION_YAML_TYPE).isEmpty())
+                    || ui.getPath().endsWith(APIDOCS_LISTING_PATH_YAML)) {
+
+                response = getListingYamlResponse(
+                        null, mc.getServletContext(), mc.getServletConfig(), mc.getHttpHeaders(),
ui);
+            }
+
+            if (response != null) {
+                requestContext.abortWith(response);
             }
         }
     }
 
-    private class ReaderConfigFilter implements ContainerRequestFilter {
+    protected class ReaderConfigFilter implements ContainerRequestFilter {
 
         @Context
-        private MessageContext mc;
+        protected MessageContext mc;
 
         @Override
         public void filter(ContainerRequestContext requestContext) throws IOException {
@@ -227,7 +243,7 @@ public class Swagger2Feature extends AbstractSwaggerFeature {
             }
         }
 
-        private void addReaderConfig(String ignoreRoutesParam) {
+        protected void addReaderConfig(String ignoreRoutesParam) {
             DefaultReaderConfig rc = new DefaultReaderConfig();
             rc.setScanAllResources(true);
             if (ignoreRoutesParam != null) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/7a35d331/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Serializers.java
----------------------------------------------------------------------
diff --git a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Serializers.java
b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Serializers.java
index 187ea96..e6f7be2 100644
--- a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Serializers.java
+++ b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Serializers.java
@@ -48,13 +48,13 @@ import io.swagger.models.Tag;
 
 public class Swagger2Serializers extends SwaggerSerializers {
 
-    private final boolean dynamicBasePath;
+    protected final boolean dynamicBasePath;
 
-    private final boolean replaceTags;
+    protected final boolean replaceTags;
 
-    private final DocumentationProvider javadocProvider;
+    protected final DocumentationProvider javadocProvider;
 
-    private final List<ClassResourceInfo> cris;
+    protected final List<ClassResourceInfo> cris;
 
     public Swagger2Serializers(
             final boolean dynamicBasePath,
@@ -145,7 +145,7 @@ public class Swagger2Serializers extends SwaggerSerializers {
         super.writeTo(data, type, genericType, annotations, mediaType, headers, out);
     }
 
-    private String getNormalizedPath(String classResourcePath, String operationResourcePath)
{
+    protected String getNormalizedPath(String classResourcePath, String operationResourcePath)
{
         StringBuilder normalizedPath = new StringBuilder();
 
         String[] segments = StringUtils.split(classResourcePath + operationResourcePath,
"/");


Mime
View raw message