Search in sources :

Example 6 with AbstractRestV2Exception

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) ResponseEntity(org.springframework.http.ResponseEntity) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with AbstractRestV2Exception

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);
}
Also used : AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) HighPriorityTask(com.serotonin.m2m2.util.timeout.HighPriorityTask) User(com.serotonin.m2m2.vo.User) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) RejectedTaskReason(com.serotonin.timer.RejectedTaskReason)

Example 8 with AbstractRestV2Exception

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);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) NestedRuntimeException(org.springframework.core.NestedRuntimeException) ResponseEntity(org.springframework.http.ResponseEntity) User(com.serotonin.m2m2.vo.User) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) IOException(java.io.IOException) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) ValidationException(com.serotonin.m2m2.vo.exception.ValidationException) ResourceNotFoundException(com.infiniteautomation.mango.rest.v2.exception.ResourceNotFoundException) NestedRuntimeException(org.springframework.core.NestedRuntimeException) NotFoundException(com.serotonin.m2m2.vo.exception.NotFoundException) IOException(java.io.IOException) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException)

Aggregations

AbstractRestV2Exception (com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception)8 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)5 ResponseEntity (org.springframework.http.ResponseEntity)5 AccessDeniedException (com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException)4 BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)4 User (com.serotonin.m2m2.vo.User)4 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)4 HttpHeaders (org.springframework.http.HttpHeaders)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)3 RQLToObjectListQuery (com.infiniteautomation.mango.db.query.pojo.RQLToObjectListQuery)2 BulkRequest (com.infiniteautomation.mango.rest.v2.bulk.BulkRequest)2 BulkResponse (com.infiniteautomation.mango.rest.v2.bulk.BulkResponse)2 VoAction (com.infiniteautomation.mango.rest.v2.bulk.VoAction)2 ServerErrorException (com.infiniteautomation.mango.rest.v2.exception.ServerErrorException)2 DataPointModel (com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel)2 MangoTaskTemporaryResourceManager (com.infiniteautomation.mango.rest.v2.temporaryResource.MangoTaskTemporaryResourceManager)2 TemporaryResource (com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResource)2 TemporaryResourceStatus (com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResource.TemporaryResourceStatus)2 TemporaryResourceManager (com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResourceManager)2