org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute Java Examples

The following examples show how to use org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractDynRouteLocator.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
@Override
protected Map<String, ZuulRoute> locateRoutes() {
    LinkedHashMap<String, ZuulRoute> routesMap = new LinkedHashMap<>();
    // 从application.properties中加载静态路由信息
    routesMap.putAll(super.locateRoutes());
    // 从数据源中加载动态路由信息
    routesMap.putAll(loadDynamicRoute());
    // 优化一下配置
    LinkedHashMap<String, ZuulRoute> values = new LinkedHashMap<>();
    for (Map.Entry<String, ZuulRoute> entry : routesMap.entrySet()) {
        String path = entry.getKey();
        // Prepend with slash if not already present.
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        if (StringUtils.hasText(this.properties.getPrefix())) {
            path = this.properties.getPrefix() + path;
            if (!path.startsWith("/")) {
                path = "/" + path;
            }
        }
        values.put(path, entry.getValue());
    }
    return values;
}
 
Example #2
Source File: HeimdallDecorationFilterTest.java    From heimdall with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #3
Source File: HeimdallDecorationFilterTest.java    From heimdall with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #4
Source File: HeimdallDecorationFilterTest.java    From heimdall with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #5
Source File: HeimdallDecorationFilterTest.java    From heimdall with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #6
Source File: HeimdallDecorationFilterTest.java    From heimdall with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #7
Source File: HeimdallDecorationFilterTest.java    From heimdall with Apache License 2.0 6 votes vote down vote up
@Test
public void throwNotAllowedRoute() {
    this.request.setRequestURI("/v2/api/foo/1");
    this.request.setMethod(HttpMethod.DELETE.name());
    Map<String, ZuulRoute> routes = new LinkedHashMap<>();
    ZuulRoute route = new ZuulRoute("idFoo", "/v2/api/foo/{id}", null, "my.dns.com.br", true, null, Collections.newSetFromMap(new ConcurrentHashMap<>()));
    routes.put("/v2/api/foo/{id}", route);

    Credential opGet = new Credential(HttpMethod.GET.name(), "/api/foo/{id}", "/path", "apiName", 10L, 88L, 10L, false);

    Mockito.when(routeLocator.getAtomicRoutes()).thenReturn(new AtomicReference<>(routes));
    Mockito.when(credentialRepository.findByPattern("/v2/api/foo/{id}")).thenReturn(Lists.newArrayList(opGet));

    this.filter.run();

    assertFalse(this.ctx.sendZuulResponse());
    assertEquals(HttpStatus.METHOD_NOT_ALLOWED.value(), this.ctx.getResponseStatusCode());
    assertEquals(HttpStatus.METHOD_NOT_ALLOWED.getReasonPhrase(), this.ctx.getResponseBody());
}
 
Example #8
Source File: RouteSortTest.java    From heimdall with Apache License 2.0 6 votes vote down vote up
@Test
public void routeWithSamePrefixValidPathAndWildCard() {
     
     ZuulRoute r1 = new ZuulRoute("/foo/bar", "");
     ZuulRoute r2 = new ZuulRoute("/foo/bar/alpha", "");
     ZuulRoute r3 = new ZuulRoute("/foo/bar/*", "");
     ZuulRoute r4 = new ZuulRoute("/*", "");
     
     actual.add(r4);
     actual.add(r3);
     actual.add(r2);
     actual.add(r1);
     
     expected.add(r1);
     expected.add(r2);
     expected.add(r3);
     expected.add(r4);

     actual.sort(new RouteSort());

     assertThat(actual, is(expected));
}
 
Example #9
Source File: RouteSortTest.java    From heimdall with Apache License 2.0 6 votes vote down vote up
@Test
public void routeWithSamePrefixAndDoubleWildCardAndSingleWildCard() {
     
     ZuulRoute r1 = new ZuulRoute("/foo/bar", "");
     ZuulRoute r2 = new ZuulRoute("/foo/bar/*", "");
     ZuulRoute r3 = new ZuulRoute("/foo/bar/**", "");
     
     actual.add(r3);
     actual.add(r2);
     actual.add(r1);
     
     expected.add(r1);
     expected.add(r2);
     expected.add(r3);

     actual.sort(new RouteSort());

     assertThat(actual, is(expected));
}
 
Example #10
Source File: NacosDynRouteLocator.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, ZuulProperties.ZuulRoute> loadDynamicRoute() {
    Map<String, ZuulRoute> routes = new LinkedHashMap<>();
    if (zuulRouteEntities == null) {
        zuulRouteEntities = getNacosConfig();
    }
    for (ZuulRouteEntity result : zuulRouteEntities) {
        if (StrUtil.isBlank(result.getPath()) || !result.isEnabled()) {
            continue;
        }
        ZuulRoute zuulRoute = new ZuulRoute();
        BeanUtil.copyProperties(result, zuulRoute);
        routes.put(zuulRoute.getPath(), zuulRoute);
    }
    return routes;
}
 
Example #11
Source File: JdbcRouteLocator.java    From open-cloud with MIT License 6 votes vote down vote up
/**
 * 加载路由配置
 *
 * @return
 */
@Override
protected Map<String, ZuulProperties.ZuulRoute> locateRoutes() {
    LinkedHashMap<String, ZuulProperties.ZuulRoute> routesMap = Maps.newLinkedHashMap();
    routesMap.putAll(super.locateRoutes());
    //从db中加载路由信息
    routesMap.putAll(loadRoutes());
    //优化一下配置
    LinkedHashMap<String, ZuulProperties.ZuulRoute> values = Maps.newLinkedHashMap();
    for (Map.Entry<String, ZuulProperties.ZuulRoute> entry : routesMap.entrySet()) {
        String path = entry.getKey();
        // Prepend with slash if not already present.
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        if (StringUtils.hasText(this.properties.getPrefix())) {
            path = this.properties.getPrefix() + path;
            if (!path.startsWith("/")) {
                path = "/" + path;
            }
        }
        values.put(path, entry.getValue());
    }
    return values;
}
 
Example #12
Source File: NewZuulRouteLocator.java    From spring-cloud-zuul-nacos with Apache License 2.0 6 votes vote down vote up
@Override
protected Map<String, ZuulRoute> locateRoutes() {
	LinkedHashMap<String, ZuulRoute> routesMap = new LinkedHashMap<String, ZuulRoute>();
	// 从application.properties中加载路由信息
	routesMap.putAll(super.locateRoutes());
	// 从Nacos中加载路由信息
	routesMap.putAll(propertiesAssemble.getProperties());
	// 优化一下配置
	LinkedHashMap<String, ZuulRoute> values = new LinkedHashMap<>();
	for (Map.Entry<String, ZuulRoute> entry : routesMap.entrySet()) {
		String path = entry.getKey();
		// Prepend with slash if not already present.
		if (!path.startsWith("/")) {
			path = "/" + path;
		}
		if (StringUtils.hasText(this.properties.getPrefix())) {
			path = this.properties.getPrefix() + path;
			if (!path.startsWith("/")) {
				path = "/" + path;
			}
		}
		values.put(path, entry.getValue());
	}
	return values;
}
 
Example #13
Source File: PropertiesAssemble.java    From spring-cloud-zuul-nacos with Apache License 2.0 6 votes vote down vote up
public Map<String, ZuulRoute> getProperties() {
	Map<String, ZuulRoute> routes = new LinkedHashMap<>();
	List<ZuulRouteEntity> results = listenerNacos("zuul-server","zuul_route");
	for (ZuulRouteEntity result : results) {
		if (StringUtils.isBlank(result.getPath())
				/*|| org.apache.commons.lang3.StringUtils.isBlank(result.getUrl())*/) {
			continue;
		}
		ZuulRoute zuulRoute = new ZuulRoute();
		try {
			BeanUtils.copyProperties(result, zuulRoute);
		} catch (Exception e) {
		}
		routes.put(zuulRoute.getPath(), zuulRoute);
	}
	return routes;
}
 
Example #14
Source File: CacheZuulRouteStorageTest.java    From heimdall with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadOneApiWithTwoResources() {
     List<Api> apis = new LinkedList<>();
     List<Environment> environments = new ArrayList<>();
     Environment environment = new Environment();
     environment.setInboundURL("dns.production.com.br");
     environment.setOutboundURL("dns.production.com.br");
     Api api = new Api(10L, "foo", "v1", "fooDescription", "/foo", false, LocalDateTime.now(), new HashSet<>(), Status.ACTIVE, environments, null);

     List<Resource> res = new LinkedList<>();
     
     Resource resource = new Resource();
     resource.setId(88L);
     resource.setApi(api);
     resource.setOperations(new ArrayList<>());
     
     Operation opPost = new Operation(10L, HttpMethod.POST, "/api/foo", "POST description", resource);
     Operation opGet = new Operation(11L, HttpMethod.GET, "/api/foo/{id}", "GET description", resource);
     Operation opDelete = new Operation(12L, HttpMethod.DELETE, "/api/foo/{id}", "DELETE description", resource);
     resource.getOperations().addAll(Arrays.asList(opDelete, opGet, opPost));
     
     res.add(resource);
     api.setResources(new HashSet<>(res));
     apis.add(api);
     
     Mockito.when(repository.findByStatus(Status.ACTIVE)).thenReturn(apis);
     Mockito.when(resourceRepository.findByApiId(Mockito.anyLong())).thenReturn(res);
     Mockito.when(apiJDBCRepository.findAllIds()).thenReturn(Collections.singletonList(10L));
     Mockito.when(operationJDBCRepository.findOperationsFromAllApis(Collections.singletonList(10L))).thenReturn(Arrays.asList("/api/foo", "/api/foo/{id}", "/api/foo/{id}"));
     ReflectionTestUtils.setField(this.storage, "profile", Constants.PRODUCTION);
     
     List<ZuulRoute> zuulRoutes = storage.findAll();
     
     assertNotNull(zuulRoutes);
     assertEquals(resource.getOperations().size(), zuulRoutes.size());
}
 
Example #15
Source File: RouteSortTest.java    From heimdall with Apache License 2.0 5 votes vote down vote up
@Test
public void routeWithWildCardNeedBeTheLast() {

     ZuulRoute r1 = new ZuulRoute("/cartoes", "");
     ZuulRoute r2 = new ZuulRoute("/cartoes/*", "");
     ZuulRoute r3 = new ZuulRoute("/foo", "");
     ZuulRoute r4 = new ZuulRoute("/foo/*", "");
     ZuulRoute r5 = new ZuulRoute("/*", "");
     
     actual.add(r5);
     actual.add(r3);
     actual.add(r2);
     actual.add(r4);
     actual.add(r1);

     expected.add(r1);
     expected.add(r2);
     expected.add(r3);
     expected.add(r4);
     expected.add(r5);

     actual.sort(new RouteSort());

     assertThat(actual, IsCollectionWithSize.hasSize(5));
     assertTrue(actual.get(actual.size() - 1).getPath().startsWith("/*"));
     assertThat(actual, is(expected));
}
 
Example #16
Source File: ProxyRouteLocator.java    From heimdall with Apache License 2.0 5 votes vote down vote up
private LinkedHashMap<String, ZuulRoute> init() {

          log.info("Calling Storage.findAll()");
          LinkedHashMap<String, ZuulRoute> routesMap = new LinkedHashMap<String, ZuulRoute>();
          for (ZuulRoute route : storage.findAll()) {
               routesMap.put(route.getPath(), route);
          }
          return routesMap;

     }
 
Example #17
Source File: CacheZuulRouteStorage.java    From heimdall with Apache License 2.0 5 votes vote down vote up
/**
 * Gets a ordered List of {@link ZuulRoute}.
 *
 * @return A ordered List of {@link ZuulRoute}
 */
public List<ZuulRoute> init() {

	log.info("Initialize routes from profiles: " + profile);
	List<ZuulRoute> routes = new LinkedList<>();
	List<Long> apiIds = apiJDBCRepository.findAllIds();
	boolean production = Constants.PRODUCTION.equals(profile);

	String destination;

	if (production) {
		destination = "producao";
	} else {
		destination = "sandbox";
	}
	
	List<String> apiPathConcatWithOperationPaths = null;
	if (apiIds != null && !apiIds.isEmpty()) {
		
		apiPathConcatWithOperationPaths = operationJDBCRepository.findOperationsFromAllApis(apiIds);
	}

	if (Objects.nonNull(apiPathConcatWithOperationPaths) && !apiPathConcatWithOperationPaths.isEmpty()) {

		for (String completePath : apiPathConcatWithOperationPaths) {

			ZuulRoute route = new ZuulRoute(completePath, destination);
			route.setStripPrefix(false);
			route.setSensitiveHeaders(Collections.newSetFromMap(new ConcurrentHashMap<>()));
			route.setRetryable(retryable);
			routes.add(route);
		}
	}

	routes.sort(new RouteSort());
	return routes;
}
 
Example #18
Source File: HeimdallDecorationFilterTest.java    From heimdall with Apache License 2.0 5 votes vote down vote up
@Test
public void routeWithoutApiBasePath() {

    this.request.setRequestURI("/v2/api/foo/1");
    this.request.setMethod(HttpMethod.GET.name());
    Map<String, ZuulRoute> routes = new LinkedHashMap<>();
    ZuulRoute route = new ZuulRoute("idFoo", "/v2/api/foo/{id}", null, "my.dns.com.br", true, null, Collections.newSetFromMap(new ConcurrentHashMap<>()));
    routes.put("/v2/api/foo/{id}", route);

    EnvironmentInfo environmentInfo = new EnvironmentInfo();
    environmentInfo.setId(1L);
    environmentInfo.setOutboundURL("http://outbound:8080");
    environmentInfo.setVariables(new HashMap<>());

    Credential opPost = new Credential(HttpMethod.POST.name(), "/api/foo", "/v2", "apiName", 10L, 88L, 10L, false);
    Credential opGet = new Credential(HttpMethod.GET.name(), "/api/foo/{id}", "/v2", "apiName", 10L, 88L, 10L, false);
    Credential opDelete = new Credential(HttpMethod.DELETE.name(), "/api/foo/{id}", "/v2", "apiName", 10L, 88L, 10L, false);

    Mockito.when(routeLocator.getAtomicRoutes()).thenReturn(new AtomicReference<>(routes));
    Mockito.when(credentialRepository.findByPattern("/v2/api/foo/{id}")).thenReturn(Lists.newArrayList(opPost, opGet, opDelete));
    Mockito.when(environmentInfoRepository.findByApiIdAndEnvironmentInboundURL(10L, "http://localhost/v2/api/foo/1")).thenReturn(environmentInfo);

    this.filter.run();

    assertEquals("/api/foo/1", this.ctx.get(REQUEST_URI_KEY));
    assertTrue(this.ctx.sendZuulResponse());
}
 
Example #19
Source File: HeimdallDecorationFilterTest.java    From heimdall with Apache License 2.0 5 votes vote down vote up
@Test
public void matchRouteWithMultiEnvironments() {
    this.request.setRequestURI("/path/api/foo");
    this.request.setMethod(HttpMethod.GET.name());
    this.request.addHeader("host", "some-path.com");

    Map<String, ZuulRoute> routes = new LinkedHashMap<>();
    ZuulRoute route = new ZuulRoute("idFoo", "/path/api/foo", null, "my.dns.com.br", true, null, Collections.newSetFromMap(new ConcurrentHashMap<>()));
    routes.put("/path/api/foo", route);

    EnvironmentInfo environmentInfo = new EnvironmentInfo();
    environmentInfo.setId(1L);
    environmentInfo.setOutboundURL("https://some-path.com");
    environmentInfo.setVariables(new HashMap<>());

    Credential opGet = new Credential(HttpMethod.GET.name(), "/api/foo", "/path", "apiName", 10L, 88L, 10L, false);
    Credential opDelete = new Credential(HttpMethod.DELETE.name(), "/api/foo", "/path", "apiName", 10L, 88L, 10L, false);

    Mockito.when(routeLocator.getAtomicRoutes()).thenReturn(new AtomicReference<>(routes));
    Mockito.when(credentialRepository.findByPattern("/path/api/foo")).thenReturn(Lists.newArrayList(opGet, opDelete));
    Mockito.when(environmentInfoRepository.findByApiIdAndEnvironmentInboundURL(10L, "some-path.com")).thenReturn(environmentInfo);

    this.filter.run();

    assertEquals("/api/foo", this.ctx.get(REQUEST_URI_KEY));
    assertTrue(this.ctx.sendZuulResponse());
}
 
Example #20
Source File: RouteSortTest.java    From heimdall with Apache License 2.0 4 votes vote down vote up
@Test
public void routeWithSamePrefix() {

     ZuulRoute r1 = new ZuulRoute("/foo/bar", "");
     ZuulRoute r2 = new ZuulRoute("/foo/bar/alpha/beta", "");
     
     actual.add(r2);
     actual.add(r1);

     expected.add(r1);
     expected.add(r2);

     actual.sort(new RouteSort());

     assertThat(actual, is(expected));
}
 
Example #21
Source File: RouteSortTest.java    From heimdall with Apache License 2.0 4 votes vote down vote up
@Test
public void routeWithSamePrefixAndSingleWildCard() {
   
     ZuulRoute r1 = new ZuulRoute("/foo/bar", "");
     ZuulRoute r2 = new ZuulRoute("/foo/bar/*", "");
     
     actual.add(r2);
     actual.add(r1);

     expected.add(r1);
     expected.add(r2);

     actual.sort(new RouteSort());

     assertThat(actual, is(expected));
}
 
Example #22
Source File: RouteSortTest.java    From heimdall with Apache License 2.0 4 votes vote down vote up
@Test
public void routeWithSamePrefixAndDoubleWildCard() {

     ZuulRoute r1 = new ZuulRoute("/foo/bar", "");
     ZuulRoute r2 = new ZuulRoute("/foo/bar/**", "");
     
     actual.add(r2);
     actual.add(r1);

     expected.add(r1);
     expected.add(r2);

     actual.sort(new RouteSort());

     assertThat(actual, is(expected));
}
 
Example #23
Source File: CacheZuulRouteStorage.java    From heimdall with Apache License 2.0 4 votes vote down vote up
@Override
public List<ZuulRoute> findAll() {

	return init();
}
 
Example #24
Source File: HeimdallDecorationFilter.java    From heimdall with Apache License 2.0 4 votes vote down vote up
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;
    }
 
Example #25
Source File: RouteSort.java    From heimdall with Apache License 2.0 3 votes vote down vote up
@Override
public int compare(ZuulRoute r1, ZuulRoute r2) {

     String pattern1 = r1.getPath();
     String pattern2 = r2.getPath();

     if (pattern1.startsWith("/*")) return AFTER;
     if (pattern2.startsWith("/*")) return BEFORE;

     String[] split1 = pattern1.split("/");
     String[] split2 = pattern2.split("/");

     int max = Math.min(split1.length, split2.length);

     for (int i = 0; i < max; i++) {

          if (!split1[i].equals(split2[i])) {

               if (split1[i].equals("**")) return AFTER;
               if (split2[i].equals("**")) return BEFORE;

               if (split1[i].equals("*")) return AFTER;
               if (split2[i].equals("*")) return BEFORE;

               return split1[i].compareTo(split2[i]);

          }

     }

     return pattern1.compareTo(pattern2);
}
 
Example #26
Source File: ZuulRouteStorage.java    From heimdall with Apache License 2.0 2 votes vote down vote up
/**
* Gets a List of {@link ZuulRoute}.
* 
* @return		The List of {@link ZuulRoute} 
*/
  List<ZuulProperties.ZuulRoute> findAll();
 
Example #27
Source File: AbstractDynRouteLocator.java    From microservices-platform with Apache License 2.0 2 votes vote down vote up
/**
 * 加载路由配置,由子类去实现
 */
public abstract Map<String, ZuulRoute> loadDynamicRoute();
 
Example #28
Source File: ProxyRouteLocator.java    From heimdall with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the routes
 * 
 * @return	{@link AtomicReference} of String, {@link ZuulRoute}
 */
public AtomicReference<Map<String, ZuulRoute>> getAtomicRoutes() {

     return routes;
}