Java Code Examples for org.wso2.carbon.identity.application.common.util.IdentityApplicationManagementUtil#closeResultSet()

The following examples show how to use org.wso2.carbon.identity.application.common.util.IdentityApplicationManagementUtil#closeResultSet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProvisioningManagementDAO.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
/**
 * @param dbConnection
 * @param idPName
 * @param tenantId
 * @return
 * @throws SQLException
 * @throws IdentityApplicationManagementException
 */
private int getIdentityProviderIdentifier(Connection dbConnection, String idPName, int tenantId)
        throws SQLException,
        IdentityApplicationManagementException {

    String sqlStmt;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    try {
        sqlStmt = IdPManagementConstants.SQLQueries.GET_IDP_ID_BY_NAME_SQL;
        prepStmt = dbConnection.prepareStatement(sqlStmt);
        prepStmt.setInt(1, tenantId);
        prepStmt.setString(2, idPName);
        rs = prepStmt.executeQuery();
        if (rs.next()) {
            return rs.getInt(1);
        } else {
            throw new IdentityApplicationManagementException("Invalid Identity Provider Name " +
                    idPName);
        }
    } finally {
        IdentityApplicationManagementUtil.closeResultSet(rs);
        IdentityApplicationManagementUtil.closeStatement(prepStmt);
    }
}
 
Example 2
Source File: ProvisioningManagementDAO.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * @param dbConnection
 * @param idPName
 * @param tenantId
 * @return
 * @throws SQLException
 * @throws IdentityApplicationManagementException
 */
private int getIdentityProviderIdentifier(Connection dbConnection, String idPName, int tenantId)
        throws SQLException,
        IdentityApplicationManagementException {

    String sqlStmt = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    try {
        sqlStmt = IdPManagementConstants.SQLQueries.GET_IDP_ID_BY_NAME_SQL;
        prepStmt = dbConnection.prepareStatement(sqlStmt);
        prepStmt.setInt(1, tenantId);
        prepStmt.setString(2, idPName);
        rs = prepStmt.executeQuery();
        if (rs.next()) {
            return rs.getInt(1);
        } else {
            throw new IdentityApplicationManagementException("Invalid Identity Provider Name " +
                    idPName);
        }
    } finally {
        IdentityApplicationManagementUtil.closeResultSet(rs);
        IdentityApplicationManagementUtil.closeStatement(prepStmt);
    }
}
 
Example 3
Source File: ProvisioningManagementDAO.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * @param dbConnection
 * @param idPId
 * @param connectorType
 * @return
 * @throws SQLException
 * @throws IdentityApplicationManagementException
 */
private int getProvisioningConfigurationIdentifier(Connection dbConnection, int idPId,
                                                   String connectorType) throws SQLException,
        IdentityApplicationManagementException {

    String sqlStmt = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    try {
        sqlStmt = IdentityProvisioningConstants.SQLQueries.GET_IDP_PROVISIONING_CONFIG_ID_SQL;
        prepStmt = dbConnection.prepareStatement(sqlStmt);
        prepStmt.setInt(1, idPId);
        prepStmt.setString(2, connectorType);
        rs = prepStmt.executeQuery();
        if (rs.next()) {
            return rs.getInt(1);
        } else {
            throw new IdentityApplicationManagementException("Invalid connector type " +
                    connectorType);
        }
    } finally {
        IdentityApplicationManagementUtil.closeResultSet(rs);
        IdentityApplicationManagementUtil.closeStatement(prepStmt);
    }
}
 
Example 4
Source File: ApplicationDAOImpl.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Get Service provider properties
 * @param dbConnection database connection
 * @param SpId SP Id
 * @return service provider properties
 */
private List<ServiceProviderProperty> getServicePropertiesBySpId(Connection dbConnection, int SpId)
        throws SQLException {

    String sqlStmt = ApplicationMgtDBQueries.GET_SP_METADATA_BY_SP_ID;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    List<ServiceProviderProperty> idpProperties = new ArrayList<ServiceProviderProperty>();
    try {
        prepStmt = dbConnection.prepareStatement(sqlStmt);
        prepStmt.setInt(1, SpId);
        rs = prepStmt.executeQuery();
        while (rs.next()) {
            ServiceProviderProperty property = new ServiceProviderProperty();
            property.setName(rs.getString("NAME"));
            property.setValue(rs.getString("VALUE"));
            property.setDisplayName(rs.getString("DISPLAY_NAME"));
            idpProperties.add(property);
        }
    } finally {
        IdentityApplicationManagementUtil.closeStatement(prepStmt);
        IdentityApplicationManagementUtil.closeResultSet(rs);
    }
    return idpProperties;
}
 
Example 5
Source File: ApplicationDAOImpl.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * @param applicationid
 * @param connection
 * @return
 * @throws SQLException
 */
private String getAuthenticationType(int applicationid, Connection connection)
        throws SQLException {

    int tenantID = CarbonContext.getThreadLocalCarbonContext().getTenantId();

    PreparedStatement authTypeStmt = null;
    ResultSet authTypeResultSet = null;
    try {
        authTypeStmt = connection
                .prepareStatement(ApplicationMgtDBQueries.LOAD_AUTH_TYPE_BY_APP_ID);
        authTypeStmt.setInt(1, applicationid);
        authTypeStmt.setInt(2, tenantID);
        authTypeResultSet = authTypeStmt.executeQuery();

        if (authTypeResultSet.next()) {
            return authTypeResultSet.getString(1);
        }

        return ApplicationConstants.AUTH_TYPE_DEFAULT;

    } finally {
        IdentityApplicationManagementUtil.closeResultSet(authTypeResultSet);
        IdentityApplicationManagementUtil.closeStatement(authTypeStmt);
    }

}
 
Example 6
Source File: ApplicationDAOImpl.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * @param applicationId
 * @param connection
 * @return
 * @throws IdentityApplicationManagementException
 */
private RequestPathAuthenticatorConfig[] getRequestPathAuthenticators(int applicationId,
                                                                      Connection connection, int tenantID) throws IdentityApplicationManagementException {

    PreparedStatement loadReqPathAuthenticators = null;
    ResultSet authResultSet = null;
    List<RequestPathAuthenticatorConfig> authenticators = new ArrayList<RequestPathAuthenticatorConfig>();

    try {
        loadReqPathAuthenticators = connection
                .prepareStatement(ApplicationMgtDBQueries.LOAD_REQ_PATH_AUTHENTICATORS_BY_APP_ID);
        loadReqPathAuthenticators.setInt(1, applicationId);
        loadReqPathAuthenticators.setInt(2, tenantID);
        authResultSet = loadReqPathAuthenticators.executeQuery();

        while (authResultSet.next()) {
            RequestPathAuthenticatorConfig reqAuth = new RequestPathAuthenticatorConfig();
            reqAuth.setName(authResultSet.getString(1));
            authenticators.add(reqAuth);
        }
    } catch (SQLException e) {
        throw new IdentityApplicationManagementException(
                "Error while retrieving all application");
    } finally {
        IdentityApplicationManagementUtil.closeStatement(loadReqPathAuthenticators);
        IdentityApplicationManagementUtil.closeResultSet(authResultSet);
    }

    return authenticators.toArray(new RequestPathAuthenticatorConfig[authenticators.size()]);
}
 
Example 7
Source File: UserAccountAssociationDAO.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Check if logged user can be associated with a given user
 *
 * @param domainName  User store domain of user
 * @param tenantId  Tenant ID of user
 * @param userName  User name
 * @return
 * @throws UserAccountAssociationException
 */
public boolean isValidUserAssociation(String domainName, int tenantId,
                                      String userName) throws UserAccountAssociationException {

    Connection dbConnection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    boolean valid = false;

    try {
        preparedStatement = dbConnection.prepareStatement(UserAccountAssociationConstants
                .SQLQueries.IS_VALID_ASSOCIATION);

        preparedStatement.setInt(1, tenantId);
        preparedStatement.setString(2, domainName);
        preparedStatement.setString(3, userName);
        preparedStatement.setInt(4, CarbonContext.getThreadLocalCarbonContext().getTenantId());
        preparedStatement.setString(5, IdentityUtil.extractDomainFromName(CarbonContext
                .getThreadLocalCarbonContext()
                .getUsername()));
        preparedStatement.setString(6, UserAccountAssociationUtil.getUsernameWithoutDomain(CarbonContext
                .getThreadLocalCarbonContext().getUsername()));
        resultSet = preparedStatement.executeQuery();

        if (resultSet.next()) {
            valid = resultSet.getInt(1) > 0;
        }
        dbConnection.commit();
    } catch (SQLException e) {
        throw new UserAccountAssociationServerException(UserAccountAssociationConstants.ErrorMessages
                .CHECK_ASSOCIATION_DB_ERROR.getDescription(), e);
    } finally {
        IdentityApplicationManagementUtil.closeResultSet(resultSet);
        IdentityApplicationManagementUtil.closeStatement(preparedStatement);
        IdentityApplicationManagementUtil.closeConnection(dbConnection);
    }

    return valid;
}
 
Example 8
Source File: ApplicationDAOImpl.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the application ID for a given application name
 *
 * @param applicationName
 * @param tenantID
 * @param connection
 * @return
 * @throws IdentityApplicationManagementException
 */
private int getApplicationIDByName(String applicationName, int tenantID, Connection connection)
        throws IdentityApplicationManagementException {

    int applicationId = 0;
    PreparedStatement getAppIDPrepStmt = null;
    ResultSet appidResult = null;

    try {
        getAppIDPrepStmt = connection
                .prepareStatement(ApplicationMgtDBQueries.LOAD_APP_ID_BY_APP_NAME);
        getAppIDPrepStmt.setString(1, applicationName);
        getAppIDPrepStmt.setInt(2, tenantID);
        appidResult = getAppIDPrepStmt.executeQuery();

        if (!connection.getAutoCommit()) {
            connection.commit();
        }

        if (appidResult.next()) {
            applicationId = appidResult.getInt(1);
        }

    } catch (SQLException e) {
        IdentityApplicationManagementUtil.closeConnection(connection);
        log.error("Error in storing the application", e);
        throw new IdentityApplicationManagementException("Error while storing application", e);
    } finally {
        IdentityApplicationManagementUtil.closeResultSet(appidResult);
        IdentityApplicationManagementUtil.closeStatement(getAppIDPrepStmt);
    }

    return applicationId;
}
 
Example 9
Source File: ApplicationDAOImpl.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Reads back the basic application data
 *
 * @param applicationID
 * @param connection
 * @return
 * @throws IdentityApplicationManagementException
 */
private String getApplicationName(int applicationID, Connection connection) throws SQLException {

    int tenantID = CarbonContext.getThreadLocalCarbonContext().getTenantId();

    if (log.isDebugEnabled()) {
        log.debug("Loading Application Name for ID: " + applicationID);
    }

    PreparedStatement loadBasicAppInfoStmt = null;
    ResultSet appNameResultSet = null;
    String applicationName = null;

    try {
        loadBasicAppInfoStmt = connection
                .prepareStatement(ApplicationMgtDBQueries.LOAD_APP_NAME_BY_APP_ID);
        loadBasicAppInfoStmt.setInt(1, applicationID);
        loadBasicAppInfoStmt.setInt(2, tenantID);
        appNameResultSet = loadBasicAppInfoStmt.executeQuery();

        if (appNameResultSet.next()) {
            applicationName = appNameResultSet.getString(1);
        }

        if (log.isDebugEnabled()) {
            log.debug("ApplicationName : " + applicationName);
        }
        return applicationName;

    } finally {
        IdentityApplicationManagementUtil.closeResultSet(appNameResultSet);
        IdentityApplicationManagementUtil.closeStatement(loadBasicAppInfoStmt);
    }
}
 
Example 10
Source File: UserAccountAssociationDAO.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if two user accounts can be popupated or not
 *
 * @param domainName1 user store domain of account 1
 * @param tenantId1   tenant id of account 1
 * @param userName1   username of account 1
 * @param domainName2 user store domain of account 2
 * @param tenantId2   tenant id of account 2
 * @param userName2   username of account 2
 * @return
 * @throws UserAccountAssociationException
 */
public boolean isValidUserAssociation(String domainName1, int tenantId1, String userName1, String domainName2,
                                      int tenantId2, String userName2) throws UserAccountAssociationException {

    Connection dbConnection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    boolean valid = false;

    try {
        preparedStatement = dbConnection.prepareStatement(UserAccountAssociationConstants
                .SQLQueries.IS_VALID_ASSOCIATION);

        preparedStatement.setInt(1, tenantId1);
        preparedStatement.setString(2, domainName1);
        preparedStatement.setString(3, userName1);
        preparedStatement.setInt(4, tenantId2);
        preparedStatement.setString(5, domainName2);
        preparedStatement.setString(6, userName2);
        resultSet = preparedStatement.executeQuery();

        if (resultSet.next()) {
            valid = resultSet.getInt(1) > 0;
        }
        dbConnection.commit();
    } catch (SQLException e) {
        throw new UserAccountAssociationServerException(UserAccountAssociationConstants.ErrorMessages
                .CHECK_ASSOCIATION_DB_ERROR.getDescription(), e);
    } finally {
        IdentityApplicationManagementUtil.closeResultSet(resultSet);
        IdentityApplicationManagementUtil.closeStatement(preparedStatement);
        IdentityApplicationManagementUtil.closeConnection(dbConnection);
    }

    return valid;
}
 
Example 11
Source File: ProvisioningManagementDAO.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * @param dbConnection
 * @param idpName
 * @param tenantId
 * @return
 * @throws SQLException
 * @throws IdentityApplicationManagementException
 */
private int getIdentityProviderIdByName(Connection dbConnection, String idpName, int tenantId)
        throws SQLException,
        IdentityApplicationManagementException {

    boolean dbConnInitialized = true;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    if (dbConnection == null) {
        dbConnection = IdentityDatabaseUtil.getDBConnection(false);
    } else {
        dbConnInitialized = false;
    }
    try {
        String sqlStmt = IdPManagementConstants.SQLQueries.GET_IDP_ROW_ID_SQL;
        prepStmt = dbConnection.prepareStatement(sqlStmt);
        prepStmt.setInt(1, tenantId);
        prepStmt.setString(2, idpName);
        rs = prepStmt.executeQuery();
        if (rs.next()) {
            return rs.getInt(1);
        }
    } finally {
        IdentityApplicationManagementUtil.closeStatement(prepStmt);
        IdentityApplicationManagementUtil.closeResultSet(rs);
        if (dbConnInitialized) {
            IdentityApplicationManagementUtil.closeConnection(dbConnection);
        }
    }
    return 0;
}
 
Example 12
Source File: ProvisioningManagementDAO.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * @param dbConnection
 * @param idpName
 * @param tenantId
 * @return
 * @throws SQLException
 * @throws IdentityApplicationManagementException
 */
private int getIdentityProviderIdByName(Connection dbConnection, String idpName, int tenantId)
        throws SQLException,
        IdentityApplicationManagementException {

    boolean dbConnInitialized = true;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    if (dbConnection == null) {
        dbConnection = IdentityDatabaseUtil.getDBConnection();
    } else {
        dbConnInitialized = false;
    }
    try {
        String sqlStmt = IdPManagementConstants.SQLQueries.GET_IDP_ROW_ID_SQL;
        prepStmt = dbConnection.prepareStatement(sqlStmt);
        prepStmt.setInt(1, tenantId);
        prepStmt.setString(2, idpName);
        rs = prepStmt.executeQuery();
        dbConnection.commit();
        if (rs.next()) {
            return rs.getInt(1);
        }
    } finally {
        IdentityApplicationManagementUtil.closeStatement(prepStmt);
        IdentityApplicationManagementUtil.closeResultSet(rs);
        if (dbConnInitialized) {
            IdentityApplicationManagementUtil.closeConnection(dbConnection);
        }
    }
    return 0;
}
 
Example 13
Source File: ProvisioningManagementDAO.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
public List<String> getSPNamesOfProvisioningConnectorsByIDP(String idPName, int tenantId)
        throws IdentityApplicationManagementException {
    Connection dbConnection = IdentityDatabaseUtil.getDBConnection(false);
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    List<String> spNames = new ArrayList<String>();
    try {
        String sqlStmt = IdentityProvisioningConstants.SQLQueries.GET_SP_NAMES_OF_PROVISIONING_CONNECTORS_BY_IDP;
        prepStmt = dbConnection.prepareStatement(sqlStmt);
        prepStmt.setString(1, idPName);
        prepStmt.setInt(2, tenantId);
        rs = prepStmt.executeQuery();

        while (rs.next()) {
            spNames.add(rs.getString(1));
        }
    } catch (SQLException e) {
        String msg = "Error occurred while retrieving SP names of provisioning connectors by IDP name";
        throw new IdentityApplicationManagementException(msg, e);
    } finally {
        if (prepStmt != null) {
            IdentityApplicationManagementUtil.closeStatement(prepStmt);
        }
        if (rs != null) {
            IdentityApplicationManagementUtil.closeResultSet(rs);
        }
        IdentityApplicationManagementUtil.closeConnection(dbConnection);
    }
    return spNames;
}
 
Example 14
Source File: UserAccountAssociationDAO.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve association key of a user
 *
 * @param domainName  User store domain of user
 * @param tenantId  Tenant ID of user
 * @param userName  User name
 * @return
 * @throws UserAccountAssociationException
 */
public String getAssociationKeyOfUser(String domainName, int tenantId,
                                      String userName) throws UserAccountAssociationException {

    Connection dbConnection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    String associationKey = null;

    try {
        preparedStatement = dbConnection.prepareStatement(UserAccountAssociationConstants
                .SQLQueries.GET_ASSOCIATION_KEY_OF_USER);

        preparedStatement.setInt(1, tenantId);
        preparedStatement.setString(2, domainName);
        preparedStatement.setString(3, userName);
        resultSet = preparedStatement.executeQuery();

        if (resultSet.next()) {
            associationKey = resultSet.getString(1);
        }
        dbConnection.commit();
    } catch (SQLException e) {
        throw new UserAccountAssociationServerException(UserAccountAssociationConstants.ErrorMessages
                .ERROR_WHILE_RETRIEVING_ASSOC_KEY.getDescription
                        (), e);
    } finally {
        IdentityApplicationManagementUtil.closeResultSet(resultSet);
        IdentityApplicationManagementUtil.closeStatement(preparedStatement);
        IdentityApplicationManagementUtil.closeConnection(dbConnection);
    }
    return associationKey;
}
 
Example 15
Source File: ApplicationDAOImpl.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Stores basic application information and meta-data such as the application name, creator and
 * tenant.
 *
 * @param serviceProvider
 * @throws IdentityApplicationManagementException
 */
@Override
public int createApplication(ServiceProvider serviceProvider, String tenantDomain)
        throws IdentityApplicationManagementException {

    // get logged-in users tenant identifier.
    int tenantID = MultitenantConstants.INVALID_TENANT_ID;

    if (tenantDomain != null) {
        tenantID = IdentityTenantUtil.getTenantId(tenantDomain);
    }

    String qualifiedUsername = CarbonContext.getThreadLocalCarbonContext().getUsername();
    if (ApplicationConstants.LOCAL_SP.equals(serviceProvider.getApplicationName())) {
        qualifiedUsername = CarbonConstants.REGISTRY_SYSTEM_USERNAME;
    }
    String username = UserCoreUtil.removeDomainFromName(qualifiedUsername);
    String userStoreDomain = IdentityUtil.extractDomainFromName(qualifiedUsername);
    String applicationName = serviceProvider.getApplicationName();
    String description = serviceProvider.getDescription();

    if (log.isDebugEnabled()) {
        log.debug("Creating Application " + applicationName + " for user " + qualifiedUsername);
    }

    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement storeAppPrepStmt = null;
    ResultSet results = null;

    try {
        String dbProductName = connection.getMetaData().getDatabaseProductName();
        storeAppPrepStmt = connection.prepareStatement(
                ApplicationMgtDBQueries.STORE_BASIC_APPINFO, new String[]{
                        DBUtils.getConvertedAutoGeneratedColumnName(dbProductName, "ID")});

        // TENANT_ID, APP_NAME, USER_STORE, USERNAME, DESCRIPTION, AUTH_TYPE
        storeAppPrepStmt.setInt(1, tenantID);
        storeAppPrepStmt.setString(2, applicationName);
        storeAppPrepStmt.setString(3, userStoreDomain);
        storeAppPrepStmt.setString(4, username);
        storeAppPrepStmt.setString(5, description);
        // by default authentication type would be default.
        // default authenticator is defined system-wide - in the configuration file.
        storeAppPrepStmt.setString(6, ApplicationConstants.AUTH_TYPE_DEFAULT);
        storeAppPrepStmt.execute();

        results = storeAppPrepStmt.getGeneratedKeys();

        if (!connection.getAutoCommit()) {
            connection.commit();
        }

        int applicationId = 0;
        if (results.next()) {
            applicationId = results.getInt(1);
        }
        // some JDBC Drivers returns this in the result, some don't
        if (applicationId == 0) {
            if (log.isDebugEnabled()) {
                log.debug("JDBC Driver did not return the application id, executing Select operation");
            }
            applicationId = getApplicationIDByName(applicationName, tenantID, connection);
        }

        if (serviceProvider.getSpProperties() != null) {
            addServiceProviderProperties(connection, applicationId,
                    Arrays.asList(serviceProvider.getSpProperties()), tenantID);
        }

        if (log.isDebugEnabled()) {
            log.debug("Application Stored successfully with application id " + applicationId);
        }

        return applicationId;

    } catch (SQLException e) {
        try {
            if (connection != null) {
                connection.rollback();
            }
        } catch (SQLException sql) {
            throw new IdentityApplicationManagementException(
                    "Error while Creating Application", sql);
        }
        throw new IdentityApplicationManagementException("Error while Creating Application", e);
    } finally {
        IdentityApplicationManagementUtil.closeResultSet(results);
        IdentityApplicationManagementUtil.closeStatement(storeAppPrepStmt);
        IdentityApplicationManagementUtil.closeConnection(connection);
    }
}
 
Example 16
Source File: ApplicationDAOImpl.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
@Override
public List<String> getAllRequestedClaimsByServiceProvider(String serviceProviderName,
                                                           String tenantDomain) throws IdentityApplicationManagementException {
    int tenantID = -123;

    if (tenantDomain != null) {
        try {
            tenantID = ApplicationManagementServiceComponentHolder.getInstance().getRealmService()
                    .getTenantManager().getTenantId(tenantDomain);
        } catch (UserStoreException e1) {
            throw new IdentityApplicationManagementException("Error while reading application");
        }
    }
    List<String> reqClaimUris = new ArrayList<String>();

    if (log.isDebugEnabled()) {
        log.debug("Reading Claim Mappings of Application " + serviceProviderName);
    }

    PreparedStatement getClaimPreStmt = null;
    ResultSet resultSet = null;
    Connection connection = IdentityDatabaseUtil.getDBConnection();
    try {

        getClaimPreStmt = connection
                .prepareStatement(ApplicationMgtDBQueries.LOAD_CLAIM_MAPPING_BY_APP_NAME);

        // IDP_CLAIM, SP_CLAIM, IS_REQUESTED
        getClaimPreStmt.setString(1, serviceProviderName);
        getClaimPreStmt.setInt(2, tenantID);
        resultSet = getClaimPreStmt.executeQuery();
        while (resultSet.next()) {
            if ("1".equalsIgnoreCase(resultSet.getString(3))) {
                reqClaimUris.add(resultSet.getString(1));
            }
        }
        connection.commit();
    } catch (SQLException e) {
        throw new IdentityApplicationManagementException(
                "Error while retrieving requested claims", e);
    } finally {
        IdentityApplicationManagementUtil.closeStatement(getClaimPreStmt);
        IdentityApplicationManagementUtil.closeResultSet(resultSet);
        IdentityApplicationManagementUtil.closeConnection(connection);
    }
    return reqClaimUris;

}
 
Example 17
Source File: ApplicationDAOImpl.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Reads the claim mappings for a given appID
 *
 * @param applicationId
 * @param connection
 * @return
 * @throws IdentityApplicationManagementException
 */
private List<RoleMapping> getRoleMappingOfApplication(int applicationId, Connection connection,
                                                      int tenantID) throws IdentityApplicationManagementException {

    ArrayList<RoleMapping> roleMappingList = new ArrayList<RoleMapping>();

    if (log.isDebugEnabled()) {
        log.debug("Reading Role Mapping of Application " + applicationId);
    }

    PreparedStatement getClientInfo = null;
    ResultSet resultSet = null;
    try {
        getClientInfo = connection
                .prepareStatement(ApplicationMgtDBQueries.LOAD_ROLE_MAPPING_BY_APP_ID);
        // IDP_ROLE, SP_ROLE
        getClientInfo.setInt(1, applicationId);
        getClientInfo.setInt(2, tenantID);
        resultSet = getClientInfo.executeQuery();

        while (resultSet.next()) {
            RoleMapping roleMapping = new RoleMapping();
            LocalRole localRole = new LocalRole();
            localRole.setLocalRoleName(resultSet.getString(1));
            roleMapping.setLocalRole(localRole);
            roleMapping.setRemoteRole(resultSet.getString(2));
            roleMappingList.add(roleMapping);

            if (log.isDebugEnabled()) {
                log.debug("Local Role: " + roleMapping.getLocalRole().getLocalRoleName()
                        + " SPRole: " + roleMapping.getRemoteRole());
            }
        }

    } catch (SQLException e) {
        throw new IdentityApplicationManagementException(
                "Error while retrieving all application");
    } finally {
        IdentityApplicationManagementUtil.closeStatement(getClientInfo);
        IdentityApplicationManagementUtil.closeResultSet(resultSet);
    }
    return roleMappingList;
}
 
Example 18
Source File: ApplicationDAOImpl.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Get application Names for user
 *
 * @return
 * @throws IdentityApplicationManagementException
 */
public ApplicationBasicInfo[] getAllApplicationBasicInfo()
        throws IdentityApplicationManagementException {

    int tenantID = CarbonContext.getThreadLocalCarbonContext().getTenantId();

    if (log.isDebugEnabled()) {
        log.debug("Reading all Applications of Tenant " + tenantID);
    }

    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement getAppNamesStmt = null;
    ResultSet appNameResultSet = null;

    ArrayList<ApplicationBasicInfo> appInfo = new ArrayList<ApplicationBasicInfo>();

    try {
        getAppNamesStmt = connection
                .prepareStatement(ApplicationMgtDBQueries.LOAD_APP_NAMES_BY_TENANT);
        getAppNamesStmt.setInt(1, tenantID);
        appNameResultSet = getAppNamesStmt.executeQuery();

        while (appNameResultSet.next()) {
            ApplicationBasicInfo basicInfo = new ApplicationBasicInfo();
            if (ApplicationConstants.LOCAL_SP.equals(appNameResultSet.getString(1))) {
                continue;
            }
            basicInfo.setApplicationName(appNameResultSet.getString(1));
            basicInfo.setDescription(appNameResultSet.getString(2));
            appInfo.add(basicInfo);
        }
        connection.commit();
    } catch (SQLException e) {
        throw new IdentityApplicationManagementException("Error while Reading all Applications");
    } finally {
        IdentityApplicationManagementUtil.closeStatement(getAppNamesStmt);
        IdentityApplicationManagementUtil.closeResultSet(appNameResultSet);
        IdentityApplicationManagementUtil.closeConnection(connection);
    }

    return appInfo.toArray(new ApplicationBasicInfo[appInfo.size()]);
}
 
Example 19
Source File: ApplicationDAOImpl.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
public String getServiceProviderNameByClientId(String clientId, String clientType,
                                               String tenantDomain) throws IdentityApplicationManagementException {
    int tenantID = -123;

    if (tenantDomain != null) {
        try {
            tenantID = ApplicationManagementServiceComponentHolder.getInstance().getRealmService()
                    .getTenantManager().getTenantId(tenantDomain);
        } catch (UserStoreException e1) {
            throw new IdentityApplicationManagementException("Error while reading application");
        }
    }

    String applicationName = null;

    // Reading application name from the database
    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement storeAppPrepStmt = null;
    ResultSet appNameResult = null;
    try {
        storeAppPrepStmt = connection
                .prepareStatement(ApplicationMgtDBQueries.LOAD_APPLICATION_NAME_BY_CLIENT_ID_AND_TYPE);
        storeAppPrepStmt.setString(1, clientId);
        storeAppPrepStmt.setString(2, clientType);
        storeAppPrepStmt.setInt(3, tenantID);
        storeAppPrepStmt.setInt(4, tenantID);
        appNameResult = storeAppPrepStmt.executeQuery();
        if (appNameResult.next()) {
            applicationName = appNameResult.getString(1);
        }
        connection.commit();
    } catch (SQLException e) {
        throw new IdentityApplicationManagementException("Error while reading application", e);
    } finally {
        IdentityApplicationManagementUtil.closeResultSet(appNameResult);
        IdentityApplicationManagementUtil.closeStatement(storeAppPrepStmt);
        IdentityApplicationManagementUtil.closeConnection(connection);
    }

    return applicationName;
}
 
Example 20
Source File: ApplicationDAOImpl.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * @param serviceProviderName
 * @param tenantDomain
 * @param localIdpAsKey
 * @return
 * @throws SQLException
 * @throws IdentityApplicationManagementException
 */
private Map<String, String> getClaimMapping(String serviceProviderName, String tenantDomain,
                                            boolean localIdpAsKey) throws SQLException, IdentityApplicationManagementException {

    int tenantID = -123;

    if (tenantDomain != null) {
        try {
            tenantID = ApplicationManagementServiceComponentHolder.getInstance().getRealmService()
                    .getTenantManager().getTenantId(tenantDomain);
        } catch (UserStoreException e1) {
            throw new IdentityApplicationManagementException("Error while reading application");
        }
    }
    Map<String, String> claimMapping = new HashMap<String, String>();

    if (log.isDebugEnabled()) {
        log.debug("Reading Claim Mappings of Application " + serviceProviderName);
    }

    PreparedStatement getClaimPreStmt = null;
    ResultSet resultSet = null;
    Connection connection = IdentityDatabaseUtil.getDBConnection();
    try {

        getClaimPreStmt = connection
                .prepareStatement(ApplicationMgtDBQueries.LOAD_CLAIM_MAPPING_BY_APP_NAME);
        // IDP_CLAIM, SP_CLAIM, IS_REQUESTED
        getClaimPreStmt.setString(1, serviceProviderName);
        getClaimPreStmt.setInt(2, tenantID);
        resultSet = getClaimPreStmt.executeQuery();

        while (resultSet.next()) {
            if (localIdpAsKey) {
                claimMapping.put(resultSet.getString(1), resultSet.getString(2));
            } else {
                claimMapping.put(resultSet.getString(2), resultSet.getString(1));
            }
        }
        connection.commit();
    } finally {
        IdentityApplicationManagementUtil.closeStatement(getClaimPreStmt);
        IdentityApplicationManagementUtil.closeResultSet(resultSet);
        IdentityApplicationManagementUtil.closeConnection(connection);
    }

    return claimMapping;
}