use of com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException in project ma-modules-public by infiniteautomation.
the class LoggingRestController method download.
@PreAuthorize("isAdmin()")
@ApiOperation(value = "View log", notes = "Optionally download file as attachment")
@RequestMapping(method = RequestMethod.GET, produces = { "text/plain" }, value = "/view/{filename}")
public ResponseEntity<FileSystemResource> download(@ApiParam(value = "Set content disposition to attachment", required = false, defaultValue = "true", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean download, @AuthenticationPrincipal User user, @PathVariable String filename, HttpServletRequest request) {
File file = new File(Common.getLogsDir(), filename);
if (file.exists()) {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set(HttpHeaders.CONTENT_DISPOSITION, download ? "attachment" : "inline");
return new ResponseEntity<>(new FileSystemResource(file), responseHeaders, HttpStatus.OK);
} else {
throw new NotFoundRestException();
}
}
use of com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException in project ma-modules-public by infiniteautomation.
the class PointValueRestController method getLatestPointValuesForDataSourceAsSingleArray.
/**
* 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 as single time ordered array.")
@RequestMapping(method = RequestMethod.GET, value = "/{dataSourceXid}/latest-data-source-single-array", produces = { "application/json", "text/csv" })
public ResponseEntity<QueryArrayStream<PointValueTimeModel>> getLatestPointValuesForDataSourceAsSingleArray(HttpServletRequest request, @ApiParam(value = "Data source xid", required = true, allowMultiple = false) @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<QueryArrayStream<PointValueTimeModel>> result = new RestProcessResult<QueryArrayStream<PointValueTimeModel>>(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.invalidValue", "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 {
IdPointValueTimeLatestPointValueFacadeStream pvtDatabaseStream = new IdPointValueTimeLatestPointValueFacadeStream(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.infiniteautomation.mango.rest.v2.exception.NotFoundRestException in project ma-modules-public by infiniteautomation.
the class RuntimeManagerRestController method relinquish.
@ApiOperation(value = "Relinquish the value of a data point", notes = "Only BACnet data points allow this", response = Void.class)
@RequestMapping(method = RequestMethod.POST, value = "/relinquish/{xid}")
public void relinquish(@ApiParam(value = "Valid Data Point XID", required = true, allowMultiple = false) @PathVariable String xid, @AuthenticationPrincipal User user, HttpServletRequest request) {
DataPointVO dataPoint = DataPointDao.instance.getByXid(xid);
if (dataPoint == null)
throw new NotFoundRestException();
Permissions.ensureDataPointReadPermission(user, dataPoint);
DataPointRT rt = Common.runtimeManager.getDataPoint(dataPoint.getId());
if (rt == null)
throw new GenericRestException(HttpStatus.BAD_REQUEST, new TranslatableMessage("rest.error.pointNotEnabled", xid));
// Get the Data Source and Relinquish the point
DataSourceRT<?> dsRt = Common.runtimeManager.getRunningDataSource(rt.getDataSourceId());
if (dsRt == null)
throw new GenericRestException(HttpStatus.BAD_REQUEST, new TranslatableMessage("rest.error.dataSourceNotEnabled", xid));
dsRt.relinquish(rt);
}
use of com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException in project ma-modules-public by infiniteautomation.
the class JsonDataRestController method getNode.
JsonNode getNode(final JsonNode existingData, final String[] dataPath) {
JsonNode node = existingData;
for (int i = 0; i < dataPath.length; i++) {
String fieldName = dataPath[i];
if (node.isObject()) {
ObjectNode objectNode = (ObjectNode) node;
node = objectNode.get(fieldName);
} else if (node.isArray()) {
ArrayNode arrayNode = (ArrayNode) node;
int index = toArrayIndex(fieldName);
node = arrayNode.get(index);
} else {
throw new BadRequestException(new TranslatableMessage("rest.error.cantGetFieldOfNodeType", node.getNodeType()));
}
if (node == null) {
throw new NotFoundRestException();
}
}
return node;
}
use of com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException in project ma-modules-public by infiniteautomation.
the class DataPointRestController method updateDataPoint.
@ApiOperation(value = "Update an existing data point")
@RequestMapping(method = RequestMethod.PUT, value = "/{xid}")
public ResponseEntity<DataPointModel> updateDataPoint(@PathVariable String xid, @ApiParam(value = "Updated data point model", required = true) @RequestBody(required = true) DataPointModel model, @AuthenticationPrincipal User user, UriComponentsBuilder builder) {
DataPointVO dataPoint = DataPointDao.instance.getByXid(xid);
if (dataPoint == null) {
throw new NotFoundRestException();
}
Permissions.ensureDataSourcePermission(user, dataPoint.getDataSourceId());
// check if they are trying to move it to another data source
String newDataSourceXid = model.getDataSourceXid();
if (newDataSourceXid != null && !newDataSourceXid.isEmpty() && !newDataSourceXid.equals(dataPoint.getDataSourceXid())) {
throw new BadRequestException(new TranslatableMessage("rest.error.pointChangeDataSource"));
}
DataPointPropertiesTemplateVO template = null;
if (model.isTemplateXidWasSet()) {
if (model.getTemplateXid() != null) {
template = (DataPointPropertiesTemplateVO) TemplateDao.instance.getByXid(model.getTemplateXid());
if (template == null) {
throw new BadRequestException(new TranslatableMessage("invalidTemplateXid"));
}
}
} else if (dataPoint.getTemplateId() != null) {
template = (DataPointPropertiesTemplateVO) TemplateDao.instance.get(dataPoint.getTemplateId());
}
DataPointDao.instance.loadPartialRelationalData(dataPoint);
model.copyPropertiesTo(dataPoint);
// load the template after copying the properties, template properties override the ones in the data point
if (template != null) {
dataPoint.withTemplate(template);
}
dataPoint.ensureValid();
// have to load any existing event detectors for the data point as we are about to replace the VO in the runtime manager
DataPointDao.instance.setEventDetectors(dataPoint);
Common.runtimeManager.saveDataPoint(dataPoint);
URI location = builder.path("/v2/data-points/{xid}").buildAndExpand(xid).toUri();
HttpHeaders headers = new HttpHeaders();
headers.setLocation(location);
return new ResponseEntity<>(new DataPointModel(dataPoint), headers, HttpStatus.OK);
}
Aggregations