/**
  * 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 {
   ContentType type;
   try {
     type = ContentType.create(contentType);
   } catch (final IllegalArgumentException e) {
     if (contentType == null) {
       throw new BatchDeserializerException(
           "Missing content type",
           e,
           BatchDeserializerException.MessageKeys.MISSING_CONTENT_TYPE,
           Integer.toString(line));
     } else {
       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());
   }
 }
 public static String getBoundary(final String contentType, final int line)
     throws BatchDeserializerException {
   final ContentType type = parseContentType(contentType, ContentType.MULTIPART_MIXED, line);
   final Map<String, String> parameters = type.getParameters();
   for (final Map.Entry<String, String> entries : parameters.entrySet()) {
     if (BOUNDARY.equalsIgnoreCase(entries.getKey())) {
       final String boundary = entries.getValue().trim();
       if (boundary.matches(PATTERN_BOUNDARY)) {
         return trimQuotes(boundary);
       } else {
         throw new BatchDeserializerException(
             "Invalid boundary format",
             BatchDeserializerException.MessageKeys.INVALID_BOUNDARY,
             Integer.toString(line));
       }
     }
   }
   throw new BatchDeserializerException(
       "Missing boundary.",
       BatchDeserializerException.MessageKeys.MISSING_BOUNDARY_DELIMITER,
       Integer.toString(line));
 }
  private void rawRequest(final ContentType contentType) {
    final URIBuilder uriBuilder =
        client.newURIBuilder(testStaticServiceRootURL).appendEntitySetSegment("People");

    final ODataRawRequest req =
        client.getRetrieveRequestFactory().getRawRequest(uriBuilder.build());
    req.setFormat(contentType.toContentTypeString());

    final ODataRawResponse res = req.execute();
    assertNotNull(res);

    final ResWrap<ClientEntitySet> entitySet = res.getBodyAs(ClientEntitySet.class);
    assertNotNull(entitySet.getPayload());
    assertTrue(entitySet.getContextURL().toASCIIString().endsWith("$metadata#People"));
  }
  @Override
  public void readEntity(
      final ODataRequest request,
      ODataResponse response,
      final UriInfo uriInfo,
      final ContentType requestedContentType)
      throws ODataApplicationException, SerializerException {
    // First we have to figure out which entity set the requested entity is in
    final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource());

    // Next we fetch the requested entity from the database
    Entity entity;
    try {
      entity = readEntityInternal(uriInfo.asUriInfoResource(), edmEntitySet);
    } catch (DataProviderException e) {
      throw new ODataApplicationException(e.getMessage(), 500, Locale.ENGLISH);
    }

    if (entity == null) {
      // If no entity was found for the given key we throw an exception.
      throw new ODataApplicationException(
          "No entity found for this key", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
    } else {
      // If an entity was found we proceed by serializing it and sending it to the client.
      ODataSerializer serializer = odata.createSerializer(requestedContentType);
      final ExpandOption expand = uriInfo.getExpandOption();
      final SelectOption select = uriInfo.getSelectOption();
      InputStream serializedContent =
          serializer
              .entity(
                  edm,
                  edmEntitySet.getEntityType(),
                  entity,
                  EntitySerializerOptions.with()
                      .contextURL(
                          isODataMetadataNone(requestedContentType)
                              ? null
                              : getContextUrl(edmEntitySet, true, expand, select, null))
                      .expand(expand)
                      .select(select)
                      .build())
              .getContent();
      response.setContent(serializedContent);
      response.setStatusCode(HttpStatusCode.OK.getStatusCode());
      response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString());
    }
  }
  @Override
  public void readEntityCollection(
      final ODataRequest request,
      ODataResponse response,
      final UriInfo uriInfo,
      final ContentType requestedContentType)
      throws ODataApplicationException, SerializerException {
    // First we have to figure out which entity set to use
    final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource());

    // Second we fetch the data for this specific entity set from the mock database and transform it
    // into an EntitySet
    // object which is understood by our serialization
    EntityCollection entitySet = dataProvider.readAll(edmEntitySet);

    // Next we create a serializer based on the requested format. This could also be a custom format
    // but we do not
    // support them in this example
    ODataSerializer serializer = odata.createSerializer(requestedContentType);

    // Now the content is serialized using the serializer.
    final ExpandOption expand = uriInfo.getExpandOption();
    final SelectOption select = uriInfo.getSelectOption();
    InputStream serializedContent =
        serializer
            .entityCollection(
                edm,
                edmEntitySet.getEntityType(),
                entitySet,
                EntityCollectionSerializerOptions.with()
                    .contextURL(
                        isODataMetadataNone(requestedContentType)
                            ? null
                            : getContextUrl(edmEntitySet, false, expand, select, null))
                    .count(uriInfo.getCountOption())
                    .expand(expand)
                    .select(select)
                    .build())
            .getContent();

    // Finally we set the response data, headers and status code
    response.setContent(serializedContent);
    response.setStatusCode(HttpStatusCode.OK.getStatusCode());
    response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString());
  }
    @Override
    @SuppressWarnings("unchecked")
    public E getBody() {
      if (entity == null) {
        try {
          final ResWrap<Entity> resource =
              odataClient
                  .getDeserializer(ContentType.parse(getContentType()))
                  .toEntity(getRawResponse());

          entity = (E) odataClient.getBinder().getODataEntity(resource);
        } catch (ODataDeserializerException e) {
          throw new IllegalArgumentException(e);
        } finally {
          this.close();
        }
      }
      return entity;
    }
  private void readWithInlineCount(final ODataClient client, final ContentType contentType) {
    final URIBuilder uriBuilder =
        client.newURIBuilder(testStaticServiceRootURL).appendEntitySetSegment("People").count(true);

    final ODataRawRequest req =
        client.getRetrieveRequestFactory().getRawRequest(uriBuilder.build());
    req.setFormat(contentType.toContentTypeString());

    final ODataRawResponse res = req.execute();
    assertNotNull(res);

    final ResWrap<ClientEntitySet> entitySet = res.getBodyAs(ClientEntitySet.class);
    assertEquals(5, entitySet.getPayload().getEntities().size());

    assertEquals(
        "Microsoft.Test.OData.Services.ODataWCFService.Address",
        entitySet
            .getPayload()
            .getEntities()
            .get(2)
            .getProperty("HomeAddress")
            .getComplexValue()
            .getTypeName());
  }
 public static boolean isODataMetadataNone(final ContentType contentType) {
   return contentType.isCompatible(ContentType.APPLICATION_JSON)
       && ContentType.VALUE_ODATA_METADATA_NONE.equals(
           contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA));
 }
  private void readProperty(
      ODataResponse response, UriInfo uriInfo, ContentType contentType, boolean complex)
      throws ODataApplicationException, SerializerException {
    // To read a property we have to first get the entity out of the entity set
    final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource());
    Entity entity;
    try {
      entity = readEntityInternal(uriInfo.asUriInfoResource(), edmEntitySet);
    } catch (DataProviderException e) {
      throw new ODataApplicationException(
          e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ENGLISH);
    }

    if (entity == null) {
      // If no entity was found for the given key we throw an exception.
      throw new ODataApplicationException(
          "No entity found for this key", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
    } else {
      // Next we get the property value from the entity and pass the value to serialization
      UriResourceProperty uriProperty =
          (UriResourceProperty)
              uriInfo.getUriResourceParts().get(uriInfo.getUriResourceParts().size() - 1);
      EdmProperty edmProperty = uriProperty.getProperty();
      Property property = entity.getProperty(edmProperty.getName());
      if (property == null) {
        throw new ODataApplicationException(
            "No property found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
      } else {
        if (property.getValue() == null) {
          response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
        } else {
          ODataSerializer serializer = odata.createSerializer(contentType);
          final ContextURL contextURL =
              isODataMetadataNone(contentType)
                  ? null
                  : getContextUrl(edmEntitySet, true, null, null, edmProperty.getName());
          InputStream serializerContent =
              complex
                  ? serializer
                      .complex(
                          edm,
                          (EdmComplexType) edmProperty.getType(),
                          property,
                          ComplexSerializerOptions.with().contextURL(contextURL).build())
                      .getContent()
                  : serializer
                      .primitive(
                          edm,
                          (EdmPrimitiveType) edmProperty.getType(),
                          property,
                          PrimitiveSerializerOptions.with()
                              .contextURL(contextURL)
                              .scale(edmProperty.getScale())
                              .nullable(edmProperty.isNullable())
                              .precision(edmProperty.getPrecision())
                              .maxLength(edmProperty.getMaxLength())
                              .unicode(edmProperty.isUnicode())
                              .build())
                      .getContent();
          response.setContent(serializerContent);
          response.setStatusCode(HttpStatusCode.OK.getStatusCode());
          response.setHeader(HttpHeader.CONTENT_TYPE, contentType.toContentTypeString());
        }
      }
    }
  }