org.apache.olingo.server.api.uri.UriResourceEntitySet Java Examples

The following examples show how to use org.apache.olingo.server.api.uri.UriResourceEntitySet. 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: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void deleteEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo)
         throws ODataApplicationException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

	// 2. delete the data in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	storage.deleteEntityData(edmEntitySet, keyPredicates);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example #2
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void updateEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
                          ContentType requestFormat, ContentType responseFormat)
						throws ODataApplicationException, DeserializerException, SerializerException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
	EdmEntityType edmEntityType = edmEntitySet.getEntityType();

	// 2. update the data in backend
	// 2.1. retrieve the payload from the PUT request for the entity to be updated 
	InputStream requestInputStream = request.getBody();
	ODataDeserializer deserializer = odata.createDeserializer(requestFormat);
	DeserializerResult result = deserializer.entity(requestInputStream, edmEntityType);
	Entity requestEntity = result.getEntity();
	// 2.2 do the modification in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	// Note that this updateEntity()-method is invoked for both PUT or PATCH operations
	HttpMethod httpMethod = request.getMethod();
	storage.updateEntityData(edmEntitySet, keyPredicates, requestEntity, httpMethod);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example #3
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void deleteEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo)
         throws ODataApplicationException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

	// 2. delete the data in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	storage.deleteEntityData(edmEntitySet, keyPredicates);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example #4
Source File: ODataDispatcher.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void handleCountDispatching(final ODataRequest request, final ODataResponse response,
    final int lastPathSegmentIndex) throws ODataApplicationException, ODataLibraryException {
  validatePreferHeader(request);
  final UriResource resource = uriInfo.getUriResourceParts().get(lastPathSegmentIndex - 1);
  if (resource instanceof UriResourceEntitySet
      || resource instanceof UriResourceNavigation
      || resource instanceof UriResourceFunction
          && ((UriResourceFunction) resource).getType().getKind() == EdmTypeKind.ENTITY) {
    handler.selectProcessor(CountEntityCollectionProcessor.class)
        .countEntityCollection(request, response, uriInfo);
  } else if (resource instanceof UriResourcePrimitiveProperty
      || resource instanceof UriResourceFunction
          && ((UriResourceFunction) resource).getType().getKind() == EdmTypeKind.PRIMITIVE) {
    handler.selectProcessor(CountPrimitiveCollectionProcessor.class)
        .countPrimitiveCollection(request, response, uriInfo);
  } else {
    handler.selectProcessor(CountComplexCollectionProcessor.class)
        .countComplexCollection(request, response, uriInfo);
  }
}
 
Example #5
Source File: CarsProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private EdmEntitySet getEdmEntitySet(final UriInfoResource uriInfo) throws ODataApplicationException {
  final List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
  /*
   * To get the entity set we have to interpret all URI segments
   */
  if (!(resourcePaths.get(0) instanceof UriResourceEntitySet)) {
    throw new ODataApplicationException("Invalid resource type for first segment.",
        HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
  }

  /*
   * Here we should interpret the whole URI but in this example we do not support navigation so we throw an exception
   */

  final UriResourceEntitySet uriResource = (UriResourceEntitySet) resourcePaths.get(0);
  return uriResource.getEntitySet();
}
 
Example #6
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public void updateMediaEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
    ContentType requestFormat, ContentType responseFormat) throws ODataApplicationException, ODataLibraryException {
  
  final UriResource firstResoucePart = uriInfo.getUriResourceParts().get(0);
  if (firstResoucePart instanceof UriResourceEntitySet) {
    final EdmEntitySet edmEntitySet = Util.getEdmEntitySet(uriInfo);
    final UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) firstResoucePart;

    final Entity entity = storage.readEntityData(edmEntitySet, uriResourceEntitySet.getKeyPredicates());
    if (entity == null) {
      throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(),
          Locale.ENGLISH);
    }
    
    final byte[] mediaContent = odata.createFixedFormatDeserializer().binary(request.getBody());
    storage.updateMedia(entity, requestFormat.toContentTypeString(), mediaContent);
    
    response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
  } else {
    throw new ODataApplicationException("Not implemented", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), 
        Locale.ENGLISH);
  }
}
 
Example #7
Source File: PreconditionsValidatorTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private boolean mustValidate(final String uri, final String entitySetName)
    throws UriParserException, UriValidationException, PreconditionException {
  final UriInfo uriInfo = new Parser(edm, odata).parseUri(uri, null, null, null);
  final List<UriResource> parts = uriInfo.getUriResourceParts();
  final boolean isMedia = parts.size() >= 2
      && parts.get(parts.size() - 1) instanceof UriResourceValue
      && parts.get(parts.size() - 2) instanceof UriResourceEntitySet;

  CustomETagSupport support = mock(CustomETagSupport.class);
  final Answer<Boolean> answer = new Answer<Boolean>() {
    public Boolean answer(final InvocationOnMock invocation) throws Throwable {
      if (entitySetName != null) {
        assertEquals(entitySetName, ((EdmBindingTarget) invocation.getArguments()[0]).getName());
      }
      return true;
    }};
  when(support.hasETag(any(EdmBindingTarget.class))).thenAnswer(answer);
  when(support.hasMediaETag(any(EdmBindingTarget.class))).thenAnswer(answer);

  return new PreconditionsValidator(uriInfo).mustValidatePreconditions(support, isMedia);
}
 
Example #8
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void updateEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
                          ContentType requestFormat, ContentType responseFormat)
						throws ODataApplicationException, DeserializerException, SerializerException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
	EdmEntityType edmEntityType = edmEntitySet.getEntityType();

	// 2. update the data in backend
	// 2.1. retrieve the payload from the PUT request for the entity to be updated 
	InputStream requestInputStream = request.getBody();
	ODataDeserializer deserializer = odata.createDeserializer(requestFormat);
	DeserializerResult result = deserializer.entity(requestInputStream, edmEntityType);
	Entity requestEntity = result.getEntity();
	// 2.2 do the modification in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	// Note that this updateEntity()-method is invoked for both PUT or PATCH operations
	HttpMethod httpMethod = request.getMethod();
	storage.updateEntityData(edmEntitySet, keyPredicates, requestEntity, httpMethod);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example #9
Source File: Storage.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private Entity readEntityByBindingLink(final String entityId, final EdmEntitySet edmEntitySet, 
    final String rawServiceUri) throws ODataApplicationException {
  
  UriResourceEntitySet entitySetResource = null;
  try {
    entitySetResource = odata.createUriHelper().parseEntityId(edm, entityId, rawServiceUri);
    
    if(!entitySetResource.getEntitySet().getName().equals(edmEntitySet.getName())) {
      throw new ODataApplicationException("Execpted an entity-id for entity set " + edmEntitySet.getName() 
        + " but found id for entity set " + entitySetResource.getEntitySet().getName(), 
        HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ENGLISH);
    }
  } catch (DeserializerException e) {
    throw new ODataApplicationException(entityId + " is not a valid entity-Id", 
        HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ENGLISH);
  }

  return readEntityData(entitySetResource.getEntitySet(), entitySetResource.getKeyPredicates());
}
 
Example #10
Source File: UriHelperImpl.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public UriResourceEntitySet parseEntityId(final Edm edm, final String entityId, final String rawServiceRoot)
    throws DeserializerException {

  String oDataPath = entityId;
  if (rawServiceRoot != null && entityId.startsWith(rawServiceRoot)) {
    oDataPath = entityId.substring(rawServiceRoot.length());
  }
  oDataPath = oDataPath.startsWith("/") ? oDataPath : "/" + oDataPath;

  try {
    final List<UriResource> uriResourceParts =
        new Parser(edm, new ODataImpl()).parseUri(oDataPath, null, null, rawServiceRoot).getUriResourceParts();
    if (uriResourceParts.size() == 1 && uriResourceParts.get(0).getKind() == UriResourceKind.entitySet) {
      final UriResourceEntitySet entityUriResource = (UriResourceEntitySet) uriResourceParts.get(0);

      return entityUriResource;
    }

    throw new DeserializerException("Invalid entity binding link", MessageKeys.INVALID_ENTITY_BINDING_LINK,
        entityId);
  } catch (final ODataLibraryException e) {
    throw new DeserializerException("Invalid entity binding link", e, MessageKeys.INVALID_ENTITY_BINDING_LINK,
        entityId);
  }
}
 
Example #11
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void readEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat)
    throws ODataApplicationException, SerializerException {

  // The sample service supports only functions imports and entity sets.
  // We do not care about bound functions and composable functions.

  UriResource uriResource = uriInfo.getUriResourceParts().get(0);

  if (uriResource instanceof UriResourceEntitySet) {
    readEntityInternal(request, response, uriInfo, responseFormat);
  } else if (uriResource instanceof UriResourceFunction) {
    readFunctionImportInternal(request, response, uriInfo, responseFormat);
  } else {
    throw new ODataApplicationException("Only EntitySet is supported",
        HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
  }
}
 
Example #12
Source File: TechnicalProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
protected EdmEntitySet getEntitySetBasedOnTypeCast(UriResourceEntitySet uriResource) {
  EdmEntitySet entitySet = null;
  EdmEntityContainer container = this.serviceMetadata.getEdm().getEntityContainer();
  if (uriResource.getTypeFilterOnEntry() != null ||
      uriResource.getTypeFilterOnCollection() != null) {
    List<EdmEntitySet> entitySets = container.getEntitySets();
    for (EdmEntitySet entitySet1 : entitySets) {
      EdmEntityType entityType = entitySet1.getEntityType();
      if ((uriResource.getTypeFilterOnEntry() != null && 
          entityType.getName().equalsIgnoreCase(uriResource.getTypeFilterOnEntry().getName())) ||
          (uriResource.getTypeFilterOnCollection() != null && 
          entityType.getName().equalsIgnoreCase(uriResource.getTypeFilterOnCollection().getName()))) {
        entitySet = entitySet1;
        break;
      }
    }
  } else {
    entitySet = uriResource.getEntitySet();
  }
  return entitySet;
}
 
Example #13
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void updateEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
                          ContentType requestFormat, ContentType responseFormat)
						throws ODataApplicationException, DeserializerException, SerializerException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
	EdmEntityType edmEntityType = edmEntitySet.getEntityType();

	// 2. update the data in backend
	// 2.1. retrieve the payload from the PUT request for the entity to be updated 
	InputStream requestInputStream = request.getBody();
	ODataDeserializer deserializer = this.odata.createDeserializer(requestFormat);
	DeserializerResult result = deserializer.entity(requestInputStream, edmEntityType);
	Entity requestEntity = result.getEntity();
	// 2.2 do the modification in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	// Note that this updateEntity()-method is invoked for both PUT or PATCH operations
	HttpMethod httpMethod = request.getMethod();
	storage.updateEntityData(edmEntitySet, keyPredicates, requestEntity, httpMethod);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example #14
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void updateEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
                          ContentType requestFormat, ContentType responseFormat)
						throws ODataApplicationException, DeserializerException, SerializerException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
	EdmEntityType edmEntityType = edmEntitySet.getEntityType();

	// 2. update the data in backend
	// 2.1. retrieve the payload from the PUT request for the entity to be updated 
	InputStream requestInputStream = request.getBody();
	ODataDeserializer deserializer = odata.createDeserializer(requestFormat);
	DeserializerResult result = deserializer.entity(requestInputStream, edmEntityType);
	Entity requestEntity = result.getEntity();
	// 2.2 do the modification in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	// Note that this updateEntity()-method is invoked for both PUT or PATCH operations
	HttpMethod httpMethod = request.getMethod();
	storage.updateEntityData(edmEntitySet, keyPredicates, requestEntity, httpMethod);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example #15
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void deleteEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo)
    throws ODataApplicationException {

  // 1. Retrieve the entity set which belongs to the requested entity
  List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
  // Note: only in our example we can assume that the first segment is the EntitySet
  UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
  EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

  // 2. delete the data in backend
  List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
  storage.deleteEntityData(edmEntitySet, keyPredicates);

  // 3. configure the response object
  response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example #16
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void deleteEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo)
         throws ODataApplicationException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

	// 2. delete the data in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	storage.deleteEntityData(edmEntitySet, keyPredicates);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example #17
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public void updateMediaEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
    ContentType requestFormat, ContentType responseFormat) throws ODataApplicationException, ODataLibraryException {
  
  final UriResource firstResoucePart = uriInfo.getUriResourceParts().get(0);
  if (firstResoucePart instanceof UriResourceEntitySet) {
    final EdmEntitySet edmEntitySet = Util.getEdmEntitySet(uriInfo);
    final UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) firstResoucePart;

    final Entity entity = storage.readEntityData(edmEntitySet, uriResourceEntitySet.getKeyPredicates());
    if (entity == null) {
      throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(),
          Locale.ENGLISH);
    }
    
    final byte[] mediaContent = odata.createFixedFormatDeserializer().binary(request.getBody());
    storage.updateMedia(entity, requestFormat.toContentTypeString(), mediaContent);
    
    response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
  } else {
    throw new ODataApplicationException("Not implemented", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), 
        Locale.ENGLISH);
  }
}
 
Example #18
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void updateEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
                          ContentType requestFormat, ContentType responseFormat)
						throws ODataApplicationException, DeserializerException, SerializerException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
	EdmEntityType edmEntityType = edmEntitySet.getEntityType();

	// 2. update the data in backend
	// 2.1. retrieve the payload from the PUT request for the entity to be updated 
	InputStream requestInputStream = request.getBody();
	ODataDeserializer deserializer = odata.createDeserializer(requestFormat);
	DeserializerResult result = deserializer.entity(requestInputStream, edmEntityType);
	Entity requestEntity = result.getEntity();
	// 2.2 do the modification in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	// Note that this updateEntity()-method is invoked for both PUT or PATCH operations
	HttpMethod httpMethod = request.getMethod();
	storage.updateEntityData(edmEntitySet, keyPredicates, requestEntity, httpMethod);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example #19
Source File: GenericEntityCollectionProcessor.java    From spring-boot-Olingo-oData with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method for providing some sample data.
 *
 * @param edmEntitySet
 *            for which the data is requested
 * @return data of requested entity set
 */
private EntitySet getData(UriInfo uriInfo) {
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths
			.get(0); // in our example, the first segment is the EntitySet
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

	EntitySet entitySet = null;

	Map<String, EntityProvider> entityProviders = ctx
			.getBeansOfType(EntityProvider.class);

	for (String entity : entityProviders.keySet()) {
		EntityProvider entityProvider = entityProviders.get(entity);
		if (entityProvider
				.getEntityType().getName()
				
				.equals(edmEntitySet.getEntityType().getName())) {
			entitySet = entityProvider.getEntitySet(uriInfo);
			break;
		}
	}
	return entitySet;
}
 
Example #20
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void updateEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
                          ContentType requestFormat, ContentType responseFormat)
						throws ODataApplicationException, DeserializerException, SerializerException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
	EdmEntityType edmEntityType = edmEntitySet.getEntityType();

	// 2. update the data in backend
	// 2.1. retrieve the payload from the PUT request for the entity to be updated 
	InputStream requestInputStream = request.getBody();
	ODataDeserializer deserializer = this.odata.createDeserializer(requestFormat);
	DeserializerResult result = deserializer.entity(requestInputStream, edmEntityType);
	Entity requestEntity = result.getEntity();
	// 2.2 do the modification in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	// Note that this updateEntity()-method is invoked for both PUT or PATCH operations
	HttpMethod httpMethod = request.getMethod();
	storage.updateEntityData(edmEntitySet, keyPredicates, requestEntity, httpMethod);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example #21
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void deleteEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo)
         throws ODataApplicationException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

	// 2. delete the data in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	storage.deleteEntityData(edmEntitySet, keyPredicates);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example #22
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void updateEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
                          ContentType requestFormat, ContentType responseFormat)
						throws ODataApplicationException, DeserializerException, SerializerException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
	EdmEntityType edmEntityType = edmEntitySet.getEntityType();

	// 2. update the data in backend
	// 2.1. retrieve the payload from the PUT request for the entity to be updated 
	InputStream requestInputStream = request.getBody();
	ODataDeserializer deserializer = odata.createDeserializer(requestFormat);
	DeserializerResult result = deserializer.entity(requestInputStream, edmEntityType);
	Entity requestEntity = result.getEntity();
	// 2.2 do the modification in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	// Note that this updateEntity()-method is invoked for both PUT or PATCH operations
	HttpMethod httpMethod = request.getMethod();
	storage.updateEntityData(edmEntitySet, keyPredicates, requestEntity, httpMethod);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example #23
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void deleteEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo)
         throws ODataApplicationException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

	// 2. delete the data in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	storage.deleteEntityData(edmEntitySet, keyPredicates);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example #24
Source File: ProductEntityProcessor.java    From syndesis with Apache License 2.0 6 votes vote down vote up
@Override
public void deleteEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo) throws ODataApplicationException {
    // 1. Retrieve the entity set which belongs to the requested entity
    List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
    // Note: only in our example we can assume that the first segment is the
    // EntitySet
    UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
    EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

    // 2. delete the data in backend
    List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
    for (UriParameter kp : keyPredicates) {
        LOG.info("Deleting entity {} ( {} )", kp.getName(), kp.getText());
    }

    storage.deleteEntityData(edmEntitySet, keyPredicates);

    // 3. configure the response object
    response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example #25
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
/**
 * This method is invoked when a single entity has to be read.
 * In our example, this can be either a "normal" read operation, or a navigation:
 * 
 * Example for "normal" read operation:
 * http://localhost:8080/DemoService/DemoService.svc/Products(1)
 * 
 * Example for navigation
 * http://localhost:8080/DemoService/DemoService.svc/Products(1)/Category
 */
public void readEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat)
    throws ODataApplicationException, SerializerException {
  
  // The sample service supports only functions imports and entity sets.
  // We do not care about bound functions and composable functions.
  
  UriResource uriResource = uriInfo.getUriResourceParts().get(0); 
  
  if(uriResource instanceof UriResourceEntitySet) {
    readEntityInternal(request, response, uriInfo, responseFormat);
  } else if(uriResource instanceof UriResourceFunction) {
    readFunctionImportInternal(request, response, uriInfo, responseFormat);
  } else {
    throw new ODataApplicationException("Only EntitySet is supported",
        HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
  }
  
}
 
Example #26
Source File: ResourcePathParser.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void requireMediaResourceInCaseOfEntity(UriResource resource) throws UriParserSemanticException {
  // If the resource is an entity or navigatio
  if (resource instanceof UriResourceEntitySet && !((UriResourceEntitySet) resource).getEntityType().hasStream()
      || resource instanceof UriResourceNavigation
      && !((EdmEntityType) ((UriResourceNavigation) resource).getType()).hasStream()) {
    throw new UriParserSemanticException("$value on entity is only allowed on media resources.",
        UriParserSemanticException.MessageKeys.NOT_A_MEDIA_RESOURCE, resource.getSegmentValue());
  }

  // Functions can also deliver an entity. In this case we have to check if the returned entity is a media resource
  if (resource instanceof UriResourceFunction) {
    EdmType returnType = ((UriResourceFunction) resource).getFunction().getReturnType().getType();
    //Collection check is above so not needed here
    if (returnType instanceof EdmEntityType && !((EdmEntityType) returnType).hasStream()) {
      throw new UriParserSemanticException("$value on returned entity is only allowed on media resources.",
          UriParserSemanticException.MessageKeys.NOT_A_MEDIA_RESOURCE, resource.getSegmentValue());
    }
  }
}
 
Example #27
Source File: Util.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public static EdmEntitySet getEdmEntitySet(UriInfoResource uriInfo) throws ODataApplicationException {

    List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
    // To get the entity set we have to interpret all URI segments
    if (!(resourcePaths.get(0) instanceof UriResourceEntitySet)) {
      // Here we should interpret the whole URI but in this example we do not support navigation so we throw an
      // exception
      throw new ODataApplicationException("Invalid resource type for first segment.", HttpStatusCode.NOT_IMPLEMENTED
          .getStatusCode(), Locale.ENGLISH);
    }

    UriResourceEntitySet uriResource = (UriResourceEntitySet) resourcePaths.get(0);

    return uriResource.getEntitySet();
  }
 
Example #28
Source File: Util.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public static EdmEntitySet getEdmEntitySet(UriInfoResource uriInfo) throws ODataApplicationException {

    List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
    // To get the entity set we have to interpret all URI segments
    if (!(resourcePaths.get(0) instanceof UriResourceEntitySet)) {
      // Here we should interpret the whole URI but in this example we do not support navigation so we throw an
      // exception
      throw new ODataApplicationException("Invalid resource type for first segment.", HttpStatusCode.NOT_IMPLEMENTED
          .getStatusCode(), Locale.ENGLISH);
    }

    UriResourceEntitySet uriResource = (UriResourceEntitySet) resourcePaths.get(0);

    return uriResource.getEntitySet();
  }
 
Example #29
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public void readEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat)
						throws ODataApplicationException, SerializerException {

	// 1. retrieve the Entity Type
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

	// 2. retrieve the data from backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);

	// 3. serialize
	EdmEntityType entityType = edmEntitySet.getEntityType();

	ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).suffix(ContextURL.Suffix.ENTITY).build();
 	// expand and select currently not supported
	EntitySerializerOptions options = EntitySerializerOptions.with().contextURL(contextUrl).build();

	ODataSerializer serializer = this.odata.createSerializer(responseFormat);
	SerializerResult serializerResult = serializer.entity(serviceMetadata, entityType, entity, options);
	InputStream entityStream = serializerResult.getContent();

	//4. configure the response object
	response.setContent(entityStream);
	response.setStatusCode(HttpStatusCode.OK.getStatusCode());
	response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
Example #30
Source File: ODataAdapter.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private void blockTypeFilters(final UriResource uriResource) throws ODataApplicationException {
    if (uriResource instanceof UriResourceEntitySet &&
        (((UriResourceEntitySet) uriResource).getTypeFilterOnCollection() != null ||
         ((UriResourceEntitySet) uriResource).getTypeFilterOnEntry() != null) ||
        uriResource instanceof UriResourceFunction &&
        (((UriResourceFunction) uriResource).getTypeFilterOnCollection() != null ||
         ((UriResourceFunction) uriResource).getTypeFilterOnEntry() != null) ||
        uriResource instanceof UriResourceNavigation &&
        (((UriResourceNavigation) uriResource).getTypeFilterOnCollection() != null ||
         ((UriResourceNavigation) uriResource).getTypeFilterOnEntry() != null)) {
        throw new ODataApplicationException("Type filters are not supported.",
                                            HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT);
    }
}