use of com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception in project ma-modules-public by infiniteautomation.
the class DataPointTagsRestController method bulkDataPointTagOperation.
@ApiOperation(value = "Bulk get/set/add data point tags for a list of XIDs", notes = "User must have read/edit permission for the data point")
@RequestMapping(method = RequestMethod.POST, value = "/bulk")
public ResponseEntity<TemporaryResource<TagBulkResponse, AbstractRestV2Exception>> bulkDataPointTagOperation(@RequestBody TagBulkRequest requestBody, @AuthenticationPrincipal User user, UriComponentsBuilder builder) {
BulkTagAction defaultAction = requestBody.getAction();
Map<String, String> defaultBody = requestBody.getBody();
List<TagIndividualRequest> requests = requestBody.getRequests();
if (requests == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "requests"));
}
String resourceId = requestBody.getId();
Long expiration = requestBody.getExpiration();
Long timeout = requestBody.getTimeout();
TemporaryResource<TagBulkResponse, AbstractRestV2Exception> responseBody = bulkResourceManager.newTemporaryResource(RESOURCE_TYPE_BULK_DATA_POINT_TAGS, resourceId, user.getId(), expiration, timeout, (resource) -> {
TagBulkResponse bulkResponse = new TagBulkResponse();
int i = 0;
resource.progress(bulkResponse, i++, requests.size());
for (TagIndividualRequest request : requests) {
TagIndividualResponse individualResponse = doIndividualRequest(request, defaultAction, defaultBody, user);
bulkResponse.addResponse(individualResponse);
resource.progressOrSuccess(bulkResponse, i++, requests.size());
}
});
HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/v2/data-point-tags/bulk/{id}").buildAndExpand(responseBody.getId()).toUri());
return new ResponseEntity<TemporaryResource<TagBulkResponse, AbstractRestV2Exception>>(responseBody, headers, HttpStatus.CREATED);
}
use of com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception 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);
}
use of com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception in project ma-core-public by infiniteautomation.
the class MangoSpringExceptionHandler method handleExceptionInternal.
/* (non-Javadoc)
* @see org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler#handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatus, org.springframework.web.context.request.WebRequest)
*/
@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
HttpServletRequest servletRequest = ((ServletWebRequest) request).getRequest();
HttpServletResponse servletResponse = ((ServletWebRequest) request).getResponse();
this.storeException(servletRequest, ex, status);
// Log all but not found exceptions
if (body instanceof ServerErrorException || body instanceof GenericRestException || !(body instanceof AbstractRestV2Exception))
ExceptionUtils.logWebException(ex, servletRequest, LOG);
if (this.browserHtmlRequestMatcher.matches(servletRequest)) {
String uri;
if (status == HttpStatus.FORBIDDEN) {
// browser HTML request
uri = ACCESS_DENIED;
User user = Common.getHttpUser();
if (user != null) {
uri = DefaultPagesDefinition.getUnauthorizedUri(servletRequest, servletResponse, user);
}
// Put exception into request scope (perhaps of use to a view)
servletRequest.setAttribute(WebAttributes.ACCESS_DENIED_403, ex);
// Set the 403 status code.
servletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
} else {
uri = DefaultPagesDefinition.getErrorUri(servletRequest, servletResponse);
}
try {
servletResponse.sendRedirect(uri);
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
return null;
} else {
// To strip off the double messages generated by this...
if (ex instanceof NestedRuntimeException)
ex = (Exception) ((NestedRuntimeException) ex).getMostSpecificCause();
// If no body provided we will create one
if (body == null)
body = new GenericRestException(status, ex);
return new ResponseEntity<Object>(body, headers, status);
}
}
Aggregations