Search in sources :

Example 46 with TranslatableMessage

use of com.serotonin.m2m2.i18n.TranslatableMessage in project ma-modules-public by infiniteautomation.

the class FileStoreRestV2Controller method delete.

@ApiOperation(value = "Delete a file or directory")
@RequestMapping(method = RequestMethod.DELETE, produces = {}, value = "/{name}/**")
public ResponseEntity<Void> delete(@ApiParam(value = "Valid File Store name", required = true, allowMultiple = false) @PathVariable("name") String name, @ApiParam(value = "Recurisve delete of directory", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean recursive, @AuthenticationPrincipal User user, HttpServletRequest request) throws IOException, HttpMediaTypeNotAcceptableException {
    FileStoreDefinition def = ModuleRegistry.getFileStoreDefinition(name);
    if (def == null)
        throw new ResourceNotFoundException("File store: " + name);
    // Check permissions
    def.ensureStoreWritePermission(user);
    File root = def.getRoot().getCanonicalFile();
    String path = parsePath(request);
    File file = new File(root, path).getCanonicalFile();
    if (!file.toPath().startsWith(root.toPath())) {
        throw new GenericRestException(HttpStatus.FORBIDDEN, new TranslatableMessage("filestore.belowRoot", path));
    }
    if (!file.exists())
        throw new NotFoundRestException();
    if (file.isDirectory() && recursive) {
        FileUtils.deleteDirectory(file);
    } else {
        if (!file.delete()) {
            throw new GenericRestException(HttpStatus.INTERNAL_SERVER_ERROR, new TranslatableMessage("filestore.errorDeletingFile"));
        }
    }
    return new ResponseEntity<>(null, HttpStatus.OK);
}
Also used : NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) ResponseEntity(org.springframework.http.ResponseEntity) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ResourceNotFoundException(com.infiniteautomation.mango.rest.v2.exception.ResourceNotFoundException) File(java.io.File) CommonsMultipartFile(org.springframework.web.multipart.commons.CommonsMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) FileStoreDefinition(com.serotonin.m2m2.module.FileStoreDefinition) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 47 with TranslatableMessage

use of com.serotonin.m2m2.i18n.TranslatableMessage in project ma-modules-public by infiniteautomation.

the class FileStoreRestV2Controller method copyFileOrFolder.

private ResponseEntity<FileModel> copyFileOrFolder(HttpServletRequest request, String fileStoreName, File root, File srcFile, String dst) throws IOException, URISyntaxException {
    if (!srcFile.exists()) {
        throw new NotFoundRestException();
    }
    if (srcFile.isDirectory()) {
        throw new GenericRestException(HttpStatus.BAD_REQUEST, new TranslatableMessage("filestore.cantCopyDirectory"));
    }
    Path srcPath = srcFile.toPath();
    File dstFile = new File(srcFile.getParentFile(), dst).getCanonicalFile();
    Path dstPath = dstFile.toPath();
    if (!dstPath.startsWith(root.toPath())) {
        throw new GenericRestException(HttpStatus.FORBIDDEN, new TranslatableMessage("filestore.belowRoot", dst));
    }
    if (dstFile.isDirectory()) {
        dstPath = dstPath.resolve(srcPath.getFileName());
    }
    Path copiedPath;
    try {
        copiedPath = java.nio.file.Files.copy(srcPath, dstPath);
    } catch (FileAlreadyExistsException e) {
        throw new GenericRestException(HttpStatus.CONFLICT, new TranslatableMessage("filestore.fileExists", dstPath.getFileName()));
    }
    File copiedFile = new File(copiedPath.toUri());
    FileModel fileModel = fileToModel(copiedFile, root, request.getServletContext());
    return new ResponseEntity<>(fileModel, HttpStatus.OK);
}
Also used : Path(java.nio.file.Path) FileModel(com.infiniteautomation.mango.rest.v2.model.filestore.FileModel) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) ResponseEntity(org.springframework.http.ResponseEntity) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) File(java.io.File) CommonsMultipartFile(org.springframework.web.multipart.commons.CommonsMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException)

Example 48 with TranslatableMessage

use of com.serotonin.m2m2.i18n.TranslatableMessage in project ma-modules-public by infiniteautomation.

the class FileStoreRestV2Controller method findUniqueFileName.

private File findUniqueFileName(File directory, String filename, boolean overwrite) throws IOException {
    File file = new File(directory, filename).getCanonicalFile();
    if (!file.toPath().startsWith(directory.toPath())) {
        throw new GenericRestException(HttpStatus.FORBIDDEN, new TranslatableMessage("filestore.belowUploadDirectory", filename));
    }
    if (overwrite) {
        return file;
    }
    File parent = file.getParentFile();
    String originalName = Files.getNameWithoutExtension(filename);
    String extension = Files.getFileExtension(filename);
    int i = 1;
    while (file.exists()) {
        if (extension.isEmpty()) {
            file = new File(parent, String.format("%s_%03d", originalName, i++));
        } else {
            file = new File(parent, String.format("%s_%03d.%s", originalName, i++, extension));
        }
    }
    return file;
}
Also used : TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) File(java.io.File) CommonsMultipartFile(org.springframework.web.multipart.commons.CommonsMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException)

Example 49 with TranslatableMessage

use of com.serotonin.m2m2.i18n.TranslatableMessage in project ma-modules-public by infiniteautomation.

the class PasswordResetController method sendEmail.

@ApiOperation(value = "Sends the user an email containing a password reset link")
@RequestMapping(method = RequestMethod.POST, value = "/send-email")
public ResponseEntity<Void> sendEmail(@RequestBody SendEmailRequestBody body) throws AddressException, TemplateException, IOException {
    User user = UserDao.instance.getUser(body.getUsername());
    if (user == null) {
        throw new NotFoundRestException();
    }
    String email = body.getEmail();
    if (email == null) {
        throw new BadRequestException(new TranslatableMessage("rest.error.emailRequired"));
    }
    String providedEmail = email.toLowerCase(Locale.ROOT);
    String userEmail = user.getEmail().toLowerCase(Locale.ROOT);
    if (!providedEmail.equals(userEmail)) {
        throw new BadRequestException(new TranslatableMessage("rest.error.incorrectEmail"));
    }
    if (user.isDisabled()) {
        throw new BadRequestException(new TranslatableMessage("rest.error.userIsDisabled"));
    }
    passwordResetService.sendEmail(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) 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 50 with TranslatableMessage

use of com.serotonin.m2m2.i18n.TranslatableMessage in project ma-modules-public by infiniteautomation.

the class PasswordResetController method createTokenForUser.

@ApiOperation(value = "Creates a password reset token and link for the given user")
@RequestMapping(method = RequestMethod.POST, value = "/create")
@PreAuthorize("isAdmin() and isPasswordAuthenticated()")
public CreateTokenResponse createTokenForUser(@RequestBody CreateTokenRequest requestBody, @AuthenticationPrincipal User currentUser) throws AddressException, TemplateException, IOException {
    String username = requestBody.getUsername();
    boolean lockPassword = requestBody.isLockPassword();
    boolean sendEmail = requestBody.isSendEmail();
    Date expiry = requestBody.getExpiry();
    User user = UserDao.instance.getUser(username);
    if (user == null) {
        throw new BadRequestException(new TranslatableMessage("rest.error.unknownUser", username));
    }
    if (user.getId() == currentUser.getId()) {
        throw new AccessDeniedException(new TranslatableMessage("rest.error.cantResetOwnUser"));
    }
    if (lockPassword) {
        UserDao.instance.lockPassword(user);
    }
    CreateTokenResponse response = new CreateTokenResponse();
    String token = passwordResetService.generateToken(user, expiry);
    response.setToken(token);
    response.setFullUrl(passwordResetService.generateResetUrl(token));
    response.setRelativeUrl(passwordResetService.generateRelativeResetUrl(token));
    if (sendEmail) {
        passwordResetService.sendEmail(user, token);
    }
    return response;
}
Also used : AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) User(com.serotonin.m2m2.vo.User) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) Date(java.util.Date) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)180 User (com.serotonin.m2m2.vo.User)53 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)52 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)52 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)33 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)33 IOException (java.io.IOException)28 HashMap (java.util.HashMap)27 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)24 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)22 ArrayList (java.util.ArrayList)22 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)20 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)20 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)19 BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)18 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)17 File (java.io.File)16 URI (java.net.URI)16 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)12 ResponseEntity (org.springframework.http.ResponseEntity)11