Java Code Examples for org.apache.olingo.odata2.api.processor.ODataErrorContext#setHttpStatus()

The following examples show how to use org.apache.olingo.odata2.api.processor.ODataErrorContext#setHttpStatus() . 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: JsonErrorProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private void testSerializeJSON(final String errorCode, final String message, final Locale locale) throws Exception {
  ODataErrorContext ctx = new ODataErrorContext();
  ctx.setContentType(HttpContentType.APPLICATION_JSON);
  ctx.setErrorCode(errorCode);
  ctx.setHttpStatus(HttpStatusCodes.INTERNAL_SERVER_ERROR);
  ctx.setLocale(locale);
  ctx.setMessage(message);

  ODataResponse response = new ProviderFacadeImpl().writeErrorDocument(ctx);
  assertNull("EntitypProvider must not set content header", response.getContentHeader());
  assertEquals(ODataServiceVersion.V10, response.getHeader(ODataHttpHeaders.DATASERVICEVERSION));
  final String jsonErrorMessage = StringHelper.inputStreamToString((InputStream) response.getEntity());
  assertEquals("{\"error\":{\"code\":"
      + (errorCode == null ? "null" : "\"" + errorCode + "\"")
      + ","
      + "\"message\":{\"lang\":"
      + (locale == null ? "null" : ("\"" + locale.getLanguage()
          + (locale.getCountry().isEmpty() ? "" : ("-" + locale.getCountry())) + "\""))
      + ",\"value\":" + (message == null ? "null" : "\"" + message + "\"") + "}}}",
      jsonErrorMessage);
}
 
Example 2
Source File: SimpleODataErrorCallback.java    From cloud-sfsf-benefits-ext with Apache License 2.0 6 votes vote down vote up
@Override
public ODataResponse handleError(final ODataErrorContext context) throws ODataApplicationException {
	Throwable rootCause = context.getException();
	LOGGER.error("Error in the OData. Reason: " + rootCause.getMessage(), rootCause); //$NON-NLS-1$

	Throwable innerCause = rootCause.getCause();
	if (rootCause instanceof ODataJPAException && innerCause != null && innerCause instanceof AppODataException) {
		context.setMessage(innerCause.getMessage());
		Throwable childInnerCause = innerCause.getCause();
		context.setInnerError(childInnerCause != null ? childInnerCause.getMessage() : ""); //$NON-NLS-1$
	} else {
		context.setMessage(HttpStatusCodes.INTERNAL_SERVER_ERROR.getInfo());
		context.setInnerError(rootCause.getMessage());
	}

	context.setHttpStatus(HttpStatusCodes.INTERNAL_SERVER_ERROR);
	return EntityProvider.writeErrorDocument(context);
}
 
Example 3
Source File: ODataExceptionMapperImpl.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private ODataErrorContext createErrorContext(final WebApplicationException exception) {
  ODataErrorContext context = new ODataErrorContext();
  if (uriInfo != null) {
    context.setRequestUri(uriInfo.getRequestUri());
  }
  if (httpHeaders != null && httpHeaders.getRequestHeaders() != null) {
    MultivaluedMap<String, String> requestHeaders = httpHeaders.getRequestHeaders();
    Set<Entry<String, List<String>>> entries = requestHeaders.entrySet();
    for (Entry<String, List<String>> entry : entries) {
      context.putRequestHeader(entry.getKey(), entry.getValue());
    }
  }
  context.setContentType(getContentType().toContentTypeString());
  context.setException(exception);
  context.setErrorCode(null);
  context.setMessage(exception.getMessage());
  context.setLocale(DEFAULT_RESPONSE_LOCALE);
  HttpStatusCodes statusCode = HttpStatusCodes.fromStatusCode(exception.getResponse().getStatus());
  context.setHttpStatus(statusCode);
  if (statusCode == HttpStatusCodes.METHOD_NOT_ALLOWED) {
    // RFC 2616, 5.1.1: " An origin server SHOULD return the status code
    // 405 (Method Not Allowed) if the method is known by the origin server
    // but not allowed for the requested resource, and 501 (Not Implemented)
    // if the method is unrecognized or not implemented by the origin server."
    // Since all recognized methods are handled elsewhere, we unconditionally
    // switch to 501 here for not-allowed exceptions thrown directly from
    // JAX-RS implementations.
    context.setHttpStatus(HttpStatusCodes.NOT_IMPLEMENTED);
    context.setMessage("The request dispatcher does not allow the HTTP method used for the request.");
    context.setLocale(Locale.ENGLISH);
  }
  return context;
}
 
Example 4
Source File: XmlErrorProducerTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void viaRuntimeDelegate() throws Exception {
  ODataErrorContext context = new ODataErrorContext();
  context.setContentType(contentType);
  context.setHttpStatus(expectedStatus);
  context.setErrorCode(null);
  context.setMessage(null);
  context.setLocale(null);
  context.setInnerError(null);
  ODataResponse response = EntityProvider.writeErrorDocument(context);
  String errorXml = verifyResponse(response);
  verifyXml(null, null, null, null, errorXml);

  context.setErrorCode("a");
  context.setMessage("a");
  context.setLocale(Locale.GERMAN);
  context.setInnerError("a");
  response = EntityProvider.writeErrorDocument(context);
  errorXml = verifyResponse(response);
  verifyXml("a", "a", Locale.GERMAN, "a", errorXml);

  context.setErrorCode(null);
  context.setInnerError(null);
  response = EntityProvider.writeErrorDocument(context);
  errorXml = verifyResponse(response);
  verifyXml(null, "a", Locale.GERMAN, null, errorXml);
}