Search in sources :

Example 1 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project carbon-apimgt by wso2.

the class EntityDAO method getLastUpdatedTimeOfResourceByName.

/**
 * Returns the last access time of the given entity identified by the NAME field.
 *
 * @param resourceTableName Table name of the entity
 * @param name value in the NAME field of the entity
 * @return Last access time of the requested resource
 * @throws APIMgtDAOException
 */
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
static String getLastUpdatedTimeOfResourceByName(String resourceTableName, String name) throws APIMgtDAOException {
    final String query = "SELECT LAST_UPDATED_TIME FROM " + resourceTableName + " WHERE NAME = ?";
    String lastUpdatedTime = null;
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, name);
        try (ResultSet rs = statement.executeQuery()) {
            if (rs.next()) {
                lastUpdatedTime = rs.getString("LAST_UPDATED_TIME");
            }
        }
        return lastUpdatedTime;
    } catch (SQLException e) {
        throw new APIMgtDAOException("Error while retrieving last access time from table : " + resourceTableName + " and entity " + name, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project carbon-apimgt by wso2.

the class ApplicationMappingUtil method fromDTOtoApplication.

public static Application fromDTOtoApplication(ApplicationDTO applicationDTO, String createdUser) {
    // subscriber field of the body is not honored
    Application application = new Application(applicationDTO.getName(), createdUser);
    application.setPolicy(new ApplicationPolicy(applicationDTO.getThrottlingTier()));
    application.setDescription(applicationDTO.getDescription());
    application.setId(applicationDTO.getApplicationId());
    application.setPermissionString(applicationDTO.getPermission());
    application.setStatus(applicationDTO.getLifeCycleStatus());
    // application.setGroupId(applicationDTO.getGroupId());
    return application;
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 3 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project carbon-apimgt by wso2.

the class APIPublisherImpl method replaceGroupNamesWithId.

/**
 * This method replaces the groupId field's value to the role id instead of the name passed by the user
 *
 * @param permissionString - the permission json string which contains role names in groupId field
 * @return permission string with replaced groupId
 * @throws ParseException         - if there is an error parsing the json string
 * @throws APIManagementException - if there is an error getting the IdentityProvider instance
 */
private String replaceGroupNamesWithId(String permissionString) throws ParseException, APIManagementException {
    JSONArray updatedPermissionArray = new JSONArray();
    JSONParser jsonParser = new JSONParser();
    JSONArray originalPermissionArray = (JSONArray) jsonParser.parse(permissionString);
    try {
        for (Object permissionObj : originalPermissionArray) {
            JSONObject jsonObject = (JSONObject) permissionObj;
            String groupName = (String) jsonObject.get(APIMgtConstants.Permission.GROUP_ID);
            String groupId = getIdentityProvider().getRoleId(groupName);
            JSONObject updatedPermissionJsonObj = new JSONObject();
            updatedPermissionJsonObj.put(APIMgtConstants.Permission.GROUP_ID, groupId);
            updatedPermissionJsonObj.put(APIMgtConstants.Permission.PERMISSION, jsonObject.get(APIMgtConstants.Permission.PERMISSION));
            updatedPermissionArray.add(updatedPermissionJsonObj);
        }
    } catch (IdentityProviderException e) {
        String errorMessage = "There are invalid roles in the permission string";
        log.error(errorMessage, e);
        throw new APIManagementException(errorMessage, e, ExceptionCodes.UNSUPPORTED_ROLE);
    }
    return updatedPermissionArray.toJSONString();
}
Also used : JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException)

Example 4 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project carbon-apimgt by wso2.

the class APIPublisherImpl method replaceGroupIdWithName.

/**
 * This method replaces the groupId field's value of the api permissions string to the role name before sending to
 * frontend
 *
 * @param permissionString - permissions string containing role ids in the groupId field
 * @return the permission string replacing the groupId field's value to role name
 * @throws ParseException         - if there is an error parsing the permission json
 * @throws APIManagementException - if there is an error getting the IdentityProvider instance
 */
private String replaceGroupIdWithName(String permissionString) throws ParseException, APIManagementException {
    JSONArray updatedPermissionArray = new JSONArray();
    JSONParser jsonParser = new JSONParser();
    JSONArray originalPermissionArray = (JSONArray) jsonParser.parse(permissionString);
    for (Object permissionObj : originalPermissionArray) {
        JSONObject jsonObject = (JSONObject) permissionObj;
        String groupId = (String) jsonObject.get(APIMgtConstants.Permission.GROUP_ID);
        try {
            String groupName = getIdentityProvider().getRoleName(groupId);
            JSONObject updatedPermissionJsonObj = new JSONObject();
            updatedPermissionJsonObj.put(APIMgtConstants.Permission.GROUP_ID, groupName);
            updatedPermissionJsonObj.put(APIMgtConstants.Permission.PERMISSION, jsonObject.get(APIMgtConstants.Permission.PERMISSION));
            updatedPermissionArray.add(updatedPermissionJsonObj);
        } catch (IdentityProviderException e) {
            // lets the execution continue after logging the exception
            String errorMessage = "Error occurred while calling SCIM endpoint to retrieve role name of role " + "with Id " + groupId;
            log.warn(errorMessage, e);
        }
    }
    return updatedPermissionArray.toJSONString();
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException)

Example 5 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.

the class Generator method fieldAnnotation.

/**
 * Get description annotation of the field.
 * @param node parent node.
 * @param param field.
 * @return description of the field.
 */
private static String fieldAnnotation(BLangNode node, BLangNode param) {
    String subName = "";
    if (param instanceof BLangVariable) {
        BLangVariable paramVariable = (BLangVariable) param;
        subName = (paramVariable.getName() == null) ? paramVariable.type.tsymbol.name.value : paramVariable.getName().getValue();
    } else if (param instanceof BLangEnum.Enumerator) {
        BLangEnum.Enumerator paramEnumVal = (BLangEnum.Enumerator) param;
        subName = paramEnumVal.getName().getValue();
    }
    for (AnnotationAttachmentNode annotation : getAnnotationAttachments(node)) {
        BLangRecordLiteral bLangRecordLiteral = (BLangRecordLiteral) annotation.getExpression();
        if (bLangRecordLiteral.getKeyValuePairs().size() != 1) {
            continue;
        }
        BLangExpression bLangLiteral = bLangRecordLiteral.getKeyValuePairs().get(0).getValue();
        String attribVal = bLangLiteral.toString();
        if (annotation.getAnnotationName().getValue().equals("Field") && attribVal.startsWith(subName + ":")) {
            return attribVal.split(subName + ":")[1].trim();
        }
    }
    // annotation's value
    for (AnnotationAttachmentNode annotation : getAnnotationAttachments(node)) {
        BLangRecordLiteral bLangRecordLiteral = (BLangRecordLiteral) annotation.getExpression();
        if (bLangRecordLiteral.getKeyValuePairs().size() != 1) {
            continue;
        }
        if (annotation.getAnnotationName().getValue().equals("Field")) {
            BLangExpression bLangLiteral = bLangRecordLiteral.getKeyValuePairs().get(0).getValue();
            return bLangLiteral.toString();
        }
    }
    return "";
}
Also used : BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Aggregations

BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)14 Test (org.testng.annotations.Test)11 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)10 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)10 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)10 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)9 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)8 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)8 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)8 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)8 Name (org.wso2.ballerinalang.compiler.util.Name)8 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)7 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)7 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)7 CompileResult (org.ballerinalang.launcher.util.CompileResult)6 PackageNode (org.ballerinalang.model.tree.PackageNode)6 BLangDocumentation (org.wso2.ballerinalang.compiler.tree.BLangDocumentation)6 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)5 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)5 BLangEnum (org.wso2.ballerinalang.compiler.tree.BLangEnum)5