public void service()

in core/cocoon-core/src/main/java/org/apache/cocoon/servlet/RequestProcessor.java [129:281]


    public void service(HttpServletRequest request, HttpServletResponse res)
    throws ServletException, IOException {
        // used for timing the processing
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        // add the cocoon version header stamp
        if (this.servletSettings.isShowVersion()) {
            res.addHeader("X-Cocoon-Version", Constants.VERSION);
        }

        // We got it... Process the request
        final String uri = getURI(request, res);
        if (uri == null) {
            // a redirect occured, so we are finished
            return;
        }

        Environment env;
        try{
            // Pass uri into environment without URLDecoding, as it is already decoded.
            env = getEnvironment(uri, request, res);
        } catch (Exception e) {
            if (getLogger().isErrorEnabled()) {
                getLogger().error("Problem with Cocoon servlet", e);
            }

            if (rethrowExceptions()) {
                throw new ServletException(e);
            }

            RequestUtil.manageException(request, res, null, uri,
                                        HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                                        "Problem in creating the Environment", null, null, e,
                                        this.servletSettings, getLogger(), this);
            return;
        }

        String contentType = null;
        try {
            if (process(env)) {
                contentType = env.getContentType();
            } else {
                // We reach this when there is nothing in the processing chain that matches
                // the request. For example, no matcher matches.
                getLogger().fatal("The Cocoon engine failed to process the request.");

                if (rethrowExceptions()) {
                    throw new ServletException("The Cocoon engine failed to process the request.");
                }

                RequestUtil.manageException(request, res, env, uri,
                                            HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                                            "Request Processing Failed",
                                            "Cocoon engine failed in processing the request",
                                            "The processing engine failed to process the request. This could be due to lack of matching or bugs in the pipeline engine.",
                                            null,
                                            this.servletSettings, getLogger(), this);
                return;
            }
        } catch (ResourceNotFoundException e) {
            if (getLogger().isDebugEnabled()) {
                getLogger().warn(e.getMessage(), e);
            } else if (getLogger().isWarnEnabled()) {
                getLogger().warn(e.getMessage());
            }

            RequestUtil.manageException(request, res, env, uri,
                                        HttpServletResponse.SC_NOT_FOUND,
                                        "Resource Not Found",
                                        "Resource Not Found",
                                        "The requested resource \"" + request.getRequestURI() + "\" could not be found",
                                        e,
                                        this.servletSettings, getLogger(), this);
            return;

        } catch (ConnectionResetException e) {
            if (getLogger().isDebugEnabled()) {
                getLogger().debug(e.toString(), e);
            } else if (getLogger().isWarnEnabled()) {
                getLogger().warn(e.toString());
            }

        } catch (IOException e) {
            // Tomcat5 wraps SocketException into ClientAbortException which extends IOException.
            if (getLogger().isDebugEnabled()) {
                getLogger().debug(e.toString(), e);
            } else if (getLogger().isWarnEnabled()) {
                getLogger().warn(e.toString());
            }

        } catch (Exception e) {
            if (getLogger().isErrorEnabled()) {
                getLogger().error("Internal Cocoon Problem", e);
            }

            if (rethrowExceptions()) {
                throw new ServletException(e);
            }

            RequestUtil.manageException(request, res, env, uri,
                                        HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                                        "Internal Server Error", null, null, e,
                                        this.servletSettings, getLogger(), this);
            return;
        }

        stopWatch.stop();
        String timeString = null;
        if (getLogger().isInfoEnabled()) {
            timeString = processTime(stopWatch.getTime());
            getLogger().info("'" + uri + "' " + timeString);
        }

        if (contentType != null && contentType.equals("text/html")) {
            String showTime = request.getParameter(Constants.SHOWTIME_PARAM);
            boolean show = this.servletSettings.isShowTime();
            if (showTime != null) {
                show = !showTime.equalsIgnoreCase("no");
            }
            if (show) {
                if (timeString == null) {
                    timeString = processTime(stopWatch.getTime());
                }
                boolean hide = this.servletSettings.isHideShowTime();
                if (showTime != null) {
                    hide = showTime.equalsIgnoreCase("hide");
                }
                ServletOutputStream out = res.getOutputStream();
                out.print((hide) ? "<!-- " : "<p>");
                out.print(timeString);
                out.println((hide) ? " -->" : "</p>");
            }
        }

        /*
         * Servlet Specification 2.2, 6.5 Closure of Response Object:
         *
         *   A number of events can indicate that the servlet has provided all of the
         *   content to satisfy the request and that the response object can be
         *   considered to be closed. The events are:
         *     o The termination of the service method of the servlet.
         *     o When the amount of content specified in the setContentLength method
         *       of the response has been written to the response.
         *     o The sendError method is called.
         *     o The sendRedirect method is called.
         *   When a response is closed, all content in the response buffer, if any remains,
         *   must be immediately flushed to the client.
         *
         * Due to the above, out.flush() and out.close() are not necessary, and sometimes
         * (if sendError or sendRedirect were used) request may be already closed.
         */
    }