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);
}
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);
}
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;
}
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);
}
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;
}
Aggregations