Search in sources :

Example 6 with AbstractDataSourceModel

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

the class DataSourceRestController method getDataSourceById.

@ApiOperation(value = "Get data source by ID", notes = "Only returns data sources available to logged in user")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/by-id/{id}")
public ResponseEntity<AbstractDataSourceModel<?>> getDataSourceById(@ApiParam(value = "Valid Data Source ID", required = true, allowMultiple = false) @PathVariable int id, HttpServletRequest request) {
    RestProcessResult<AbstractDataSourceModel<?>> result = new RestProcessResult<AbstractDataSourceModel<?>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        DataSourceVO<?> vo = DataSourceDao.instance.get(id);
        if (vo == null) {
            return new ResponseEntity<AbstractDataSourceModel<?>>(HttpStatus.NOT_FOUND);
        } else {
            try {
                if (Permissions.hasDataSourcePermission(user, vo))
                    return result.createResponseEntity(vo.asModel());
                else {
                    result.addRestMessage(getUnauthorizedMessage());
                    return result.createResponseEntity();
                }
            } catch (PermissionException e) {
                LOG.warn(e.getMessage(), e);
                result.addRestMessage(getUnauthorizedMessage());
                return result.createResponseEntity();
            }
        }
    }
    return result.createResponseEntity();
}
Also used : AbstractDataSourceModel(com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ResponseEntity(org.springframework.http.ResponseEntity) User(com.serotonin.m2m2.vo.User) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with AbstractDataSourceModel

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

the class DataSourceRestController method saveDataSource.

@ApiOperation(value = "Save data source")
@RequestMapping(method = { RequestMethod.POST }, produces = { "application/json" })
public ResponseEntity<AbstractDataSourceModel<?>> saveDataSource(@RequestBody(required = true) AbstractDataSourceModel<?> model, UriComponentsBuilder builder, HttpServletRequest request) {
    RestProcessResult<AbstractDataSourceModel<?>> result = new RestProcessResult<AbstractDataSourceModel<?>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        try {
            if (!Permissions.hasDataSourcePermission(user)) {
                result.addRestMessage(this.getUnauthorizedMessage());
                return result.createResponseEntity();
            }
        } catch (PermissionException pe) {
            LOG.warn(pe.getMessage(), pe);
            result.addRestMessage(this.getUnauthorizedMessage());
            return result.createResponseEntity();
        }
        DataSourceVO<?> vo = model.getData();
        // Check to see if the data source already exists
        if (!StringUtils.isEmpty(vo.getXid())) {
            DataSourceVO<?> existing = (DataSourceVO<?>) DataSourceDao.instance.getByXid(model.getXid());
            if (existing != null) {
                result.addRestMessage(HttpStatus.CONFLICT, new TranslatableMessage("rest.exception.alreadyExists", model.getXid()));
                return result.createResponseEntity();
            }
        }
        if (StringUtils.isEmpty(vo.getXid()))
            vo.setXid(DataSourceDao.instance.generateUniqueXid());
        if (!model.validate() || !Permissions.hasPermission(vo.getEditPermission(), user.getPermissions())) {
            result.addRestMessage(this.getValidationFailedError());
            return result.createResponseEntity(model);
        } else {
            Common.runtimeManager.saveDataSource(vo);
            DataSourceVO<?> created = (DataSourceVO<?>) DataSourceDao.instance.getByXid(model.getXid());
            URI location = builder.path("/v1/data-sources/{xid}").buildAndExpand(new Object[] { created.asModel().getXid() }).toUri();
            result.addRestMessage(this.getResourceCreatedMessage(location));
            return result.createResponseEntity(created.asModel());
        }
    } else {
        return result.createResponseEntity();
    }
}
Also used : AbstractDataSourceModel(com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with AbstractDataSourceModel

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

the class DataSourceRestController method getDataSource.

@ApiOperation(value = "Get data source by xid", notes = "Only returns data sources available to logged in user")
@RequestMapping(method = RequestMethod.GET, value = "/{xid}", produces = { "application/json" })
public ResponseEntity<AbstractDataSourceModel<?>> getDataSource(HttpServletRequest request, @PathVariable String xid) {
    RestProcessResult<AbstractDataSourceModel<?>> result = new RestProcessResult<AbstractDataSourceModel<?>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        DataSourceVO<?> vo = DataSourceDao.instance.getByXid(xid);
        if (vo == null) {
            return new ResponseEntity<AbstractDataSourceModel<?>>(HttpStatus.NOT_FOUND);
        } else {
            try {
                if (Permissions.hasDataSourcePermission(user, vo))
                    return result.createResponseEntity(vo.asModel());
                else {
                    result.addRestMessage(getUnauthorizedMessage());
                    return result.createResponseEntity();
                }
            } catch (PermissionException e) {
                LOG.warn(e.getMessage(), e);
                result.addRestMessage(getUnauthorizedMessage());
                return result.createResponseEntity();
            }
        }
    }
    return result.createResponseEntity();
}
Also used : AbstractDataSourceModel(com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ResponseEntity(org.springframework.http.ResponseEntity) User(com.serotonin.m2m2.vo.User) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with AbstractDataSourceModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel in project ma-core-public by infiniteautomation.

the class DataSourceModelDeserializer method deserialize.

/* (non-Javadoc)
	 * @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)
	 */
@Override
public AbstractDataSourceModel<?> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    JsonNode tree = jp.readValueAsTree();
    String typeName = tree.get("modelType").asText();
    DataSourceDefinition definition = ModuleRegistry.getDataSourceDefinition(typeName);
    if (definition == null)
        throw new ModelNotFoundException(typeName);
    AbstractDataSourceModel<?> model = (AbstractDataSourceModel<?>) mapper.treeToValue(tree, definition.getModelClass());
    // Set the definition here
    model.setDefinition(definition);
    return model;
}
Also used : AbstractDataSourceModel(com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel) ModelNotFoundException(com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException) DataSourceDefinition(com.serotonin.m2m2.module.DataSourceDefinition) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

AbstractDataSourceModel (com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel)9 User (com.serotonin.m2m2.vo.User)8 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)7 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)7 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)4 URI (java.net.URI)3 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)2 ResponseEntity (org.springframework.http.ResponseEntity)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 DataSourceDefinition (com.serotonin.m2m2.module.DataSourceDefinition)1 MockDataSourceVO (com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO)1 ModelNotFoundException (com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 MvcResult (org.springframework.test.web.servlet.MvcResult)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1