Search in sources :

Example 1 with BStruct

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

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

the class BatchUpdate method execute.

@Override
public void execute(Context context) {
    try {
        BStruct bConnector = (BStruct) context.getRefArgument(0);
        String query = context.getStringArgument(0);
        BRefValueArray parameters = (BRefValueArray) context.getNullableRefArgument(1);
        SQLDatasource datasource = (SQLDatasource) bConnector.getNativeData(Constants.CLIENT_CONNECTOR);
        Map<String, String> tags = new HashMap<>();
        tags.put(TAG_KEY_DB_STATEMENT, query);
        tags.put(TAG_KEY_DB_TYPE, TAG_DB_TYPE_SQL);
        TraceUtil.getTracer(context.getParentWorkerExecutionContext()).addTags(tags);
        executeBatchUpdate(context, datasource, query, parameters);
    } catch (Throwable e) {
        context.setReturnValues(SQLDatasourceUtils.getSQLConnectorError(context, e));
        SQLDatasourceUtils.handleErrorOnTransaction(context);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HashMap(java.util.HashMap) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) SQLDatasource(org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource)

Example 3 with BStruct

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

the class SQLDatasourceUtils method getSQLConnectorError.

public static BStruct getSQLConnectorError(Context context, Throwable throwable) {
    PackageInfo sqlPackageInfo = context.getProgramFile().getPackageInfo(Constants.SQL_PACKAGE_PATH);
    StructInfo errorStructInfo = sqlPackageInfo.getStructInfo(Constants.SQL_CONNECTOR_ERROR);
    BStruct sqlConnectorError = new BStruct(errorStructInfo.getType());
    if (throwable.getMessage() == null) {
        sqlConnectorError.setStringField(0, Constants.SQL_EXCEPTION_OCCURED);
    } else {
        sqlConnectorError.setStringField(0, throwable.getMessage());
    }
    return sqlConnectorError;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Example 4 with BStruct

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

the class Update method execute.

@Override
public void execute(Context context) {
    try {
        BStruct bConnector = (BStruct) context.getRefArgument(0);
        String query = context.getStringArgument(0);
        BRefValueArray parameters = (BRefValueArray) context.getNullableRefArgument(1);
        SQLDatasource datasource = (SQLDatasource) bConnector.getNativeData(Constants.CLIENT_CONNECTOR);
        Map<String, String> tags = new HashMap<>();
        tags.put(TAG_KEY_DB_STATEMENT, query);
        tags.put(TAG_KEY_DB_TYPE, TAG_DB_TYPE_SQL);
        TraceUtil.getTracer(context.getParentWorkerExecutionContext()).addTags(tags);
        executeUpdate(context, datasource, query, parameters);
    } catch (Throwable e) {
        context.setReturnValues(SQLDatasourceUtils.getSQLConnectorError(context, e));
        SQLDatasourceUtils.handleErrorOnTransaction(context);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HashMap(java.util.HashMap) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) SQLDatasource(org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource)

Example 5 with BStruct

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

the class GetClient method execute.

@Override
public void execute(Context context) {
    BStruct clientEndPoint = (BStruct) context.getRefArgument(0);
    BStruct clientConnector = (BStruct) clientEndPoint.getNativeData(Constants.B_CONNECTOR);
    context.setReturnValues(clientConnector);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct)

Aggregations

BStruct (org.ballerinalang.model.values.BStruct)460 BValue (org.ballerinalang.model.values.BValue)187 Test (org.testng.annotations.Test)161 BString (org.ballerinalang.model.values.BString)131 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)53 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)39 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)37 BInteger (org.ballerinalang.model.values.BInteger)33 BMap (org.ballerinalang.model.values.BMap)29 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)26 BStructType (org.ballerinalang.model.types.BStructType)25 IOException (java.io.IOException)23 BBoolean (org.ballerinalang.model.values.BBoolean)23 BJSON (org.ballerinalang.model.values.BJSON)22 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)21 StructInfo (org.ballerinalang.util.codegen.StructInfo)21 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)20 File (java.io.File)17 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)17 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)16