Exemplo n.º 1
0
  /**
   * Internal method that allows a redirect to be sent with a status other than {@link
   * HttpServletResponse#SC_FOUND} (302). No attempt is made to validate the status code.
   */
  public void sendRedirect(String location, int status) throws IOException {
    if (isCommitted()) {
      throw new IllegalStateException(sm.getString("coyoteResponse.sendRedirect.ise"));
    }

    // Ignore any call from an included servlet
    if (included) {
      return;
    }

    // Clear any data content that has been buffered
    resetBuffer(true);

    // Generate a temporary redirect to the specified location
    try {
      String absolute = toAbsolute(location);
      setStatus(status);
      setHeader("Location", absolute);
      if (getContext().getSendRedirectBody()) {
        PrintWriter writer = getWriter();
        writer.print(
            sm.getString("coyoteResponse.sendRedirect.note", RequestUtil.filter(absolute)));
        flushBuffer();
      }
    } catch (IllegalArgumentException e) {
      setStatus(SC_NOT_FOUND);
    }

    // Cause the response to be finished (from the application perspective)
    setSuspended(true);
  }
  /**
   * Handle an HTTP status code or Java exception by forwarding control to the location included in
   * the specified errorPage object. It is assumed that the caller has already recorded any request
   * attributes that are to be forwarded to this page. Return <code>true</code> if we successfully
   * utilized the specified error page location, or <code>false</code> if the default error report
   * should be rendered.
   *
   * @param request The request being processed
   * @param response The response being generated
   * @param errorPage The errorPage directive we are obeying
   */
  private boolean custom(Request request, Response response, ErrorPage errorPage) {

    if (container.getLogger().isDebugEnabled())
      container.getLogger().debug("Processing " + errorPage);

    try {
      // Forward control to the specified location
      ServletContext servletContext = request.getContext().getServletContext();
      RequestDispatcher rd = servletContext.getRequestDispatcher(errorPage.getLocation());

      if (response.isCommitted()) {
        // Response is committed - including the error page is the
        // best we can do
        rd.include(request.getRequest(), response.getResponse());
      } else {
        // Reset the response (keeping the real error code and message)
        response.resetBuffer(true);
        response.setContentLength(-1);

        rd.forward(request.getRequest(), response.getResponse());

        // If we forward, the response is suspended again
        response.setSuspended(false);
      }

      // Indicate that we have successfully processed this custom page
      return (true);

    } catch (Throwable t) {
      ExceptionUtils.handleThrowable(t);
      // Report our failure to process this custom page
      container.getLogger().error("Exception Processing " + errorPage, t);
      return (false);
    }
  }
  @Override
  public void resetBuffer() {

    if (isCommitted())
      throw new IllegalStateException(sm.getString("coyoteResponse.resetBuffer.ise"));

    response.resetBuffer();
  }
Exemplo n.º 4
0
  /**
   * Send an error response with the specified status and message.
   *
   * @param status HTTP status code to send
   * @param message Corresponding message to send
   * @exception IllegalStateException if this response has already been committed
   * @exception IOException if an input/output error occurs
   */
  @Override
  public void sendError(int status, String message) throws IOException {

    if (isCommitted()) {
      throw new IllegalStateException(sm.getString("coyoteResponse.sendError.ise"));
    }

    // Ignore any call from an included servlet
    if (included) {
      return;
    }

    setError();

    coyoteResponse.setStatus(status);
    coyoteResponse.setMessage(message);

    // Clear any data content that has been buffered
    resetBuffer();

    // Cause the response to be finished (from the application perspective)
    setSuspended(true);
  }
Exemplo n.º 5
0
 /**
  * Reset the data buffer but not any status or header information.
  *
  * @exception IllegalStateException if the response has already been committed
  */
 @Override
 public void resetBuffer() {
   resetBuffer(false);
 }