Search in sources :

Example 1 with BinaryWSProcedureExecution

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

the class S3ProcedureExecution method invokeHTTP.

protected BinaryWSProcedureExecution invokeHTTP(String method, String uri, Object payload, Map<String, String> headers) throws TranslatorException {
    Map<String, List<String>> targetHeaders = new HashMap<String, List<String>>();
    headers.forEach((k, v) -> targetHeaders.put(k, Arrays.asList(v)));
    if (LogManager.isMessageToBeRecorded(LogConstants.CTX_WS, MessageLevel.DETAIL)) {
        try {
            LogManager.logDetail(LogConstants.CTX_WS, "Source-URL=", // $NON-NLS-1$ //$NON-NLS-2$
            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.OBJECT), 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.ef.getLanguageFactory().createCall("invokeHttp", parameters, null);
    BinaryWSProcedureExecution execution = new BinaryWSProcedureExecution(call, this.metadata, this.ec, null, this.conn);
    execution.setUseResponseContext(true);
    execution.setCustomHeaders(targetHeaders);
    return execution;
}
Also used : Call(org.teiid.language.Call) Argument(org.teiid.language.Argument) HashMap(java.util.HashMap) Literal(org.teiid.language.Literal) BinaryWSProcedureExecution(org.teiid.translator.ws.BinaryWSProcedureExecution) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with BinaryWSProcedureExecution

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

the class ODataMetadataProcessor method getEds.

private EdmDataServices getEds(WSConnection conn) throws TranslatorException {
    try {
        BaseQueryExecution execution = new BaseQueryExecution(ef, null, null, conn);
        // $NON-NLS-1$ //$NON-NLS-2$
        BinaryWSProcedureExecution call = execution.executeDirect("GET", "$metadata", null, execution.getDefaultHeaders());
        if (call.getResponseCode() != Status.OK.getStatusCode()) {
            throw execution.buildError(call);
        }
        Blob out = (Blob) call.getOutputParameterValues().get(0);
        EdmDataServices eds = new EdmxFormatParser().parseMetadata(StaxUtil.newXMLEventReader(new InputStreamReader(out.getBinaryStream())));
        return eds;
    } catch (SQLException e) {
        throw new TranslatorException(e);
    } catch (Exception e) {
        throw new TranslatorException(e);
    }
}
Also used : Blob(java.sql.Blob) InputStreamReader(java.io.InputStreamReader) SQLException(java.sql.SQLException) BinaryWSProcedureExecution(org.teiid.translator.ws.BinaryWSProcedureExecution) EdmxFormatParser(org.odata4j.format.xml.EdmxFormatParser) TranslatorException(org.teiid.translator.TranslatorException) SQLException(java.sql.SQLException) TranslatorException(org.teiid.translator.TranslatorException)

Example 3 with BinaryWSProcedureExecution

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

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

the class ODataProcedureExecution method execute.

@Override
public void execute() throws TranslatorException {
    String URI = this.visitor.buildURL();
    Schema schema = visitor.getProcedure().getParent();
    EdmDataServices edm = new TeiidEdmMetadata(schema.getName(), ODataEntitySchemaBuilder.buildMetadata(schema));
    if (this.visitor.hasCollectionReturn()) {
        if (this.visitor.isReturnComplexType()) {
            // complex return
            this.response = executeWithComplexReturn(this.visitor.getMethod(), URI, null, this.visitor.getReturnEntityTypeName(), edm, null, Status.OK, Status.NO_CONTENT);
        } else {
            // entity type return
            this.response = executeWithReturnEntity(this.visitor.getMethod(), URI, null, this.visitor.getTable().getName(), edm, null, Status.OK, Status.NO_CONTENT);
        }
        if (this.response != null && this.response.hasError()) {
            throw this.response.getError();
        }
    } else {
        try {
            BinaryWSProcedureExecution execution = executeDirect(this.visitor.getMethod(), URI, null, getDefaultHeaders());
            if (execution.getResponseCode() != Status.OK.getStatusCode()) {
                throw buildError(execution);
            }
            Blob blob = (Blob) execution.getOutputParameterValues().get(0);
            ODataVersion version = getODataVersion(execution);
            // if the procedure is not void
            if (this.visitor.getReturnType() != null) {
                FormatParser<? extends OObject> parser = FormatParserFactory.getParser(OSimpleObject.class, FormatType.ATOM, new Settings(version, edm, this.visitor.getProcedure().getName(), // entitykey
                null, // isResponse
                true, ODataTypeManager.odataType(this.visitor.getReturnType())));
                OSimpleObject object = (OSimpleObject) parser.parse(new InputStreamReader(blob.getBinaryStream()));
                this.returnValue = this.translator.retrieveValue(object.getValue(), this.visitor.getReturnTypeClass());
            }
        } catch (SQLException e) {
            throw new TranslatorException(e);
        }
    }
}
Also used : Blob(java.sql.Blob) OSimpleObject(org.odata4j.core.OSimpleObject) InputStreamReader(java.io.InputStreamReader) SQLException(java.sql.SQLException) Schema(org.teiid.metadata.Schema) BinaryWSProcedureExecution(org.teiid.translator.ws.BinaryWSProcedureExecution) ODataVersion(org.odata4j.core.ODataVersion) EdmDataServices(org.odata4j.edm.EdmDataServices) TranslatorException(org.teiid.translator.TranslatorException) Settings(org.odata4j.format.Settings)

Example 5 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 {
    String URI = this.visitor.buildURL();
    if (this.visitor.isCount()) {
        Map<String, List<String>> headers = new TreeMap<String, List<String>>();
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        headers.put("Accept", Arrays.asList("text/xml", "text/plain"));
        // $NON-NLS-1$
        BinaryWSProcedureExecution execution = executeDirect("GET", URI, null, headers);
        if (execution.getResponseCode() != Status.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 {
        Schema schema = visitor.getEnityTable().getParent();
        EdmDataServices edm = new TeiidEdmMetadata(schema.getName(), ODataEntitySchemaBuilder.buildMetadata(schema));
        // $NON-NLS-1$
        this.response = executeWithReturnEntity("GET", URI, null, visitor.getEnityTable().getName(), edm, null, Status.OK, Status.NO_CONTENT, Status.NOT_FOUND);
        if (this.response != null && this.response.hasError()) {
            throw this.response.getError();
        }
    }
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) BinaryWSProcedureExecution(org.teiid.translator.ws.BinaryWSProcedureExecution) Schema(org.teiid.metadata.Schema) List(java.util.List) TranslatorException(org.teiid.translator.TranslatorException) EdmDataServices(org.odata4j.edm.EdmDataServices) IOException(java.io.IOException) TreeMap(java.util.TreeMap)

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