use of jolie.runtime.ByteArray in project jolie by jolie.
the class HttpProtocol method send_encodeContent.
private EncodedContent send_encodeContent(CommMessage message, Method method, String charset, String format) throws IOException {
EncodedContent ret = new EncodedContent();
if (inInputPort == false && (method == Method.GET || method == Method.DELETE)) {
// We are building a GET or DELETE request
return ret;
}
if ("xml".equals(format)) {
ret.contentType = "text/xml";
Document doc = docBuilder.newDocument();
Element root = doc.createElement(message.operationName() + ((inInputPort) ? "Response" : ""));
doc.appendChild(root);
if (message.isFault()) {
Element faultElement = doc.createElement(message.fault().faultName());
root.appendChild(faultElement);
XmlUtils.valueToDocument(message.fault().value(), faultElement, doc);
} else {
XmlUtils.valueToDocument(message.value(), root, doc);
}
Source src = new DOMSource(doc);
ByteArrayOutputStream tmpStream = new ByteArrayOutputStream();
Result dest = new StreamResult(tmpStream);
transformer.setOutputProperty(OutputKeys.ENCODING, charset);
try {
transformer.transform(src, dest);
} catch (TransformerException e) {
throw new IOException(e);
}
ret.content = new ByteArray(tmpStream.toByteArray());
} else if ("binary".equals(format)) {
ret.contentType = "application/octet-stream";
ret.content = message.value().byteArrayValue();
} else if ("html".equals(format)) {
ret.contentType = "text/html";
if (message.isFault()) {
StringBuilder builder = new StringBuilder();
builder.append("<html><head><title>").append(message.fault().faultName()).append("</title></head><body>").append(message.fault().value().strValue()).append("</body></html>");
ret.content = new ByteArray(builder.toString().getBytes(charset));
} else {
ret.content = new ByteArray(message.value().strValue().getBytes(charset));
}
} else if ("multipart/form-data".equals(format)) {
ret.contentType = "multipart/form-data; boundary=" + BOUNDARY;
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
StringBuilder builder = new StringBuilder();
for (Entry<String, ValueVector> entry : message.value().children().entrySet()) {
if (!entry.getKey().startsWith("@")) {
builder.append("--").append(BOUNDARY).append(HttpUtils.CRLF).append("Content-Disposition: form-data; name=\"").append(entry.getKey()).append('\"');
boolean isBinary = false;
if (hasOperationSpecificParameter(message.operationName(), Parameters.MULTIPART_HEADERS)) {
Value specOpParam = getOperationSpecificParameterFirstValue(message.operationName(), Parameters.MULTIPART_HEADERS);
if (specOpParam.hasChildren("partName")) {
ValueVector partNames = specOpParam.getChildren("partName");
for (int p = 0; p < partNames.size(); p++) {
if (partNames.get(p).hasChildren("part")) {
if (partNames.get(p).getFirstChild("part").strValue().equals(entry.getKey())) {
isBinary = true;
if (partNames.get(p).hasChildren("filename")) {
builder.append("; filename=\"").append(partNames.get(p).getFirstChild("filename").strValue()).append("\"");
}
if (partNames.get(p).hasChildren("contentType")) {
builder.append(HttpUtils.CRLF).append("Content-Type:").append(partNames.get(p).getFirstChild("contentType").strValue());
}
}
}
}
}
}
builder.append(HttpUtils.CRLF).append(HttpUtils.CRLF);
if (isBinary) {
bStream.write(builder.toString().getBytes(charset));
bStream.write(entry.getValue().first().byteArrayValue().getBytes());
builder.delete(0, builder.length() - 1);
builder.append(HttpUtils.CRLF);
} else {
builder.append(entry.getValue().first().strValue()).append(HttpUtils.CRLF);
}
}
}
builder.append("--" + BOUNDARY + "--");
bStream.write(builder.toString().getBytes(charset));
ret.content = new ByteArray(bStream.toByteArray());
} else if ("x-www-form-urlencoded".equals(format)) {
ret.contentType = "application/x-www-form-urlencoded";
Iterator<Entry<String, ValueVector>> it = message.value().children().entrySet().iterator();
StringBuilder builder = new StringBuilder();
if (message.isFault()) {
builder.append("faultName=").append(URLEncoder.encode(message.fault().faultName(), HttpUtils.URL_DECODER_ENC)).append("&data=").append(URLEncoder.encode(message.fault().value().strValue(), HttpUtils.URL_DECODER_ENC));
} else {
Entry<String, ValueVector> entry;
while (it.hasNext()) {
entry = it.next();
builder.append(URLEncoder.encode(entry.getKey(), HttpUtils.URL_DECODER_ENC)).append("=").append(URLEncoder.encode(entry.getValue().first().strValue(), HttpUtils.URL_DECODER_ENC));
if (it.hasNext()) {
builder.append('&');
}
}
}
ret.content = new ByteArray(builder.toString().getBytes(charset));
} else if ("json".equals(format)) {
ret.contentType = ContentTypes.APPLICATION_JSON;
StringBuilder jsonStringBuilder = new StringBuilder();
if (message.isFault()) {
Value error = message.value().getFirstChild("error");
error.getFirstChild("code").setValue(-32000);
error.getFirstChild("message").setValue(message.fault().faultName());
error.getChildren("data").set(0, message.fault().value());
JsUtils.faultValueToJsonString(message.value(), getSendType(message), jsonStringBuilder);
} else {
JsUtils.valueToJsonString(message.value(), true, getSendType(message), jsonStringBuilder);
}
ret.content = new ByteArray(jsonStringBuilder.toString().getBytes(charset));
} else if ("ndjson".equals(format)) {
ret.contentType = ContentTypes.APPLICATION_NDJSON;
StringBuilder ndJsonStringBuilder = new StringBuilder();
if (message.isFault()) {
Value error = message.value().getFirstChild("error");
error.getFirstChild("code").setValue(-32000);
error.getFirstChild("message").setValue(message.fault().faultName());
error.getChildren("data").set(0, message.fault().value());
JsUtils.faultValueToJsonString(message.value(), getSendType(message), ndJsonStringBuilder);
} else {
if (!message.value().hasChildren("item")) {
Interpreter.getInstance().logWarning("ndJson requires at least one child node 'item'");
}
JsUtils.valueToNdJsonString(message.value(), true, getSendType(message), ndJsonStringBuilder);
}
ret.content = new ByteArray(ndJsonStringBuilder.toString().getBytes(charset));
} else if ("raw".equals(format)) {
ret.contentType = "text/plain";
if (message.isFault()) {
ret.content = new ByteArray(message.fault().value().strValue().getBytes(charset));
} else {
ret.content = new ByteArray(message.value().strValue().getBytes(charset));
}
}
return ret;
}
use of jolie.runtime.ByteArray in project jolie by jolie.
the class HttpProtocol method recv_parseMessage.
private void recv_parseMessage(HttpMessage message, DecodedMessage decodedMessage, String type, String charset) throws IOException {
final String operationName = message.isResponse() ? inputId : decodedMessage.operationName;
if (getOperationSpecificStringParameter(operationName, Parameters.FORCE_CONTENT_DECODING).equals(NativeType.STRING.id())) {
decodedMessage.value.setValue(new String(message.content(), charset));
} else if (getOperationSpecificStringParameter(operationName, Parameters.FORCE_CONTENT_DECODING).equals(NativeType.RAW.id())) {
decodedMessage.value.setValue(new ByteArray(message.content()));
} else if ("text/html".equals(type)) {
decodedMessage.value.setValue(new String(message.content(), charset));
} else if ("application/x-www-form-urlencoded".equals(type)) {
parseForm(message, decodedMessage.value, charset);
} else if ("text/xml".equals(type) || type.contains("xml")) {
parseXML(message, decodedMessage.value, charset);
} else if ("multipart/form-data".equals(type)) {
parseMultiPartFormData(message, decodedMessage.value);
} else if ("application/octet-stream".equals(type) || type.startsWith("image/") || "application/zip".equals(type)) {
decodedMessage.value.setValue(new ByteArray(message.content()));
} else if (ContentTypes.APPLICATION_NDJSON.equals(type) || type.contains("ndjson")) {
boolean strictEncoding = checkStringParameter(Parameters.JSON_ENCODING, "strict");
parseNdJson(message, decodedMessage.value, strictEncoding, charset);
} else if (ContentTypes.APPLICATION_JSON.equals(type) || type.contains("json")) {
boolean strictEncoding = checkStringParameter(Parameters.JSON_ENCODING, "strict");
parseJson(message, decodedMessage.value, strictEncoding, charset);
} else {
decodedMessage.value.setValue(new String(message.content(), charset));
}
}
use of jolie.runtime.ByteArray in project jolie by jolie.
the class XmlRpcProtocol method navigateValue.
private void navigateValue(Value value, Element element) throws IOException {
NodeList contents = element.getChildNodes();
if (contents.getLength() < 1) {
throw new IOException("empty value node");
}
Node contentNode = contents.item(0);
if (contentNode.getNodeType() != Node.ELEMENT_NODE) {
throw new IOException("a value node may contain only one sub-element");
}
Element content = (Element) contentNode;
String name = content.getNodeName();
switch(name) {
case "array":
Value currentValue;
ValueVector vec = value.getChildren(ARRAY_KEY);
Element data = getFirstElement(content, "data");
NodeList dataChildren = data.getElementsByTagName("value");
for (int i = 0; i < dataChildren.getLength(); i++) {
Element member = (Element) dataChildren.item(i);
if (!member.getParentNode().equals(data)) {
// not a direct child
continue;
}
currentValue = Value.create();
navigateValue(currentValue, member);
vec.add(currentValue);
}
break;
case "struct":
NodeList members = content.getElementsByTagName("member");
for (int i = 0; i < members.getLength(); i++) {
Element member = (Element) members.item(i);
if (!member.getParentNode().equals(content)) {
// not a direct child
continue;
}
Element valueNode = getFirstElement(member, "value");
navigateValue(value.getNewChild(getFirstElement(member, "name").getTextContent()), valueNode);
}
break;
case "string":
value.setValue(content.getTextContent());
break;
case "int":
case "i4":
try {
value.setValue(Integer.parseInt(content.getTextContent()));
} catch (NumberFormatException e) {
throw new IOException(e);
}
break;
case "double":
try {
value.setValue(Double.parseDouble(content.getTextContent()));
} catch (NumberFormatException e) {
throw new IOException(e);
}
break;
case "boolean":
try {
value.setValue(Integer.parseInt(content.getTextContent()) != 0);
} catch (NumberFormatException e) {
throw new IOException(e);
}
break;
case "base64":
value.setValue(new ByteArray(Base64.getDecoder().decode(content.getTextContent())));
break;
default:
// parse everything else as string (including <dateTime.iso8601>)
value.setValue(content.getTextContent());
break;
}
}
use of jolie.runtime.ByteArray in project jolie by jolie.
the class DatabaseService method setValue.
private static void setValue(Value fieldValue, ResultSet result, int columnType, int index) throws SQLException {
ByteArray supportByteArray;
switch(columnType) {
case java.sql.Types.INTEGER:
case java.sql.Types.SMALLINT:
case java.sql.Types.TINYINT:
fieldValue.setValue(result.getInt(index));
break;
case java.sql.Types.BIGINT:
fieldValue.setValue(result.getLong(index));
break;
case java.sql.Types.REAL:
case java.sql.Types.DOUBLE:
fieldValue.setValue(result.getDouble(index));
break;
case java.sql.Types.DECIMAL:
{
BigDecimal dec = result.getBigDecimal(index);
if (dec == null) {
fieldValue.setValue(0);
} else {
if (dec.scale() <= 0) {
// May lose information.
// Pay some attention to this when Long becomes supported by JOLIE.
fieldValue.setValue(dec.intValue());
} else if (dec.scale() > 0) {
fieldValue.setValue(dec.doubleValue());
}
}
}
break;
case java.sql.Types.FLOAT:
fieldValue.setValue(result.getFloat(index));
break;
case java.sql.Types.BLOB:
supportByteArray = new ByteArray(result.getBytes(index));
fieldValue.setValue(supportByteArray);
break;
case java.sql.Types.CLOB:
Clob clob = result.getClob(index);
fieldValue.setValue(clob.getSubString(1, (int) clob.length()));
break;
case java.sql.Types.BINARY:
supportByteArray = new ByteArray(result.getBytes(index));
fieldValue.setValue(supportByteArray);
break;
case java.sql.Types.VARBINARY:
supportByteArray = new ByteArray(result.getBytes(index));
fieldValue.setValue(supportByteArray);
break;
case Types.LONGVARBINARY:
supportByteArray = new ByteArray(result.getBytes(index));
fieldValue.setValue(supportByteArray);
break;
case java.sql.Types.NVARCHAR:
case java.sql.Types.NCHAR:
case java.sql.Types.LONGNVARCHAR:
String s = result.getNString(index);
if (s == null) {
s = "";
}
fieldValue.setValue(s);
break;
case java.sql.Types.NUMERIC:
{
BigDecimal dec = result.getBigDecimal(index);
if (dec == null) {
fieldValue.setValue(0);
} else {
if (dec.scale() <= 0) {
// May lose information.
// Pay some attention to this when Long becomes supported by JOLIE.
fieldValue.setValue(dec.intValue());
} else if (dec.scale() > 0) {
fieldValue.setValue(dec.doubleValue());
}
}
}
break;
case java.sql.Types.BIT:
case java.sql.Types.BOOLEAN:
fieldValue.setValue(result.getBoolean(index));
break;
case java.sql.Types.VARCHAR:
default:
String str = result.getString(index);
if (str == null) {
str = "";
}
fieldValue.setValue(str);
break;
}
}
use of jolie.runtime.ByteArray in project jolie by jolie.
the class FileService method readBinaryIntoValue.
private static void readBinaryIntoValue(InputStream istream, long size, Value value) throws IOException {
byte[] buffer = new byte[(int) size];
istream.read(buffer);
value.setValue(new ByteArray(buffer));
}
Aggregations