本文整理汇总了Java中org.apache.olingo.server.api.serializer.SerializerResult.getContent方法的典型用法代码示例。如果您正苦于以下问题:Java SerializerResult.getContent方法的具体用法?Java SerializerResult.getContent怎么用?Java SerializerResult.getContent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.olingo.server.api.serializer.SerializerResult
的用法示例。
在下文中一共展示了SerializerResult.getContent方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeProperty
import org.apache.olingo.server.api.serializer.SerializerResult; //导入方法依赖的package包/类
private void writeProperty(ODataRequest request, ODataResponse response, ContentType responseFormat,
EdmEntitySet edmEntitySet, String edmPropertyName, EdmPrimitiveType edmPropertyType, Property property)
throws SerializerException, ODataApplicationException {
Object value = property.getValue();
if (value != null) {
// 3.1. configure the serializer
ODataSerializer serializer = odata.createSerializer(responseFormat);
ContextURL contextUrl = null;
try {
//Need absolute URI for PowewrQuery and Linqpad (and probably other MS based OData clients)
contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName)
.serviceRoot(new URI(request.getRawBaseUri() + "/")).build();
} catch (URISyntaxException e) {
throw new ODataApplicationException("Inavlid RawBaseURI " + request.getRawBaseUri(),
HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ROOT);
}
PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
// 3.2. serialize
SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property,
options);
InputStream propertyStream = serializerResult.getContent();
//4. configure the response object
response.setContent(propertyStream);
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
} else {
// in case there's no value for the property, we can skip the serialization
response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
}
开发者ID:peterjohnlawrence,项目名称:com.inova8.odata2sparql.v4,代码行数:34,代码来源:SparqlPrimitiveValueProcessor.java
示例2: readEntity
import org.apache.olingo.server.api.serializer.SerializerResult; //导入方法依赖的package包/类
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());
}
示例3: readEntityCollection
import org.apache.olingo.server.api.serializer.SerializerResult; //导入方法依赖的package包/类
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat) throws ODataApplicationException, SerializerException {
// 1st retrieve the requested EntitySet from the uriInfo (representation of the parsed URI)
List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); // in our example, the first segment is the EntitySet
EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
// 2nd: fetch the data from backend for this requested EntitySetName and deliver as EntitySet
EntityCollection entityCollection = storage.readEntitySetData(edmEntitySet);
// 3rd: create a serializer based on the requested format (json)
ODataSerializer serializer = odata.createSerializer(responseFormat);
// and serialize the content: transform from the EntitySet object to InputStream
EdmEntityType edmEntityType = edmEntitySet.getEntityType();
ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();
final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
EntityCollectionSerializerOptions opts =
EntityCollectionSerializerOptions.with().id(id).contextURL(contextUrl).build();
SerializerResult serializerResult = serializer.entityCollection(serviceMetadata, edmEntityType, entityCollection, opts);
InputStream serializedContent = serializerResult.getContent();
// 4th: configure the response object: set the body, headers and status code
response.setContent(serializedContent);
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
示例4: readEntity
import org.apache.olingo.server.api.serializer.SerializerResult; //导入方法依赖的package包/类
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());
}
示例5: readEntityCollection
import org.apache.olingo.server.api.serializer.SerializerResult; //导入方法依赖的package包/类
@Override
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo,
ContentType responseFormat) throws ODataApplicationException, SerializerException {
// 1st: retrieve the requested EntitySet from the uriInfo (representation of the parsed URI)
List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
// in our example, the first segment is the EntitySet
UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
// 2nd: fetch the data from backend for this requested EntitySetName and deliver as EntitySet
EntityCollection entityCollection = getData(edmEntitySet);
EntityCollection modifiedEntityCollection = new EntityCollection();
List<Entity> modifiedEntityList = new ArrayList<Entity>();
modifiedEntityList.addAll(entityCollection.getEntities());
// // 3rd: Apply system query option
// // The system query options have to be applied in a defined order
// // 3.1.) $filter
// modifiedEntityList = applyFilterQueryOption(modifiedEntityList, uriInfo.getFilterOption());
//
// // 3.2.) $orderby
// modifiedEntityList = applyOrderQueryOption(modifiedEntityList, uriInfo.getOrderByOption());
//
// // 3.3.) $count
// modifiedEntityList = applyCountQueryOption(modifiedEntityCollection, modifiedEntityList,
// uriInfo.getCountOption());
//
// // 3.4.) $skip
// modifiedEntityList = applySkipQueryOption(modifiedEntityList, uriInfo.getSkipOption());
//
// // 3.5.) $top
// modifiedEntityList = applyTopQueryOption(modifiedEntityList, uriInfo.getTopOption());
//
// // 3.6.) Server driven paging (not part of this tutorial)
// // 3.7.) $expand
// modifiedEntityList = applyExpandQueryOption(modifiedEntityList, edmEntitySet,
// uriInfo.getExpandOption());
// 3.8.) $select
SelectOption selectOption = uriInfo.getSelectOption();
// Set the (may) modified entityList to the new entity collection
modifiedEntityCollection.getEntities().addAll(modifiedEntityList);
// 4th: create a serializer based on the requested format (json)
ODataSerializer serializer = odata.createSerializer(responseFormat);
// we need the property names of the $select, in order to build the context URL
EdmEntityType edmEntityType = edmEntitySet.getEntityType();
String selectList = odata.createUriHelper().buildContextURLSelectList(edmEntityType,
uriInfo.getExpandOption(), selectOption);
ContextURL contextUrl =
ContextURL.with().entitySet(edmEntitySet).selectList(selectList).build();
// adding the selectOption to the serializerOpts will actually tell the lib to do the job
final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with()
.contextURL(contextUrl).count(uriInfo.getCountOption()).select(selectOption)
.expand(uriInfo.getExpandOption()).id(id).build();
// and serialize the content: transform from the EntitySet object to InputStream
SerializerResult serializerResult =
serializer.entityCollection(serviceMetadata, edmEntityType, modifiedEntityCollection, opts);
InputStream serializedContent = serializerResult.getContent();
// 5th: configure the response object: set the body, headers and status code
response.setContent(serializedContent);
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
示例6: readPrimitive
import org.apache.olingo.server.api.serializer.SerializerResult; //导入方法依赖的package包/类
public void readPrimitive(ODataRequest request, ODataResponse response,
UriInfo uriInfo, ContentType responseFormat)
throws ODataApplicationException, SerializerException {
// 1. Retrieve info from URI
// 1.1. retrieve the info about the requested entity set
List<UriResource> resourceParts = uriInfo.getUriResourceParts();
// Note: only in our example we can rely that the first segment is the EntitySet
UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
// the key for the entity
List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();
// 1.2. retrieve the requested (Edm) property
// the last segment is the Property
UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1);
EdmProperty edmProperty = uriProperty.getProperty();
String edmPropertyName = edmProperty.getName();
// in our example, we know we have only primitive types in our model
EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();
// 2. retrieve data from backend
// 2.1. retrieve the entity data, for which the property has to be read
Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
if (entity == null) { // Bad request
throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
}
// 2.2. retrieve the property data from the entity
Property property = entity.getProperty(edmPropertyName);
if (property == null) {
throw new ODataApplicationException("Property not found", HttpStatusCode.NOT_FOUND.getStatusCode(),
Locale.ENGLISH);
}
// 3. serialize
Object value = property.getValue();
if (value != null) {
// 3.1. configure the serializer
ODataSerializer serializer = odata.createSerializer(responseFormat);
ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
// 3.2. serialize
SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
InputStream propertyStream = serializerResult.getContent();
//4. configure the response object
response.setContent(propertyStream);
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
} else {
// in case there's no value for the property, we can skip the serialization
response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
}
示例7: readEntityCollection
import org.apache.olingo.server.api.serializer.SerializerResult; //导入方法依赖的package包/类
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo,
ContentType responseFormat) throws ODataApplicationException, SerializerException {
// 1st: retrieve the requested EntitySet from the uriInfo (representation of the parsed URI)
List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
// in our example, the first segment is the EntitySet
UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
// 2nd: fetch the data from backend for this requested EntitySetName and deliver as EntitySet
EntityCollection entityCollection = storage.readEntitySetData(edmEntitySet);
// 3rd: Check if filter system query option is provided and apply the expression if necessary
FilterOption filterOption = uriInfo.getFilterOption();
if(filterOption != null) {
// Apply $filter system query option
try {
List<Entity> entityList = entityCollection.getEntities();
Iterator<Entity> entityIterator = entityList.iterator();
// Evaluate the expression for each entity
// If the expression is evaluated to "true", keep the entity otherwise remove it from the entityList
while (entityIterator.hasNext()) {
// To evaluate the the expression, create an instance of the Filter Expression Visitor and pass
// the current entity to the constructor
Entity currentEntity = entityIterator.next();
Expression filterExpression = filterOption.getExpression();
FilterExpressionVisitor expressionVisitor = new FilterExpressionVisitor(currentEntity);
// Start evaluating the expression
Object visitorResult = filterExpression.accept(expressionVisitor);
// The result of the filter expression must be of type Edm.Boolean
if(visitorResult instanceof Boolean) {
if(!Boolean.TRUE.equals(visitorResult)) {
// The expression evaluated to false (or null), so we have to remove the currentEntity from entityList
entityIterator.remove();
}
} else {
throw new ODataApplicationException("A filter expression must evaulate to type Edm.Boolean",
HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ENGLISH);
}
}
} catch (ExpressionVisitException e) {
throw new ODataApplicationException("Exception in filter evaluation",
HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ENGLISH);
}
}
// 4th: create a serializer based on the requested format (json)
ODataSerializer serializer = odata.createSerializer(responseFormat);
// and serialize the content: transform from the EntitySet object to InputStream
EdmEntityType edmEntityType = edmEntitySet.getEntityType();
ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();
final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with()
.contextURL(contextUrl).id(id).build();
SerializerResult serializerResult = serializer.entityCollection(serviceMetadata, edmEntityType, entityCollection,
opts);
InputStream serializedContent = serializerResult.getContent();
// 5th: configure the response object: set the body, headers and status code
response.setContent(serializedContent);
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
示例8: readPrimitive
import org.apache.olingo.server.api.serializer.SerializerResult; //导入方法依赖的package包/类
public void readPrimitive(ODataRequest request, ODataResponse response,
UriInfo uriInfo, ContentType responseFormat)
throws ODataApplicationException, SerializerException {
// 1. Retrieve info from URI
// 1.1. retrieve the info about the requested entity set
List<UriResource> resourceParts = uriInfo.getUriResourceParts();
// Note: only in our example we can rely that the first segment is the EntitySet
UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
// the key for the entity
List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();
// 1.2. retrieve the requested (Edm) property
UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); // the last segment is the Property
EdmProperty edmProperty = uriProperty.getProperty();
String edmPropertyName = edmProperty.getName();
// in our example, we know we have only primitive types in our model
EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();
// 2. retrieve data from backend
// 2.1. retrieve the entity data, for which the property has to be read
Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
if (entity == null) { // Bad request
throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
}
// 2.2. retrieve the property data from the entity
Property property = entity.getProperty(edmPropertyName);
if (property == null) {
throw new ODataApplicationException("Property not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
}
// 3. serialize
Object value = property.getValue();
if (value != null) {
// 3.1. configure the serializer
ODataSerializer serializer = odata.createSerializer(responseFormat);
ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
// 3.2. serialize
SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
InputStream propertyStream = serializerResult.getContent();
//4. configure the response object
response.setContent(propertyStream);
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
} else {
// in case there's no value for the property, we can skip the serialization
response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
}
示例9: readPrimitive
import org.apache.olingo.server.api.serializer.SerializerResult; //导入方法依赖的package包/类
public void readPrimitive(ODataRequest request, ODataResponse response,UriInfo uriInfo, ContentType responseFormat)
throws ODataApplicationException, SerializerException {
// 1. Retrieve info from URI
// 1.1. retrieve the info about the requested entity set
List<UriResource> resourceParts = uriInfo.getUriResourceParts();
// Note: only in our example we can rely that the first segment is the EntitySet
UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
// the key for the entity
List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();
// 1.2. retrieve the requested (Edm) property
// the last segment is the Property
UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1);
EdmProperty edmProperty = uriProperty.getProperty();
String edmPropertyName = edmProperty.getName();
// in our example, we know we have only primitive types in our model
EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();
// 2. retrieve data from backend
// 2.1. retrieve the entity data, for which the property has to be read
Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
if (entity == null) { // Bad request
throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
}
// 2.2. retrieve the property data from the entity
Property property = entity.getProperty(edmPropertyName);
if (property == null) {
throw new ODataApplicationException("Property not found", HttpStatusCode.NOT_FOUND.getStatusCode(),
Locale.ENGLISH);
}
// 3. serialize
Object value = property.getValue();
if (value != null) {
// 3.1. configure the serializer
ODataSerializer serializer = odata.createSerializer(responseFormat);
ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
// 3.2. serialize
SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
InputStream propertyStream = serializerResult.getContent();
//4. configure the response object
response.setContent(propertyStream);
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
} else {
// in case there's no value for the property, we can skip the serialization
response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
}
示例10: readEntityCollection
import org.apache.olingo.server.api.serializer.SerializerResult; //导入方法依赖的package包/类
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo,
ContentType responseFormat) throws ODataApplicationException, SerializerException {
// 1st retrieve the requested EntitySet from the uriInfo (representation of the parsed URI)
List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
// in our example, the first segment is the EntitySet
UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
// 2nd: fetch the data from backend for this requested EntitySetName and deliver as EntitySet
EntityCollection entityCollection = storage.readEntitySetData(edmEntitySet);
List<Entity> entityList = entityCollection.getEntities();
// 3rd apply $orderby
OrderByOption orderByOption = uriInfo.getOrderByOption();
if (orderByOption != null) {
List<OrderByItem> orderItemList = orderByOption.getOrders();
final OrderByItem orderByItem = orderItemList.get(0); // in our example we support only one
Expression expression = orderByItem.getExpression();
if(expression instanceof Member){
UriInfoResource resourcePath = ((Member)expression).getResourcePath();
UriResource uriResource = resourcePath.getUriResourceParts().get(0);
if (uriResource instanceof UriResourcePrimitiveProperty) {
EdmProperty edmProperty = ((UriResourcePrimitiveProperty)uriResource).getProperty();
final String sortPropertyName = edmProperty.getName();
// do the sorting for the list of entities
Collections.sort(entityList, new Comparator<Entity>() {
// we delegate the sorting to the native sorter of Integer and String
public int compare(Entity entity1, Entity entity2) {
int compareResult = 0;
if(sortPropertyName.equals("ID")){
Integer integer1 = (Integer) entity1.getProperty(sortPropertyName).getValue();
Integer integer2 = (Integer) entity2.getProperty(sortPropertyName).getValue();
compareResult = integer1.compareTo(integer2);
}else{
String propertyValue1 = (String) entity1.getProperty(sortPropertyName).getValue();
String propertyValue2 = (String) entity2.getProperty(sortPropertyName).getValue();
compareResult = propertyValue1.compareTo(propertyValue2);
}
// if 'desc' is specified in the URI, change the order of the list
if(orderByItem.isDescending()){
return - compareResult; // just convert the result to negative value to change the order
}
return compareResult;
}
});
}
}
}
// 4th: create a serializer based on the requested format (json)
ODataSerializer serializer = odata.createSerializer(responseFormat);
// and serialize the content: transform from the EntitySet object to InputStream
EdmEntityType edmEntityType = edmEntitySet.getEntityType();
ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();
final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
EntityCollectionSerializerOptions opts =
EntityCollectionSerializerOptions.with().id(id).contextURL(contextUrl).build();
SerializerResult serializerResult = serializer.entityCollection(serviceMetadata, edmEntityType,
entityCollection, opts);
InputStream serializedContent = serializerResult.getContent();
// 5th: configure the response object: set the body, headers and status code
response.setContent(serializedContent);
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
示例11: readPrimitive
import org.apache.olingo.server.api.serializer.SerializerResult; //导入方法依赖的package包/类
public void readPrimitive(ODataRequest request, ODataResponse response,
UriInfo uriInfo, ContentType responseFormat)
throws ODataApplicationException, SerializerException {
// 1. Retrieve info from URI
// 1.1. retrieve the info about the requested entity set
List<UriResource> resourceParts = uriInfo.getUriResourceParts();
// Note: only in our example we can rely that the first segment is the EntitySet
UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
// the key for the entity
List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();
// 1.2. retrieve the requested (Edm) property
// the last segment is the Property
UriResourceProperty uriProperty = (UriResourceProperty) resourceParts.get(resourceParts.size() - 1);
EdmProperty edmProperty = uriProperty.getProperty();
String edmPropertyName = edmProperty.getName();
// in our example, we know we have only primitive types in our model
EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();
// 2. retrieve data from backend
// 2.1. retrieve the entity data, for which the property has to be read
Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
if (entity == null) { // Bad request
throw new ODataApplicationException("Entity not found",
HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
}
// 2.2. retrieve the property data from the entity
Property property = entity.getProperty(edmPropertyName);
if (property == null) {
throw new ODataApplicationException("Property not found",
HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
}
// 3. serialize
Object value = property.getValue();
if (value != null) {
// 3.1. configure the serializer
ODataSerializer serializer = odata.createSerializer(responseFormat);
ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
// 3.2. serialize
SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
InputStream propertyStream = serializerResult.getContent();
// 4. configure the response object
response.setContent(propertyStream);
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
} else {
// in case there's no value for the property, we can skip the serialization
response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
}
示例12: readPrimitive
import org.apache.olingo.server.api.serializer.SerializerResult; //导入方法依赖的package包/类
public void readPrimitive(ODataRequest request, ODataResponse response,
UriInfo uriInfo, ContentType responseFormat)
throws ODataApplicationException, SerializerException {
// 1. Retrieve info from URI
// 1.1. retrieve the info about the requested entity set
List<UriResource> resourceParts = uriInfo.getUriResourceParts();
// Note: only in our example we can rely that the first segment is the EntitySet
UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
// the key for the entity
List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();
// 1.2. retrieve the requested (Edm) property
// the last segment is the Property
UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1);
EdmProperty edmProperty = uriProperty.getProperty();
String edmPropertyName = edmProperty.getName();
// in our example, we know we have only primitive types in our model
EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();
// 2. retrieve data from backend
// 2.1. retrieve the entity data, for which the property has to be read
Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
if (entity == null) { // Bad request
throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ROOT);
}
// 2.2. retrieve the property data from the entity
Property property = entity.getProperty(edmPropertyName);
if (property == null) {
throw new ODataApplicationException("Property not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ROOT);
}
// 3. serialize
Object value = property.getValue();
if (value != null) {
// 3.1. configure the serializer
ODataSerializer serializer = odata.createSerializer(responseFormat);
ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
// 3.2. serialize
SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
InputStream propertyStream = serializerResult.getContent();
//4. configure the response object
response.setContent(propertyStream);
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
} else {
// in case there's no value for the property, we can skip the serialization
response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
}
示例13: readEntityCollection
import org.apache.olingo.server.api.serializer.SerializerResult; //导入方法依赖的package包/类
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo,
ContentType responseFormat) throws ODataApplicationException, SerializerException {
// 1st retrieve the requested EntitySet from the uriInfo (representation of the parsed URI)
List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
// in our example, the first segment is the EntitySet
UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
// 2nd: fetch the data from backend for this requested EntitySetName and deliver as EntitySet
EntityCollection entityCollection = storage.readEntitySetData(edmEntitySet);
// 3rd: apply System Query Options
// modify the result set according to the query options, specified by the end user
List<Entity> entityList = entityCollection.getEntities();
EntityCollection returnEntityCollection = new EntityCollection();
// handle $count: always return the original number of entities, without considering $top and $skip
CountOption countOption = uriInfo.getCountOption();
if (countOption != null) {
boolean isCount = countOption.getValue();
if (isCount) {
returnEntityCollection.setCount(entityList.size());
}
}
// handle $skip
SkipOption skipOption = uriInfo.getSkipOption();
if (skipOption != null) {
int skipNumber = skipOption.getValue();
if (skipNumber >= 0) {
if(skipNumber <= entityList.size()) {
entityList = entityList.subList(skipNumber, entityList.size());
} else {
// The client skipped all entities
entityList.clear();
}
} else {
throw new ODataApplicationException("Invalid value for $skip", HttpStatusCode.BAD_REQUEST.getStatusCode(),
Locale.ROOT);
}
}
// handle $top
TopOption topOption = uriInfo.getTopOption();
if (topOption != null) {
int topNumber = topOption.getValue();
if (topNumber >= 0) {
if(topNumber <= entityList.size()) {
entityList = entityList.subList(0, topNumber);
} // else the client has requested more entities than available => return what we have
} else {
throw new ODataApplicationException("Invalid value for $top", HttpStatusCode.BAD_REQUEST.getStatusCode(),
Locale.ROOT);
}
}
// after applying the system query options, create the EntityCollection based on the reduced list
for (Entity entity : entityList) {
returnEntityCollection.getEntities().add(entity);
}
// 4th: create a serializer based on the requested format (json)
ODataSerializer serializer = odata.createSerializer(responseFormat);
// and serialize the content: transform from the EntitySet object to InputStream
EdmEntityType edmEntityType = edmEntitySet.getEntityType();
ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();
final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
EntityCollectionSerializerOptions opts =
EntityCollectionSerializerOptions.with().contextURL(contextUrl).id(id).count(countOption).build();
SerializerResult serializerResult =
serializer.entityCollection(serviceMetadata, edmEntityType, returnEntityCollection, opts);
InputStream serializedContent = serializerResult.getContent();
// 5th: configure the response object: set the body, headers and status code
response.setContent(serializedContent);
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}