Search in sources :

Example 1 with BString

use of org.ballerinalang.model.values.BString in project carbon-apimgt by wso2.

the class Analyze method execute.

@Override
public BValue[] execute(Context context) {
    String payloadType = getStringArgument(context, 0);
    String payload = getStringArgument(context, 1);
    String apiContext = getStringArgument(context, 2);
    String policyId = getStringArgument(context, 3);
    APIMThreatAnalyzer analyzer = AnalyzerHolder.getAnalyzer(payloadType, policyId);
    if (analyzer == null) {
        return getBValues(new BBoolean(false), new BString("Unknown Payload Type"));
    }
    boolean noThreatsDetected = true;
    String errMessage = null;
    try {
        analyzer.analyze(payload, apiContext);
    } catch (APIMThreatAnalyzerException e) {
        noThreatsDetected = false;
        errMessage = e.getMessage();
    }
    AnalyzerHolder.returnObject(analyzer);
    return getBValues(new BBoolean(noThreatsDetected), new BString(errMessage));
}
Also used : BString(org.ballerinalang.model.values.BString) BBoolean(org.ballerinalang.model.values.BBoolean) BString(org.ballerinalang.model.values.BString) APIMThreatAnalyzer(org.wso2.carbon.apimgt.ballerina.threatprotection.analyzer.APIMThreatAnalyzer)

Example 2 with BString

use of org.ballerinalang.model.values.BString in project carbon-apimgt by wso2.

the class PutCacheEntry method execute.

@Override
public BValue[] execute(Context context) {
    String cacheName = getStringArgument(context, 0);
    String cacheKey = getStringArgument(context, 1);
    BValue cacheEntry = getRefArgument(context, 0);
    // TODO If cache is not created then need to send proper message or create and put entry.
    CacheManagerHolder.getInstance().getCacheManager().getCache(cacheName).put(cacheKey, cacheEntry);
    return getBValues(new BString(cacheName));
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BString(org.ballerinalang.model.values.BString)

Example 3 with BString

use of org.ballerinalang.model.values.BString in project carbon-apimgt by wso2.

the class CacheTest method testCacheForNonExistence.

@Test
public void testCacheForNonExistence() {
    ProgramFile bLangProgram1 = BTestUtils.parseBalFile("samples/cache/nonExistenceEntryCacheTest.bal");
    // Create arguments to initiate cache
    BValue[] args = { new BString("cacheName"), new BString("15"), new BString("cacheKey") };
    // Test ballerina cache create, put and get for BString
    BValue[] returns = BLangFunctions.invokeNew(bLangProgram1, "testCacheForNonExistence", args);
    Assert.assertTrue(returns[0] instanceof BBoolean);
    BBoolean value = (BBoolean) returns[0];
    Assert.assertFalse(value.booleanValue());
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BBoolean(org.ballerinalang.model.values.BBoolean) ProgramFile(org.ballerinalang.util.codegen.ProgramFile) Test(org.testng.annotations.Test)

Example 4 with BString

use of org.ballerinalang.model.values.BString in project carbon-apimgt by wso2.

the class MapRemoveTestCase method testCacheOperations.

@Test
public void testCacheOperations() {
    // Create arguments to initiate map put
    BValue[] args = { new BString("testMapEntry"), new BString("testMapValue") };
    // Test ballerina map put, remove and get
    BValue[] returns = BLangFunctions.invokeNew(bLangProgram, "testMapRemove", args);
    // Assert if cache entry is BValue
    Assert.assertTrue(returns[0] instanceof BValue);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Example 5 with BString

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

the class Replace method execute.

@Override
public void execute(Context context) {
    String mainString = context.getStringArgument(0);
    String replacePattern = context.getStringArgument(1);
    String replaceWith = context.getStringArgument(2);
    String replacedString = mainString.replace(replacePattern, replaceWith);
    context.setReturnValues(new BString(replacedString));
}
Also used : BString(org.ballerinalang.model.values.BString) 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