Search in sources :

Example 86 with BString

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);
}
Also used : BMap(org.ballerinalang.model.values.BMap) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Example 87 with BString

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));
}
Also used : BEnumerator(org.ballerinalang.model.values.BEnumerator) BString(org.ballerinalang.model.values.BString) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BString(org.ballerinalang.model.values.BString) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 88 with BString

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);
}
Also used : BString(org.ballerinalang.model.values.BString) BString(org.ballerinalang.model.values.BString)

Example 89 with BString

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();
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BString(org.ballerinalang.model.values.BString) BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) BBoolean(org.ballerinalang.model.values.BBoolean) UnsupportedFieldTypeException(org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException) BString(org.ballerinalang.model.values.BString) BFloat(org.ballerinalang.model.values.BFloat) Descriptors(com.google.protobuf.Descriptors)

Example 90 with BString

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++;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BMap(org.ballerinalang.model.values.BMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BString(org.ballerinalang.model.values.BString) DescriptorProtos(com.google.protobuf.DescriptorProtos) BString(org.ballerinalang.model.values.BString)

Aggregations

BString (org.ballerinalang.model.values.BString)418 BValue (org.ballerinalang.model.values.BValue)339 Test (org.testng.annotations.Test)321 BInteger (org.ballerinalang.model.values.BInteger)77 BStruct (org.ballerinalang.model.values.BStruct)76 BMap (org.ballerinalang.model.values.BMap)37 BBoolean (org.ballerinalang.model.values.BBoolean)36 BFloat (org.ballerinalang.model.values.BFloat)34 BJSON (org.ballerinalang.model.values.BJSON)28 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)21 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)18 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)11 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)11 CompileResult (org.ballerinalang.launcher.util.CompileResult)11 BBlob (org.ballerinalang.model.values.BBlob)11 BStringArray (org.ballerinalang.model.values.BStringArray)11 BType (org.ballerinalang.model.types.BType)10 BIntArray (org.ballerinalang.model.values.BIntArray)9 BRefType (org.ballerinalang.model.values.BRefType)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7