Search in sources :

Example 1 with Edm

use of org.apache.olingo.odata2.api.edm.Edm in project camel by apache.

the class Olingo2AppAPITest method testReadFeed.

@Test
public void testReadFeed() throws Exception {
    final TestOlingo2ResponseHandler<ODataFeed> responseHandler = new TestOlingo2ResponseHandler<ODataFeed>();
    olingoApp.read(edm, MANUFACTURERS, null, responseHandler);
    final ODataFeed dataFeed = responseHandler.await();
    assertNotNull("Data feed", dataFeed);
    LOG.info("Entries:  {}", prettyPrint(dataFeed));
}
Also used : ODataFeed(org.apache.olingo.odata2.api.ep.feed.ODataFeed) Test(org.junit.Test)

Example 2 with Edm

use of org.apache.olingo.odata2.api.edm.Edm in project camel by apache.

the class Olingo2AppAPITest method testCreateUpdateDeleteEntry.

@Test
public void testCreateUpdateDeleteEntry() throws Exception {
    // create entry to update
    final TestOlingo2ResponseHandler<ODataEntry> entryHandler = new TestOlingo2ResponseHandler<ODataEntry>();
    olingoApp.create(edm, MANUFACTURERS, getEntityData(), entryHandler);
    ODataEntry createdEntry = entryHandler.await();
    LOG.info("Created Entry:  {}", prettyPrint(createdEntry));
    Map<String, Object> data = getEntityData();
    @SuppressWarnings("unchecked") Map<String, Object> address = (Map<String, Object>) data.get(ADDRESS);
    data.put("Name", "MyCarManufacturer Renamed");
    address.put("Street", "Main Street");
    final TestOlingo2ResponseHandler<HttpStatusCodes> statusHandler = new TestOlingo2ResponseHandler<HttpStatusCodes>();
    olingoApp.update(edm, TEST_CREATE_MANUFACTURER, data, statusHandler);
    statusHandler.await();
    statusHandler.reset();
    data.put("Name", "MyCarManufacturer Patched");
    olingoApp.patch(edm, TEST_CREATE_MANUFACTURER, data, statusHandler);
    statusHandler.await();
    entryHandler.reset();
    olingoApp.read(edm, TEST_CREATE_MANUFACTURER, null, entryHandler);
    ODataEntry updatedEntry = entryHandler.await();
    LOG.info("Updated Entry successfully:  {}", prettyPrint(updatedEntry));
    statusHandler.reset();
    olingoApp.delete(TEST_CREATE_MANUFACTURER, statusHandler);
    HttpStatusCodes statusCode = statusHandler.await();
    LOG.info("Deletion of Entry was successful:  {}: {}", statusCode.getStatusCode(), statusCode.getInfo());
    try {
        LOG.info("Verify Delete Entry");
        entryHandler.reset();
        olingoApp.read(edm, TEST_CREATE_MANUFACTURER, null, entryHandler);
        entryHandler.await();
        fail("Entry not deleted!");
    } catch (Exception e) {
        LOG.info("Deleted entry not found: {}", e.getMessage());
    }
}
Also used : HttpStatusCodes(org.apache.olingo.odata2.api.commons.HttpStatusCodes) ODataEntry(org.apache.olingo.odata2.api.ep.entry.ODataEntry) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with Edm

use of org.apache.olingo.odata2.api.edm.Edm in project camel by apache.

the class Olingo2AppAPITest method testReadUnparsedEntry.

@Test
public void testReadUnparsedEntry() throws Exception {
    final TestOlingo2ResponseHandler<InputStream> responseHandler = new TestOlingo2ResponseHandler<InputStream>();
    olingoApp.uread(edm, TEST_MANUFACTURER, null, responseHandler);
    InputStream rawentry = responseHandler.await();
    ODataEntry entry = EntityProvider.readEntry(TEST_FORMAT_STRING, edmEntitySetMap.get(MANUFACTURERS), rawentry, EntityProviderReadProperties.init().build());
    LOG.info("Single Entry:  {}", prettyPrint(entry));
    responseHandler.reset();
    olingoApp.uread(edm, TEST_CAR, null, responseHandler);
    rawentry = responseHandler.await();
    entry = EntityProvider.readEntry(TEST_FORMAT_STRING, edmEntitySetMap.get(CARS), rawentry, EntityProviderReadProperties.init().build());
    LOG.info("Single Entry:  {}", prettyPrint(entry));
    responseHandler.reset();
    final Map<String, String> queryParams = new HashMap<String, String>();
    queryParams.put(SystemQueryOption.$expand.toString(), CARS);
    olingoApp.uread(edm, TEST_MANUFACTURER, queryParams, responseHandler);
    rawentry = responseHandler.await();
    ODataEntry entryExpanded = EntityProvider.readEntry(TEST_FORMAT_STRING, edmEntitySetMap.get(MANUFACTURERS), rawentry, EntityProviderReadProperties.init().build());
    LOG.info("Single Entry with expanded Cars relation:  {}", prettyPrint(entryExpanded));
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) ODataEntry(org.apache.olingo.odata2.api.ep.entry.ODataEntry) Test(org.junit.Test)

Example 4 with Edm

use of org.apache.olingo.odata2.api.edm.Edm in project camel by apache.

the class Olingo2AppImpl method replaceContentId.

private static String replaceContentId(Edm edm, String entityReference, Map<String, String> contentIdMap) throws EdmException {
    final int pathSeparator = entityReference.indexOf('/');
    final StringBuilder referencedEntity;
    if (pathSeparator == -1) {
        referencedEntity = new StringBuilder(contentIdMap.get(entityReference));
    } else {
        referencedEntity = new StringBuilder(contentIdMap.get(entityReference.substring(0, pathSeparator)));
    }
    // create a dummy entity location by adding a dummy key predicate
    // look for a Container name if available
    String referencedEntityName = referencedEntity.toString();
    final int containerSeparator = referencedEntityName.lastIndexOf('.');
    final EdmEntityContainer entityContainer;
    if (containerSeparator != -1) {
        final String containerName = referencedEntityName.substring(0, containerSeparator);
        referencedEntityName = referencedEntityName.substring(containerSeparator + 1);
        entityContainer = edm.getEntityContainer(containerName);
        if (entityContainer == null) {
            throw new IllegalArgumentException("EDM does not have entity container " + containerName);
        }
    } else {
        entityContainer = edm.getDefaultEntityContainer();
        if (entityContainer == null) {
            throw new IllegalArgumentException("EDM does not have a default entity container" + ", use a fully qualified entity set name");
        }
    }
    final EdmEntitySet entitySet = entityContainer.getEntitySet(referencedEntityName);
    final List<EdmProperty> keyProperties = entitySet.getEntityType().getKeyProperties();
    if (keyProperties.size() == 1) {
        referencedEntity.append("('dummy')");
    } else {
        referencedEntity.append("(");
        for (EdmProperty keyProperty : keyProperties) {
            referencedEntity.append(keyProperty.getName()).append('=').append("'dummy',");
        }
        referencedEntity.deleteCharAt(referencedEntity.length() - 1);
        referencedEntity.append(')');
    }
    return pathSeparator == -1 ? referencedEntityName : referencedEntity.append(entityReference.substring(pathSeparator)).toString();
}
Also used : EdmEntitySet(org.apache.olingo.odata2.api.edm.EdmEntitySet) EdmProperty(org.apache.olingo.odata2.api.edm.EdmProperty) EdmEntityContainer(org.apache.olingo.odata2.api.edm.EdmEntityContainer)

Example 5 with Edm

use of org.apache.olingo.odata2.api.edm.Edm in project camel by apache.

the class Olingo2AppWrapper method getEdm.

// double checked locking based singleton Edm reader
public Edm getEdm() throws RuntimeCamelException {
    Edm localEdm = edm;
    if (localEdm == null) {
        synchronized (this) {
            localEdm = edm;
            if (localEdm == null) {
                final CountDownLatch latch = new CountDownLatch(1);
                final Exception[] error = new Exception[1];
                olingo2App.read(null, "$metadata", null, new Olingo2ResponseHandler<Edm>() {

                    @Override
                    public void onResponse(Edm response) {
                        edm = response;
                        latch.countDown();
                    }

                    @Override
                    public void onException(Exception ex) {
                        error[0] = ex;
                        latch.countDown();
                    }

                    @Override
                    public void onCanceled() {
                        error[0] = new RuntimeCamelException("OData HTTP request cancelled!");
                        latch.countDown();
                    }
                });
                try {
                    // wait until response or timeout
                    latch.await();
                    final Exception ex = error[0];
                    if (ex != null) {
                        if (ex instanceof RuntimeCamelException) {
                            throw (RuntimeCamelException) ex;
                        } else {
                            final String message = ex.getMessage() != null ? ex.getMessage() : ex.getClass().getName();
                            throw new RuntimeCamelException("Error reading EDM: " + message, ex);
                        }
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeCamelException(e.getMessage(), e);
                }
                localEdm = edm;
            }
        }
    }
    return localEdm;
}
Also used : RuntimeCamelException(org.apache.camel.RuntimeCamelException) CountDownLatch(java.util.concurrent.CountDownLatch) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Edm(org.apache.olingo.odata2.api.edm.Edm)

Aggregations

HashMap (java.util.HashMap)11 Test (org.junit.Test)8 Map (java.util.Map)7 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 Olingo2BatchRequest (org.apache.camel.component.olingo2.api.batch.Olingo2BatchRequest)5 ODataEntry (org.apache.olingo.odata2.api.ep.entry.ODataEntry)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Olingo2BatchResponse (org.apache.camel.component.olingo2.api.batch.Olingo2BatchResponse)4 EdmEntitySet (org.apache.olingo.odata2.api.edm.EdmEntitySet)4 ODataFeed (org.apache.olingo.odata2.api.ep.feed.ODataFeed)4 List (java.util.List)3 HttpStatusCodes (org.apache.olingo.odata2.api.commons.HttpStatusCodes)3 Edm (org.apache.olingo.odata2.api.edm.Edm)3 EdmProperty (org.apache.olingo.odata2.api.edm.EdmProperty)3 ODataApplicationException (org.apache.olingo.odata2.api.exception.ODataApplicationException)3 ODataResponse (org.apache.olingo.odata2.api.processor.ODataResponse)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Olingo2BatchQueryRequest (org.apache.camel.component.olingo2.api.batch.Olingo2BatchQueryRequest)2 ContentType (org.apache.http.entity.ContentType)2