camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zregv...@apache.org
Subject [camel] branch master updated: CAMEL-12855 - camel-swagger-java not honoring the x-forwarded-[host, proto, prefix] headers.
Date Fri, 05 Oct 2018 19:34:49 GMT
This is an automated email from the ASF dual-hosted git repository.

zregvart pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 081474d  CAMEL-12855 - camel-swagger-java not honoring the x-forwarded-[host,proto,prefix]
headers.
081474d is described below

commit 081474d7256cbbd9f041867e38115a85ab8daf0f
Author: Thiago Locatelli <thiago.locatellidasilva@transamerica.com>
AuthorDate: Fri Oct 5 08:56:09 2018 -0400

    CAMEL-12855 - camel-swagger-java not honoring the x-forwarded-[host,proto,prefix] headers.
---
 .../apache/camel/swagger/RestSwaggerProcessor.java |  2 +-
 .../apache/camel/swagger/RestSwaggerSupport.java   | 46 +++++++++++++++++++++-
 .../camel/swagger/servlet/RestSwaggerServlet.java  | 14 ++++++-
 3 files changed, 59 insertions(+), 3 deletions(-)

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 5c284d6..91b4146 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
@@ -116,7 +116,7 @@ public class RestSwaggerProcessor implements Processor {
                 if (!match) {
                     adapter.noContent();
                 } else {
-                    support.renderResourceListing(adapter, swaggerConfig, name, route, json,
yaml, exchange.getContext().getClassResolver(), configuration);
+                    support.renderResourceListing(adapter, swaggerConfig, name, route, json,
yaml, exchange.getIn().getHeaders(), exchange.getContext().getClassResolver(), configuration);
                 }
             }
         } catch (Exception e) {
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 c2e7890..7a90ab3 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
@@ -35,6 +35,7 @@ import io.swagger.jaxrs.config.BeanConfig;
 import io.swagger.models.Contact;
 import io.swagger.models.Info;
 import io.swagger.models.License;
+import io.swagger.models.Scheme;
 import io.swagger.models.Swagger;
 import io.swagger.util.Yaml;
 import org.apache.camel.Exchange;
@@ -45,6 +46,8 @@ import org.apache.camel.spi.ClassResolver;
 import org.apache.camel.spi.RestConfiguration;
 import org.apache.camel.util.CamelVersionHelper;
 import org.apache.camel.util.EndpointHelper;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.URISupport;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -56,6 +59,11 @@ import static org.apache.camel.swagger.SwaggerHelper.clearVendorExtensions;
  * such as servlet/jetty/netty4-http to offer Swagger API listings with minimal effort.
  */
 public class RestSwaggerSupport {
+	
+    private static final String HEADER_X_FORWARDED_PREFIX = "X-Forwarded-Prefix";
+    private static final String HEADER_X_FORWARDED_HOST = "X-Forwarded-Host";
+    private static final String HEADER_X_FORWARDED_PROTO = "X-Forwarded-Proto";
+    private static final String HEADER_HOST = "Host";
 
     private static final Logger LOG = LoggerFactory.getLogger(RestSwaggerSupport.class);
     private RestSwaggerReader reader = new RestSwaggerReader();
@@ -194,7 +202,7 @@ public class RestSwaggerSupport {
     }
 
     public void renderResourceListing(RestApiResponseAdapter response, BeanConfig swaggerConfig,
String contextId, String route, boolean json, boolean yaml,
-                                      ClassResolver classResolver, RestConfiguration configuration)
throws Exception {
+            Map<String, Object> headers, ClassResolver classResolver, RestConfiguration
configuration) throws Exception {
         LOG.trace("renderResourceListing");
 
         if (cors) {
@@ -209,6 +217,7 @@ public class RestSwaggerSupport {
 
                 // read the rest-dsl into swagger model
                 Swagger swagger = reader.read(rests, route, swaggerConfig, contextId, classResolver);
+                setupXForwardedHeaders(swagger, headers);
 
                 if (!configuration.isApiVendorExtension()) {
                     clearVendorExtensions(swagger);
@@ -228,6 +237,7 @@ public class RestSwaggerSupport {
 
                 // read the rest-dsl into swagger model
                 Swagger swagger = reader.read(rests, route, swaggerConfig, contextId, classResolver);
+                setupXForwardedHeaders(swagger, headers);
 
                 if (!configuration.isApiVendorExtension()) {
                     clearVendorExtensions(swagger);
@@ -345,4 +355,38 @@ public class RestSwaggerSupport {
         response.setHeader("Access-Control-Max-Age", maxAge);
     }
 
+    private void setupXForwardedHeaders(Swagger swagger, Map<String, Object> headers)
{
+
+        String host = (String) headers.get(HEADER_HOST);
+        if(ObjectHelper.isNotEmpty(host)) {
+            swagger.setHost(host);
+        }
+
+        String forwardedPrefix = (String) headers.get(HEADER_X_FORWARDED_PREFIX);
+        if (ObjectHelper.isNotEmpty(forwardedPrefix)) {
+            String prefixedBasePath = "/" + URISupport.stripPrefix(forwardedPrefix, "/")
+
+                    (!forwardedPrefix.endsWith("/") ? "/" : "") + URISupport.stripPrefix(swagger.getBasePath(),
"/");
+            swagger.setBasePath(prefixedBasePath);
+        }
+
+        String forwardedHost = (String) headers.get(HEADER_X_FORWARDED_HOST);
+        if(ObjectHelper.isNotEmpty(forwardedHost)) {
+            swagger.setHost(forwardedHost);
+        }
+
+        String proto = (String) headers.get(HEADER_X_FORWARDED_PROTO);
+        if(ObjectHelper.isNotEmpty(proto)) {
+            String[] schemes = proto.split(",");
+            List<Scheme> schs = new ArrayList<>();
+            for(String scheme : schemes) {
+                String trimmedScheme = scheme.trim();
+                schs.add(Scheme.forValue(trimmedScheme));
+            }
+            swagger.setSchemes(schs);
+        }
+        else {
+            swagger.setSchemes(null);
+        }
+    }
+
 }
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 c784a8e..cf56047 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
@@ -209,7 +209,7 @@ public class RestSwaggerServlet extends HttpServlet {
                 if (!match) {
                     adapter.noContent();
                 } else {
-                    support.renderResourceListing(adapter, swaggerConfig, name, route, json,
yaml, classResolver, new RestConfiguration());
+                    support.renderResourceListing(adapter, swaggerConfig, name, route, json,
yaml, getHeaders(request), classResolver, new RestConfiguration());
                 }
             }
         } catch (Exception e) {
@@ -260,4 +260,16 @@ public class RestSwaggerServlet extends HttpServlet {
         return path;
     }
 
+    private Map<String, Object> getHeaders(HttpServletRequest request) {
+        Map<String, Object> headers = new HashMap<>();
+        @SuppressWarnings("unchecked")
+        Enumeration<String> headerNames = request.getHeaderNames();
+        if (headerNames != null) {
+            while (headerNames.hasMoreElements()) {
+                headers.put(headerNames.nextElement(), request.getHeader(headerNames.nextElement()));
+            }
+        }
+        return headers;
+    }
+
 }
\ No newline at end of file


Mime
View raw message