Search in sources :

Example 71 with RequestContext

use of com.netflix.zuul.context.RequestContext in project bucket4j-spring-boot-starter by MarcGiffing.

the class ServletRateLimitFilter method run.

@Override
public Object run() {
    RequestContext context = RequestContext.getCurrentContext();
    HttpServletRequest request = context.getRequest();
    Long remainingLimit = null;
    for (RateLimitCheck rl : filterConfig.getRateLimitChecks()) {
        ConsumptionProbe probe = rl.rateLimit(request);
        if (probe != null) {
            if (probe.isConsumed()) {
                remainingLimit = getRemainingLimit(remainingLimit, probe);
            } else {
                context.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
                context.setResponseBody(filterConfig.getHttpResponseBody());
                context.setSendZuulResponse(false);
            }
            if (filterConfig.getStrategy().equals(RateLimitConditionMatchingStrategy.FIRST)) {
                break;
            }
        }
    }
    ;
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RateLimitCheck(com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck) RequestContext(com.netflix.zuul.context.RequestContext) ConsumptionProbe(io.github.bucket4j.ConsumptionProbe)

Example 72 with RequestContext

use of com.netflix.zuul.context.RequestContext in project bucket4j-spring-boot-starter by MarcGiffing.

the class ZuulRateLimitFilter method run.

@Override
public Object run() {
    RequestContext context = getCurrentRequestContext();
    HttpServletRequest request = context.getRequest();
    Long remainingLimit = null;
    for (RateLimitCheck rl : filterConfig.getRateLimitChecks()) {
        ConsumptionProbe probe = rl.rateLimit(request);
        if (probe != null) {
            if (probe.isConsumed()) {
                remainingLimit = getRemainingLimit(remainingLimit, probe);
            } else {
                context.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
                context.setResponseBody(filterConfig.getHttpResponseBody());
                context.setSendZuulResponse(false);
                break;
            }
            if (filterConfig.getStrategy().equals(RateLimitConditionMatchingStrategy.FIRST)) {
                break;
            }
        }
    }
    ;
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RateLimitCheck(com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck) RequestContext(com.netflix.zuul.context.RequestContext) ConsumptionProbe(io.github.bucket4j.ConsumptionProbe)

Example 73 with RequestContext

use of com.netflix.zuul.context.RequestContext in project bucket4j-spring-boot-starter by MarcGiffing.

the class ZuulRateLimitFilter method shouldFilter.

@Override
public boolean shouldFilter() {
    RequestContext currentContext = RequestContext.getCurrentContext();
    HttpServletRequest request = currentContext.getRequest();
    String requestURI = request.getRequestURI();
    return requestURI.matches(filterConfig.getUrl());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestContext(com.netflix.zuul.context.RequestContext)

Example 74 with RequestContext

use of com.netflix.zuul.context.RequestContext in project bucket4j-spring-boot-starter by MarcGiffing.

the class ServletRateLimitFilterTest method should_execute_only_one_check_when_using_RateLimitConditionMatchingStrategy_FIRST.

@Test
public void should_execute_only_one_check_when_using_RateLimitConditionMatchingStrategy_FIRST() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/url");
    RequestContext context = new RequestContext();
    context.setRequest(request);
    RequestContext.testSetCurrentContext(context);
    configuration.setStrategy(RateLimitConditionMatchingStrategy.FIRST);
    when(rateLimitCheck1.rateLimit(any())).thenReturn(consumptionProbe);
    standaloneSetup(new TestController()).addFilters(filter).build().perform(get(("/test")));
    verify(rateLimitCheck1, times(1)).rateLimit(any());
    verify(rateLimitCheck2, times(0)).rateLimit(any());
    verify(rateLimitCheck3, times(0)).rateLimit(any());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestContext(com.netflix.zuul.context.RequestContext) Test(org.junit.Test)

Example 75 with RequestContext

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

the class FirstFilter 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)

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