Search in sources :

Example 26 with TranslatableMessage

use of com.serotonin.m2m2.i18n.TranslatableMessage in project ma-modules-public by infiniteautomation.

the class EventHandlerRestController method update.

@ApiOperation(value = "Update an existing event handler", notes = "")
@RequestMapping(method = RequestMethod.PUT, consumes = { "application/json" }, produces = { "application/json" }, value = "/{xid}")
public ResponseEntity<AbstractEventHandlerModel<?>> update(@PathVariable String xid, @ApiParam(value = "Updated model", required = true) @RequestBody(required = true) AbstractEventHandlerModel<?> model, UriComponentsBuilder builder, HttpServletRequest request) {
    RestProcessResult<AbstractEventHandlerModel<?>> result = new RestProcessResult<AbstractEventHandlerModel<?>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        AbstractEventHandlerVO<?> vo = model.getData();
        AbstractEventHandlerVO<?> existing = EventHandlerDao.instance.getByXid(xid);
        if (existing == null) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        }
        // Check Event Type Permission
        if (!Permissions.hasAdmin(user)) {
            result.addRestMessage(HttpStatus.UNAUTHORIZED, new TranslatableMessage("permissions.accessDenied", user.getUsername(), SuperadminPermissionDefinition.GROUP_NAME));
            return result.createResponseEntity();
        }
        // Ensure we keep the same ID
        vo.setId(existing.getId());
        if (!model.validate()) {
            result.addRestMessage(this.getValidationFailedError());
            return result.createResponseEntity(model);
        } else {
            String initiatorId = request.getHeader("initiatorId");
            EventHandlerDao.instance.save(vo, initiatorId);
        }
        // Put a link to the updated data in the header?
        URI location = builder.path("/v1/event-handlers/{xid}").buildAndExpand(vo.getXid()).toUri();
        result.addRestMessage(getResourceUpdatedMessage(location));
        return result.createResponseEntity(model);
    }
    // Not logged in
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) AbstractEventHandlerModel(com.serotonin.m2m2.web.mvc.rest.v1.model.events.handlers.AbstractEventHandlerModel) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 27 with TranslatableMessage

use of com.serotonin.m2m2.i18n.TranslatableMessage in project ma-modules-public by infiniteautomation.

the class JsonDataRestController method replaceNode.

JsonNode replaceNode(final JsonNode existingData, final String[] dataPath, final JsonNode newData) {
    if (dataPath.length == 0) {
        return newData;
    }
    String[] parentPath = Arrays.copyOfRange(dataPath, 0, dataPath.length - 1);
    String fieldName = dataPath[dataPath.length - 1];
    JsonNode parent = getNode(existingData, parentPath);
    if (parent.isObject()) {
        ObjectNode parentObject = (ObjectNode) parent;
        parentObject.set(fieldName, newData);
    } else if (parent.isArray()) {
        ArrayNode parentArray = (ArrayNode) parent;
        int index = toArrayIndex(fieldName);
        parentArray.set(index, newData);
    } else {
        throw new BadRequestException(new TranslatableMessage("rest.error.cantSetFieldOfNodeType", parent.getNodeType()));
    }
    return existingData;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 28 with TranslatableMessage

use of com.serotonin.m2m2.i18n.TranslatableMessage in project ma-modules-public by infiniteautomation.

the class JsonDataRestController method getNode.

JsonNode getNode(final JsonNode existingData, final String[] dataPath) {
    JsonNode node = existingData;
    for (int i = 0; i < dataPath.length; i++) {
        String fieldName = dataPath[i];
        if (node.isObject()) {
            ObjectNode objectNode = (ObjectNode) node;
            node = objectNode.get(fieldName);
        } else if (node.isArray()) {
            ArrayNode arrayNode = (ArrayNode) node;
            int index = toArrayIndex(fieldName);
            node = arrayNode.get(index);
        } else {
            throw new BadRequestException(new TranslatableMessage("rest.error.cantGetFieldOfNodeType", node.getNodeType()));
        }
        if (node == null) {
            throw new NotFoundRestException();
        }
    }
    return node;
}
Also used : NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 29 with TranslatableMessage

use of com.serotonin.m2m2.i18n.TranslatableMessage in project ma-modules-public by infiniteautomation.

the class JsonDataRestController method deleteNode.

boolean deleteNode(final JsonNode existingData, final String[] dataPath) {
    if (dataPath.length == 0)
        throw new IllegalArgumentException();
    String[] parentPath = Arrays.copyOfRange(dataPath, 0, dataPath.length - 1);
    String fieldName = dataPath[dataPath.length - 1];
    JsonNode parent = getNode(existingData, parentPath);
    JsonNode deletedValue = null;
    if (parent.isObject()) {
        ObjectNode parentObject = (ObjectNode) parent;
        deletedValue = parentObject.remove(fieldName);
    } else if (parent.isArray()) {
        ArrayNode parentArray = (ArrayNode) parent;
        int index = toArrayIndex(fieldName);
        deletedValue = parentArray.remove(index);
    } else {
        throw new BadRequestException(new TranslatableMessage("rest.error.cantDeleteFieldOfNodeType", parent.getNodeType()));
    }
    return deletedValue != null;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 30 with TranslatableMessage

use of com.serotonin.m2m2.i18n.TranslatableMessage 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)

Aggregations

TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)180 User (com.serotonin.m2m2.vo.User)53 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)52 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)52 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)33 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)33 IOException (java.io.IOException)28 HashMap (java.util.HashMap)27 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)24 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)22 ArrayList (java.util.ArrayList)22 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)20 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)20 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)19 BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)18 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)17 File (java.io.File)16 URI (java.net.URI)16 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)12 ResponseEntity (org.springframework.http.ResponseEntity)11