Example usage for org.apache.shiro.util JdbcUtils closeResultSet

List of usage examples for org.apache.shiro.util JdbcUtils closeResultSet

Introduction

In this page you can find the example usage for org.apache.shiro.util JdbcUtils closeResultSet.

Prototype

public static void closeResultSet(ResultSet rs) 

Source Link

Document

Close the given JDBC ResultSet and ignore any thrown exception.

Usage

From source file:aaa.realms.MySQLRealm.java

License:Apache License

private String[] getPasswordForUser(Connection conn, String username) throws SQLException {

    String[] result;//  w  ww .ja va  2 s. co m
    boolean returningSeparatedSalt = false;
    switch (saltStyle) {
    case NO_SALT:
    case CRYPT:
    case EXTERNAL:
        result = new String[1];
        break;
    default:
        result = new String[2];
        returningSeparatedSalt = true;
    }

    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        ps = conn.prepareStatement(authenticationQuery);
        ps.setString(1, username);

        // Execute query
        rs = ps.executeQuery();

        // Loop over results - although we are only expecting one result, since usernames should be unique
        boolean foundResult = false;
        while (rs.next()) {

            // Check to ensure only one row is processed
            if (foundResult) {
                throw new AuthenticationException(
                        "More than one user row found for user [" + username + "]. Usernames must be unique.");
            }

            result[0] = rs.getString(1);
            if (returningSeparatedSalt) {
                result[1] = rs.getString(2);
            }

            foundResult = true;
        }
    } finally {
        JdbcUtils.closeResultSet(rs);
        JdbcUtils.closeStatement(ps);
    }

    return result;
}

From source file:aaa.realms.MySQLRealm.java

License:Apache License

protected Set<String> getRoleNamesForUser(Connection conn, String username) throws SQLException {
    PreparedStatement ps = null;/*  ww  w.  j a  v  a  2  s.c  om*/
    ResultSet rs = null;
    Set<String> roleNames = new LinkedHashSet<String>();
    try {
        ps = conn.prepareStatement(userRolesQuery);
        ps.setString(1, username);

        // Execute query
        rs = ps.executeQuery();

        // Loop over results and add each returned role to a set
        while (rs.next()) {

            String roleName = rs.getString(1);

            // Add the role to the list of names if it isn't null
            if (roleName != null) {
                roleNames.add(roleName);
            } else {
                if (log.isWarnEnabled()) {
                    log.warn("Null role name found while retrieving role names for user [" + username + "]");
                }
            }
        }
    } finally {
        JdbcUtils.closeResultSet(rs);
        JdbcUtils.closeStatement(ps);
    }
    return roleNames;
}

From source file:aaa.realms.MySQLRealm.java

License:Apache License

protected Set<String> getPermissions(Connection conn, String username) throws SQLException {
    PreparedStatement ps = null;//from  w  ww . j ava2 s.  c  o m
    Set<String> permissions = new LinkedHashSet<String>();
    try {
        ps = conn.prepareStatement(permissionsQuery);

        ps.setString(1, username);

        ResultSet rs = null;

        try {
            // Execute query
            rs = ps.executeQuery();

            // Loop over results and add each returned role to a set
            while (rs.next()) {

                String permissionString = rs.getString(1);

                // Add the permission to the set of permissions
                permissions.add(permissionString);
            }
        } finally {
            JdbcUtils.closeResultSet(rs);
        }

    } finally {
        JdbcUtils.closeStatement(ps);
    }

    return permissions;
}

From source file:aaa.realms.MySQLRealm.java

License:Apache License

public Set<String> getUserDomain(Connection conn, String username) {

    PreparedStatement ps = null;// w ww. jav a2s.  com
    Set<String> domains = new LinkedHashSet<>();
    try {
        ps = conn.prepareStatement(userDomainQuery);
        ps.setString(1, username);
        ResultSet rs = null;

        try {
            rs = ps.executeQuery();
            while (rs.next()) {
                String domainID = rs.getString(1);
                domains.add(domainID);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.closeResultSet(rs);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        JdbcUtils.closeStatement(ps);
    }
    return domains;

}

From source file:br.com.betsportclub.controller.security.SecurityRealm.java

License:Apache License

private String[] getPasswordForUser(Connection conn, String username) throws SQLException {

    String[] result;/*from  w w w  .  j a v  a  2  s.  c om*/
    boolean returningSeparatedSalt = false;
    switch (saltStyle) {
    case NO_SALT:
    case CRYPT:
    case EXTERNAL:
        result = new String[1];
        break;
    default:
        result = new String[2];
        returningSeparatedSalt = true;
    }

    PreparedStatement ps = null;
    ResultSet rs = null;
    try {

        authenticationQuery = authenticationQuery.replace("$ds_schema", schema);

        ps = conn.prepareStatement(authenticationQuery);
        ps.setString(1, username);
        //ps.setString(2, username);

        // Execute query
        rs = ps.executeQuery();

        // Loop over results - although we are only expecting one result, since usernames should be unique
        boolean foundResult = false;
        while (rs.next()) {

            // Check to ensure only one row is processed
            if (foundResult) {
                throw new AuthenticationException(
                        "More than one user row found for user [" + username + "]. Usernames must be unique.");
            }

            result[0] = rs.getString(1);
            if (returningSeparatedSalt) {
                result[1] = rs.getString(2);
            }

            foundResult = true;
        }
    } finally {
        JdbcUtils.closeResultSet(rs);
        JdbcUtils.closeStatement(ps);
    }

    return result;
}

From source file:br.com.betsportclub.controller.security.SecurityRealm.java

License:Apache License

protected Set<String> getRoleNamesForUser(Connection conn, String username) throws SQLException {
    PreparedStatement ps = null;//w w w  .  j a v  a 2 s.  c om
    ResultSet rs = null;
    Set<String> roleNames = new LinkedHashSet<String>();
    try {

        userRolesQuery = userRolesQuery.replace("$ds_schema", schema);

        ps = conn.prepareStatement(userRolesQuery);
        ps.setString(1, username);
        ps.setString(2, username);

        // Execute query
        rs = ps.executeQuery();

        // Loop over results and add each returned role to a set
        while (rs.next()) {

            String roleName = rs.getString(1);

            // Add the role to the list of names if it isn't null
            if (roleName != null) {
                roleNames.add(roleName);
            } else {
                if (log.isWarnEnabled()) {
                    log.warn("Null role name found while retrieving role names for user [" + username + "]");
                }
            }
        }
    } finally {
        JdbcUtils.closeResultSet(rs);
        JdbcUtils.closeStatement(ps);
    }
    return roleNames;
}

From source file:br.com.betsportclub.controller.security.SecurityRealm.java

License:Apache License

protected Set<String> getPermissions(Connection conn, String username, Collection<String> roleNames)
        throws SQLException {
    PreparedStatement ps = null;/*from ww w  . j  a v  a2 s .c  o m*/
    Set<String> permissions = new LinkedHashSet<String>();
    try {

        permissionsQuery = permissionsQuery.replace("$ds_schema", schema);

        ps = conn.prepareStatement(permissionsQuery);
        for (String roleName : roleNames) {

            //ps.setString(1, roleName);
            ps.setLong(1, new Long(roleName));

            ResultSet rs = null;

            try {
                // Execute query
                rs = ps.executeQuery();

                // Loop over results and add each returned role to a set
                while (rs.next()) {

                    String permissionString = rs.getString(1);

                    // Add the permission to the set of permissions
                    permissions.add(permissionString);
                }
            } finally {
                JdbcUtils.closeResultSet(rs);
            }

        }
    } finally {
        JdbcUtils.closeStatement(ps);
    }

    return permissions;
}

From source file:com.charmyin.shiro.realm.jdbc.CustomJdbcRealm.java

License:Apache License

/**
 * ?roleNameroleIdsrole??/* w  w  w  . ja  v a 2s . co  m*/
 * @param conn
 * @param username
 * @return Object[] roleSetObjects(they are type of Set<String>) roleSetObjects[0]--roleIds, roleSetObjects[1]--roleNames, roleSetObjects[2]--rolePermissions
 * @throws SQLException
 */
protected Set<String> getRoleNamesForUser(Connection conn, String username) throws SQLException {
    PreparedStatement ps = null;
    ResultSet rs = null;
    Set<String> roleNames = new LinkedHashSet<String>();
    try {
        ps = conn.prepareStatement(userRolesQuery);
        ps.setString(1, username);

        // Execute query
        rs = ps.executeQuery();

        // Loop over results and add each returned role to a set
        while (rs.next()) {

            String roleName = rs.getString(1);

            // Add the role to the list of names if it isn't null
            if (roleName != null) {

                roleNames.add(roleName);
            } else {
                if (log.isWarnEnabled()) {
                    log.warn("Null role name found while retrieving role names for user [" + username + "]");
                }
            }
        }
    } finally {
        JdbcUtils.closeResultSet(rs);
        JdbcUtils.closeStatement(ps);
    }
    return roleNames;
}

From source file:com.charmyin.shiro.realm.jdbc.CustomJdbcRealm.java

License:Apache License

protected Set<String> getPermissions(Connection conn, String username, Collection<String> roleNames)
        throws SQLException {
    PreparedStatement ps = null;/*  w w w.j av a 2 s  .c  om*/
    //???json??"[{"permission":"aaaa:bbb:ddd","remark":"bbb"},{"permission":"ddd","remark":"eeee"}]"
    Set<String> rawPermissions = new LinkedHashSet<String>();

    //String to save all the menu ids of the user
    StringBuilder menuIdsSb = new StringBuilder();

    log.debug("Getting the " + username + "'s permissions");

    try {
        ps = conn.prepareStatement(permissionsQuery);
        for (String roleName : roleNames) {

            ps.setString(1, roleName);

            ResultSet rs = null;

            try {
                // Execute query
                rs = ps.executeQuery();

                // Loop over results and add each returned role to a set
                while (rs.next()) {
                    //Get the menu_ids of a role
                    String menuIdsString = rs.getString(2);
                    menuIdsSb.append(menuIdsString).append(",");

                    String RolePermissionString = rs.getString(1);
                    // Add the permission to the set of menuPermissions
                    rawPermissions.add(RolePermissionString);
                }
            } finally {
                JdbcUtils.closeResultSet(rs);
            }
        }
    } finally {
        JdbcUtils.closeStatement(ps);
    }

    //Get permission set by menuIds 
    Set<String> permissionsByMenuId = getPermissionByMenuIds(conn, menuIdsSb);

    //add permissionsByMenuId to rawPermissions
    rawPermissions.addAll(permissionsByMenuId);

    //permissions
    ObjectMapper mapper = new ObjectMapper();
    List<Permission> permissionsList = new ArrayList<Permission>();

    //json??
    for (String str : rawPermissions) {
        if (str == null || str.trim().equals("")) {
            continue;
        }
        try {
            permissionsList
                    .addAll((List<Permission>) mapper.readValue(str, new TypeReference<List<Permission>>() {
                    }));
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    //????
    Set<String> permissions = new LinkedHashSet<String>();
    //permissions.addAll();
    for (Permission p : permissionsList) {
        permissions.add(p.getPermission());
    }
    return permissions;
}

From source file:com.charmyin.shiro.realm.jdbc.CustomJdbcRealm.java

License:Apache License

/**
 * Get permission string set by menu ids
 * @param menuIdsSb/*w  w  w .  ja  va  2  s .  com*/
 * @return
 */
private Set<String> getPermissionByMenuIds(Connection conn, StringBuilder menuIdsSb) throws SQLException {
    //if nomenuid exists, return null
    String menuIdsStr = menuIdsSb.toString();
    if (menuIdsStr.trim().length() < 1) {
        return null;
    }

    String[] menuIdsArray = menuIdsStr.split(",");
    //Menu Ids set
    Set<String> set = new HashSet<String>();
    CollectionUtils.addAll(set, menuIdsArray);

    Set<String> permissionSet = new HashSet<String>();
    //Get menu permissions by menu ids
    PreparedStatement ps = null;
    try {
        ps = conn.prepareStatement(permissionsQueryByMenuId);
        for (String menuId : set) {
            int menuIdInt = 0;
            //no empty value
            try {
                menuIdInt = Integer.parseInt(menuId);
            } catch (Exception e) {
                log.warn("menuId:" + menuId + " is not integer!");
                continue;
            }
            ps.setInt(1, menuIdInt);
            ResultSet rs = null;
            try {
                // Execute query
                rs = ps.executeQuery();
                // Loop over results and add each returned role to a set
                while (rs.next()) {
                    String permissionString = rs.getString(1);
                    // Add the menu Permission to the set of menuPermissions
                    permissionSet.add(permissionString);
                }
            } finally {
                JdbcUtils.closeResultSet(rs);
            }
        }
    } finally {
        JdbcUtils.closeStatement(ps);
    }
    return permissionSet;
}