[ 
https://issues.apache.org/jira/browse/CAMEL-12855?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16640238#comment-16640238
 ] 

ASF GitHub Bot commented on CAMEL-12855:
----------------------------------------

thiagolocatelli commented on a change in pull request #2554: CAMEL-12855 - 
camel-swagger-java not honoring the x-forwarded-[host,proto,prefix] headers.
URL: https://github.com/apache/camel/pull/2554#discussion_r223113753
 
 

 ##########
 File path: 
components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerSupport.java
 ##########
 @@ -345,4 +353,36 @@ private static void 
setupCorsHeaders(RestApiResponseAdapter response, Map<String
         response.setHeader("Access-Control-Max-Age", maxAge);
     }
 
+    private void setupXForwardedHeaders(Swagger swagger, Map<String, Object> 
headers) {
+
+        String host = (String) headers.get(HEADER_HOST);
+        if(host != null && host.length() > 0) {
 
 Review comment:
   it should work, the prefix could also come with "/" at the end, I will apply 
this suggestion.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> camel-swagger-java not honoring the x-forwarded-[host,proto,prefix] headers.
> ----------------------------------------------------------------------------
>
>                 Key: CAMEL-12855
>                 URL: https://issues.apache.org/jira/browse/CAMEL-12855
>             Project: Camel
>          Issue Type: New Feature
>          Components: camel-swagger
>    Affects Versions: 2.22.1
>            Reporter: Thiago Locatelli
>            Assignee: Zoran Regvart
>            Priority: Major
>
> We have a camel spring boot application sitting behind an api gateway with a 
> Swagger page for accessing the documentation for all of our services (created 
> with Spring Boot and Zuul Proxy, Consul as service discovery). The Swagger 
> gateway sends three headers to the camel application to retrieve the swagger 
> docs, but camel-swagger-java is not honoring those headers, thus the 
> attributes host, basePath and schemes remain the same as if the swagger 
> documentation was being acessed directly.
> Example, when trying to get the swagger docs for our customer-service camel 
> application:
> [https://api-gateway.dev-internal.io/api-gateway/customer-service/v2/api-docs]
>  
> {code:java}
> x-forwarded-prefix: /api-gateway/
> x-forwarded-host: api-gateway.dev-internal.io
> x-forwarded-proto: https,http{code}
>  
> The expected results should be
> {code:java}
> {
>     "swagger": "2.0",
>     ...
>     "host": "api-gateway.dev-internal.io",
>     "basePath": "/api-gateway/customer-service",
>     "schemes": [ "https", "http" ]
>     ...
> }{code}
> Current results:
> {code:java}
> {
>     "swagger": "2.0",
>     ...
>     "host": "",
>     "basePath": "/customer-service",
>     "schemes": [ "http" ]
>     ...
> }{code}
>  
> we had a simple solution, which might not be the correct one. We changed the 
> RestSwaggerSupport.java:
>  
> {code:java}
> public class RestSwaggerSupport {
>     private static final String X_FORWARDED_PREFIX = "X-Forwarded-Prefix";    
>  
>     private static final String X_FORWARDED_HOST = "X-Forwarded-Host";
>     public void renderResourceListing(RestApiResponseAdapter response, 
> BeanConfig swaggerConfig, String contextId, String route, boolean json, 
> boolean yaml, Exchange exchange, ClassResolver classResolver, 
> RestConfiguration configuration, String prefix) throws Exception {
>     ...
>         Swagger swagger = reader.read(rests, route, swaggerConfig, contextId, 
> exchange == null ? classResolver : exchange.getContext().getClassResolver()); 
>   
>         setupXForwardedHeaders(swagger, exchange);
>     ...
>     }
>     private void setupXForwardedHeaders(Swagger swagger, Exchange exchange) {
>         if (exchange.getIn().getHeaders().containsKey(X_FORWARDED_PREFIX)) {
>             String prefix = exchange.getIn().getHeader(X_FORWARDED_PREFIX, 
> String.class);
>             prefix = prefix.replace("/", "");
>             if (prefix != null) {
>                 String path = swagger.getBasePath();
>                 path = "/" + prefix + path;
>                 swagger.setBasePath(path);
>             }
>         }
>         if(exchange.getIn().getHeaders().containsKey(X_FORWARDED_HOST)) {
>             String host = exchange.getIn().getHeader(X_FORWARDED_HOST, 
> String.class);
>             if(host != null && host.length() > 0) {
>                 swagger.setHost(host);
>             }
>         }
>     ...
> }
> {code}
>  
> If this is a valid approach, I will submit a PR. 
>  
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to