Search in sources :

Example 16 with RestValidationFailedException

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();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) XidPointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeModel) User(com.serotonin.m2m2.vo.User) XidPointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeModel) RecentPointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.RecentPointValueTimeModel) PointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.PointValueTimeModel) RestMessage(com.serotonin.m2m2.web.mvc.rest.v1.message.RestMessage) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 17 with RestValidationFailedException

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();
}
Also used : RestValidationMessage(com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage) ThreadPoolSettingsModel(com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 18 with RestValidationFailedException

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();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 19 with RestValidationFailedException

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();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) HashMap(java.util.HashMap) MangoEmailContent(com.serotonin.m2m2.email.MangoEmailContent) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) Translations(com.serotonin.m2m2.i18n.Translations) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) InvalidRQLRestException(com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException) IOException(java.io.IOException) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 20 with RestValidationFailedException

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();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) UserCommentModel(com.serotonin.m2m2.web.mvc.rest.v1.model.comment.UserCommentModel) URI(java.net.URI) UserCommentVO(com.serotonin.m2m2.vo.comment.UserCommentVO) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)22 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)21 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)21 User (com.serotonin.m2m2.vo.User)20 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)12 RestValidationFailedException (com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException)10 InvalidRQLRestException (com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException)7 URI (java.net.URI)6 UserModel (com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel)5 IOException (java.io.IOException)5 AccessDeniedException (com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException)4 WatchListVO (com.serotonin.m2m2.watchlist.WatchListVO)4 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)3 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)3 RestValidationMessage (com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage)3 WatchListDataPointModel (com.serotonin.m2m2.web.mvc.rest.v1.model.WatchListDataPointModel)3 WatchListModel (com.serotonin.m2m2.web.mvc.rest.v1.model.WatchListModel)3 ThreadPoolSettingsModel (com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel)3 UserCommentModel (com.serotonin.m2m2.web.mvc.rest.v1.model.comment.UserCommentModel)3 ApiResponses (com.wordnik.swagger.annotations.ApiResponses)3