Search in sources :

Example 1 with EdmProperty

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

use of org.apache.olingo.odata2.api.edm.EdmProperty in project wildfly-camel by wildfly-extras.

the class MyODataSingleProcessor method getKeyValue.

private int getKeyValue(KeyPredicate key) throws ODataException {
    EdmProperty property = key.getProperty();
    EdmSimpleType type = (EdmSimpleType) property.getType();
    return type.valueOfString(key.getLiteral(), EdmLiteralKind.DEFAULT, property.getFacets(), Integer.class);
}
Also used : EdmSimpleType(org.apache.olingo.odata2.api.edm.EdmSimpleType) EdmProperty(org.apache.olingo.odata2.api.edm.EdmProperty)

Example 3 with EdmProperty

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

the class Olingo2AppImpl method readContent.

@SuppressWarnings("unchecked")
private <T> T readContent(UriInfoWithType uriInfo, InputStream content) throws EntityProviderException, ODataApplicationException {
    T response;
    switch(uriInfo.getUriType()) {
        case URI0:
            // service document
            response = (T) EntityProvider.readServiceDocument(content, SERVICE_DOCUMENT_CONTENT_TYPE.toString());
            break;
        case URI8:
            // $metadata
            response = (T) EntityProvider.readMetadata(content, false);
            break;
        case URI7A:
            // link
            response = (T) EntityProvider.readLink(getContentType(), uriInfo.getTargetEntitySet(), content);
            break;
        case URI7B:
            // links
            response = (T) EntityProvider.readLinks(getContentType(), uriInfo.getTargetEntitySet(), content);
            break;
        case URI3:
            // complex property
            final List<EdmProperty> complexPropertyPath = uriInfo.getPropertyPath();
            final EdmProperty complexProperty = complexPropertyPath.get(complexPropertyPath.size() - 1);
            response = (T) EntityProvider.readProperty(getContentType(), complexProperty, content, EntityProviderReadProperties.init().build());
            break;
        case URI4:
        case URI5:
            // simple property
            final List<EdmProperty> simplePropertyPath = uriInfo.getPropertyPath();
            final EdmProperty simpleProperty = simplePropertyPath.get(simplePropertyPath.size() - 1);
            if (uriInfo.isValue()) {
                response = (T) EntityProvider.readPropertyValue(simpleProperty, content);
            } else {
                response = (T) EntityProvider.readProperty(getContentType(), simpleProperty, content, EntityProviderReadProperties.init().build());
            }
            break;
        case URI15:
        case URI16:
        case URI50A:
        case URI50B:
            // $count
            final String stringCount = new String(EntityProvider.readBinary(content), Consts.UTF_8);
            response = (T) Long.valueOf(stringCount);
            break;
        case URI1:
        case URI6B:
            if (uriInfo.getCustomQueryOptions().containsKey("!deltatoken")) {
                // ODataDeltaFeed
                response = (T) EntityProvider.readDeltaFeed(getContentType(), uriInfo.getTargetEntitySet(), content, EntityProviderReadProperties.init().build());
            } else {
                // ODataFeed
                response = (T) EntityProvider.readFeed(getContentType(), uriInfo.getTargetEntitySet(), content, EntityProviderReadProperties.init().build());
            }
            break;
        case URI2:
        case URI6A:
            response = (T) EntityProvider.readEntry(getContentType(), uriInfo.getTargetEntitySet(), content, EntityProviderReadProperties.init().build());
            break;
        // Function Imports
        case URI10:
        case URI11:
        case URI12:
        case URI13:
        case URI14:
            response = (T) EntityProvider.readFunctionImport(getContentType(), uriInfo.getFunctionImport(), content, EntityProviderReadProperties.init().build());
            break;
        default:
            throw new ODataApplicationException("Unsupported resource type " + uriInfo.getTargetType(), Locale.ENGLISH);
    }
    return response;
}
Also used : EdmProperty(org.apache.olingo.odata2.api.edm.EdmProperty) ODataApplicationException(org.apache.olingo.odata2.api.exception.ODataApplicationException)

Example 4 with EdmProperty

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

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

EdmProperty (org.apache.olingo.odata2.api.edm.EdmProperty)5 EdmEntitySet (org.apache.olingo.odata2.api.edm.EdmEntitySet)3 ODataApplicationException (org.apache.olingo.odata2.api.exception.ODataApplicationException)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Olingo2BatchRequest (org.apache.camel.component.olingo2.api.batch.Olingo2BatchRequest)2 ODataResponse (org.apache.olingo.odata2.api.processor.ODataResponse)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Iterator (java.util.Iterator)1 Olingo2BatchChangeRequest (org.apache.camel.component.olingo2.api.batch.Olingo2BatchChangeRequest)1 Olingo2BatchResponse (org.apache.camel.component.olingo2.api.batch.Olingo2BatchResponse)1 Header (org.apache.http.Header)1 HttpResponse (org.apache.http.HttpResponse)1