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

The following examples show how to use org.apache.olingo.server.api.uri.UriInfo#asUriInfoResource() . 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: TechnicalPrimitiveComplexProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void deleteProperty(final ODataRequest request, ODataResponse response, final UriInfo uriInfo,
    final boolean isValue) throws ODataLibraryException, ODataApplicationException {
  final UriInfoResource resource = uriInfo.asUriInfoResource();
  validatePath(resource);
  getEdmEntitySet(uriInfo); // including checks

  Entity entity = readEntity(uriInfo);
  odata.createETagHelper().checkChangePreconditions(entity.getETag(),
      request.getHeaders(HttpHeader.IF_MATCH),
      request.getHeaders(HttpHeader.IF_NONE_MATCH));

  final List<UriResource> resourceParts = resource.getUriResourceParts();
  final int trailing = isValue ? 1 : 0;
  final List<String> path = getPropertyPath(resourceParts, trailing);

  Property property = getPropertyData(entity, path);

  final EdmProperty edmProperty = ((UriResourceProperty) resourceParts.get(resourceParts.size() - trailing - 1))
      .getProperty();

  if (edmProperty.isNullable()) {
    property.setValue(property.getValueType(), edmProperty.isCollection() ? Collections.emptyList() : null);
    dataProvider.updateETag(entity);
    response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
    if (entity.getETag() != null) {
      response.setHeader(HttpHeader.ETAG, entity.getETag());
    }
  } else {
    throw new ODataApplicationException("Not nullable.", HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ROOT);
  }
}
 
Example 2
Source File: DataRequest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
static ContextURL.Builder buildEntitySetContextURL(UriHelper helper,
    EdmBindingTarget edmEntitySet, List<UriParameter> keyPredicates, UriInfo uriInfo,
    LinkedList<UriResourceNavigation> navigations, boolean collectionReturn, boolean singleton, ODataRequest request)
    throws SerializerException {

  ContextURL.Builder builder =
      ContextURL.with().entitySetOrSingletonOrType(getTargetEntitySet(edmEntitySet, navigations));
  String select = helper.buildContextURLSelectList(edmEntitySet.getEntityType(),
      uriInfo.getExpandOption(), uriInfo.getSelectOption());
  if (!singleton) {
    builder.suffix(collectionReturn ? null : Suffix.ENTITY);
  }

  setServiceRoot(builder, request);    
  builder.selectList(select);

  final UriInfoResource resource = uriInfo.asUriInfoResource();
  final List<UriResource> resourceParts = resource.getUriResourceParts();
  final List<String> path = getPropertyPath(resourceParts);
  String propertyPath = buildPropertyPath(path);
  final String navPath = buildNavPath(helper, edmEntitySet.getEntityType(), navigations, collectionReturn);
  if (navPath != null && !navPath.isEmpty()) {
    if (keyPredicates != null) {
      builder.keyPath(helper.buildContextURLKeyPredicate(keyPredicates));
    }
    if (propertyPath != null) {
      propertyPath = navPath+"/"+propertyPath;
    } else {
      propertyPath = navPath;
    }
  }
  builder.navOrPropertyPath(propertyPath);
  return builder;
}
 
Example 3
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());
    }
  }
}
 
Example 4
Source File: TechnicalPrimitiveComplexProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private void updateProperty(final ODataRequest request, ODataResponse response, final UriInfo uriInfo,
    final ContentType requestFormat, final ContentType responseFormat, final RepresentationType representationType)
    throws ODataApplicationException, ODataLibraryException {
  final UriInfoResource resource = uriInfo.asUriInfoResource();
  validatePath(resource);
  final EdmEntitySet edmEntitySet = getEdmEntitySet(resource);

  Entity entity = readEntity(uriInfo);
  odata.createETagHelper().checkChangePreconditions(entity.getETag(),
      request.getHeaders(HttpHeader.IF_MATCH),
      request.getHeaders(HttpHeader.IF_NONE_MATCH));

  final List<UriResource> resourceParts = resource.getUriResourceParts();
  final int trailing = representationType == RepresentationType.VALUE ? 1 : 0;
  final List<String> path = getPropertyPath(resourceParts, trailing);
  final EdmProperty edmProperty = ((UriResourceProperty) resourceParts.get(resourceParts.size() - trailing - 1))
      .getProperty();

  Property property = getPropertyData(entity, path);

  if (representationType == RepresentationType.VALUE || 
  		edmProperty.getType().getFullQualifiedName()
  		.getFullQualifiedNameAsString().equalsIgnoreCase(EDMSTREAM)) {
    final FixedFormatDeserializer deserializer = odata.createFixedFormatDeserializer();
    final Object value = edmProperty.getType() == odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Binary) 
  		  || edmProperty.getType() == odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Stream) ?
        deserializer.binary(request.getBody()) :
        deserializer.primitiveValue(request.getBody(), edmProperty);
    dataProvider.updatePropertyValue(property, value);
  } else {
    final Property changedProperty = odata.createDeserializer(requestFormat)
        .property(request.getBody(), edmProperty).getProperty();
    if (changedProperty.isNull() && !edmProperty.isNullable()) {
      throw new ODataApplicationException("Not nullable.", HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ROOT);
    }
    dataProvider.updateProperty(edmProperty, property, changedProperty, request.getMethod() == HttpMethod.PATCH);
  }

  dataProvider.updateETag(entity);

  final Return returnPreference = odata.createPreferences(request.getHeaders(HttpHeader.PREFER)).getReturn();
  if (returnPreference == null || returnPreference == Return.REPRESENTATION) {
    response.setStatusCode(HttpStatusCode.OK.getStatusCode());
    if (representationType == RepresentationType.VALUE || 
      		edmProperty.getType().getFullQualifiedName()
        		.getFullQualifiedNameAsString().equalsIgnoreCase(EDMSTREAM)) {
      response.setContent(
          serializePrimitiveValue(property, edmProperty, (EdmPrimitiveType) edmProperty.getType(), null));
    } else {
      final SerializerResult result = serializeProperty(entity, edmEntitySet, path, property, edmProperty,
          edmProperty.getType(), null, representationType, responseFormat, null, null);
      response.setContent(result.getContent());
    }
    if (edmProperty.getType().getFullQualifiedName()
      		.getFullQualifiedNameAsString().equalsIgnoreCase(EDMSTREAM)) {
    	  response.setHeader(HttpHeader.CONTENT_TYPE, requestFormat.toContentTypeString());
      } else {
    	  response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
      }
  } else {
    response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
  }
  if (returnPreference != null) {
    response.setHeader(HttpHeader.PREFERENCE_APPLIED,
        PreferencesApplied.with().returnRepresentation(returnPreference).build().toValueString());
  }
  if (entity.getETag() != null) {
    response.setHeader(HttpHeader.ETAG, entity.getETag());
  }
}