Search in sources :

Example 81 with RequestContext

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

the class ApplicationHeadersFilter method run.

@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    String requestURI = this.urlPathHelper.getPathWithinApplication(ctx.getRequest());
    ApplicationRoute route = this.routeLocator.getMatchingRoute(requestURI);
    if (route != null) {
        HttpHeaders headers = headersProvider.getHeaders(route.getApplication());
        for (Entry<String, List<String>> header : headers.entrySet()) {
            ctx.addZuulRequestHeader(header.getKey(), header.getValue().get(0));
        }
    }
    return null;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) List(java.util.List) RequestContext(com.netflix.zuul.context.RequestContext) ApplicationRoute(de.codecentric.boot.admin.zuul.ApplicationRouteLocator.ApplicationRoute)

Example 82 with RequestContext

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

the class ZuulServlet method service.

@Override
public void service(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse) throws ServletException, IOException {
    try {
        init((HttpServletRequest) servletRequest, (HttpServletResponse) servletResponse);
        // Marks this request as having passed through the "Zuul engine", as opposed to servlets
        // explicitly bound in web.xml, for which requests will not have the same data attached
        RequestContext context = RequestContext.getCurrentContext();
        context.setZuulEngineRan();
        try {
            preRoute();
        } catch (ZuulException e) {
            error(e);
            postRoute();
            return;
        }
        try {
            route();
        } catch (ZuulException e) {
            error(e);
            postRoute();
            return;
        }
        try {
            postRoute();
        } catch (ZuulException e) {
            error(e);
            return;
        }
    } catch (Throwable e) {
        error(new ZuulException(e, 500, "UNHANDLED_EXCEPTION_" + e.getClass().getName()));
    } finally {
        RequestContext.getCurrentContext().unset();
    }
}
Also used : ZuulException(com.netflix.zuul.exception.ZuulException) RequestContext(com.netflix.zuul.context.RequestContext)

Example 83 with RequestContext

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

the class PostFilter 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("post filter......");
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestContext(com.netflix.zuul.context.RequestContext)

Example 84 with RequestContext

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

the class PreFilter 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("pre filter......");
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestContext(com.netflix.zuul.context.RequestContext)

Example 85 with RequestContext

use of com.netflix.zuul.context.RequestContext in project paascloud-master by paascloud.

the class RenewFilter method run.

/**
 * Run object.
 *
 * @return the object
 */
@Override
public Object run() {
    log.info("RenewFilter - token续租...");
    RequestContext requestContext = RequestContext.getCurrentContext();
    try {
        doSomething(requestContext);
    } catch (Exception e) {
        log.error("RenewFilter - token续租. [FAIL] EXCEPTION={}", e.getMessage(), e);
        throw new BusinessException(ErrorCodeEnum.UAC10011041);
    }
    return null;
}
Also used : BusinessException(com.paascloud.base.exception.BusinessException) RequestContext(com.netflix.zuul.context.RequestContext) BusinessException(com.paascloud.base.exception.BusinessException)

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