Search in sources :

Example 11 with BadRequestException

use of com.infiniteautomation.mango.rest.v2.exception.BadRequestException in project ma-modules-public by infiniteautomation.

the class DataPointRestController method doIndividualRequest.

private DataPointIndividualResponse doIndividualRequest(DataPointIndividualRequest request, VoAction defaultAction, DataPointModel defaultBody, User user, UriComponentsBuilder builder) {
    DataPointIndividualResponse result = new DataPointIndividualResponse();
    try {
        String xid = request.getXid();
        VoAction action = request.getAction() == null ? defaultAction : request.getAction();
        if (action == null) {
            throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "action"));
        }
        result.setAction(action);
        DataPointModel body = request.getBody() == null ? defaultBody : request.getBody();
        switch(action) {
            case GET:
                if (xid == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
                }
                result.setBody(this.getDataPoint(xid, user));
                break;
            case CREATE:
                if (body == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
                }
                result.setBody(this.createDataPoint(body, user, builder).getBody());
                break;
            case UPDATE:
                if (xid == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
                }
                if (body == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
                }
                result.setBody(this.updateDataPoint(xid, body, user, builder).getBody());
                break;
            case DELETE:
                if (xid == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
                }
                result.setBody(this.deleteDataPoint(xid, user));
                break;
        }
    } catch (Exception e) {
        result.exceptionCaught(e);
    }
    return result;
}
Also used : VoAction(com.infiniteautomation.mango.rest.v2.bulk.VoAction) DataPointModel(com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)

Example 12 with BadRequestException

use of com.infiniteautomation.mango.rest.v2.exception.BadRequestException in project ma-modules-public by infiniteautomation.

the class PointValueRestController method buildMap.

/**
 * Build and validate the map of Requested Data Points
 * @param user
 * @param xids
 * @return
 */
protected Map<Integer, DataPointVO> buildMap(User user, String[] xids, RollupEnum rollup) {
    // Build the map, check permissions
    Map<Integer, DataPointVO> voMap = new HashMap<Integer, DataPointVO>();
    for (String xid : xids) {
        DataPointVO vo = DataPointDao.instance.getByXid(xid);
        if (vo == null) {
            throw new NotFoundRestException();
        } else {
            if (!Permissions.hasDataPointReadPermission(user, vo))
                throw new AccessDeniedException();
        }
        // TODO Add support for NONE Default Rollup
        if (rollup == RollupEnum.POINT_DEFAULT && vo.getRollup() == RollupEnum.NONE.getId())
            throw new BadRequestException(new TranslatableMessage("common.default", "Default point rollup of NONE is not yet supported for point with xid: " + xid));
        ;
        // Validate the rollup
        switch(vo.getPointLocator().getDataTypeId()) {
            case DataTypes.ALPHANUMERIC:
            case DataTypes.BINARY:
            case DataTypes.IMAGE:
            case DataTypes.MULTISTATE:
                if (rollup.nonNumericSupport() == false)
                    throw new BadRequestException(new TranslatableMessage("rest.validate.rollup.incompatible", rollup.toString(), xid));
                break;
            case DataTypes.NUMERIC:
                break;
        }
        voMap.put(vo.getId(), vo);
    }
    // Do we have any points
    if (voMap.isEmpty())
        throw new NotFoundRestException();
    return voMap;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) HashMap(java.util.HashMap) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 13 with BadRequestException

use of com.infiniteautomation.mango.rest.v2.exception.BadRequestException in project ma-modules-public by infiniteautomation.

the class DataPointTagsRestController method bulkDataPointTagOperationSync.

@ApiOperation(value = "Synchronously 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-sync")
public TagBulkResponse bulkDataPointTagOperationSync(@RequestBody TagBulkRequest requestBody, @AuthenticationPrincipal User user) {
    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"));
    }
    TagBulkResponse response = new TagBulkResponse();
    for (TagIndividualRequest request : requests) {
        TagIndividualResponse individualResponse = doIndividualRequest(request, defaultAction, defaultBody, user);
        response.addResponse(individualResponse);
    }
    return response;
}
Also used : 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 14 with BadRequestException

use of com.infiniteautomation.mango.rest.v2.exception.BadRequestException in project ma-modules-public by infiniteautomation.

the class DataPointTagsRestController method doIndividualRequest.

private TagIndividualResponse doIndividualRequest(TagIndividualRequest request, BulkTagAction defaultAction, Map<String, String> defaultBody, User user) {
    TagIndividualResponse result = new TagIndividualResponse();
    try {
        String xid = request.getXid();
        if (xid == null) {
            throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
        }
        result.setXid(xid);
        BulkTagAction action = request.getAction() == null ? defaultAction : request.getAction();
        if (action == null) {
            throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "action"));
        }
        result.setAction(action);
        Map<String, String> tags = request.getBody() == null ? defaultBody : request.getBody();
        switch(action) {
            case GET:
                result.setBody(this.getTagsForDataPoint(xid, user));
                break;
            case SET:
                if (tags == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
                }
                result.setBody(this.setTagsForDataPoint(xid, tags, user));
                break;
            case MERGE:
                if (tags == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
                }
                result.setBody(this.addTagsForDataPoint(xid, tags, user));
                break;
        }
    } catch (Exception e) {
        result.exceptionCaught(e);
    }
    return result;
}
Also used : BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)

Example 15 with BadRequestException

use of com.infiniteautomation.mango.rest.v2.exception.BadRequestException 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

BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)17 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)17 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)6 ResponseEntity (org.springframework.http.ResponseEntity)6 AccessDeniedException (com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 AbstractRestV2Exception (com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception)4 DataPointModel (com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel)4 HttpHeaders (org.springframework.http.HttpHeaders)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)3 User (com.serotonin.m2m2.vo.User)3 URI (java.net.URI)3 VoAction (com.infiniteautomation.mango.rest.v2.bulk.VoAction)2 JsonObject (com.serotonin.json.type.JsonObject)2 DataPointPropertiesTemplateVO (com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO)2