@Test public void testCallMethodIsOptionWithCors() { this.request.setRequestURI("/v2/api/foo"); this.request.setMethod(HttpMethod.OPTIONS.name()); Map<String, ZuulRoute> routes = new LinkedHashMap<>(); ZuulRoute route = new ZuulRoute("idFoo", "/v2/api/foo", null, "my.dns.com.br", true, null, Collections.newSetFromMap(new ConcurrentHashMap<>())); routes.put("/v2/api/foo", route); Credential opPost = new Credential(HttpMethod.POST.name(), "/api/foo", "/v2", "apiName", 11L, 88L, 10L, true); Credential opDelete = new Credential(HttpMethod.DELETE.name(), "/api/foo", "/v2", "apiName", 12L, 88L, 10L, true); Credential opGet = new Credential(HttpMethod.GET.name(), "/api/foo", "/v2", "apiName", 13L, 88L, 10L, true); Mockito.when(routeLocator.getAtomicRoutes()).thenReturn(new AtomicReference<>(routes)); Mockito.when(credentialRepository.findByPattern("/v2/api/foo")).thenReturn(Lists.newArrayList(opPost, opDelete, opGet)); HeimdallRoute heimdallRoute = this.filter.getMatchingHeimdallRoute("/v2/api/foo", HttpMethod.OPTIONS.name(), this.ctx); assertNotNull(heimdallRoute); assertEquals("/api/foo", heimdallRoute.getRoute().getPath()); }
protected HeimdallRoute getMatchingHeimdallRoute(String requestURI, String method, RequestContext ctx) { boolean auxMatch = false; for (Entry<String, ZuulRoute> entry : routeLocator.getAtomicRoutes().get().entrySet()) { if (entry.getKey() != null) { String pattern = entry.getKey(); if (this.pathMatcher.match(pattern, requestURI)) { auxMatch = true; List<Credential> credentials = credentialRepository.findByPattern(pattern); Credential credential = null; if (Objects.nonNull(credentials) && !credentials.isEmpty()) { if (method.equals(HttpMethod.OPTIONS.name())) { Optional<Credential> first = credentials.stream().findFirst(); if (first.get().isCors()) { credential = first.get(); } } if (Objects.isNull(credential)) { credential = credentials.stream() .filter(o -> o.getMethod().equals(HttpMethod.ALL.name()) || method.equals(o.getMethod().toUpperCase())) .findFirst().orElse(null); } } if (credential != null) { ZuulRoute zuulRoute = entry.getValue(); String basePath = credential.getApiBasePath(); requestURI = org.apache.commons.lang.StringUtils.removeStart(requestURI, basePath); ctx.put(PATTERN, org.apache.commons.lang.StringUtils.removeStart(pattern, basePath)); ctx.put(API_NAME, credential.getApiName()); ctx.put(API_ID, credential.getApiId()); ctx.put(RESOURCE_ID, credential.getResourceId()); ctx.put(OPERATION_ID, credential.getOperationId()); ctx.put(OPERATION_PATH, credential.getOperationPath()); String host = ctx.getRequest().getHeader("Host"); EnvironmentInfo environment; String location = null; if (host != null && !host.isEmpty()) { environment = environmentInfoRepository.findByApiIdAndEnvironmentInboundURL(credential.getApiId(), host.toLowerCase()); } else { environment = environmentInfoRepository.findByApiIdAndEnvironmentInboundURL(credential.getApiId(), ctx.getRequest().getRequestURL().toString().toLowerCase()); } if (environment != null) { location = environment.getOutboundURL(); ctx.put(ENVIRONMENT_VARIABLES, environment.getVariables()); } Route route = new Route(zuulRoute.getId(), requestURI, location, "", zuulRoute.getRetryable() != null ? zuulRoute.getRetryable() : false, zuulRoute.isCustomSensitiveHeaders() ? zuulRoute.getSensitiveHeaders() : null); TraceContextHolder traceContextHolder = TraceContextHolder.getInstance(); traceContextHolder.getActualTrace().setApiId(credential.getApiId()); traceContextHolder.getActualTrace().setApiName(credential.getApiName()); traceContextHolder.getActualTrace().setResourceId(credential.getResourceId()); traceContextHolder.getActualTrace().setOperationId(credential.getOperationId()); return new HeimdallRoute(pattern, route, false); } else { ctx.put(INTERRUPT, true); } } } } if (auxMatch) { return new HeimdallRoute().methodNotAllowed(); } return null; }
HeimdallDecorationFilter extends PreDecorationFilter { protected HeimdallRoute getMatchingHeimdallRoute(String requestURI, String method, RequestContext ctx) { boolean auxMatch = false; for (Entry<String, ZuulRoute> entry : routeLocator.getAtomicRoutes().get().entrySet()) { if (entry.getKey() != null) { String pattern = entry.getKey(); if (this.pathMatcher.match(pattern, requestURI)) { auxMatch = true; List<Credential> credentials = credentialRepository.findByPattern(pattern); Credential credential = null; if (Objects.nonNull(credentials) && !credentials.isEmpty()) { if (method.equals(HttpMethod.OPTIONS.name())) { Optional<Credential> first = credentials.stream().findFirst(); if (first.get().isCors()) { credential = first.get(); } } if (Objects.isNull(credential)) { credential = credentials.stream() .filter(o -> o.getMethod().equals(HttpMethod.ALL.name()) || method.equals(o.getMethod().toUpperCase())) .findFirst().orElse(null); } } if (credential != null) { ZuulRoute zuulRoute = entry.getValue(); String basePath = credential.getApiBasePath(); requestURI = org.apache.commons.lang.StringUtils.removeStart(requestURI, basePath); ctx.put(PATTERN, org.apache.commons.lang.StringUtils.removeStart(pattern, basePath)); ctx.put(API_NAME, credential.getApiName()); ctx.put(API_ID, credential.getApiId()); ctx.put(RESOURCE_ID, credential.getResourceId()); ctx.put(OPERATION_ID, credential.getOperationId()); ctx.put(OPERATION_PATH, credential.getOperationPath()); String host = ctx.getRequest().getHeader("Host"); EnvironmentInfo environment; String location = null; if (host != null && !host.isEmpty()) { environment = environmentInfoRepository.findByApiIdAndEnvironmentInboundURL(credential.getApiId(), host.toLowerCase()); } else { environment = environmentInfoRepository.findByApiIdAndEnvironmentInboundURL(credential.getApiId(), ctx.getRequest().getRequestURL().toString().toLowerCase()); } if (environment != null) { location = environment.getOutboundURL(); ctx.put(ENVIRONMENT_VARIABLES, environment.getVariables()); } Route route = new Route(zuulRoute.getId(), requestURI, location, "", zuulRoute.getRetryable() != null ? zuulRoute.getRetryable() : false, zuulRoute.isCustomSensitiveHeaders() ? zuulRoute.getSensitiveHeaders() : null); TraceContextHolder traceContextHolder = TraceContextHolder.getInstance(); traceContextHolder.getActualTrace().setApiId(credential.getApiId()); traceContextHolder.getActualTrace().setApiName(credential.getApiName()); traceContextHolder.getActualTrace().setResourceId(credential.getResourceId()); traceContextHolder.getActualTrace().setOperationId(credential.getOperationId()); return new HeimdallRoute(pattern, route, false); } else { ctx.put(INTERRUPT, true); } } } } if (auxMatch) { return new HeimdallRoute().methodNotAllowed(); } return null; } }
HeimdallDecorationFilter extends PreDecorationFilter { protected HeimdallRoute getMatchingHeimdallRoute(String requestURI, String method, RequestContext ctx) { boolean auxMatch = false; for (Entry<String, ZuulRoute> entry : routeLocator.getAtomicRoutes().get().entrySet()) { if (entry.getKey() != null) { String pattern = entry.getKey(); if (this.pathMatcher.match(pattern, requestURI)) { auxMatch = true; List<Credential> credentials = credentialRepository.findByPattern(pattern); Credential credential = null; if (Objects.nonNull(credentials) && !credentials.isEmpty()) { if (method.equals(HttpMethod.OPTIONS.name())) { Optional<Credential> first = credentials.stream().findFirst(); if (first.get().isCors()) { credential = first.get(); } } if (Objects.isNull(credential)) { credential = credentials.stream() .filter(o -> o.getMethod().equals(HttpMethod.ALL.name()) || method.equals(o.getMethod().toUpperCase())) .findFirst().orElse(null); } } if (credential != null) { ZuulRoute zuulRoute = entry.getValue(); String basePath = credential.getApiBasePath(); requestURI = org.apache.commons.lang.StringUtils.removeStart(requestURI, basePath); ctx.put(PATTERN, org.apache.commons.lang.StringUtils.removeStart(pattern, basePath)); ctx.put(API_NAME, credential.getApiName()); ctx.put(API_ID, credential.getApiId()); ctx.put(RESOURCE_ID, credential.getResourceId()); ctx.put(OPERATION_ID, credential.getOperationId()); ctx.put(OPERATION_PATH, credential.getOperationPath()); String host = ctx.getRequest().getHeader("Host"); EnvironmentInfo environment; String location = null; if (host != null && !host.isEmpty()) { environment = environmentInfoRepository.findByApiIdAndEnvironmentInboundURL(credential.getApiId(), host.toLowerCase()); } else { environment = environmentInfoRepository.findByApiIdAndEnvironmentInboundURL(credential.getApiId(), ctx.getRequest().getRequestURL().toString().toLowerCase()); } if (environment != null) { location = environment.getOutboundURL(); ctx.put(ENVIRONMENT_VARIABLES, environment.getVariables()); } Route route = new Route(zuulRoute.getId(), requestURI, location, "", zuulRoute.getRetryable() != null ? zuulRoute.getRetryable() : false, zuulRoute.isCustomSensitiveHeaders() ? zuulRoute.getSensitiveHeaders() : null); TraceContextHolder traceContextHolder = TraceContextHolder.getInstance(); traceContextHolder.getActualTrace().setApiId(credential.getApiId()); traceContextHolder.getActualTrace().setApiName(credential.getApiName()); traceContextHolder.getActualTrace().setResourceId(credential.getResourceId()); traceContextHolder.getActualTrace().setOperationId(credential.getOperationId()); return new HeimdallRoute(pattern, route, false); } else { ctx.put(INTERRUPT, true); } } } } if (auxMatch) { return new HeimdallRoute().methodNotAllowed(); } return null; } HeimdallDecorationFilter(ProxyRouteLocator routeLocator, String dispatcherServletPath, ZuulProperties properties, ProxyRequestHelper proxyRequestHelper, RequestHelper requestHelper, CredentialRepository credentialRepository, EnvironmentInfoRepository environmentInfoRepository); }
HeimdallDecorationFilter extends PreDecorationFilter { protected HeimdallRoute getMatchingHeimdallRoute(String requestURI, String method, RequestContext ctx) { boolean auxMatch = false; for (Entry<String, ZuulRoute> entry : routeLocator.getAtomicRoutes().get().entrySet()) { if (entry.getKey() != null) { String pattern = entry.getKey(); if (this.pathMatcher.match(pattern, requestURI)) { auxMatch = true; List<Credential> credentials = credentialRepository.findByPattern(pattern); Credential credential = null; if (Objects.nonNull(credentials) && !credentials.isEmpty()) { if (method.equals(HttpMethod.OPTIONS.name())) { Optional<Credential> first = credentials.stream().findFirst(); if (first.get().isCors()) { credential = first.get(); } } if (Objects.isNull(credential)) { credential = credentials.stream() .filter(o -> o.getMethod().equals(HttpMethod.ALL.name()) || method.equals(o.getMethod().toUpperCase())) .findFirst().orElse(null); } } if (credential != null) { ZuulRoute zuulRoute = entry.getValue(); String basePath = credential.getApiBasePath(); requestURI = org.apache.commons.lang.StringUtils.removeStart(requestURI, basePath); ctx.put(PATTERN, org.apache.commons.lang.StringUtils.removeStart(pattern, basePath)); ctx.put(API_NAME, credential.getApiName()); ctx.put(API_ID, credential.getApiId()); ctx.put(RESOURCE_ID, credential.getResourceId()); ctx.put(OPERATION_ID, credential.getOperationId()); ctx.put(OPERATION_PATH, credential.getOperationPath()); String host = ctx.getRequest().getHeader("Host"); EnvironmentInfo environment; String location = null; if (host != null && !host.isEmpty()) { environment = environmentInfoRepository.findByApiIdAndEnvironmentInboundURL(credential.getApiId(), host.toLowerCase()); } else { environment = environmentInfoRepository.findByApiIdAndEnvironmentInboundURL(credential.getApiId(), ctx.getRequest().getRequestURL().toString().toLowerCase()); } if (environment != null) { location = environment.getOutboundURL(); ctx.put(ENVIRONMENT_VARIABLES, environment.getVariables()); } Route route = new Route(zuulRoute.getId(), requestURI, location, "", zuulRoute.getRetryable() != null ? zuulRoute.getRetryable() : false, zuulRoute.isCustomSensitiveHeaders() ? zuulRoute.getSensitiveHeaders() : null); TraceContextHolder traceContextHolder = TraceContextHolder.getInstance(); traceContextHolder.getActualTrace().setApiId(credential.getApiId()); traceContextHolder.getActualTrace().setApiName(credential.getApiName()); traceContextHolder.getActualTrace().setResourceId(credential.getResourceId()); traceContextHolder.getActualTrace().setOperationId(credential.getOperationId()); return new HeimdallRoute(pattern, route, false); } else { ctx.put(INTERRUPT, true); } } } } if (auxMatch) { return new HeimdallRoute().methodNotAllowed(); } return null; } HeimdallDecorationFilter(ProxyRouteLocator routeLocator, String dispatcherServletPath, ZuulProperties properties, ProxyRequestHelper proxyRequestHelper, RequestHelper requestHelper, CredentialRepository credentialRepository, EnvironmentInfoRepository environmentInfoRepository); @Override boolean shouldFilter(); @Override Object run(); }
HeimdallDecorationFilter extends PreDecorationFilter { protected HeimdallRoute getMatchingHeimdallRoute(String requestURI, String method, RequestContext ctx) { boolean auxMatch = false; for (Entry<String, ZuulRoute> entry : routeLocator.getAtomicRoutes().get().entrySet()) { if (entry.getKey() != null) { String pattern = entry.getKey(); if (this.pathMatcher.match(pattern, requestURI)) { auxMatch = true; List<Credential> credentials = credentialRepository.findByPattern(pattern); Credential credential = null; if (Objects.nonNull(credentials) && !credentials.isEmpty()) { if (method.equals(HttpMethod.OPTIONS.name())) { Optional<Credential> first = credentials.stream().findFirst(); if (first.get().isCors()) { credential = first.get(); } } if (Objects.isNull(credential)) { credential = credentials.stream() .filter(o -> o.getMethod().equals(HttpMethod.ALL.name()) || method.equals(o.getMethod().toUpperCase())) .findFirst().orElse(null); } } if (credential != null) { ZuulRoute zuulRoute = entry.getValue(); String basePath = credential.getApiBasePath(); requestURI = org.apache.commons.lang.StringUtils.removeStart(requestURI, basePath); ctx.put(PATTERN, org.apache.commons.lang.StringUtils.removeStart(pattern, basePath)); ctx.put(API_NAME, credential.getApiName()); ctx.put(API_ID, credential.getApiId()); ctx.put(RESOURCE_ID, credential.getResourceId()); ctx.put(OPERATION_ID, credential.getOperationId()); ctx.put(OPERATION_PATH, credential.getOperationPath()); String host = ctx.getRequest().getHeader("Host"); EnvironmentInfo environment; String location = null; if (host != null && !host.isEmpty()) { environment = environmentInfoRepository.findByApiIdAndEnvironmentInboundURL(credential.getApiId(), host.toLowerCase()); } else { environment = environmentInfoRepository.findByApiIdAndEnvironmentInboundURL(credential.getApiId(), ctx.getRequest().getRequestURL().toString().toLowerCase()); } if (environment != null) { location = environment.getOutboundURL(); ctx.put(ENVIRONMENT_VARIABLES, environment.getVariables()); } Route route = new Route(zuulRoute.getId(), requestURI, location, "", zuulRoute.getRetryable() != null ? zuulRoute.getRetryable() : false, zuulRoute.isCustomSensitiveHeaders() ? zuulRoute.getSensitiveHeaders() : null); TraceContextHolder traceContextHolder = TraceContextHolder.getInstance(); traceContextHolder.getActualTrace().setApiId(credential.getApiId()); traceContextHolder.getActualTrace().setApiName(credential.getApiName()); traceContextHolder.getActualTrace().setResourceId(credential.getResourceId()); traceContextHolder.getActualTrace().setOperationId(credential.getOperationId()); return new HeimdallRoute(pattern, route, false); } else { ctx.put(INTERRUPT, true); } } } } if (auxMatch) { return new HeimdallRoute().methodNotAllowed(); } return null; } HeimdallDecorationFilter(ProxyRouteLocator routeLocator, String dispatcherServletPath, ZuulProperties properties, ProxyRequestHelper proxyRequestHelper, RequestHelper requestHelper, CredentialRepository credentialRepository, EnvironmentInfoRepository environmentInfoRepository); @Override boolean shouldFilter(); @Override Object run();  }