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