Search in sources :

Example 71 with ValidationException

use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by MangoAutomation.

the class DataSourceService method copy.

/**
 * Copy a data source and optionally its points
 */
public DataSourceVO copy(String xid, String copyXid, String copyName, String copyDeviceName, boolean enabled, boolean copyPoints) throws PermissionException, NotFoundException {
    DataSourceVO existing = get(xid);
    PermissionHolder user = Common.getUser();
    ensureCreatePermission(user, existing);
    // Determine the new name
    String newName;
    if (StringUtils.isEmpty(copyName)) {
        newName = StringUtils.abbreviate(TranslatableMessage.translate(Common.getTranslations(), "common.copyPrefix", existing.getName()), 40);
    } else {
        newName = copyName;
    }
    // Determine the new xid
    String newXid;
    if (StringUtils.isEmpty(copyXid)) {
        newXid = DataSourceDao.getInstance().generateUniqueXid();
    } else {
        newXid = copyXid;
    }
    String newDeviceName;
    if (StringUtils.isEmpty(copyDeviceName)) {
        newDeviceName = existing.getName();
    } else {
        newDeviceName = copyDeviceName;
    }
    // Ensure device name is valid
    if (StringValidation.isLengthGreaterThan(newDeviceName, 255)) {
        ProcessResult result = new ProcessResult();
        result.addMessage("deviceName", new TranslatableMessage("validate.notLongerThan", 255));
        throw new ValidationException(result);
    }
    DataSourceVO copy = (DataSourceVO) existing.copy();
    copy.setId(Common.NEW_ID);
    copy.setName(newName);
    copy.setXid(newXid);
    copy.setEnabled(enabled);
    ensureValid(copy);
    // Save it
    insert(copy);
    if (copyPoints) {
        // Copy the points from this data source
        dataPointService.copyDataSourcePoints(existing.getId(), copy.getId(), newDeviceName);
    }
    return get(newXid);
}
Also used : DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) PollingDataSourceVO(com.serotonin.m2m2.vo.dataSource.PollingDataSourceVO) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder)

Example 72 with ValidationException

use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by MangoAutomation.

the class AbstractBasicVOService method insert.

/**
 */
public T insert(T vo) throws PermissionException, ValidationException {
    PermissionHolder user = Common.getUser();
    // Ensure they can create
    ensureCreatePermission(user, vo);
    // Ensure id is not set
    if (vo.getId() != Common.NEW_ID) {
        ProcessResult result = new ProcessResult();
        result.addContextualMessage("id", "validate.invalidValue");
        throw new ValidationException(result, vo.getClass());
    }
    ensureValid(vo);
    dao.insert(vo);
    return vo;
}
Also used : ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder)

Example 73 with ValidationException

use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by MangoAutomation.

the class AbstractVOService method insert.

/**
 */
@Override
public T insert(T vo) throws PermissionException, ValidationException {
    PermissionHolder user = Common.getUser();
    // Ensure they can create
    ensureCreatePermission(user, vo);
    // Ensure id is not set
    if (vo.getId() != Common.NEW_ID) {
        ProcessResult result = new ProcessResult();
        result.addContextualMessage("id", "validate.invalidValue");
        throw new ValidationException(result, vo.getClass());
    }
    // Generate an Xid if necessary
    if (StringUtils.isEmpty(vo.getXid()))
        vo.setXid(dao.generateUniqueXid());
    ensureValid(vo);
    dao.insert(vo);
    return vo;
}
Also used : ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder)

Example 74 with ValidationException

use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by MangoAutomation.

the class MangoPasswordAuthenticationProvider method authenticate.

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (!(authentication instanceof UsernamePasswordAuthenticationToken)) {
        return null;
    }
    if (!(authentication.getDetails() instanceof WebAuthenticationDetails)) {
        throw new InternalAuthenticationServiceException("Expected authentication details to be instance of WebAuthenticationDetails");
    }
    String username = authentication.getName();
    WebAuthenticationDetails details = (WebAuthenticationDetails) authentication.getDetails();
    String ip = details.getRemoteAddress();
    try {
        boolean ipRateExceeded = this.ipRateLimiter != null && this.ipRateLimiter.checkRateExceeded(ip);
        boolean usernameRateExceeded = this.usernameRateLimiter != null && this.usernameRateLimiter.checkRateExceeded(username);
        if (ipRateExceeded) {
            throw new IpAddressAuthenticationRateException(ip);
        }
        if (usernameRateExceeded) {
            throw new UsernameAuthenticationRateException(username);
        }
        UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
        if (!(userDetails instanceof User)) {
            throw new InternalAuthenticationServiceException("Expected user details to be instance of User");
        }
        User user = (User) userDetails;
        String newPassword = null;
        if (authentication instanceof PasswordResetAuthenticationToken) {
            newPassword = ((PasswordResetAuthenticationToken) authentication).getNewPassword();
        }
        try {
            this.userDetailsChecker.check(user);
        } catch (CredentialsExpiredException e) {
            if (newPassword == null) {
                throw e;
            }
        }
        String credentials = (String) authentication.getCredentials();
        // Validating the password against the database.
        if (!Common.checkPassword(credentials, user.getPassword())) {
            throw new BadCredentialsException(Common.translate("login.validation.invalidLogin"));
        }
        if (newPassword != null) {
            if (newPassword.equals(credentials)) {
                throw new PasswordChangeException(new TranslatableMessage("rest.exception.samePassword"));
            }
            final String password = newPassword;
            runAs.runAs(user, () -> {
                try {
                    usersService.updatePassword(user, password);
                } catch (ValidationException e) {
                    throw new PasswordChangeException(new TranslatableMessage("rest.exception.complexityRequirementsFailed"));
                }
                return null;
            });
        }
        return new UsernamePasswordAuthenticationToken(user, user.getPassword(), Collections.unmodifiableCollection(user.getAuthorities()));
    } catch (AuthenticationException e) {
        if (this.ipRateLimiter != null) {
            this.ipRateLimiter.hit(ip);
        }
        if (this.usernameRateLimiter != null) {
            this.usernameRateLimiter.hit(username);
        }
        throw e;
    }
}
Also used : User(com.serotonin.m2m2.vo.User) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) CredentialsExpiredException(org.springframework.security.authentication.CredentialsExpiredException) AuthenticationException(org.springframework.security.core.AuthenticationException) InternalAuthenticationServiceException(org.springframework.security.authentication.InternalAuthenticationServiceException) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) UserDetails(org.springframework.security.core.userdetails.UserDetails) WebAuthenticationDetails(org.springframework.security.web.authentication.WebAuthenticationDetails) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 75 with ValidationException

use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by MangoAutomation.

the class NumericPointValueWithDifferentSeriesIdTest method createMockDataPoints.

@Override
protected List<IDataPoint> createMockDataPoints(int count) {
    List<IDataPoint> points = super.createMockDataPoints(count);
    // Change series id
    DataPointService service = Common.getBean(DataPointService.class);
    DataPointDao dao = Common.getBean(DataPointDao.class);
    for (IDataPoint point : points) {
        DataPointVO vo = (DataPointVO) point;
        vo.setSeriesId(dao.insertNewTimeSeries());
        try {
            service.update(vo.getId(), vo);
        } catch (ValidationException e) {
            String failureMessage = "";
            for (ProcessMessage m : e.getValidationResult().getMessages()) {
                String messagePart = m.getContextKey() + " -> " + m.getContextualMessage().translate(Common.getTranslations()) + "\n";
                failureMessage += messagePart;
            }
            fail(failureMessage);
        }
    }
    return points;
}
Also used : DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) ProcessMessage(com.serotonin.m2m2.i18n.ProcessMessage)

Aggregations

ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)75 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)31 JsonException (com.serotonin.json.JsonException)22 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)20 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)16 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)15 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)15 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)14 JsonValue (com.serotonin.json.type.JsonValue)10 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)9 JsonObject (com.serotonin.json.type.JsonObject)8 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)8 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)7 DataPointService (com.infiniteautomation.mango.spring.service.DataPointService)6 JsonArray (com.serotonin.json.type.JsonArray)6 DataPointWithEventDetectors (com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors)6 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)6 ArrayList (java.util.ArrayList)6 ExpectValidationException (com.infiniteautomation.mango.rules.ExpectValidationException)4 PublishedPointService (com.infiniteautomation.mango.spring.service.PublishedPointService)4