use of com.infiniteautomation.mango.rest.latest.exception.AbstractRestException in project ma-modules-public by infiniteautomation.
the class PointValueRestController method purgePointValues.
@ApiOperation(value = "Purge Point Values for one or many data points, or a single data source", notes = "User must have edit access to data source and its points, use created header to track progress/cancel")
@RequestMapping(method = RequestMethod.POST, value = "/purge")
public ResponseEntity<TemporaryResource<PurgePointValuesResponseModel, AbstractRestException>> purgePointValues(@RequestBody() PurgeDataPointValuesModel model, UriComponentsBuilder builder) {
model.ensureValid();
TemporaryResource<PurgePointValuesResponseModel, AbstractRestException> response = resourceManager.newTemporaryResource("DATA_POINT_PURGE", null, model.getExpiry(), model.getTimeout(), (resource) -> {
PurgePointValuesResponseModel result = new PurgePointValuesResponseModel();
Map<Integer, DataSourceVO> dataSourceMap = new HashMap<>();
Map<String, DataPointVO> dataPointsMap = new HashMap<>();
// Build the list of data point Xids
List<String> xids = model.getXids();
if (xids != null && !xids.isEmpty()) {
for (String xid : xids) {
DataPointVO vo = dataPointService.get(xid);
dataPointsMap.put(xid, vo);
if (vo != null) {
dataSourceMap.computeIfAbsent(vo.getDataSourceId(), (key) -> dataSourceService.get(vo.getDataSourceId()));
}
}
} else {
DataSourceVO ds = dataSourceService.get(model.getDataSourceXid());
xids = new ArrayList<>();
if (ds != null) {
dataSourceMap.put(ds.getId(), ds);
List<DataPointVO> points = dataPointService.getDataPoints(ds.getId());
for (DataPointVO point : points) {
xids.add(point.getXid());
dataPointsMap.put(point.getXid(), point);
}
}
}
int maximum = xids.size();
int position = 0;
// Initial status
resource.progressOrSuccess(result, position, maximum);
for (String xid : xids) {
try {
// Get the point and its data source XID
DataPointVO dp = dataPointsMap.get(xid);
if (dp == null)
throw new NotFoundException();
DataSourceVO ds = dataSourceMap.get(dp.getDataSourceId());
if (ds == null)
throw new NotFoundException();
// Do purge based on settings
if (model.isPurgeAll())
Common.runtimeManager.purgeDataPointValues(dp);
else if (model.isUseTimeRange())
Common.runtimeManager.purgeDataPointValuesBetween(dp, model.getTimeRange().getFrom().getTime(), model.getTimeRange().getTo().getTime());
else {
long before = DateUtils.minus(Common.timer.currentTimeMillis(), TimePeriodType.convertFrom(model.getDuration().getType()), model.getDuration().getPeriods());
Common.runtimeManager.purgeDataPointValues(dp, before);
}
result.getSuccessfullyPurged().add(xid);
} catch (NotFoundException e) {
result.getNotFound().add(xid);
} catch (PermissionException e) {
result.getNoEditPermission().add(xid);
}
position++;
resource.progressOrSuccess(result, position, maximum);
}
return null;
});
HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/point-values/purge/{id}").buildAndExpand(response.getId()).toUri());
return new ResponseEntity<>(response, headers, HttpStatus.CREATED);
}
use of com.infiniteautomation.mango.rest.latest.exception.AbstractRestException in project ma-modules-public by infiniteautomation.
the class PublishedPointsRestController method bulkPublishedPointOperation.
@ApiOperation(value = "Bulk get/create/update/delete published points", notes = "User be superadmin")
@RequestMapping(method = RequestMethod.POST, value = "/bulk")
public ResponseEntity<TemporaryResource<PublishedPointBulkResponse, AbstractRestException>> bulkPublishedPointOperation(@RequestBody PublishedPointBulkRequest requestBody, UriComponentsBuilder builder) {
VoAction defaultAction = requestBody.getAction();
AbstractPublishedPointModel<?> defaultBody = requestBody.getBody();
List<PublishedPointIndividualRequest> requests = requestBody.getRequests();
if (requests == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "requests"));
} else if (requests.isEmpty()) {
throw new BadRequestException(new TranslatableMessage("rest.error.cantBeEmpty", "requests"));
}
String resourceId = requestBody.getId();
Long expiration = requestBody.getExpiration();
Long timeout = requestBody.getTimeout();
TemporaryResource<PublishedPointBulkResponse, AbstractRestException> responseBody = resourceManager.newTemporaryResource(RESOURCE_TYPE_BULK_PUBLISHED_POINT, resourceId, expiration, timeout, (resource) -> {
PublishedPointBulkResponse bulkResponse = new PublishedPointBulkResponse();
int i = 0;
resource.progressOrSuccess(bulkResponse, i++, requests.size());
PermissionHolder resourceUser = Common.getUser();
for (PublishedPointIndividualRequest request : requests) {
UriComponentsBuilder reqBuilder = UriComponentsBuilder.newInstance();
PublishedPointIndividualResponse individualResponse = doIndividualRequest(request, defaultAction, defaultBody, resourceUser, reqBuilder);
bulkResponse.addResponse(individualResponse);
resource.progressOrSuccess(bulkResponse, i++, requests.size());
}
return null;
});
HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/published-points/bulk/{id}").buildAndExpand(responseBody.getId()).toUri());
return new ResponseEntity<>(responseBody, headers, HttpStatus.CREATED);
}
use of com.infiniteautomation.mango.rest.latest.exception.AbstractRestException in project ma-modules-public by infiniteautomation.
the class MangoTaskTemporaryResourceManager method scheduleTask.
private void scheduleTask(TemporaryResource<T, AbstractRestException> resource) {
TaskData tasks = (TaskData) resource.getData();
// high priority task will start with user that calls this method in the SecurityContext
tasks.mainTask = new HighPriorityTask("Temporary resource " + resource.getResourceType() + " " + resource.getId()) {
@Override
public void run(long runtime) {
try {
resource.runTask();
} catch (Exception e) {
AbstractRestException error = MangoTaskTemporaryResourceManager.this.mapException(e);
resource.safeError(error);
}
}
@Override
public void rejected(RejectedTaskReason reason) {
super.rejected(reason);
TranslatableMessage msg = null;
switch(reason.getCode()) {
case RejectedTaskReason.POOL_FULL:
msg = new TranslatableMessage("rest.error.rejectedTaskPoolFull");
break;
case RejectedTaskReason.TASK_QUEUE_FULL:
msg = new TranslatableMessage("rest.error.rejectedTaskQueueFull");
break;
case RejectedTaskReason.CURRENTLY_RUNNING:
msg = new TranslatableMessage("rest.error.rejectedTaskAlreadyRunning");
break;
}
ServerErrorException ex = msg == null ? new ServerErrorException() : new ServerErrorException(msg);
AbstractRestException error = MangoTaskTemporaryResourceManager.this.mapException(ex);
resource.safeError(error);
}
};
Common.backgroundProcessing.execute(tasks.mainTask);
this.scheduleTimeout(resource);
}
use of com.infiniteautomation.mango.rest.latest.exception.AbstractRestException in project ma-modules-public by infiniteautomation.
the class SystemActionTemporaryResourceManager method create.
/**
* Create the task
* @param permissionTypeName - To get from system settings
*/
public <T extends SystemActionResult> ResponseEntity<TemporaryResource<T, AbstractRestException>> create(SystemActionModel requestBody, PermissionHolder user, UriComponentsBuilder builder, String permissionTypeName, String resourceType, ResourceTask<SystemActionResult, AbstractRestException> task) {
requestBody.ensureValid();
Long expiration = requestBody.getExpiration();
Long timeout = requestBody.getTimeout();
if (permissionTypeName != null) {
PermissionDefinition def = ModuleRegistry.getPermissionDefinition(permissionTypeName);
if (def == null) {
throw new NotFoundException();
} else {
service.ensurePermission(user, def.getPermission());
}
} else {
service.ensureAdminRole(user);
}
@SuppressWarnings("unchecked") TemporaryResource<T, AbstractRestException> responseBody = (TemporaryResource<T, AbstractRestException>) resourceManager.newTemporaryResource(resourceType, null, expiration, timeout, task);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/system-actions/status/{id}").buildAndExpand(responseBody.getId()).toUri());
return new ResponseEntity<TemporaryResource<T, AbstractRestException>>(responseBody, headers, HttpStatus.CREATED);
}
use of com.infiniteautomation.mango.rest.latest.exception.AbstractRestException in project ma-modules-public by infiniteautomation.
the class UserRestController method bulkUserOperation.
@ApiOperation(value = "Bulk get/create/update/delete users", notes = "User must have read/edit permission for the user")
@RequestMapping(method = RequestMethod.POST, value = "/bulk")
public ResponseEntity<TemporaryResource<UserBulkResponse, AbstractRestException>> bulkUserOperation(@RequestBody UserBulkRequest requestBody, HttpServletRequest servletRequest, Authentication authentication, UriComponentsBuilder builder) {
VoAction defaultAction = requestBody.getAction();
UserModel defaultBody = requestBody.getBody();
List<UserIndividualRequest> requests = requestBody.getRequests();
if (requests == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "requests"));
} else if (requests.isEmpty()) {
throw new BadRequestException(new TranslatableMessage("rest.error.cantBeEmpty", "requests"));
}
String resourceId = requestBody.getId();
Long expiration = requestBody.getExpiration();
Long timeout = requestBody.getTimeout();
TemporaryResource<UserBulkResponse, AbstractRestException> responseBody = bulkResourceManager.newTemporaryResource(RESOURCE_TYPE_BULK_USER, resourceId, expiration, timeout, (resource) -> {
UserBulkResponse bulkResponse = new UserBulkResponse();
int i = 0;
resource.progressOrSuccess(bulkResponse, i++, requests.size());
for (UserIndividualRequest request : requests) {
UriComponentsBuilder reqBuilder = UriComponentsBuilder.newInstance();
PermissionHolder resourceUser = Common.getUser();
UserIndividualResponse individualResponse = doIndividualRequest(request, defaultAction, defaultBody, resourceUser, servletRequest, authentication, reqBuilder);
bulkResponse.addResponse(individualResponse);
resource.progressOrSuccess(bulkResponse, i++, requests.size());
}
return null;
});
HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/users/bulk/{id}").buildAndExpand(responseBody.getId()).toUri());
return new ResponseEntity<>(responseBody, headers, HttpStatus.CREATED);
}
Aggregations