Search in sources :

Example 11 with DataPointModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.DataPointModel in project ma-modules-public by infiniteautomation.

the class DataPointRestController method getDataPoint.

@ApiOperation(value = "Get data point by XID", notes = "Only points that user has read permission to are returned")
@RequestMapping(method = RequestMethod.GET, value = "/{xid}")
public DataPointModel getDataPoint(@ApiParam(value = "Valid Data Point XID", required = true, allowMultiple = false) @PathVariable String xid, @AuthenticationPrincipal User user) {
    DataPointVO dataPoint = DataPointDao.instance.getByXid(xid);
    if (dataPoint == null) {
        throw new NotFoundRestException();
    }
    DataPointDao.instance.loadPartialRelationalData(dataPoint);
    Permissions.ensureDataPointReadPermission(user, dataPoint);
    return new DataPointModel(dataPoint);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) DataPointModel(com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with DataPointModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.DataPointModel in project ma-modules-public by infiniteautomation.

the class DataPointRestController method deleteDataPoint.

@ApiOperation(value = "Delete a data point")
@RequestMapping(method = RequestMethod.DELETE, value = "/{xid}")
public DataPointModel deleteDataPoint(@ApiParam(value = "Valid Data Point XID", required = true, allowMultiple = false) @PathVariable String xid, @AuthenticationPrincipal User user) {
    DataPointVO dataPoint = DataPointDao.instance.getByXid(xid);
    if (dataPoint == null) {
        throw new NotFoundRestException();
    }
    Permissions.ensureDataPointReadPermission(user, dataPoint);
    Common.runtimeManager.deleteDataPoint(dataPoint);
    return new DataPointModel(dataPoint);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) DataPointModel(com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with DataPointModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.DataPointModel in project ma-modules-public by infiniteautomation.

the class DataPointRestController method getDataPointById.

@ApiOperation(value = "Get data point by ID", notes = "Only points that user has read permission to are returned")
@RequestMapping(method = RequestMethod.GET, value = "/by-id/{id}")
public DataPointModel getDataPointById(@ApiParam(value = "Valid Data Point ID", required = true, allowMultiple = false) @PathVariable int id, @AuthenticationPrincipal User user) {
    DataPointVO dataPoint = DataPointDao.instance.get(id);
    if (dataPoint == null) {
        throw new NotFoundRestException();
    }
    DataPointDao.instance.loadPartialRelationalData(dataPoint);
    Permissions.ensureDataPointReadPermission(user, dataPoint);
    return new DataPointModel(dataPoint);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) DataPointModel(com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with DataPointModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.DataPointModel 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 15 with DataPointModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.DataPointModel in project ma-modules-public by infiniteautomation.

the class DataPointRestController method getDataPointsForDataSource.

@ApiOperation(value = "Get all data points for data source", notes = "Returned as CSV or JSON, only points that user has read permission to are returned")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json", "text/csv", "application/sero-json" }, value = "/data-source/{xid}")
public ResponseEntity<List<DataPointModel>> getDataPointsForDataSource(@ApiParam(value = "Valid Data Source XID", required = true, allowMultiple = false) @PathVariable String xid, HttpServletRequest request) {
    RestProcessResult<List<DataPointModel>> result = new RestProcessResult<List<DataPointModel>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        DataSourceVO<?> dataSource = DataSourceDao.instance.getDataSource(xid);
        if (dataSource == null) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        }
        try {
            if (!Permissions.hasDataSourcePermission(user, dataSource)) {
                LOG.warn("User: " + user.getUsername() + " tried to access data source with xid " + xid);
                result.addRestMessage(getUnauthorizedMessage());
                return result.createResponseEntity();
            }
        } catch (PermissionException e) {
            LOG.warn(e.getMessage(), e);
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        }
        List<DataPointVO> dataPoints = DataPointDao.instance.getDataPoints(dataSource.getId(), null);
        List<DataPointModel> userDataPoints = new ArrayList<DataPointModel>();
        for (DataPointVO vo : dataPoints) {
            try {
                if (Permissions.hasDataPointReadPermission(user, vo)) {
                    userDataPoints.add(new DataPointModel(vo));
                }
            } catch (PermissionException e) {
            // Munched
            }
        }
        result.addRestMessage(getSuccessMessage());
        return result.createResponseEntity(userDataPoints);
    }
    return result.createResponseEntity();
}
Also used : PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) DataPointModel(com.serotonin.m2m2.web.mvc.rest.v1.model.DataPointModel) User(com.serotonin.m2m2.vo.User) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ApiOperation (com.wordnik.swagger.annotations.ApiOperation)18 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)18 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)16 User (com.serotonin.m2m2.vo.User)11 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)11 DataPointModel (com.serotonin.m2m2.web.mvc.rest.v1.model.DataPointModel)11 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)9 DataPointModel (com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel)8 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)7 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)6 DataPointPropertiesTemplateVO (com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO)5 URI (java.net.URI)5 BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)4 ResponseEntity (org.springframework.http.ResponseEntity)4 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)3 PlainRenderer (com.serotonin.m2m2.view.text.PlainRenderer)3 RestMessage (com.serotonin.m2m2.web.mvc.rest.v1.message.RestMessage)3 DataPointStreamCallback (com.serotonin.m2m2.web.mvc.rest.v1.model.dataPoint.DataPointStreamCallback)3 HttpHeaders (org.springframework.http.HttpHeaders)3 VoAction (com.infiniteautomation.mango.rest.v2.bulk.VoAction)2