use of org.ballerinalang.model.values.BString in project ballerina by ballerina-lang.
the class StructAccessWithIndexTest method testBasicStruct.
@Test(description = "Test Basic struct operations")
public void testBasicStruct() {
BValue[] returns = BRunUtil.invoke(compileResult, "testCreateStruct");
Assert.assertTrue(returns[0] instanceof BString);
Assert.assertEquals(returns[0].stringValue(), "Jack");
Assert.assertTrue(returns[1] instanceof BMap);
BMap<String, ?> adrsMap = ((BMap) returns[1]);
Assert.assertEquals(adrsMap.get("country"), new BString("USA"));
Assert.assertEquals(adrsMap.get("state"), new BString("CA"));
Assert.assertTrue(returns[2] instanceof BInteger);
Assert.assertEquals(((BInteger) returns[2]).intValue(), 25);
}
use of org.ballerinalang.model.values.BString in project ballerina by ballerina-lang.
the class GetHash method execute.
/**
* Hashes the string contents (assumed to be UTF-8) using the SHA-256 algorithm.
*/
@Override
public void execute(Context context) {
String baseString = context.getStringArgument(0);
BEnumerator algorithm = (BEnumerator) context.getRefArgument(0);
String hashAlgorithm;
// todo document the supported algorithm
switch(algorithm.getName()) {
case "SHA1":
hashAlgorithm = "SHA-1";
break;
case "SHA256":
hashAlgorithm = "SHA-256";
break;
case "MD5":
hashAlgorithm = "MD5";
break;
default:
throw new BallerinaException("Unsupported algorithm " + algorithm + " for HMAC calculation");
}
String result;
try {
MessageDigest messageDigest;
messageDigest = MessageDigest.getInstance(hashAlgorithm);
messageDigest.update(baseString.getBytes("UTF-8"));
byte[] bytes = messageDigest.digest();
final char[] hexArray = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
final int byteVal = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[byteVal >>> 4];
hexChars[j * 2 + 1] = hexArray[byteVal & 0x0F];
}
result = new String(hexChars);
} catch (NoSuchAlgorithmException e) {
throw new BallerinaException("Error while calculating HMAC for " + algorithm + ": " + e.getMessage(), context);
} catch (UnsupportedEncodingException e) {
throw new BallerinaException("Error while encoding" + e.getMessage(), context);
}
context.setReturnValues(new BString(result));
}
use of org.ballerinalang.model.values.BString in project ballerina by ballerina-lang.
the class MessageUtils method getHeader.
public static BValue getHeader(Context context) {
String headerName = context.getStringArgument(0);
String headerValue = getHeaderValue(headerName);
return new BString(headerValue);
}
use of org.ballerinalang.model.values.BString in project ballerina by ballerina-lang.
the class MessageUtils method generateProtoMessage.
/**
* Returns protobuf message corresponding to the B7a message.
*
* @param responseValue B7a message.
* @param outputType protobuf message type.
* @return generated protobuf message.
*/
public static Message generateProtoMessage(BValue responseValue, Descriptors.Descriptor outputType) {
Message.Builder responseBuilder = Message.newBuilder(outputType.getName());
int stringIndex = 0;
int intIndex = 0;
int floatIndex = 0;
int boolIndex = 0;
int refIndex = 0;
for (Descriptors.FieldDescriptor fieldDescriptor : outputType.getFields()) {
String fieldName = fieldDescriptor.getName();
switch(fieldDescriptor.getType().toProto().getNumber()) {
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_DOUBLE_VALUE:
{
double value = 0F;
if (responseValue instanceof BStruct) {
value = ((BStruct) responseValue).getFloatField(floatIndex++);
} else {
if (responseValue instanceof BFloat) {
value = ((BFloat) responseValue).value();
}
}
responseBuilder.addField(fieldName, value);
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FLOAT_VALUE:
{
float value = 0F;
if (responseValue instanceof BStruct) {
value = Float.parseFloat(String.valueOf(((BStruct) responseValue).getFloatField(floatIndex++)));
} else {
if (responseValue instanceof BFloat) {
value = Float.parseFloat(String.valueOf(((BFloat) responseValue).value()));
}
}
responseBuilder.addField(fieldName, value);
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT64_VALUE:
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_UINT64_VALUE:
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED64_VALUE:
{
long value = 0;
if (responseValue instanceof BStruct) {
value = ((BStruct) responseValue).getIntField(intIndex++);
} else {
if (responseValue instanceof BInteger) {
value = ((BInteger) responseValue).value();
}
}
responseBuilder.addField(fieldName, value);
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32_VALUE:
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_FIXED32_VALUE:
{
int value = 0;
if (responseValue instanceof BStruct) {
value = Integer.parseInt(String.valueOf(((BStruct) responseValue).getIntField(intIndex++)));
} else {
if (responseValue instanceof BInteger) {
value = Integer.parseInt(String.valueOf(((BInteger) responseValue).value()));
}
}
responseBuilder.addField(fieldName, value);
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_BOOL_VALUE:
{
boolean value = false;
if (responseValue instanceof BStruct) {
value = ((BStruct) responseValue).getBooleanField(boolIndex++) > 0;
} else {
if (responseValue instanceof BBoolean) {
value = ((BBoolean) responseValue).value();
}
}
responseBuilder.addField(fieldName, value);
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_STRING_VALUE:
{
String value = null;
if (responseValue instanceof BStruct) {
value = ((BStruct) responseValue).getStringField(stringIndex++);
} else {
if (responseValue instanceof BString) {
value = ((BString) responseValue).value();
}
}
responseBuilder.addField(fieldName, value);
break;
}
case DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE_VALUE:
{
if (responseValue instanceof BStruct) {
BValue bValue = ((BStruct) responseValue).getRefField(refIndex++);
responseBuilder.addField(fieldName, generateProtoMessage(bValue, fieldDescriptor.getMessageType()));
}
break;
}
default:
{
throw new UnsupportedFieldTypeException("Error while decoding request message. Field " + "type is not supported : " + fieldDescriptor.getType());
}
}
}
return responseBuilder.build();
}
use of org.ballerinalang.model.values.BString in project ballerina by ballerina-lang.
the class DescriptorBuilder method buildMap.
public void buildMap() throws IOException {
InputStream targetStream = new ByteArrayInputStream(rootDescriptor);
DescriptorProtos.FileDescriptorProto fileDescriptorSet = DescriptorProtos.FileDescriptorProto.parseFrom(targetStream);
BMap<String, BString> descriptorMap = new BMap<String, BString>();
if (!"".equals(fileDescriptorSet.getPackage())) {
descriptorMap.put(fileDescriptorSet.getPackage() + PACKAGE_SEPARATOR + fileDescriptorSet.getName(), new BString(bytesToHex(rootDescriptor)));
} else {
descriptorMap.put(fileDescriptorSet.getName(), new BString(bytesToHex(rootDescriptor)));
}
for (byte[] str : dependentDescriptors) {
if (str.length > 0) {
targetStream = new ByteArrayInputStream(str);
fileDescriptorSet = DescriptorProtos.FileDescriptorProto.parseFrom(targetStream);
descriptorMap.put(fileDescriptorSet.getPackage() + "." + fileDescriptorSet.getName(), new BString(bytesToHex(str)));
}
}
int i = 0;
for (Object key : descriptorMap.keySet()) {
Descriptor descriptor;
if (i != descriptorMap.keySet().size() - 1) {
descriptor = new Descriptor((String) key, descriptorMap.get((String) key).stringValue(), ",");
} else {
descriptor = new Descriptor((String) key, descriptorMap.get((String) key).stringValue(), null);
}
clientStubBal.addDescriptor(descriptor);
i++;
}
}
Aggregations