@Override protected void loadAllEntitySets() { List<? extends EntitySet> localEntitySets = xmlEntityContainer.getEntitySets(); if (localEntitySets != null) { for (EntitySet entitySet : localEntitySets) { EdmEntitySet edmSet; final FullQualifiedName entityType = new EdmTypeInfo.Builder() .setTypeExpression(entitySet.getEntityType()) .setDefaultNamespace(entityContainerName.getNamespace()) .build() .getFullQualifiedName(); if (entitySet instanceof org.apache.olingo.client.api.edm.xml.v4.EntitySet) { edmSet = new EdmEntitySetImpl( edm, this, entitySet.getName(), entityType, (org.apache.olingo.client.api.edm.xml.v4.EntitySet) entitySet); } else { edmSet = new EdmEntitySetProxy(edm, this, entitySet.getName(), entityType, xmlMetadata); } entitySets.put(edmSet.getName(), edmSet); } } }
private void appendFunctionImports( final XMLStreamWriter writer, final List<EdmFunctionImport> functionImports, final String containerNamespace) throws XMLStreamException { for (EdmFunctionImport functionImport : functionImports) { writer.writeStartElement(XML_FUNCTION_IMPORT); writer.writeAttribute(XML_NAME, functionImport.getName()); String functionFQNString; FullQualifiedName functionFqn = functionImport.getFunctionFqn(); if (namespaceToAlias.get(functionFqn.getNamespace()) != null) { functionFQNString = namespaceToAlias.get(functionFqn.getNamespace()) + "." + functionFqn.getName(); } else { functionFQNString = functionFqn.getFullQualifiedNameAsString(); } writer.writeAttribute(XML_FUNCTION, functionFQNString); EdmEntitySet returnedEntitySet = functionImport.getReturnedEntitySet(); if (returnedEntitySet != null) { writer.writeAttribute( XML_ENTITY_SET, containerNamespace + "." + returnedEntitySet.getName()); } // Default is false and we do not write the default if (functionImport.isIncludeInServiceDocument()) { writer.writeAttribute( XML_INCLUDE_IN_SERVICE_DOCUMENT, "" + functionImport.isIncludeInServiceDocument()); } writer.writeEndElement(); } }
private void writeEntitySets(final JsonGenerator gen, final EdmEntityContainer container) throws IOException { for (EdmEntitySet edmEntitySet : container.getEntitySets()) { if (edmEntitySet.isIncludeInServiceDocument()) { writeElement(gen, null, edmEntitySet.getName(), edmEntitySet.getName()); } } }
private void appendEntitySets(final XMLStreamWriter writer, final List<EdmEntitySet> entitySets) throws XMLStreamException { for (EdmEntitySet entitySet : entitySets) { writer.writeStartElement(XML_ENTITY_SET); writer.writeAttribute(XML_NAME, entitySet.getName()); writer.writeAttribute( XML_ENTITY_TYPE, getAliasedFullQualifiedName(entitySet.getEntityType(), false)); if (!entitySet.isIncludeInServiceDocument()) { writer.writeAttribute( XML_INCLUDE_IN_SERVICE_DOCUMENT, "" + entitySet.isIncludeInServiceDocument()); } appendNavigationPropertyBindings(writer, entitySet); writer.writeEndElement(); } }
@Override public void readEntity( final ODataRequest request, ODataResponse response, final UriInfo uriInfo, final ContentType requestedContentType) throws ODataApplicationException, SerializerException { // First we have to figure out which entity set the requested entity is in final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource()); // Next we fetch the requested entity from the database Entity entity; try { entity = readEntityInternal(uriInfo.asUriInfoResource(), edmEntitySet); } catch (DataProviderException e) { throw new ODataApplicationException(e.getMessage(), 500, Locale.ENGLISH); } if (entity == null) { // If no entity was found for the given key we throw an exception. throw new ODataApplicationException( "No entity found for this key", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH); } else { // If an entity was found we proceed by serializing it and sending it to the client. ODataSerializer serializer = odata.createSerializer(requestedContentType); final ExpandOption expand = uriInfo.getExpandOption(); final SelectOption select = uriInfo.getSelectOption(); InputStream serializedContent = serializer .entity( edm, edmEntitySet.getEntityType(), entity, EntitySerializerOptions.with() .contextURL( isODataMetadataNone(requestedContentType) ? null : getContextUrl(edmEntitySet, true, expand, select, null)) .expand(expand) .select(select) .build()) .getContent(); response.setContent(serializedContent); response.setStatusCode(HttpStatusCode.OK.getStatusCode()); response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString()); } }
@Override public void readEntityCollection( final ODataRequest request, ODataResponse response, final UriInfo uriInfo, final ContentType requestedContentType) throws ODataApplicationException, SerializerException { // First we have to figure out which entity set to use final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource()); // Second we fetch the data for this specific entity set from the mock database and transform it // into an EntitySet // object which is understood by our serialization EntityCollection entitySet = dataProvider.readAll(edmEntitySet); // Next we create a serializer based on the requested format. This could also be a custom format // but we do not // support them in this example ODataSerializer serializer = odata.createSerializer(requestedContentType); // Now the content is serialized using the serializer. final ExpandOption expand = uriInfo.getExpandOption(); final SelectOption select = uriInfo.getSelectOption(); InputStream serializedContent = serializer .entityCollection( edm, edmEntitySet.getEntityType(), entitySet, EntityCollectionSerializerOptions.with() .contextURL( isODataMetadataNone(requestedContentType) ? null : getContextUrl(edmEntitySet, false, expand, select, null)) .count(uriInfo.getCountOption()) .expand(expand) .select(select) .build()) .getContent(); // Finally we set the response data, headers and status code response.setContent(serializedContent); response.setStatusCode(HttpStatusCode.OK.getStatusCode()); response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString()); }
private ContextURL getContextUrl( final EdmEntitySet entitySet, final boolean isSingleEntity, final ExpandOption expand, final SelectOption select, final String navOrPropertyPath) throws SerializerException { return ContextURL.with() .entitySet(entitySet) .selectList( odata .createUriHelper() .buildContextURLSelectList(entitySet.getEntityType(), expand, select)) .suffix(isSingleEntity ? Suffix.ENTITY : null) .navOrPropertyPath(navOrPropertyPath) .build(); }
/** * Example: For the following navigation: DemoService.svc/Categories(1)/Products we need the * EdmEntitySet for the navigation property "Products" * * <p>This is defined as follows in the metadata: <code> * * <EntitySet Name="Categories" EntityType="OData.Demo.Category"> * <NavigationPropertyBinding Path="Products" Target="Products"/> * </EntitySet> * </code> The "Target" attribute specifies the target EntitySet Therefore we need the * startEntitySet "Categories" in order to retrieve the target EntitySet "Products" */ public static EdmEntitySet getNavigationTargetEntitySet( EdmEntitySet startEntitySet, EdmNavigationProperty edmNavigationProperty) throws ODataApplicationException { EdmEntitySet navigationTargetEntitySet = null; String navPropName = edmNavigationProperty.getName(); EdmBindingTarget edmBindingTarget = startEntitySet.getRelatedBindingTarget(navPropName); if (edmBindingTarget == null) { throw new ODataApplicationException( "Not supported.", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT); } if (edmBindingTarget instanceof EdmEntitySet) { navigationTargetEntitySet = (EdmEntitySet) edmBindingTarget; } else { throw new ODataApplicationException( "Not supported.", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT); } return navigationTargetEntitySet; }