String path = request.getPath();
if (log.isDebugEnabled()) log.debug("Processing path '" + path + "'" );
ListIterator<Middleware> middlewareIterator=settings.getMiddlewares().listIterator();
for (; middlewareIterator.hasNext(); ) {
Middleware m = middlewareIterator.next();
HttpResponse r = m.processRequest(request);
if (r != null) {
return r;
}
}
HttpResponse response = null;
Urls urls = getUrls();
for(Url url: urls.getUrlPatterns()) {
if (url.matches(path)) {
try {
MethodObjectParams mop = url.getMethodObjectParams(path, request);
for (Middleware m : settings.getMiddlewares()) {
m.processMethodAndParams(request, mop);
}
// TODO Move somewhere else
for (Annotation annotation : mop.getMethod().getAnnotations()) {
// TODO Should this return the response, or let it fall through to the middleware?
if (annotation instanceof LoginRequired) {
if (request.getUser() == null || request.getUser() instanceof AnonymousUser) {
response = new SimpleHttpResponse("Login Required");
}
} else if (annotation instanceof RoleRequired) {
RoleRequired rr = (RoleRequired)annotation;
if (request.getUser() == null || request.getUser() instanceof AnonymousUser || !request.getUser().hasRole(rr.role())) {
response = new SimpleHttpResponse("Not Authorized");
}
}
}
if (response == null ) response = invoke(mop, injector);
break;
} catch (Exception e) {
for (; middlewareIterator.hasPrevious(); ) {
Middleware m = middlewareIterator.previous();
response = m.processException(request, response, e);
}
// got to be a better solution to this...
//middlewareIterator=settings.getMiddlewares().listIterator();
for (; middlewareIterator.hasNext(); middlewareIterator.next()) ;
if( response == null) {
if (e instanceof HttpException) {
throw (HttpException)e;
}
throw new HttpException(e);
}
}
}
}
if (response != null && response.getContext() != null) {
// TODO move message handling to template context processor
// should be an object that will automatically remove messages/errors if they are read from the template
response.getContext().put("messages", request.getSession().getAndClearMessages());
response.getContext().put("errors", request.getSession().getAndClearErrors());
response.getContext().put("absolute_path", request.getContext());
String media_url = settings.getSettings().get("MEDIA_URL");
if (media_url != null) {
response.getContext().put("MEDIA_URL", media_url);
}
for (ContextProcessor rc : settings.getContextProcessors() ) {
response.getContext().putAll(rc.evaluate(request));
}
}
for (; middlewareIterator.hasPrevious(); ) {
Middleware m = middlewareIterator.previous();
response = m.processResponse(request, response);
}
if (response != null) {
return response;
}