Just to add here , we are using netty-http camel component for this case.
Also please find below code snippet of our WebServiceRoute.java file.

@Component
public class WebServiceRoute extends RouteBuilder implements
InitializingBean {

        public static final String NETTY_HTTP_PORT = "netty.http.port";

        private static final Set<String> uniquePath = new HashSet<String>();

        @Inject
        private SpringSecurityAuthorizationPolicy
aDASpringSecurityAuthorizationPolicy;

        @Inject
        private TokenAuthenticationProcessor tokenAuthenticationProcessor;

        @Inject
        private Environment env;

        @Inject
        private Yaml yaml;

        @Inject
        private HazelcastInstance hazelcast;

        private String gatewayBasePath;

        private String modularWsUrl;

        private boolean needSecurity;

        private String currentVersion = "Unknown";

        @Override
        public void afterPropertiesSet() throws Exception {
                this.gatewayBasePath = 
this.env.getProperty(DIGITAL_WS_BASEPATH);
                this.modularWsUrl = this.env.getProperty(MODULAR_WS_URL);
                this.needSecurity = 
this.env.getProperty(DIGITAL_SECURITY_REQUIRE_TOKEN,
Boolean.class);
                this.currentVersion = this.env.getProperty(APPLICATION_VERSION);
        }

        @Override
        public void configure() throws Exception {

        
this.restConfiguration().component("netty-http").host("0.0.0.0").port(this.env.getProperty(NETTY_HTTP_PORT));

                this.registerAuthentication();
                this.registerGatewayVersion();

                for (WebServiceRouteDefinition webServiceRouteDefinition :
this.getWebServiceRouteDefinitions()) {
                        this.addRoute(webServiceRouteDefinition);
                }
        }

        private void registerGatewayVersion() {
                this.rest(this.gatewayBasePath +
"/current-gateway-version").verb("get").route().setBody(this.constant(this.currentVersion));
        }

        private void registerAuthentication() {
                List<String> wsPaths = new ArrayList<String>();
                // Enable CORS to custom web service path for verb option
                for (String wsPath : wsPaths) {
                        this.enableCORS(this.rest(wsPath));
                }
        }

        @SuppressWarnings("unchecked")
        private Collection<WebServiceRouteDefinition>
getWebServiceRouteDefinitions() throws IOException {
                // Read Web service route definitions from cache
                IMap<Object, Object> webServiceRouteDefinitions =
this.hazelcast.getMap(DIGITAL_WS_ROUTE_DEFINTIONS);

                if (CollectionUtils.isEmpty(webServiceRouteDefinitions)) {
                        // Web service route definitions are not in cache yet
                        // Read them from configuration file and add them in 
cache
                        String webServiceRouteDefinitionsFile =
this.env.getProperty(DIGITAL_WS_ROUTE_DEFINTIONS_FILE);
                        InputStream webServiceRouteDefinitionInpuStream = null;

                        webServiceRouteDefinitionInpuStream = new
FileInputStream(webServiceRouteDefinitionsFile);

                        List<WebServiceRouteDefinition> 
webServiceRouteDefinitionsList =
(List<WebServiceRouteDefinition>)
this.yaml.load(webServiceRouteDefinitionInpuStream);

                        if 
(!CollectionUtils.isEmpty(webServiceRouteDefinitionsList)) {
                                for (WebServiceRouteDefinition 
webServiceRouteDefinition :
webServiceRouteDefinitionsList) {
                                        
this.completeWebServiceRouteDefinition(webServiceRouteDefinition);
                                        
webServiceRouteDefinitions.put(webServiceRouteDefinition.getPath() +
"|" + webServiceRouteDefinition.getHttpMethod(), webServiceRouteDefinition);
                                }
                        }
                }

                return (Collection<WebServiceRouteDefinition>) (Collection<?>)
webServiceRouteDefinitions.values();
        }

        private void completeWebServiceRouteDefinition(final
WebServiceRouteDefinition webServiceRouteDefinition) {
                if (webServiceRouteDefinition.isAddBasePath()) {
                        // Complete path by Gateway base path
                        webServiceRouteDefinition.setPath(this.gatewayBasePath +
webServiceRouteDefinition.getPath());
                }

                if 
(CollectionUtils.isEmpty(webServiceRouteDefinition.getToUris())) {
                        // Modular Web Service, destination URI is Modular 
backend
                        List<String> toUris = new ArrayList<>();

                        toUris.add(this.modularWsUrl);
                        webServiceRouteDefinition.setToUris(toUris);
                }

                if (!webServiceRouteDefinition.isUrlRewrite()) {
                        
webServiceRouteDefinition.setPath(webServiceRouteDefinition.getPath() +
"?matchOnUriPrefix=" + webServiceRouteDefinition.isMatchOnUriPrefix());
                }

                String toUriConfiguration = "";
                if (webServiceRouteDefinition.isBridgeEndpoint()) {
                        toUriConfiguration += "bridgeEndpoint=true";
                }
                if (webServiceRouteDefinition.isNotThrowExceptionOnFailure()) {
                        if (!toUriConfiguration.isEmpty()) {
                                toUriConfiguration += "&";
                        }
                        toUriConfiguration += "throwExceptionOnFailure=false";
                }
                if (webServiceRouteDefinition.isUrlRewrite()) {
                        if (!toUriConfiguration.isEmpty()) {
                                toUriConfiguration += "&";
                        }
                        toUriConfiguration += "urlRewrite=#urlRewriter";
                }
                if (!toUriConfiguration.isEmpty()) {
                        final String finalToUriConfiguration = "?" + 
toUriConfiguration;
                        webServiceRouteDefinition.getToUris().replaceAll(toUri 
-> toUri +
finalToUriConfiguration);
                }
        }

        private void addRoute(final WebServiceRouteDefinition
webServiceRouteDefinition) {
                RestDefinition restDefinition =
this.rest(webServiceRouteDefinition.getPath());
                
                if 
(HttpMethod.GET.equals(webServiceRouteDefinition.getHttpMethod())) {
                        restDefinition = 
restDefinition.get(webServiceRouteDefinition.getUri());
                } else if
(HttpMethod.POST.equals(webServiceRouteDefinition.getHttpMethod())) {
                        restDefinition = 
restDefinition.post(webServiceRouteDefinition.getUri());
                } else if
(HttpMethod.DELETE.equals(webServiceRouteDefinition.getHttpMethod())) {
                        restDefinition =
restDefinition.delete(webServiceRouteDefinition.getUri());
                } else if
(HttpMethod.PUT.equals(webServiceRouteDefinition.getHttpMethod())) {
                        restDefinition = 
restDefinition.put(webServiceRouteDefinition.getUri());
                }
                
                List<String> endpoints = webServiceRouteDefinition.getToUris();
                        ProcessorDefinition<?> routeDefinition =
restDefinition.enableCORS(true).route();
        
                        if (this.needSecurity && 
webServiceRouteDefinition.isNeedSecurity()) {
                                routeDefinition =
routeDefinition.process(this.tokenAuthenticationProcessor).policy(this.aDASpringSecurityAuthorizationPolicy);
                        }
        
                        if (webServiceRouteDefinition.isMulticast()) {
                                
routeDefinition.multicast().to(endpoints.get(0), endpoints.get(1));
                        } else {
                        
routeDefinition.loadBalance().roundRobin().to(webServiceRouteDefinition.getToUris().toArray(new
String[] {}));
                        }
        
                        if (webServiceRouteDefinition.isForceJsonContentType()) 
{
                                routeDefinition.setHeader("Content-Type",
this.constant("application/json;charset=UTF-8"));
                        }
        
                        // For security issue
                        routeDefinition = 
this.applySecurityFeature(routeDefinition);
                /**
                 * we put path in Set first so we can make sure that there is 
no duplicate
CORS handler for one single URL
                 */
                String realPath = webServiceRouteDefinition.getPath();
                if (realPath.contains("?")) {
                        realPath = realPath.substring(0,
webServiceRouteDefinition.getPath().lastIndexOf("?"));
                }
                if (!uniquePath.contains(realPath)) {
                        System.out.println("WebServiceRoute.addRoute() " +
webServiceRouteDefinition.getPath() + " " +
webServiceRouteDefinition.getHttpMethod());
                        if (webServiceRouteDefinition.getUri().equals("")) {
                        // Enable CORS for OPTION requests
                        
this.enableCORS(this.rest(webServiceRouteDefinition.getPath()));
                        } else {
//Here we are setting the verb for OPTIOINS method and this part is not
working.                        
this.rest(webServiceRouteDefinition.getPath()).verb("options").route().loadBalance().roundRobin().to(webServiceRouteDefinition.getToUris().toArray(new
String[] {}));
                        }
                        uniquePath.add(realPath);
                }
        }

        private ProcessorDefinition<?> applySecurityFeature(final
ProcessorDefinition<?> def) {
                def.setHeader("X-XSS-Protection", this.constant("1;
mode=block")).setHeader("X-Content-Type-Options",
this.constant("nosniff")).setHeader("X-Frame-Options",
this.constant("SAMEORIGIN"))
                                .setHeader("Strict-Transport-Security", 
this.constant("max-age=16070400;
includeSubDomains"));

                return def;
        }

        private RestDefinition enableCORS(final RestDefinition def) {
                // TODO: This does not work for routes containing path 
parameters like for
instance: /service/{id}
                
def.verb("options").route().setHeader("Access-Control-Allow-Origin",
this.constant("*")).setHeader("Access-Control-Allow-Methods",
this.constant("DELETE, GET, OPTIONS,POST"))
                                .setHeader("Access-Control-Allow-Headers",
                                                this.constant(
                                                                "Origin, 
Accept, X-Requested-With, Content-Type,
Access-Control-Allow-Origin, Access-Control-Allow-Methods,
Access-Control-Allow-Headers, Access-Control-Expose-Headers,
Access-Control-Request-Method, Access-Control-Request-Headers,
authenticationToken, tenant"))
                                .setHeader("Allow", this.constant("DELETE, GET, 
OPTIONS, POST"));
                return def;
        }
}

It would be great if anyone can give me some clue regarding this problem.



--
View this message in context: 
http://camel.465427.n5.nabble.com/CORS-configuration-not-working-for-web-services-with-path-parameter-tp5782441p5782444.html
Sent from the Camel Development mailing list archive at Nabble.com.

Reply via email to