Search in sources :

Example 31 with NotFoundRestException

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

the class PublisherRestV2Controller method updatePublisher.

/**
 * Update a publisher
 * @param xid
 * @param model
 * @param builder
 * @param request
 * @return
 */
@ApiOperation(value = "Update publisher", notes = "admin only")
@RequestMapping(method = RequestMethod.PUT, value = "/{xid}", consumes = { "application/json", "application/sero-json" }, produces = { "application/json", "application/sero-json" })
public ResponseEntity<AbstractPublisherModel<?, ?>> updatePublisher(@AuthenticationPrincipal User user, @ApiParam(value = "Valid Publisher XID", required = true, allowMultiple = false) @PathVariable String xid, @RequestBody(required = true) AbstractPublisherModel<?, ?> model, UriComponentsBuilder builder, HttpServletRequest request) {
    assertAdmin(user);
    PublisherVO<?> vo = model.getData();
    PublisherVO<?> existing = this.dao.getByXid(xid);
    if (existing == null)
        throw new NotFoundRestException();
    vo.setId(existing.getId());
    vo.ensureValid();
    Common.runtimeManager.savePublisher(vo);
    URI location = builder.path("/v2/publishers/{xid}").buildAndExpand(xid).toUri();
    return getResourceUpdated(vo.asModel(), location);
}
Also used : NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 32 with NotFoundRestException

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

the class PublisherRestV2Controller method getPublisher.

@ApiOperation(value = "Get publisher by xid", notes = "Only returns publishers for admin users")
@RequestMapping(method = RequestMethod.GET, value = "/{xid}", produces = { "application/json" })
public ResponseEntity<AbstractPublisherModel<?, ?>> getPublisher(@AuthenticationPrincipal User user, HttpServletRequest request, @ApiParam(value = "Valid Publisher XID", required = true, allowMultiple = false) @PathVariable String xid) {
    assertAdmin(user);
    PublisherVO<?> vo = this.dao.getByXid(xid);
    if (vo == null)
        throw new NotFoundRestException();
    else
        return new ResponseEntity<>(vo.asModel(), HttpStatus.OK);
}
Also used : NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 33 with NotFoundRestException

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

the class PointValueRestController method getLatestPointValuesForDataSourceAsMultipleArrays.

/**
 * Get the latest point values a set of points return as map of xid to array of values
 *
 * @param xid
 * @param limit
 * @return
 */
@ApiOperation(value = "Get Latest Point Values for all points on a data source directly from the Runtime Manager, this makes Cached and Intra-Interval data available.", notes = "Default limit 100, time descending order, Default to return cached data. Returns data as map of xid to values.")
@RequestMapping(method = RequestMethod.GET, value = "/{dataSourceXid}/latest-data-source-multiple-arrays", produces = { "application/json", "text/csv" })
public ResponseEntity<ObjectStream<Map<String, List<PointValueTime>>>> getLatestPointValuesForDataSourceAsMultipleArrays(HttpServletRequest request, @ApiParam(value = "Data source xid", required = true, allowMultiple = true) @PathVariable String dataSourceXid, @ApiParam(value = "Return rendered value as String", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean useRendered, @ApiParam(value = "Return converted value using displayed unit", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean unitConversion, @ApiParam(value = "Limit results", allowMultiple = false, defaultValue = "100") @RequestParam(value = "limit", defaultValue = "100") int limit, @ApiParam(value = "Return cached data?", allowMultiple = false, defaultValue = "true") @RequestParam(value = "useCache", defaultValue = "true") boolean useCache, @ApiParam(value = "Date Time format pattern for timestamps as strings, if not included epoch milli number is used", required = false, allowMultiple = false) @RequestParam(value = "dateTimeFormat", required = false) String dateTimeFormat, @ApiParam(value = "Time zone of output, used if formatted times are returned", required = false, allowMultiple = false) @RequestParam(value = "timezone", required = false) String timezone) {
    RestProcessResult<ObjectStream<Map<String, List<PointValueTime>>>> result = new RestProcessResult<ObjectStream<Map<String, List<PointValueTime>>>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        DataSourceVO<?> ds = DataSourceDao.instance.getByXid(dataSourceXid);
        if (ds == null)
            throw new NotFoundRestException();
        if (dateTimeFormat != null) {
            try {
                DateTimeFormatter.ofPattern(dateTimeFormat);
            } catch (IllegalArgumentException e) {
                RestValidationResult vr = new RestValidationResult();
                vr.addError("validate.invalid", "dateTimeFormat");
                throw new ValidationFailedRestException(vr);
            }
        }
        if (timezone != null) {
            try {
                ZoneId.of(timezone);
            } catch (Exception e) {
                RestValidationResult vr = new RestValidationResult();
                vr.addError("validate.invalidValue", "timezone");
                throw new ValidationFailedRestException(vr);
            }
        }
        List<DataPointVO> points = DataPointDao.instance.getDataPointsForDataSourceStart(ds.getId());
        Map<Integer, DataPointVO> pointIdMap = new HashMap<Integer, DataPointVO>(points.size());
        for (DataPointVO vo : points) {
            if (Permissions.hasDataPointReadPermission(user, vo))
                pointIdMap.put(vo.getId(), vo);
            else {
                // Abort, invalid permissions
                result.addRestMessage(getUnauthorizedMessage());
                return result.createResponseEntity();
            }
        }
        // Do we have any valid points?
        if (pointIdMap.size() == 0) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        }
        try {
            XidPointValueTimeLatestPointFacadeStream pvtDatabaseStream = new XidPointValueTimeLatestPointFacadeStream(pointIdMap, useRendered, unitConversion, limit, useCache, dateTimeFormat, timezone);
            return result.createResponseEntity(pvtDatabaseStream);
        } catch (PermissionException e) {
            LOG.error(e.getMessage(), e);
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    } else {
        return result.createResponseEntity();
    }
}
Also used : RestValidationResult(com.infiniteautomation.mango.rest.v2.model.RestValidationResult) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) User(com.serotonin.m2m2.vo.User) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) HashMap(java.util.HashMap) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) RTException(com.serotonin.m2m2.rt.RTException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) XidPointValueTimeLatestPointFacadeStream(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeLatestPointFacadeStream) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ObjectStream(com.serotonin.m2m2.web.mvc.rest.v1.model.ObjectStream) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 34 with NotFoundRestException

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

the class DataSourceRestController method enableDisable.

@ApiOperation(value = "Enable/disable/restart a data source")
@RequestMapping(method = RequestMethod.PUT, value = "/enable-disable/{xid}")
public ResponseEntity<DataPointModel> enableDisable(@AuthenticationPrincipal User user, @PathVariable String xid, @ApiParam(value = "Enable or disable the data source", required = true, allowMultiple = false) @RequestParam(required = true) boolean enabled, @ApiParam(value = "Restart the data source, enabled must equal true", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean restart) {
    DataSourceVO<?> dsvo = DataSourceDao.instance.getByXid(xid);
    if (dsvo == null)
        throw new NotFoundRestException();
    try {
        Permissions.ensureDataSourcePermission(user, dsvo);
    } catch (PermissionException e) {
        throw new AccessDeniedException("User does not have permission to edit the data source", e);
    }
    if (enabled && restart) {
        dsvo.setEnabled(true);
        // saving will restart it
        Common.runtimeManager.saveDataSource(dsvo);
    } else if (dsvo.isEnabled() != enabled) {
        dsvo.setEnabled(enabled);
        Common.runtimeManager.saveDataSource(dsvo);
    }
    return new ResponseEntity<>(HttpStatus.OK);
}
Also used : PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) ResponseEntity(org.springframework.http.ResponseEntity) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 35 with NotFoundRestException

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

the class UserRestController method lockPassword.

@ApiOperation(value = "Locks a user's password", notes = "The user with a locked password cannot login using a username and password. " + "However the user's auth tokens will still work and the user can still reset their password using a reset token or email link")
@RequestMapping(method = RequestMethod.PUT, value = "/{username}/lock-password")
public ResponseEntity<Void> lockPassword(@ApiParam(value = "Username", required = true, allowMultiple = false) @PathVariable String username, @AuthenticationPrincipal User currentUser) {
    if (!currentUser.isAdmin()) {
        throw new AccessDeniedException();
    }
    User user = UserDao.instance.getUser(username);
    if (user == null) {
        throw new NotFoundRestException();
    }
    UserDao.instance.lockPassword(user);
    return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
Also used : AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) 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) 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