use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-modules-public by infiniteautomation.
the class WorkItemRestController method getWorkItemsByPriority.
@ApiOperation(value = "Get list of work items by classname", notes = "Returns the Work Item specified by the given classname and priority")
@RequestMapping(method = RequestMethod.GET, value = "/by-priority/{priority}")
public List<WorkItemInfo> getWorkItemsByPriority(@ApiParam(value = "priority", required = true, allowMultiple = false) @PathVariable String priority, @RequestParam(value = "classname", required = false, defaultValue = "") String classname, @AuthenticationPrincipal PermissionHolder user) {
permissionService.ensureAdminRole(user);
List<WorkItemInfo> list;
if (priority.equalsIgnoreCase("HIGH")) {
list = Common.backgroundProcessing.getHighPriorityServiceItems();
} else if (priority.equalsIgnoreCase("MEDIUM")) {
list = Common.backgroundProcessing.getMediumPriorityServiceQueueItems();
} else if (priority.equalsIgnoreCase("LOW")) {
list = Common.backgroundProcessing.getLowPriorityServiceQueueItems();
} else {
ProcessResult result = new ProcessResult();
result.addContextualMessage("priority", "validate.invalidValue");
throw new ValidationException(result);
}
// Filter if we need to
if (StringUtils.isNotEmpty(classname)) {
List<WorkItemInfo> modelList = new ArrayList<>();
for (WorkItemInfo model : list) {
if (model.getClassname().equalsIgnoreCase(classname)) {
modelList.add(model);
}
}
return modelList;
} else {
return list;
}
}
use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-modules-public by infiniteautomation.
the class InternalLifecycle method maybeInstallSystemMonitor.
/**
*/
private void maybeInstallSystemMonitor(boolean safe) {
DataSourceVO ds = DataSourceDao.getInstance().getByXid(SYSTEM_DATASOURCE_XID);
if (ds == null) {
// Create Data Source
DataSourceDefinition<InternalDataSourceVO> def = ModuleRegistry.getDataSourceDefinition(InternalDataSourceDefinition.DATA_SOURCE_TYPE);
ds = def.baseCreateDataSourceVO();
InternalDataSourceVO vo = (InternalDataSourceVO) ds;
vo.setXid(SYSTEM_DATASOURCE_XID);
vo.setName(SYSTEM_DATASOURCE_DEVICE_NAME);
vo.setUpdatePeriods(10);
vo.setUpdatePeriodType(TimePeriods.SECONDS);
try {
DataSourceDao.getInstance().insert(vo);
// Setup the Points
maybeCreatePoints(safe, ds);
// Enable the data source
InternalDataSourceVO existing = (InternalDataSourceVO) ds.copy();
if (!safe) {
vo.setEnabled(true);
Common.getBean(DataSourceService.class).update(existing.getId(), vo);
}
} catch (ValidationException e) {
for (ProcessMessage message : e.getValidationResult().getMessages()) {
LOG.error(message.toString(Common.getTranslations()));
}
}
} else {
// Ensure all points are added
maybeCreatePoints(safe, ds);
}
}
use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-modules-public by infiniteautomation.
the class DataPointModel method toVO.
@Override
public DataPointVO toVO() {
DataPointVO point = new DataPointVO();
if (xid != null) {
point.setXid(xid);
}
if (name != null) {
point.setName(name);
}
if (enabled != null) {
point.setEnabled(enabled);
}
if (deviceName != null) {
point.setDeviceName(deviceName);
}
// TODO Use ModelMapper.unmap
if (readPermission != null) {
point.setReadPermission(readPermission.getPermission());
}
// TODO Use ModelMapper.unmap
if (editPermission != null) {
point.setEditPermission(editPermission.getPermission());
}
// TODO Use ModelMapper.unmap
if (setPermission != null) {
point.setSetPermission(setPermission.getPermission());
}
// TODO Use ModelMapper.unmap
if (StringUtils.isNotEmpty(dataSourceXid)) {
DataSourceVO ds = DataSourceDao.getInstance().getByXid(dataSourceXid);
if (ds != null) {
point.setDataSourceId(ds.getId());
point.setDataSourceName(ds.getName());
point.setDataSourceXid(ds.getXid());
point.setDataSourceTypeName(ds.getDefinition().getDataSourceTypeName());
}
}
// TODO Use ModelMapper.unmap
if (point.getDataSourceId() <= 0 && dataSourceId != null && dataSourceId > 0) {
point.setDataSourceId(dataSourceId);
}
if (purgeOverride != null) {
point.setPurgeOverride(purgeOverride);
// Ensure that a purge period must be supplied
if (purgeOverride) {
point.setPurgePeriod(-1);
point.setPurgeType(-1);
}
}
if (purgePeriod != null) {
point.setPurgePeriod(purgePeriod.getPeriods());
point.setPurgeType(Common.TIME_PERIOD_CODES.getId(purgePeriod.getPeriodType()));
}
if (unit != null) {
try {
point.setUnit(JUnitUtil.parseLocal(unit));
} catch (IllegalArgumentException e) {
// TODO unmap
ProcessResult result = new ProcessResult();
result.addContextualMessage("unit", "validate.unitInvalid", e.getMessage());
throw new ValidationException(result);
}
}
if (useIntegralUnit != null) {
point.setUseIntegralUnit(useIntegralUnit);
}
if (integralUnit != null) {
try {
point.setIntegralUnit(JUnitUtil.parseLocal(integralUnit));
} catch (IllegalArgumentException e) {
// TODO unmap
ProcessResult result = new ProcessResult();
result.addContextualMessage("integralUnit", "validate.unitInvalid", e.getMessage());
throw new ValidationException(result);
}
}
if (useRenderedUnit != null) {
point.setUseRenderedUnit(useRenderedUnit);
}
if (this.renderedUnit != null) {
try {
point.setRenderedUnit(JUnitUtil.parseLocal(renderedUnit));
} catch (IllegalArgumentException e) {
// TODO unmap
ProcessResult result = new ProcessResult();
result.addContextualMessage("renderedUnit", "validate.unitInvalid", e.getMessage());
throw new ValidationException(result);
}
}
if (chartColour != null) {
point.setChartColour(chartColour);
}
if (plotType != null) {
point.setPlotType(DataPointVO.PLOT_TYPE_CODES.getId(plotType));
}
if (this.tags != null) {
Map<String, String> existingTags = point.getTags();
if (!this.mergeTags || existingTags == null) {
// existingTags is only null if someone tried to use mergeTags when creating a data point
point.setTags(this.tags);
} else {
Map<String, String> mergedTags = new HashMap<>(existingTags);
for (Entry<String, String> entry : this.tags.entrySet()) {
String tagKey = entry.getKey();
String tagValue = entry.getValue();
if (tagValue == null) {
mergedTags.remove(tagKey);
} else {
mergedTags.put(tagKey, tagValue);
}
}
point.setTags(mergedTags);
}
} else {
// TODO unmap
if (id != null && id > 0) {
point.setTags(DataPointTagsDao.getInstance().getTagsForDataPointId(id));
} else if (xid != null) {
Integer id = DataPointDao.getInstance().getIdByXid(xid);
if (id != null) {
point.setTags(DataPointTagsDao.getInstance().getTagsForDataPointId(id));
}
}
}
if (this.loggingProperties != null) {
loggingProperties.copyPropertiesTo(point);
}
if (this.textRenderer != null) {
point.setTextRenderer(textRenderer.toVO());
}
if (pointLocator != null) {
point.setPointLocator(pointLocator.toVO());
}
if (this.rollup != null) {
point.setRollup(Common.ROLLUP_CODES.getId(this.rollup));
}
if (this.simplifyType != null) {
point.setSimplifyType(DataPointVO.SIMPLIFY_TYPE_CODES.getId(this.simplifyType));
}
if (this.simplifyTolerance != null) {
point.setSimplifyTolerance(simplifyTolerance);
}
if (this.simplifyTarget != null) {
point.setSimplifyTarget(simplifyTarget);
}
if (this.preventSetExtremeValues != null) {
point.setPreventSetExtremeValues(this.preventSetExtremeValues);
}
if (this.setExtremeLowLimit != null) {
point.setSetExtremeLowLimit(this.setExtremeLowLimit);
}
if (this.setExtremeHighLimit != null) {
point.setSetExtremeHighLimit(this.setExtremeHighLimit);
}
if (this.data != null) {
point.setData(data);
}
return point;
}
use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by infiniteautomation.
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;
}
}
use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by infiniteautomation.
the class MangoJavaScriptService method ensureValid.
/**
* Ensure the script is valid
*/
public void ensureValid(MangoJavaScript vo) throws ValidationException {
PermissionHolder user = Common.getUser();
permissionService.ensurePermission(user, dataSourcePermissionDefinition.getPermission());
ProcessResult result = validate(vo);
if (!result.isValid())
throw new ValidationException(result);
}
Aggregations