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

The following examples show how to use org.apache.olingo.odata2.api.processor.ODataErrorContext#setContentType() . 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: JsonErrorDocumentConsumer.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private ODataErrorContext parseJson(final JsonReader reader) throws IOException, EntityProviderException {
  ODataErrorContext errorContext;

  if (reader.hasNext()) {
    reader.beginObject();
    String currentName = reader.nextName();
    if (FormatJson.ERROR.equals(currentName)) {
      errorContext = parseError(reader);
    } else {
      throw new EntityProviderException(EntityProviderException.INVALID_STATE.addContent(
          "Invalid object with name '" + currentName + "' found."));
    }
  } else {
    throw new EntityProviderException(EntityProviderException.INVALID_STATE.addContent(
        "No content to parse found."));
  }

  errorContext.setContentType(ContentType.APPLICATION_JSON.toContentTypeString());
  return errorContext;
}
 
Example 2
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 3
Source File: JsonErrorDocumentDeserializer.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param reader
 * @return ODataErrorContext
 * @throws IOException
 * @throws EntityProviderException
 */
private ODataErrorContext parseJson(final JsonReader reader) throws IOException, EntityProviderException {
  ODataErrorContext errorContext;

  if (reader.hasNext()) {
    reader.beginObject();
    String currentName = reader.nextName();
    if (FormatJson.ERROR.equals(currentName)) {
      errorContext = parseError(reader);
    } else {
      throw new EntityProviderException(EntityProviderException.INVALID_STATE.addContent(
          "Invalid object with name '" + currentName + FOUND));
    }
  } else {
    throw new EntityProviderException(EntityProviderException.INVALID_STATE.addContent(
        "No content to parse found."));
  }

  errorContext.setContentType(ContentType.APPLICATION_JSON.toContentTypeString());
  return errorContext;
}
 
Example 4
Source File: XmlErrorDocumentConsumer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private ODataErrorContext parserError(final XMLStreamReader reader)
    throws XMLStreamException, EntityProviderException {
  // read xml tag
  reader.require(XMLStreamConstants.START_DOCUMENT, null, null);
  reader.nextTag();

  // read error tag
  reader.require(XMLStreamConstants.START_ELEMENT, Edm.NAMESPACE_M_2007_08, FormatXml.M_ERROR);

  // read error data
  boolean codeFound = false;
  boolean messageFound = false;
  ODataErrorContext errorContext = new ODataErrorContext();
  while (notFinished(reader)) {
    reader.nextTag();
    if (reader.isStartElement()) {
      String name = reader.getLocalName();
      if (FormatXml.M_CODE.equals(name)) {
        codeFound = true;
        handleCode(reader, errorContext);
      } else if (FormatXml.M_MESSAGE.equals(name)) {
        messageFound = true;
        handleMessage(reader, errorContext);
      } else if (FormatXml.M_INNER_ERROR.equals(name)) {
        handleInnerError(reader, errorContext);
      } else {
        throw new EntityProviderException(
            EntityProviderException.INVALID_CONTENT.addContent(name, FormatXml.M_ERROR));
      }
    }
  }
  validate(codeFound, messageFound);

  errorContext.setContentType(ContentType.APPLICATION_XML.toContentTypeString());
  return errorContext;
}
 
Example 5
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 6
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);
}
 
Example 7
Source File: XmlErrorDocumentDeserializer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private ODataErrorContext parserError(final XMLStreamReader reader)
    throws XMLStreamException, EntityProviderException {
  // read xml tag
  reader.require(XMLStreamConstants.START_DOCUMENT, null, null);
  reader.nextTag();

  // read error tag
  reader.require(XMLStreamConstants.START_ELEMENT, Edm.NAMESPACE_M_2007_08, FormatXml.M_ERROR);

  // read error data
  boolean codeFound = false;
  boolean messageFound = false;
  ODataErrorContext errorContext = new ODataErrorContext();
  while (notFinished(reader)) {
    reader.nextTag();
    if (reader.isStartElement()) {
      String name = reader.getLocalName();
      if (FormatXml.M_CODE.equals(name)) {
        codeFound = true;
        handleCode(reader, errorContext);
      } else if (FormatXml.M_MESSAGE.equals(name)) {
        messageFound = true;
        handleMessage(reader, errorContext);
      } else if (FormatXml.M_INNER_ERROR.equals(name)) {
        handleInnerError(reader, errorContext);
      } else {
        throw new EntityProviderException(
            EntityProviderException.INVALID_CONTENT.addContent(name, FormatXml.M_ERROR));
      }
    }
  }
  validate(codeFound, messageFound);

  errorContext.setContentType(ContentType.APPLICATION_XML.toContentTypeString());
  return errorContext;
}