@Override @Produces(MediaType.APPLICATION_JSON) public Response toResponse(EJBException exception) { if (exception.getCause() instanceof IllegalArgumentException) { return handleIllegalArgumentException((IllegalArgumentException) exception.getCause()); } else if (exception.getCause() instanceof AccessControlException) { return handleAccessControlException((AccessControlException) exception.getCause()); } else if (exception.getCause() instanceof ConstraintViolationException) { return handleConstraintViolation((ConstraintViolationException) exception.getCause()); } else if (exception.getCause() instanceof RollbackException) { return handleRollbackException((RollbackException) exception.getCause()); } else if (exception.getCause() instanceof AccessLocalException) { return handleAccessLocalException((AccessLocalException) exception.getCause()); } else if(exception.getCause() instanceof IllegalStateException) { return handleIllegalStateException((IllegalStateException) exception.getCause()); } LOG.log(Level.INFO, "EJBException Caused by: {0}", exception.getCause().toString()); LOG.log(Level.INFO, "EJBException: {0}", exception.getCause().getMessage()); JsonResponse jsonResponse = new JsonResponse(); jsonResponse.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase()); jsonResponse.setStatusCode(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); jsonResponse.setErrorMsg(exception.getCause().getMessage()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(jsonResponse).build(); }
@Override public Response toResponse(AccessLocalException e) { LOGGER.log(Level.SEVERE, "Access denied : " + e.getMessage(), e); return Response.status(Response.Status.UNAUTHORIZED) .header("Reason-Phrase", "Access denied") .build(); }
private Response handleAccessLocalException(AccessLocalException accessLocalException) { LOG.log(Level.INFO, "AccessLocalException: {0}", accessLocalException.getMessage()); JsonResponse jsonResponse = new JsonResponse(); jsonResponse.setStatus(Response.Status.UNAUTHORIZED.getReasonPhrase()); jsonResponse.setStatusCode(Response.Status.UNAUTHORIZED.getStatusCode()); jsonResponse.setErrorMsg(accessLocalException.getMessage()); return Response.status(Response.Status.UNAUTHORIZED).entity(jsonResponse).build(); }
@Override public Response toResponse(AccessLocalException ex) { log.log(Level.INFO, "AuthExceptionMapper: {0}", ex.getClass()); ErrorResponse json = new ErrorResponse(); json.setDescription(ex.getMessage()); return Response.status(Response.Status.UNAUTHORIZED) .entity(json) .type(MediaType.APPLICATION_JSON). build(); }
@Override public Response toResponse(AccessLocalException ex) { log.log(Level.INFO, "AuthExceptionMapper: {0}", ex.getClass()); JsonResponse json = new JsonResponse(); json.setStatusCode(Response.Status.UNAUTHORIZED.getStatusCode()); json.setErrorMsg(ex.getMessage()); return Response.status(Response.Status.UNAUTHORIZED) .entity(json) .type(MediaType.APPLICATION_JSON). build(); }
/** * Renamed method so it shows up with a much more understandable purpose as it * will be the top element in the stacktrace * * @param e Throwable * @param method Method */ protected Throwable convertException(final Throwable e, final Method method) { if (!remote && e instanceof RemoteException) { if (e instanceof TransactionRequiredException) { return new TransactionRequiredLocalException(e.getMessage()).initCause(getCause(e)); } if (e instanceof TransactionRolledbackException) { return new TransactionRolledbackLocalException(e.getMessage()).initCause(getCause(e)); } /** * If a client attempts to invoke a method on a removed bean's business interface, * we must throw a javax.ejb.NoSuchEJBException. If the business interface is a * remote business interface that extends java.rmi.Remote, the * java.rmi.NoSuchObjectException is thrown to the client instead. * See EJB 3.0, section 4.4 */ if (e instanceof NoSuchObjectException) { if (java.rmi.Remote.class.isAssignableFrom(method.getDeclaringClass())) { return e; } else { return new NoSuchEJBException(e.getMessage()).initCause(getCause(e)); } } if (e instanceof AccessException) { return new AccessLocalException(e.getMessage()).initCause(getCause(e)); } return new EJBException(e.getMessage()).initCause(getCause(e)); } if (remote && e instanceof EJBAccessException) { if (e.getCause() instanceof Exception) { return new AccessException(e.getMessage(), (Exception) e.getCause()); } else { return new AccessException(e.getMessage()); } } if (!remote && e instanceof EJBTransactionRolledbackException) { return new TransactionRolledbackLocalException(e.getMessage()).initCause(getCause(e)); } return e; }
/** * Renamed method so it shows up with a much more understandable purpose as it * will be the top element in the stacktrace * * @param e Throwable * @param method Method * @param interfce Class */ protected Throwable convertException(Throwable e, final Method method, final Class interfce) { final boolean rmiRemote = Remote.class.isAssignableFrom(interfce); if (e instanceof TransactionRequiredException) { if (!rmiRemote && interfaceType.isBusiness()) { return new EJBTransactionRequiredException(e.getMessage()).initCause(getCause(e)); } else if (interfaceType.isLocal()) { return new TransactionRequiredLocalException(e.getMessage()).initCause(getCause(e)); } else { return e; } } if (e instanceof TransactionRolledbackException) { if (!rmiRemote && interfaceType.isBusiness()) { return new EJBTransactionRolledbackException(e.getMessage()).initCause(getCause(e)); } else if (interfaceType.isLocal()) { return new TransactionRolledbackLocalException(e.getMessage()).initCause(getCause(e)); } else { return e; } } if (e instanceof NoSuchObjectException) { if (!rmiRemote && interfaceType.isBusiness()) { return new NoSuchEJBException(e.getMessage()).initCause(getCause(e)); } else if (interfaceType.isLocal()) { return new NoSuchObjectLocalException(e.getMessage()).initCause(getCause(e)); } else { return e; } } if (e instanceof AccessException) { if (!rmiRemote && interfaceType.isBusiness()) { return new AccessLocalException(e.getMessage()).initCause(getCause(e)); } else if (interfaceType.isLocal()) { return new AccessLocalException(e.getMessage()).initCause(getCause(e)); } else { return e; } } if (e instanceof RemoteException) { if (!rmiRemote && interfaceType.isBusiness()) { return new EJBException(e.getMessage()).initCause(getCause(e)); } else if (interfaceType.isLocal()) { return new EJBException(e.getMessage()).initCause(getCause(e)); } else { return e; } } for (final Class<?> type : method.getExceptionTypes()) { if (type.isAssignableFrom(e.getClass())) { return e; } } // Exception is undeclared // Try and find a runtime exception in there while (e.getCause() != null && !(e instanceof RuntimeException)) { e = e.getCause(); } return e; }