Search in sources :

Example 1 with AbstractEventHandlerModel

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;
}
Also used : AbstractEventHandlerModel(com.serotonin.m2m2.web.mvc.rest.v1.model.events.handlers.AbstractEventHandlerModel) ModelNotFoundException(com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with AbstractEventHandlerModel

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();
}
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) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with AbstractEventHandlerModel

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();
    }
}
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) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with AbstractEventHandlerModel

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();
}
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 5 with AbstractEventHandlerModel

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();
}
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)

Aggregations

AbstractEventHandlerModel (com.serotonin.m2m2.web.mvc.rest.v1.model.events.handlers.AbstractEventHandlerModel)6 User (com.serotonin.m2m2.vo.User)5 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)5 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 URI (java.net.URI)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 InvalidRQLRestException (com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException)1 EventHandlerDao (com.serotonin.m2m2.db.dao.EventHandlerDao)1 AbstractEventHandlerVO (com.serotonin.m2m2.vo.event.AbstractEventHandlerVO)1 ModelNotFoundException (com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException)1 FilteredPageQueryStream (com.serotonin.m2m2.web.mvc.rest.v1.model.FilteredPageQueryStream)1 QueryDataPageStream (com.serotonin.m2m2.web.mvc.rest.v1.model.QueryDataPageStream)1 EventHandlerStreamCallback (com.serotonin.m2m2.web.mvc.rest.v1.model.eventHandler.EventHandlerStreamCallback)1 ASTNode (net.jazdw.rql.parser.ASTNode)1