use of com.disney.groovity.servlet.error.GroovityError in project groovity by disney.
the class GroovityScriptView method getGroovyError.
protected GroovityError getGroovyError(HttpServletRequest request) {
GroovityError groovyError = (GroovityError) request.getAttribute(GROOVITY_ERROR);
if (groovyError == null) {
groovyError = new GroovityError();
groovyError.setScriptClass(this.scriptClass);
groovyError.setScriptPath(this.name);
String uri = request.getRequestURI();
if (request.getQueryString() != null) {
uri = uri.concat("?").concat(request.getQueryString());
}
groovyError.setUri(uri);
request.setAttribute(GROOVITY_ERROR, groovyError);
groovyError.setLogger(((GroovityClassLoader) scriptClass.getClassLoader()).getLogger());
}
return groovyError;
}
use of com.disney.groovity.servlet.error.GroovityError 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());
}
}
}
}
use of com.disney.groovity.servlet.error.GroovityError in project groovity by disney.
the class GroovityServletResponseWrapper method sendError.
public void sendError(int sc, String message) throws IOException {
HttpServletResponse response = (HttpServletResponse) getResponse();
boolean handled = false;
GroovityError error = groovityScriptView.getGroovyError(request);
if (message != null) {
error.setMessage(message);
}
error.setStatus(sc);
error.setReason(EnglishReasonPhraseCatalog.INSTANCE.getReason(sc, getLocale()));
response.setStatus(sc);
if (groovityScriptView.errorHandlers != null) {
try {
handled = groovityScriptView.errorHandlers.handleError(request, response, error);
// System.out.println("Handled sendError: "+handled);
} catch (ServletException e) {
throw new IOException(e);
}
}
if (!handled) {
// System.out.println("Sending error as chain did not handle "+sc+"
// : "+message);
response.sendError(sc, message);
}
}
Aggregations