Search in sources :

Example 1 with EdmException

use of org.apache.olingo.odata2.api.edm.EdmException 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 2 with EdmException

use of org.apache.olingo.odata2.api.edm.EdmException 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 EdmException

use of org.apache.olingo.odata2.api.edm.EdmException 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)

Example 4 with EdmException

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

the class Olingo2AppImpl method writeContent.

private ODataResponse writeContent(Edm edm, UriInfoWithType uriInfo, Object content) throws ODataApplicationException, EdmException, EntityProviderException, URISyntaxException, IOException {
    String responseContentType = getContentType();
    ODataResponse response;
    switch(uriInfo.getUriType()) {
        case URI4:
        case URI5:
            // simple property
            final List<EdmProperty> simplePropertyPath = uriInfo.getPropertyPath();
            final EdmProperty simpleProperty = simplePropertyPath.get(simplePropertyPath.size() - 1);
            responseContentType = simpleProperty.getMimeType();
            if (uriInfo.isValue()) {
                response = EntityProvider.writePropertyValue(simpleProperty, content);
                responseContentType = TEXT_PLAIN_WITH_CS_UTF_8.toString();
            } else {
                response = EntityProvider.writeProperty(getContentType(), simpleProperty, content);
            }
            break;
        case URI3:
            // complex property
            final List<EdmProperty> complexPropertyPath = uriInfo.getPropertyPath();
            final EdmProperty complexProperty = complexPropertyPath.get(complexPropertyPath.size() - 1);
            response = EntityProvider.writeProperty(responseContentType, complexProperty, content);
            break;
        case URI7A:
            // $links with 0..1 cardinality property
            final EdmEntitySet targetLinkEntitySet = uriInfo.getTargetEntitySet();
            EntityProviderWriteProperties linkProperties = EntityProviderWriteProperties.serviceRoot(new URI(serviceUri + SEPARATOR)).build();
            @SuppressWarnings("unchecked") final Map<String, Object> linkMap = (Map<String, Object>) content;
            response = EntityProvider.writeLink(responseContentType, targetLinkEntitySet, linkMap, linkProperties);
            break;
        case URI7B:
            // $links with * cardinality property
            final EdmEntitySet targetLinksEntitySet = uriInfo.getTargetEntitySet();
            EntityProviderWriteProperties linksProperties = EntityProviderWriteProperties.serviceRoot(new URI(serviceUri + SEPARATOR)).build();
            @SuppressWarnings("unchecked") final List<Map<String, Object>> linksMap = (List<Map<String, Object>>) content;
            response = EntityProvider.writeLinks(responseContentType, targetLinksEntitySet, linksMap, linksProperties);
            break;
        case URI1:
        case URI2:
        case URI6A:
        case URI6B:
            // Entity
            final EdmEntitySet targetEntitySet = uriInfo.getTargetEntitySet();
            EntityProviderWriteProperties properties = EntityProviderWriteProperties.serviceRoot(new URI(serviceUri + SEPARATOR)).build();
            @SuppressWarnings("unchecked") final Map<String, Object> objectMap = (Map<String, Object>) content;
            response = EntityProvider.writeEntry(responseContentType, targetEntitySet, objectMap, properties);
            break;
        case URI9:
            // $batch
            @SuppressWarnings("unchecked") final List<Olingo2BatchRequest> batchParts = (List<Olingo2BatchRequest>) content;
            response = parseBatchRequest(edm, batchParts);
            break;
        default:
            // notify exception and return!!!
            throw new ODataApplicationException("Unsupported resource type " + uriInfo.getTargetType(), Locale.ENGLISH);
    }
    return response.getContentHeader() != null ? response : ODataResponse.fromResponse(response).contentHeader(responseContentType).build();
}
Also used : Olingo2BatchRequest(org.apache.camel.component.olingo2.api.batch.Olingo2BatchRequest) EntityProviderWriteProperties(org.apache.olingo.odata2.api.ep.EntityProviderWriteProperties) URI(java.net.URI) ODataApplicationException(org.apache.olingo.odata2.api.exception.ODataApplicationException) ODataResponse(org.apache.olingo.odata2.api.processor.ODataResponse) List(java.util.List) ArrayList(java.util.ArrayList) EdmEntitySet(org.apache.olingo.odata2.api.edm.EdmEntitySet) EdmProperty(org.apache.olingo.odata2.api.edm.EdmProperty) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

HashMap (java.util.HashMap)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 Olingo2BatchRequest (org.apache.camel.component.olingo2.api.batch.Olingo2BatchRequest)2 EdmEntitySet (org.apache.olingo.odata2.api.edm.EdmEntitySet)2 EdmProperty (org.apache.olingo.odata2.api.edm.EdmProperty)2 ODataResponse (org.apache.olingo.odata2.api.processor.ODataResponse)2 URI (java.net.URI)1 List (java.util.List)1 Map (java.util.Map)1 Olingo2BatchQueryRequest (org.apache.camel.component.olingo2.api.batch.Olingo2BatchQueryRequest)1 BatchChangeSetPart (org.apache.olingo.odata2.api.client.batch.BatchChangeSetPart)1 BatchPart (org.apache.olingo.odata2.api.client.batch.BatchPart)1 EdmEntityContainer (org.apache.olingo.odata2.api.edm.EdmEntityContainer)1 EntityProviderWriteProperties (org.apache.olingo.odata2.api.ep.EntityProviderWriteProperties)1 ODataApplicationException (org.apache.olingo.odata2.api.exception.ODataApplicationException)1