Search in sources :

Example 1 with Processor

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());
            }
        }
    }
}
Also used : Processor(com.disney.groovity.servlet.GroovityScriptView.Processor) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) GroovityError(com.disney.groovity.servlet.error.GroovityError) ServletException(javax.servlet.ServletException) WriteListener(javax.servlet.WriteListener) PrintWriter(java.io.PrintWriter) GroovityErrorHandlerChain(com.disney.groovity.servlet.error.GroovityErrorHandlerChain) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) Collection(java.util.Collection) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

Processor (com.disney.groovity.servlet.GroovityScriptView.Processor)1 GroovityError (com.disney.groovity.servlet.error.GroovityError)1 GroovityErrorHandlerChain (com.disney.groovity.servlet.error.GroovityErrorHandlerChain)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Collection (java.util.Collection)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ServletException (javax.servlet.ServletException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 WriteListener (javax.servlet.WriteListener)1 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)1