use of spark.route.RouteMatch in project concourse by cinchapi.
the class MatcherFilter method doFilter.
public void doFilter(ServletRequest servletRequest, // NOSONAR
ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
// NOSONAR
// NOSONAR
HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
// NOSONAR
String httpMethodStr = httpRequest.getMethod().toLowerCase();
// NOSONAR
String uri = httpRequest.getRequestURI();
String acceptType = httpRequest.getHeader(ACCEPT_TYPE_REQUEST_MIME_HEADER);
String bodyContent = null;
if (!isStaticFileRequest(servletRequest, servletResponse, chain)) {
RequestWrapper req = new RequestWrapper();
ResponseWrapper res = new ResponseWrapper();
try {
// BEFORE filters
List<RouteMatch> matchSet = routeMatcher.findTargetsForRequestedRoute(HttpMethod.before, uri, acceptType);
for (RouteMatch filterMatch : matchSet) {
Object filterTarget = filterMatch.getTarget();
if (filterTarget instanceof spark.Filter) {
Request request = RequestResponseFactory.create(filterMatch, httpRequest);
Response response = RequestResponseFactory.create(httpResponse);
spark.Filter filter = (spark.Filter) filterTarget;
req.setDelegate(request);
res.setDelegate(response);
filter.handle(req, res);
String bodyAfterFilter = Access.getBody(response);
if (bodyAfterFilter != null) {
bodyContent = bodyAfterFilter;
}
}
}
// BEFORE filters, END
HttpMethod httpMethod = HttpMethod.valueOf(httpMethodStr);
RouteMatch match = null;
match = routeMatcher.findTargetForRequestedRoute(httpMethod, uri, acceptType);
Object target = null;
if (match != null) {
target = match.getTarget();
} else if (httpMethod == HttpMethod.head && bodyContent == null) {
// See if get is mapped to provide default head mapping
bodyContent = routeMatcher.findTargetForRequestedRoute(HttpMethod.get, uri, acceptType) != null ? "" : null;
} else if (httpMethod == HttpMethod.options && bodyContent == null) {
// CON-476: For an OPTIONS request, attempt to get all the
// targets for the route and specify those in the response
Set<HttpMethod> methods = routeMatcher.findMethodsForRequestedPath(uri, acceptType);
if (!methods.isEmpty()) {
httpResponse.setHeader("Allow", StringUtils.join(methods, ','));
bodyContent = "";
}
}
if (target != null) {
try {
String result = null;
if (target instanceof Route) {
Route route = ((Route) target);
Request request = RequestResponseFactory.create(match, httpRequest);
Response response = RequestResponseFactory.create(httpResponse);
req.setDelegate(request);
res.setDelegate(response);
Object element = route.handle(req, res);
result = route.render(element);
}
if (result != null) {
bodyContent = result;
}
} catch (HaltException hEx) {
// NOSONAR
throw hEx;
} catch (Exception e) {
Logger.error("", e);
httpResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
bodyContent = INTERNAL_ERROR;
}
}
// AFTER filters
matchSet = routeMatcher.findTargetsForRequestedRoute(HttpMethod.after, uri, acceptType);
for (RouteMatch filterMatch : matchSet) {
Object filterTarget = filterMatch.getTarget();
if (filterTarget instanceof spark.Filter) {
Request request = RequestResponseFactory.create(filterMatch, httpRequest);
Response response = RequestResponseFactory.create(httpResponse);
req.setDelegate(request);
res.setDelegate(response);
spark.Filter filter = (spark.Filter) filterTarget;
filter.handle(req, res);
String bodyAfterFilter = Access.getBody(response);
if (bodyAfterFilter != null) {
bodyContent = bodyAfterFilter;
}
}
}
// AFTER filters, END
} catch (HaltException hEx) {
httpResponse.setStatus(hEx.getStatusCode());
if (hEx.getBody() != null) {
bodyContent = hEx.getBody();
} else {
bodyContent = "";
}
}
}
boolean consumed = bodyContent != null;
if (!consumed && hasOtherHandlers) {
throw new NotConsumedException();
}
if (!consumed && !isServletContext) {
httpResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
bodyContent = String.format(NOT_FOUND, uri);
consumed = true;
}
if (consumed) {
// Write body content
if (!httpResponse.isCommitted()) {
if (httpResponse.getContentType() == null) {
httpResponse.setContentType("text/html; charset=utf-8");
}
httpResponse.getOutputStream().write(bodyContent.getBytes("utf-8"));
}
} else if (chain != null) {
chain.doFilter(httpRequest, httpResponse);
}
}
Aggregations