protected void writeLinkInline( XMLWriter2 writer, OLink linkToInline, String href, String baseUri, String updated, boolean isResponse) { writer.startElement(new QName2(m, "inline", "m")); if (linkToInline instanceof ORelatedEntitiesLinkInline) { ORelatedEntitiesLinkInline relLink = ((ORelatedEntitiesLinkInline) linkToInline); List<OEntity> entities = relLink.getRelatedEntities(); if (entities != null && !entities.isEmpty()) { writer.startElement(new QName2("feed")); writeElement(writer, "title", linkToInline.getTitle(), "type", "text"); writeElement(writer, "id", baseUri + href); writeElement(writer, "updated", updated); writeElement( writer, "link", null, "rel", "self", "title", linkToInline.getTitle(), "href", href); for (OEntity entity : ((ORelatedEntitiesLinkInline) linkToInline).getRelatedEntities()) { writer.startElement("entry"); writeEntry( writer, entity, entity.getProperties(), entity.getLinks(), entity.getEntitySet().name, baseUri, updated, entity.getEntitySet(), isResponse); writer.endElement("entry"); } writer.endElement("feed"); } } else if (linkToInline instanceof ORelatedEntityLinkInline) { OEntity entity = ((ORelatedEntityLinkInline) linkToInline).getRelatedEntity(); if (entity != null) { writer.startElement("entry"); writeEntry( writer, entity, entity.getProperties(), entity.getLinks(), entity.getEntitySet().name, baseUri, updated, entity.getEntitySet(), isResponse); writer.endElement("entry"); } } else throw new RuntimeException("Unknown OLink type " + linkToInline.getClass()); writer.endElement("inline"); }
private OEntity processOEntity(EntityResource<OEntity> entityResource) { List<OLink> embeddedLinks = formOLinks(entityResource); OEntity embeddedEntity = entityResource.getEntity(); // replace the OLink's on the embedded entity OEntity newOEntity = OEntities.create( embeddedEntity.getEntitySet(), embeddedEntity.getEntityKey(), embeddedEntity.getProperties(), embeddedLinks); return newOEntity; }
private OAtomEntity getAtomInfo(OEntity oe) { if (oe != null) { OAtomEntity atomEntity = oe.findExtension(OAtomEntity.class); if (atomEntity != null) return atomEntity; } return new OAtomEntity() { @Override public String getAtomEntityTitle() { return null; } @Override public String getAtomEntitySummary() { return null; } @Override public String getAtomEntityAuthor() { return null; } @Override public LocalDateTime getAtomEntityUpdated() { return null; } }; }
protected void deleteEntityAndTest(ODataConsumer consumer, String customerID) { OEntity customer = consumer.getEntity("Customers", customerID).execute(); Assert.assertNotNull(customer); Assert.assertEquals(customerID, customer.getEntityKey().asSingleValue()); Assert.assertNotNull( consumer .getEntities("Customers") .execute() .firstOrNull(OPredicates.entityPropertyValueEquals("CustomerID", customerID))); consumer.deleteEntity("Customers", customer.getEntityKey()).execute(); Assert.assertNull( consumer .getEntities("Customers") .execute() .firstOrNull(OPredicates.entityPropertyValueEquals("CustomerID", customerID))); }
/** * To handle generic case where user do not know the type, here we will be looking for a type and * then generate EntityResource<E> * * @param entity * @return EntityResource<E> */ public static <E> EntityResource<E> createEntityResource(E entity) { GenericEntity<E> ge = new GenericEntity<E>(entity) {}; Type t = com.temenos.interaction.core.command.CommandHelper.getEffectiveGenericType( ge.getType(), entity); if (ResourceTypeHelper.isType(ge.getRawType(), t, OEntity.class, OEntity.class)) { OEntity te = (OEntity) entity; String entityName = te != null && te.getEntityType() != null ? te.getEntityType().getName() : null; return com.temenos.interaction.core.command.CommandHelper.createEntityResource( entityName, entity, OEntity.class); } else if (ResourceTypeHelper.isType(ge.getRawType(), t, Entity.class, Entity.class)) { Entity te = (Entity) entity; String entityName = te != null ? te.getName() : null; return com.temenos.interaction.core.command.CommandHelper.createEntityResource( entityName, entity, Entity.class); } else { // Call the generic and lets see what happens return com.temenos.interaction.core.command.CommandHelper.createEntityResource(entity, null); } }
/* * Using the supplied EntityResource, add the embedded resources * from the OEntity embedded resources. NB - only an OEntity can * carry OLinks. */ public void addExpandedLinks(EntityResource<OEntity> entityResource) { RequestContext requestContext = RequestContext.getRequestContext(); Collection<Link> links = entityResource.getLinks(); if (links != null) { OEntity oentity = entityResource.getEntity(); List<OLink> olinks = oentity.getLinks(); for (OLink olink : olinks) { if (olink.isInline()) { String relid = InternalUtil.getEntityRelId(oentity); String href = relid + "/" + olink.getTitle(); for (Link link : links) { String linkHref = link.getHref(); if (requestContext != null) { // Extract the transition fragment from the URI path linkHref = link.getRelativeHref(getBaseUri(serviceDocument, uriInfo)); } if (href.equals(linkHref)) { if (entityResource.getEmbedded() == null) { entityResource.setEmbedded(new HashMap<Transition, RESTResource>()); } if (olink.isCollection()) { List<OEntity> oentities = olink.getRelatedEntities(); Collection<EntityResource<OEntity>> entityResources = new ArrayList<EntityResource<OEntity>>(); for (OEntity oe : oentities) { entityResources.add(new EntityResource<OEntity>(oe)); } entityResource .getEmbedded() .put(link.getTransition(), new CollectionResource<OEntity>(entityResources)); } else { // replace the OLink's on the current entity OEntity inlineOentity = olink.getRelatedEntity(); List<OLink> inlineResourceOlinks = formOLinks(new EntityResource<OEntity>(inlineOentity)); OEntity newInlineOentity = OEntities.create( inlineOentity.getEntitySet(), inlineOentity.getEntityKey(), inlineOentity.getProperties(), inlineResourceOlinks); entityResource .getEmbedded() .put(link.getTransition(), new EntityResource<OEntity>(newInlineOentity)); } } } } } } }
protected String writeEntry( XMLWriter2 writer, OEntity oe, List<OProperty<?>> entityProperties, List<OLink> entityLinks, String baseUri, String updated, EdmEntitySet ees, boolean isResponse) { String relid = null; String absid = null; if (isResponse) { relid = InternalUtil.getEntityRelId(oe); absid = baseUri + relid; writeElement(writer, "id", absid); } OAtomEntity oae = getAtomInfo(oe); writeElement(writer, "title", oae.getAtomEntityTitle(), "type", "text"); String summary = oae.getAtomEntitySummary(); if (summary != null) { writeElement(writer, "summary", summary, "type", "text"); } LocalDateTime updatedTime = oae.getAtomEntityUpdated(); if (updatedTime != null) { updated = InternalUtil.toString(updatedTime.toDateTime(DateTimeZone.UTC)); } writeElement(writer, "updated", updated); writer.startElement("author"); writeElement(writer, "name", oae.getAtomEntityAuthor()); writer.endElement("author"); if (isResponse) { writeElement( writer, "link", null, "rel", "edit", "title", ees.getType().getName(), "href", relid); } if (entityLinks != null) { if (isResponse) { // the producer has populated the link collection, we just what he gave us. for (OLink link : entityLinks) { String rel = related + link.getTitle(); String type = (link.isCollection()) ? atom_feed_content_type : atom_entry_content_type; String href = relid + "/" + link.getTitle(); if (link.isInline()) { writer.startElement("link"); writer.writeAttribute("rel", rel); writer.writeAttribute("type", type); writer.writeAttribute("title", link.getTitle()); writer.writeAttribute("href", href); // write the inlined entities inside the link element writeLinkInline(writer, link, href, baseUri, updated, isResponse); writer.endElement("link"); } else { // deferred link. writeElement( writer, "link", null, "rel", rel, "type", type, "title", link.getTitle(), "href", href); } } } else { // for requests we include only the provided links // Note: It seems that OLinks for responses are only built using the // title and OLinks for requests have the additional info in them // alread. I'm leaving that inconsistency in place for now but this // else and its preceding if could probably be unified. for (OLink olink : entityLinks) { String type = olink.isCollection() ? atom_feed_content_type : atom_entry_content_type; writer.startElement("link"); writer.writeAttribute("rel", olink.getRelation()); writer.writeAttribute("type", type); writer.writeAttribute("title", olink.getTitle()); writer.writeAttribute("href", olink.getHref()); if (olink.isInline()) { // write the inlined entities inside the link element writeLinkInline(writer, olink, olink.getHref(), baseUri, updated, isResponse); } writer.endElement("link"); } } } // else entityLinks null writeElement( writer, "category", null, // oe is null for creates "term", oe == null ? ees.getType().getFullyQualifiedTypeName() : oe.getEntityType().getFullyQualifiedTypeName(), "scheme", scheme); boolean hasStream = false; if (oe != null) { OAtomStreamEntity stream = oe.findExtension(OAtomStreamEntity.class); if (stream != null) { hasStream = true; writer.startElement("content"); writer.writeAttribute("type", stream.getAtomEntityType()); writer.writeAttribute("src", baseUri + stream.getAtomEntitySource()); writer.endElement("content"); } } if (!hasStream) { writer.startElement("content"); writer.writeAttribute("type", MediaType.APPLICATION_XML); } writer.startElement(new QName2(m, "properties", "m")); writeProperties(writer, entityProperties); writer.endElement("properties"); if (!hasStream) { writer.endElement("content"); } return absid; }
@Override public BaseResponse callFunction( ODataContext context, EdmFunctionImport name, Map<String, OFunctionParameter> params, QueryInfo queryInfo) { BaseResponse response; ServiceOperationsProducerMock.LOGGER.debug("EdmFunctionImport Object: " + name.getName()); ServiceOperationsProducerMock.LOGGER.debug("EdmFunctionImport Parameter: " + params); ServiceOperationsProducerMock.LOGGER.debug("EdmFunctionImport QueryInfo: " + queryInfo); this.queryParameter = params; this.queryInfo = queryInfo; if (ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_STRING.equals(name.getName())) { response = Responses.simple( EdmSimpleType.STRING, name.getName(), ServiceOperationsProducerMock.SOME_TEXT); } else if (ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_STRING_PUT.equals( name.getName())) { response = Responses.simple( EdmSimpleType.STRING, name.getName(), ServiceOperationsProducerMock.SOME_TEXT); } else if (ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_STRING_GET.equals( name.getName())) { response = Responses.simple( EdmSimpleType.STRING, name.getName(), ServiceOperationsProducerMock.SOME_TEXT); } else if (ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_STRING_DELETE.equals( name.getName())) { response = Responses.simple( EdmSimpleType.STRING, name.getName(), ServiceOperationsProducerMock.SOME_TEXT); } else if (ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_STRING_PATCH.equals( name.getName())) { response = Responses.simple( EdmSimpleType.STRING, name.getName(), ServiceOperationsProducerMock.SOME_TEXT); } else if (ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_STRING_MERGE.equals( name.getName())) { response = Responses.simple( EdmSimpleType.STRING, name.getName(), ServiceOperationsProducerMock.SOME_TEXT); } else if (ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_STRING_POST.equals( name.getName())) { response = Responses.simple( EdmSimpleType.STRING, name.getName(), ServiceOperationsProducerMock.SOME_TEXT); } else if (ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_BOOLEAN.equals(name.getName())) { response = Responses.simple( EdmSimpleType.BOOLEAN, name.getName(), ServiceOperationsProducerMock.BOOLEAN_VALUE); } else if (ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_INT16.equals(name.getName())) { response = Responses.simple( EdmSimpleType.INT16, name.getName(), ServiceOperationsProducerMock.INT16_VALUE); } else if (ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_ENTITY.equals(name.getName())) { OEntity entity = this.createEmployeeEntity(); response = Responses.entity(entity); } else if (ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_COMPLEX_TYPE.equals( name.getName())) { OComplexObject complexObject = this.createComplexTypeLocation(); response = Responses.complexObject( complexObject, ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_COMPLEX_TYPE); } else if (ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_COLLECTION_STRING.equals( name.getName())) { Builder<OObject> collectionBuilder = OCollections.newBuilder(EdmSimpleType.STRING); collectionBuilder .add( OSimpleObjects.create( EdmSimpleType.STRING, ServiceOperationsProducerMock.COLLECTION_STRING1)) .build(); collectionBuilder .add( OSimpleObjects.create( EdmSimpleType.STRING, ServiceOperationsProducerMock.COLLECTION_STRING2)) .build(); OCollection<OObject> collection = collectionBuilder.build(); response = Responses.collection( collection, null, null, null, ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_COLLECTION_STRING); } else if (ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_COLLECTION_DOUBLE.equals( name.getName())) { Builder<OObject> collectionBuilder = OCollections.newBuilder(EdmSimpleType.DOUBLE); collectionBuilder .add( OSimpleObjects.create( EdmSimpleType.DOUBLE, ServiceOperationsProducerMock.COLLECTION_DOUBLE1)) .build(); collectionBuilder .add( OSimpleObjects.create( EdmSimpleType.DOUBLE, ServiceOperationsProducerMock.COLLECTION_DOUBLE2)) .build(); OCollection<OObject> collection = collectionBuilder.build(); response = Responses.collection( collection, null, null, null, ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_COLLECTION_DOUBLE); } else if (ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_COLLECTION_COMPLEX_TYPE.equals( name.getName())) { OComplexObject complexObject1 = this.createComplexTypeLocation(); OComplexObject complexObject2 = this.createComplexTypeLocation(); EdmComplexType type = this.getMetadata() .findEdmComplexType(ServiceOperationsProducerMock.COMPLEY_TYPE_NAME_LOCATION); Builder<OObject> collectionBuilder = OCollections.newBuilder(type); collectionBuilder.add(complexObject1); collectionBuilder.add(complexObject2); OCollection<OObject> collection = collectionBuilder.build(); response = Responses.collection( collection, null, null, null, ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_COLLECTION_COMPLEX_TYPE); } else if (ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_COLLECTION_ENTITY.equals( name.getName())) { OEntity entity = this.createEmployeeEntity(); Builder<OObject> collectionBuilder = OCollections.newBuilder(entity.getType()); collectionBuilder.add(entity); OCollection<OObject> collection = collectionBuilder.build(); response = Responses.collection( collection, entity.getEntitySet(), null, null, ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_COLLECTION_ENTITY); } else if (ServiceOperationsMetadataUtil.TEST_FUNCTION_RETURN_ENTITYSET.equals( name.getName())) { List<OEntity> entities = new ArrayList<OEntity>(); entities.add(createEmployeeEntity()); response = Responses.entities(entities, name.getEntitySet(), null, null); } else { throw new RuntimeException("Unsupported Test Case for FunctionImport: " + name.getName()); } return response; }
/** * Writes a Atom (OData) representation of {@link EntityResource} to the output stream. * * @precondition supplied {@link EntityResource} is non null * @precondition {@link EntityResource#getEntity()} returns a valid OEntity, this provider only * supports serialising OEntities * @postcondition non null Atom (OData) XML document written to OutputStream * @invariant valid OutputStream */ @SuppressWarnings("unchecked") @Override public void writeTo( RESTResource resource, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { assert (resource != null); assert (uriInfo != null); // Set response headers if (httpHeaders != null) { httpHeaders.putSingle( HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_ATOM_XML); // Workaround for // https://issues.apache.org/jira/browse/WINK-374 } try { RESTResource restResource = processLinks((RESTResource) resource); Collection<Link> processedLinks = restResource.getLinks(); if (ResourceTypeHelper.isType(type, genericType, EntityResource.class, OEntity.class)) { EntityResource<OEntity> entityResource = (EntityResource<OEntity>) resource; OEntity tempEntity = entityResource.getEntity(); EdmEntitySet entitySet = getEdmEntitySet(entityResource.getEntityName()); List<OLink> olinks = formOLinks(entityResource); // Write entry // create OEntity with our EdmEntitySet see issue // https://github.com/aphethean/IRIS/issues/20 OEntity oentity = OEntities.create( entitySet, tempEntity.getEntityKey(), tempEntity.getProperties(), null); entryWriter.write( uriInfo, new OutputStreamWriter(entityStream, UTF_8), Responses.entity(oentity), entitySet, olinks); } else if (ResourceTypeHelper.isType(type, genericType, EntityResource.class, Entity.class)) { EntityResource<Entity> entityResource = (EntityResource<Entity>) resource; // Write entry Entity entity = entityResource.getEntity(); String entityName = entityResource.getEntityName(); // Write Entity object with Abdera implementation entityEntryWriter.write( uriInfo, new OutputStreamWriter(entityStream, UTF_8), entityName, entity, processedLinks, entityResource.getEmbedded()); } else if (ResourceTypeHelper.isType(type, genericType, EntityResource.class)) { EntityResource<Object> entityResource = (EntityResource<Object>) resource; // Links and entity properties Object entity = entityResource.getEntity(); String entityName = entityResource.getEntityName(); EntityProperties props = new EntityProperties(); if (entity != null) { Map<String, Object> objProps = (transformer != null ? transformer : new BeanTransformer()).transform(entity); if (objProps != null) { for (String propName : objProps.keySet()) { props.setProperty(new EntityProperty(propName, objProps.get(propName))); } } } entityEntryWriter.write( uriInfo, new OutputStreamWriter(entityStream, UTF_8), entityName, new Entity(entityName, props), processedLinks, entityResource.getEmbedded()); } else if (ResourceTypeHelper.isType( type, genericType, CollectionResource.class, OEntity.class)) { CollectionResource<OEntity> collectionResource = ((CollectionResource<OEntity>) resource); EdmEntitySet entitySet = getEdmEntitySet(collectionResource.getEntityName()); List<EntityResource<OEntity>> collectionEntities = (List<EntityResource<OEntity>>) collectionResource.getEntities(); List<OEntity> entities = new ArrayList<OEntity>(); Map<OEntity, Collection<Link>> linkId = new HashMap<OEntity, Collection<Link>>(); for (EntityResource<OEntity> collectionEntity : collectionEntities) { // create OEntity with our EdmEntitySet see issue // https://github.com/aphethean/IRIS/issues/20 OEntity tempEntity = collectionEntity.getEntity(); List<OLink> olinks = formOLinks(collectionEntity); Collection<Link> links = collectionEntity.getLinks(); OEntity entity = OEntities.create( entitySet, null, tempEntity.getEntityKey(), tempEntity.getEntityTag(), tempEntity.getProperties(), olinks); entities.add(entity); linkId.put(entity, links); } // TODO implement collection properties and get transient values for inlinecount and // skiptoken Integer inlineCount = null; String skipToken = null; feedWriter.write( uriInfo, new OutputStreamWriter(entityStream, UTF_8), processedLinks, Responses.entities(entities, entitySet, inlineCount, skipToken), metadata.getModelName(), linkId); } else if (ResourceTypeHelper.isType( type, genericType, CollectionResource.class, Entity.class)) { CollectionResource<Entity> collectionResource = ((CollectionResource<Entity>) resource); // TODO implement collection properties and get transient values for inlinecount and // skiptoken Integer inlineCount = null; String skipToken = null; // Write feed AtomEntityFeedFormatWriter entityFeedWriter = new AtomEntityFeedFormatWriter(serviceDocument, metadata); entityFeedWriter.write( uriInfo, new OutputStreamWriter(entityStream, UTF_8), collectionResource, inlineCount, skipToken, metadata.getModelName()); } else { logger.error( "Accepted object for writing in isWriteable, but type not supported in writeTo method"); throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR); } } catch (ODataProducerException e) { logger.error("An error occurred while writing " + mediaType + " resource representation", e); } }
/** * Create an OData entity resource (entry) * * @param e OEntity * @return entity resource EntityResource of parameter type OEntity */ public static EntityResource<OEntity> createEntityResource(OEntity e) { String entityName = e != null && e.getEntityType() != null ? e.getEntityType().getName() : null; return com.temenos.interaction.core.command.CommandHelper.createEntityResource( entityName, e, OEntity.class); }