use of com.serotonin.m2m2.vo.permission.Permissions in project ma-modules-public by infiniteautomation.
the class EventDetectorRestV2Controller method getForDataPoint.
@ApiOperation(value = "Get all Event Detectors for a given data point", notes = "", response = AbstractEventDetectorModel.class, responseContainer = "List")
@RequestMapping(method = RequestMethod.GET, value = "/data-point/{xid}")
public ResponseEntity<List<AbstractEventDetectorModel<?>>> getForDataPoint(@AuthenticationPrincipal User user, @ApiParam(value = "Valid Data Point XID", required = true, allowMultiple = false) @PathVariable String xid, HttpServletRequest request) {
DataPointVO dp = DataPointDao.instance.getByXid(xid);
if (dp == null)
throw new NotFoundRestException();
// Check permissions
if (!user.isAdmin())
Permissions.ensureDataPointReadPermission(user, dp);
DataPointDao.instance.setEventDetectors(dp);
List<AbstractEventDetectorModel<?>> models = new ArrayList<AbstractEventDetectorModel<?>>();
for (AbstractPointEventDetectorVO<?> ped : dp.getEventDetectors()) models.add(ped.asModel());
return new ResponseEntity<>(models, HttpStatus.OK);
}
use of com.serotonin.m2m2.vo.permission.Permissions in project ma-modules-public by infiniteautomation.
the class EventDetectorRestV2Controller method queryRQL.
@ApiOperation(value = "Query Event Detectors", notes = "Use RQL formatted query, filtered by data point permissions", response = AbstractEventDetectorModel.class, responseContainer = "List")
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<QueryDataPageStream<AbstractEventDetectorVO<?>>> queryRQL(@AuthenticationPrincipal User user, HttpServletRequest request) {
ASTNode node = parseRQLtoAST(request.getQueryString());
if (user.isAdmin()) {
// admin users don't need to filter the results
return new ResponseEntity<>(getPageStream(node), HttpStatus.OK);
} else {
EventDetectorStreamCallback callback = new EventDetectorStreamCallback(this, user);
FilteredPageQueryStream<AbstractEventDetectorVO<?>, AbstractEventDetectorModel<?>, EventDetectorDao> stream = new FilteredPageQueryStream<AbstractEventDetectorVO<?>, AbstractEventDetectorModel<?>, EventDetectorDao>(EventDetectorDao.instance, this, node, callback);
stream.setupQuery();
return new ResponseEntity<>(stream, HttpStatus.OK);
}
}
use of com.serotonin.m2m2.vo.permission.Permissions in project ma-modules-public by infiniteautomation.
the class PointValueRestController method buildMap.
/**
* Build and validate the map of Requested Data Points
* @param user
* @param xids
* @return
*/
protected Map<Integer, DataPointVO> buildMap(User user, String[] xids, RollupEnum rollup) {
// Build the map, check permissions
Map<Integer, DataPointVO> voMap = new HashMap<Integer, DataPointVO>();
for (String xid : xids) {
DataPointVO vo = DataPointDao.instance.getByXid(xid);
if (vo == null) {
throw new NotFoundRestException();
} else {
if (!Permissions.hasDataPointReadPermission(user, vo))
throw new AccessDeniedException();
}
// TODO Add support for NONE Default Rollup
if (rollup == RollupEnum.POINT_DEFAULT && vo.getRollup() == RollupEnum.NONE.getId())
throw new BadRequestException(new TranslatableMessage("common.default", "Default point rollup of NONE is not yet supported for point with xid: " + xid));
;
// Validate the rollup
switch(vo.getPointLocator().getDataTypeId()) {
case DataTypes.ALPHANUMERIC:
case DataTypes.BINARY:
case DataTypes.IMAGE:
case DataTypes.MULTISTATE:
if (rollup.nonNumericSupport() == false)
throw new BadRequestException(new TranslatableMessage("rest.validate.rollup.incompatible", rollup.toString(), xid));
break;
case DataTypes.NUMERIC:
break;
}
voMap.put(vo.getId(), vo);
}
// Do we have any points
if (voMap.isEmpty())
throw new NotFoundRestException();
return voMap;
}
use of com.serotonin.m2m2.vo.permission.Permissions in project ma-modules-public by infiniteautomation.
the class PointValueRestController method getLatestPointValuesForDataSourceAsMultipleArrays.
/**
* Get the latest point values a set of points return as map of xid to array of values
*
* @param xid
* @param limit
* @return
*/
@ApiOperation(value = "Get Latest Point Values for all points on a data source directly from the Runtime Manager, this makes Cached and Intra-Interval data available.", notes = "Default limit 100, time descending order, Default to return cached data. Returns data as map of xid to values.")
@RequestMapping(method = RequestMethod.GET, value = "/{dataSourceXid}/latest-data-source-multiple-arrays", produces = { "application/json", "text/csv" })
public ResponseEntity<ObjectStream<Map<String, List<PointValueTime>>>> getLatestPointValuesForDataSourceAsMultipleArrays(HttpServletRequest request, @ApiParam(value = "Data source xid", required = true, allowMultiple = true) @PathVariable String dataSourceXid, @ApiParam(value = "Return rendered value as String", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean useRendered, @ApiParam(value = "Return converted value using displayed unit", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean unitConversion, @ApiParam(value = "Limit results", allowMultiple = false, defaultValue = "100") @RequestParam(value = "limit", defaultValue = "100") int limit, @ApiParam(value = "Return cached data?", allowMultiple = false, defaultValue = "true") @RequestParam(value = "useCache", defaultValue = "true") boolean useCache, @ApiParam(value = "Date Time format pattern for timestamps as strings, if not included epoch milli number is used", required = false, allowMultiple = false) @RequestParam(value = "dateTimeFormat", required = false) String dateTimeFormat, @ApiParam(value = "Time zone of output, used if formatted times are returned", required = false, allowMultiple = false) @RequestParam(value = "timezone", required = false) String timezone) {
RestProcessResult<ObjectStream<Map<String, List<PointValueTime>>>> result = new RestProcessResult<ObjectStream<Map<String, List<PointValueTime>>>>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
DataSourceVO<?> ds = DataSourceDao.instance.getByXid(dataSourceXid);
if (ds == null)
throw new NotFoundRestException();
if (dateTimeFormat != null) {
try {
DateTimeFormatter.ofPattern(dateTimeFormat);
} catch (IllegalArgumentException e) {
RestValidationResult vr = new RestValidationResult();
vr.addError("validate.invalid", "dateTimeFormat");
throw new ValidationFailedRestException(vr);
}
}
if (timezone != null) {
try {
ZoneId.of(timezone);
} catch (Exception e) {
RestValidationResult vr = new RestValidationResult();
vr.addError("validate.invalidValue", "timezone");
throw new ValidationFailedRestException(vr);
}
}
List<DataPointVO> points = DataPointDao.instance.getDataPointsForDataSourceStart(ds.getId());
Map<Integer, DataPointVO> pointIdMap = new HashMap<Integer, DataPointVO>(points.size());
for (DataPointVO vo : points) {
if (Permissions.hasDataPointReadPermission(user, vo))
pointIdMap.put(vo.getId(), vo);
else {
// Abort, invalid permissions
result.addRestMessage(getUnauthorizedMessage());
return result.createResponseEntity();
}
}
// Do we have any valid points?
if (pointIdMap.size() == 0) {
result.addRestMessage(getDoesNotExistMessage());
return result.createResponseEntity();
}
try {
XidPointValueTimeLatestPointFacadeStream pvtDatabaseStream = new XidPointValueTimeLatestPointFacadeStream(pointIdMap, useRendered, unitConversion, limit, useCache, dateTimeFormat, timezone);
return result.createResponseEntity(pvtDatabaseStream);
} catch (PermissionException e) {
LOG.error(e.getMessage(), e);
result.addRestMessage(getUnauthorizedMessage());
return result.createResponseEntity();
}
} else {
return result.createResponseEntity();
}
}
use of com.serotonin.m2m2.vo.permission.Permissions in project ma-modules-public by infiniteautomation.
the class PointValueRestController method pointValuesForMultiplePointsAsSingleArray.
private ResponseEntity<QueryArrayStream<PointValueTimeModel>> pointValuesForMultiplePointsAsSingleArray(HttpServletRequest request, String[] xids, boolean useRendered, boolean unitConversion, DateTime from, DateTime to, RollupEnum rollup, TimePeriodType timePeriodType, Integer timePeriods, String timezone, Integer limit, String dateTimeFormat) {
RestProcessResult<QueryArrayStream<PointValueTimeModel>> result = new RestProcessResult<QueryArrayStream<PointValueTimeModel>>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
if (dateTimeFormat != null) {
try {
DateTimeFormatter.ofPattern(dateTimeFormat);
} catch (IllegalArgumentException e) {
RestValidationResult vr = new RestValidationResult();
vr.addError("validate.invalid", "dateTimeFormat");
throw new ValidationFailedRestException(vr);
}
}
Map<Integer, DataPointVO> pointIdMap = new HashMap<Integer, DataPointVO>(xids.length);
DataPointVO vo;
for (String xid : xids) {
vo = DataPointDao.instance.getByXid(xid);
if (vo != null) {
if (Permissions.hasDataPointReadPermission(user, vo))
pointIdMap.put(vo.getId(), vo);
else {
// Abort, invalid permissions
result.addRestMessage(getUnauthorizedMessage());
return result.createResponseEntity();
}
}
}
if (timezone != null) {
try {
ZoneId.of(timezone);
} catch (Exception e) {
RestValidationResult vr = new RestValidationResult();
vr.addError("validate.invalidValue", "timezone");
throw new ValidationFailedRestException(vr);
}
}
// Do we have any valid points?
if (pointIdMap.size() == 0) {
result.addRestMessage(getDoesNotExistMessage());
return result.createResponseEntity();
}
try {
long current = Common.timer.currentTimeMillis();
if (from == null)
from = new DateTime(current);
if (to == null)
to = new DateTime(current);
// better not to for RESTfulness
if (timezone != null) {
DateTimeZone zone = DateTimeZone.forID(timezone);
from = from.withZone(zone);
to = to.withZone(zone);
}
// Are we using rollup
if ((rollup != null) && (rollup != RollupEnum.NONE)) {
if (rollup == RollupEnum.FFT) {
// Special Rollup for FFT's with no time rollup action
// TODO Need a way to return frequency or period values
// IdPointValueFftCalculator calc = new
// IdPointValueFftCalculator(pointIdMap, from.getTime(), to.getTime(),
// true);
// return result.createResponseEntity(calc);
} else {
TimePeriod timePeriod = null;
if ((timePeriodType != null) && (timePeriods != null)) {
timePeriod = new TimePeriod(timePeriods, timePeriodType);
}
IdPointValueRollupCalculator calc = new IdPointValueRollupCalculator(pointIdMap, useRendered, unitConversion, rollup, timePeriod, from, to, limit, dateTimeFormat, timezone);
return result.createResponseEntity(calc);
}
return result.createResponseEntity();
} else {
IdPointValueTimeDatabaseStream pvtDatabaseStream = new IdPointValueTimeDatabaseStream(pointIdMap, useRendered, unitConversion, from.getMillis(), to.getMillis(), this.dao, limit, dateTimeFormat, timezone);
return result.createResponseEntity(pvtDatabaseStream);
}
} catch (PermissionException e) {
LOG.error(e.getMessage(), e);
result.addRestMessage(getUnauthorizedMessage());
return result.createResponseEntity();
}
} else {
return result.createResponseEntity();
}
}
Aggregations