Search in sources :

Example 1 with JsonDataModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.jsondata.JsonDataModel in project ma-modules-public by infiniteautomation.

the class JsonDataRestController method getPublicData.

@ApiOperation(value = "Get Public JSON Data", notes = "Returns only the data")
@RequestMapping(method = RequestMethod.GET, value = "/public/{xid}")
public ResponseEntity<JsonDataModel> getPublicData(HttpServletRequest request, @ApiParam(value = "XID", required = true, allowMultiple = false) @PathVariable String xid) {
    RestProcessResult<JsonDataModel> result = new RestProcessResult<JsonDataModel>(HttpStatus.OK);
    JsonDataVO vo = JsonDataDao.instance.getByXid(xid);
    if (vo == null) {
        result.addRestMessage(getDoesNotExistMessage());
        return result.createResponseEntity();
    } else {
        // Check existing permissions
        if (!vo.isPublicData()) {
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        } else {
            return result.createResponseEntity(new JsonDataModel(vo));
        }
    }
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) JsonDataVO(com.serotonin.m2m2.vo.json.JsonDataVO) JsonDataModel(com.serotonin.m2m2.web.mvc.rest.v1.model.jsondata.JsonDataModel) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with JsonDataModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.jsondata.JsonDataModel in project ma-modules-public by infiniteautomation.

the class JsonDataRestController method getDataWithPath.

@ApiOperation(value = "Get JSON Data using a path", notes = "To get a sub component of the data use a path of member.submember")
@RequestMapping(method = RequestMethod.GET, value = "/{xid}/{path:.*}")
public ResponseEntity<JsonDataModel> getDataWithPath(HttpServletRequest request, @ApiParam(value = "XID", required = true, allowMultiple = false) @PathVariable String xid, @ApiParam(value = "Data path using dots as separator", required = true, allowMultiple = false) @PathVariable String path) throws UnsupportedEncodingException {
    RestProcessResult<JsonDataModel> result = new RestProcessResult<JsonDataModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        JsonDataVO vo = JsonDataDao.instance.getByXid(xid);
        if (vo == null) {
            result.addRestMessage(getDoesNotExistMessage());
        } else {
            // Check existing permissions
            if (!Permissions.hasPermission(user, vo.getReadPermission())) {
                result.addRestMessage(getUnauthorizedMessage());
                return result.createResponseEntity();
            }
            String[] pathParts = splitAndDecodePath(path);
            if (pathParts.length == 0) {
                return result.createResponseEntity(new JsonDataModel(vo));
            } else {
                JsonNode data = (JsonNode) vo.getJsonData();
                JsonNode subNode = getNode(data, pathParts);
                vo.setJsonData(subNode);
                return result.createResponseEntity(new JsonDataModel(vo));
            }
        }
    }
    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) JsonDataModel(com.serotonin.m2m2.web.mvc.rest.v1.model.jsondata.JsonDataModel) JsonNode(com.fasterxml.jackson.databind.JsonNode) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with JsonDataModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.jsondata.JsonDataModel in project ma-modules-public by infiniteautomation.

the class JsonDataRestController method deletePartialJsonData.

@ApiOperation(value = "Partially Delete JSON Data", notes = "{path} is the path to data with dots data.member.submember", response = JsonDataModel.class)
@ApiResponses({ @ApiResponse(code = 201, message = "Data Deleted", response = JsonDataModel.class), @ApiResponse(code = 401, message = "Unauthorized Access", response = ResponseEntity.class), @ApiResponse(code = 403, message = "Data Doesn't Exists") })
@RequestMapping(method = RequestMethod.DELETE, value = "/{xid}/{path:.*}")
public ResponseEntity<JsonDataModel> deletePartialJsonData(@ApiParam(value = "XID", required = true, allowMultiple = false) @PathVariable String xid, @ApiParam(value = "Data path using dots as separator", required = true, allowMultiple = false) @PathVariable String path, UriComponentsBuilder builder, HttpServletRequest request) throws RestValidationFailedException {
    RestProcessResult<JsonDataModel> result = new RestProcessResult<JsonDataModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        JsonDataVO vo = this.dao.getByXid(xid);
        if (vo != null) {
            // Check existing permissions
            if (!Permissions.hasPermission(user, vo.getEditPermission())) {
                result.addRestMessage(getUnauthorizedMessage());
                return result.createResponseEntity();
            }
            JsonDataModel model = new JsonDataModel(vo);
            String[] pathParts;
            if (path == null || (pathParts = path.split("\\.")).length == 0) {
                // Delete the whole thing
                this.dao.delete(vo.getId());
            } else {
                // Delete something from the map
                JsonNode existingData = (JsonNode) vo.getJsonData();
                boolean deleted = deleteNode(existingData, pathParts);
                if (!deleted) {
                    result.addRestMessage(getDoesNotExistMessage());
                    return result.createResponseEntity();
                }
                if (!model.validate()) {
                    result.addRestMessage(this.getValidationFailedError());
                    return result.createResponseEntity(model);
                }
                try {
                    String initiatorId = request.getHeader("initiatorId");
                    this.dao.save(vo, initiatorId);
                } catch (Exception e) {
                    LOG.error(e.getMessage(), e);
                    result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
                }
            }
            return result.createResponseEntity(model);
        } else {
            result.addRestMessage(getDoesNotExistMessage());
        }
    }
    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) JsonDataModel(com.serotonin.m2m2.web.mvc.rest.v1.model.jsondata.JsonDataModel) JsonNode(com.fasterxml.jackson.databind.JsonNode) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) ApiResponses(com.wordnik.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with JsonDataModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.jsondata.JsonDataModel in project ma-modules-public by infiniteautomation.

the class JsonDataRestController method modifyJsonData.

/**
 * Helper to modify data
 * @param result
 * @param xid
 * @param path
 * @param readPermissions
 * @param editPermissions
 * @param name
 * @param data
 * @param builder
 * @param request
 * @return
 */
private ResponseEntity<JsonDataModel> modifyJsonData(MapOperation operation, RestProcessResult<JsonDataModel> result, String xid, String[] pathParts, Set<String> readPermissions, Set<String> editPermissions, String name, boolean publicData, JsonNode data, UriComponentsBuilder builder, HttpServletRequest request) {
    // check we are using this method only for replace and append
    if (operation != MapOperation.REPLACE && operation != MapOperation.APPEND)
        throw new IllegalArgumentException();
    User user = this.checkUser(request, result);
    if (!result.isOk()) {
        return result.createResponseEntity();
    }
    JsonNode dataToReturn = data;
    JsonDataVO vo = this.dao.getByXid(xid);
    if (vo != null) {
        // Check existing permissions
        if (!Permissions.hasPermission(user, vo.getEditPermission())) {
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        }
        // Replace the data
        vo.setName(name);
        vo.setPublicData(publicData);
        vo.setReadPermission(Permissions.implodePermissionGroups(readPermissions));
        vo.setEditPermission(Permissions.implodePermissionGroups(editPermissions));
        JsonNode existingData = (JsonNode) vo.getJsonData();
        if (operation == MapOperation.REPLACE) {
            JsonNode newData = replaceNode(existingData, pathParts, data);
            vo.setJsonData(newData);
        } else if (operation == MapOperation.APPEND) {
            dataToReturn = mergeNode(existingData, pathParts, data);
        }
    } else {
        // can't append/merge to a non-existing object or replace data at a path of a non existing object
        if (operation == MapOperation.APPEND || pathParts.length > 0) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        }
        // Going to create a new one
        vo = new JsonDataVO();
        vo.setXid(xid);
        vo.setName(name);
        vo.setPublicData(publicData);
        vo.setReadPermission(Permissions.implodePermissionGroups(readPermissions));
        vo.setEditPermission(Permissions.implodePermissionGroups(editPermissions));
        vo.setJsonData(data);
    }
    JsonDataModel model = new JsonDataModel(vo);
    if (!model.validate()) {
        result.addRestMessage(this.getValidationFailedError());
        // return only the data that was saved, i.e. the data that we supplied a path to
        vo.setJsonData(data);
        return result.createResponseEntity(model);
    }
    // Ensure we have the correct permissions
    // First we must check to ensure that the User actually has editPermission before they can save it otherwise
    // they won't be able to modify it.
    Set<String> userPermissions = Permissions.explodePermissionGroups(user.getPermissions());
    if (!user.isAdmin() && Collections.disjoint(userPermissions, editPermissions)) {
        // Return validation error
        result.addRestMessage(this.getValidationFailedError());
        model.addValidationMessage("jsonData.editPermissionRequired", RestMessageLevel.ERROR, "editPermission");
        vo.setJsonData(data);
        return result.createResponseEntity(model);
    }
    try {
        String initiatorId = request.getHeader("initiatorId");
        this.dao.save(vo, initiatorId);
        // return only the data that was saved, i.e. the data that we supplied a path to
        vo.setJsonData(dataToReturn);
        URI location = builder.path("/v1/json-data/{xid}").buildAndExpand(new Object[] { vo.getXid() }).toUri();
        result.addRestMessage(this.getResourceCreatedMessage(location));
        return result.createResponseEntity(model);
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
    }
    return result.createResponseEntity();
}
Also used : User(com.serotonin.m2m2.vo.User) JsonDataVO(com.serotonin.m2m2.vo.json.JsonDataVO) JsonDataModel(com.serotonin.m2m2.web.mvc.rest.v1.model.jsondata.JsonDataModel) JsonNode(com.fasterxml.jackson.databind.JsonNode) URI(java.net.URI) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

JsonDataVO (com.serotonin.m2m2.vo.json.JsonDataVO)4 JsonDataModel (com.serotonin.m2m2.web.mvc.rest.v1.model.jsondata.JsonDataModel)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 User (com.serotonin.m2m2.vo.User)3 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)3 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)2 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)2 RestValidationFailedException (com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ApiResponses (com.wordnik.swagger.annotations.ApiResponses)1 URI (java.net.URI)1