String message = "";
int applicationErrorCode = 0;
int status;
Throwable cause = ex;
BasicResponse resp = new BasicResponse(BasicResponse.FAILURE);
resp.setError(new Error());
if (ex instanceof AccessDeniedException) {
// 403 FORBIDDEN
status = 403;
applicationErrorCode = ApplicationCodeConstants.AUTHENTICATION_ENABLED_AND_FAILED;
resp.getError().setErrorText("Access Denied");
} else if (ex instanceof AuthenticationException) {
// UNAUTHORIZED
status = 401;
applicationErrorCode = ApplicationCodeConstants.UNAUTHORIZED;
resp.getError().setErrorText("Unauthorized access");
} else if (ex instanceof WebApplicationException) { // CXF raises this
// kind of exception
// 500 INTERNAL_SERVER_ERROR
status = 500;
applicationErrorCode = ApplicationCodeConstants.INTERNAL_APPLICATION_ERROR;
if (ex.getCause() == null) {
message = ex.toString();
} else {
cause = ex.getCause();
if (cause instanceof SAXParseException) {
applicationErrorCode = ApplicationCodeConstants.INVALID_XML;
resp.getError().setErrorText("XML Validation Error: " + ex.getCause().getMessage());
} else if (cause instanceof ProvisionException) {
ProvisionException provEx = (ProvisionException) ex.getCause();
applicationErrorCode = provEx.getErrorCode();
resp.getError().setErrorText(ex.getCause().getMessage());
} else if (cause instanceof InvalidIDException) {
// This block is called in API WebService
// Should do the same as other InvalidIDException catch
// 404 NOT_FOUND
status = 404;
applicationErrorCode = ApplicationCodeConstants.ID_NOT_FOUND;
resp.getError().setErrorText(ex.getCause().getMessage());
} else {
message = cause.getMessage();
if (message == null) {
message = ex.getCause().toString();
}
applicationErrorCode = ApplicationCodeConstants.INTERNAL_APPLICATION_ERROR;
resp.getError().setErrorText(message);
}
}
} else if (ex instanceof InvalidIDException) {
// Should do the same as other InvalidIDException catch
// 404 NOT_FOUND
status = 404;
applicationErrorCode = ApplicationCodeConstants.ID_NOT_FOUND;
resp.getError().setErrorText(ex.getMessage());
} else if (ex instanceof NotImplementedException) {
// 501 NOT_IMPLEMENTED
status = 501;
applicationErrorCode = ApplicationCodeConstants.NOT_IMPLEMENTED;
resp.getError().setErrorText(ex.getMessage());
} else if (ex instanceof ProvisionException) {
// 500 INTERNAL_SERVER_ERROR
status = 500;
ProvisionException provEx = (ProvisionException) ex.getCause();
applicationErrorCode = provEx.getErrorCode();
resp.getError().setErrorText(ex.getCause().getMessage());
} else {
// 500 INTERNAL_SERVER_ERROR
status = 500;
message = ex.toString();
applicationErrorCode = ApplicationCodeConstants.INTERNAL_APPLICATION_ERROR;
resp.getError().setErrorText("Provisioning Server Error: "+message);
}
StackTraceElement[] stack = cause.getStackTrace();
ArrayList<StackTraceElement> e3Stack = new ArrayList<StackTraceElement>();
for (StackTraceElement elem : stack) {
if (elem.getClassName().indexOf("com.alu.e3") != -1) {
e3Stack.add(elem);
}
}
cause.setStackTrace(e3Stack.toArray(new StackTraceElement[e3Stack.size()]));
if(logger.isErrorEnabled()) {
logger.error("E3 Runtime exception (HTTP status:" + status + ")", cause);
}
resp.getError().setErrorCode(String.valueOf(applicationErrorCode));
ResponseBuilder builder = Response.status(status).type(MediaType.APPLICATION_XML).header("X-Application-Error-Code", applicationErrorCode).entity(resp);
if(status == 401){
builder.header("WWW-Authenticate", "Basic");
}