Skip to content

Commit

Permalink
Ensure post filters always run
Browse files Browse the repository at this point in the history
If the pre-filters block a request from continuing the post filters do
not run.
  • Loading branch information
janderson2 committed Feb 18, 2022
1 parent 76b499d commit 4a1208b
Showing 1 changed file with 9 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,42 +135,24 @@ public void reload() throws IOException {
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

// Should we try redirecting to index.html?
boolean isRootRequest = isRootRequest(request);
boolean isApiRequest = isApiRequest(target);
if (preFilter(request, response)) {
if (isApiRequest) {
try {
try {
if (preFilter(request, response)) {
if (isApiRequest) {
apiHandler.handle(target, baseRequest, request, response);
} finally {
postFilters.stream().forEach(pf -> pf.filter(request, response));
} else if (filesHandler != null) {
filesHandler.handle(target, baseRequest, request, response);
} else {
notFound(target, response);
}
} else if (filesHandler != null) {
filesHandler.handle(target, baseRequest, request, response);
} else {
notFound(target, response);
}
} finally {
postFilters.stream().forEach(pf -> pf.filter(request, response));
}

baseRequest.setHandled(true);
}

/**
* Determines if the given request is for the root resource (ie /).
*
* @param request The {@link HttpServletRequest}
* @return If {@link HttpServletRequest#getPathInfo()} is null, empty string
* or "/", then true.
*/
boolean isRootRequest(HttpServletRequest request) {
String path = request.getPathInfo();
if (StringUtils.isBlank(path)) {
return true;
} else if (StringUtils.equals("/", path)) {
return true;
}
return false;
}

static boolean isApiRequest(String target) {
String extension = FilenameUtils.getExtension(target);
return StringUtils.isBlank(extension);
Expand Down

0 comments on commit 4a1208b

Please sign in to comment.