Search in sources :

Example 1 with UrlCleaner

use of com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner in project Sentinel by alibaba.

the class CommonFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest sRequest = (HttpServletRequest) request;
    Entry urlEntry = null;
    try {
        String target = FilterUtil.filterTarget(sRequest);
        // Clean and unify the URL.
        // For REST APIs, you have to clean the URL (e.g. `/foo/1` and `/foo/2` -> `/foo/:id`), or
        // the amount of context and resources will exceed the threshold.
        UrlCleaner urlCleaner = WebCallbackManager.getUrlCleaner();
        if (urlCleaner != null) {
            target = urlCleaner.clean(target);
        }
        // in the UrlCleaner implementation.
        if (!StringUtil.isEmpty(target)) {
            // Parse the request origin using registered origin parser.
            String origin = parseOrigin(sRequest);
            String contextName = webContextUnify ? WebServletConfig.WEB_SERVLET_CONTEXT_NAME : target;
            ContextUtil.enter(contextName, origin);
            if (httpMethodSpecify) {
                // Add HTTP method prefix if necessary.
                String pathWithHttpMethod = sRequest.getMethod().toUpperCase() + COLON + target;
                urlEntry = SphU.entry(pathWithHttpMethod, ResourceTypeConstants.COMMON_WEB, EntryType.IN);
            } else {
                urlEntry = SphU.entry(target, ResourceTypeConstants.COMMON_WEB, EntryType.IN);
            }
        }
        chain.doFilter(request, response);
    } catch (BlockException e) {
        HttpServletResponse sResponse = (HttpServletResponse) response;
        // Return the block page, or redirect to another URL.
        WebCallbackManager.getUrlBlockHandler().blocked(sRequest, sResponse, e);
    } catch (IOException | ServletException | RuntimeException e2) {
        Tracer.traceEntry(e2, urlEntry);
        throw e2;
    } finally {
        if (urlEntry != null) {
            urlEntry.exit();
        }
        ContextUtil.exit();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Entry(com.alibaba.csp.sentinel.Entry) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) UrlCleaner(com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException)

Example 2 with UrlCleaner

use of com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner in project Sentinel by alibaba.

the class CommonFilterTest method testUrlCleaner.

private void testUrlCleaner() throws Exception {
    final String fooPrefix = "/foo/";
    String url1 = fooPrefix + 1;
    String url2 = fooPrefix + 2;
    WebCallbackManager.setUrlCleaner(new UrlCleaner() {

        @Override
        public String clean(String originUrl) {
            if (originUrl.startsWith(fooPrefix)) {
                return "/foo/*";
            }
            return originUrl;
        }
    });
    this.mvc.perform(get(url1).accept(MediaType.TEXT_PLAIN)).andExpect(status().isOk()).andExpect(content().string("Hello 1"));
    this.mvc.perform(get(url2).accept(MediaType.TEXT_PLAIN)).andExpect(status().isOk()).andExpect(content().string("Hello 2"));
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(fooPrefix + "*");
    assertEquals(2, cn.passQps(), 0.01);
    assertNull(ClusterBuilderSlot.getClusterNode(url1));
    assertNull(ClusterBuilderSlot.getClusterNode(url2));
    WebCallbackManager.setUrlCleaner(new DefaultUrlCleaner());
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) DefaultUrlCleaner(com.alibaba.csp.sentinel.adapter.servlet.callback.DefaultUrlCleaner) UrlCleaner(com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner) DefaultUrlCleaner(com.alibaba.csp.sentinel.adapter.servlet.callback.DefaultUrlCleaner)

Example 3 with UrlCleaner

use of com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner in project Sentinel by alibaba.

the class CommonFilterTest method testUrlExclusion.

private void testUrlExclusion() throws Exception {
    final String excludePrefix = "/exclude/";
    String url = excludePrefix + 1;
    WebCallbackManager.setUrlCleaner(new UrlCleaner() {

        @Override
        public String clean(String originUrl) {
            if (originUrl.startsWith(excludePrefix)) {
                return "";
            }
            return originUrl;
        }
    });
    this.mvc.perform(get(url).accept(MediaType.TEXT_PLAIN)).andExpect(status().isOk()).andExpect(content().string("Exclude 1"));
    assertNull(ClusterBuilderSlot.getClusterNode(url));
    WebCallbackManager.setUrlCleaner(new DefaultUrlCleaner());
}
Also used : DefaultUrlCleaner(com.alibaba.csp.sentinel.adapter.servlet.callback.DefaultUrlCleaner) UrlCleaner(com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner) DefaultUrlCleaner(com.alibaba.csp.sentinel.adapter.servlet.callback.DefaultUrlCleaner)

Aggregations

UrlCleaner (com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner)3 DefaultUrlCleaner (com.alibaba.csp.sentinel.adapter.servlet.callback.DefaultUrlCleaner)2 Entry (com.alibaba.csp.sentinel.Entry)1 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)1 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1