/** * Utility method to close statements and connections. * * @param preparedStatement * The prepared statement to close. */ public static void cleanup(PreparedStatement preparedStatement, ResultSet resultSet) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { log.error("Error closing prepared statement.", e); } } cleanup(preparedStatement); }
public void deleteCredentials(String gatewayName, String tokenId, Connection connection) throws CredentialStoreException { String sql = "DELETE FROM CREDENTIALS WHERE GATEWAY_ID=? AND TOKEN_ID=?"; PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, gatewayName); preparedStatement.setString(2, tokenId); preparedStatement.executeUpdate(); connection.commit(); } catch (SQLException e) { StringBuilder stringBuilder = new StringBuilder("Error deleting credentials for ."); stringBuilder.append("gateway - ").append(gatewayName); stringBuilder.append("token id - ").append(tokenId); log.error(stringBuilder.toString(), e); throw new CredentialStoreException(stringBuilder.toString(), e); } finally { DBUtil.cleanup(preparedStatement); } }
/** * */ public String getGatewayID(String tokenId, Connection connection) throws CredentialStoreException { String sql = "SELECT GATEWAY_ID FROM CREDENTIALS WHERE TOKEN_ID=?"; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, tokenId); resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { return resultSet.getString("GATEWAY_ID"); } } catch (SQLException e) { StringBuilder stringBuilder = new StringBuilder("Error retrieving credentials for user."); stringBuilder.append("token id - ").append(tokenId); log.debug(stringBuilder.toString(), e); throw new CredentialStoreException(stringBuilder.toString(), e); } finally { DBUtil.cleanup(preparedStatement, resultSet); } return null; } /**
public CommunityUser getCommunityUser(String gatewayName, String communityUserName, Connection connection) throws CredentialStoreException { String sql = "SELECT * FROM COMMUNITY_USER WHERE GATEWAY_ID=? AND COMMUNITY_USER_NAME=?"; PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, gatewayName); preparedStatement.setString(2, communityUserName); ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { String email = resultSet.getString("COMMUNITY_USER_EMAIL"); // TODO fix typo return new CommunityUser(gatewayName, communityUserName, email); } } catch (SQLException e) { StringBuilder stringBuilder = new StringBuilder("Error retrieving community user."); stringBuilder.append("gateway - ").append(gatewayName); stringBuilder.append("community user name - ").append(communityUserName); log.error(stringBuilder.toString(), e); throw new CredentialStoreException(stringBuilder.toString(), e); } finally { DBUtil.cleanup(preparedStatement); } return null; }
public CommunityUser getCommunityUserByToken(String gatewayName, String tokenId, Connection connection) throws CredentialStoreException { String sql = "SELECT * FROM COMMUNITY_USER WHERE GATEWAY_ID=? AND TOKEN_ID=?"; PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, gatewayName); preparedStatement.setString(2, tokenId); ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { String communityUserName = resultSet.getString("COMMUNITY_USER_NAME"); String email = resultSet.getString("COMMUNITY_USER_EMAIL"); // TODO fix typo return new CommunityUser(gatewayName, communityUserName, email); } } catch (SQLException e) { StringBuilder stringBuilder = new StringBuilder("Error retrieving community user."); stringBuilder.append("gateway - ").append(gatewayName); stringBuilder.append("token- ").append(tokenId); log.error(stringBuilder.toString(), e); throw new CredentialStoreException(stringBuilder.toString(), e); } finally { DBUtil.cleanup(preparedStatement); } return null; }
public void removeCredentials(String gatewayName, String tokenId) throws CredentialStoreException { Connection connection = getConnection(); try { credentialsDAO.deleteCredentials(gatewayName, tokenId, connection); } finally { DBUtil.cleanup(connection); } }
@Override public String getGatewayID(String tokenId) throws CredentialStoreException { Connection connection = getConnection(); try { return this.credentialsDAO.getGatewayID(tokenId, connection); } finally { DBUtil.cleanup(connection); } }
@Override public List<Credential> getAllCredentialsPerGateway(String gatewayId) throws CredentialStoreException { Connection connection = getConnection(); try { return this.credentialsDAO.getCredentials(gatewayId, connection); } finally { DBUtil.cleanup(connection); } }
public List<CommunityUser> getCommunityUsers(String gatewayName, Connection connection) throws CredentialStoreException { List<CommunityUser> userList = new ArrayList<CommunityUser>(); String sql = "SELECT * FROM COMMUNITY_USER WHERE GATEWAY_ID=?"; PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, gatewayName); ResultSet resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { String userName = resultSet.getString("COMMUNITY_USER_NAME"); String email = resultSet.getString("COMMUNITY_USER_EMAIL"); // TODO fix typo userList.add(new CommunityUser(gatewayName, userName, email)); } } catch (SQLException e) { StringBuilder stringBuilder = new StringBuilder("Error retrieving community users for "); stringBuilder.append("gateway - ").append(gatewayName); log.error(stringBuilder.toString(), e); throw new CredentialStoreException(stringBuilder.toString(), e); } finally { DBUtil.cleanup(preparedStatement); } return userList; }
public void deleteCommunityUserByToken(CommunityUser user, String token, Connection connection) throws CredentialStoreException { String sql = "DELETE FROM COMMUNITY_USER WHERE GATEWAY_ID=? AND COMMUNITY_USER_NAME=? AND TOKEN_ID=?"; PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, user.getGatewayName()); preparedStatement.setString(2, user.getUserName()); preparedStatement.setString(3, token); preparedStatement.executeUpdate(); connection.commit(); } catch (SQLException e) { StringBuilder stringBuilder = new StringBuilder("Error deleting community user."); stringBuilder.append("gateway - ").append(user.getGatewayName()); stringBuilder.append("community user name - ").append(user.getUserName()); log.error(stringBuilder.toString(), e); throw new CredentialStoreException(stringBuilder.toString(), e); } finally { DBUtil.cleanup(preparedStatement); } }
@Override public Credential getCredential(String gatewayId, String tokenId) throws CredentialStoreException { Connection connection = getConnection(); try { return this.credentialsDAO.getCredential(gatewayId, tokenId, connection); } finally { DBUtil.cleanup(connection); } }
public List<Credential> getAllCredentials() throws CredentialStoreException { Connection connection = getConnection(); try { return this.credentialsDAO.getCredentials(connection); } finally { DBUtil.cleanup(connection); } }
public void deleteCommunityUser(CommunityUser user, Connection connection) throws CredentialStoreException { String sql = "DELETE FROM COMMUNITY_USER WHERE GATEWAY_ID=? AND COMMUNITY_USER_NAME=?"; PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, user.getGatewayName()); preparedStatement.setString(2, user.getUserName()); preparedStatement.executeUpdate(); connection.commit(); } catch (SQLException e) { StringBuilder stringBuilder = new StringBuilder("Error deleting community user."); stringBuilder.append("gateway - ").append(user.getGatewayName()); stringBuilder.append("community user name - ").append(user.getUserName()); log.error(stringBuilder.toString(), e); throw new CredentialStoreException(stringBuilder.toString(), e); } finally { DBUtil.cleanup(preparedStatement); } }
DBUtil.cleanup(preparedStatement, resultSet);
} finally { DBUtil.cleanup(preparedStatement);
public void addCommunityUser(CommunityUser user, String token, Connection connection) throws CredentialStoreException { String sql = "INSERT INTO COMMUNITY_USER VALUES (?, ?, ?, ?)"; PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, user.getGatewayName()); preparedStatement.setString(2, user.getUserName()); preparedStatement.setString(3, token); preparedStatement.setString(4, user.getUserEmail()); preparedStatement.executeUpdate(); connection.commit(); } catch (SQLException e) { StringBuilder stringBuilder = new StringBuilder("Error persisting community user."); stringBuilder.append("gateway - ").append(user.getGatewayName()); stringBuilder.append("community user name - ").append(user.getUserName()); stringBuilder.append("community user email - ").append(user.getUserEmail()); stringBuilder.append("token id - ").append(token); log.error(stringBuilder.toString(), e); throw new CredentialStoreException(stringBuilder.toString(), e); } finally { DBUtil.cleanup(preparedStatement); } }
public String getPortalUser(String gatewayName, String tokenId) throws CredentialStoreException { Connection connection = getConnection(); Credential credential; try { credential = this.credentialsDAO.getCredential(gatewayName, tokenId, connection); } finally { DBUtil.cleanup(connection); } return credential.getPortalUserName(); }
public void writeCredentials(Credential credential) throws CredentialStoreException { SSHCredential sshCredential = (SSHCredential) credential; Connection connection = null; try { connection = dbUtil.getConnection(); // First delete existing credentials credentialsDAO.deleteCredentials(sshCredential.getGateway(), sshCredential.getToken(), connection); // Add the new certificate credentialsDAO.addCredentials(sshCredential.getGateway(), credential, connection); if (!connection.getAutoCommit()) { connection.commit(); } } catch (SQLException e) { if (connection != null) { try { connection.rollback(); } catch (SQLException e1) { logger.error("Unable to rollback transaction", e1); } } throw new CredentialStoreException("Unable to retrieve database connection.", e); } finally { DBUtil.cleanup(connection); } }
public void writeCredentials(Credential credential) throws CredentialStoreException { CertificateCredential certificateCredential = (CertificateCredential) credential; Connection connection = null; try { connection = dbUtil.getConnection(); // Write community user writeCommunityUser(certificateCredential.getCommunityUser(), credential.getToken(), connection); // First delete existing credentials credentialsDAO.deleteCredentials(certificateCredential.getCommunityUser().getGatewayName(), certificateCredential.getToken(), connection); // Add the new certificate credentialsDAO.addCredentials(certificateCredential.getCommunityUser().getGatewayName(), credential, connection); if (!connection.getAutoCommit()) { connection.commit(); } } catch (SQLException e) { if (connection != null) { try { connection.rollback(); } catch (SQLException e1) { log.error("Unable to rollback transaction", e1); } } throw new CredentialStoreException("Unable to retrieve database connection.", e); } finally { DBUtil.cleanup(connection); } }
public CertificateAuditInfo getAuditInfo(String gatewayName, String tokenId) throws CredentialStoreException { Connection connection = getConnection(); CertificateAuditInfo certificateAuditInfo; try { CertificateCredential certificateCredential = (CertificateCredential) this.credentialsDAO.getCredential( gatewayName, tokenId, connection); certificateAuditInfo = new CertificateAuditInfo(); CommunityUser retrievedUser = certificateCredential.getCommunityUser(); certificateAuditInfo.setCommunityUserName(retrievedUser.getUserName()); certificateAuditInfo.setCredentialLifeTime(certificateCredential.getLifeTime()); certificateAuditInfo.setCredentialsRequestedTime(certificateCredential.getCertificateRequestedTime()); certificateAuditInfo.setGatewayName(gatewayName); certificateAuditInfo.setNotAfter(certificateCredential.getNotAfter()); certificateAuditInfo.setNotBefore(certificateCredential.getNotBefore()); certificateAuditInfo.setPortalUserName(certificateCredential.getPortalUserName()); } finally { DBUtil.cleanup(connection); } return certificateAuditInfo; }