Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the feature of excluding unused URLs in CommonFilter (#287) #914

Merged
merged 3 commits into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
target = urlCleaner.clean(target);
}

// Parse the request origin using registered origin parser.
String origin = parseOrigin(sRequest);

ContextUtil.enter(target, origin);
entry = SphU.entry(target, EntryType.IN);


// Add method specification if necessary
if (httpMethodSpecify) {
methodEntry = SphU.entry(sRequest.getMethod().toUpperCase() + COLON + target,
EntryType.IN);
// If you intend to exclude some URLs, you can convert the URLs to the empty string ""
// in the UrlCleaner implementation.
if (target != "") {
sczyh30 marked this conversation as resolved.
Show resolved Hide resolved
// Parse the request origin using registered origin parser.
String origin = parseOrigin(sRequest);
ContextUtil.enter(target, origin);
entry = SphU.entry(target, EntryType.IN);
// Add method specification if necessary
if (httpMethodSpecify) {
methodEntry = SphU.entry(sRequest.getMethod().toUpperCase() + COLON + target,
EntryType.IN);
}
}

chain.doFilter(request, response);
} catch (BlockException e) {
HttpServletResponse sResponse = (HttpServletResponse) response;
Expand All @@ -107,7 +107,9 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
if (entry != null) {
entry.exit();
}
ContextUtil.exit();
if (ContextUtil.getContext() != null) {
sczyh30 marked this conversation as resolved.
Show resolved Hide resolved
ContextUtil.exit();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void testCommonFilterMiscellaneous() throws Exception {

// Test for url cleaner.
testUrlCleaner();

testUrlExclusion();
testCustomOriginParser();
}

Expand Down Expand Up @@ -139,6 +139,25 @@ public String clean(String originUrl) {
WebCallbackManager.setUrlCleaner(new DefaultUrlCleaner());
}

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

private void testCustomOriginParser() throws Exception {
String url = "/hello";
String limitOrigin = "userA";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public String apiError() {
public String apiFoo(@PathVariable("id") Long id) {
return "Hello " + id;
}

@GetMapping("/exclude/{id}")
public String apiExclude(@PathVariable("id") Long id) {
return "Exclude " + id;
}
}