use of com.disney.groovity.servlet.GroovityScriptView.Processor in project groovity by disney.
the class GroovityServlet method service.
/**
* Primary request entry method for this servlet
*
* see {@link HttpServlet#service(HttpServletRequest, HttpServletResponse)}
*/
@Override
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
GroovityError error = null;
try {
Processor processor = groovityScriptViewFactory.createProcessor(req);
if (processor != null) {
if (req.getMethod().equals("HEAD")) {
// prevent any body from being written, but we'll capture
// the length
final AtomicLong length = new AtomicLong(0);
final AtomicBoolean scriptSetLength = new AtomicBoolean(false);
final AtomicReference<PrintWriter> respWriter = new AtomicReference<PrintWriter>(null);
final ServletOutputStream nullStream = new ServletOutputStream() {
@Override
public void write(int b) throws IOException {
length.incrementAndGet();
}
@Override
public void write(byte[] buf, int offset, int len) throws IOException {
length.addAndGet(len);
}
public void setWriteListener(WriteListener arg0) {
}
public boolean isReady() {
return true;
}
};
processor.process(req, new HttpServletResponseWrapper(res) {
PrintWriter writer;
@Override
public ServletOutputStream getOutputStream() throws IOException {
return nullStream;
}
@Override
public PrintWriter getWriter() throws UnsupportedEncodingException {
if (writer == null) {
writer = new PrintWriter(new OutputStreamWriter(nullStream, getCharacterEncoding()));
respWriter.set(writer);
}
return writer;
}
@Override
public void setContentLength(int len) {
res.setContentLength(len);
scriptSetLength.set(true);
}
});
if (!scriptSetLength.get()) {
if (respWriter.get() != null) {
respWriter.get().flush();
}
res.setContentLength((int) length.get());
}
} else {
processor.process(req, res);
}
} else {
error = new GroovityError();
Object acceptMethods = req.getAttribute(REQUEST_ATTRIBUTE_ALLOW_METHODS);
if (acceptMethods != null && acceptMethods instanceof Collection) {
@SuppressWarnings("unchecked") Collection<String> am = (Collection<String>) acceptMethods;
String sep = "";
StringBuilder sb = new StringBuilder();
for (Object m : am) {
String method = m.toString();
sb.append(sep).append(method);
sep = ", ";
}
res.setHeader("Allow", sb.toString());
error.setStatus(405);
error.setMessage("Allowed methods for " + req.getRequestURI() + " are " + sb.toString());
} else {
error.setStatus(404);
}
}
} catch (Throwable e) {
error = new GroovityError();
error.setCause(e);
error.setStatus(500);
}
if (error != null) {
error.setReason(EnglishReasonPhraseCatalog.INSTANCE.getReason(error.getStatus(), res.getLocale()));
String uri = req.getRequestURI();
if (req.getQueryString() != null) {
uri = uri.concat("?").concat(req.getQueryString());
}
error.setUri(uri);
req.setAttribute(GroovityScriptView.GROOVITY_ERROR, error);
boolean handled = false;
GroovityErrorHandlerChain handlers = groovityScriptViewFactory.getErrorHandlers();
if (handlers != null) {
handled = handlers.handleError(req, res, error);
}
if (!handled) {
Throwable cause = error.getCause();
if (cause != null) {
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
}
throw new ServletException(cause);
} else {
res.sendError(error.getStatus(), error.getMessage());
}
}
}
}
Aggregations