Search in sources :

Example 11 with BinaryWSProcedureExecution

use of org.teiid.translator.ws.BinaryWSProcedureExecution in project teiid by teiid.

the class BaseQueryExecution method executeDirect.

protected BinaryWSProcedureExecution executeDirect(String method, String uri, String payload, Map<String, List<String>> headers) throws TranslatorException {
    if (LogManager.isMessageToBeRecorded(LogConstants.CTX_ODATA, MessageLevel.DETAIL)) {
        try {
            // $NON-NLS-1$ //$NON-NLS-2$
            LogManager.logDetail(LogConstants.CTX_ODATA, "Source-URL=", URLDecoder.decode(uri, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
        }
    }
    List<Argument> parameters = new ArrayList<Argument>();
    parameters.add(new Argument(Direction.IN, new Literal(method, TypeFacility.RUNTIME_TYPES.STRING), null));
    parameters.add(new Argument(Direction.IN, new Literal(payload, TypeFacility.RUNTIME_TYPES.STRING), null));
    parameters.add(new Argument(Direction.IN, new Literal(uri, TypeFacility.RUNTIME_TYPES.STRING), null));
    parameters.add(new Argument(Direction.IN, new Literal(true, TypeFacility.RUNTIME_TYPES.BOOLEAN), null));
    // the engine currently always associates out params at resolve time even if the values are not directly read by the call
    parameters.add(new Argument(Direction.OUT, TypeFacility.RUNTIME_TYPES.STRING, null));
    Call call = this.translator.getLanguageFactory().createCall(ODataExecutionFactory.INVOKE_HTTP, parameters, null);
    BinaryWSProcedureExecution execution = new BinaryWSProcedureExecution(call, this.metadata, this.executionContext, null, this.connection);
    execution.setUseResponseContext(true);
    execution.setCustomHeaders(headers);
    execution.execute();
    return execution;
}
Also used : Call(org.teiid.language.Call) Argument(org.teiid.language.Argument) Literal(org.teiid.language.Literal) BinaryWSProcedureExecution(org.teiid.translator.ws.BinaryWSProcedureExecution) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 12 with BinaryWSProcedureExecution

use of org.teiid.translator.ws.BinaryWSProcedureExecution in project teiid by teiid.

the class BaseQueryExecution method executeWithComplexReturn.

protected ODataEntitiesResponse executeWithComplexReturn(String method, String uri, String payload, String complexTypeName, EdmDataServices edsMetadata, String eTag, Status... 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) {
        // $NON-NLS-1$ //$NON-NLS-2$
        headers.put("Content-Type", Arrays.asList("application/atom+xml"));
    }
    BinaryWSProcedureExecution execution = executeDirect(method, uri, payload, headers);
    for (Status status : expectedStatus) {
        if (status.getStatusCode() == execution.getResponseCode()) {
            if (execution.getResponseCode() != Status.NO_CONTENT.getStatusCode()) {
                Blob blob = (Blob) execution.getOutputParameterValues().get(0);
                // ODataVersion version = getDataServiceVersion((String)execution.getResponseHeader(ODataConstants.Headers.DATA_SERVICE_VERSION));
                EdmComplexType complexType = edsMetadata.findEdmComplexType(complexTypeName);
                if (complexType == null) {
                    throw new RuntimeException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID17016, complexType));
                }
                try {
                    return parserComplex(StaxUtil.newXMLEventReader(new InputStreamReader(blob.getBinaryStream())), complexType, edsMetadata);
                } catch (SQLException e) {
                    throw new TranslatorException(ODataPlugin.Event.TEIID17010, e, e.getMessage());
                }
            }
            // this is success with no-data
            return new ODataEntitiesResponse();
        }
    }
    // throw an error
    return new ODataEntitiesResponse(buildError(execution));
}
Also used : Status(javax.ws.rs.core.Response.Status) Blob(java.sql.Blob) InputStreamReader(java.io.InputStreamReader) SQLException(java.sql.SQLException) BinaryWSProcedureExecution(org.teiid.translator.ws.BinaryWSProcedureExecution) ArrayList(java.util.ArrayList) List(java.util.List) TranslatorException(org.teiid.translator.TranslatorException) EdmComplexType(org.odata4j.edm.EdmComplexType)

Example 13 with BinaryWSProcedureExecution

use of org.teiid.translator.ws.BinaryWSProcedureExecution 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)

Example 14 with BinaryWSProcedureExecution

use of org.teiid.translator.ws.BinaryWSProcedureExecution 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)

Example 15 with BinaryWSProcedureExecution

use of org.teiid.translator.ws.BinaryWSProcedureExecution in project teiid by teiid.

the class ODataUpdateExecution method execute.

@Override
public void execute() throws TranslatorException {
    if (this.visitor.getMethod().equals("DELETE")) {
        // $NON-NLS-1$
        // DELETE
        BinaryWSProcedureExecution execution = executeDirect(this.visitor.getMethod(), this.visitor.buildURL(), null, getDefaultHeaders());
        if (execution.getResponseCode() != Status.OK.getStatusCode() && (execution.getResponseCode() != Status.NO_CONTENT.getStatusCode())) {
            throw buildError(execution);
        }
    } else if (this.visitor.getMethod().equals("PUT")) {
        // $NON-NLS-1$
        // UPDATE
        Schema schema = visitor.getTable().getParent();
        EdmDataServices edm = new TeiidEdmMetadata(schema.getName(), ODataEntitySchemaBuilder.buildMetadata(schema));
        // $NON-NLS-1$
        BinaryWSProcedureExecution execution = executeDirect("GET", this.visitor.buildURL(), null, getDefaultHeaders());
        if (execution.getResponseCode() == Status.OK.getStatusCode()) {
            // $NON-NLS-1$
            String etag = getHeader(execution, "ETag");
            String payload = buildPayload(this.visitor.getTable().getName(), this.visitor.getPayload(), edm);
            this.response = executeWithReturnEntity(this.visitor.getMethod(), this.visitor.buildURL(), payload, this.visitor.getTable().getName(), edm, etag, Status.OK, Status.NO_CONTENT);
            if (this.response != null) {
                if (this.response.hasError()) {
                    throw this.response.getError();
                }
            }
        }
    } else if (this.visitor.getMethod().equals("POST")) {
        // $NON-NLS-1$
        // INSERT
        Schema schema = visitor.getTable().getParent();
        EdmDataServices edm = new TeiidEdmMetadata(schema.getName(), ODataEntitySchemaBuilder.buildMetadata(schema));
        String payload = buildPayload(this.visitor.getTable().getName(), this.visitor.getPayload(), edm);
        this.response = executeWithReturnEntity(this.visitor.getMethod(), this.visitor.buildURL(), payload, this.visitor.getTable().getName(), edm, null, Status.CREATED);
        if (this.response != null) {
            if (this.response.hasError()) {
                throw this.response.getError();
            }
        }
    }
}
Also used : BinaryWSProcedureExecution(org.teiid.translator.ws.BinaryWSProcedureExecution) Schema(org.teiid.metadata.Schema) EdmDataServices(org.odata4j.edm.EdmDataServices)

Aggregations

BinaryWSProcedureExecution (org.teiid.translator.ws.BinaryWSProcedureExecution)16 Blob (java.sql.Blob)11 List (java.util.List)10 SQLException (java.sql.SQLException)9 ArrayList (java.util.ArrayList)9 TranslatorException (org.teiid.translator.TranslatorException)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 Argument (org.teiid.language.Argument)4 Call (org.teiid.language.Call)4 Literal (org.teiid.language.Literal)4 InputStream (java.io.InputStream)3 InputStreamReader (java.io.InputStreamReader)3 HashMap (java.util.HashMap)3 EdmDataServices (org.odata4j.edm.EdmDataServices)3 Schema (org.teiid.metadata.Schema)3 IOException (java.io.IOException)2 TreeMap (java.util.TreeMap)2 Status (javax.ws.rs.core.Response.Status)2 HttpStatusCode (org.apache.olingo.commons.api.http.HttpStatusCode)2 ODataVersion (org.odata4j.core.ODataVersion)2