Search in sources :

Example 1 with EmailValidator

use of org.apache.commons.validator.routines.EmailValidator in project cosmic by MissionCriticalCloud.

the class DomainManagerImpl method createDomain.

@Override
@DB
public Domain createDomain(final String name, final Long parentId, final Long ownerId, final String networkDomain, String domainUUID, final String email) {
    // Verify network domain
    if (networkDomain != null) {
        if (!NetUtils.verifyDomainName(networkDomain)) {
            throw new InvalidParameterValueException("Invalid network domain. Total length shouldn't exceed 190 chars. Each domain label must be between 1 and 63 characters long, can contain ASCII letters " + "'a' through 'z', the digits '0' through '9', " + "and the hyphen ('-'); can't start or end with \"-\"");
        }
    }
    final SearchCriteria<DomainVO> sc = _domainDao.createSearchCriteria();
    sc.addAnd("name", SearchCriteria.Op.EQ, name);
    sc.addAnd("parent", SearchCriteria.Op.EQ, parentId);
    final List<DomainVO> domains = _domainDao.search(sc, null);
    if (!domains.isEmpty()) {
        throw new InvalidParameterValueException("Domain with name " + name + " already exists for the parent id=" + parentId);
    }
    if (domainUUID == null) {
        domainUUID = UUID.randomUUID().toString();
    }
    final EmailValidator validator = EmailValidator.getInstance();
    if (!StringUtils.isEmpty(email) && !validator.isValid(email)) {
        throw new InvalidParameterValueException("Email address is not formatted correctly");
    }
    final String domainUUIDFinal = domainUUID;
    final DomainVO domain = Transaction.execute(new TransactionCallback<DomainVO>() {

        @Override
        public DomainVO doInTransaction(final TransactionStatus status) {
            final DomainVO domain = _domainDao.create(new DomainVO(name, ownerId, parentId, networkDomain, domainUUIDFinal, email));
            _resourceCountDao.createResourceCounts(domain.getId(), ResourceLimit.ResourceOwnerType.Domain);
            return domain;
        }
    });
    CallContext.current().putContextParameter(Domain.class, domain.getUuid());
    _messageBus.publish(_name, MESSAGE_ADD_DOMAIN_EVENT, PublishScope.LOCAL, domain.getId());
    return domain;
}
Also used : DomainVO(com.cloud.domain.DomainVO) EmailValidator(org.apache.commons.validator.routines.EmailValidator) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) TransactionStatus(com.cloud.utils.db.TransactionStatus) DB(com.cloud.utils.db.DB)

Example 2 with EmailValidator

use of org.apache.commons.validator.routines.EmailValidator in project cosmic by MissionCriticalCloud.

the class DomainManagerImpl method updateDomain.

@Override
@ActionEvent(eventType = EventTypes.EVENT_DOMAIN_UPDATE, eventDescription = "updating Domain")
@DB
public DomainVO updateDomain(final UpdateDomainCmd cmd) {
    final Long domainId = cmd.getId();
    final String domainName = cmd.getDomainName();
    final String networkDomain = cmd.getNetworkDomain();
    final String email = cmd.getEmail();
    // check if domain exists in the system
    final DomainVO domain = _domainDao.findById(domainId);
    if (domain == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find domain with specified domain id");
        ex.addProxyObject(domainId.toString(), "domainId");
        throw ex;
    } else if (domain.getParent() == null && domainName != null) {
        // check if domain is ROOT domain - and deny to edit it with the new name
        throw new InvalidParameterValueException("ROOT domain can not be edited with a new name");
    }
    // check permissions
    final Account caller = CallContext.current().getCallingAccount();
    _accountMgr.checkAccess(caller, domain);
    // domain name is unique in the cloud
    if (domainName != null) {
        final SearchCriteria<DomainVO> sc = _domainDao.createSearchCriteria();
        sc.addAnd("name", SearchCriteria.Op.EQ, domainName);
        final List<DomainVO> domains = _domainDao.search(sc, null);
        final boolean sameDomain = (domains.size() == 1 && domains.get(0).getId() == domainId);
        if (!domains.isEmpty() && !sameDomain) {
            final InvalidParameterValueException ex = new InvalidParameterValueException("Failed to update specified domain id with name '" + domainName + "' since it already exists in the system");
            ex.addProxyObject(domain.getUuid(), "domainId");
            throw ex;
        }
    }
    // validate network domain
    if (networkDomain != null && !networkDomain.isEmpty()) {
        if (!NetUtils.verifyDomainName(networkDomain)) {
            throw new InvalidParameterValueException("Invalid network domain. Total length shouldn't exceed 190 chars. Each domain label must be between 1 and 63 characters long, can contain ASCII letters " + "'a' through 'z', the digits '0' through '9', " + "and the hyphen ('-'); can't start or end with \"-\"");
        }
    }
    final EmailValidator validator = EmailValidator.getInstance();
    if (!StringUtils.isEmpty(email) && !validator.isValid(email)) {
        throw new InvalidParameterValueException("Email address is not formatted correctly");
    }
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(final TransactionStatus status) {
            if (domainName != null) {
                final String updatedDomainPath = getUpdatedDomainPath(domain.getPath(), domainName);
                updateDomainChildren(domain, updatedDomainPath);
                domain.setName(domainName);
                domain.setPath(updatedDomainPath);
            }
            if (networkDomain != null) {
                if (networkDomain.isEmpty()) {
                    domain.setNetworkDomain(null);
                } else {
                    domain.setNetworkDomain(networkDomain);
                }
            }
            if (email != null) {
                if (email.isEmpty()) {
                    domain.setEmail(null);
                } else {
                    domain.setEmail(email);
                }
            }
            _domainDao.update(domainId, domain);
            CallContext.current().putContextParameter(Domain.class, domain.getUuid());
        }
    });
    return _domainDao.findById(domainId);
}
Also used : EmailValidator(org.apache.commons.validator.routines.EmailValidator) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) DomainVO(com.cloud.domain.DomainVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) Domain(com.cloud.domain.Domain) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 3 with EmailValidator

use of org.apache.commons.validator.routines.EmailValidator in project grails-core by grails.

the class EmailConstraint method processValidate.

@Override
protected void processValidate(Object target, Object propertyValue, Errors errors) {
    if (!email) {
        return;
    }
    EmailValidator emailValidator = EmailValidator.getInstance();
    Object[] args = new Object[] { constraintPropertyName, constraintOwningClass, propertyValue };
    String value = propertyValue.toString();
    if (GrailsStringUtils.isBlank(value)) {
        return;
    }
    if (!emailValidator.isValid(value)) {
        rejectValue(target, errors, ConstrainedProperty.DEFAULT_INVALID_EMAIL_MESSAGE_CODE, ConstrainedProperty.EMAIL_CONSTRAINT + ConstrainedProperty.INVALID_SUFFIX, args);
    }
}
Also used : EmailValidator(org.apache.commons.validator.routines.EmailValidator)

Aggregations

EmailValidator (org.apache.commons.validator.routines.EmailValidator)3 DomainVO (com.cloud.domain.DomainVO)2 DB (com.cloud.utils.db.DB)2 TransactionStatus (com.cloud.utils.db.TransactionStatus)2 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)2 Domain (com.cloud.domain.Domain)1 ActionEvent (com.cloud.event.ActionEvent)1 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)1