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;
}
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));
}
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 });
}
};
}
}
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);
}
}
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();
}
}
}
}
Aggregations