Search in sources :

Example 1 with RequestContext

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

the class ApplicationHeadersFilterTest method creteRequestContext.

private RequestContext creteRequestContext(String uri) {
    RequestContext context = new RequestContext();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI(uri);
    context.setRequest(request);
    return context;
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestContext(com.netflix.zuul.context.RequestContext)

Example 2 with RequestContext

use of com.netflix.zuul.context.RequestContext in project zuul by Netflix.

the class FilterProcessor method processZuulFilter.

/**
     * Processes an individual ZuulFilter. This method adds Debug information. Any uncaught Thowables are caught by this method and converted to a ZuulException with a 500 status code.
     *
     * @param filter
     * @return the return value for that filter
     * @throws ZuulException
     */
public Object processZuulFilter(ZuulFilter filter) throws ZuulException {
    RequestContext ctx = RequestContext.getCurrentContext();
    boolean bDebug = ctx.debugRouting();
    final String metricPrefix = "zuul.filter-";
    long execTime = 0;
    String filterName = "";
    try {
        long ltime = System.currentTimeMillis();
        filterName = filter.getClass().getSimpleName();
        RequestContext copy = null;
        Object o = null;
        Throwable t = null;
        if (bDebug) {
            Debug.addRoutingDebug("Filter " + filter.filterType() + " " + filter.filterOrder() + " " + filterName);
            copy = ctx.copy();
        }
        ZuulFilterResult result = filter.runFilter();
        ExecutionStatus s = result.getStatus();
        execTime = System.currentTimeMillis() - ltime;
        switch(s) {
            case FAILED:
                t = result.getException();
                ctx.addFilterExecutionSummary(filterName, ExecutionStatus.FAILED.name(), execTime);
                break;
            case SUCCESS:
                o = result.getResult();
                ctx.addFilterExecutionSummary(filterName, ExecutionStatus.SUCCESS.name(), execTime);
                if (bDebug) {
                    Debug.addRoutingDebug("Filter {" + filterName + " TYPE:" + filter.filterType() + " ORDER:" + filter.filterOrder() + "} Execution time = " + execTime + "ms");
                    Debug.compareContextState(filterName, copy);
                }
                break;
            default:
                break;
        }
        if (t != null)
            throw t;
        usageNotifier.notify(filter, s);
        return o;
    } catch (Throwable e) {
        if (bDebug) {
            Debug.addRoutingDebug("Running Filter failed " + filterName + " type:" + filter.filterType() + " order:" + filter.filterOrder() + " " + e.getMessage());
        }
        usageNotifier.notify(filter, ExecutionStatus.FAILED);
        if (e instanceof ZuulException) {
            throw (ZuulException) e;
        } else {
            ZuulException ex = new ZuulException(e, "Filter threw Exception", 500, filter.filterType() + ":" + filterName);
            ctx.addFilterExecutionSummary(filterName, ExecutionStatus.FAILED.name(), execTime);
            throw ex;
        }
    }
}
Also used : ZuulException(com.netflix.zuul.exception.ZuulException) RequestContext(com.netflix.zuul.context.RequestContext)

Example 3 with RequestContext

use of com.netflix.zuul.context.RequestContext in project zuul by Netflix.

the class ZuulRunner method init.

/**
     * sets HttpServlet request and HttpResponse
     *
     * @param servletRequest
     * @param servletResponse
     */
public void init(HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
    RequestContext ctx = RequestContext.getCurrentContext();
    if (bufferRequests) {
        ctx.setRequest(new HttpServletRequestWrapper(servletRequest));
    } else {
        ctx.setRequest(servletRequest);
    }
    ctx.setResponse(new HttpServletResponseWrapper(servletResponse));
}
Also used : HttpServletRequestWrapper(com.netflix.zuul.http.HttpServletRequestWrapper) HttpServletResponseWrapper(com.netflix.zuul.http.HttpServletResponseWrapper) RequestContext(com.netflix.zuul.context.RequestContext)

Example 4 with RequestContext

use of com.netflix.zuul.context.RequestContext in project SpringCloudDemo by RickJou.

the class ErrorFilter method run.

@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    Object accessToken = request.getParameter("accessToken");
    if (accessToken == null) {
        ctx.setSendZuulResponse(false);
        ctx.setResponseStatusCode(401);
    }
    log.info("error filter......");
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestContext(com.netflix.zuul.context.RequestContext)

Example 5 with RequestContext

use of com.netflix.zuul.context.RequestContext in project topcom-cloud by 545314690.

the class AccessFilter method shouldFilter.

@Override
public boolean shouldFilter() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    // 跨域不拦截OPTIONS方法
    if (request.getMethod().equals(RequestMethod.OPTIONS.name())) {
        return false;
    }
    Optional<String> matched = config.getIngorePatterns().stream().filter(pattern -> {
        return pathMatcher.match(pattern, request.getRequestURI());
    }).findFirst();
    return !matched.isPresent();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestContext(com.netflix.zuul.context.RequestContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) PathMatcher(org.springframework.util.PathMatcher) ResponseData(com.topcom.zuulapi.vo.ResponseData) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Optional(java.util.Optional) AntPathMatcher(org.springframework.util.AntPathMatcher) ZuulFilter(com.netflix.zuul.ZuulFilter) Logger(java.util.logging.Logger) AuthClient(com.topcom.zuulapi.client.AuthClient) 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