camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [2/2] camel git commit: CAMEL-9761: camel-swagger-java - Allow to use custom CORS headers for api-docs
Date Sat, 26 Mar 2016 06:41:27 GMT
CAMEL-9761: camel-swagger-java - Allow to use custom CORS headers for api-docs


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/10bff735
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/10bff735
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/10bff735

Branch: refs/heads/camel-2.17.x
Commit: 10bff7354321fe28dc6b3f7786c44af9cade06c6
Parents: 4135ac0
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Sat Mar 26 07:39:03 2016 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sat Mar 26 07:39:42 2016 +0100

----------------------------------------------------------------------
 .../camel/swagger/RestSwaggerProcessor.java     |  9 ++-
 .../camel/swagger/RestSwaggerSupport.java       | 48 +++++++++++---
 .../swagger/SwaggerRestApiProcessorFactory.java |  8 ++-
 .../swagger/servlet/RestSwaggerCorsFilter.java  | 68 ++++++++++++++++++--
 .../swagger/servlet/RestSwaggerServlet.java     |  9 ++-
 5 files changed, 121 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/10bff735/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerProcessor.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerProcessor.java
b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerProcessor.java
index 51ffd12..9edf41e 100644
--- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerProcessor.java
+++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerProcessor.java
@@ -22,6 +22,7 @@ import java.util.Map;
 import io.swagger.jaxrs.config.BeanConfig;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
+import org.apache.camel.spi.RestConfiguration;
 import org.apache.camel.util.EndpointHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
@@ -34,11 +35,13 @@ public class RestSwaggerProcessor implements Processor {
     private final RestSwaggerSupport support;
     private final String contextIdPattern;
     private final boolean contextIdListing;
+    private final RestConfiguration configuration;
 
     @SuppressWarnings("unchecked")
-    public RestSwaggerProcessor(String contextIdPattern, boolean contextIdListing, Map<String,
Object> parameters) {
+    public RestSwaggerProcessor(String contextIdPattern, boolean contextIdListing, Map<String,
Object> parameters, RestConfiguration configuration) {
         this.contextIdPattern = contextIdPattern;
         this.contextIdListing = contextIdListing;
+        this.configuration = configuration;
         this.support = new RestSwaggerSupport();
         this.swaggerConfig = new BeanConfig();
 
@@ -79,7 +82,7 @@ public class RestSwaggerProcessor implements Processor {
         try {
             // render list of camel contexts as root
             if (contextIdListing && (ObjectHelper.isEmpty(route) || route.equals("/")))
{
-                support.renderCamelContexts(adapter, contextId, contextIdPattern, json, yaml);
+                support.renderCamelContexts(adapter, contextId, contextIdPattern, json, yaml,
configuration);
             } else {
                 String name;
                 if (ObjectHelper.isNotEmpty(route)) {
@@ -112,7 +115,7 @@ public class RestSwaggerProcessor implements Processor {
                 if (!match) {
                     adapter.noContent();
                 } else {
-                    support.renderResourceListing(adapter, swaggerConfig, name, route, json,
yaml, exchange.getContext().getClassResolver());
+                    support.renderResourceListing(adapter, swaggerConfig, name, route, json,
yaml, exchange.getContext().getClassResolver(), configuration);
                 }
             }
         } catch (Exception e) {

http://git-wip-us.apache.org/repos/asf/camel/blob/10bff735/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerSupport.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerSupport.java
b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerSupport.java
index a275a0e..982d8f3 100644
--- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerSupport.java
+++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerSupport.java
@@ -191,14 +191,11 @@ public class RestSwaggerSupport {
     }
 
     public void renderResourceListing(RestApiResponseAdapter response, BeanConfig swaggerConfig,
String contextId, String route, boolean json, boolean yaml,
-                                      ClassResolver classResolver) throws Exception {
+                                      ClassResolver classResolver, RestConfiguration configuration)
throws Exception {
         LOG.trace("renderResourceListing");
 
         if (cors) {
-            response.setHeader("Access-Control-Allow-Origin", RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_ORIGIN);
-            response.setHeader("Access-Control-Allow-Methods", RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS);
-            response.setHeader("Access-Control-Allow-Headers", RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS);
-            response.setHeader("Access-Control-Max-Age", RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE);
+            setupCorsHeaders(response, configuration.getCorsHeaders());
         }
 
         List<RestDefinition> rests = getRestDefinitions(contextId);
@@ -246,14 +243,12 @@ public class RestSwaggerSupport {
     /**
      * Renders a list of available CamelContexts in the JVM
      */
-    public void renderCamelContexts(RestApiResponseAdapter response, String contextId, String
contextIdPattern, boolean json, boolean yaml) throws Exception {
+    public void renderCamelContexts(RestApiResponseAdapter response, String contextId, String
contextIdPattern, boolean json, boolean yaml,
+                                    RestConfiguration configuration) throws Exception {
         LOG.trace("renderCamelContexts");
 
         if (cors) {
-            response.setHeader("Access-Control-Allow-Origin", RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_ORIGIN);
-            response.setHeader("Access-Control-Allow-Methods", RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS);
-            response.setHeader("Access-Control-Allow-Headers", RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS);
-            response.setHeader("Access-Control-Max-Age", RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE);
+            setupCorsHeaders(response, configuration.getCorsHeaders());
         }
 
         List<String> contexts = findCamelContexts();
@@ -305,4 +300,37 @@ public class RestSwaggerSupport {
         response.writeBytes(sb.toString().getBytes());
     }
 
+    private static void setupCorsHeaders(RestApiResponseAdapter response, Map<String,
String> corsHeaders) {
+        // use default value if none has been configured
+        String allowOrigin = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Origin")
: null;
+        if (allowOrigin == null) {
+            allowOrigin = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_ORIGIN;
+        }
+        String allowMethods = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Methods")
: null;
+        if (allowMethods == null) {
+            allowMethods = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS;
+        }
+        String allowHeaders = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Headers")
: null;
+        if (allowHeaders == null) {
+            allowHeaders = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS;
+        }
+        String maxAge = corsHeaders != null ? corsHeaders.get("Access-Control-Max-Age") :
null;
+        if (maxAge == null) {
+            maxAge = RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE;
+        }
+
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Using CORS headers[");
+            LOG.trace("  Access-Control-Allow-Origin={}", allowOrigin);
+            LOG.trace("  Access-Control-Allow-Methods={}", allowMethods);
+            LOG.trace("  Access-Control-Allow-Headers={}", allowHeaders);
+            LOG.trace("  Access-Control-Max-Age={}", maxAge);
+            LOG.trace("]");
+        }
+        response.setHeader("Access-Control-Allow-Origin", allowOrigin);
+        response.setHeader("Access-Control-Allow-Methods", allowMethods);
+        response.setHeader("Access-Control-Allow-Headers", allowHeaders);
+        response.setHeader("Access-Control-Max-Age", maxAge);
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/10bff735/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java
b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java
index 7b0f5df..c815001 100644
--- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java
+++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/SwaggerRestApiProcessorFactory.java
@@ -54,6 +54,12 @@ public class SwaggerRestApiProcessorFactory implements RestApiProcessorFactory
{
             options.put("base.path", path);
         }
 
-        return new RestSwaggerProcessor(contextIdPattern, contextIdListing, options);
+        // is cors enabled?
+        Object cors = options.get("cors");
+        if (cors == null && configuration.isEnableCORS()) {
+            options.put("cors", "true");
+        }
+
+        return new RestSwaggerProcessor(contextIdPattern, contextIdListing, options, configuration);
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/10bff735/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/servlet/RestSwaggerCorsFilter.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/servlet/RestSwaggerCorsFilter.java
b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/servlet/RestSwaggerCorsFilter.java
index 3570fe6..d29d76a 100644
--- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/servlet/RestSwaggerCorsFilter.java
+++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/servlet/RestSwaggerCorsFilter.java
@@ -17,6 +17,8 @@
 package org.apache.camel.swagger.servlet;
 
 import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
 import javax.servlet.Filter;
 import javax.servlet.FilterChain;
 import javax.servlet.FilterConfig;
@@ -30,12 +32,45 @@ import org.apache.camel.spi.RestConfiguration;
 /**
  * A simple CORS filter that can used to allow the swagger ui or other API browsers from
remote origins to access the
  * Rest services exposes by this Camel swagger component.
+ * <p/>
+ * You can configure CORS headers in the init parameters to the Servlet Filter using the
names:
+ * <ul>
+ *     <li>Access-Control-Allow-Origin</li>
+ *     <li>Access-Control-Allow-Methods</li>
+ *     <li>Access-Control-Allow-Headers</li>
+ *     <li>Access-Control-Max-Age</li>
+ * </ul>
+ * If a parameter is not configured then the default value is used.
+ * The default values are defined as:
+ * <ul>
+ *     <li>{@link RestConfiguration#CORS_ACCESS_CONTROL_ALLOW_ORIGIN}</li>
+ *     <li>{@link RestConfiguration#CORS_ACCESS_CONTROL_ALLOW_METHODS}</li>
+ *     <li>{@link RestConfiguration#CORS_ACCESS_CONTROL_ALLOW_HEADERS}</li>
+ *     <li>{@link RestConfiguration#CORS_ACCESS_CONTROL_MAX_AGE}</li>
+ * </ul>
  */
 public class RestSwaggerCorsFilter implements Filter {
 
+    private final Map<String, String> corsHeaders = new HashMap<String, String>();
+
     @Override
     public void init(FilterConfig filterConfig) throws ServletException {
-        // noop
+        String s = filterConfig.getInitParameter("Access-Control-Allow-Origin");
+        if (s != null) {
+            corsHeaders.put("Access-Control-Allow-Origin", s);
+        }
+        s = filterConfig.getInitParameter("Access-Control-Allow-Methods");
+        if (s != null) {
+            corsHeaders.put("Access-Control-Allow-Methods", s);
+        }
+        s = filterConfig.getInitParameter("Access-Control-Allow-Headers");
+        if (s != null) {
+            corsHeaders.put("Access-Control-Allow-Headers", s);
+        }
+        s = filterConfig.getInitParameter("Access-Control-Max-Age");
+        if (s != null) {
+            corsHeaders.put("Access-Control-Max-Age", s);
+        }
     }
 
     @Override
@@ -47,11 +82,34 @@ public class RestSwaggerCorsFilter implements Filter {
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
         HttpServletResponse res = (HttpServletResponse) response;
 
-        res.setHeader("Access-Control-Allow-Origin", RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_ORIGIN);
-        res.setHeader("Access-Control-Allow-Methods", RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS);
-        res.setHeader("Access-Control-Allow-Headers", RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS);
-        res.setHeader("Access-Control-Max-Age", RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE);
+        setupCorsHeaders(res, corsHeaders);
 
         chain.doFilter(request, response);
     }
+
+    private static void setupCorsHeaders(HttpServletResponse response, Map<String, String>
corsHeaders) {
+        // use default value if none has been configured
+        String allowOrigin = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Origin")
: null;
+        if (allowOrigin == null) {
+            allowOrigin = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_ORIGIN;
+        }
+        String allowMethods = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Methods")
: null;
+        if (allowMethods == null) {
+            allowMethods = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS;
+        }
+        String allowHeaders = corsHeaders != null ? corsHeaders.get("Access-Control-Allow-Headers")
: null;
+        if (allowHeaders == null) {
+            allowHeaders = RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS;
+        }
+        String maxAge = corsHeaders != null ? corsHeaders.get("Access-Control-Max-Age") :
null;
+        if (maxAge == null) {
+            maxAge = RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE;
+        }
+
+        response.setHeader("Access-Control-Allow-Origin", allowOrigin);
+        response.setHeader("Access-Control-Allow-Methods", allowMethods);
+        response.setHeader("Access-Control-Allow-Headers", allowHeaders);
+        response.setHeader("Access-Control-Max-Age", maxAge);
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/10bff735/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/servlet/RestSwaggerServlet.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/servlet/RestSwaggerServlet.java
b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/servlet/RestSwaggerServlet.java
index cfa8539..3aa7a75 100644
--- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/servlet/RestSwaggerServlet.java
+++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/servlet/RestSwaggerServlet.java
@@ -95,6 +95,11 @@ public class RestSwaggerServlet extends HttpServlet {
             Object value = config.getInitParameter(name);
             parameters.put(name, value);
         }
+        // when using servlet then use the cors filter to enable cors
+        if (parameters.get("cors") != null) {
+            LOG.warn("Use RestSwaggerCorsFilter when uisng this Servlet to enable CORS");
+            parameters.remove("cors");
+        }
         support.initSwagger(swaggerConfig, parameters);
 
         // allow to configure these options from the servlet config as well
@@ -143,7 +148,7 @@ public class RestSwaggerServlet extends HttpServlet {
         try {
             // render list of camel contexts as root
             if (apiContextIdListing && (ObjectHelper.isEmpty(route) || route.equals("/")))
{
-                support.renderCamelContexts(adapter, contextId, apiContextIdPattern, json,
yaml);
+                support.renderCamelContexts(adapter, contextId, apiContextIdPattern, json,
yaml, null);
             } else {
                 String name = null;
                 if (ObjectHelper.isNotEmpty(route)) {
@@ -183,7 +188,7 @@ public class RestSwaggerServlet extends HttpServlet {
                 if (!match) {
                     adapter.noContent();
                 } else {
-                    support.renderResourceListing(adapter, swaggerConfig, name, route, json,
yaml, classResolver);
+                    support.renderResourceListing(adapter, swaggerConfig, name, route, json,
yaml, classResolver, null);
                 }
             }
         } catch (Exception e) {


Mime
View raw message