Search in sources :

Example 46 with RestProcessResult

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult in project ma-modules-public by infiniteautomation.

the class JsonDataRestController method list.

@ApiOperation(value = "List all available xids", notes = "Shows any xids that you have read permissions for", response = List.class)
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<List<String>> list(HttpServletRequest request) {
    RestProcessResult<List<String>> result = new RestProcessResult<List<String>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        List<JsonDataVO> all = this.dao.getAll();
        List<String> available = new ArrayList<String>();
        for (JsonDataVO vo : all) {
            // Check existing permissions
            if (Permissions.hasPermission(user, vo.getReadPermission())) {
                available.add(vo.getXid());
            }
        }
        return result.createResponseEntity(available);
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) JsonDataVO(com.serotonin.m2m2.vo.json.JsonDataVO) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 47 with RestProcessResult

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult in project ma-modules-public by infiniteautomation.

the class PointHierarchyRestController method getFolder.

/**
 * Get the folder via a name
 * @param folderName
 * @param request
 * @return
 */
@ApiOperation(value = "Get point hierarchy folder by name", notes = "Points returned based on user priviledges")
@RequestMapping(method = RequestMethod.GET, value = "/by-name/{folderName}", produces = { "application/json" })
public ResponseEntity<JsonStream<PointHierarchyModel>> getFolder(@PathVariable String folderName, @RequestParam(name = "subfolders", defaultValue = "true") boolean getSubFolders, @RequestParam(name = "points", defaultValue = "true") boolean getPoints, HttpServletRequest request) {
    RestProcessResult<JsonStream<PointHierarchyModel>> result = new RestProcessResult<JsonStream<PointHierarchyModel>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        PointHierarchy ph = DataPointDao.instance.getPointHierarchy(true);
        PointFolder folder = ph.getRoot();
        PointFolder desiredFolder = null;
        if (folder.getName().equals(folderName))
            desiredFolder = folder;
        else
            desiredFolder = recursiveFolderSearch(folder, folderName);
        if (desiredFolder == null) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        } else {
            PointHiearchyFolderStream stream = new PointHiearchyFolderStream(desiredFolder, user, getSubFolders, getPoints);
            return result.createResponseEntity(stream);
        }
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) PointHierarchyModel(com.serotonin.m2m2.web.mvc.rest.v1.model.PointHierarchyModel) User(com.serotonin.m2m2.vo.User) PointHierarchy(com.serotonin.m2m2.vo.hierarchy.PointHierarchy) PointFolder(com.serotonin.m2m2.vo.hierarchy.PointFolder) JsonStream(com.serotonin.m2m2.web.mvc.rest.v1.model.JsonStream) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 48 with RestProcessResult

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult in project ma-modules-public by infiniteautomation.

the class ServerRestController method queryTimezone.

@ApiOperation(value = "Query Timezones", notes = "", response = TimezoneModel.class, responseContainer = "Array")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/timezones")
public ResponseEntity<PageQueryResultModel<TimezoneModel>> queryTimezone(HttpServletRequest request) {
    RestProcessResult<PageQueryResultModel<TimezoneModel>> result = new RestProcessResult<PageQueryResultModel<TimezoneModel>>(HttpStatus.OK);
    this.checkUser(request, result);
    if (result.isOk()) {
        try {
            // Parse the RQL Query
            ASTNode root = parseRQLtoAST(request.getQueryString());
            List<TimezoneModel> list = root.accept(new RQLToObjectListQuery<TimezoneModel>(), allTimezones);
            PageQueryResultModel<TimezoneModel> model = new PageQueryResultModel<TimezoneModel>(list, allTimezones.size() + 1);
            return result.createResponseEntity(model);
        } catch (InvalidRQLRestException e) {
            LOG.error(e.getMessage(), e);
            result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
            return result.createResponseEntity();
        }
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) InvalidRQLRestException(com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException) ASTNode(net.jazdw.rql.parser.ASTNode) TimezoneModel(com.serotonin.m2m2.web.mvc.rest.v1.model.system.TimezoneModel) PageQueryResultModel(com.serotonin.m2m2.web.mvc.rest.v1.model.PageQueryResultModel) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 49 with RestProcessResult

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult in project ma-modules-public by infiniteautomation.

the class ServerRestController method getSystemInfo.

@ApiOperation(value = "System Info", notes = "Provides disk use, db sizes and point, event counts", response = Map.class)
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/system-info")
public ResponseEntity<SystemInfoModel> getSystemInfo(HttpServletRequest request) {
    RestProcessResult<SystemInfoModel> result = new RestProcessResult<SystemInfoModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (user.isAdmin()) {
            SystemInfoModel model = new SystemInfoModel();
            // Database size
            model.setSqlDbSizeBytes(Common.databaseProxy.getDatabaseSizeInBytes());
            // Do we have any NoSQL Data
            if (Common.databaseProxy.getNoSQLProxy() != null) {
                String pointValueStoreName = Common.envProps.getString("db.nosql.pointValueStoreName", "mangoTSDB");
                model.setNoSqlDbSizeBytes(Common.databaseProxy.getNoSQLProxy().getDatabaseSizeInBytes(pointValueStoreName));
            }
            // Filedata data
            DirectoryInfo fileDatainfo = DirectoryUtils.getSize(new File(Common.getFiledataPath()));
            model.setFileDataSizeBytes(fileDatainfo.getSize());
            // Point history counts.
            model.setTopPoints(DataPointDao.instance.getTopPointHistoryCounts());
            model.setEventCount(EventDao.instance.getEventCount());
            // Disk Info
            FileSystem fs = FileSystems.getDefault();
            List<DiskInfoModel> disks = new ArrayList<DiskInfoModel>();
            model.setDisks(disks);
            for (Path root : fs.getRootDirectories()) {
                try {
                    FileStore store = Files.getFileStore(root);
                    DiskInfoModel disk = new DiskInfoModel();
                    disk.setName(root.getRoot().toString());
                    disk.setTotalSpaceBytes(store.getTotalSpace());
                    disk.setUsableSpaceBytes(store.getUsableSpace());
                    disks.add(disk);
                } catch (IOException e) {
                }
            }
            // CPU Info
            OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
            model.setLoadAverage(osBean.getSystemLoadAverage());
            // OS Info
            model.setArchitecture(osBean.getArch());
            model.setOperatingSystem(osBean.getName());
            model.setOsVersion(osBean.getVersion());
            return result.createResponseEntity(model);
        } else {
            result.addRestMessage(HttpStatus.UNAUTHORIZED, new TranslatableMessage("common.default", "User not admin"));
        }
    }
    return result.createResponseEntity();
}
Also used : Path(java.nio.file.Path) SystemInfoModel(com.serotonin.m2m2.web.mvc.rest.v1.model.system.SystemInfoModel) User(com.serotonin.m2m2.vo.User) ArrayList(java.util.ArrayList) DiskInfoModel(com.serotonin.m2m2.web.mvc.rest.v1.model.system.DiskInfoModel) IOException(java.io.IOException) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) FileStore(java.nio.file.FileStore) FileSystem(java.nio.file.FileSystem) DirectoryInfo(com.serotonin.util.DirectoryInfo) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) File(java.io.File) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 50 with RestProcessResult

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult in project ma-modules-public by infiniteautomation.

the class UserCommentRestController method createNewUserComment.

/**
 * Create a new User Comment
 *
 * The timestamp and UserID are optional
 * Username is not used for input
 *
 * @param model
 * @param request
 * @return
 * @throws RestValidationFailedException
 */
@ApiOperation(value = "Create New User Comment", notes = "")
@RequestMapping(method = RequestMethod.POST, consumes = { "application/json", "text/csv" }, produces = { "application/json", "text/csv" })
public ResponseEntity<UserCommentModel> createNewUserComment(@ApiParam(value = "User Comment to save", required = true) @RequestBody(required = true) UserCommentModel model, UriComponentsBuilder builder, HttpServletRequest request) throws RestValidationFailedException {
    RestProcessResult<UserCommentModel> result = new RestProcessResult<UserCommentModel>(HttpStatus.CREATED);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        // Assign a userId if there isn't one
        if (model.getUserId() == 0) {
            model.setUserId(user.getId());
            model.setUsername(user.getUsername());
        }
        // Don't let non admin users create notes from other people
        if (!hasEditPermission(model.getData(), user)) {
            result.addRestMessage(this.getUnauthorizedMessage());
            return result.createResponseEntity();
        }
        if (model.getTimestamp() <= 0) {
            model.setTimestamp(System.currentTimeMillis());
        }
        if (model.validate()) {
            try {
                String initiatorId = request.getHeader("initiatorId");
                UserCommentDao.instance.save(model.getData(), initiatorId);
                LOG.info("User with name/id: " + user.getUsername() + "/" + user.getId() + " created a User Comment for user: " + model.getData().getUserId());
                URI location = builder.path("v1/comments/{xid}").buildAndExpand(model.getXid()).toUri();
                result.addRestMessage(getResourceCreatedMessage(location));
                return result.createResponseEntity(model);
            } catch (Exception e) {
                result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
                return result.createResponseEntity();
            }
        } else {
            result.addRestMessage(this.getValidationFailedError());
            return result.createResponseEntity(model);
        }
    }
    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) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) InvalidRQLRestException(com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)132 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)125 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)125 User (com.serotonin.m2m2.vo.User)113 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)33 ArrayList (java.util.ArrayList)30 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)29 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)28 List (java.util.List)27 InvalidRQLRestException (com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException)23 ASTNode (net.jazdw.rql.parser.ASTNode)23 RestValidationFailedException (com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException)22 URI (java.net.URI)18 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)15 HashMap (java.util.HashMap)14 ApiResponses (com.wordnik.swagger.annotations.ApiResponses)13 ValidationFailedRestException (com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException)11 RTException (com.serotonin.m2m2.rt.RTException)11 DataPointModel (com.serotonin.m2m2.web.mvc.rest.v1.model.DataPointModel)11 QueryDataPageStream (com.serotonin.m2m2.web.mvc.rest.v1.model.QueryDataPageStream)11