Example usage for org.apache.commons.lang3.exception ExceptionUtils indexOfType

List of usage examples for org.apache.commons.lang3.exception ExceptionUtils indexOfType

Introduction

In this page you can find the example usage for org.apache.commons.lang3.exception ExceptionUtils indexOfType.

Prototype

public static int indexOfType(final Throwable throwable, final Class<?> type) 

Source Link

Document

Returns the (zero based) index of the first Throwable that matches the specified class or subclass in the exception chain.

Usage

From source file:com.haulmont.cuba.web.gui.WebTimer.java

public WebTimer() {
    component = new Label();
    timerImpl = new CubaTimer();
    timerImpl.setExceptionHandler(e -> {
        int reIdx = ExceptionUtils.indexOfType(e, RemoteException.class);
        if (reIdx > -1) {
            RemoteException re = (RemoteException) ExceptionUtils.getThrowableList(e).get(reIdx);
            for (RemoteException.Cause cause : re.getCauses()) {
                //noinspection ThrowableResultOfMethodCallIgnored
                if (cause.getThrowable() instanceof NoUserSessionException) {
                    log.warn("NoUserSessionException in timer {}, timer will be stopped",
                            timerImpl.getLoggingTimerId());
                    stop();//from  w  ww.ja  va 2  s. c o  m
                    break;
                }
            }
        } else if (ExceptionUtils.indexOfThrowable(e, NoUserSessionException.class) > -1) {
            log.warn("NoUserSessionException in timer {}, timer will be stopped",
                    timerImpl.getLoggingTimerId());
            stop();
        }

        throw new RuntimeException("Exception in timer", e);
    });
}

From source file:com.francetelecom.clara.cloud.commons.toggles.PaasJndiStateRepository.java

@Override
public FeatureState getFeatureState(Feature feature) {
    try {/*from   ww  w  . j  av  a2s .  c o m*/
        InitialContext jndiContext = getInitialContext();
        String value = (String) jndiContext.lookup(feature.name());
        if (!(value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false"))) {
            return null;
        }
        boolean enabled = Boolean.valueOf(value);
        List<String> users = getFeatureUsers(feature);
        FeatureState featureState = new FeatureState(feature, enabled, users);
        return featureState;
    } catch (NameNotFoundException e) {
        logger.warn("state of feature " + feature.name() + " is not already defined in jndi");
    } catch (NamingException e) {
        if (ExceptionUtils.indexOfType(e, NameNotFoundException.class) != -1) {
            logger.warn("state of feature " + feature.name() + " is not already defined in jndi");
        } else {
            logger.error("unable to read state of feature " + feature.name(), e);
        }
    } catch (ClassCastException e) {
        logger.error("unable to read state of feature " + feature.name(), e);
    }
    // By default return null (no feature state found or invalid)
    return null;
}

From source file:at.bitfire.davdroid.webdav.TlsSniSocketFactoryTest.java

public void testHostnameNotInCertificate() throws IOException {
    try {//w  w  w  .jav  a  2 s.c  o m
        // host with certificate that doesn't match host name
        // use the IP address as host name because IP addresses are usually not in the certificate subject
        final String ipHostname = sampleTlsEndpoint.getAddress().getHostAddress();
        InetSocketAddress host = new InetSocketAddress(ipHostname, 443);
        @Cleanup
        Socket socket = factory.connectSocket(0, null, new HttpHost(ipHostname), host, null, null);
        fail();
    } catch (SSLException e) {
        Log.i(TAG, "Expected exception", e);
        assertFalse(ExceptionUtils.indexOfType(e, SSLException.class) == -1);
    }
}

From source file:at.bitfire.davdroid.webdav.TlsSniSocketFactoryTest.java

public void testUntrustedCertificate() throws IOException {
    try {/*  w  w  w.  j  a v  a 2  s . c  o m*/
        // host with certificate that is not trusted by default
        InetSocketAddress host = new InetSocketAddress("cacert.org", 443);

        @Cleanup
        Socket socket = factory.connectSocket(0, null, new HttpHost(host.getHostName()), host, null, null);
        fail();
    } catch (SSLHandshakeException e) {
        Log.i(TAG, "Expected exception", e);
        assertFalse(ExceptionUtils.indexOfType(e, CertPathValidatorException.class) == -1);
    }
}

From source file:com.qwazr.server.ServerException.java

public static ServerException of(String message, final Throwable throwable) {
    if (throwable instanceof ServerException)
        return (ServerException) throwable;

    int status = 500;

    final int serverExceptionPos = ExceptionUtils.indexOfType(throwable, ServerException.class);
    if (serverExceptionPos != -1)
        return (ServerException) ExceptionUtils.getThrowableList(throwable).get(serverExceptionPos);

    final int webApplicationExceptionPos = ExceptionUtils.indexOfType(throwable, WebApplicationException.class);
    if (webApplicationExceptionPos != -1)
        status = ((WebApplicationException) ExceptionUtils.getThrowableList(throwable)
                .get(webApplicationExceptionPos)).getResponse().getStatus();

    if (StringUtils.isBlank(message)) {
        message = throwable.getMessage();
        if (StringUtils.isBlank(message))
            message = ExceptionUtils.getRootCauseMessage(throwable);
        if (StringUtils.isBlank(message))
            message = "Internal server error";
    }//  w  ww.  j a  va  2 s. co m

    return new ServerException(status, message, throwable);
}

From source file:org.finra.dm.service.helper.DmErrorInformationExceptionHandler.java

/**
 * Handle Activiti exceptions. Note that this method properly handles a null response being passed in.
 *
 * @param exception the exception./*from  ww w.  j  a v a  2s  .co  m*/
 * @param response the response.
 *
 * @return the error information.
 */
@ExceptionHandler(value = ActivitiException.class)
@ResponseBody
public ErrorInformation handleActivitiException(Exception exception, HttpServletResponse response) {
    if ((ExceptionUtils.indexOfThrowable(exception, ActivitiClassLoadingException.class) != -1)
            || (ExceptionUtils.indexOfType(exception, ELException.class) != -1)) {
        // These exceptions are caused by invalid workflow configurations (i.e. user error) so they are considered a bad request.
        return getErrorInformationAndSetStatus(HttpStatus.BAD_REQUEST, exception, response);
    } else {
        // For all other exceptions, something is wrong that we weren't expecting so we'll return this as an internal server error and log the error.
        logError("An Activiti error occurred.", exception);
        return getErrorInformationAndSetStatus(HttpStatus.INTERNAL_SERVER_ERROR, exception, response);
    }
}