use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult in project ma-modules-public by infiniteautomation.
the class EventHandlerRestController method delete.
@ApiOperation(value = "Delete an event handler", notes = "The user must have event type permission")
@RequestMapping(method = RequestMethod.DELETE, value = "/{xid}", produces = { "application/json" })
public ResponseEntity<AbstractEventHandlerModel<?>> delete(@PathVariable String xid, UriComponentsBuilder builder, HttpServletRequest request) {
RestProcessResult<AbstractEventHandlerModel<?>> result = new RestProcessResult<AbstractEventHandlerModel<?>>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
AbstractEventHandlerVO<?> existing = EventHandlerDao.instance.getByXid(xid);
if (existing == null) {
result.addRestMessage(this.getDoesNotExistMessage());
return result.createResponseEntity();
} else {
// Check Event Type Permission
if (!Permissions.hasAdmin(user)) {
result.addRestMessage(HttpStatus.UNAUTHORIZED, new TranslatableMessage("permissions.accessDenied", user.getUsername(), SuperadminPermissionDefinition.GROUP_NAME));
return result.createResponseEntity();
}
// All Good Delete It
EventHandlerDao.instance.delete(existing.getId());
return result.createResponseEntity(existing.asModel());
}
} else {
return result.createResponseEntity();
}
}
use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult 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();
}
use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult 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));
}
}
}
use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult 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();
}
use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult 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();
}
Aggregations