Search in sources :

Example 1 with FileStore

use of com.serotonin.m2m2.vo.FileStore 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 2 with FileStore

use of com.serotonin.m2m2.vo.FileStore in project ma-modules-public by infiniteautomation.

the class ScriptRestController method evalScript.

@Async
@ApiOperation(value = "Evaluate a filestore file as a script on the backend using a scripting engine")
@RequestMapping(method = RequestMethod.POST, value = "/eval-file-store/{fileStoreName}/**")
public CompletableFuture<Void> evalScript(@ApiParam(value = "File store name", required = true) @PathVariable(required = true) String fileStoreName, @ApiParam(value = "Script engine name", required = false) @RequestParam(required = false) String engineName, @ApiParam(value = "Script file character set", required = false, defaultValue = "UTF-8") @RequestParam(required = false, defaultValue = "UTF-8") String fileCharset, @ApiParam(value = "Script roles", required = false, allowMultiple = true) @RequestParam(required = false) String[] roles, @ApiIgnore @RemainingPath String path, @AuthenticationPrincipal PermissionHolder user, HttpServletRequest request, HttpServletResponse response) throws IOException {
    Path filePath = fileStoreService.getPathForRead(fileStoreName, path);
    if (!Files.exists(filePath)) {
        throw new NotFoundException();
    }
    if (engineName == null) {
        engineName = scriptService.findEngineForFile(filePath);
    }
    Charset fileCharsetParsed = Charset.forName(fileCharset);
    Set<Role> roleSet;
    if (roles != null) {
        roleSet = Arrays.stream(roles).map(xid -> this.roleService.get(xid).getRole()).collect(Collectors.toSet());
    } else {
        roleSet = user.getRoles();
    }
    EvalContext evalContext = new EvalContext();
    Reader reader = new BufferedReader(new InputStreamReader(request.getInputStream(), Charset.forName(request.getCharacterEncoding())));
    Writer writer = new OutputStreamWriter(response.getOutputStream(), Charset.forName(response.getCharacterEncoding()));
    evalContext.setReader(reader);
    evalContext.setWriter(writer);
    evalContext.addBinding("reader", reader);
    evalContext.addBinding("writer", writer);
    if (permissionService.hasPermission(user, requestResponsePermission.getPermission())) {
        evalContext.addBinding("request", request);
        evalContext.addBinding("response", response);
    }
    this.scriptService.eval(new PathMangoScript(engineName, roleSet, filePath, fileCharsetParsed), evalContext);
    return CompletableFuture.completedFuture(null);
}
Also used : Path(java.nio.file.Path) RemainingPath(com.infiniteautomation.mango.rest.latest.resolver.RemainingPath) Role(com.serotonin.m2m2.vo.role.Role) InputStreamReader(java.io.InputStreamReader) EvalContext(com.infiniteautomation.mango.spring.script.EvalContext) BufferedReader(java.io.BufferedReader) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) Charset(java.nio.charset.Charset) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) PathMangoScript(com.infiniteautomation.mango.spring.script.PathMangoScript) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) Async(org.springframework.scheduling.annotation.Async) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with FileStore

use of com.serotonin.m2m2.vo.FileStore in project ma-modules-public by infiniteautomation.

the class UserFileStoresRestController method updateUserFileStore.

@ApiOperation(value = "Update a user file store")
@RequestMapping(method = RequestMethod.PUT, value = "/{xid}")
public ResponseEntity<FileStoreModel> updateUserFileStore(@ApiParam(value = "File store XID", required = true) @PathVariable("xid") String xid, @ApiParam(value = "Updated file store", required = true) @RequestBody FileStoreModel fileStore, UriComponentsBuilder builder) {
    FileStore updated = this.fileStoreService.update(xid, fileStore.toVO());
    URI location = builder.path("/user-file-stores/{xid}").buildAndExpand(updated.getXid()).toUri();
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(location);
    return new ResponseEntity<>(new FileStoreModel(updated), headers, HttpStatus.OK);
}
Also used : FileStoreModel(com.infiniteautomation.mango.rest.latest.model.filestore.FileStoreModel) FileStore(com.serotonin.m2m2.vo.FileStore) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) URI(java.net.URI) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with FileStore

use of com.serotonin.m2m2.vo.FileStore in project ma-modules-public by infiniteautomation.

the class UserFileStoresRestController method getUserFileStoreModel.

@ApiOperation(value = "Get a user file store model")
@RequestMapping(method = RequestMethod.GET, value = "/{xid}")
public MappingJacksonValue getUserFileStoreModel(@PathVariable("xid") String xid, @AuthenticationPrincipal PermissionHolder user) {
    FileStore fs = this.fileStoreService.get(xid);
    // Seeing the permissions fields should require write protection
    MappingJacksonValue resultWithView = new MappingJacksonValue(new FileStoreModel(fs));
    if (fileStoreService.hasEditPermission(user, fs)) {
        resultWithView.setSerializationView(RoleViews.ShowRoles.class);
    } else {
        resultWithView.setSerializationView(Object.class);
    }
    return resultWithView;
}
Also used : FileStoreModel(com.infiniteautomation.mango.rest.latest.model.filestore.FileStoreModel) FileStore(com.serotonin.m2m2.vo.FileStore) MappingJacksonValue(org.springframework.http.converter.json.MappingJacksonValue) RoleViews(com.infiniteautomation.mango.rest.latest.model.RoleViews) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with FileStore

use of com.serotonin.m2m2.vo.FileStore in project ma-core-public by infiniteautomation.

the class FileStoreDao method mapRecord.

@Override
public FileStore mapRecord(Record record) {
    FileStore result = new FileStore();
    result.setId(record.get(table.id));
    result.setXid(record.get(table.xid));
    result.setName(record.get(table.name));
    result.setReadPermission(new MangoPermission(record.get(table.readPermissionId)));
    result.setWritePermission(new MangoPermission(record.get(table.writePermissionId)));
    return result;
}
Also used : FileStore(com.serotonin.m2m2.vo.FileStore) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Aggregations

FileStore (com.serotonin.m2m2.vo.FileStore)21 Path (java.nio.file.Path)10 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)9 IOException (java.io.IOException)8 TranslatableIllegalArgumentException (com.infiniteautomation.mango.util.exception.TranslatableIllegalArgumentException)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)4 ApiOperation (io.swagger.annotations.ApiOperation)4 FileStoreModel (com.infiniteautomation.mango.rest.latest.model.filestore.FileStoreModel)3 TranslatableRuntimeException (com.infiniteautomation.mango.util.exception.TranslatableRuntimeException)3 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)3 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)3 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)3 NoSuchFileException (java.nio.file.NoSuchFileException)3 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)2 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 ConditionSortLimit (com.infiniteautomation.mango.db.query.ConditionSortLimit)1 RoleViews (com.infiniteautomation.mango.rest.latest.model.RoleViews)1