use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult in project ma-modules-public by infiniteautomation.
the class DataSourceRestController method queryRQL.
@ApiOperation(value = "Query Data Sources", notes = "Use RQL formatted query", response = AbstractDataSourceModel.class, responseContainer = "List")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" })
public ResponseEntity<QueryDataPageStream<DataSourceVO<?>>> queryRQL(HttpServletRequest request) {
RestProcessResult<QueryDataPageStream<DataSourceVO<?>>> result = new RestProcessResult<QueryDataPageStream<DataSourceVO<?>>>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
try {
ASTNode node = parseRQLtoAST(request.getQueryString());
DataSourceStreamCallback callback = new DataSourceStreamCallback(this, user);
return result.createResponseEntity(getPageStream(node, callback));
} catch (InvalidRQLRestException e) {
LOG.error(e.getMessage(), e);
result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
return result.createResponseEntity();
}
}
return result.createResponseEntity();
}
use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult 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.message.RestProcessResult 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.message.RestProcessResult 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.message.RestProcessResult 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