Search in sources :

Example 1 with ErrorBean

use of org.alfresco.web.bean.ErrorBean in project acs-community-packaging by Alfresco.

the class AlfrescoFacesPortlet method handleError.

/**
 * Handles errors that occur during a render request
 */
private void handleError(RenderRequest request, RenderResponse response, Throwable error) throws PortletException, IOException {
    // get the error bean from the session and set the error that occurred.
    PortletSession session = request.getPortletSession();
    ErrorBean errorBean = (ErrorBean) session.getAttribute(ErrorBean.ERROR_BEAN_NAME, PortletSession.PORTLET_SCOPE);
    if (errorBean == null) {
        errorBean = new ErrorBean();
        session.setAttribute(ErrorBean.ERROR_BEAN_NAME, errorBean, PortletSession.PORTLET_SCOPE);
    }
    errorBean.setLastError(error);
    // if the faces context is available set the current view to the browse page
    // so that the error page goes back to the application (rather than going back
    // to the same page which just throws the error again meaning we can never leave
    // the error page)
    FacesContext context = FacesContext.getCurrentInstance();
    if (context != null) {
        ViewHandler viewHandler = context.getApplication().getViewHandler();
        // TODO: configure the portlet error return page
        UIViewRoot view = viewHandler.createView(context, FacesHelper.BROWSE_VIEW_ID);
        context.setViewRoot(view);
    }
    // get the error page and include that instead
    String errorPage = getErrorPage();
    if (logger.isDebugEnabled())
        logger.debug("An error has occurred, redirecting to error page: " + errorPage);
    response.setContentType("text/html");
    PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher(errorPage);
    dispatcher.include(request, response);
}
Also used : PortletRequestDispatcher(javax.portlet.PortletRequestDispatcher) FacesContext(javax.faces.context.FacesContext) PortletSession(javax.portlet.PortletSession) ErrorBean(org.alfresco.web.bean.ErrorBean) ViewHandler(javax.faces.application.ViewHandler) UIViewRoot(javax.faces.component.UIViewRoot)

Example 2 with ErrorBean

use of org.alfresco.web.bean.ErrorBean in project acs-community-packaging by Alfresco.

the class AlfrescoFacesPortlet method handleError.

/**
 * Handles errors that occur during a process action request
 */
private void handleError(ActionRequest request, ActionResponse response, Throwable error) throws PortletException, IOException {
    // get the error bean from the session and set the error that occurred.
    PortletSession session = request.getPortletSession();
    ErrorBean errorBean = (ErrorBean) session.getAttribute(ErrorBean.ERROR_BEAN_NAME, PortletSession.PORTLET_SCOPE);
    if (errorBean == null) {
        errorBean = new ErrorBean();
        session.setAttribute(ErrorBean.ERROR_BEAN_NAME, errorBean, PortletSession.PORTLET_SCOPE);
    }
    errorBean.setLastError(error);
    response.setRenderParameter(ERROR_OCCURRED, "true");
}
Also used : PortletSession(javax.portlet.PortletSession) ErrorBean(org.alfresco.web.bean.ErrorBean)

Example 3 with ErrorBean

use of org.alfresco.web.bean.ErrorBean in project acs-community-packaging by Alfresco.

the class UploadFileServlet method handleUploadException.

private void handleUploadException(HttpServletRequest request, HttpServletResponse response, Throwable error, String returnPage) {
    try {
        HttpSession session = request.getSession(true);
        ErrorBean errorBean = (ErrorBean) session.getAttribute(ErrorBean.ERROR_BEAN_NAME);
        if (errorBean == null) {
            errorBean = new ErrorBean();
            session.setAttribute(ErrorBean.ERROR_BEAN_NAME, errorBean);
        }
        errorBean.setLastError(error);
        errorBean.setReturnPage(returnPage);
    } catch (Throwable e) {
        logger.error("Error while handling upload Exception", e);
    }
    try {
        String errorPage = Application.getErrorPage(getServletContext());
        if (logger.isDebugEnabled()) {
            logger.debug("An error has occurred. Sending back response for redirecting to error page: " + errorPage);
        }
        response.setContentType(MimetypeMap.MIMETYPE_HTML);
        response.setCharacterEncoding("utf-8");
        final PrintWriter out = response.getWriter();
        out.println("<html><body><script type=\"text/javascript\">");
        out.println("window.parent.location.replace(\" " + request.getContextPath() + errorPage + "\")");
        out.println("</script></body></html> ");
        out.close();
    } catch (Exception e) {
        logger.error("Error while handling upload Exception", e);
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) ErrorBean(org.alfresco.web.bean.ErrorBean) ServletException(javax.servlet.ServletException) JSONException(org.json.JSONException) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) PrintWriter(java.io.PrintWriter)

Example 4 with ErrorBean

use of org.alfresco.web.bean.ErrorBean in project acs-community-packaging by Alfresco.

the class Application method handleServletError.

/**
 * Handles errors thrown from servlets
 *
 * @param servletContext The servlet context
 * @param request The HTTP request
 * @param response The HTTP response
 * @param error The exception
 * @param logger The logger
 */
public static void handleServletError(ServletContext servletContext, HttpServletRequest request, HttpServletResponse response, Throwable error, Log logger, String returnPage) throws IOException, ServletException {
    // get the error bean from the session and set the error that occurred.
    HttpSession session = request.getSession();
    ErrorBean errorBean = (ErrorBean) session.getAttribute(ErrorBean.ERROR_BEAN_NAME);
    if (errorBean == null) {
        errorBean = new ErrorBean();
        session.setAttribute(ErrorBean.ERROR_BEAN_NAME, errorBean);
    }
    errorBean.setLastError(error);
    errorBean.setReturnPage(returnPage);
    // try and find the configured error page
    boolean errorShown = false;
    String errorPage = getErrorPage(servletContext);
    if (errorPage != null) {
        if (logger.isDebugEnabled())
            logger.debug("An error has occurred, redirecting to error page: " + errorPage);
        if (response.isCommitted() == false) {
            errorShown = true;
            response.sendRedirect(request.getContextPath() + errorPage);
        } else {
            if (logger.isDebugEnabled())
                logger.debug("Response is already committed, re-throwing error");
        }
    } else {
        if (logger.isDebugEnabled())
            logger.debug("No error page defined, re-throwing error");
    }
    // if we could not show the error page for whatever reason, re-throw the error
    if (!errorShown) {
        if (error instanceof IOException) {
            throw (IOException) error;
        } else if (error instanceof ServletException) {
            throw (ServletException) error;
        } else {
            throw new ServletException(error);
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) HttpSession(javax.servlet.http.HttpSession) ErrorBean(org.alfresco.web.bean.ErrorBean) IOException(java.io.IOException)

Example 5 with ErrorBean

use of org.alfresco.web.bean.ErrorBean in project acs-community-packaging by Alfresco.

the class Application method handleSystemError.

/**
 * Handles error conditions detected by servlets.
 *
 * @param servletContext
 *           The servlet context
 * @param request
 *           The HTTP request
 * @param response
 *           The HTTP response
 * @param messageKey
 *           the resource bundle key for the error mesage
 * @param statusCode
 *           the status code to set on the response
 * @param logger
 *           The logger
 * @throws IOException
 *            Signals that an I/O exception has occurred.
 * @throws ServletException
 *            the servlet exception
 */
public static void handleSystemError(ServletContext servletContext, HttpServletRequest request, HttpServletResponse response, String messageKey, int statusCode, Log logger) throws IOException, ServletException {
    // get the error bean from the session and set the error that occurred.
    HttpSession session = request.getSession();
    ErrorBean errorBean = (ErrorBean) session.getAttribute(ErrorBean.ERROR_BEAN_NAME);
    if (errorBean == null) {
        errorBean = new ErrorBean();
        session.setAttribute(ErrorBean.ERROR_BEAN_NAME, errorBean);
    }
    errorBean.setErrorMessageKey(messageKey);
    errorBean.setReturnPage(null);
    // try and find the configured error page
    boolean errorShown = false;
    String errorPage = getErrorPage(servletContext);
    if (errorPage != null) {
        if (logger.isDebugEnabled())
            logger.debug("An error has occurred, forwarding to error page: " + errorPage);
        if (!response.isCommitted()) {
            errorShown = true;
            response.reset();
            response.setStatus(statusCode);
            response.setContentType(MimetypeMap.MIMETYPE_HTML);
            response.setCharacterEncoding("utf-8");
            servletContext.getRequestDispatcher(errorPage).include(request, response);
        } else {
            if (logger.isDebugEnabled())
                logger.debug("Response is already committed, re-throwing error");
        }
    } else {
        if (logger.isDebugEnabled())
            logger.debug("No error page defined, re-throwing error");
    }
    // if we could not show the error page for whatever reason, re-throw the error
    if (!errorShown) {
        throw new ServletException(getMessage(session, messageKey));
    }
}
Also used : ServletException(javax.servlet.ServletException) HttpSession(javax.servlet.http.HttpSession) ErrorBean(org.alfresco.web.bean.ErrorBean)

Aggregations

ErrorBean (org.alfresco.web.bean.ErrorBean)6 IOException (java.io.IOException)3 ServletException (javax.servlet.ServletException)3 HttpSession (javax.servlet.http.HttpSession)3 PrintWriter (java.io.PrintWriter)2 PortletSession (javax.portlet.PortletSession)2 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 ResourceBundle (java.util.ResourceBundle)1 ViewHandler (javax.faces.application.ViewHandler)1 UIViewRoot (javax.faces.component.UIViewRoot)1 FacesContext (javax.faces.context.FacesContext)1 PortletRequestDispatcher (javax.portlet.PortletRequestDispatcher)1 JspException (javax.servlet.jsp.JspException)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 JSONException (org.json.JSONException)1