Search in sources :

Example 1 with BEnumerator

use of org.ballerinalang.model.values.BEnumerator 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 2 with BEnumerator

use of org.ballerinalang.model.values.BEnumerator in project ballerina by ballerina-lang.

the class FormatTo method execute.

@Override
public void execute(Context context) {
    BStruct timeStruct = ((BStruct) context.getRefArgument(0));
    BEnumerator pattern = (BEnumerator) context.getRefArgument(1);
    switch(pattern.getName()) {
        case "RFC_1123":
            ZonedDateTime zonedDateTime = getZonedDateTime(timeStruct);
            String formattedDateTime = zonedDateTime.format(DateTimeFormatter.RFC_1123_DATE_TIME);
            context.setReturnValues(new BString(formattedDateTime));
            break;
        default:
            throw new BallerinaException("failed to format date/time: unrecognized time format");
    }
}
Also used : BEnumerator(org.ballerinalang.model.values.BEnumerator) BStruct(org.ballerinalang.model.values.BStruct) ZonedDateTime(java.time.ZonedDateTime) BString(org.ballerinalang.model.values.BString) BString(org.ballerinalang.model.values.BString) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 3 with BEnumerator

use of org.ballerinalang.model.values.BEnumerator in project ballerina by ballerina-lang.

the class AbstractSQLAction method getParameterDirection.

private int getParameterDirection(BStruct parameter) {
    int direction = 0;
    BEnumerator dirEnum = (BEnumerator) parameter.getRefField(2);
    if (dirEnum != null) {
        String sqlType = dirEnum.getName();
        switch(sqlType) {
            case Constants.QueryParamDirection.DIR_OUT:
                direction = Constants.QueryParamDirection.OUT;
                break;
            case Constants.QueryParamDirection.DIR_INOUT:
                direction = Constants.QueryParamDirection.INOUT;
                break;
        }
    }
    return direction;
}
Also used : BEnumerator(org.ballerinalang.model.values.BEnumerator) BString(org.ballerinalang.model.values.BString)

Example 4 with BEnumerator

use of org.ballerinalang.model.values.BEnumerator in project ballerina by ballerina-lang.

the class ProgramFileReader method readEnumInfoEntries.

private void readEnumInfoEntries(DataInputStream dataInStream, PackageInfo packageInfo) throws IOException {
    int enumCount = dataInStream.readShort();
    for (int i = 0; i < enumCount; i++) {
        // Create enum info entry
        int enumNameCPIndex = dataInStream.readInt();
        int flags = dataInStream.readInt();
        UTF8CPEntry enumNameUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(enumNameCPIndex);
        String enumName = enumNameUTF8Entry.getValue();
        EnumInfo enumInfo = new EnumInfo(packageInfo.getPkgNameCPIndex(), packageInfo.getPkgPath(), enumNameCPIndex, enumName, flags);
        packageInfo.addEnumInfo(enumName, enumInfo);
        // Set enum type
        BEnumType enumType = new BEnumType(enumName, packageInfo.getPkgPath());
        enumInfo.setType(enumType);
        int enumeratorCount = dataInStream.readShort();
        BEnumerator[] enumerators = new BEnumerator[enumeratorCount];
        for (int j = 0; j < enumeratorCount; j++) {
            int enumeratorNameCPIndex = dataInStream.readInt();
            UTF8CPEntry enumeratorNameUTF8Entry = (UTF8CPEntry) packageInfo.getCPEntry(enumeratorNameCPIndex);
            String enumeratorName = enumeratorNameUTF8Entry.getValue();
            BEnumerator enumerator = new BEnumerator(enumeratorName, enumType);
            enumerators[j] = enumerator;
        }
        enumType.setEnumerators(enumerators);
        // Read attributes of the struct info
        readAttributeInfoEntries(dataInStream, packageInfo, enumInfo);
    }
}
Also used : UTF8CPEntry(org.ballerinalang.util.codegen.cpentries.UTF8CPEntry) BEnumerator(org.ballerinalang.model.values.BEnumerator) BEnumType(org.ballerinalang.model.types.BEnumType)

Example 5 with BEnumerator

use of org.ballerinalang.model.values.BEnumerator in project ballerina by ballerina-lang.

the class AbstractSQLAction method getSQLType.

private String getSQLType(BStruct parameter) {
    String sqlType = "";
    BEnumerator typeEnum = (BEnumerator) parameter.getRefField(0);
    if (typeEnum != null) {
        sqlType = typeEnum.getName();
    }
    return sqlType;
}
Also used : BEnumerator(org.ballerinalang.model.values.BEnumerator) BString(org.ballerinalang.model.values.BString)

Aggregations

BEnumerator (org.ballerinalang.model.values.BEnumerator)7 BString (org.ballerinalang.model.values.BString)5 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 BStruct (org.ballerinalang.model.values.BStruct)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvalidKeyException (java.security.InvalidKeyException)1 MessageDigest (java.security.MessageDigest)1 DateTimeException (java.time.DateTimeException)1 ZonedDateTime (java.time.ZonedDateTime)1 TemporalAccessor (java.time.temporal.TemporalAccessor)1 Mac (javax.crypto.Mac)1 SecretKey (javax.crypto.SecretKey)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1 BEnumType (org.ballerinalang.model.types.BEnumType)1 StructInfo (org.ballerinalang.util.codegen.StructInfo)1 UTF8CPEntry (org.ballerinalang.util.codegen.cpentries.UTF8CPEntry)1