Search in sources :

Example 1 with RemainingPath

use of com.infiniteautomation.mango.rest.latest.resolver.RemainingPath in project ma-modules-public by infiniteautomation.

the class ScriptRestController method evalScript.

@Async
@ApiOperation(value = "Evaluate a filestore file as a script on the backend using a scripting engine")
@RequestMapping(method = RequestMethod.POST, value = "/eval-file-store/{fileStoreName}/**")
public CompletableFuture<Void> evalScript(@ApiParam(value = "File store name", required = true) @PathVariable(required = true) String fileStoreName, @ApiParam(value = "Script engine name", required = false) @RequestParam(required = false) String engineName, @ApiParam(value = "Script file character set", required = false, defaultValue = "UTF-8") @RequestParam(required = false, defaultValue = "UTF-8") String fileCharset, @ApiParam(value = "Script roles", required = false, allowMultiple = true) @RequestParam(required = false) String[] roles, @ApiIgnore @RemainingPath String path, @AuthenticationPrincipal PermissionHolder user, HttpServletRequest request, HttpServletResponse response) throws IOException {
    Path filePath = fileStoreService.getPathForRead(fileStoreName, path);
    if (!Files.exists(filePath)) {
        throw new NotFoundException();
    }
    if (engineName == null) {
        engineName = scriptService.findEngineForFile(filePath);
    }
    Charset fileCharsetParsed = Charset.forName(fileCharset);
    Set<Role> roleSet;
    if (roles != null) {
        roleSet = Arrays.stream(roles).map(xid -> this.roleService.get(xid).getRole()).collect(Collectors.toSet());
    } else {
        roleSet = user.getRoles();
    }
    EvalContext evalContext = new EvalContext();
    Reader reader = new BufferedReader(new InputStreamReader(request.getInputStream(), Charset.forName(request.getCharacterEncoding())));
    Writer writer = new OutputStreamWriter(response.getOutputStream(), Charset.forName(response.getCharacterEncoding()));
    evalContext.setReader(reader);
    evalContext.setWriter(writer);
    evalContext.addBinding("reader", reader);
    evalContext.addBinding("writer", writer);
    if (permissionService.hasPermission(user, requestResponsePermission.getPermission())) {
        evalContext.addBinding("request", request);
        evalContext.addBinding("response", response);
    }
    this.scriptService.eval(new PathMangoScript(engineName, roleSet, filePath, fileCharsetParsed), evalContext);
    return CompletableFuture.completedFuture(null);
}
Also used : Path(java.nio.file.Path) RemainingPath(com.infiniteautomation.mango.rest.latest.resolver.RemainingPath) Role(com.serotonin.m2m2.vo.role.Role) InputStreamReader(java.io.InputStreamReader) EvalContext(com.infiniteautomation.mango.spring.script.EvalContext) BufferedReader(java.io.BufferedReader) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) Charset(java.nio.charset.Charset) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) PathMangoScript(com.infiniteautomation.mango.spring.script.PathMangoScript) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) Async(org.springframework.scheduling.annotation.Async) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with RemainingPath

use of com.infiniteautomation.mango.rest.latest.resolver.RemainingPath in project ma-modules-public by infiniteautomation.

the class FileStoreRestController method uploadWithPath.

@ApiOperation(value = "Upload a file to a store with a path", notes = "Must have write access to the store")
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, value = "/{name}/**")
public ResponseEntity<List<FileModel>> uploadWithPath(@ApiParam(value = "Valid File Store name", required = true) @PathVariable("name") String name, @RequestParam(required = false, defaultValue = "false") boolean overwrite, @ApiIgnore @RemainingPath String pathInStore, MultipartHttpServletRequest multipartRequest, HttpServletRequest request) throws IOException {
    FileStorePath outputDirectory = this.service.createDirectory(name, pathInStore);
    // Put the file where it belongs
    List<FileModel> fileModels = new ArrayList<>();
    MultiValueMap<String, MultipartFile> filesMap = multipartRequest.getMultiFileMap();
    for (String nameField : filesMap.keySet()) {
        for (MultipartFile file : filesMap.get(nameField)) {
            String filename;
            if (file instanceof CommonsMultipartFile) {
                FileItem fileItem = ((CommonsMultipartFile) file).getFileItem();
                filename = fileItem.getName();
            } else {
                filename = file.getName();
            }
            Path newFile = findUniqueFileName(outputDirectory.getAbsolutePath(), filename, overwrite);
            try (OutputStream output = Files.newOutputStream(newFile)) {
                try (InputStream input = file.getInputStream()) {
                    StreamUtils.copy(input, output);
                }
            }
            fileModels.add(fileToModel(outputDirectory.resolve(newFile), request.getServletContext()));
        }
    }
    return new ResponseEntity<>(fileModels, HttpStatus.OK);
}
Also used : Path(java.nio.file.Path) RemainingPath(com.infiniteautomation.mango.rest.latest.resolver.RemainingPath) FileStorePath(com.infiniteautomation.mango.spring.service.FileStoreService.FileStorePath) FileStorePath(com.infiniteautomation.mango.spring.service.FileStoreService.FileStorePath) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) CommonsMultipartFile(org.springframework.web.multipart.commons.CommonsMultipartFile) FileModel(com.infiniteautomation.mango.rest.latest.model.filestore.FileModel) FileItem(org.apache.commons.fileupload.FileItem) CommonsMultipartFile(org.springframework.web.multipart.commons.CommonsMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) ResponseEntity(org.springframework.http.ResponseEntity) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

RemainingPath (com.infiniteautomation.mango.rest.latest.resolver.RemainingPath)2 ApiOperation (io.swagger.annotations.ApiOperation)2 Path (java.nio.file.Path)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 FileModel (com.infiniteautomation.mango.rest.latest.model.filestore.FileModel)1 EvalContext (com.infiniteautomation.mango.spring.script.EvalContext)1 PathMangoScript (com.infiniteautomation.mango.spring.script.PathMangoScript)1 FileStorePath (com.infiniteautomation.mango.spring.service.FileStoreService.FileStorePath)1 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)1 Role (com.serotonin.m2m2.vo.role.Role)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Reader (java.io.Reader)1 Writer (java.io.Writer)1 Charset (java.nio.charset.Charset)1 ArrayList (java.util.ArrayList)1 FileItem (org.apache.commons.fileupload.FileItem)1