Search in sources :

Example 76 with RequestContext

use of com.netflix.zuul.context.RequestContext in project micro-service by Lovnx.

the class SecondFilter method run.

@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    // HttpServletResponse response = ctx.getResponse();
    log.info("第二级过滤器!");
    log.info("===============");
    throw new RuntimeException();
// log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));
// System.out.println(request.getRequestURL());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestContext(com.netflix.zuul.context.RequestContext)

Example 77 with RequestContext

use of com.netflix.zuul.context.RequestContext in project micro-service by Lovnx.

the class ErrorFilter method run.

@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    // HttpServletResponse response = ctx.getResponse();
    log.info("进入错误异常的过滤器!");
    log.info("===============");
    // log.info("access token ok");
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestContext(com.netflix.zuul.context.RequestContext)

Example 78 with RequestContext

use of com.netflix.zuul.context.RequestContext in project micro-service by Lovnx.

the class ResultFilter method run.

@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    // HttpServletResponse response = ctx.getResponse();
    log.info("进入结果处理的过滤器!");
    log.info("===============");
    // log.info("access token ok");
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestContext(com.netflix.zuul.context.RequestContext)

Example 79 with RequestContext

use of com.netflix.zuul.context.RequestContext in project new-cloud by xie-summer.

the class RedisRateLimitZuulFilter method run.

@Override
public Object run() {
    try {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletResponse response = currentContext.getResponse();
        if (!redisTemplate.hasKey(TIME_KEY)) {
            redisTemplate.opsForValue().set(TIME_KEY, 0, 1, TimeUnit.SECONDS);
        }
        if (redisTemplate.hasKey(TIME_KEY) && redisTemplate.opsForValue().increment(COUNTER_KEY, 1) > 400) {
            HttpStatus httpStatus = HttpStatus.TOO_MANY_REQUESTS;
            response.setContentType(MediaType.TEXT_PLAIN_VALUE);
            response.setStatus(httpStatus.value());
            response.getWriter().append(httpStatus.getReasonPhrase());
            currentContext.setSendZuulResponse(false);
            throw new RateLimiterException(httpStatus.getReasonPhrase(), httpStatus.value(), httpStatus.getReasonPhrase());
        }
    } catch (Throwable e) {
        ReflectionUtils.rethrowRuntimeException(e);
    }
    return null;
}
Also used : HttpStatus(org.springframework.http.HttpStatus) HttpServletResponse(javax.servlet.http.HttpServletResponse) RateLimiterException(com.cloud.common.exception.RateLimiterException) RequestContext(com.netflix.zuul.context.RequestContext)

Example 80 with RequestContext

use of com.netflix.zuul.context.RequestContext in project spring-boot-admin by codecentric.

the class ApplicationHeadersFilterTest method test_add_headers_on_matching_route_only.

@Test
public void test_add_headers_on_matching_route_only() {
    Application application = Application.create("test").withHealthUrl("/health").build();
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.add("test", "qwertz");
    when(routeLocator.getMatchingRoute("/health")).thenReturn(new ApplicationRoute(application, null, null, null, null));
    when(headerProvider.getHeaders(application)).thenReturn(httpHeaders);
    RequestContext context = creteRequestContext("/health");
    RequestContext.testSetCurrentContext(context);
    filter.run();
    assertThat(context.getZuulRequestHeaders().get("test"), is("qwertz"));
    context = creteRequestContext("/foobar");
    RequestContext.testSetCurrentContext(context);
    filter.run();
    assertThat(context.getZuulRequestHeaders().get("test"), nullValue());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) RequestContext(com.netflix.zuul.context.RequestContext) Application(de.codecentric.boot.admin.model.Application) ApplicationRoute(de.codecentric.boot.admin.zuul.ApplicationRouteLocator.ApplicationRoute) Test(org.junit.Test)

Aggregations

RequestContext (com.netflix.zuul.context.RequestContext)163 Test (org.junit.Test)59 HttpServletRequest (javax.servlet.http.HttpServletRequest)27 ZuulRoute (org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute)24 Before (org.junit.Before)23 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)22 Set (java.util.Set)10 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)9 ZuulException (com.netflix.zuul.exception.ZuulException)7 InputStream (java.io.InputStream)7 HashSet (java.util.HashSet)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 Route (org.springframework.cloud.netflix.zuul.filters.Route)7 ZuulProperties (org.springframework.cloud.netflix.zuul.filters.ZuulProperties)7 IOException (java.io.IOException)6 HttpServletRequestWrapper (com.netflix.zuul.http.HttpServletRequestWrapper)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 List (java.util.List)4 HttpHeaders (org.springframework.http.HttpHeaders)4 RateLimitCheck (com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck)3