Example usage for javax.servlet ServletException getRootCause

List of usage examples for javax.servlet ServletException getRootCause

Introduction

In this page you can find the example usage for javax.servlet ServletException getRootCause.

Prototype


public Throwable getRootCause() 

Source Link

Document

Returns the exception that caused this servlet exception.

Usage

From source file:com.liferay.portal.struts.StrutsUtil.java

public static void forward(String uri, ServletContext ctx, HttpServletRequest req, HttpServletResponse res)
        throws ServletException {

    if (!res.isCommitted()) {
        String path = Constants.TEXT_HTML_DIR + uri;
        if (BrowserSniffer.is_wml(req)) {
            path = Constants.TEXT_WML_DIR + uri;
        }/*from   w  w w  . ja  v a 2 s.co m*/

        ServletContext portalCtx = ctx.getContext(PropsUtil.get(PropsUtil.PORTAL_CTX));

        if (portalCtx == null) {
            portalCtx = ctx;
        }

        RequestDispatcher rd = portalCtx.getRequestDispatcher(path);

        try {
            rd.forward(req, res);
        } catch (IOException ioe1) {
            Logger.error(StrutsUtil.class, ioe1.getMessage(), ioe1);
        } catch (ServletException se1) {
            req.setAttribute(PageContext.EXCEPTION, se1.getRootCause());

            String errorPath = Constants.TEXT_HTML_DIR + Constants.COMMON_ERROR;
            if (BrowserSniffer.is_wml(req)) {
                path = Constants.TEXT_WML_DIR + Constants.COMMON_ERROR;
            }

            rd = portalCtx.getRequestDispatcher(errorPath);

            try {
                rd.forward(req, res);
            } catch (IOException ioe2) {
                Logger.error(StrutsUtil.class, ioe2.getMessage(), ioe2);
            } catch (ServletException se2) {
                throw se2;
            }
        }
    } else {
        _log.warn(uri + " is already committed");
    }
}

From source file:com.mtgi.analytics.servlet.BehaviorTrackingFilter.java

private static final void handleServerError(BehaviorEvent event, Throwable e)
        throws ServletException, IOException {

    event.addData().add("response-status", 500);

    if (e instanceof ServletException) {
        ServletException se = (ServletException) e;
        Throwable cause = se.getRootCause();
        if (cause != null)
            event.setError(cause);// w w w.j a v a 2  s.com
        else
            event.setError(se);
        throw se;
    } else {
        event.setError(e);
    }

    //propagate exception
    if (e instanceof IOException)
        throw (IOException) e;
    if (e instanceof RuntimeException)
        throw (RuntimeException) e;
    //should not get this far in normal execution, but cover this case anyway..
    throw new ServletException(e);
}

From source file:hudson.security.UnwrapSecurityExceptionFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    try {/*from  w w w  . j ava  2  s  .com*/
        chain.doFilter(request, response);
    } catch (ServletException e) {
        Throwable t = e.getRootCause();
        if (t instanceof JellyTagException) {
            JellyTagException jte = (JellyTagException) t;
            Throwable cause = jte.getCause();
            if (cause instanceof AcegiSecurityException) {
                AcegiSecurityException se = (AcegiSecurityException) cause;
                throw new ServletException(se);
            }
        }
        throw e;
    }
}

From source file:eionet.cr.web.util.StripesExceptionHandler.java

/**
 *
 * @param servletException//w  ww .  j  a  v  a  2  s.  co m
 * @return
 */
private Throwable getRootCause(ServletException servletException) {

    Throwable rootCause = servletException.getRootCause();
    if (rootCause instanceof ServletException)
        return getRootCause((ServletException) rootCause);
    else
        return rootCause;
}

From source file:com.knowbout.hibernate.OpenSessionInViewFilter.java

/**
 * /*from w ww  .  j  a v  a  2 s. c o  m*/
 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
 */
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    HibernateUtil.openSession();
    try {
        chain.doFilter(request, response);
    } catch (ServletException se) {
        String message = se.toString();
        if (se.getRootCause() != null) {
            message = se.getRootCause().toString();
        }
        if (printFullExceptions) {
            if (se.getRootCause() != null) {
                log.error(message, se.getRootCause());
            } else {
                log.error(message, se);
            }
        } else {
            log.error(message);
        }
        throw se;
    } catch (Throwable t) {
        if (printFullExceptions) {
            log.error(t.getMessage(), t);
        } else {
            log.error(t.toString());
        }
        throw new ServletException(t);
    } finally {
        HibernateUtil.closeSession();
    }
}

From source file:com.knowbout.hibernate.OpenTransactionInViewFilter.java

/**
 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
 *///from w  w  w  .  jav  a  2s . c om
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    TransactionManager.beginTransaction();
    boolean commit = false;
    try {
        chain.doFilter(request, response);
        commit = !TransactionManager.isRollbackOnly();
    } catch (ServletException se) {
        String message = se.toString();
        if (se.getRootCause() != null) {
            message = se.getRootCause().toString();
        }
        if (printFullExceptions) {
            if (se.getRootCause() != null) {
                log.error(message, se.getRootCause());
            } else {
                log.error(message, se);
            }
        } else {
            log.error(message);
        }
        commit = false;
        throw se;
    } catch (Throwable t) {
        log.error("Error with transaction, rolling back instead.", t);
        commit = false;
    } finally {
        //dispose of the open transaction by commit or rollback
        //transactionHolder.remove(transaction);
        if (commit) {
            try {
                TransactionManager.commitTransaction();
            } catch (Throwable e) {
                log.error("Cannot commit transaction, rolling back instead.", e);
                TransactionManager.rollbackTransaction();
                throw new ServletException(e);
            }
        } else {
            try {
                TransactionManager.rollbackTransaction();
            } catch (Throwable e) {
                log.error("Cannot rollback transaction.", e);
                throw new ServletException(e);
            }
        }
    }
}

From source file:com.appeligo.search.entity.SetPermissionsInViewFilter.java

/**
 * //from w  w w  .  ja va2  s .  c o  m
 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
 */
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    User user = null;

    if (request instanceof HttpServletRequest) {
        String username = ((HttpServletRequest) request).getRemoteUser();
        if (username != null) {
            user = User.findByUsername(username);
        }
    }
    Permissions.setCurrentUser(user);
    try {
        chain.doFilter(request, response);
    } catch (ServletException se) {
        String message = se.toString();
        if (se.getRootCause() != null) {
            message = se.getRootCause().toString();
        }
        if (message != null && ((message.indexOf("ClientAbortException") >= 0)
                || (message.indexOf("Connection reset by peer") >= 0))) {
            log.warn(message);
        } else {
            if (printFullExceptions) {
                log.error("Caught servlet exception:");
                if (se.getRootCause() != null) {
                    log.error(message, se.getRootCause());
                } else {
                    log.error(message, se);
                }
            } else {
                log.error(message);
            }
            throw se;
        }
    } catch (Throwable t) {
        if (printFullExceptions) {
            log.error(t.getMessage(), t);
        } else {
            log.error(t.toString());
        }
        throw new ServletException(t);
    } finally {
        Permissions.setCurrentUser(null);
    }
}

From source file:com.pagecrumb.proxy.ProxyFilter.java

/**
 * TODO Add hander for null baseURL //from w  w  w .  j  av  a  2 s  . c  o m
 * @param ServletRequest servletrequest - request from client
 * @param ervletResponse servletresponse - response from server
 */
@Override
public void doFilter(ServletRequest servletrequest, final ServletResponse servletresponse, FilterChain chain)
        throws IOException, ServletException {
    log.info("Before invoking chain");
    try {
        String baseURL = ((HttpServletRequest) servletrequest).getQueryString();
        if (baseURL != null) {
            log.info(this.getClass().toString() + " " + "Requested URL: " + baseURL);

            // TODO Must not pass request to .css or .ico etc. to the GenericResponseWrapper
            // Must use regex here, every thing that ends with ".*" must not be passed except

            //if (baseURL.matches(".*?\\.css.*")) {
            //   GenericResponseWrapper responseWrapper 
            //      = new GenericResponseWrapper((HttpServletResponse) servletresponse, baseURL, "css");   
            //   chain.doFilter(servletrequest, responseWrapper);
            //}
            if (baseURL.matches(".*?\\.png.*") || baseURL.matches(".*?\\.ico.*")
                    || baseURL.matches(".*?\\.gif.*") || baseURL.matches(".*?\\.jpeg.*")
                    || baseURL.matches(".*?\\.jpg.*") || baseURL.matches(".*?\\.js.*")) { // Do not process Javascript for now 
                // Pass the wrapper on to the next filter or servlet
                log.info("Bypassing Parser - Just do Filter");
                chain.doFilter(servletrequest, servletresponse);
            } else {
                String gwtModuleBase = (String) servletrequest.getAttribute("X-GWT-Module-Base");
                log.info("Module-Base: " + gwtModuleBase);
                GenericResponseWrapper responseWrapper = new GenericResponseWrapper(
                        (HttpServletResponse) servletresponse, baseURL);
                chain.doFilter(servletrequest, responseWrapper);
                log.info("Content type was " + responseWrapper.getContentType());
            }
        } else {
            PrintWriter pw = servletresponse.getWriter();
            pw.println("<html><body><p>Oops, query URL is missing.</p></body></html>");
        }
    } catch (ServletException e) {
        log.error("Caught Servlet Exception");
        Throwable rootCause = e.getRootCause();
        log.error("Root cause is " + rootCause.toString());
        if (rootCause instanceof RuntimeException) { // This is true for any FacesException.
            log.error("Rethrowing exception as RuntimeException" + rootCause.toString());
            throw (RuntimeException) rootCause; // Throw wrapped RuntimeException instead of ServletException.
        } else {
            throw e;
        }
    }
    log.info("After invoking chain");
}

From source file:com.liferay.portlet.StrutsPortlet.java

protected void include(RenderRequest req, RenderResponse res) throws IOException, PortletException {

    // Call render of com.liferay.portal.struts.PortletAction

    Map strutsAttributes = null;/*from  w w w . j  a v  a  2  s.  co  m*/

    if (_portletConfig.isWARFile()) {

        // Remove any Struts request attributes

        strutsAttributes = _removeStrutsAttributes(req);

        //req.setAttribute(
        //   WebKeys.PORTLET_STRUTS_ATTRIBUTES, strutsAttributes);
    }

    // Process render

    try {
        _getPortletRequestProcessor(req).process(req, res);
    } catch (IOException ioe) {
        throw ioe;
    } catch (ServletException se) {
        _log.error(se.getRootCause());

        throw new PortletException(se);
    } finally {
        if (_portletConfig.isWARFile()) {

            // Set the Struts request attributes

            _setStrutsAttributes(req, strutsAttributes);

            //req.removeAttribute(WebKeys.PORTLET_STRUTS_ATTRIBUTES);
        }
    }

    if (_copyRequestParameters) {
        PortalUtil.clearRequestParameters(req);
    }
}

From source file:br.com.caelum.vraptor.config.BasicConfigurationTest.java

@Test
public void shouldThrowInstantiationExceptionCauseIfThereIsSuchException() {
    when(context.getInitParameter(BasicConfiguration.CONTAINER_PROVIDER))
            .thenReturn(DogProvider.class.getName());
    try {/* w  w  w. jav a 2 s  .c o  m*/
        config.getProvider();
        fail();
    } catch (ServletException e) {
        assertNotNull("Should have a cause", e.getRootCause());
        assertEquals(IOException.class, e.getRootCause().getClass());
    }
}