Search in sources :

Example 1 with ODataEntry

use of org.apache.olingo.odata2.api.ep.entry.ODataEntry 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 2 with ODataEntry

use of org.apache.olingo.odata2.api.ep.entry.ODataEntry 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 3 with ODataEntry

use of org.apache.olingo.odata2.api.ep.entry.ODataEntry in project camel by apache.

the class Olingo2ComponentTest method testRead.

@Test
public void testRead() throws Exception {
    final Map<String, Object> headers = new HashMap<String, Object>();
    // read ServiceDocument
    final ServiceDocument document = requestBodyAndHeaders("direct://READSERVICEDOC", null, headers);
    assertNotNull(document);
    assertFalse("ServiceDocument entity sets", document.getEntitySetsInfo().isEmpty());
    LOG.info("Service document has {} entity sets", document.getEntitySetsInfo().size());
    // parameter type is java.util.Map
    final HashMap<String, String> queryParams = new HashMap<String, String>();
    queryParams.put(SystemQueryOption.$top.name(), "5");
    headers.put("CamelOlingo2.queryParams", queryParams);
    // read ODataFeed
    final ODataFeed manufacturers = requestBodyAndHeaders("direct://READFEED", null, headers);
    assertNotNull(manufacturers);
    final List<ODataEntry> manufacturersEntries = manufacturers.getEntries();
    assertFalse("Manufacturers empty entries", manufacturersEntries.isEmpty());
    LOG.info("Manufacturers feed has {} entries", manufacturersEntries.size());
    // read ODataEntry
    headers.clear();
    headers.put(Olingo2Constants.PROPERTY_PREFIX + "keyPredicate", "'1'");
    final ODataEntry manufacturer = requestBodyAndHeaders("direct://READENTRY", null, headers);
    assertNotNull(manufacturer);
    final Map<String, Object> properties = manufacturer.getProperties();
    assertEquals("Manufacturer Id", "1", properties.get(ID_PROPERTY));
    LOG.info("Manufacturer: {}", properties.toString());
}
Also used : ODataFeed(org.apache.olingo.odata2.api.ep.feed.ODataFeed) HashMap(java.util.HashMap) ServiceDocument(org.apache.olingo.odata2.api.servicedocument.ServiceDocument) ODataEntry(org.apache.olingo.odata2.api.ep.entry.ODataEntry) Test(org.junit.Test)

Example 4 with ODataEntry

use of org.apache.olingo.odata2.api.ep.entry.ODataEntry in project camel by apache.

the class Olingo2AppImpl method writeContent.

private <T> void writeContent(final Edm edm, HttpEntityEnclosingRequestBase httpEntityRequest, final UriInfoWithType uriInfo, final Object content, final Olingo2ResponseHandler<T> responseHandler) {
    try {
        // process resource by UriType
        final ODataResponse response = writeContent(edm, uriInfo, content);
        // copy all response headers
        for (String header : response.getHeaderNames()) {
            httpEntityRequest.setHeader(header, response.getHeader(header));
        }
        // get (http) entity which is for default Olingo2 implementation an InputStream
        if (response.getEntity() instanceof InputStream) {
            httpEntityRequest.setEntity(new InputStreamEntity((InputStream) response.getEntity()));
        /*
                // avoid sending it without a header field set
                if (!httpEntityRequest.containsHeader(HttpHeaders.CONTENT_TYPE)) {
                    httpEntityRequest.addHeader(HttpHeaders.CONTENT_TYPE, getContentType());
                }
*/
        }
        // execute HTTP request
        final Header requestContentTypeHeader = httpEntityRequest.getFirstHeader(HttpHeaders.CONTENT_TYPE);
        final ContentType requestContentType = requestContentTypeHeader != null ? ContentType.parse(requestContentTypeHeader.getValue()) : contentType;
        execute(httpEntityRequest, requestContentType, new AbstractFutureCallback<T>(responseHandler) {

            @SuppressWarnings("unchecked")
            @Override
            public void onCompleted(HttpResponse result) throws IOException, EntityProviderException, BatchException, ODataApplicationException {
                // if a entity is created (via POST request) the response body contains the new created entity
                HttpStatusCodes statusCode = HttpStatusCodes.fromStatusCode(result.getStatusLine().getStatusCode());
                // look for no content, or no response body!!!
                final boolean noEntity = result.getEntity() == null || result.getEntity().getContentLength() == 0;
                if (statusCode == HttpStatusCodes.NO_CONTENT || noEntity) {
                    responseHandler.onResponse((T) HttpStatusCodes.fromStatusCode(result.getStatusLine().getStatusCode()));
                } else {
                    switch(uriInfo.getUriType()) {
                        case URI9:
                            // $batch
                            final List<BatchSingleResponse> singleResponses = EntityProvider.parseBatchResponse(result.getEntity().getContent(), result.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue());
                            // parse batch response bodies
                            final List<Olingo2BatchResponse> responses = new ArrayList<Olingo2BatchResponse>();
                            Map<String, String> contentIdLocationMap = new HashMap<String, String>();
                            final List<Olingo2BatchRequest> batchRequests = (List<Olingo2BatchRequest>) content;
                            final Iterator<Olingo2BatchRequest> iterator = batchRequests.iterator();
                            for (BatchSingleResponse response : singleResponses) {
                                final Olingo2BatchRequest request = iterator.next();
                                if (request instanceof Olingo2BatchChangeRequest && ((Olingo2BatchChangeRequest) request).getContentId() != null) {
                                    contentIdLocationMap.put("$" + ((Olingo2BatchChangeRequest) request).getContentId(), response.getHeader(HttpHeaders.LOCATION));
                                }
                                try {
                                    responses.add(parseResponse(edm, contentIdLocationMap, request, response));
                                } catch (Exception e) {
                                    // report any parsing errors as error response
                                    responses.add(new Olingo2BatchResponse(Integer.parseInt(response.getStatusCode()), response.getStatusInfo(), response.getContentId(), response.getHeaders(), new ODataApplicationException("Error parsing response for " + request + ": " + e.getMessage(), Locale.ENGLISH, e)));
                                }
                            }
                            responseHandler.onResponse((T) responses);
                            break;
                        case URI4:
                        case URI5:
                            // simple property
                            // get the response content as Object for $value or Map<String, Object> otherwise
                            final List<EdmProperty> simplePropertyPath = uriInfo.getPropertyPath();
                            final EdmProperty simpleProperty = simplePropertyPath.get(simplePropertyPath.size() - 1);
                            if (uriInfo.isValue()) {
                                responseHandler.onResponse((T) EntityProvider.readPropertyValue(simpleProperty, result.getEntity().getContent()));
                            } else {
                                responseHandler.onResponse((T) EntityProvider.readProperty(getContentType(), simpleProperty, result.getEntity().getContent(), EntityProviderReadProperties.init().build()));
                            }
                            break;
                        case URI3:
                            // complex property
                            // get the response content as Map<String, Object>
                            final List<EdmProperty> complexPropertyPath = uriInfo.getPropertyPath();
                            final EdmProperty complexProperty = complexPropertyPath.get(complexPropertyPath.size() - 1);
                            responseHandler.onResponse((T) EntityProvider.readProperty(getContentType(), complexProperty, result.getEntity().getContent(), EntityProviderReadProperties.init().build()));
                            break;
                        case URI7A:
                            // $links with 0..1 cardinality property
                            // get the response content as String
                            final EdmEntitySet targetLinkEntitySet = uriInfo.getTargetEntitySet();
                            responseHandler.onResponse((T) EntityProvider.readLink(getContentType(), targetLinkEntitySet, result.getEntity().getContent()));
                            break;
                        case URI7B:
                            // $links with * cardinality property
                            // get the response content as java.util.List<String>
                            final EdmEntitySet targetLinksEntitySet = uriInfo.getTargetEntitySet();
                            responseHandler.onResponse((T) EntityProvider.readLinks(getContentType(), targetLinksEntitySet, result.getEntity().getContent()));
                            break;
                        case URI1:
                        case URI2:
                        case URI6A:
                        case URI6B:
                            // Entity
                            // get the response content as an ODataEntry object
                            responseHandler.onResponse((T) EntityProvider.readEntry(response.getContentHeader(), uriInfo.getTargetEntitySet(), result.getEntity().getContent(), EntityProviderReadProperties.init().build()));
                            break;
                        default:
                            throw new ODataApplicationException("Unsupported resource type " + uriInfo.getTargetType(), Locale.ENGLISH);
                    }
                }
            }
        });
    } catch (ODataException e) {
        responseHandler.onException(e);
    } catch (URISyntaxException e) {
        responseHandler.onException(e);
    } catch (UnsupportedEncodingException e) {
        responseHandler.onException(e);
    } catch (IOException e) {
        responseHandler.onException(e);
    }
}
Also used : ContentType(org.apache.http.entity.ContentType) HttpStatusCodes(org.apache.olingo.odata2.api.commons.HttpStatusCodes) Olingo2BatchRequest(org.apache.camel.component.olingo2.api.batch.Olingo2BatchRequest) URISyntaxException(java.net.URISyntaxException) BatchException(org.apache.olingo.odata2.api.batch.BatchException) Olingo2BatchResponse(org.apache.camel.component.olingo2.api.batch.Olingo2BatchResponse) Iterator(java.util.Iterator) Olingo2BatchChangeRequest(org.apache.camel.component.olingo2.api.batch.Olingo2BatchChangeRequest) List(java.util.List) ArrayList(java.util.ArrayList) EdmEntitySet(org.apache.olingo.odata2.api.edm.EdmEntitySet) EdmProperty(org.apache.olingo.odata2.api.edm.EdmProperty) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BatchSingleResponse(org.apache.olingo.odata2.api.client.batch.BatchSingleResponse) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ODataApplicationException(org.apache.olingo.odata2.api.exception.ODataApplicationException) URISyntaxException(java.net.URISyntaxException) ODataApplicationException(org.apache.olingo.odata2.api.exception.ODataApplicationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) EntityProviderException(org.apache.olingo.odata2.api.ep.EntityProviderException) EdmException(org.apache.olingo.odata2.api.edm.EdmException) ODataException(org.apache.olingo.odata2.api.exception.ODataException) IOException(java.io.IOException) BatchException(org.apache.olingo.odata2.api.batch.BatchException) InputStreamEntity(org.apache.http.entity.InputStreamEntity) EntityProviderException(org.apache.olingo.odata2.api.ep.EntityProviderException) ODataException(org.apache.olingo.odata2.api.exception.ODataException) Header(org.apache.http.Header) ODataResponse(org.apache.olingo.odata2.api.processor.ODataResponse) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with ODataEntry

use of org.apache.olingo.odata2.api.ep.entry.ODataEntry in project camel by apache.

the class Olingo2AppAPITest method prettyPrint.

private static String prettyPrint(ODataFeed dataFeed) {
    StringBuilder builder = new StringBuilder();
    builder.append("[\n");
    for (ODataEntry entry : dataFeed.getEntries()) {
        builder.append(prettyPrint(entry.getProperties(), 1)).append('\n');
    }
    builder.append("]\n");
    return builder.toString();
}
Also used : ODataEntry(org.apache.olingo.odata2.api.ep.entry.ODataEntry)

Aggregations

ODataEntry (org.apache.olingo.odata2.api.ep.entry.ODataEntry)9 HashMap (java.util.HashMap)8 Test (org.junit.Test)7 Map (java.util.Map)5 ArrayList (java.util.ArrayList)3 Olingo2BatchRequest (org.apache.camel.component.olingo2.api.batch.Olingo2BatchRequest)3 Olingo2BatchResponse (org.apache.camel.component.olingo2.api.batch.Olingo2BatchResponse)3 HttpStatusCodes (org.apache.olingo.odata2.api.commons.HttpStatusCodes)3 ODataFeed (org.apache.olingo.odata2.api.ep.feed.ODataFeed)3 InputStream (java.io.InputStream)2 List (java.util.List)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URISyntaxException (java.net.URISyntaxException)1 Calendar (java.util.Calendar)1 Iterator (java.util.Iterator)1 Olingo2BatchChangeRequest (org.apache.camel.component.olingo2.api.batch.Olingo2BatchChangeRequest)1 Header (org.apache.http.Header)1 HttpResponse (org.apache.http.HttpResponse)1