Search in sources :

Example 1 with BatchChangeSetPart

use of org.apache.olingo.odata2.api.client.batch.BatchChangeSetPart in project camel by apache.

the class Olingo2AppImpl method addChangeSetParts.

private void addChangeSetParts(ArrayList<BatchPart> parts, ArrayList<BatchChangeSetPart> changeSetParts) {
    final BatchChangeSet changeSet = BatchChangeSet.newBuilder().build();
    for (BatchChangeSetPart changeSetPart : changeSetParts) {
        changeSet.add(changeSetPart);
    }
    parts.add(changeSet);
}
Also used : BatchChangeSet(org.apache.olingo.odata2.api.client.batch.BatchChangeSet) BatchChangeSetPart(org.apache.olingo.odata2.api.client.batch.BatchChangeSetPart)

Example 2 with BatchChangeSetPart

use of org.apache.olingo.odata2.api.client.batch.BatchChangeSetPart in project camel by apache.

the class Olingo2AppImpl method parseBatchRequest.

private ODataResponse parseBatchRequest(final Edm edm, final List<Olingo2BatchRequest> batchParts) throws IOException, EntityProviderException, ODataApplicationException, EdmException, URISyntaxException {
    // create Batch request from parts
    final ArrayList<BatchPart> parts = new ArrayList<BatchPart>();
    final ArrayList<BatchChangeSetPart> changeSetParts = new ArrayList<BatchChangeSetPart>();
    final Map<String, String> contentIdMap = new HashMap<String, String>();
    for (Olingo2BatchRequest batchPart : batchParts) {
        if (batchPart instanceof Olingo2BatchQueryRequest) {
            // need to add change set parts collected so far??
            if (!changeSetParts.isEmpty()) {
                addChangeSetParts(parts, changeSetParts);
                changeSetParts.clear();
                contentIdMap.clear();
            }
            // add to request parts
            final UriInfoWithType uriInfo = parseUri(edm, batchPart.getResourcePath(), null);
            parts.add(createBatchQueryPart(uriInfo, (Olingo2BatchQueryRequest) batchPart));
        } else {
            // add to change set parts
            final BatchChangeSetPart changeSetPart = createBatchChangeSetPart(edm, contentIdMap, (Olingo2BatchChangeRequest) batchPart);
            changeSetParts.add(changeSetPart);
        }
    }
    // add any remaining change set parts
    if (!changeSetParts.isEmpty()) {
        addChangeSetParts(parts, changeSetParts);
    }
    final String boundary = BOUNDARY_PREFIX + UUID.randomUUID();
    InputStream batchRequest = EntityProvider.writeBatchRequest(parts, boundary);
    // two blank lines are already added. No need to add extra blank lines
    final String contentHeader = BATCH_CONTENT_TYPE + BOUNDARY_PARAMETER + boundary;
    return ODataResponse.entity(batchRequest).contentHeader(contentHeader).build();
}
Also used : BatchChangeSetPart(org.apache.olingo.odata2.api.client.batch.BatchChangeSetPart) BatchPart(org.apache.olingo.odata2.api.client.batch.BatchPart) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Olingo2BatchRequest(org.apache.camel.component.olingo2.api.batch.Olingo2BatchRequest) Olingo2BatchQueryRequest(org.apache.camel.component.olingo2.api.batch.Olingo2BatchQueryRequest)

Example 3 with BatchChangeSetPart

use of org.apache.olingo.odata2.api.client.batch.BatchChangeSetPart in project camel by apache.

the class Olingo2AppImpl method createBatchChangeSetPart.

private BatchChangeSetPart createBatchChangeSetPart(Edm edm, Map<String, String> contentIdMap, Olingo2BatchChangeRequest batchRequest) throws EdmException, URISyntaxException, EntityProviderException, IOException, ODataApplicationException {
    // build body string
    String resourcePath = batchRequest.getResourcePath();
    // is it a referenced entity?
    if (resourcePath.startsWith("$")) {
        resourcePath = replaceContentId(edm, resourcePath, contentIdMap);
    }
    final UriInfoWithType uriInfo = parseUri(edm, resourcePath, null);
    // serialize data into ODataResponse object, if set in request and this is not a DELETE request
    final Map<String, String> headers = new HashMap<String, String>();
    byte[] body = null;
    if (batchRequest.getBody() != null && !Operation.DELETE.equals(batchRequest.getOperation())) {
        final ODataResponse response = writeContent(edm, uriInfo, batchRequest.getBody());
        // copy response headers
        for (String header : response.getHeaderNames()) {
            headers.put(header, response.getHeader(header));
        }
        // get (http) entity which is for default Olingo2 implementation an InputStream
        body = response.getEntity() instanceof InputStream ? EntityProvider.readBinary((InputStream) response.getEntity()) : null;
        if (body != null) {
            headers.put(HttpHeaders.CONTENT_LENGTH, String.valueOf(body.length));
        }
    }
    // Olingo is sensitive to batch part charset case!!
    headers.put(HttpHeaders.ACCEPT, getResourceContentType(uriInfo).toString().toLowerCase());
    if (!headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
        headers.put(HttpHeaders.CONTENT_TYPE, getContentType());
    }
    // add request headers
    headers.putAll(batchRequest.getHeaders());
    final String contentId = batchRequest.getContentId();
    if (contentId != null) {
        contentIdMap.put("$" + contentId, resourcePath);
    }
    return BatchChangeSetPart.uri(createBatchUri(batchRequest)).method(batchRequest.getOperation().getHttpMethod()).contentId(contentId).headers(headers).body(body == null ? null : new String(body, Consts.UTF_8)).build();
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ODataResponse(org.apache.olingo.odata2.api.processor.ODataResponse)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 BatchChangeSetPart (org.apache.olingo.odata2.api.client.batch.BatchChangeSetPart)2 ArrayList (java.util.ArrayList)1 Olingo2BatchQueryRequest (org.apache.camel.component.olingo2.api.batch.Olingo2BatchQueryRequest)1 Olingo2BatchRequest (org.apache.camel.component.olingo2.api.batch.Olingo2BatchRequest)1 BatchChangeSet (org.apache.olingo.odata2.api.client.batch.BatchChangeSet)1 BatchPart (org.apache.olingo.odata2.api.client.batch.BatchPart)1 ODataResponse (org.apache.olingo.odata2.api.processor.ODataResponse)1