Java Code Examples for org.apache.olingo.commons.api.format.ContentType#isCompatible()

The following examples show how to use org.apache.olingo.commons.api.format.ContentType#isCompatible() . 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: BatchLineReader.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void updateCurrentCharset(final String currentLine) {
  if (currentLine != null) {
    if (currentLine.startsWith(HttpHeader.CONTENT_TYPE)) {
      final ContentType contentType = ContentType.parse(
          currentLine.substring(HttpHeader.CONTENT_TYPE.length() + 1, currentLine.length() - 2).trim());
      if (contentType != null) {
        final String charsetString = contentType.getParameter(ContentType.PARAMETER_CHARSET);
        currentCharset = charsetString == null ?
            contentType.isCompatible(ContentType.APPLICATION_JSON) || contentType.getSubtype().contains("xml") ?
                Charset.forName("UTF-8") :
                DEFAULT_CHARSET :
            Charset.forName(charsetString);

        final String boundary = contentType.getParameter(BOUNDARY);
        if (boundary != null) {
          currentBoundary = DOUBLE_DASH + boundary;
        }
      }
    } else if (CRLF.equals(currentLine)) {
      readState.foundLinebreak();
    } else if (isBoundary(currentLine)) {
      readState.foundBoundary();
    }
  }
}
 
Example 2
Source File: ODataImpl.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataDeserializer createDeserializer(ContentType contentType, ServiceMetadata metadata, List<String> versions)
    throws DeserializerException {
  IConstants constants = new Constantsv00();
  if(versions!=null && !versions.isEmpty() && getMaxVersion(versions)>4){
    constants = new Constantsv01() ;
  }
  if (contentType != null && contentType.isCompatible(ContentType.JSON)) {
    return new ODataJsonDeserializer(contentType, metadata, constants);
  } else if (contentType != null && (contentType.isCompatible(ContentType.APPLICATION_XML)
      || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML))) {
    return new ODataXmlDeserializer(metadata);
  } else {
    throw new DeserializerException("Unsupported format: " + 
  ((contentType != null) ? contentType.toContentTypeString() : null),
        DeserializerException.MessageKeys.UNSUPPORTED_FORMAT, 
        ((contentType != null) ? contentType.toContentTypeString() : null));
  }
}
 
Example 3
Source File: BatchParserCommon.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
/**
 * Get the content type based on <code>contentType</code> parameter.
 * If this content type is not compatible to the expected ContentType a
 * BatchDeserializerException is thrown.
 *
 * @param contentType content type string which is parsed
 * @param expected content type to which the parsed must be compatible
 * @param line parsed line
 * @return the parsed content type or if not compatible or parseable an exception is thrown (never returns null)
 * @throws BatchDeserializerException
 */
public static ContentType parseContentType(final String contentType, final ContentType expected, final int line)
    throws BatchDeserializerException {
  if (contentType == null) {
    throw new BatchDeserializerException("Missing content type",
        BatchDeserializerException.MessageKeys.MISSING_CONTENT_TYPE, Integer.toString(line));
  }
  ContentType type;
  try {
    type = ContentType.create(contentType);
  } catch (final IllegalArgumentException e) {
    throw new BatchDeserializerException("Invalid content type.", e,
        BatchDeserializerException.MessageKeys.INVALID_CONTENT_TYPE, Integer.toString(line));
  }
  if (type.isCompatible(expected)) {
    return type;
  } else {
    throw new BatchDeserializerException("Content type is not the expected content type",
        BatchDeserializerException.MessageKeys.UNEXPECTED_CONTENT_TYPE,
        Integer.toString(line), expected.toContentTypeString(), type.toContentTypeString());
  }
}
 
Example 4
Source File: ODataImpl.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataDeserializer createDeserializer(ContentType contentType, List<String> versions)
    throws DeserializerException {
  IConstants constants = new Constantsv00();
  if(versions!=null && !versions.isEmpty() && getMaxVersion(versions)>4){
    constants = new Constantsv01() ;
  }
  if (contentType != null && contentType.isCompatible(ContentType.JSON)) {
    return new ODataJsonDeserializer(contentType, constants);
  } else if (contentType != null && (contentType.isCompatible(ContentType.APPLICATION_XML)
      || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML))) {
    return new ODataXmlDeserializer();
  } else {
    throw new DeserializerException("Unsupported format: " + 
  ((contentType != null) ? contentType.toContentTypeString() : null),
        DeserializerException.MessageKeys.UNSUPPORTED_FORMAT, 
        ((contentType != null) ? contentType.toContentTypeString() : null));
  }

}
 
Example 5
Source File: ODataImpl.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataSerializer createSerializer(final ContentType contentType) throws SerializerException {
  ODataSerializer serializer = null;

  if (contentType != null && contentType.isCompatible(ContentType.APPLICATION_JSON)) {
    final String metadata = contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA);
    if (metadata == null
        || ContentType.VALUE_ODATA_METADATA_MINIMAL.equalsIgnoreCase(metadata)
        || ContentType.VALUE_ODATA_METADATA_NONE.equalsIgnoreCase(metadata)
        || ContentType.VALUE_ODATA_METADATA_FULL.equalsIgnoreCase(metadata)) {
      serializer = new ODataJsonSerializer(contentType, new Constantsv00());
    }
  } else if (contentType != null && (contentType.isCompatible(ContentType.APPLICATION_XML)
      || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML))) {
    serializer = new ODataXmlSerializer();
  }

  if (serializer == null) {
    throw new SerializerException("Unsupported format: " + 
  ((contentType != null) ? contentType.toContentTypeString() : null),
        SerializerException.MessageKeys.UNSUPPORTED_FORMAT, 
        ((contentType != null) ? contentType.toContentTypeString() : null));
  } else {
    return serializer;
  }
}
 
Example 6
Source File: ClientEntitySetIterator.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param odataClient client instance getting this request
 * @param stream source stream.
 * @param contentType OData format.
 */
public ClientEntitySetIterator(final ODataClient odataClient, final InputStream stream,
                               final ContentType contentType) {

  this.odataClient = odataClient;
  this.stream = stream;
  this.contentType = contentType;
  this.osEntitySet = new ByteArrayOutputStream();
  
  if(contentType.isCompatible(ContentType.APPLICATION_ATOM_SVC)
      || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML)) {
    namespaces = getAllElementAttributes(stream, "feed", osEntitySet);
  } else {
    namespaces = null;
    try {
      if (consume(stream, "\"value\":", osEntitySet, true) >= 0) {
        int c = 0;
        while (c != '[' && (c = stream.read()) >= 0) {
          osEntitySet.write(c);
        }
      }
    } catch (IOException e) {
      LOG.error("Error parsing entity set", e);
      throw new IllegalStateException(e);
    }
  }
}
 
Example 7
Source File: ODataClientImpl.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public ODataSerializer getSerializer(final ContentType contentType) {
  return contentType.isCompatible(ContentType.APPLICATION_ATOM_SVC)
      || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML)
      || contentType.isCompatible(ContentType.APPLICATION_XML) ?
      new AtomSerializer() : new JsonSerializer(false, contentType);
}
 
Example 8
Source File: BatchRequest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void validateContentType() throws ODataApplicationException {
  final ContentType contentType = getRequestContentType();
  if (contentType == null || !contentType.isCompatible(ContentType.MULTIPART_MIXED)) {
    throw new ODataApplicationException("Invalid content type",
        HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.getDefault());
  }
}
 
Example 9
Source File: ODataImpl.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public ODataDeserializer createDeserializer(final ContentType contentType,
    ServiceMetadata metadata) throws DeserializerException {
  if (contentType != null && contentType.isCompatible(ContentType.JSON)) {
    return new ODataJsonDeserializer(contentType, metadata);
  } else if (contentType != null && (contentType.isCompatible(ContentType.APPLICATION_XML)
      || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML))) {
    return new ODataXmlDeserializer(metadata);
  } else {
    throw new DeserializerException("Unsupported format: " + 
  ((contentType != null) ? contentType.toContentTypeString() : null),
        DeserializerException.MessageKeys.UNSUPPORTED_FORMAT, 
        ((contentType != null) ? contentType.toContentTypeString() : null));
  }
}
 
Example 10
Source File: ODataImpl.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public ODataDeserializer createDeserializer(final ContentType contentType) throws DeserializerException {
  if (contentType != null && contentType.isCompatible(ContentType.JSON)) {
    return new ODataJsonDeserializer(contentType);
  } else if (contentType != null && (contentType.isCompatible(ContentType.APPLICATION_XML)
      || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML))) {
    return new ODataXmlDeserializer();
  } else {
    throw new DeserializerException("Unsupported format: " + 
  ((contentType != null) ? contentType.toContentTypeString() : null),
        DeserializerException.MessageKeys.UNSUPPORTED_FORMAT, 
        ((contentType != null) ? contentType.toContentTypeString() : null));
  }
}
 
Example 11
Source File: ODataImpl.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public EdmDeltaSerializer createEdmDeltaSerializer(final ContentType contentType, final List<String> versions)
    throws SerializerException {
  if (contentType != null && contentType.isCompatible(ContentType.APPLICATION_JSON)) {
    if(versions!=null && !versions.isEmpty()){
     return getMaxVersion(versions)>4 ?  new JsonDeltaSerializerWithNavigations(contentType):
       new JsonDeltaSerializer(contentType);
    }
    return new JsonDeltaSerializerWithNavigations(contentType);
  }
  throw new SerializerException("Unsupported format: " + 
  ((contentType != null) ? contentType.toContentTypeString() : null),
      SerializerException.MessageKeys.UNSUPPORTED_FORMAT, 
      ((contentType != null) ? contentType.toContentTypeString() : null));
}
 
Example 12
Source File: ODataImpl.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public EdmAssistedSerializer createEdmAssistedSerializer(final ContentType contentType) throws SerializerException {
  if (contentType != null && contentType.isCompatible(ContentType.APPLICATION_JSON)) {
    return new EdmAssistedJsonSerializer(contentType);
  }
  throw new SerializerException("Unsupported format: " + 
  ((contentType != null) ? contentType.toContentTypeString() : null),
      SerializerException.MessageKeys.UNSUPPORTED_FORMAT, 
      ((contentType != null) ? contentType.toContentTypeString() : null));
}
 
Example 13
Source File: ODataImpl.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public ODataSerializer createSerializer(final ContentType contentType, 
    final List<String> versions) throws SerializerException {
  ODataSerializer serializer = null;
  IConstants constants = new Constantsv00();
  if(versions!=null && !versions.isEmpty() && getMaxVersion(versions) > 4){
    constants = new Constantsv01() ;
  }
  if (contentType != null && contentType.isCompatible(ContentType.APPLICATION_JSON)) {
    final String metadata = contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA);
    if (metadata == null
        || ContentType.VALUE_ODATA_METADATA_MINIMAL.equalsIgnoreCase(metadata)
        || ContentType.VALUE_ODATA_METADATA_NONE.equalsIgnoreCase(metadata)
        || ContentType.VALUE_ODATA_METADATA_FULL.equalsIgnoreCase(metadata)) {
      serializer = new ODataJsonSerializer(contentType, constants);
    }
  } else if (contentType != null && (contentType.isCompatible(ContentType.APPLICATION_XML)
      || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML))) {
    serializer = new ODataXmlSerializer();
  }

  if (serializer == null) {
    throw new SerializerException("Unsupported format: " + 
  ((contentType != null) ? contentType.toContentTypeString() : null),
        SerializerException.MessageKeys.UNSUPPORTED_FORMAT, 
        ((contentType != null) ? contentType.toContentTypeString() : null));
  } else {
    return serializer;
  }
}
 
Example 14
Source File: BatchRequestTransformator.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private Charset getCharset(final BatchQueryOperation operation) {
  final ContentType contentType = ContentType.parse(operation.getHeaders().getHeader(HttpHeader.CONTENT_TYPE));
  if (contentType != null) {
    final String charsetValue = contentType.getParameter(ContentType.PARAMETER_CHARSET);
    if (charsetValue == null) {
      if (contentType.isCompatible(ContentType.APPLICATION_JSON) || contentType.getSubtype().contains("xml")) {
        return Charset.forName("UTF-8");
      }
    } else {
      return Charset.forName(charsetValue);
    }
  }
  return Charset.forName("ISO-8859-1");
}
 
Example 15
Source File: EntityTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private void singleton(final ContentType contentType) throws Exception {
  final InputStream input = getClass().getResourceAsStream("VipCustomer." + getSuffix(contentType));
  final ClientEntity entity = client.getBinder().getODataEntity(
      client.getDeserializer(contentType).toEntity(input));
  assertNotNull(entity);

  assertEquals("Microsoft.Test.OData.Services.ODataWCFService.Customer", entity.getTypeName().toString());

  final ClientProperty birthday = entity.getProperty("Birthday");
  assertTrue(birthday.hasPrimitiveValue());
  assertEquals(EdmDateTimeOffset.getInstance(), birthday.getPrimitiveValue().getType());

  final ClientProperty timeBetweenLastTwoOrders = entity.getProperty("TimeBetweenLastTwoOrders");
  assertTrue(timeBetweenLastTwoOrders.hasPrimitiveValue());
  assertEquals(EdmDuration.getInstance(), timeBetweenLastTwoOrders.getPrimitiveValue().getType());

  int checked = 0;
  for (ClientLink link : entity.getNavigationLinks()) {
    if ("Parent".equals(link.getName())) {
      checked++;
      assertEquals(ClientLinkType.ENTITY_NAVIGATION, link.getType());
    }
    if ("Orders".equals(link.getName())) {
      checked++;
      if (contentType.isCompatible(ContentType.APPLICATION_ATOM_SVC)
          || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML)) {
        assertEquals(ClientLinkType.ENTITY_SET_NAVIGATION, link.getType());
      }
    }
    if ("Company".equals(link.getName())) {
      checked++;
      assertEquals(ClientLinkType.ENTITY_NAVIGATION, link.getType());
    }
  }
  assertEquals(3, checked);

  assertEquals(2, entity.getOperations().size());
  assertEquals("#Microsoft.Test.OData.Services.ODataWCFService.ResetAddress",
      entity.getOperation("Microsoft.Test.OData.Services.ODataWCFService.ResetAddress").getMetadataAnchor());
  assertEquals("#Microsoft.Test.OData.Services.ODataWCFService.GetHomeAddress",
      entity.getOperation("Microsoft.Test.OData.Services.ODataWCFService.GetHomeAddress").getMetadataAnchor());

  // operations won't get serialized
  entity.getOperations().clear();
  final ClientEntity written = client.getBinder().getODataEntity(
      new ResWrap<Entity>((URI) null, null, client.getBinder().getEntity(entity)));
  assertEquals(entity, written);
  input.close();
}
 
Example 16
Source File: ContentTypeHelper.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static boolean isODataMetadataNone(final ContentType contentType) {
  return contentType.isCompatible(ContentType.APPLICATION_JSON)
      && ContentType.VALUE_ODATA_METADATA_NONE.equalsIgnoreCase(
          contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA));
}
 
Example 17
Source File: TechnicalProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
protected boolean isODataMetadataNone(final ContentType contentType) {
  return contentType.isCompatible(ContentType.APPLICATION_JSON)
      && ContentType.VALUE_ODATA_METADATA_NONE.equalsIgnoreCase(
          contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA));
}
 
Example 18
Source File: DemoActionProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
protected boolean isODataMetadataNone(final ContentType contentType) {
  return contentType.isCompatible(ContentType.APPLICATION_JSON)
      && ContentType.VALUE_ODATA_METADATA_NONE.equalsIgnoreCase(
          contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA));
}
 
Example 19
Source File: StaticContentController.java    From teiid-spring-boot with Apache License 2.0 4 votes vote down vote up
static void writeError(ServletRequest request, String code, String message,
        HttpServletResponse httpResponse, int statusCode) throws IOException {
    httpResponse.setStatus(statusCode);
    String format = request.getParameter("$format"); //$NON-NLS-1$
    if (format == null) {
        //TODO: could also look at the accepts header
        ContentType contentType = ContentType.parse(request.getContentType());
        if (contentType == null || contentType.isCompatible(ContentType.APPLICATION_JSON)) {
            format = "json"; //$NON-NLS-1$
        } else {
            format = "xml"; //$NON-NLS-1$
        }
    }
    PrintWriter writer = httpResponse.getWriter();
    if (code == null) {
        code = "";
    }
    if (message == null) {
        message = "";
    }
    if (format.equalsIgnoreCase("json")) { //$NON-NLS-1$
        httpResponse.setHeader(HttpHeader.CONTENT_TYPE, ContentType.APPLICATION_JSON.toContentTypeString());
        writer.write("{ \"error\": { \"code\": \""); //$NON-NLS-1$
        JSONParser.escape(code, writer);
        writer.write("\", \"message\": \""); //$NON-NLS-1$
        JSONParser.escape(message, writer);
        writer.write("\" } }"); //$NON-NLS-1$
    } else {
        try {
            httpResponse.setHeader(HttpHeader.CONTENT_TYPE, ContentType.APPLICATION_XML.toContentTypeString());
            XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
            xmlStreamWriter.writeStartElement("m", "error", "http://docs.oasis-open.org/odata/ns/metadata"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            xmlStreamWriter.writeNamespace("m", "http://docs.oasis-open.org/odata/ns/metadata"); //$NON-NLS-1$ //$NON-NLS-2$
            xmlStreamWriter.writeStartElement("m", "code", "http://docs.oasis-open.org/odata/ns/metadata"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            xmlStreamWriter.writeCharacters(code);
            xmlStreamWriter.writeEndElement();
            xmlStreamWriter.writeStartElement("m", "message", "http://docs.oasis-open.org/odata/ns/metadata"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            xmlStreamWriter.writeCharacters(message);
            xmlStreamWriter.writeEndElement();
            xmlStreamWriter.writeEndElement();
            xmlStreamWriter.flush();
        } catch (XMLStreamException x) {
            throw new IOException(x);
        }
    }
    writer.close();
}
 
Example 20
Source File: AbstractTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
protected String getSuffix(final ContentType contentType) {
  return contentType.isCompatible(ContentType.APPLICATION_ATOM_SVC)
      || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML)
      || contentType.isCompatible(ContentType.APPLICATION_XML) ? "xml": "json";
}