Search in sources :

Example 1 with ResourceNotFoundException

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

the class FileStoreRestV2Controller method download.

@ApiOperation(value = "List a directory or download a file from a store")
@RequestMapping(method = RequestMethod.GET, produces = {}, value = "/{name}/**")
public ResponseEntity<?> download(@ApiParam(value = "Valid File Store name", required = true, allowMultiple = false) @PathVariable("name") String name, @ApiParam(value = "Set content disposition to attachment", required = false, defaultValue = "true", allowMultiple = false) @RequestParam(required = false, defaultValue = "true") boolean download, @AuthenticationPrincipal User user, HttpServletRequest request, HttpServletResponse response) throws IOException, HttpMediaTypeNotAcceptableException {
    FileStoreDefinition def = ModuleRegistry.getFileStoreDefinition(name);
    if (def == null)
        throw new ResourceNotFoundException("File store: " + name);
    // Check permissions
    def.ensureStoreReadPermission(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 AccessDeniedException("Path is below file store root");
    }
    // TODO Allow downloading directory as a zip
    if (file.isFile()) {
        return getFile(file, download, request, response);
    } else {
        return listStoreContents(file, root, request);
    }
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) 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) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ResourceNotFoundException

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

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

the class FileStoreRestV2Controller method listStoreContents.

protected ResponseEntity<List<FileModel>> listStoreContents(File directory, File root, HttpServletRequest request) throws IOException {
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
    if (directory.equals(root) && !root.exists())
        return new ResponseEntity<>(Collections.emptyList(), responseHeaders, HttpStatus.OK);
    if (!directory.exists()) {
        throw new ResourceNotFoundException(relativePath(root, directory));
    }
    Collection<File> files = Arrays.asList(directory.listFiles());
    List<FileModel> found = new ArrayList<>(files.size());
    for (File file : files) found.add(fileToModel(file, root, request.getServletContext()));
    Set<MediaType> mediaTypes = Sets.newHashSet(MediaType.APPLICATION_JSON_UTF8);
    request.setAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, mediaTypes);
    return new ResponseEntity<>(found, responseHeaders, HttpStatus.OK);
}
Also used : FileModel(com.infiniteautomation.mango.rest.v2.model.filestore.FileModel) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) ArrayList(java.util.ArrayList) MediaType(org.springframework.http.MediaType) 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)

Aggregations

ResourceNotFoundException (com.infiniteautomation.mango.rest.v2.exception.ResourceNotFoundException)3 File (java.io.File)3 MultipartFile (org.springframework.web.multipart.MultipartFile)3 CommonsMultipartFile (org.springframework.web.multipart.commons.CommonsMultipartFile)3 FileStoreDefinition (com.serotonin.m2m2.module.FileStoreDefinition)2 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)2 ResponseEntity (org.springframework.http.ResponseEntity)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 GenericRestException (com.infiniteautomation.mango.rest.v2.exception.GenericRestException)1 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)1 FileModel (com.infiniteautomation.mango.rest.v2.model.filestore.FileModel)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 ArrayList (java.util.ArrayList)1 HttpHeaders (org.springframework.http.HttpHeaders)1 MediaType (org.springframework.http.MediaType)1 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1