Search in sources :

Example 1 with ClientEntity

use of org.apache.olingo.client.api.domain.ClientEntity in project tdi-studio-se by Talend.

the class DynamicsCRMClient method getEntityType.

/**
     * Get entity type by EntitySet name
     * 
     * @param entitySetName
     * @return entity type value
     */
public String getEntityType(String entitySetName) {
    // TODO need to check whether have another better way
    URIBuilder uriBuilder = odataClient.newURIBuilder(serviceRootURL).appendEntitySetSegment("EntityDefinitions");
    uriBuilder.addQueryOption(QueryOption.SELECT, "EntitySetName,LogicalName");
    uriBuilder.filter("EntitySetName eq '" + entitySetName + "'");
    ODataEntitySetIteratorRequest<ClientEntitySet, ClientEntity> request = odataClient.getRetrieveRequestFactory().getEntitySetIteratorRequest(uriBuilder.build());
    request.addCustomHeader(HttpHeader.AUTHORIZATION, "Bearer " + authResult.getAccessToken());
    ODataRetrieveResponse<ClientEntitySetIterator<ClientEntitySet, ClientEntity>> response = request.execute();
    try {
        ClientEntitySetIterator<ClientEntitySet, ClientEntity> entitySetIterator = response.getBody();
        if (entitySetIterator.hasNext()) {
            ClientProperty localName = entitySetIterator.next().getProperty("LogicalName");
            if (localName != null && localName.getValue() != null) {
                return localName.getValue().toString();
            }
        }
    } finally {
        response.close();
        // Close reponse would also close connection. So here need recreate httpclient
        httpClient = null;
    }
    return null;
}
Also used : ClientEntity(org.apache.olingo.client.api.domain.ClientEntity) ClientProperty(org.apache.olingo.client.api.domain.ClientProperty) ClientEntitySetIterator(org.apache.olingo.client.api.domain.ClientEntitySetIterator) URIBuilder(org.apache.olingo.client.api.uri.URIBuilder) ClientEntitySet(org.apache.olingo.client.api.domain.ClientEntitySet)

Example 2 with ClientEntity

use of org.apache.olingo.client.api.domain.ClientEntity in project wildfly-camel by wildfly-extras.

the class Olingo4IntegrationTest method testCreateUpdateDelete.

@Test
public void testCreateUpdateDelete() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        public void configure() {
            from("direct://create-entity").to("olingo4://create/People");
            from("direct://update-entity").to("olingo4://update/People('lewisblack')");
            from("direct://delete-entity").to("olingo4://delete/People('lewisblack')");
            from("direct://read-deleted-entity").to("olingo4://delete/People('lewisblack')");
            from("direct://batch").to("olingo4://batch");
        }
    });
    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        final ClientEntity clientEntity = createEntity();
        ClientEntity entity = template.requestBody("direct://create-entity", clientEntity, ClientEntity.class);
        Assert.assertNotNull(entity);
        Assert.assertEquals("Lewis", entity.getProperty("FirstName").getValue().toString());
        Assert.assertEquals("", entity.getProperty("MiddleName").getValue().toString());
        // update
        clientEntity.getProperties().add(objFactory.newPrimitiveProperty("MiddleName", objFactory.newPrimitiveValueBuilder().buildString("Lewis")));
        HttpStatusCode status = template.requestBody("direct://update-entity", clientEntity, HttpStatusCode.class);
        Assert.assertNotNull("Update status", status);
        Assert.assertEquals("Update status", HttpStatusCode.NO_CONTENT.getStatusCode(), status.getStatusCode());
        LOG.info("Update entity status: {}", status);
        // delete
        status = template.requestBody("direct://delete-entity", null, HttpStatusCode.class);
        Assert.assertNotNull("Delete status", status);
        Assert.assertEquals("Delete status", HttpStatusCode.NO_CONTENT.getStatusCode(), status.getStatusCode());
        LOG.info("Delete status: {}", status);
        // check for delete
        try {
            template.requestBody("direct://read-deleted-entity", null, HttpStatusCode.class);
        } catch (CamelExecutionException e) {
            Assert.assertEquals("Resource Not Found [HTTP/1.1 404 Not Found]", e.getCause().getMessage());
        }
    } finally {
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) CamelExecutionException(org.apache.camel.CamelExecutionException) ProducerTemplate(org.apache.camel.ProducerTemplate) ClientEntity(org.apache.olingo.client.api.domain.ClientEntity) RouteBuilder(org.apache.camel.builder.RouteBuilder) HttpStatusCode(org.apache.olingo.commons.api.http.HttpStatusCode) Test(org.junit.Test)

Example 3 with ClientEntity

use of org.apache.olingo.client.api.domain.ClientEntity in project wildfly-camel by wildfly-extras.

the class Olingo4IntegrationTest method createEntity.

private ClientEntity createEntity() {
    ClientEntity clientEntity = objFactory.newEntity(null);
    clientEntity.getProperties().add(objFactory.newPrimitiveProperty("UserName", objFactory.newPrimitiveValueBuilder().buildString("lewisblack")));
    clientEntity.getProperties().add(objFactory.newPrimitiveProperty("FirstName", objFactory.newPrimitiveValueBuilder().buildString("Lewis")));
    clientEntity.getProperties().add(objFactory.newPrimitiveProperty("LastName", objFactory.newPrimitiveValueBuilder().buildString("Black")));
    return clientEntity;
}
Also used : ClientEntity(org.apache.olingo.client.api.domain.ClientEntity)

Example 4 with ClientEntity

use of org.apache.olingo.client.api.domain.ClientEntity in project wildfly-camel by wildfly-extras.

the class Olingo4IntegrationTest method testRead.

@Test
public void testRead() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        public void configure() {
            // test routes for read
            from("direct://readmetadata").to("olingo4://read/$metadata");
            from("direct://readdocument").to("olingo4://read/");
            from("direct://readentities").to("olingo4://read/People?$top=5&$orderby=FirstName asc");
            from("direct://readcount").to("olingo4://read/People/$count");
            from("direct://readvalue").to("olingo4://read/People('russellwhyte')/Gender/$value");
            from("direct://readsingleprop").to("olingo4://read/Airports('KSFO')/Name");
            from("direct://readcomplexprop").to("olingo4://read/Airports('KSFO')/Location");
            from("direct://readentitybyid").to("olingo4://read/People('russellwhyte')");
            from("direct://callunboundfunction").to("olingo4://read/GetNearestAirport(lat=33,lon=-118)");
        }
    });
    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        final Map<String, Object> headers = new HashMap<String, Object>();
        // Read metadata ($metadata) object
        final Edm metadata = (Edm) template.requestBodyAndHeaders("direct://readmetadata", null, headers);
        Assert.assertNotNull(metadata);
        Assert.assertEquals(1, metadata.getSchemas().size());
        // Read service document object
        final ClientServiceDocument document = (ClientServiceDocument) template.requestBodyAndHeaders("direct://readdocument", null, headers);
        Assert.assertNotNull(document);
        Assert.assertTrue(document.getEntitySets().size() > 1);
        LOG.info("Service document has {} entity sets", document.getEntitySets().size());
        // Read entity set of the People object
        final ClientEntitySet entities = (ClientEntitySet) template.requestBodyAndHeaders("direct://readentities", null, headers);
        Assert.assertNotNull(entities);
        Assert.assertEquals(5, entities.getEntities().size());
        // Read object count with query options passed through header
        final Long count = (Long) template.requestBodyAndHeaders("direct://readcount", null, headers);
        Assert.assertEquals(20, count.intValue());
        final ClientPrimitiveValue value = (ClientPrimitiveValue) template.requestBodyAndHeaders("direct://readvalue", null, headers);
        LOG.info("Client value \"{}\" has type {}", value.toString(), value.getTypeName());
        Assert.assertEquals("Male", value.asPrimitive().toString());
        final ClientPrimitiveValue singleProperty = (ClientPrimitiveValue) template.requestBodyAndHeaders("direct://readsingleprop", null, headers);
        Assert.assertTrue(singleProperty.isPrimitive());
        Assert.assertEquals("San Francisco International Airport", singleProperty.toString());
        final ClientComplexValue complexProperty = (ClientComplexValue) template.requestBodyAndHeaders("direct://readcomplexprop", null, headers);
        Assert.assertTrue(complexProperty.isComplex());
        Assert.assertEquals("San Francisco", complexProperty.get("City").getComplexValue().get("Name").getValue().toString());
        final ClientEntity entity = (ClientEntity) template.requestBodyAndHeaders("direct://readentitybyid", null, headers);
        Assert.assertNotNull(entity);
        Assert.assertEquals("Russell", entity.getProperty("FirstName").getValue().toString());
        final ClientEntity unbFuncReturn = (ClientEntity) template.requestBodyAndHeaders("direct://callunboundfunction", null, headers);
        Assert.assertNotNull(unbFuncReturn);
    } finally {
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) ClientPrimitiveValue(org.apache.olingo.client.api.domain.ClientPrimitiveValue) ClientEntity(org.apache.olingo.client.api.domain.ClientEntity) RouteBuilder(org.apache.camel.builder.RouteBuilder) HashMap(java.util.HashMap) ClientComplexValue(org.apache.olingo.client.api.domain.ClientComplexValue) Edm(org.apache.olingo.commons.api.edm.Edm) ClientServiceDocument(org.apache.olingo.client.api.domain.ClientServiceDocument) ClientEntitySet(org.apache.olingo.client.api.domain.ClientEntitySet) Test(org.junit.Test)

Example 5 with ClientEntity

use of org.apache.olingo.client.api.domain.ClientEntity in project syndesis by syndesisio.

the class AbstractODataConnector method createEndpointUri.

@Override
public String createEndpointUri(final String scheme, final Map<String, String> options) throws URISyntaxException {
    // set serviceUri on delegate component
    Olingo4Component delegate = getCamelContext().getComponent(scheme, Olingo4Component.class);
    Olingo4Configuration configuration = new Olingo4Configuration();
    configuration.setServiceUri(this.serviceUri);
    delegate.setConfiguration(configuration);
    setAfterProducer(exchange -> {
        if (!exchange.isFailed()) {
            ClientEntity clientEntity = exchange.getIn().getBody(ClientEntity.class);
            if (clientEntity != null) {
                // convert client entity to JSON
                final StringWriter writer = new StringWriter();
                final Entity entity = odataClient.getBinder().getEntity(clientEntity);
                final ODataSerializer serializer = odataClient.getSerializer(APPLICATION_JSON);
                serializer.write(writer, entity);
                exchange.getIn().setBody(writer.toString());
            }
        }
    // TODO handle failure on missing resource 404
    });
    return super.createEndpointUri(scheme, options);
}
Also used : ClientEntity(org.apache.olingo.client.api.domain.ClientEntity) Entity(org.apache.olingo.commons.api.data.Entity) ClientEntity(org.apache.olingo.client.api.domain.ClientEntity) ODataSerializer(org.apache.olingo.client.api.serialization.ODataSerializer) Olingo4Configuration(org.apache.camel.component.olingo4.Olingo4Configuration) StringWriter(java.io.StringWriter) Olingo4Component(org.apache.camel.component.olingo4.Olingo4Component)

Aggregations

ClientEntity (org.apache.olingo.client.api.domain.ClientEntity)5 CamelContext (org.apache.camel.CamelContext)2 ProducerTemplate (org.apache.camel.ProducerTemplate)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)2 ClientEntitySet (org.apache.olingo.client.api.domain.ClientEntitySet)2 Test (org.junit.Test)2 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 CamelExecutionException (org.apache.camel.CamelExecutionException)1 Olingo4Component (org.apache.camel.component.olingo4.Olingo4Component)1 Olingo4Configuration (org.apache.camel.component.olingo4.Olingo4Configuration)1 ClientComplexValue (org.apache.olingo.client.api.domain.ClientComplexValue)1 ClientEntitySetIterator (org.apache.olingo.client.api.domain.ClientEntitySetIterator)1 ClientPrimitiveValue (org.apache.olingo.client.api.domain.ClientPrimitiveValue)1 ClientProperty (org.apache.olingo.client.api.domain.ClientProperty)1 ClientServiceDocument (org.apache.olingo.client.api.domain.ClientServiceDocument)1 ODataSerializer (org.apache.olingo.client.api.serialization.ODataSerializer)1 URIBuilder (org.apache.olingo.client.api.uri.URIBuilder)1 Entity (org.apache.olingo.commons.api.data.Entity)1