Search in sources :

Example 26 with NotFoundRestException

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

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

the class PointValueRestController method deletePointValues.

@ApiOperation(value = "Delete point values >= from  and < to", notes = "The user must have set permission to the data point. If date is not supplied it defaults to now.")
@RequestMapping(method = RequestMethod.DELETE, value = "/{xid}", produces = { "application/json", "text/csv", "application/sero-json" })
public ResponseEntity<Long> deletePointValues(@ApiParam(value = "Point xids", required = true) @PathVariable String xid, @ApiParam(value = "From time", required = false, allowMultiple = false) @RequestParam(value = "from", required = false) @DateTimeFormat(iso = ISO.DATE_TIME) ZonedDateTime from, @ApiParam(value = "To time", required = false, allowMultiple = false) @RequestParam(value = "to", required = false) @DateTimeFormat(iso = ISO.DATE_TIME) ZonedDateTime to, @ApiParam(value = "Time zone", required = false, allowMultiple = false) @RequestParam(value = "timezone", required = false) String timezone, @AuthenticationPrincipal User user, UriComponentsBuilder builder, HttpServletRequest request) {
    DataPointVO vo = DataPointDao.instance.getByXid(xid);
    if (vo == null) {
        throw new NotFoundRestException();
    } else {
        if (!Permissions.hasDataPointSetPermission(user, vo))
            throw new AccessDeniedException();
    }
    ZoneId zoneId;
    if (timezone == null) {
        if (from != null) {
            zoneId = from.getZone();
        } else if (to != null)
            zoneId = to.getZone();
        else
            zoneId = TimeZone.getDefault().toZoneId();
    } else {
        zoneId = ZoneId.of(timezone);
    }
    // Set the timezone on the from and to dates
    long current = Common.timer.currentTimeMillis();
    if (from != null)
        from = from.withZoneSameInstant(zoneId);
    else
        from = ZonedDateTime.ofInstant(Instant.ofEpochMilli(current), zoneId);
    if (to != null)
        to = to.withZoneSameInstant(zoneId);
    else
        to = ZonedDateTime.ofInstant(Instant.ofEpochMilli(current), zoneId);
    return ResponseEntity.ok(Common.runtimeManager.purgeDataPointValuesBetween(vo.getId(), from.toInstant().toEpochMilli(), to.toInstant().toEpochMilli()));
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) ZoneId(java.time.ZoneId) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 28 with NotFoundRestException

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

the class AuthenticationTokenRestController method createTokenForUser.

@ApiOperation(value = "Revoke all tokens for user", notes = "Revokes all tokens for a given user")
@RequestMapping(path = "/revoke/{username}", method = RequestMethod.POST)
@PreAuthorize("isAdmin() and isPasswordAuthenticated()")
public ResponseEntity<Void> createTokenForUser(@PathVariable String username, HttpServletRequest request) {
    User user = UserDao.instance.getUser(username);
    if (user == null) {
        throw new NotFoundRestException();
    }
    tokenAuthService.revokeTokens(user);
    sessionRegistry.userUpdated(request, user);
    return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
Also used : NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) ResponseEntity(org.springframework.http.ResponseEntity) User(com.serotonin.m2m2.vo.User) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 29 with NotFoundRestException

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

the class DataPointTagsRestController method addTagsForDataPoint.

@ApiOperation(value = "Merge data point tags by data point XID", notes = "User must have edit permission for the data point's data source." + "Adds a tag or replaces the tag value for each tag key. Any other existing tags will be kept.")
@RequestMapping(method = RequestMethod.PUT, value = "/point/{xid}")
public Map<String, String> addTagsForDataPoint(@ApiParam(value = "Data point XID", required = true, allowMultiple = false) @PathVariable String xid, @RequestBody Map<String, String> tags, @AuthenticationPrincipal User user) {
    return DataPointTagsDao.instance.doInTransaction(txStatus -> {
        DataPointVO dataPoint = DataPointDao.instance.getByXid(xid);
        if (dataPoint == null) {
            throw new NotFoundRestException();
        }
        Permissions.ensureDataSourcePermission(user, dataPoint.getDataSourceId());
        Map<String, String> existingTags = DataPointTagsDao.instance.getTagsForDataPointId(dataPoint.getId());
        Map<String, String> newTags = new HashMap<>(existingTags);
        for (Entry<String, String> entry : tags.entrySet()) {
            String tagKey = entry.getKey();
            String tagVal = entry.getValue();
            if (tagVal == null) {
                newTags.remove(tagKey);
            } else {
                newTags.put(tagKey, tagVal);
            }
        }
        dataPoint.setTags(newTags);
        DataPointTagsDao.instance.saveDataPointTags(dataPoint);
        // we set the tags on the data point then retrieve them so that the device and name tags are removed
        return dataPoint.getTags();
    });
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) HashMap(java.util.HashMap) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 30 with NotFoundRestException

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

the class VirtualSerialPortRestV2Controller method update.

@PreAuthorize("isAdmin()")
@ApiOperation(value = "Update virtual serial port", notes = "")
@RequestMapping(method = RequestMethod.PUT, consumes = { "application/json", "application/sero-json" }, produces = { "application/json", "text/csv", "application/sero-json" }, value = { "/{xid}" })
public ResponseEntity<VirtualSerialPortConfig> update(@ApiParam(value = "Valid virtual serial port id", required = true, allowMultiple = false) @PathVariable String xid, @ApiParam(value = "Virtual Serial Port", required = true) @RequestBody(required = true) VirtualSerialPortConfig model, @AuthenticationPrincipal User user, UriComponentsBuilder builder, HttpServletRequest request) {
    // Check to see if it already exists
    VirtualSerialPortConfig existing = VirtualSerialPortConfigDao.instance.getByXid(model.getXid());
    if (existing == null)
        throw new NotFoundRestException();
    // Validate
    model.ensureValid();
    // Save it
    VirtualSerialPortConfigDao.instance.save(model);
    // Put a link to the updated data in the header
    URI location = builder.path("/v2/virtual-serial-ports/{xid}").buildAndExpand(model.getXid()).toUri();
    return getResourceUpdated(model, location);
}
Also used : NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) VirtualSerialPortConfig(com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfig) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)35 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)31 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)31 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)19 ResponseEntity (org.springframework.http.ResponseEntity)17 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)12 GenericRestException (com.infiniteautomation.mango.rest.v2.exception.GenericRestException)6 File (java.io.File)6 URI (java.net.URI)6 User (com.serotonin.m2m2.vo.User)5 MultipartFile (org.springframework.web.multipart.MultipartFile)5 CommonsMultipartFile (org.springframework.web.multipart.commons.CommonsMultipartFile)5 BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)4 DataPointModel (com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel)4 ArrayList (java.util.ArrayList)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 AccessDeniedException (com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException)3 FileModel (com.infiniteautomation.mango.rest.v2.model.filestore.FileModel)3 FileStoreDefinition (com.serotonin.m2m2.module.FileStoreDefinition)3 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)3