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();  }
@Test public void testCallMethodAll() { 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/{id}", "/v2", "apiName", 10L, 88L, 10L, false); Credential opDelete = new Credential(HttpMethod.DELETE.name(), "/api/foo/{id}", "/v2", "apiName", 11L, 88L, 10L, false); Credential opAll = new Credential(HttpMethod.ALL.name(), "/api/foo/{id}", "/v2", "apiName", 12L, 88L, 10L, false); Mockito.when(routeLocator.getAtomicRoutes()).thenReturn(new AtomicReference<>(routes)); Mockito.when(credentialRepository.findByPattern("/v2/api/foo")).thenReturn(Lists.newArrayList(opPost, opDelete, opAll)); HeimdallRoute heimdallRoute = this.filter.getMatchingHeimdallRoute("/v2/api/foo", HttpMethod.GET.name(), this.ctx); assertNotNull(heimdallRoute); assertEquals("/api/foo", heimdallRoute.getRoute().getPath()); }
@Test public void testCallMethodDifferentTheAll() { this.request.setRequestURI("/v2/api/foo"); this.request.setMethod(HttpMethod.GET.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", 10L, 88L, 10L, false); Credential opDelete = new Credential(HttpMethod.DELETE.name(), "/api/foo", "/v2", "apiName", 11L, 88L, 10L, false); Credential opGet = new Credential(HttpMethod.GET.name(), "/api/foo", "/v2", "apiName", 11L, 88L, 10L, false); 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.GET.name(), this.ctx); assertNotNull(heimdallRoute); assertEquals("/api/foo", heimdallRoute.getRoute().getPath()); }
@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()); }
@Test public void testCallMethodIsOptionWithoutCors() { 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", 10L, 88L, 10L, false); Credential opDelete = new Credential(HttpMethod.DELETE.name(), "/api/foo", "/v2", "apiName", 12L, 88L, 10L, false); Credential opGet = new Credential(HttpMethod.GET.name(), "/api/foo", "/v2", "apiName", 13L, 88L, 10L, false); 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); assertTrue(heimdallRoute.isMethodNotAllowed()); }
@Test public void testCallMethodInOperationsWithoutCors() { 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", 10L, 88L, 10L, false); Credential opDelete = new Credential(HttpMethod.OPTIONS.name(), "/api/foo", "/v2", "apiName", 11L, 88L, 10L, false); Credential opGet = new Credential(HttpMethod.GET.name(), "/api/foo", "/v2", "apiName", 12L, 88L, 10L, false); 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()); }