Search in sources :

Example 1 with TemporaryResource

use of com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResource in project ma-modules-public by infiniteautomation.

the class TemporaryResourceWebSocketHandler method notifySession.

private void notifySession(WebSocketSession session, CrudNotificationType type, TemporaryResource<?, ?> resource) throws JsonProcessingException, IOException {
    User user = this.getUser(session);
    if (user == null)
        return;
    TemporaryResourceSubscription subscription = (TemporaryResourceSubscription) session.getAttributes().get(SUBSCRIPTION_ATTRIBUTE);
    if (resource.getUserId() == user.getId() || (user.isAdmin() && !subscription.isOwnResourcesOnly())) {
        Set<TemporaryResourceStatus> statuses = subscription.getStatuses();
        Set<String> resourceTypes = subscription.getResourceTypes();
        if ((subscription.isAnyStatus() || statuses.contains(resource.getStatus())) && (subscription.isAnyResourceType() || resourceTypes.contains(resource.getResourceType()))) {
            WebSocketNotification<TemporaryResource<?, ?>> notificationMessage = new WebSocketNotification<>(type, resource);
            boolean showResult = !resource.isComplete() && subscription.isShowResultWhenIncomplete() || resource.isComplete() && subscription.isShowResultWhenComplete();
            if (type == CrudNotificationType.DELETE) {
                showResult = false;
            }
            Class<?> view = showResult ? TemporaryResourceViews.ShowResult.class : Object.class;
            if (log.isTraceEnabled()) {
                log.trace("Notifying session " + session.getId() + " of change to resource " + resource);
            }
            try {
                this.sendMessageUsingView(session, notificationMessage, view);
            } catch (Exception e) {
                if (log.isWarnEnabled()) {
                    log.warn("Error notifying session " + session.getId() + " of change to resource " + resource, e);
                }
            }
        }
    }
}
Also used : User(com.serotonin.m2m2.vo.User) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TemporaryResourceStatus(com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResource.TemporaryResourceStatus)

Example 2 with TemporaryResource

use of com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResource in project ma-modules-public by infiniteautomation.

the class DataPointRestController method getBulkDataPointOperations.

@ApiOperation(value = "Get a list of current bulk data point operations", notes = "User can only get their own bulk data point operations unless they are an admin")
@RequestMapping(method = RequestMethod.GET, value = "/bulk")
public MappingJacksonValue getBulkDataPointOperations(@AuthenticationPrincipal User user, HttpServletRequest request) {
    List<TemporaryResource<DataPointBulkResponse, AbstractRestV2Exception>> preFiltered = this.bulkResourceManager.list().stream().filter((tr) -> user.isAdmin() || user.getId() == tr.getUserId()).collect(Collectors.toList());
    List<TemporaryResource<DataPointBulkResponse, AbstractRestV2Exception>> results = preFiltered;
    ASTNode query = BaseMangoRestController.parseRQLtoAST(request.getQueryString());
    if (query != null) {
        results = query.accept(new RQLToObjectListQuery<TemporaryResource<DataPointBulkResponse, AbstractRestV2Exception>>(), preFiltered);
    }
    PageQueryResultModel<TemporaryResource<DataPointBulkResponse, AbstractRestV2Exception>> result = new PageQueryResultModel<>(results, preFiltered.size());
    // hide result property by setting a view
    MappingJacksonValue resultWithView = new MappingJacksonValue(result);
    resultWithView.setSerializationView(Object.class);
    return resultWithView;
}
Also used : BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) PathVariable(org.springframework.web.bind.annotation.PathVariable) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) RequestParam(org.springframework.web.bind.annotation.RequestParam) DataSourceDao(com.serotonin.m2m2.db.dao.DataSourceDao) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ApiParam(com.wordnik.swagger.annotations.ApiParam) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) VoIndividualRequest(com.infiniteautomation.mango.rest.v2.bulk.VoIndividualRequest) URI(java.net.URI) ConditionSortLimitWithTagKeys(com.infiniteautomation.mango.db.query.ConditionSortLimitWithTagKeys) BaseMangoRestController(com.serotonin.m2m2.web.mvc.rest.BaseMangoRestController) TemporaryResourceWebSocketHandler(com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResourceWebSocketHandler) HttpHeaders(org.springframework.http.HttpHeaders) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) VoAction(com.infiniteautomation.mango.rest.v2.bulk.VoAction) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) ModuleRegistry(com.serotonin.m2m2.module.ModuleRegistry) StreamedArrayWithTotal(com.infiniteautomation.mango.rest.v2.model.StreamedArrayWithTotal) List(java.util.List) Permissions(com.serotonin.m2m2.vo.permission.Permissions) ASTNode(net.jazdw.rql.parser.ASTNode) TemporaryResourceManager(com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResourceManager) VoIndividualResponse(com.infiniteautomation.mango.rest.v2.bulk.VoIndividualResponse) DataPointModel(com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel) LogFactory(org.apache.commons.logging.LogFactory) BulkRequest(com.infiniteautomation.mango.rest.v2.bulk.BulkRequest) BulkResponse(com.infiniteautomation.mango.rest.v2.bulk.BulkResponse) TemporaryResourceWebSocketDefinition(com.serotonin.m2m2.web.mvc.rest.v1.publisher.TemporaryResourceWebSocketDefinition) User(com.serotonin.m2m2.vo.User) RQLToObjectListQuery(com.infiniteautomation.mango.db.query.pojo.RQLToObjectListQuery) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) TemporaryResourceStatusUpdate(com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResourceStatusUpdate) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) RequestBody(org.springframework.web.bind.annotation.RequestBody) TemplateDao(com.serotonin.m2m2.db.dao.TemplateDao) HttpServletRequest(javax.servlet.http.HttpServletRequest) Api(com.wordnik.swagger.annotations.Api) MappingJacksonValue(org.springframework.http.converter.json.MappingJacksonValue) AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) DataPointPropertiesTemplateVO(com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO) Common(com.serotonin.m2m2.Common) TemporaryResourceStatus(com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResource.TemporaryResourceStatus) DataPointFilter(com.serotonin.m2m2.web.mvc.rest.v1.model.dataPoint.DataPointFilter) TemporaryResource(com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResource) HttpStatus(org.springframework.http.HttpStatus) PageQueryResultModel(com.serotonin.m2m2.web.mvc.rest.v1.model.PageQueryResultModel) StreamedVOQueryWithTotal(com.infiniteautomation.mango.rest.v2.model.StreamedVOQueryWithTotal) AuthenticationPrincipal(org.springframework.security.core.annotation.AuthenticationPrincipal) Log(org.apache.commons.logging.Log) ResponseEntity(org.springframework.http.ResponseEntity) MangoTaskTemporaryResourceManager(com.infiniteautomation.mango.rest.v2.temporaryResource.MangoTaskTemporaryResourceManager) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) ASTNode(net.jazdw.rql.parser.ASTNode) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) TemporaryResource(com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResource) MappingJacksonValue(org.springframework.http.converter.json.MappingJacksonValue) RQLToObjectListQuery(com.infiniteautomation.mango.db.query.pojo.RQLToObjectListQuery) PageQueryResultModel(com.serotonin.m2m2.web.mvc.rest.v1.model.PageQueryResultModel) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with TemporaryResource

use of com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResource in project ma-modules-public by infiniteautomation.

the class DataPointRestController method bulkDataPointOperation.

@ApiOperation(value = "Bulk get/create/update/delete data points", notes = "User must have read/edit permission for the data point")
@RequestMapping(method = RequestMethod.POST, value = "/bulk")
public ResponseEntity<TemporaryResource<DataPointBulkResponse, AbstractRestV2Exception>> bulkDataPointOperation(@RequestBody DataPointBulkRequest requestBody, @AuthenticationPrincipal User user, UriComponentsBuilder builder) {
    VoAction defaultAction = requestBody.getAction();
    DataPointModel defaultBody = requestBody.getBody();
    List<DataPointIndividualRequest> 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<DataPointBulkResponse, AbstractRestV2Exception> responseBody = bulkResourceManager.newTemporaryResource(RESOURCE_TYPE_BULK_DATA_POINT, resourceId, user.getId(), expiration, timeout, (resource) -> {
        DataPointBulkResponse bulkResponse = new DataPointBulkResponse();
        int i = 0;
        resource.progress(bulkResponse, i++, requests.size());
        for (DataPointIndividualRequest request : requests) {
            UriComponentsBuilder reqBuilder = UriComponentsBuilder.newInstance();
            DataPointIndividualResponse individualResponse = doIndividualRequest(request, defaultAction, defaultBody, user, reqBuilder);
            bulkResponse.addResponse(individualResponse);
            resource.progressOrSuccess(bulkResponse, i++, requests.size());
        }
    });
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(builder.path("/v2/data-points/bulk/{id}").buildAndExpand(responseBody.getId()).toUri());
    return new ResponseEntity<TemporaryResource<DataPointBulkResponse, AbstractRestV2Exception>>(responseBody, headers, HttpStatus.CREATED);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) VoAction(com.infiniteautomation.mango.rest.v2.bulk.VoAction) DataPointModel(com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) ResponseEntity(org.springframework.http.ResponseEntity) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) 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 4 with TemporaryResource

use of com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResource in project ma-modules-public by infiniteautomation.

the class DataPointTagsRestController method getBulkDataPointTagOperations.

@ApiOperation(value = "Get a list of current bulk tag operations", notes = "User can only get their own bulk tag operations unless they are an admin")
@RequestMapping(method = RequestMethod.GET, value = "/bulk")
public MappingJacksonValue getBulkDataPointTagOperations(@AuthenticationPrincipal User user, HttpServletRequest request) {
    List<TemporaryResource<TagBulkResponse, AbstractRestV2Exception>> preFiltered = this.bulkResourceManager.list().stream().filter((tr) -> user.isAdmin() || user.getId() == tr.getUserId()).collect(Collectors.toList());
    List<TemporaryResource<TagBulkResponse, AbstractRestV2Exception>> results = preFiltered;
    ASTNode query = BaseMangoRestController.parseRQLtoAST(request.getQueryString());
    if (query != null) {
        results = query.accept(new RQLToObjectListQuery<TemporaryResource<TagBulkResponse, AbstractRestV2Exception>>(), preFiltered);
    }
    PageQueryResultModel<TemporaryResource<TagBulkResponse, AbstractRestV2Exception>> result = new PageQueryResultModel<>(results, preFiltered.size());
    // hide result property by setting a view
    MappingJacksonValue resultWithView = new MappingJacksonValue(result);
    resultWithView.setSerializationView(Object.class);
    return resultWithView;
}
Also used : BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) PathVariable(org.springframework.web.bind.annotation.PathVariable) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) RQLToObjectListQuery(com.infiniteautomation.mango.db.query.pojo.RQLToObjectListQuery) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) TemporaryResourceStatusUpdate(com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResourceStatusUpdate) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) HashMap(java.util.HashMap) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) RequestBody(org.springframework.web.bind.annotation.RequestBody) ApiParam(com.wordnik.swagger.annotations.ApiParam) HttpServletRequest(javax.servlet.http.HttpServletRequest) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) Map(java.util.Map) Api(com.wordnik.swagger.annotations.Api) MappingJacksonValue(org.springframework.http.converter.json.MappingJacksonValue) AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) BaseMangoRestController(com.serotonin.m2m2.web.mvc.rest.BaseMangoRestController) TemporaryResourceWebSocketHandler(com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResourceWebSocketHandler) DataPointTagsDao(com.serotonin.m2m2.db.dao.DataPointTagsDao) HttpHeaders(org.springframework.http.HttpHeaders) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) Set(java.util.Set) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) IndividualRequest(com.infiniteautomation.mango.rest.v2.bulk.IndividualRequest) TemporaryResourceStatus(com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResource.TemporaryResourceStatus) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) ModuleRegistry(com.serotonin.m2m2.module.ModuleRegistry) TemporaryResource(com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResource) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) PageQueryResultModel(com.serotonin.m2m2.web.mvc.rest.v1.model.PageQueryResultModel) Permissions(com.serotonin.m2m2.vo.permission.Permissions) ASTNode(net.jazdw.rql.parser.ASTNode) Entry(java.util.Map.Entry) AuthenticationPrincipal(org.springframework.security.core.annotation.AuthenticationPrincipal) TemporaryResourceManager(com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResourceManager) ResponseEntity(org.springframework.http.ResponseEntity) MangoTaskTemporaryResourceManager(com.infiniteautomation.mango.rest.v2.temporaryResource.MangoTaskTemporaryResourceManager) BulkRequest(com.infiniteautomation.mango.rest.v2.bulk.BulkRequest) BulkResponse(com.infiniteautomation.mango.rest.v2.bulk.BulkResponse) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) TemporaryResourceWebSocketDefinition(com.serotonin.m2m2.web.mvc.rest.v1.publisher.TemporaryResourceWebSocketDefinition) User(com.serotonin.m2m2.vo.User) RestExceptionIndividualResponse(com.infiniteautomation.mango.rest.v2.bulk.RestExceptionIndividualResponse) ASTNode(net.jazdw.rql.parser.ASTNode) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) TemporaryResource(com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResource) MappingJacksonValue(org.springframework.http.converter.json.MappingJacksonValue) RQLToObjectListQuery(com.infiniteautomation.mango.db.query.pojo.RQLToObjectListQuery) PageQueryResultModel(com.serotonin.m2m2.web.mvc.rest.v1.model.PageQueryResultModel) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with TemporaryResource

use of com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResource 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)

Aggregations

AbstractRestV2Exception (com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception)5 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)5 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 ResponseEntity (org.springframework.http.ResponseEntity)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 AccessDeniedException (com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException)3 TemporaryResourceStatus (com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResource.TemporaryResourceStatus)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 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)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 TemporaryResourceManager (com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResourceManager)2 TemporaryResourceStatusUpdate (com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResourceStatusUpdate)2