Search in sources :

Example 1 with JsonDeserializer

use of org.apache.olingo.client.core.serialization.JsonDeserializer in project teiid by teiid.

the class ODataProcedureExecution method handleResponse.

private void handleResponse(final Procedure procedure, final String baseUri, final InputStream payload) throws TranslatorException, ODataDeserializerException {
    if (procedure.getResultSet() != null) {
        ODataType type = ODataType.valueOf(procedure.getResultSet().getProperty(ODataMetadataProcessor.ODATA_TYPE, false));
        this.response = new ODataResponse(payload, type, new DocumentNode()) {

            @Override
            public InputStream nextBatch(java.net.URI uri) throws TranslatorException {
                return executeSkipToken(uri, baseUri, new HttpStatusCode[] { HttpStatusCode.OK });
            }
        };
    } else if (getReturnParameter() != null) {
        // this is scalar result
        JsonDeserializer parser = new JsonDeserializer(false);
        Property property = parser.toProperty(payload).getPayload();
        if (property.isCollection()) {
            this.returnValue = property.asCollection();
        } else {
            this.returnValue = property.asPrimitive();
        }
    }
}
Also used : DocumentNode(org.teiid.translator.document.DocumentNode) InputStream(java.io.InputStream) ODataType(org.teiid.translator.odata4.ODataMetadataProcessor.ODataType) HttpStatusCode(org.apache.olingo.commons.api.http.HttpStatusCode) TranslatorException(org.teiid.translator.TranslatorException) JsonDeserializer(org.apache.olingo.client.core.serialization.JsonDeserializer) Property(org.apache.olingo.commons.api.data.Property)

Example 2 with JsonDeserializer

use of org.apache.olingo.client.core.serialization.JsonDeserializer in project teiid by teiid.

the class ODataResponse method parsePayload.

private Iterator<ODataDocument> parsePayload(InputStream payload) throws TranslatorException {
    try {
        JsonDeserializer parser = new JsonDeserializer(false);
        if (this.resultsType == ODataType.ENTITY) {
            Entity entity = parser.toEntity(payload).getPayload();
            ODataDocument document = ODataDocument.createDocument(entity);
            return Arrays.asList(document).iterator();
        } else if (this.resultsType == ODataType.ENTITY_COLLECTION) {
            EntityCollection entityCollection = parser.toEntitySet(payload).getPayload();
            this.nextUri = entityCollection.getNext();
            ArrayList<ODataDocument> documents = new ArrayList<ODataDocument>();
            for (Entity entity : entityCollection.getEntities()) {
                documents.add(ODataDocument.createDocument(entity));
            }
            return documents.iterator();
        } else {
            // complex
            Property property = parser.toProperty(payload).getPayload();
            if (property.isCollection()) {
                ArrayList<ODataDocument> documents = new ArrayList<ODataDocument>();
                for (Object obj : property.asCollection()) {
                    ComplexValue complexValue = (ComplexValue) obj;
                    documents.add(ODataDocument.createDocument(complexValue));
                }
                return documents.iterator();
            } else {
                ODataDocument document = ODataDocument.createDocument(property.asComplex());
                return Arrays.asList(document).iterator();
            }
        }
    } catch (ODataDeserializerException e) {
        throw new TranslatorException(e);
    }
}
Also used : Entity(org.apache.olingo.commons.api.data.Entity) ComplexValue(org.apache.olingo.commons.api.data.ComplexValue) EntityCollection(org.apache.olingo.commons.api.data.EntityCollection) ArrayList(java.util.ArrayList) TranslatorException(org.teiid.translator.TranslatorException) ODataDeserializerException(org.apache.olingo.client.api.serialization.ODataDeserializerException) JsonDeserializer(org.apache.olingo.client.core.serialization.JsonDeserializer) Property(org.apache.olingo.commons.api.data.Property)

Example 3 with JsonDeserializer

use of org.apache.olingo.client.core.serialization.JsonDeserializer in project teiid by teiid.

the class BaseQueryExecution method buildError.

protected TranslatorException buildError(BinaryWSProcedureExecution execution) {
    // do some error handling
    try {
        Blob blob = (Blob) execution.getOutputParameterValues().get(0);
        if (blob != null) {
            boolean json = false;
            // $NON-NLS-1$
            String contentTypeString = getHeader(execution, "Content-Type");
            if (contentTypeString != null) {
                ContentType contentType = ContentType.parse(contentTypeString);
                if (contentType != null && ContentType.APPLICATION_JSON.isCompatible(contentType)) {
                    json = true;
                }
            }
            ODataDeserializer parser = null;
            if (json) {
                parser = new JsonDeserializer(false);
            } else {
                // TODO: it may not be atom, it could just be xml/html
                parser = new AtomDeserializer();
            }
            ODataError error = parser.toError(blob.getBinaryStream());
            return new TranslatorException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID17013, execution.getResponseCode(), error.getCode(), error.getMessage(), error.getInnerError()));
        }
        return new TranslatorException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID17031, execution.getResponseCode()));
    } catch (Throwable t) {
        return new TranslatorException(t);
    }
}
Also used : Blob(java.sql.Blob) ContentType(org.apache.olingo.commons.api.format.ContentType) ODataDeserializer(org.apache.olingo.client.api.serialization.ODataDeserializer) TranslatorException(org.teiid.translator.TranslatorException) AtomDeserializer(org.apache.olingo.client.core.serialization.AtomDeserializer) ODataError(org.apache.olingo.commons.api.ex.ODataError) JsonDeserializer(org.apache.olingo.client.core.serialization.JsonDeserializer)

Example 4 with JsonDeserializer

use of org.apache.olingo.client.core.serialization.JsonDeserializer in project teiid by teiid.

the class ODataUpdateExecution method handleInsert.

private void handleInsert(ODataUpdateQuery odataQuery) throws TranslatorException {
    try {
        Map<String, List<String>> headers = getDefaultUpdateHeaders();
        // $NON-NLS-1$
        headers.put("Prefer", Arrays.asList("return=representation"));
        String uri = odataQuery.buildInsertURL("");
        InputStream response = null;
        BinaryWSProcedureExecution execution = invokeHTTP(odataQuery.getInsertMethod(), uri, odataQuery.getPayload(null), headers);
        // 201 - the created entity returned
        if (HttpStatusCode.CREATED.getStatusCode() == execution.getResponseCode()) {
            Blob blob = (Blob) execution.getOutputParameterValues().get(0);
            response = blob.getBinaryStream();
        } else if (HttpStatusCode.NO_CONTENT.getStatusCode() == execution.getResponseCode()) {
            // get Location header and get content
            String entityUri = (String) execution.getResponseHeader("Location");
            if (entityUri != null) {
                // in the cases of property update there will be no Location header
                response = executeQuery("GET", entityUri, null, null, new HttpStatusCode[] { HttpStatusCode.OK });
            }
        } else {
            throw buildError(execution);
        }
        if (response != null) {
            JsonDeserializer serializer = new JsonDeserializer(false);
            this.createdEntity = serializer.toEntity(response).getPayload();
        }
    } catch (ODataDeserializerException e) {
        throw new TranslatorException(e);
    } catch (SQLException e) {
        throw new TranslatorException(e);
    }
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) BinaryWSProcedureExecution(org.teiid.translator.ws.BinaryWSProcedureExecution) ArrayList(java.util.ArrayList) List(java.util.List) TranslatorException(org.teiid.translator.TranslatorException) ODataDeserializerException(org.apache.olingo.client.api.serialization.ODataDeserializerException) JsonDeserializer(org.apache.olingo.client.core.serialization.JsonDeserializer)

Aggregations

JsonDeserializer (org.apache.olingo.client.core.serialization.JsonDeserializer)4 TranslatorException (org.teiid.translator.TranslatorException)4 InputStream (java.io.InputStream)2 Blob (java.sql.Blob)2 ArrayList (java.util.ArrayList)2 ODataDeserializerException (org.apache.olingo.client.api.serialization.ODataDeserializerException)2 Property (org.apache.olingo.commons.api.data.Property)2 SQLException (java.sql.SQLException)1 List (java.util.List)1 ODataDeserializer (org.apache.olingo.client.api.serialization.ODataDeserializer)1 AtomDeserializer (org.apache.olingo.client.core.serialization.AtomDeserializer)1 ComplexValue (org.apache.olingo.commons.api.data.ComplexValue)1 Entity (org.apache.olingo.commons.api.data.Entity)1 EntityCollection (org.apache.olingo.commons.api.data.EntityCollection)1 ODataError (org.apache.olingo.commons.api.ex.ODataError)1 ContentType (org.apache.olingo.commons.api.format.ContentType)1 HttpStatusCode (org.apache.olingo.commons.api.http.HttpStatusCode)1 DocumentNode (org.teiid.translator.document.DocumentNode)1 ODataType (org.teiid.translator.odata4.ODataMetadataProcessor.ODataType)1 BinaryWSProcedureExecution (org.teiid.translator.ws.BinaryWSProcedureExecution)1