Example usage for org.springframework.util Assert hasLength

List of usage examples for org.springframework.util Assert hasLength

Introduction

In this page you can find the example usage for org.springframework.util Assert hasLength.

Prototype

public static void hasLength(@Nullable String text, Supplier<String> messageSupplier) 

Source Link

Document

Assert that the given String is not empty; that is, it must not be null and not the empty String.

Usage

From source file:org.opennms.protocols.radius.springsecurity.RadiusAuthenticationProvider.java

/**
 * Create an instance using the supplied server and shared secret.
 *
 * @param server a {@link java.lang.String} object.
 * @param sharedSecret a {@link java.lang.String} object.
 *//* w  ww  . ja  v a 2 s .  c  om*/
public RadiusAuthenticationProvider(String server, String sharedSecret) {
    Assert.hasLength(server, "A server must be specified");
    this.server = server;
    Assert.hasLength(sharedSecret, "A shared secret must be specified");
    this.secret = sharedSecret;
}

From source file:org.pentaho.platform.engine.security.userroledao.hibernate.HibernateUserRoleDao.java

public void createUser(IPentahoUser userToCreate)
        throws AlreadyExistsException, UncategorizedUserRoleDaoException {
    Assert.notNull(userToCreate, Messages.getString("HibernateUserRoleDao.ERROR_0001_USER_CANNOT_BE_NULL")); //$NON-NLS-1$
    Assert.hasLength(userToCreate.getUsername(),
            Messages.getString("HibernateUserRoleDao.ERROR_0002_USERNAME_CANNOT_BE_BLANK")); //$NON-NLS-1$
    Assert.notNull(userToCreate.getPassword(),
            Messages.getString("HibernateUserRoleDao.ERROR_0003_PASSWORD_CANNOT_BE_NULL")); //$NON-NLS-1$

    if (getUser(userToCreate.getUsername()) == null) {
        try {//from   ww w  .j  a va2s .  co m
            getHibernateTemplate().save(userToCreate);
        } catch (DataAccessException e) {
            throw new UncategorizedUserRoleDaoException(
                    Messages.getString("HibernateUserRoleDao.ERROR_0004_DATA_ACCESS_EXCEPTION"), e); //$NON-NLS-1$
        }
    } else {
        throw new AlreadyExistsException(userToCreate.getUsername());
    }
}

From source file:org.pentaho.platform.engine.security.userroledao.hibernate.HibernateUserRoleDao.java

public void deleteUser(IPentahoUser userToDelete) throws NotFoundException, UncategorizedUserRoleDaoException {
    Assert.notNull(userToDelete, Messages.getString("HibernateUserRoleDao.ERROR_0001_USER_CANNOT_BE_NULL")); //$NON-NLS-1$
    Assert.hasLength(userToDelete.getUsername(),
            Messages.getString("HibernateUserRoleDao.ERROR_0002_USERNAME_CANNOT_BE_BLANK")); //$NON-NLS-1$

    IPentahoUser user = getUser(userToDelete.getUsername());
    if (user != null) {
        try {// w  w w . j a  v a2 s  .c om
            getHibernateTemplate().delete(user);
        } catch (DataAccessException e) {
            throw new UncategorizedUserRoleDaoException(
                    Messages.getString("HibernateUserRoleDao.ERROR_0004_DATA_ACCESS_EXCEPTION"), e); //$NON-NLS-1$
        }
    } else {
        throw new NotFoundException(userToDelete.getUsername());
    }
}

From source file:org.pentaho.platform.engine.security.userroledao.hibernate.HibernateUserRoleDao.java

public IPentahoUser getUser(String username) throws UncategorizedUserRoleDaoException {
    Assert.hasLength(username, Messages.getString("HibernateUserRoleDao.ERROR_0002_USERNAME_CANNOT_BE_BLANK")); //$NON-NLS-1$

    try {/*ww w. j a v  a2 s  . c  om*/
        return (PentahoUser) getHibernateTemplate().get(PentahoUser.class, username);
    } catch (DataAccessException e) {
        throw new UncategorizedUserRoleDaoException(
                Messages.getString("HibernateUserRoleDao.ERROR_0004_DATA_ACCESS_EXCEPTION"), e); //$NON-NLS-1$
    }
}

From source file:org.pentaho.platform.engine.security.userroledao.hibernate.HibernateUserRoleDao.java

public void updateUser(IPentahoUser userToUpdate) throws NotFoundException, UncategorizedUserRoleDaoException {
    Assert.notNull(userToUpdate, Messages.getString("HibernateUserRoleDao.ERROR_0001_USER_CANNOT_BE_NULL")); //$NON-NLS-1$
    Assert.hasLength(userToUpdate.getUsername(),
            Messages.getString("HibernateUserRoleDao.ERROR_0002_USERNAME_CANNOT_BE_BLANK")); //$NON-NLS-1$
    Assert.notNull(userToUpdate.getPassword(),
            Messages.getString("HibernateUserRoleDao.ERROR_0003_PASSWORD_CANNOT_BE_NULL")); //$NON-NLS-1$

    if (getUser(userToUpdate.getUsername()) != null) {
        try {//from  ww  w.  ja  v a  2 s  . c o m
            getHibernateTemplate().update(getHibernateTemplate().merge(userToUpdate));
        } catch (DataAccessException e) {
            throw new UncategorizedUserRoleDaoException(
                    Messages.getString("HibernateUserRoleDao.ERROR_0004_DATA_ACCESS_EXCEPTION"), e); //$NON-NLS-1$
        }
    } else {
        throw new NotFoundException(userToUpdate.getUsername());
    }
}

From source file:org.pentaho.platform.engine.security.userroledao.hibernate.HibernateUserRoleDao.java

/**
 * This method is more complex because it must manage this role's users manually.
 */// ww w . ja  v  a2  s  .com
public void createRole(IPentahoRole roleToCreate)
        throws AlreadyExistsException, UncategorizedUserRoleDaoException {
    Assert.notNull(roleToCreate, Messages.getString("HibernateUserRoleDao.ERROR_0005_ROLE_CANNOT_BE_NULL")); //$NON-NLS-1$
    Assert.hasLength(roleToCreate.getName(),
            Messages.getString("HibernateUserRoleDao.ERROR_0006_ROLE_NAME_CANNOT_BE_BLANK")); //$NON-NLS-1$

    if (getRole(roleToCreate.getName()) == null) {
        try {
            getHibernateTemplate().save(roleToCreate);
        } catch (DataAccessException e) {
            throw new UncategorizedUserRoleDaoException(
                    Messages.getString("HibernateUserRoleDao.ERROR_0004_DATA_ACCESS_EXCEPTION"), e); //$NON-NLS-1$
        }
    } else {
        throw new AlreadyExistsException(roleToCreate.getName());
    }

    // manually manage users set
    for (IPentahoUser user : roleToCreate.getUsers()) {
        addUser(roleToCreate, user.getUsername());
    }
}

From source file:org.pentaho.platform.engine.security.userroledao.hibernate.HibernateUserRoleDao.java

/**
 * This method is more complex because it must manage this role's users manually.
 *///  w  ww. j  a v  a  2 s  .  c  o m
public void deleteRole(IPentahoRole roleToDelete) throws NotFoundException, UncategorizedUserRoleDaoException {
    Assert.notNull(roleToDelete, Messages.getString("HibernateUserRoleDao.ERROR_0005_ROLE_CANNOT_BE_NULL")); //$NON-NLS-1$
    Assert.hasLength(roleToDelete.getName(),
            Messages.getString("HibernateUserRoleDao.ERROR_0006_ROLE_NAME_CANNOT_BE_BLANK")); //$NON-NLS-1$

    IPentahoRole role = getRole(roleToDelete.getName());
    if (role != null) {
        try {
            // for each user that is a member of this role, manually remove the role assignment from the user
            for (IPentahoUser user : role.getUsers()) {
                user.removeRole(role);
                updateUser(user);
            }
            // delete the role itself now that it is no longer referenced anywhere 
            getHibernateTemplate().delete(role);
        } catch (DataAccessException e) {
            throw new UncategorizedUserRoleDaoException(
                    Messages.getString("HibernateUserRoleDao.ERROR_0004_DATA_ACCESS_EXCEPTION"), e); //$NON-NLS-1$
        }
    } else {
        throw new NotFoundException(roleToDelete.getName());
    }
}

From source file:org.pentaho.platform.engine.security.userroledao.hibernate.HibernateUserRoleDao.java

/**
 * This method is more complex because it must manage this role's users manually.
 *//*from w  ww. j  a v a2s  .  co  m*/
public IPentahoRole getRole(String name) throws UncategorizedUserRoleDaoException {
    Assert.hasLength(name, Messages.getString("HibernateUserRoleDao.ERROR_0006_ROLE_NAME_CANNOT_BE_BLANK")); //$NON-NLS-1$

    try {
        IPentahoRole role = (PentahoRole) getHibernateTemplate().get(PentahoRole.class, name);

        if (role == null) {
            return null;
        }

        List<IPentahoUser> users = getUsersForRole(role.getName());
        for (IPentahoUser user : users) {
            role.addUser(user);
        }
        return role;
    } catch (DataAccessException e) {
        throw new UncategorizedUserRoleDaoException(
                Messages.getString("HibernateUserRoleDao.ERROR_0004_DATA_ACCESS_EXCEPTION"), e); //$NON-NLS-1$
    }
}

From source file:org.pentaho.platform.engine.security.userroledao.hibernate.HibernateUserRoleDao.java

/**
 * This method is more complex because it must manage this role's users manually.
 *///ww w .j a  va 2 s .c  o m
@SuppressWarnings("unchecked")
public void updateRole(IPentahoRole roleToUpdate) throws NotFoundException, UncategorizedUserRoleDaoException {
    Assert.notNull(roleToUpdate, Messages.getString("HibernateUserRoleDao.ERROR_0005_ROLE_CANNOT_BE_NULL")); //$NON-NLS-1$
    Assert.hasLength(roleToUpdate.getName(),
            Messages.getString("HibernateUserRoleDao.ERROR_0006_ROLE_NAME_CANNOT_BE_BLANK")); //$NON-NLS-1$

    IPentahoRole originalRole = getRole(roleToUpdate.getName());

    if (originalRole == null) {
        throw new NotFoundException(roleToUpdate.getName());
    }

    // make a copy of originalRole's users since the merge call below will change the users
    Set<IPentahoUser> originalRoleUsers = new HashSet<IPentahoUser>(originalRole.getUsers());

    try {
        getHibernateTemplate().update(getHibernateTemplate().merge(roleToUpdate));
    } catch (DataAccessException e) {
        throw new UncategorizedUserRoleDaoException(
                Messages.getString("HibernateUserRoleDao.ERROR_0004_DATA_ACCESS_EXCEPTION"), e); //$NON-NLS-1$
    }

    // manually manage users set

    // use relative complement (aka set-theoretic difference, aka subtraction) to get the users to add and users to 
    // remove
    Set<IPentahoUser> usersToAdd = new HashSet<IPentahoUser>(
            CollectionUtils.subtract(roleToUpdate.getUsers(), originalRoleUsers));
    Set<IPentahoUser> usersToRemove = new HashSet<IPentahoUser>(
            CollectionUtils.subtract(originalRoleUsers, roleToUpdate.getUsers()));

    for (IPentahoUser user : usersToAdd) {
        addUser(roleToUpdate, user.getUsername());
    }

    for (IPentahoUser user : usersToRemove) {
        removeUser(roleToUpdate, user.getUsername());
    }

}

From source file:org.pentaho.platform.engine.security.userroledao.hibernate.HibernateUserRoleDao.java

/**
 * Manually update role's users.//from  w w  w  .  j a v  a2  s. c  o  m
 */
protected void addUser(IPentahoRole roleToUpdate, String username)
        throws NotFoundException, UncategorizedUserRoleDaoException {
    Assert.notNull(roleToUpdate, Messages.getString("HibernateUserRoleDao.ERROR_0005_ROLE_CANNOT_BE_NULL")); //$NON-NLS-1$
    Assert.hasLength(roleToUpdate.getName(),
            Messages.getString("HibernateUserRoleDao.ERROR_0006_ROLE_NAME_CANNOT_BE_BLANK")); //$NON-NLS-1$
    Assert.hasLength(username, Messages.getString("HibernateUserRoleDao.ERROR_0002_USERNAME_CANNOT_BE_BLANK")); //$NON-NLS-1$

    IPentahoUser user = getUser(username);
    if (user != null) {
        user.addRole(roleToUpdate);
        updateUser(user);
    } else {
        throw new NotFoundException(username);
    }
}