use of com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException in project ma-modules-public by infiniteautomation.
the class PointValueRestController method putPointsValues.
@ApiOperation(value = "Update one or many data point's current value", notes = "Each data point must exist and be enabled")
@RequestMapping(method = RequestMethod.PUT, produces = { "application/json" }, consumes = { "application/json" })
public ResponseEntity<List<XidPointValueTimeModel>> putPointsValues(HttpServletRequest request, @RequestBody(required = true) List<XidPointValueTimeModel> models, @ApiParam(value = "Return converted value using displayed unit", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean unitConversion) throws RestValidationFailedException {
RestProcessResult<List<XidPointValueTimeModel>> result = new RestProcessResult<List<XidPointValueTimeModel>>(HttpStatus.OK);
List<XidPointValueTimeModel> setValues = new ArrayList<XidPointValueTimeModel>();
User user = this.checkUser(request, result);
if (result.isOk()) {
for (XidPointValueTimeModel model : models) {
RestProcessResult<PointValueTimeModel> pointResult = setPointValue(user, model.getXid(), model, unitConversion, ServletUriComponentsBuilder.fromContextPath(request));
if (pointResult.getHighestStatus().value() == HttpStatus.CREATED.value()) {
// Save the model for later
setValues.add(model);
}
for (RestMessage message : pointResult.getRestMessages()) {
result.addRestMessage(message);
}
}
if (setValues.size() > 0)
return result.createResponseEntity(setValues);
}
return result.createResponseEntity();
}
use of com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException in project ma-modules-public by infiniteautomation.
the class BackgroundProcessingRestController method setLowPrioritySettings.
@ApiOperation(value = "Update low priority service settings", notes = "Only corePoolSize and maximumPoolSize are used")
@RequestMapping(method = RequestMethod.PUT, produces = { "application/json" }, value = "/low-priority-thread-pool-settings")
public ResponseEntity<ThreadPoolSettingsModel> setLowPrioritySettings(@ApiParam(value = "Settings", required = true, allowMultiple = false) @RequestBody ThreadPoolSettingsModel model, HttpServletRequest request) throws RestValidationFailedException {
RestProcessResult<ThreadPoolSettingsModel> result = new RestProcessResult<ThreadPoolSettingsModel>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
if (Permissions.hasAdmin(user)) {
// Validate the settings
int currentCorePoolSize = SystemSettingsDao.getIntValue(SystemSettingsDao.LOW_PRI_CORE_POOL_SIZE);
if ((model.getCorePoolSize() != null) && (model.getCorePoolSize() < BackgroundProcessing.LOW_PRI_MAX_POOL_SIZE_MIN)) {
// Test to ensure we aren't setting too low
model.getMessages().add(new RestValidationMessage(new TranslatableMessage("validate.greaterThanOrEqualTo", BackgroundProcessing.LOW_PRI_MAX_POOL_SIZE_MIN), RestMessageLevel.ERROR, "corePoolSize"));
result.addRestMessage(this.getValidationFailedError());
} else if (!validate(model, currentCorePoolSize, model.getCorePoolSize() == null ? currentCorePoolSize : model.getCorePoolSize())) {
result.addRestMessage(this.getValidationFailedError());
} else {
if (model.getCorePoolSize() != null) {
Common.backgroundProcessing.setLowPriorityServiceCorePoolSize(model.getCorePoolSize());
SystemSettingsDao.instance.setIntValue(SystemSettingsDao.LOW_PRI_CORE_POOL_SIZE, model.getCorePoolSize());
} else {
// Get the info for the user
int corePoolSize = Common.backgroundProcessing.getLowPriorityServiceCorePoolSize();
model.setCorePoolSize(corePoolSize);
}
if (model.getMaximumPoolSize() == null) {
// Get the info for the user
int maximumPoolSize = Common.backgroundProcessing.getLowPriorityServiceMaximumPoolSize();
model.setMaximumPoolSize(maximumPoolSize);
}
// Get the settings for the model
int activeCount = Common.backgroundProcessing.getLowPriorityServiceActiveCount();
int largestPoolSize = Common.backgroundProcessing.getLowPriorityServiceLargestPoolSize();
model.setActiveCount(activeCount);
model.setLargestPoolSize(largestPoolSize);
}
return result.createResponseEntity(model);
} else {
LOG.warn("Non admin user: " + user.getUsername() + " attempted to set low priority thread pool settings.");
result.addRestMessage(this.getUnauthorizedMessage());
return result.createResponseEntity();
}
}
return result.createResponseEntity();
}
use of com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException in project ma-modules-public by infiniteautomation.
the class ServerRestController method restart.
@ApiOperation(value = "Restart Mango", notes = "Returns URL for status updates while web interface is still active")
@RequestMapping(method = RequestMethod.PUT, produces = { "application/json" }, value = "/restart")
public ResponseEntity<String> restart(UriComponentsBuilder builder, HttpServletRequest request) throws RestValidationFailedException {
RestProcessResult<String> result = new RestProcessResult<String>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
if (Permissions.hasAdmin(user)) {
ProcessResult r = ModulesDwr.scheduleRestart();
if (r.getData().get("shutdownUri") != null) {
// TODO Make SystemStatus web socket and push out message around shutdown
URI location = builder.path("/status/mango").buildAndExpand().toUri();
result.addRestMessage(getResourceUpdatedMessage(location));
} else {
result.addRestMessage(HttpStatus.NOT_MODIFIED, new TranslatableMessage("modules.restartAlreadyScheduled"));
}
} else {
LOG.warn("Non admin user: " + user.getUsername() + " attempted to restart Mango.");
result.addRestMessage(this.getUnauthorizedMessage());
}
}
return result.createResponseEntity();
}
use of com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException in project ma-modules-public by infiniteautomation.
the class ServerRestController method sendTestEmail.
@ApiOperation(value = "Send a test email", notes = "Sends email to supplied address")
@RequestMapping(method = RequestMethod.PUT, consumes = { "application/json", "text/csv" }, produces = { "application/json", "text/csv" }, value = "/email/test")
public ResponseEntity<String> sendTestEmail(@RequestParam(value = "email", required = true, defaultValue = "") String email, @RequestParam(value = "username", required = true, defaultValue = "") String username, HttpServletRequest request) throws RestValidationFailedException {
RestProcessResult<String> result = new RestProcessResult<String>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
if (Permissions.hasAdmin(user)) {
try {
Translations translations = Common.getTranslations();
Map<String, Object> model = new HashMap<>();
model.put("message", new TranslatableMessage("ftl.userTestEmail", username));
MangoEmailContent cnt = new MangoEmailContent("testEmail", model, translations, translations.translate("ftl.testEmail"), Common.UTF8);
EmailWorkItem.queueEmail(email, cnt);
return result.createResponseEntity(new TranslatableMessage("common.testEmailSent", email).translate(Common.getTranslations()));
} catch (Exception e) {
result.addRestMessage(this.getInternalServerErrorMessage(e.getMessage()));
}
} else {
LOG.warn("Non admin user: " + user.getUsername() + " attempted to send a test email.");
result.addRestMessage(this.getUnauthorizedMessage());
}
}
return result.createResponseEntity();
}
use of com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException in project ma-modules-public by infiniteautomation.
the class UserCommentRestController method updateUserComment.
@ApiOperation(value = "Updates a user comment")
@RequestMapping(method = RequestMethod.PUT, consumes = { "application/json" }, produces = { "application/json" }, value = "/{xid}")
public ResponseEntity<UserCommentModel> updateUserComment(@PathVariable String xid, @RequestBody(required = true) UserCommentModel model, UriComponentsBuilder builder, HttpServletRequest request) throws RestValidationFailedException {
RestProcessResult<UserCommentModel> result = new RestProcessResult<UserCommentModel>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
UserCommentVO u = UserCommentDao.instance.getByXid(xid);
if (u == null) {
result.addRestMessage(getDoesNotExistMessage());
return result.createResponseEntity();
} else {
// Change the owner
if (model.getUserId() == 0) {
model.setUserId(user.getId());
model.setUsername(user.getUsername());
}
// Check permissions
if (hasEditPermission(model.getData(), user)) {
// Validate and Update
if (!model.validate()) {
result.addRestMessage(this.getValidationFailedError());
} else {
UserCommentDao.instance.save(model.getData());
URI location = builder.path("v1/comments/{xid}").buildAndExpand(model.getXid()).toUri();
result.addRestMessage(getResourceUpdatedMessage(location));
}
return result.createResponseEntity(model);
} else {
result.addRestMessage(this.getUnauthorizedMessage());
return result.createResponseEntity();
}
}
}
return result.createResponseEntity();
}
Aggregations