res.sendRedirect(res.encodeRedirectURL(prefix + "/"));
return;
}
String contentType = null;
ContextMap ctxMap = null;
try {
try {
if (uri.charAt(0) == '/') {
uri = uri.substring(1);
}
Environment env = this.getEnvironment(uri, request, res);
// Initialize a fresh log context containing the object model : it
// will be used by the CocoonLogFormatter
ctxMap = ContextMap.getCurrentContext();
// Add thread name (default content for empty context)
String threadName = Thread.currentThread().getName();
ctxMap.set("threadName", threadName);
// Add the object model
ctxMap.set("objectModel", env.getObjectModel());
// Add a unique request id (threadName + currentTime
ctxMap.set("request-id", threadName + System.currentTimeMillis());
if (this.cocoon.process(env)) {
contentType = env.getContentType();
} else {
// Should not get here!
// means SC_NOT_FOUND
res.sendError(res.SC_NOT_FOUND);
SimpleNotifyingBean n = new SimpleNotifyingBean(this);
n.setType("error");
n.setTitle("Resource not found");
n.setSource("Cocoon servlet");
n.setMessage("The requested resource not found.");
n.setDescription("The requested URI \""
+ request.getRequestURI()
+ "\" was not found.");
n.addExtraDescription("request-uri", request.getRequestURI());
n.addExtraDescription("path-info", uri);
// send the notification but don't include it in the output stream
// as the status SC_NOT_FOUND is enough
res.setContentType(Notifier.notify(n, (OutputStream)null));
}
} catch (ResourceNotFoundException rse) {
if (log.isWarnEnabled()) {
log.warn("The resource was not found", rse);
}
res.sendError(res.SC_NOT_FOUND);
SimpleNotifyingBean n = new SimpleNotifyingBean(this);
n.setType("resource-not-found");
n.setTitle("Resource not found");
n.setSource("Cocoon servlet");
n.setMessage("Resource not found");
n.setDescription("The requested URI \""
+ request.getRequestURI()
+ "\" was not found.");
n.addExtraDescription("request-uri", request.getRequestURI());
n.addExtraDescription("path-info", uri);
StringWriter writer = new StringWriter();
rse.printStackTrace(new PrintWriter(writer));
n.addExtraDescription("stack-trace",writer.toString());
// send the notification but don't include it in the output stream
// as the status SC_NOT_FOUND is enough
res.setContentType(Notifier.notify(n, (OutputStream)null));
} catch (ConnectionResetException cre) {
if (log.isWarnEnabled()) {
log.warn("The connection was reset", cre);
}
SimpleNotifyingBean n = new SimpleNotifyingBean(this);
n.setType("error");
n.setTitle("Resource not found");
n.setSource("Cocoon servlet");
n.setMessage("Resource not found");
n.setDescription("The requested URI \""
+ request.getRequestURI()
+ "\" was not found.");
n.addExtraDescription("request-uri", request.getRequestURI());
n.addExtraDescription("path-info", uri);
// send the notification but don't include it in the output stream
// as the connection was reset anyway
res.setContentType(Notifier.notify(n, (OutputStream)null));
} catch (Exception e) {
if (log.isErrorEnabled()) {
log.error("Problem with servlet", e);
}
//res.setStatus(res.SC_INTERNAL_SERVER_ERROR);
HashMap extraDescriptions = new HashMap(3);
extraDescriptions.put("request-uri", request.getRequestURI());
extraDescriptions.put("path-info", uri);
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
extraDescriptions.put("stack-trace",writer.toString());
Notifying n=new DefaultNotifyingBuilder().build(
this, e, "fatal","Internal server error","Cocoon servlet",null,null,extraDescriptions);
res.setContentType(contentType = Notifier.notify(n, res.getOutputStream()));
}
long end = System.currentTimeMillis();
String timeString = processTime(end - start);
if (log.isInfoEnabled()) {
log.info("'" + uri + "' " + timeString);
}
if (contentType != null && contentType.equals("text/html")) {
String showTime = request.getParameter(Constants.SHOWTIME_PARAM);
boolean show = this.showTime;
if (showTime != null) {
show = !showTime.equalsIgnoreCase("no");
}
if (show) {
boolean hide = this.hiddenShowTime;
if (showTime != null) {
hide = showTime.equalsIgnoreCase("hide");
}
ServletOutputStream out = res.getOutputStream();
out.print((hide) ? "<!-- " : "<p>");
out.print(timeString);
out.println((hide) ? " -->" : "</p>");
out.flush();
out.close();
}
}
} finally {
if (ctxMap != null) ctxMap.clear();
}
}