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();
}
}
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());
}
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());
}
Aggregations