use of com.serotonin.m2m2.web.mvc.rest.v1.model.events.handlers.AbstractEventHandlerModel in project ma-core-public by infiniteautomation.
the class AbstractEventHandlerModelDeserializer method deserialize.
/* (non-Javadoc)
* @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)
*/
@Override
public AbstractEventHandlerModel<?> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
JsonNode tree = jp.readValueAsTree();
String typeName = tree.get("handlerType").asText();
EventHandlerDefinition<?> def = ModuleRegistry.getEventHandlerDefinition(typeName);
if (def == null)
throw new ModelNotFoundException(typeName);
AbstractEventHandlerModel<?> model = (AbstractEventHandlerModel<?>) mapper.treeToValue(tree, def.getModelClass());
model.setDefinition(def);
return model;
}
use of com.serotonin.m2m2.web.mvc.rest.v1.model.events.handlers.AbstractEventHandlerModel in project ma-modules-public by infiniteautomation.
the class EventHandlerRestController method get.
@ApiOperation(value = "Get EventHandler by XID", notes = "EventType permission required")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json", "application/sero-json" }, value = "/{xid}")
public ResponseEntity<AbstractEventHandlerModel<?>> get(@ApiParam(value = "Valid Eventh Handler XID", required = true, allowMultiple = false) @PathVariable String xid, HttpServletRequest request) {
RestProcessResult<AbstractEventHandlerModel<?>> result = new RestProcessResult<AbstractEventHandlerModel<?>>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
AbstractEventHandlerVO<?> vo = EventHandlerDao.instance.getByXid(xid);
if (vo == null) {
result.addRestMessage(getDoesNotExistMessage());
return result.createResponseEntity();
} else {
// Check Permissions
if (Permissions.hasAdmin(user))
return result.createResponseEntity(vo.asModel());
else
result.addRestMessage(HttpStatus.UNAUTHORIZED, new TranslatableMessage("permissions.accessDenied", user.getUsername(), SuperadminPermissionDefinition.GROUP_NAME));
}
}
return result.createResponseEntity();
}
use of com.serotonin.m2m2.web.mvc.rest.v1.model.events.handlers.AbstractEventHandlerModel 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.model.events.handlers.AbstractEventHandlerModel 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.model.events.handlers.AbstractEventHandlerModel in project ma-modules-public by infiniteautomation.
the class EventHandlerRestController method save.
@ApiOperation(value = "Save a new event handler", notes = "User must have event type permission")
@RequestMapping(method = RequestMethod.POST, consumes = { "application/json" }, produces = { "application/json" })
public ResponseEntity<AbstractEventHandlerModel<?>> save(@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()) {
// Check Event Type Permission
if (!Permissions.hasAdmin(user)) {
result.addRestMessage(HttpStatus.UNAUTHORIZED, new TranslatableMessage("permissions.accessDenied", user.getUsername(), SuperadminPermissionDefinition.GROUP_NAME));
return result.createResponseEntity();
}
// Set XID if required
if (StringUtils.isEmpty(model.getXid())) {
model.setXid(EventHandlerDao.instance.generateUniqueXid());
}
if (!model.validate()) {
result.addRestMessage(this.getValidationFailedError());
return result.createResponseEntity(model);
} else {
AbstractEventHandlerVO<?> vo = model.getData();
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(model.getXid()).toUri();
result.addRestMessage(getResourceCreatedMessage(location));
return result.createResponseEntity(model);
}
// Not logged in
return result.createResponseEntity();
}
Aggregations