Java Code Examples for org.apache.olingo.commons.api.data.Property#isNull()

The following examples show how to use org.apache.olingo.commons.api.data.Property#isNull() . 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: ODataAdapter.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * This method return the entity collection which are able to navigate from the parent entity (source) using uri navigation properties.
 * <p/>
 * In this method we check the parent entities primary keys and return the entity according to the values.
 * we use ODataDataHandler, navigation properties to get particular foreign keys.
 *
 * @param metadata     Service Metadata
 * @param parentEntity parentEntity
 * @param navigation   UriResourceNavigation
 * @return EntityCollection
 * @throws ODataServiceFault
 */
private EntityCollection getNavigableEntitySet(ServiceMetadata metadata, Entity parentEntity,
                                               EdmNavigationProperty navigation, String url)
        throws ODataServiceFault, ODataApplicationException {
    EdmEntityType type = metadata.getEdm().getEntityType(new FullQualifiedName(parentEntity.getType()));
    String linkName = navigation.getName();
    List<Property> properties = new ArrayList<>();
    Map<String, EdmProperty> propertyMap = new HashMap<>();
    for (NavigationKeys keys : this.dataHandler.getNavigationProperties().get(type.getName())
                                               .getNavigationKeys(linkName)) {
        Property property = parentEntity.getProperty(keys.getPrimaryKey());
        if (property != null && !property.isNull()) {
            propertyMap.put(keys.getForeignKey(), (EdmProperty) type.getProperty(property.getName()));
            property.setName(keys.getForeignKey());
            properties.add(property);
        }
    }
    if(!properties.isEmpty()) {
        return createEntityCollectionFromDataEntryList(linkName, dataHandler
                .readTableWithKeys(linkName, wrapPropertiesToDataEntry(type, properties, propertyMap)), url);
    }
    return null;
}
 
Example 2
Source File: RequestValidator.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void validateProperties(final List<Property> properties, final EdmBindingTarget edmBindingTarget,
    final EdmStructuredType edmType, final List<String> keyPredicateNames, final List<String> path)
    throws DataProviderException {

  for (final String propertyName : edmType.getPropertyNames()) {
    final EdmProperty edmProperty = (EdmProperty) edmType.getProperty(propertyName);

    // Ignore key properties, they are set automatically
    if (!keyPredicateNames.contains(propertyName)) {
      final Property property = getProperty(properties, propertyName);

      // Check if all "not nullable" properties are set
      if (!edmProperty.isNullable()) {
        if ((property != null && property.isNull()) // Update,insert; Property is explicit set to null
            || (isInsert && property == null) // Insert; Property not provided
            || (!isInsert && !isPatch && property == null)) { // Insert(Put); Property not provided
          throw new DataProviderException("Property " + propertyName + " must not be null",
              HttpStatusCode.BAD_REQUEST);
        }
      }

      // Validate property value
      validatePropertyValue(property, edmProperty, edmBindingTarget, path);
    }
  }
}
 
Example 3
Source File: ODataXmlSerializer.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
protected void writeProperty(final ServiceMetadata metadata,
    final EdmProperty edmProperty, final Property property,
    final Set<List<String>> selectedPaths,
    final String xml10InvalidCharReplacement, final XMLStreamWriter writer, 
    Set<List<String>> expandedPaths, Linked linked, ExpandOption expand)
    throws XMLStreamException, SerializerException {
  writer.writeStartElement(DATA, edmProperty.getName(), NS_DATA);
  if (property == null || property.isNull()) {
    if (edmProperty.isNullable()) {
      writer.writeAttribute(METADATA, NS_METADATA, Constants.ATTR_NULL, "true");
    } else {
      throw new SerializerException("Non-nullable property not present!",
          SerializerException.MessageKeys.MISSING_PROPERTY, edmProperty.getName());
    }
  } else {
    writePropertyValue(metadata, edmProperty, property, selectedPaths, 
        xml10InvalidCharReplacement, writer, expandedPaths, linked, expand);
  }
  writer.writeEndElement();
}
 
Example 4
Source File: ODataAdapter.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * This method return the entity which is able to navigate from the parent entity (source) using uri navigation properties.
 * <p/>
 * In this method we check the parent entities foreign keys and return the entity according to the values.
 * we use ODataDataHandler, navigation properties to get particular foreign keys.
 *
 * @param metadata     Service Metadata
 * @param parentEntity Entity (Source)
 * @param navigation   UriResourceNavigation (Destination)
 * @return Entity (Destination)
 * @throws ODataApplicationException
 * @throws ODataServiceFault
 * @see ODataDataHandler#getNavigationProperties()
 */
private Entity getNavigableEntity(ServiceMetadata metadata, Entity parentEntity, EdmNavigationProperty navigation,
                                  String baseUrl) throws ODataApplicationException, ODataServiceFault {
    EdmEntityType type = metadata.getEdm().getEntityType(new FullQualifiedName(parentEntity.getType()));
    String linkName = navigation.getName();
    List<Property> properties = new ArrayList<>();
    Map<String, EdmProperty> propertyMap = new HashMap<>();
    for (NavigationKeys keys : this.dataHandler.getNavigationProperties().get(linkName)
                                               .getNavigationKeys(type.getName())) {
        Property property = parentEntity.getProperty(keys.getForeignKey());
        if (property != null && !property.isNull()) {
            propertyMap.put(keys.getPrimaryKey(), (EdmProperty) type.getProperty(property.getName()));
            property.setName(keys.getPrimaryKey());
            properties.add(property);
        }
    }
    EntityCollection results = null;
    if (!properties.isEmpty()) {
        results = createEntityCollectionFromDataEntryList(linkName, dataHandler
                .readTableWithKeys(linkName, wrapPropertiesToDataEntry(type, properties, propertyMap)), baseUrl);
    }
    if (results != null && !results.getEntities().isEmpty()) {
        return results.getEntities().get(0);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Reference is not found.");
        }
        return null;
    }
}
 
Example 5
Source File: ODataJsonSerializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected void writeProperty(final ServiceMetadata metadata,
    final EdmProperty edmProperty, final Property property,
    final Set<List<String>> selectedPaths, final JsonGenerator json, 
    Set<List<String>> expandedPaths, Linked linked, ExpandOption expand)
    throws IOException, SerializerException {
  boolean isStreamProperty = isStreamProperty(edmProperty);
  writePropertyType(edmProperty, json);
  if (!isStreamProperty) {
    json.writeFieldName(edmProperty.getName());
  }
  if (property == null || property.isNull()) {
    if (edmProperty.isNullable() == Boolean.FALSE && !isStreamProperty) {
      throw new SerializerException("Non-nullable property not present!",
          SerializerException.MessageKeys.MISSING_PROPERTY, edmProperty.getName());
    } else {
      if (!isStreamProperty) {
        if (edmProperty.isCollection()) {
          json.writeStartArray();
          json.writeEndArray();
        } else {
          json.writeNull();
        }
      }
    }
  } else {
    writePropertyValue(metadata, edmProperty, property, selectedPaths, json, 
        expandedPaths, linked, expand);
  }
}
 
Example 6
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());
  }
}
 
Example 7
Source File: TechnicalActionProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
public void processActionPrimitiveCollection(final ODataRequest request, ODataResponse response,
    final UriInfo uriInfo, final ContentType requestFormat, final ContentType responseFormat)
    throws ODataApplicationException, ODataLibraryException {
  EdmAction action = null;
  Map<String, Parameter> parameters = null;
  List<UriResource> uriResource = uriInfo.asUriInfoResource().getUriResourceParts();
  Property property = null;
  if (uriResource.size() > 1) {
    UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) uriResource.get(0);
    action = ((UriResourceAction) uriResource.get(uriResource.size() - 1)).getAction();
    parameters = readParameters(action, request.getBody(), requestFormat);
    property = dataProvider.processBoundActionPrimitiveCollection(action.getName(), parameters, 
        uriResourceEntitySet.getEntitySet(), uriResourceEntitySet.getKeyPredicates());
  } else {
    action = ((UriResourceAction) uriResource.get(0))
        .getAction();
    parameters = readParameters(action, request.getBody(), requestFormat);
    property =
        dataProvider.processActionPrimitiveCollection(action.getName(), parameters);
  }

  if (property == null || property.isNull()) {
    // Collection Propertys must never be null
    throw new ODataApplicationException("The action could not be executed.",
        HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ROOT);
  } else if (property.asCollection().contains(null) && !action.getReturnType().isNullable()) {
    // Not nullable return type but array contains a null value
    throw new ODataApplicationException("The action could not be executed.",
        HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ROOT);
  }

  final Return returnPreference = odata.createPreferences(request.getHeaders(HttpHeader.PREFER)).getReturn();
  if (returnPreference == null || returnPreference == Return.REPRESENTATION) {
    final EdmPrimitiveType type = (EdmPrimitiveType) action.getReturnType().getType();
    final ContextURL contextURL = ContextURL.with().type(type).navOrPropertyPath(action.getName())
        .asCollection().build();
    final PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextURL).build();
    final SerializerResult result =
        odata.createSerializer(responseFormat).primitiveCollection(serviceMetadata, type, property, options);
    response.setContent(result.getContent());
    response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
    response.setStatusCode(HttpStatusCode.OK.getStatusCode());
  } else {
    response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
  }
  if (returnPreference != null) {
    response.setHeader(HttpHeader.PREFERENCE_APPLIED,
        PreferencesApplied.with().returnRepresentation(returnPreference).build().toValueString());
  }
}
 
Example 8
Source File: TechnicalActionProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
public void processActionPrimitive(final ODataRequest request, ODataResponse response,
    final UriInfo uriInfo, final ContentType requestFormat, final ContentType responseFormat)
    throws ODataApplicationException, ODataLibraryException {
  EdmAction action = null;
  Map<String, Parameter> parameters = null;
  List<UriResource> uriResource = uriInfo.asUriInfoResource().getUriResourceParts();
  Property property = null;
  if (uriResource.size() > 1) {
    UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) uriResource.get(0);
    action = ((UriResourceAction) uriResource.get(uriResource.size() - 1)).getAction();
    parameters = readParameters(action, request.getBody(), requestFormat);
    property = dataProvider.processBoundActionPrimitive(action.getName(), parameters, 
        uriResourceEntitySet.getEntitySet(), uriResourceEntitySet.getKeyPredicates());
  } else {
    action = ((UriResourceAction) uriResource.get(0))
        .getAction();
    parameters = readParameters(action, request.getBody(), requestFormat);
    property = dataProvider.processActionPrimitive(action.getName(), parameters);
  }
  
  EdmPrimitiveType type = (EdmPrimitiveType) action.getReturnType().getType();
  if (property == null || property.isNull()) {
    if (action.getReturnType().isNullable()) {
      response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
    } else {
      // Not nullable return type so we have to give back an Internal Server Error
      throw new ODataApplicationException("The action could not be executed.",
          HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ROOT);
    }
  } else {
    final Return returnPreference = odata.createPreferences(request.getHeaders(HttpHeader.PREFER)).getReturn();
    if (returnPreference == null || returnPreference == Return.REPRESENTATION) {
      final ContextURL contextURL = ContextURL.with().type(type).build();
      final PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextURL).build();
      final SerializerResult result = odata.createSerializer(responseFormat)
          .primitive(serviceMetadata, type, property, options);
      response.setContent(result.getContent());
      response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
      response.setStatusCode(HttpStatusCode.OK.getStatusCode());
    } else {
      response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
    }
    if (returnPreference != null) {
      response.setHeader(HttpHeader.PREFERENCE_APPLIED,
          PreferencesApplied.with().returnRepresentation(returnPreference).build().toValueString());
    }
  }
}
 
Example 9
Source File: TechnicalActionProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
public void processActionComplexCollection(final ODataRequest request, ODataResponse response,
    final UriInfo uriInfo, final ContentType requestFormat, final ContentType responseFormat)
    throws ODataApplicationException, ODataLibraryException {
  EdmAction action = null;
  Map<String, Parameter> parameters = null;
  Property property = null;
  final List<UriResource> resourcePaths = uriInfo.asUriInfoResource().getUriResourceParts();
  if (resourcePaths.size() > 1) {
    if (resourcePaths.get(0) instanceof UriResourceEntitySet) {
      UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
      EdmEntitySet entitySet = uriResourceEntitySet.getEntitySet();
      action = ((UriResourceAction) resourcePaths.get(resourcePaths.size() - 1))
          .getAction();
      parameters = readParameters(action, request.getBody(), requestFormat);
      property =
          dataProvider.processBoundActionComplexCollection(action.getName(), parameters, entitySet, 
              uriResourceEntitySet.getKeyPredicates());
    }
  } else {
    action = ((UriResourceAction) resourcePaths.get(0))
        .getAction();
    parameters = readParameters(action, request.getBody(), requestFormat);
    property =
        dataProvider.processActionComplexCollection(action.getName(), parameters);
  }
  
  if (property == null || property.isNull()) {
    // Collection Propertys must never be null
    throw new ODataApplicationException("The action could not be executed.",
        HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ROOT);
  } else if (property.asCollection().contains(null) && !action.getReturnType().isNullable()) {
    // Not nullable return type but array contains a null value
    throw new ODataApplicationException("The action could not be executed.",
        HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ROOT);
  }
  final Return returnPreference = odata.createPreferences(request.getHeaders(HttpHeader.PREFER)).getReturn();
  if (returnPreference == null || returnPreference == Return.REPRESENTATION) {
    final EdmComplexType type = (EdmComplexType) action.getReturnType().getType();
    final ContextURL contextURL = ContextURL.with().type(type).asCollection().build();
    final ComplexSerializerOptions options = ComplexSerializerOptions.with().contextURL(contextURL).build();
    final SerializerResult result =
        odata.createSerializer(responseFormat).complexCollection(serviceMetadata, type, property, options);
    response.setContent(result.getContent());
    response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
    response.setStatusCode(HttpStatusCode.OK.getStatusCode());
  } else {
    response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
  }
  if (returnPreference != null) {
    response.setHeader(HttpHeader.PREFERENCE_APPLIED,
        PreferencesApplied.with().returnRepresentation(returnPreference).build().toValueString());
  }
}
 
Example 10
Source File: TechnicalActionProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
public void processActionComplex(final ODataRequest request, ODataResponse response, final UriInfo uriInfo,
    final ContentType requestFormat, final ContentType responseFormat)
    throws ODataApplicationException, ODataLibraryException {
  EdmAction action = null;
  Map<String, Parameter> parameters = null;
  Property property = null;
  final List<UriResource> resourcePaths = uriInfo.asUriInfoResource().getUriResourceParts();
  if (resourcePaths.size() > 1) {
    if (resourcePaths.get(0) instanceof UriResourceEntitySet) {
      UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
      EdmEntitySet entitySet = uriResourceEntitySet.getEntitySet();
      action = ((UriResourceAction) resourcePaths.get(resourcePaths.size() - 1))
          .getAction();
      parameters = readParameters(action, request.getBody(), requestFormat);
      property =
          dataProvider.processBoundActionComplex(action.getName(), parameters, entitySet, 
              uriResourceEntitySet.getKeyPredicates());
    }
  } else {
    action = ((UriResourceAction) resourcePaths.get(0))
        .getAction();
    parameters = readParameters(action, request.getBody(), requestFormat);
    property = dataProvider.processActionComplex(action.getName(), parameters);
  }
  
  EdmComplexType type = (EdmComplexType) action.getReturnType().getType();
  if (property == null || property.isNull()) {
    if (action.getReturnType().isNullable()) {
      response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
    } else {
      // Not nullable return type so we have to give back an Internal Server Error
      throw new ODataApplicationException("The action could not be executed.",
          HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ROOT);
    }
  } else {
    final Return returnPreference = odata.createPreferences(request.getHeaders(HttpHeader.PREFER)).getReturn();
    if (returnPreference == null || returnPreference == Return.REPRESENTATION) {
      final ContextURL contextURL = ContextURL.with().type(type).build();
      final ComplexSerializerOptions options = ComplexSerializerOptions.with().contextURL(contextURL).build();
      final SerializerResult result =
          odata.createSerializer(responseFormat).complex(serviceMetadata, type, property, options);
      response.setContent(result.getContent());
      response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
      response.setStatusCode(HttpStatusCode.OK.getStatusCode());
    } else {
      response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
    }
    if (returnPreference != null) {
      response.setHeader(HttpHeader.PREFERENCE_APPLIED,
          PreferencesApplied.with().returnRepresentation(returnPreference).build().toValueString());
    }
  }
}