Search in sources :

Example 1 with BadRequestException

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

the class JsonDataRestController method replaceNode.

JsonNode replaceNode(final JsonNode existingData, final String[] dataPath, final JsonNode newData) {
    if (dataPath.length == 0) {
        return newData;
    }
    String[] parentPath = Arrays.copyOfRange(dataPath, 0, dataPath.length - 1);
    String fieldName = dataPath[dataPath.length - 1];
    JsonNode parent = getNode(existingData, parentPath);
    if (parent.isObject()) {
        ObjectNode parentObject = (ObjectNode) parent;
        parentObject.set(fieldName, newData);
    } else if (parent.isArray()) {
        ArrayNode parentArray = (ArrayNode) parent;
        int index = toArrayIndex(fieldName);
        parentArray.set(index, newData);
    } else {
        throw new BadRequestException(new TranslatableMessage("rest.error.cantSetFieldOfNodeType", parent.getNodeType()));
    }
    return existingData;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 2 with BadRequestException

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

the class JsonDataRestController method getNode.

JsonNode getNode(final JsonNode existingData, final String[] dataPath) {
    JsonNode node = existingData;
    for (int i = 0; i < dataPath.length; i++) {
        String fieldName = dataPath[i];
        if (node.isObject()) {
            ObjectNode objectNode = (ObjectNode) node;
            node = objectNode.get(fieldName);
        } else if (node.isArray()) {
            ArrayNode arrayNode = (ArrayNode) node;
            int index = toArrayIndex(fieldName);
            node = arrayNode.get(index);
        } else {
            throw new BadRequestException(new TranslatableMessage("rest.error.cantGetFieldOfNodeType", node.getNodeType()));
        }
        if (node == null) {
            throw new NotFoundRestException();
        }
    }
    return node;
}
Also used : NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 3 with BadRequestException

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

the class JsonDataRestController method deleteNode.

boolean deleteNode(final JsonNode existingData, final String[] dataPath) {
    if (dataPath.length == 0)
        throw new IllegalArgumentException();
    String[] parentPath = Arrays.copyOfRange(dataPath, 0, dataPath.length - 1);
    String fieldName = dataPath[dataPath.length - 1];
    JsonNode parent = getNode(existingData, parentPath);
    JsonNode deletedValue = null;
    if (parent.isObject()) {
        ObjectNode parentObject = (ObjectNode) parent;
        deletedValue = parentObject.remove(fieldName);
    } else if (parent.isArray()) {
        ArrayNode parentArray = (ArrayNode) parent;
        int index = toArrayIndex(fieldName);
        deletedValue = parentArray.remove(index);
    } else {
        throw new BadRequestException(new TranslatableMessage("rest.error.cantDeleteFieldOfNodeType", parent.getNodeType()));
    }
    return deletedValue != null;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 4 with BadRequestException

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

the class DataPointRestController method updateDataPoint.

@ApiOperation(value = "Update an existing data point")
@RequestMapping(method = RequestMethod.PUT, value = "/{xid}")
public ResponseEntity<DataPointModel> updateDataPoint(@PathVariable String xid, @ApiParam(value = "Updated data point model", required = true) @RequestBody(required = true) DataPointModel model, @AuthenticationPrincipal User user, UriComponentsBuilder builder) {
    DataPointVO dataPoint = DataPointDao.instance.getByXid(xid);
    if (dataPoint == null) {
        throw new NotFoundRestException();
    }
    Permissions.ensureDataSourcePermission(user, dataPoint.getDataSourceId());
    // check if they are trying to move it to another data source
    String newDataSourceXid = model.getDataSourceXid();
    if (newDataSourceXid != null && !newDataSourceXid.isEmpty() && !newDataSourceXid.equals(dataPoint.getDataSourceXid())) {
        throw new BadRequestException(new TranslatableMessage("rest.error.pointChangeDataSource"));
    }
    DataPointPropertiesTemplateVO template = null;
    if (model.isTemplateXidWasSet()) {
        if (model.getTemplateXid() != null) {
            template = (DataPointPropertiesTemplateVO) TemplateDao.instance.getByXid(model.getTemplateXid());
            if (template == null) {
                throw new BadRequestException(new TranslatableMessage("invalidTemplateXid"));
            }
        }
    } else if (dataPoint.getTemplateId() != null) {
        template = (DataPointPropertiesTemplateVO) TemplateDao.instance.get(dataPoint.getTemplateId());
    }
    DataPointDao.instance.loadPartialRelationalData(dataPoint);
    model.copyPropertiesTo(dataPoint);
    // load the template after copying the properties, template properties override the ones in the data point
    if (template != null) {
        dataPoint.withTemplate(template);
    }
    dataPoint.ensureValid();
    // have to load any existing event detectors for the data point as we are about to replace the VO in the runtime manager
    DataPointDao.instance.setEventDetectors(dataPoint);
    Common.runtimeManager.saveDataPoint(dataPoint);
    URI location = builder.path("/v2/data-points/{xid}").buildAndExpand(xid).toUri();
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(location);
    return new ResponseEntity<>(new DataPointModel(dataPoint), headers, HttpStatus.OK);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) HttpHeaders(org.springframework.http.HttpHeaders) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) ResponseEntity(org.springframework.http.ResponseEntity) DataPointModel(com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel) DataPointPropertiesTemplateVO(com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with BadRequestException

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

the class DataPointRestController method createDataPoint.

@ApiOperation(value = "Create a new data point")
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<DataPointModel> createDataPoint(@ApiParam(value = "Data point model", required = true) @RequestBody(required = true) DataPointModel model, @AuthenticationPrincipal User user, UriComponentsBuilder builder) {
    DataSourceVO<?> dataSource = DataSourceDao.instance.getByXid(model.getDataSourceXid());
    if (dataSource == null) {
        throw new BadRequestException(new TranslatableMessage("rest.error.invalidDataSourceXid"));
    }
    Permissions.ensureDataSourcePermission(user, dataSource);
    DataPointVO dataPoint = new DataPointVO(dataSource);
    model.copyPropertiesTo(dataPoint);
    if (model.getTemplateXid() != null) {
        DataPointPropertiesTemplateVO template = (DataPointPropertiesTemplateVO) TemplateDao.instance.getByXid(model.getTemplateXid());
        if (template == null) {
            throw new BadRequestException(new TranslatableMessage("rest.error.invalidTemplateXid"));
        }
        dataPoint.withTemplate(template);
    }
    dataPoint.ensureValid();
    Common.runtimeManager.saveDataPoint(dataPoint);
    URI location = builder.path("/v2/data-points/{xid}").buildAndExpand(dataPoint.getXid()).toUri();
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(location);
    return new ResponseEntity<>(new DataPointModel(dataPoint), headers, HttpStatus.CREATED);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) DataPointModel(com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel) DataPointPropertiesTemplateVO(com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) URI(java.net.URI) 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