Search in sources :

Example 1 with HttpStatusCode

use of org.apache.olingo.commons.api.http.HttpStatusCode in project wildfly-camel by wildfly-extras.

the class Olingo4IntegrationTest method testCreateUpdateDelete.

@Test
public void testCreateUpdateDelete() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        public void configure() {
            from("direct://create-entity").to("olingo4://create/People");
            from("direct://update-entity").to("olingo4://update/People('lewisblack')");
            from("direct://delete-entity").to("olingo4://delete/People('lewisblack')");
            from("direct://read-deleted-entity").to("olingo4://delete/People('lewisblack')");
            from("direct://batch").to("olingo4://batch");
        }
    });
    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        final ClientEntity clientEntity = createEntity();
        ClientEntity entity = template.requestBody("direct://create-entity", clientEntity, ClientEntity.class);
        Assert.assertNotNull(entity);
        Assert.assertEquals("Lewis", entity.getProperty("FirstName").getValue().toString());
        Assert.assertEquals("", entity.getProperty("MiddleName").getValue().toString());
        // update
        clientEntity.getProperties().add(objFactory.newPrimitiveProperty("MiddleName", objFactory.newPrimitiveValueBuilder().buildString("Lewis")));
        HttpStatusCode status = template.requestBody("direct://update-entity", clientEntity, HttpStatusCode.class);
        Assert.assertNotNull("Update status", status);
        Assert.assertEquals("Update status", HttpStatusCode.NO_CONTENT.getStatusCode(), status.getStatusCode());
        LOG.info("Update entity status: {}", status);
        // delete
        status = template.requestBody("direct://delete-entity", null, HttpStatusCode.class);
        Assert.assertNotNull("Delete status", status);
        Assert.assertEquals("Delete status", HttpStatusCode.NO_CONTENT.getStatusCode(), status.getStatusCode());
        LOG.info("Delete status: {}", status);
        // check for delete
        try {
            template.requestBody("direct://read-deleted-entity", null, HttpStatusCode.class);
        } catch (CamelExecutionException e) {
            Assert.assertEquals("Resource Not Found [HTTP/1.1 404 Not Found]", e.getCause().getMessage());
        }
    } finally {
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) CamelExecutionException(org.apache.camel.CamelExecutionException) ProducerTemplate(org.apache.camel.ProducerTemplate) ClientEntity(org.apache.olingo.client.api.domain.ClientEntity) RouteBuilder(org.apache.camel.builder.RouteBuilder) HttpStatusCode(org.apache.olingo.commons.api.http.HttpStatusCode) Test(org.junit.Test)

Example 2 with HttpStatusCode

use of org.apache.olingo.commons.api.http.HttpStatusCode in project teiid by teiid.

the class BaseQueryExecution method executeQuery.

protected InputStream executeQuery(String method, String uri, String payload, String eTag, HttpStatusCode[] expectedStatus) throws TranslatorException {
    Map<String, List<String>> headers = getDefaultHeaders();
    if (eTag != null) {
        // $NON-NLS-1$
        headers.put("If-Match", Arrays.asList(eTag));
    }
    if (payload != null) {
        headers.put("Content-Type", // $NON-NLS-1$
        Arrays.asList(ContentType.APPLICATION_JSON.toContentTypeString()));
    }
    BinaryWSProcedureExecution execution;
    try {
        execution = invokeHTTP(method, uri, payload, headers);
        for (HttpStatusCode status : expectedStatus) {
            if (status.getStatusCode() == execution.getResponseCode()) {
                if (execution.getResponseCode() != HttpStatusCode.NO_CONTENT.getStatusCode() && execution.getResponseCode() != HttpStatusCode.NOT_FOUND.getStatusCode()) {
                    Blob blob = (Blob) execution.getOutputParameterValues().get(0);
                    return blob.getBinaryStream();
                }
                // this is success with no-data
                return null;
            }
        }
    } catch (SQLException e) {
        throw new TranslatorException(e);
    }
    // throw an error
    throw buildError(execution);
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) BinaryWSProcedureExecution(org.teiid.translator.ws.BinaryWSProcedureExecution) HttpStatusCode(org.apache.olingo.commons.api.http.HttpStatusCode) ArrayList(java.util.ArrayList) List(java.util.List) TranslatorException(org.teiid.translator.TranslatorException)

Example 3 with HttpStatusCode

use of org.apache.olingo.commons.api.http.HttpStatusCode 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 4 with HttpStatusCode

use of org.apache.olingo.commons.api.http.HttpStatusCode in project teiid by teiid.

the class ODataProcedureExecution method execute.

@Override
public void execute() throws TranslatorException {
    try {
        String parameters = getQueryParameters(this.command);
        InputStream response = null;
        Procedure procedure = this.command.getMetadataObject();
        if (isFunction(procedure)) {
            String URI = buildFunctionURL(this.command, parameters);
            response = executeQuery("GET", URI, null, null, new HttpStatusCode[] { HttpStatusCode.OK });
            handleResponse(procedure, URI, response);
        } else {
            String URI = this.command.getProcedureName();
            response = executeQuery("POST", URI, parameters, null, new HttpStatusCode[] { HttpStatusCode.OK });
            handleResponse(procedure, URI, response);
        }
    } catch (ODataDeserializerException e) {
        throw new TranslatorException(e);
    } catch (EdmPrimitiveTypeException e) {
        throw new TranslatorException(e);
    }
}
Also used : InputStream(java.io.InputStream) HttpStatusCode(org.apache.olingo.commons.api.http.HttpStatusCode) Procedure(org.teiid.metadata.Procedure) TranslatorException(org.teiid.translator.TranslatorException) ODataDeserializerException(org.apache.olingo.client.api.serialization.ODataDeserializerException) EdmPrimitiveTypeException(org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException)

Example 5 with HttpStatusCode

use of org.apache.olingo.commons.api.http.HttpStatusCode in project teiid by teiid.

the class ODataQueryExecution method execute.

@Override
public void execute() throws TranslatorException {
    final String URI = this.visitor.buildURL("");
    if (isCount) {
        Map<String, List<String>> headers = new TreeMap<String, List<String>>();
        // $NON-NLS-1$ //$NON-NLS-2$
        headers.put("Accept", Arrays.asList("text/plain"));
        // $NON-NLS-1$
        BinaryWSProcedureExecution execution = invokeHTTP("GET", URI, null, headers);
        if (execution.getResponseCode() != HttpStatusCode.OK.getStatusCode()) {
            throw buildError(execution);
        }
        Blob blob = (Blob) execution.getOutputParameterValues().get(0);
        try {
            this.countResponse = Integer.parseInt(ObjectConverterUtil.convertToString(blob.getBinaryStream()));
        } catch (IOException e) {
            throw new TranslatorException(e);
        } catch (SQLException e) {
            throw new TranslatorException(e);
        }
    } else {
        InputStream payload = executeQuery(// //$NON-NLS-1$
        "GET", // //$NON-NLS-1$
        URI, // //$NON-NLS-1$
        null, // //$NON-NLS-1$
        null, new HttpStatusCode[] { HttpStatusCode.OK, HttpStatusCode.NO_CONTENT, HttpStatusCode.NOT_FOUND });
        this.response = new ODataResponse(payload, ODataType.ENTITY_COLLECTION, this.visitor.getODataQuery().getRootDocument()) {

            @Override
            public InputStream nextBatch(java.net.URI uri) throws TranslatorException {
                return executeSkipToken(uri, URI.toString(), new HttpStatusCode[] { HttpStatusCode.OK, HttpStatusCode.NO_CONTENT, HttpStatusCode.NOT_FOUND });
            }
        };
    }
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) IOException(java.io.IOException) TreeMap(java.util.TreeMap) BinaryWSProcedureExecution(org.teiid.translator.ws.BinaryWSProcedureExecution) HttpStatusCode(org.apache.olingo.commons.api.http.HttpStatusCode) List(java.util.List) TranslatorException(org.teiid.translator.TranslatorException)

Aggregations

HttpStatusCode (org.apache.olingo.commons.api.http.HttpStatusCode)5 TranslatorException (org.teiid.translator.TranslatorException)4 InputStream (java.io.InputStream)3 Blob (java.sql.Blob)2 SQLException (java.sql.SQLException)2 List (java.util.List)2 BinaryWSProcedureExecution (org.teiid.translator.ws.BinaryWSProcedureExecution)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 CamelContext (org.apache.camel.CamelContext)1 CamelExecutionException (org.apache.camel.CamelExecutionException)1 ProducerTemplate (org.apache.camel.ProducerTemplate)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 ClientEntity (org.apache.olingo.client.api.domain.ClientEntity)1 ODataDeserializerException (org.apache.olingo.client.api.serialization.ODataDeserializerException)1 JsonDeserializer (org.apache.olingo.client.core.serialization.JsonDeserializer)1 Property (org.apache.olingo.commons.api.data.Property)1 EdmPrimitiveTypeException (org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException)1