Search in sources :

Example 6 with GenericRestException

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

the class FileStoreRestV2Controller method moveFileOrFolder.

private ResponseEntity<FileModel> moveFileOrFolder(HttpServletRequest request, String fileStoreName, File root, File fileOrFolder, String moveTo) throws IOException, URISyntaxException {
    if (!fileOrFolder.exists()) {
        throw new NotFoundRestException();
    }
    Path srcPath = fileOrFolder.toPath();
    File dstFile = new File(fileOrFolder.getParentFile(), moveTo).getCanonicalFile();
    Path dstPath = dstFile.toPath();
    if (!dstPath.startsWith(root.toPath())) {
        throw new GenericRestException(HttpStatus.FORBIDDEN, new TranslatableMessage("filestore.belowRoot", moveTo));
    }
    if (dstFile.isDirectory()) {
        dstPath = dstPath.resolve(srcPath.getFileName());
    }
    Path movedPath;
    try {
        movedPath = java.nio.file.Files.move(srcPath, dstPath);
    } catch (FileAlreadyExistsException e) {
        throw new GenericRestException(HttpStatus.CONFLICT, new TranslatableMessage("filestore.fileExists", dstPath.getFileName()));
    }
    File movedFile = new File(movedPath.toUri());
    FileModel fileModel = fileToModel(movedFile, 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 7 with GenericRestException

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

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

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

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

the class JsonEmportV2Controller method importConfiguration.

@PreAuthorize("isAdmin()")
@ApiOperation(value = "Import Configuration", notes = "Submit the request and get a URL for the results")
@RequestMapping(method = { RequestMethod.POST })
public ResponseEntity<ImportStatusProvider> importConfiguration(HttpServletRequest request, UriComponentsBuilder builder, @ApiParam(value = "Optional timeout for resource to expire, defaults to 5 minutes", required = false, allowMultiple = false) @RequestParam(value = "timeout", required = false) Long timeout, @RequestBody(required = true) JsonValue config, @AuthenticationPrincipal User user) {
    if (config instanceof JsonObject) {
        // Setup the Temporary Resource
        String resourceId = importStatusResources.generateResourceId();
        ImportStatusProvider statusProvider = new ImportStatusProvider(importStatusResources, resourceId, websocket, timeout, config.toJsonObject(), user);
        this.importStatusResources.put(resourceId, statusProvider);
        URI location = builder.path("/v2/json-emport/import/{id}").buildAndExpand(resourceId).toUri();
        return getResourceCreated(statusProvider, location);
    } else {
        throw new GenericRestException(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("emport.invalidImportData"));
    }
}
Also used : JsonObject(com.serotonin.json.type.JsonObject) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) URI(java.net.URI) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

GenericRestException (com.infiniteautomation.mango.rest.v2.exception.GenericRestException)13 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)11 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)9 ResponseEntity (org.springframework.http.ResponseEntity)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)8 File (java.io.File)6 MultipartFile (org.springframework.web.multipart.MultipartFile)6 CommonsMultipartFile (org.springframework.web.multipart.commons.CommonsMultipartFile)6 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)5 FileModel (com.infiniteautomation.mango.rest.v2.model.filestore.FileModel)3 FileStoreDefinition (com.serotonin.m2m2.module.FileStoreDefinition)3 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 ResourceNotFoundException (com.infiniteautomation.mango.rest.v2.exception.ResourceNotFoundException)2 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)2 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)2 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)2 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)2 ScriptLog (com.serotonin.m2m2.rt.script.ScriptLog)2