Java Code Examples for javax.ws.rs.WebApplicationException#getCause()

The following examples show how to use javax.ws.rs.WebApplicationException#getCause() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GsonWebRenderer.java    From greenbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Response toResponse(Exception exception) {
	int statusCode = Status.INTERNAL_SERVER_ERROR.getStatusCode();
	ExceptionContent exceptionContent;
	if (exception instanceof WebApplicationException) {
		WebApplicationException applicationException = (WebApplicationException) exception;
		statusCode = applicationException.getResponse().getStatus();
		Object entity = applicationException.getResponse().getEntity();
		if (entity instanceof ExceptionContent) {
			exceptionContent = (ExceptionContent) entity;
		} else {
			exceptionContent = new ExceptionContent(applicationException.getCause() == null ? applicationException
					: applicationException.getCause());
		}
	} else {
		exceptionContent = new ExceptionContent(exception);
	}
	return Response.status(statusCode).entity(exceptionContent).build();
}
 
Example 2
Source File: ODataExceptionWrapper.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private List<Locale> getLanguages(final ODataContext context) {
  try {
    if (context.getAcceptableLanguages().isEmpty()) {
      return Arrays.asList(DEFAULT_RESPONSE_LOCALE);
    }
    return context.getAcceptableLanguages();
  } catch (WebApplicationException e) {
    if (e.getCause() != null && e.getCause().getClass() == ParseException.class) {
      // invalid accept-language string in http header
      // compensate exception with using default locale
      return Arrays.asList(DEFAULT_RESPONSE_LOCALE);
    }
    // not able to compensate exception -> re-throw
    throw e;
  }
}
 
Example 3
Source File: ODataExceptionWrapper.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private List<Locale> getLanguages(final HttpHeaders httpHeaders) {
  try {
    if (httpHeaders.getAcceptableLanguages().isEmpty()) {
      return Arrays.asList(DEFAULT_RESPONSE_LOCALE);
    }
    return httpHeaders.getAcceptableLanguages();
  } catch (WebApplicationException e) {
    if (e.getCause() != null && e.getCause().getClass() == ParseException.class) {
      // invalid accept-language string in http header
      // compensate exception with using default locale
      return Arrays.asList(DEFAULT_RESPONSE_LOCALE);
    }
    // not able to compensate exception -> re-throw
    throw e;
  }
}
 
Example 4
Source File: ODataExceptionWrapper.java    From cloud-odata-java with Apache License 2.0 6 votes vote down vote up
private List<Locale> getLanguages(final ODataContext context) {
  try {
    if (context.getAcceptableLanguages().isEmpty()) {
      return Arrays.asList(DEFAULT_RESPONSE_LOCALE);
    }
    return context.getAcceptableLanguages();
  } catch (WebApplicationException e) {
    if (e.getCause() != null && e.getCause().getClass() == ParseException.class) {
      // invalid accept-language string in http header
      // compensate exception with using default locale
      return Arrays.asList(DEFAULT_RESPONSE_LOCALE);
    }
    // not able to compensate exception -> re-throw
    throw e;
  }
}
 
Example 5
Source File: ODataExceptionWrapper.java    From cloud-odata-java with Apache License 2.0 6 votes vote down vote up
private List<Locale> getLanguages(final HttpHeaders httpHeaders) {
  try {
    if (httpHeaders.getAcceptableLanguages().isEmpty()) {
      return Arrays.asList(DEFAULT_RESPONSE_LOCALE);
    }
    return httpHeaders.getAcceptableLanguages();
  } catch (WebApplicationException e) {
    if (e.getCause() != null && e.getCause().getClass() == ParseException.class) {
      // invalid accept-language string in http header
      // compensate exception with using default locale
      return Arrays.asList(DEFAULT_RESPONSE_LOCALE);
    }
    // not able to compensate exception -> re-throw
    throw e;
  }
}
 
Example 6
Source File: TitusExceptionMapper.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private Response fromWebApplicationException(WebApplicationException e) {
    int status = e.getResponse().getStatus();

    Throwable cause = e.getCause() == null ? e : e.getCause();
    String errorMessage = toStandardHttpErrorMessage(status, cause);

    ErrorResponse errorResponse = ErrorResponse.newError(status, errorMessage)
            .clientRequest(httpServletRequest)
            .serverContext()
            .exceptionContext(cause)
            .build();
    return Response.status(status).entity(errorResponse).build();
}
 
Example 7
Source File: WebApplicationExceptionMapper.java    From ctsms with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public Response toResponse(WebApplicationException ex) {
	if (ex != null && ex.getCause() instanceof AuthorisationException) {
		return buildJsonResponse(AuthorisationExceptionMapper.STATUS, ex.getCause()).build();
	}
	return buildJsonResponse(Status.INTERNAL_SERVER_ERROR, ex).build();
}
 
Example 8
Source File: WebApplicationExceptionMapper.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected String buildErrorMessage(Response r, WebApplicationException ex) {
    StringBuilder sb = new StringBuilder();
    sb.append(ERROR_MESSAGE_START).append(r.getStatus());

    Throwable cause = ex.getCause();
    String message = cause == null ? ex.getMessage() : cause.getMessage();
    if (message == null && cause != null) {
        message = "exception cause class: " + cause.getClass().getName();
    }
    if (message != null) {
        sb.append(", message: ").append(message);
    }
    return sb.toString();
}
 
Example 9
Source File: RequestHandlerImpl.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
private void handleWebApplicationException(WebApplicationException webApplicationException, GenericContainerResponse response) {
    LOG.debug("WebApplicationException occurs", webApplicationException);
    ErrorPages errorPages = (ErrorPages)EnvironmentContext.getCurrent().get(ErrorPages.class);

    Response errorResponse = webApplicationException.getResponse();
    int errorStatus = errorResponse.getStatus();
    Throwable cause = webApplicationException.getCause();

    propagateErrorIfHaveErrorPage(webApplicationException, errorPages);
    propagateErrorIfHaveErrorPage(cause, errorPages);
    propagateErrorIfHaveErrorPage(errorStatus, errorPages);

    if (Tracer.isTracingEnabled()) {
        Tracer.trace("WebApplicationException occurs, cause = (%s)", cause);
    }

    if (errorResponse.hasEntity()) {
        setupInternalResponseHeaders(errorStatus, errorResponse.getMetadata());
    } else {
        ExceptionMapper exceptionMapper = providers.getExceptionMapper(WebApplicationException.class);
        if (exceptionMapper != null) {
            if (Tracer.isTracingEnabled()) {
                Tracer.trace("Found ExceptionMapper for WebApplicationException = (%s)", exceptionMapper);
            }
            errorResponse = exceptionMapper.toResponse(webApplicationException);
        } else if (cause != null) {
            if (isNullOrEmpty(cause.getMessage())) {
                errorResponse = createErrorResponse(errorStatus, cause.toString());
            } else {
                errorResponse = createErrorResponse(errorStatus, cause.getMessage());
            }
        } else if (!isNullOrEmpty(webApplicationException.getMessage())) {
            errorResponse = createErrorResponse(errorStatus, webApplicationException.getMessage());
        }
    }
    response.setResponse(errorResponse);
}