Search in sources :

Example 66 with ApiOperation

use of com.wordnik.swagger.annotations.ApiOperation 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 67 with ApiOperation

use of com.wordnik.swagger.annotations.ApiOperation 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 68 with ApiOperation

use of com.wordnik.swagger.annotations.ApiOperation 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 69 with ApiOperation

use of com.wordnik.swagger.annotations.ApiOperation 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 70 with ApiOperation

use of com.wordnik.swagger.annotations.ApiOperation 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)

Aggregations

ApiOperation (com.wordnik.swagger.annotations.ApiOperation)274 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)212 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)125 User (com.serotonin.m2m2.vo.User)115 URI (java.net.URI)59 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)54 ArrayList (java.util.ArrayList)50 Produces (javax.ws.rs.Produces)49 ResponseEntity (org.springframework.http.ResponseEntity)44 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)42 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)41 Path (javax.ws.rs.Path)40 Response (javax.ws.rs.core.Response)38 ApiResponses (com.wordnik.swagger.annotations.ApiResponses)36 DefaultValue (javax.ws.rs.DefaultValue)35 HeaderParam (javax.ws.rs.HeaderParam)35 ListResponse (org.gluu.oxtrust.model.scim2.ListResponse)34 ASTNode (net.jazdw.rql.parser.ASTNode)31 List (java.util.List)29 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)25