Search in sources :

Example 1 with BBoolean

use of org.ballerinalang.model.values.BBoolean 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 BBoolean

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

the class ConfigureXmlAnalyzer method execute.

@Override
public BValue[] execute(Context context) {
    String event = getStringArgument(context, 0);
    BStruct xmlInfo = ((BStruct) getRefArgument(context, 0));
    if (xmlInfo != null) {
        String xmlPolicyId = xmlInfo.getStringField(0);
        switch(event) {
            case THREAT_PROTECTION_POLICY_ADD:
            case THREAT_PROTECTION_POLICY_UPDATE:
                String name = xmlInfo.getStringField(1);
                boolean dtdEnabled = xmlInfo.getBooleanField(0) != 0;
                boolean externalEntitiesEnabled = xmlInfo.getBooleanField(1) != 0;
                int maxXMLDepth = (int) xmlInfo.getIntField(0);
                int elementCount = (int) xmlInfo.getIntField(1);
                int attributeCount = (int) xmlInfo.getIntField(2);
                int attributeLength = (int) xmlInfo.getIntField(3);
                int entityExpansionLimit = (int) xmlInfo.getIntField(4);
                int childrenPerElement = (int) xmlInfo.getIntField(5);
                XMLConfig xmlConfig = new XMLConfig();
                xmlConfig.setName(name);
                xmlConfig.setDtdEnabled(dtdEnabled);
                xmlConfig.setExternalEntitiesEnabled(externalEntitiesEnabled);
                xmlConfig.setMaxDepth(maxXMLDepth);
                xmlConfig.setMaxElementCount(elementCount);
                xmlConfig.setMaxAttributeCount(attributeCount);
                xmlConfig.setMaxAttributeLength(attributeLength);
                xmlConfig.setEntityExpansionLimit(entityExpansionLimit);
                xmlConfig.setMaxChildrenPerElement(childrenPerElement);
                // put into ConfigurationHolder
                ConfigurationHolder.addXmlConfig(xmlPolicyId, xmlConfig);
                break;
            case THREAT_PROTECTION_POLICY_DELETE:
                ConfigurationHolder.removeXmlConfig(xmlPolicyId);
                break;
            default:
                log.warn("Unknown event type for XML Threat Protection Policy. Event: " + event);
                break;
        }
    }
    return getBValues(new BBoolean(true));
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) XMLConfig(org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.XMLConfig) BBoolean(org.ballerinalang.model.values.BBoolean)

Example 3 with BBoolean

use of org.ballerinalang.model.values.BBoolean 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 BBoolean

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

the class ConfigureJsonAnalyzer method execute.

@Override
public BValue[] execute(Context context) {
    String event = getStringArgument(context, 0);
    // configure json analyzer
    BStruct jsonInfo = ((BStruct) getRefArgument(context, 0));
    if (jsonInfo != null) {
        String jsonPolicyId = jsonInfo.getStringField(0);
        switch(event) {
            case THREAT_PROTECTION_POLICY_ADD:
            case THREAT_PROTECTION_POLICY_UPDATE:
                String name = jsonInfo.getStringField(1);
                int propertyCount = (int) jsonInfo.getIntField(0);
                int stringLength = (int) jsonInfo.getIntField(1);
                int arrayElementCount = (int) jsonInfo.getIntField(2);
                int keyLength = (int) jsonInfo.getIntField(3);
                int maxJSONDepth = (int) jsonInfo.getIntField(4);
                JSONConfig jsonConfig = new JSONConfig();
                jsonConfig.setName(name);
                jsonConfig.setMaxPropertyCount(propertyCount);
                jsonConfig.setMaxStringLength(stringLength);
                jsonConfig.setMaxArrayElementCount(arrayElementCount);
                jsonConfig.setMaxKeyLength(keyLength);
                jsonConfig.setMaxJsonDepth(maxJSONDepth);
                // put into ConfigurationHolder
                ConfigurationHolder.addJsonConfig(jsonPolicyId, jsonConfig);
                break;
            case THREAT_PROTECTION_POLICY_DELETE:
                ConfigurationHolder.removeJsonConfig(jsonPolicyId);
                break;
            default:
                log.warn("Unknown event type for Threat Protection Policy. Event: " + event);
                break;
        }
    }
    return getBValues(new BBoolean(true));
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BBoolean(org.ballerinalang.model.values.BBoolean) JSONConfig(org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.JSONConfig)

Example 5 with BBoolean

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

the class CacheTest method testCacheOperations.

@Test
public void testCacheOperations() {
    // Create arguments to initiate cache
    BValue[] args = { new BString("cacheName"), new BString("15"), new BString("cacheKey"), new BString("cacheValue") };
    // Test ballerina cache create, put and get for BString
    BValue[] returns = BLangFunctions.invokeNew(bLangProgram, "testCache", args);
    // Assert if cache entry is BValue
    Assert.assertTrue(returns[0] instanceof BValue);
    final String expected = "cacheValue";
    // Assert if return entry matched with exact entry we put there
    Assert.assertEquals(returns[0].stringValue(), expected);
    // Test ballerina Boolean values.
    BValue[] argsBoolean = { new BString("cacheName"), new BString("15"), new BString("cacheKey"), new BBoolean(false) };
    BValue[] returnsBoolean = BLangFunctions.invokeNew(bLangProgram, "testCache", argsBoolean);
    Assert.assertTrue(returnsBoolean[0] instanceof BBoolean);
    // Test ballerina JSON values.
    BValue[] argsJSON = { new BString("cacheName"), new BString("15"), new BString("cacheKey"), new BJSON("{}") };
    BValue[] returnsJSON = BLangFunctions.invokeNew(bLangProgram, "testCache", argsJSON);
    Assert.assertTrue(returnsJSON[0] instanceof BJSON);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BBoolean(org.ballerinalang.model.values.BBoolean) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Aggregations

BBoolean (org.ballerinalang.model.values.BBoolean)6 BString (org.ballerinalang.model.values.BString)4 BValue (org.ballerinalang.model.values.BValue)3 Test (org.testng.annotations.Test)3 BJSON (org.ballerinalang.model.values.BJSON)2 BStruct (org.ballerinalang.model.values.BStruct)2 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)1 APIMThreatAnalyzer (org.wso2.carbon.apimgt.ballerina.threatprotection.analyzer.APIMThreatAnalyzer)1 JSONConfig (org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.JSONConfig)1 XMLConfig (org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.XMLConfig)1