Java Code Examples for org.apache.olingo.server.api.uri.UriInfo#getSelectOption()

The following examples show how to use org.apache.olingo.server.api.uri.UriInfo#getSelectOption() . 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: CarsProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@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();
  final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
  InputStream serializedContent = serializer.entityCollection(edm, edmEntitySet.getEntityType(), entitySet,
      EntityCollectionSerializerOptions.with()
          .id(id)
          .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());
}
 
Example 2
Source File: CarsProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@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());
  }
}
 
Example 3
Source File: TechnicalEntityProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private void readEntity(final ODataRequest request, final ODataResponse response, final UriInfo uriInfo,
    final ContentType requestedFormat, final boolean isReference)
    throws ODataApplicationException, ODataLibraryException {
  //
  if (odata.createPreferences(request.getHeaders(HttpHeader.PREFER)).hasRespondAsync()) {
    TechnicalAsyncService asyncService = TechnicalAsyncService.getInstance();
    TechnicalEntityProcessor processor = new TechnicalEntityProcessor(dataProvider, serviceMetadata);
    processor.init(odata, serviceMetadata);
    AsyncProcessor<EntityProcessor> asyncProcessor = asyncService.register(processor, EntityProcessor.class);
    asyncProcessor.prepareFor().readEntity(request, response, uriInfo, requestedFormat);
    String location = asyncProcessor.processAsync();
    TechnicalAsyncService.acceptedResponse(response, location);
    //
    return;
  }
  //

  final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo);
  
  //for Singleton/$ref edmEntityset will be null throw error
  validateSingletonRef(isReference,edmEntitySet);
 
  EdmEntityType edmEntityType = null;
  edmEntityType = getEdmTypeForContNavProperty(uriInfo);
  final boolean iscontNav = checkIfContNavigation(uriInfo);
  if (edmEntityType == null) {
    edmEntityType = getEdmType(uriInfo, edmEntitySet);
  }

  final Entity entity = readEntity(uriInfo);

  if (odata.createETagHelper().checkReadPreconditions(entity.getETag(),
      request.getHeaders(HttpHeader.IF_MATCH),
      request.getHeaders(HttpHeader.IF_NONE_MATCH))) {
    response.setStatusCode(HttpStatusCode.NOT_MODIFIED.getStatusCode());
    response.setHeader(HttpHeader.ETAG, entity.getETag());
    return;
  }

  final ExpandOption expand = uriInfo.getExpandOption();
  final SelectOption select = uriInfo.getSelectOption();

  final ExpandSystemQueryOptionHandler expandHandler = new ExpandSystemQueryOptionHandler();
  final Entity entitySerialization = expandHandler.transformEntityGraphToTree(entity, edmEntitySet, expand, null);
  expandHandler.applyExpandQueryOptions(entitySerialization, edmEntitySet, expand, uriInfo,
      serviceMetadata.getEdm());

  final SerializerResult serializerResult = isReference ?
      serializeReference(entity, edmEntitySet, requestedFormat) :
      serializeEntity(request, entitySerialization, edmEntitySet, edmEntityType, 
          requestedFormat, expand, select, iscontNav);

  if (entity.getETag() != null) {
    response.setHeader(HttpHeader.ETAG, entity.getETag());
  }
  response.setContent(serializerResult.getContent());
  response.setStatusCode(HttpStatusCode.OK.getStatusCode());
  response.setHeader(HttpHeader.CONTENT_TYPE, requestedFormat.toContentTypeString());
}
 
Example 4
Source File: TechnicalPrimitiveComplexProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private void readProperty(final ODataRequest request, ODataResponse response, final UriInfo uriInfo,
    final ContentType contentType, final RepresentationType representationType)
        throws ODataApplicationException, ODataLibraryException {
  final UriInfoResource resource = uriInfo.asUriInfoResource();
  validateOptions(resource);
  validatePath(resource);
  final EdmEntitySet edmEntitySet = getEdmEntitySet(resource);

  final List<UriResource> resourceParts = resource.getUriResourceParts();
  final int trailing =
      representationType == RepresentationType.COUNT || representationType == RepresentationType.VALUE ? 1 : 0;
  final List<String> path = getPropertyPath(resourceParts, trailing);

  final Entity entity = readEntity(uriInfo);

  if (entity != null && entity.getETag() != null) {
    if (odata.createETagHelper().checkReadPreconditions(entity.getETag(),
        request.getHeaders(HttpHeader.IF_MATCH),
        request.getHeaders(HttpHeader.IF_NONE_MATCH))) {
      response.setStatusCode(HttpStatusCode.NOT_MODIFIED.getStatusCode());
      response.setHeader(HttpHeader.ETAG, entity.getETag());
      return;
    }
  }

  final Property property = entity == null ?
      getPropertyData(
          dataProvider.readFunctionPrimitiveComplex(((UriResourceFunction) resourceParts.get(0)).getFunction(),
          ((UriResourceFunction) resourceParts.get(0)).getParameters(), resource), path) :
      getData(entity, path, resourceParts, resource);

  // TODO: implement filter on collection properties (on a shallow copy of the values)
  // FilterHandler.applyFilterSystemQuery(uriInfo.getFilterOption(), property, uriInfo, serviceMetadata.getEdm());

  if (property == null && representationType != RepresentationType.COUNT) {
    if (representationType == RepresentationType.VALUE) {
      response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
    } else {
      throw new ODataApplicationException("Nothing found.", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ROOT);
    }
  } else {
    if (property.getValue() == null && representationType != RepresentationType.COUNT) {
      response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
    } else {
      response.setStatusCode(HttpStatusCode.OK.getStatusCode());
      if (representationType == RepresentationType.COUNT) {
        response.setContent(odata.createFixedFormatSerializer().count(
            property.asCollection().size()));
      } else {
        final EdmProperty edmProperty = path.isEmpty() ? null :
            ((UriResourceProperty) resourceParts.get(resourceParts.size() - trailing - 1)).getProperty();
        EdmType type = null;
        if (resourceParts.get(resourceParts.size() - trailing - 1) 
           instanceof UriResourceComplexProperty &&
            ((UriResourceComplexProperty)resourceParts.get(resourceParts.size() - trailing - 1)).
            getComplexTypeFilter() != null) {
          type = ((UriResourceComplexProperty)resourceParts.get(resourceParts.size() - trailing - 1)).
              getComplexTypeFilter();
        }else if(resourceParts.get(resourceParts.size() - trailing - 1) 
           instanceof UriResourceFunction &&
            ((UriResourceFunction)resourceParts.get(resourceParts.size() - trailing - 1)).
            getFunction() != null){ 
          type = ((UriResourceFunction)resourceParts.get(resourceParts.size() - trailing - 1)).
              getType();
        }else {
          type = edmProperty == null ?
              ((UriResourceFunction) resourceParts.get(0)).getType() :
              edmProperty.getType();
        }
        final EdmReturnType returnType = resourceParts.get(0) instanceof UriResourceFunction ?
            ((UriResourceFunction) resourceParts.get(0)).getFunction().getReturnType() : 
              resourceParts.get(1) instanceof UriResourceFunction ? 
                  ((UriResourceFunction) resourceParts.get(1)).getFunction().getReturnType():null ;

        if (representationType == RepresentationType.VALUE) {
          response.setContent(serializePrimitiveValue(property, edmProperty, (EdmPrimitiveType) type, returnType));
        }else if(representationType == RepresentationType.PRIMITIVE && type.getFullQualifiedName()
            .getFullQualifiedNameAsString().equals(EDMSTREAM)){
          response.setContent(odata.createFixedFormatSerializer().binary(dataProvider.readStreamProperty(property)));
          response.setStatusCode(HttpStatusCode.OK.getStatusCode());
          response.setHeader(HttpHeader.CONTENT_TYPE, ((Link)property.getValue()).getType());
          if (entity.getMediaETag() != null) {
            response.setHeader(HttpHeader.ETAG, entity.getMediaETag());
          }
        }else {
          final ExpandOption expand = uriInfo.getExpandOption();
          final SelectOption select = uriInfo.getSelectOption();
          final SerializerResult result = serializeProperty(entity, edmEntitySet, path, property, edmProperty,
              type, returnType, representationType, contentType, expand, select);
          response.setContent(result.getContent());
        }
      }
      response.setHeader(HttpHeader.CONTENT_TYPE, contentType.toContentTypeString());
    }
    if (entity != null && entity.getETag() != null) {
      response.setHeader(HttpHeader.ETAG, entity.getETag());
    }
  }
}