Search in sources :

Example 1 with Olingo2BatchQueryRequest

use of org.apache.camel.component.olingo2.api.batch.Olingo2BatchQueryRequest in project camel by apache.

the class Olingo2AppImpl method parseResponse.

private Olingo2BatchResponse parseResponse(Edm edm, Map<String, String> contentIdLocationMap, Olingo2BatchRequest request, BatchSingleResponse response) throws EntityProviderException, ODataApplicationException {
    // validate HTTP status
    final int statusCode = Integer.parseInt(response.getStatusCode());
    final String statusInfo = response.getStatusInfo();
    final BasicHttpResponse httpResponse = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, statusCode, statusInfo));
    final Map<String, String> headers = response.getHeaders();
    for (Map.Entry<String, String> entry : headers.entrySet()) {
        httpResponse.setHeader(entry.getKey(), entry.getValue());
    }
    ByteArrayInputStream content = null;
    try {
        if (response.getBody() != null) {
            final ContentType partContentType = receiveWithCharsetParameter(ContentType.parse(headers.get(HttpHeaders.CONTENT_TYPE)), Consts.UTF_8);
            final String charset = partContentType.getCharset().toString();
            final String body = response.getBody();
            content = body != null ? new ByteArrayInputStream(body.getBytes(charset)) : null;
            httpResponse.setEntity(new StringEntity(body, charset));
        }
        AbstractFutureCallback.checkStatus(httpResponse);
    } catch (ODataApplicationException e) {
        return new Olingo2BatchResponse(statusCode, statusInfo, response.getContentId(), response.getHeaders(), e);
    } catch (UnsupportedEncodingException e) {
        return new Olingo2BatchResponse(statusCode, statusInfo, response.getContentId(), response.getHeaders(), e);
    }
    // resolve resource path and query params and parse batch part uri
    final String resourcePath = request.getResourcePath();
    final String resolvedResourcePath;
    if (resourcePath.startsWith("$") && !(METADATA.equals(resourcePath) || BATCH.equals(resourcePath))) {
        resolvedResourcePath = findLocation(resourcePath, contentIdLocationMap);
    } else {
        final String resourceLocation = response.getHeader(HttpHeaders.LOCATION);
        resolvedResourcePath = resourceLocation != null ? resourceLocation.substring(serviceUri.length()) : resourcePath;
    }
    final Map<String, String> resolvedQueryParams = request instanceof Olingo2BatchQueryRequest ? ((Olingo2BatchQueryRequest) request).getQueryParams() : null;
    final UriInfoWithType uriInfo = parseUri(edm, resolvedResourcePath, resolvedQueryParams);
    // resolve response content
    final Object resolvedContent = content != null ? readContent(uriInfo, content) : null;
    return new Olingo2BatchResponse(statusCode, statusInfo, response.getContentId(), response.getHeaders(), resolvedContent);
}
Also used : ContentType(org.apache.http.entity.ContentType) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ODataApplicationException(org.apache.olingo.odata2.api.exception.ODataApplicationException) BasicStatusLine(org.apache.http.message.BasicStatusLine) Olingo2BatchQueryRequest(org.apache.camel.component.olingo2.api.batch.Olingo2BatchQueryRequest) StringEntity(org.apache.http.entity.StringEntity) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) Olingo2BatchResponse(org.apache.camel.component.olingo2.api.batch.Olingo2BatchResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with Olingo2BatchQueryRequest

use of org.apache.camel.component.olingo2.api.batch.Olingo2BatchQueryRequest 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 Olingo2BatchQueryRequest

use of org.apache.camel.component.olingo2.api.batch.Olingo2BatchQueryRequest in project camel by apache.

the class Olingo2AppImpl method createBatchUri.

private String createBatchUri(Olingo2BatchRequest part) {
    String result;
    if (part instanceof Olingo2BatchQueryRequest) {
        final Olingo2BatchQueryRequest queryPart = (Olingo2BatchQueryRequest) part;
        result = createUri(queryPart.getResourcePath(), queryPart.getQueryParams());
    } else {
        result = createUri(part.getResourcePath());
    }
    // strip base URI
    return result.substring(serviceUri.length() + 1);
}
Also used : Olingo2BatchQueryRequest(org.apache.camel.component.olingo2.api.batch.Olingo2BatchQueryRequest)

Aggregations

Olingo2BatchQueryRequest (org.apache.camel.component.olingo2.api.batch.Olingo2BatchQueryRequest)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HashMap (java.util.HashMap)2 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Olingo2BatchRequest (org.apache.camel.component.olingo2.api.batch.Olingo2BatchRequest)1 Olingo2BatchResponse (org.apache.camel.component.olingo2.api.batch.Olingo2BatchResponse)1 ContentType (org.apache.http.entity.ContentType)1 StringEntity (org.apache.http.entity.StringEntity)1 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)1 BasicStatusLine (org.apache.http.message.BasicStatusLine)1 BatchChangeSetPart (org.apache.olingo.odata2.api.client.batch.BatchChangeSetPart)1 BatchPart (org.apache.olingo.odata2.api.client.batch.BatchPart)1 ODataApplicationException (org.apache.olingo.odata2.api.exception.ODataApplicationException)1