Example usage for java.lang.reflect UndeclaredThrowableException getUndeclaredThrowable

List of usage examples for java.lang.reflect UndeclaredThrowableException getUndeclaredThrowable

Introduction

In this page you can find the example usage for java.lang.reflect UndeclaredThrowableException getUndeclaredThrowable.

Prototype

public Throwable getUndeclaredThrowable() 

Source Link

Document

Returns the Throwable instance wrapped in this UndeclaredThrowableException , which may be null .

Usage

From source file:de.codecentric.boot.admin.zuul.filters.post.SendResponseFilterTests.java

@Test
public void closeResponseOutpusStreamError() throws Exception {
    HttpServletResponse response = mock(HttpServletResponse.class);

    RequestContext context = new RequestContext();
    context.setRequest(new MockHttpServletRequest());
    context.setResponse(response);//from   ww w . j a v a  2s . com
    context.setResponseDataStream(new ByteArrayInputStream("Hello\n".getBytes("UTF-8")));
    CloseableHttpResponse zuulResponse = mock(CloseableHttpResponse.class);
    context.set("zuulResponse", zuulResponse);
    RequestContext.testSetCurrentContext(context);

    SendResponseFilter filter = new SendResponseFilter();

    ServletOutputStream zuuloutputstream = mock(ServletOutputStream.class);
    doThrow(new IOException("Response to client closed")).when(zuuloutputstream).write(isA(byte[].class),
            anyInt(), anyInt());

    when(response.getOutputStream()).thenReturn(zuuloutputstream);

    try {
        filter.run();
    } catch (UndeclaredThrowableException ex) {
        assertThat(ex.getUndeclaredThrowable().getMessage(), is("Response to client closed"));
    }

    verify(zuulResponse).close();
}

From source file:MBeanTyper.java

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (MBeanTyper.DEBUG) {
        System.err.println("  ++ method=" + method.getName() + ",args=" + args);
    }/*  w  ww .ja  v  a2  s . c o m*/
    try {
        if (method.getDeclaringClass() == Object.class) {
            String name = method.getName();
            if (name.equals("hashCode")) {
                return new Integer(this.hashCode());
            } else if (name.equals("toString")) {
                return this.toString();
            } else if (name.equals("equals")) {
                // FIXME: this needs to be reviewed - we should be
                // smarter about this ...
                return new Boolean(equals(args[0]));
            }
        } else if (isJMXAttribute(method) && (args == null || args.length <= 0)) {
            String name = method.getName().substring(3);
            return server.getAttribute(mbean, name);
        }

        String sig[] = (String[]) signatureCache.get(method);
        if (sig == null) {
            // get the method signature from the method argument directly
            // vs. the arguments passed, since there may be primitives that
            // are wrapped as objects in the arguments
            Class _args[] = method.getParameterTypes();
            if (_args != null && _args.length > 0) {
                sig = new String[_args.length];
                for (int c = 0; c < sig.length; c++) {
                    if (_args[c] != null) {
                        sig[c] = _args[c].getName();
                    }
                }
            } else {
                sig = new String[0];
            }
            signatureCache.put(method, sig);
        }
        return server.invoke(mbean, method.getName(), args, sig);
    } catch (Throwable t) {
        if (MBeanTyper.DEBUG) {
            t.printStackTrace();
        }
        if (t instanceof UndeclaredThrowableException) {
            UndeclaredThrowableException ut = (UndeclaredThrowableException) t;
            throw ut.getUndeclaredThrowable();
        } else if (t instanceof InvocationTargetException) {
            InvocationTargetException it = (InvocationTargetException) t;
            throw it.getTargetException();
        } else if (t instanceof MBeanException) {
            MBeanException me = (MBeanException) t;
            throw me.getTargetException();
        } else {
            throw t;
        }
    }
}

From source file:com.taobao.common.tedis.group.ReliableAsynTedisGroup.java

public void init() {
    if (!inited) {
        try {/*from  ww  w .  ja v a  2 s . c o m*/
            if (this.cm == null) {
                this.cm = new DiamondConfigManager(appName, version);
            }
            this.ospreyManager = new OspreyManager("tedis-" + appName + "-" + version);
            this.ospreyManager.registerProcessor(new OspreyProcessor<ReliableAsynMessage>() {

                @Override
                public Class<ReliableAsynMessage> interest() {
                    return ReliableAsynMessage.class;
                }

                @Override
                public Result process(ReliableAsynMessage message) {
                    Result result = new Result();
                    long time = System.currentTimeMillis();

                    Single single = cm.getRouter().getAtomic(message.getSingleKey());
                    if (single == null) {
                        result.setSuccess(false);
                        result.setErrorMessage("Current atomic is null");
                        return result;
                    }

                    try {
                        message.getMethod().invoke(single.getTedis(), message.getArgs());
                    } catch (Throwable t) {
                        logger.warn("write exception:" + single.getProperties(), t);
                        try {
                            statLog(message.getMethod().getName(), false, time);
                            // 
                            InvocationTargetException ite = (InvocationTargetException) t;
                            UndeclaredThrowableException ute = (UndeclaredThrowableException) ite
                                    .getTargetException();
                            if (ute.getUndeclaredThrowable() instanceof TimeoutException) {
                                result.setSuccess(false);
                                result.setErrorMessage("TimeoutException");
                                result.setRuntimeException(ute);
                                return result;
                            } else {
                                ExecutionException ee = (ExecutionException) ute.getUndeclaredThrowable();
                                InvocationTargetException ite_1 = (InvocationTargetException) ee.getCause();
                                TedisException te = (TedisException) ite_1.getTargetException();
                                if (te.getCause() instanceof TedisConnectionException) {
                                    result.setSuccess(false);
                                    result.setErrorMessage("JedisConnectionException");
                                    result.setRuntimeException(te);
                                    return result;
                                }
                            }
                        } catch (Throwable tt) {
                            logger.warn(":", tt);
                        }
                    }
                    return result;
                }
            });
            this.ospreyManager.init();
            tedis = (RedisCommands) Proxy.newProxyInstance(RedisCommands.class.getClassLoader(),
                    new Class[] { RedisCommands.class }, new TedisGroupInvocationHandler());
        } catch (Exception e) {
            throw new TedisException("init failed", e);
        }
        inited = true;
    }
}

From source file:de.xwic.appkit.core.dao.AbstractDAO.java

private Object getPropertyValue(IEntity entity, Property property, ValidationResult result) throws Exception {

    String keyPropName = entity.type().getName() + "." + property.getName();

    try {/*from ww w.j av a 2s  .  c  o m*/
        Method mRead = property.getDescriptor().getReadMethod();
        if (mRead == null) {
            // the property is not defined on the entity class. Search for the property in the superclass
            // and use that. This is needed for cases where the entity is using the history and therefore
            // extending a base implementation
            PropertyDescriptor pd = new PropertyDescriptor(property.getName(),
                    entity.getClass().getSuperclass());
            mRead = pd.getReadMethod();
            if (mRead == null) {
                throw new ConfigurationException("The property " + property.getName()
                        + " can not be resolved on entity " + entity.getClass().getName());
            }
        }
        Object value = mRead.invoke(entity, (Object[]) null);
        return value;
    } catch (Exception se) {
        Throwable e = se;
        while (e != null) {
            if (e instanceof SecurityException) {
                result.addWarning(keyPropName, ValidationResult.FIELD_REQUIRED_NOT_ACCESSABLE);
                break;
            } else if (e instanceof InvocationTargetException) {
                InvocationTargetException ite = (InvocationTargetException) e;
                e = ite.getTargetException();
                if (e == ite) {
                    break;
                }
            } else if (e instanceof UndeclaredThrowableException) {
                UndeclaredThrowableException ute = (UndeclaredThrowableException) e;
                e = ute.getUndeclaredThrowable();
                if (e == ute) {
                    break;
                }
            } else {
                throw se;
            }
        }
    }

    return null;
}

From source file:com.cloudera.beeswax.BeeswaxServiceImpl.java

private <T> T doWithState(RunningQueryState state, PrivilegedExceptionAction<T> action)
        throws BeeswaxException {
    try {//www .  ja va  2  s . co  m
        UserGroupInformation ugi;
        if (UserGroupInformation.isSecurityEnabled())
            ugi = UserGroupInformation.createProxyUser(state.query.hadoop_user,
                    UserGroupInformation.getLoginUser());
        else {
            ugi = UserGroupInformation.createRemoteUser(state.query.hadoop_user);
        }
        return ugi.doAs(action);
    } catch (UndeclaredThrowableException e) {
        if (e.getUndeclaredThrowable() instanceof PrivilegedActionException) {
            Throwable bwe = e.getUndeclaredThrowable().getCause();
            if (bwe instanceof BeeswaxException) {
                LOG.error("Caught BeeswaxException", (BeeswaxException) bwe);
                throw (BeeswaxException) bwe;
            }
        }
        LOG.error("Caught unexpected exception.", e);
        throw new BeeswaxException(e.getMessage(), state.handle.log_context, state.handle);
    } catch (IOException e) {
        LOG.error("Caught IOException", e);
        throw new BeeswaxException(e.getMessage(), state.handle.log_context, state.handle);
    } catch (InterruptedException e) {
        LOG.error("Caught InterruptedException", e);
        throw new BeeswaxException(e.getMessage(), state.handle.log_context, state.handle);
    }
}

From source file:org.apache.hadoop.hbase.client.ConnectionImplementation.java

private boolean isKeepAliveMasterConnectedAndRunning(MasterServiceState mss) {
    if (mss.getStub() == null) {
        return false;
    }/*from   w w  w.j  a  v  a  2s . c o  m*/
    try {
        return mss.isMasterRunning();
    } catch (UndeclaredThrowableException e) {
        // It's somehow messy, but we can receive exceptions such as
        //  java.net.ConnectException but they're not declared. So we catch it...
        LOG.info("Master connection is not running anymore", e.getUndeclaredThrowable());
        return false;
    } catch (ServiceException se) {
        LOG.warn("Checking master connection", se);
        return false;
    }
}

From source file:org.apache.hadoop.hbase.security.access.SecureTestUtil.java

public static void verifyDenied(User user, boolean requireException, AccessTestAction... actions)
        throws Exception {
    for (AccessTestAction action : actions) {
        try {//from   ww  w .  j  av  a 2s  .  co m
            Object obj = user.runAs(action);
            if (requireException) {
                fail("Expected exception was not thrown for user '" + user.getShortName() + "'");
            }
            if (obj != null && obj instanceof List<?>) {
                List<?> results = (List<?>) obj;
                if (results != null && !results.isEmpty()) {
                    fail("Unexpected results for user '" + user.getShortName() + "'");
                }
            }
        } catch (IOException e) {
            boolean isAccessDeniedException = false;
            if (e instanceof RetriesExhaustedWithDetailsException) {
                // in case of batch operations, and put, the client assembles a
                // RetriesExhaustedWithDetailsException instead of throwing an
                // AccessDeniedException
                for (Throwable ex : ((RetriesExhaustedWithDetailsException) e).getCauses()) {
                    if (ex instanceof AccessDeniedException) {
                        isAccessDeniedException = true;
                        break;
                    }
                }
            } else {
                // For doBulkLoad calls AccessDeniedException
                // is buried in the stack trace
                Throwable ex = e;
                do {
                    if (ex instanceof AccessDeniedException) {
                        isAccessDeniedException = true;
                        break;
                    }
                } while ((ex = ex.getCause()) != null);
            }
            if (!isAccessDeniedException) {
                fail("Expected exception was not thrown for user '" + user.getShortName() + "'");
            }
        } catch (UndeclaredThrowableException ute) {
            // TODO why we get a PrivilegedActionException, which is unexpected?
            Throwable ex = ute.getUndeclaredThrowable();
            if (ex instanceof PrivilegedActionException) {
                ex = ((PrivilegedActionException) ex).getException();
            }
            if (ex instanceof ServiceException) {
                ServiceException se = (ServiceException) ex;
                if (se.getCause() != null && se.getCause() instanceof AccessDeniedException) {
                    // expected result
                    return;
                }
            }
            fail("Expected exception was not thrown for user '" + user.getShortName() + "'");
        }
    }
}

From source file:org.apache.phoenix.end2end.BasePermissionsIT.java

/** This passes only if desired exception is caught for all users. */
<T> void verifyDenied(User user, Class<T> exception, TableDDLPermissionsIT.AccessTestAction... actions)
        throws Exception {
    for (TableDDLPermissionsIT.AccessTestAction action : actions) {
        try {//from w w w  .j  ava2s .c  o  m
            user.runAs(action);
            fail("Expected exception was not thrown for user '" + user.getShortName() + "'");
        } catch (IOException e) {
            fail("Expected exception was not thrown for user '" + user.getShortName() + "'");
        } catch (UndeclaredThrowableException ute) {
            Throwable ex = ute.getUndeclaredThrowable();

            // HBase AccessDeniedException(ADE) is handled in different ways in different parts of code
            // 1. Wrap HBase ADE in PhoenixIOException (Mostly for create, delete statements)
            // 2. Wrap HBase ADE in ExecutionException (Mostly for scans)
            // 3. Directly throwing HBase ADE or custom msg with HBase ADE
            // Thus we iterate over the chain of throwables and find ADE
            for (Throwable throwable : Throwables.getCausalChain(ex)) {
                if (exception.equals(throwable.getClass())) {
                    if (throwable instanceof AccessDeniedException) {
                        validateAccessDeniedException((AccessDeniedException) throwable);
                    }
                    return;
                }
            }

        } catch (RuntimeException ex) {
            // This can occur while accessing tabledescriptors from client by the unprivileged user
            if (ex.getCause() instanceof AccessDeniedException) {
                // expected result
                validateAccessDeniedException((AccessDeniedException) ex.getCause());
                return;
            }
        }
        fail("Expected exception was not thrown for user '" + user.getShortName() + "'");
    }
}

From source file:org.cloudata.core.common.testhelper.ProxyExceptionHelper.java

public static void handleException(UndeclaredThrowableException e, Log LOG) throws IOException {
    if (e.getUndeclaredThrowable() instanceof InvocationTargetException) {
        InvocationTargetException ex = ((InvocationTargetException) e.getUndeclaredThrowable());
        if (ex.getTargetException() instanceof IOException) {
            throw (IOException) ex.getTargetException();
        } else {/*from w  w  w .j a va 2 s .  co  m*/
            LOG.fatal("Unexpected exception is occurred", ex.getTargetException());
            throw new IOException("Unexpected exception is occurred : " + e, e);
        }
    } else {
        LOG.fatal("Unexpected exception is occurred", e);
        throw new IOException("Unexpected exception is occurred : " + e, e);
    }

}

From source file:org.uddi.v2_service.DispositionReport.java

/** 
 * Convenience method to figure out if the Exception at hand contains a
 * DispositionReport. Disposition report will be null if none can be found.
 * /*from  w  w w .j  a  v  a2  s. c  o m*/
 * @param e the Exception at hang
 * @return DispositionReport if one can be found, or null if it is not.
 */
public static org.uddi.api_v2.DispositionReport getDispositionReport(Exception e) {
    org.uddi.api_v2.DispositionReport report = null;
    if (e instanceof DispositionReport) {
        DispositionReport faultMsg = (DispositionReport) e;
        report = faultMsg.faultInfo;
    } else if (e instanceof SOAPFaultException) {
        SOAPFaultException soapFault = (SOAPFaultException) e;
        Detail detail = soapFault.getFault().getDetail();
        if (detail != null && detail.getFirstChild() != null) {
            try {
                report = new org.uddi.api_v2.DispositionReport(detail.getFirstChild());
            } catch (JAXBException je) {
                log.error("Could not unmarshall detail to a DispositionReport");
            }
        }
    } else if (e instanceof UndeclaredThrowableException) {
        UndeclaredThrowableException ute = (UndeclaredThrowableException) e;
        if (ute.getUndeclaredThrowable() != null && ute.getUndeclaredThrowable().getCause() != null
                && ute.getUndeclaredThrowable().getCause().getCause() instanceof DispositionReport) {
            DispositionReport faultMsg = (DispositionReport) ute.getUndeclaredThrowable().getCause().getCause();
            report = faultMsg.getFaultInfo();
        }
    } else {
        log.error("Unsupported Exception: " + e.getClass());
    }
    return report;
}