use of com.infiniteautomation.mango.rest.v2.exception.ServerErrorException in project ma-modules-public by infiniteautomation.
the class EventDetectorRestV2Controller method update.
@ApiOperation(value = "Update an Event Detector", notes = "")
@RequestMapping(method = RequestMethod.PUT, value = { "/{xid}" })
public ResponseEntity<AbstractEventDetectorModel<?>> update(@ApiParam(value = "Valid Event Detector XID", required = true, allowMultiple = false) @PathVariable String xid, @ApiParam(value = "Event Detector", required = true) @RequestBody(required = true) AbstractEventDetectorModel<?> model, @AuthenticationPrincipal User user, UriComponentsBuilder builder, HttpServletRequest request) {
AbstractEventDetectorVO<?> vo = model.getData();
// Check to see if it already exists
AbstractEventDetectorVO<?> existing = this.dao.getByXid(xid);
if (existing == null) {
throw new NotFoundRestException();
} else {
// Set the ID
vo.setId(existing.getId());
}
// Check permission
DataPointVO dp = DataPointDao.instance.get(vo.getSourceId());
if (dp == null)
throw new NotFoundRestException();
Permissions.ensureDataSourcePermission(user, dp.getDataSourceId());
// TODO Fix this when we have other types of detectors
AbstractPointEventDetectorVO<?> ped = (AbstractPointEventDetectorVO<?>) vo;
ped.njbSetDataPoint(dp);
// Validate
ped.ensureValid();
// Replace it on the data point, if it isn't replaced we fail.
boolean replaced = false;
DataPointDao.instance.setEventDetectors(dp);
ListIterator<AbstractPointEventDetectorVO<?>> it = dp.getEventDetectors().listIterator();
while (it.hasNext()) {
AbstractPointEventDetectorVO<?> ed = it.next();
if (ed.getId() == ped.getId()) {
it.set(ped);
replaced = true;
break;
}
}
if (!replaced)
throw new ServerErrorException(new TranslatableMessage("rest.error.eventDetectorNotAssignedToThisPoint"));
// Save the data point
Common.runtimeManager.saveDataPoint(dp);
URI location = builder.path("/v2/event-detectors/{xid}").buildAndExpand(vo.getXid()).toUri();
return getResourceUpdated(vo.asModel(), location);
}
use of com.infiniteautomation.mango.rest.v2.exception.ServerErrorException in project ma-modules-public by infiniteautomation.
the class SessionExceptionRestV2Controller method clearLatest.
@ApiOperation(value = "Clear Last Exception for your session", notes = "")
@ApiResponses({ @ApiResponse(code = 401, message = "Unauthorized user access", response = ResponseEntity.class), @ApiResponse(code = 404, message = "No Exception exists", response = ResponseEntity.class), @ApiResponse(code = 500, message = "Error processing request", response = ResponseEntity.class) })
@RequestMapping(method = { RequestMethod.PUT }, value = { "/latest" }, produces = { "application/json" })
public ResponseEntity<Map<String, Exception>> clearLatest(HttpServletRequest request) {
RestProcessResult<Map<String, Exception>> result = new RestProcessResult<>(HttpStatus.OK);
// Get latest Session Exception
HttpSession session = request.getSession(false);
if (session == null)
throw new ServerErrorException(new TranslatableMessage("rest.error.noSession"));
Map<String, Exception> exceptionMap = new HashMap<String, Exception>();
for (String key : exceptionKeys) {
exceptionMap.put(key, (Exception) session.getAttribute(key));
session.removeAttribute(key);
}
return result.createResponseEntity(exceptionMap);
}
use of com.infiniteautomation.mango.rest.v2.exception.ServerErrorException in project ma-modules-public by infiniteautomation.
the class SessionExceptionRestV2Controller method getLatest.
@ApiOperation(value = "Get Last Exception for your session", notes = "")
@ApiResponses({ @ApiResponse(code = 401, message = "Unauthorized user access", response = ResponseEntity.class), @ApiResponse(code = 404, message = "No Exception exists", response = ResponseEntity.class), @ApiResponse(code = 500, message = "Error processing request", response = ResponseEntity.class) })
@RequestMapping(method = { RequestMethod.GET }, value = { "/latest" }, produces = { "application/json" })
public ResponseEntity<Map<String, Exception>> getLatest(HttpServletRequest request) {
RestProcessResult<Map<String, Exception>> result = new RestProcessResult<>(HttpStatus.OK);
// Get latest Session Exception
HttpSession session = request.getSession(false);
if (session == null)
throw new ServerErrorException(new TranslatableMessage("rest.error.noSession"));
Map<String, Exception> exceptionMap = new HashMap<String, Exception>();
for (String key : exceptionKeys) {
exceptionMap.put(key, (Exception) session.getAttribute(key));
}
return result.createResponseEntity(exceptionMap);
}
use of com.infiniteautomation.mango.rest.v2.exception.ServerErrorException in project ma-modules-public by infiniteautomation.
the class EventDetectorRestV2Controller method delete.
@ApiOperation(value = "Delete an Event Detector", notes = "")
@RequestMapping(method = RequestMethod.DELETE, value = { "/{xid}" })
public ResponseEntity<AbstractEventDetectorModel<?>> delete(@ApiParam(value = "Valid Event Detector XID", required = true, allowMultiple = false) @PathVariable String xid, @AuthenticationPrincipal User user, UriComponentsBuilder builder, HttpServletRequest request) {
// Check to see if it already exists
AbstractEventDetectorVO<?> existing = this.dao.getByXid(xid);
if (existing == null) {
throw new NotFoundRestException();
}
// Check permission
DataPointVO dp = DataPointDao.instance.get(existing.getSourceId());
Permissions.ensureDataSourcePermission(user, dp.getDataSourceId());
// TODO Fix this when we have other types of detectors
AbstractPointEventDetectorVO<?> ped = (AbstractPointEventDetectorVO<?>) existing;
ped.njbSetDataPoint(dp);
// Remove it from the data point, if it isn't replaced we fail.
boolean removed = false;
DataPointDao.instance.setEventDetectors(dp);
ListIterator<AbstractPointEventDetectorVO<?>> it = dp.getEventDetectors().listIterator();
while (it.hasNext()) {
AbstractPointEventDetectorVO<?> ed = it.next();
if (ed.getId() == ped.getId()) {
it.remove();
removed = true;
break;
}
}
if (!removed)
throw new ServerErrorException(new TranslatableMessage("rest.error.eventDetectorNotAssignedToThisPoint"));
// Save the data point
Common.runtimeManager.saveDataPoint(dp);
return new ResponseEntity<>(existing.asModel(), HttpStatus.OK);
}
use of com.infiniteautomation.mango.rest.v2.exception.ServerErrorException in project ma-modules-public by infiniteautomation.
the class MangoTaskTemporaryResourceManager method scheduleTask.
private void scheduleTask(TemporaryResource<T, AbstractRestV2Exception> resource) {
TaskData tasks = (TaskData) resource.getData();
// TODO Mango 3.4 keep user inside the resource isntead of user id?
// maybe change the user inside DataPointRestController bulk operation lambda function to get user from background context
User user = UserDao.instance.get(resource.getUserId());
if (user == null) {
AccessDeniedException error = new AccessDeniedException();
resource.safeError(error);
return;
}
tasks.mainTask = new HighPriorityTask("Temporary resource " + resource.getResourceType() + " " + resource.getId()) {
@Override
public void run(long runtime) {
try {
BackgroundContext.set(user);
resource.getTask().run(resource);
} catch (Exception e) {
AbstractRestV2Exception error = MangoTaskTemporaryResourceManager.this.mapException(e);
resource.safeError(error);
} finally {
BackgroundContext.remove();
}
}
@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);
AbstractRestV2Exception error = MangoTaskTemporaryResourceManager.this.mapException(ex);
resource.safeError(error);
}
};
Common.backgroundProcessing.execute(tasks.mainTask);
this.scheduleTimeout(resource);
}
Aggregations