public void service(HttpServletRequest servletRequest,
HttpServletResponse servletResponse) throws IOException {
// setting the Sling request and response
final RequestData requestData = new RequestData(this, servletRequest,
servletResponse);
SlingHttpServletRequest request = requestData.getSlingRequest();
SlingHttpServletResponse response = requestData.getSlingResponse();
// request entry log
if (requestLogger != null) {
requestLogger.logRequestEntry(request, response);
}
Session session = null;
try {
// check that we have all required services
String missing = null;
if (getResourceResolverFactory() == null) {
missing = "ResourceResolverFactory";
} else if (getServletResolver() == null) {
missing = "ServletResolver";
} else if (mimeTypeService == null) {
missing = "MimeTypeService";
}
if (missing != null) {
final String err = missing
+ " service missing, cannot service requests";
final int status = HttpServletResponse.SC_SERVICE_UNAVAILABLE;
log.error("{} , sending status {}", err, status);
sendError(status, err, null, servletRequest, servletResponse);
return;
}
// get JCR Session
session = (Session) servletRequest.getAttribute(SESSION);
if (session == null) {
log.error("service: Cannot handle request: Missing JCR Session");
sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"Missing JCR Session", null, servletRequest,
servletResponse);
return;
}
// initialize the request data - resolve resource and servlet
ResourceResolver resolver = getResourceResolverFactory().getResourceResolver(
session);
Resource resource = requestData.initResource(resolver);
requestData.initServlet(resource);
Filter[] filters = requestFilterChain.getFilters();
if (filters != null) {
FilterChain processor = new RequestSlingFilterChain(this,
filters);
request.getRequestProgressTracker().log(
"Applying request filters");
processor.doFilter(request, response);
} else {
// no filters, directly call resource level filters and servlet
processRequest(request, response);
}
} catch (ResourceNotFoundException rnfe) {
// send this exception as a 404 status
log.info("service: Resource {} not found", rnfe.getResource());
getErrorHandler().handleError(HttpServletResponse.SC_NOT_FOUND,
rnfe.getMessage(), request, response);
} catch (SlingException se) {
// if we have request data and a non-null active servlet name
// we assume, that this is the name of the causing servlet
if (requestData.getActiveServletName() != null) {
request.setAttribute(ERROR_SERVLET_NAME,
requestData.getActiveServletName());
}
// send this exception as is (albeit unwrapping and wrapped
// exception.
Throwable t = (se.getCause() != null) ? se.getCause() : se;
log.error("service: Uncaught SlingException", t);
getErrorHandler().handleError(t, request, response);
} catch (AccessControlException ace) {
// SLING-319 if anything goes wrong, send 403/FORBIDDEN
log.info(
"service: Authenticated user {} does not have enough rights to executed requested action",
request.getRemoteUser());
getErrorHandler().handleError(HttpServletResponse.SC_FORBIDDEN,
null, request, response);
} catch (Throwable t) {
// if we have request data and a non-null active servlet name
// we assume, that this is the name of the causing servlet
if (requestData.getActiveServletName() != null) {
request.setAttribute(ERROR_SERVLET_NAME,
requestData.getActiveServletName());
}
log.error("service: Uncaught Throwable", t);
getErrorHandler().handleError(t, request, response);
} finally {
// request exit log
if (requestLogger != null) {
requestLogger.logRequestExit(request, response);
}
// dispose any request data
requestData.dispose();
// logout the session we have got for this request
if (session != null) {
session.logout();
}