Search in sources :

Example 1 with FileEntry

use of org.ngrinder.script.model.FileEntry in project Sermant by huaweicloud.

the class RestFileEntryController method upload.

private void upload(User user, String path, String description, MultipartFile file) throws IOException {
    FileEntry fileEntry = new FileEntry();
    fileEntry.setContentBytes(file.getBytes());
    fileEntry.setDescription(description);
    fileEntry.setPath(FilenameUtils.separatorsToUnix(FilenameUtils.concat(path, file.getOriginalFilename())));
    fileEntryService.save(user, fileEntry);
}
Also used : FileEntry(org.ngrinder.script.model.FileEntry)

Example 2 with FileEntry

use of org.ngrinder.script.model.FileEntry in project Sermant by huaweicloud.

the class RestFileEntryController method getAllFiles.

private List<FileEntry> getAllFiles(User user, String path) {
    final String trimmedPath = StringUtils.trimToEmpty(path);
    List<FileEntry> files = newArrayList(filter(fileEntryService.getAll(user), new Predicate<FileEntry>() {

        @Override
        public boolean apply(@Nullable FileEntry input) {
            return input != null && trimPathSeparatorBothSides(getPath(input.getPath())).equals(trimmedPath);
        }
    }));
    for (FileEntry each : files) {
        each.setPath(removePrependedSlash(each.getPath()));
    }
    return files;
}
Also used : FileEntry(org.ngrinder.script.model.FileEntry) Nullable(javax.annotation.Nullable) Predicate(com.google.common.base.Predicate)

Example 3 with FileEntry

use of org.ngrinder.script.model.FileEntry in project Sermant by huaweicloud.

the class RestFileEntryController method addFolderApi.

/**
 * NEW
 * @param user
 * @param path
 * @param folderName
 * @return
 */
@RestAPI
@ResponseBody
@RequestMapping(value = "/api/new/folder", /**
 *params = "type=folder",*
 */
method = RequestMethod.POST)
public ResponseEntity<FileEntry> addFolderApi(User user, @RequestParam String path, @RequestParam("folderName") String folderName) {
    // "fileName"
    fileEntryService.addFolder(user, path, StringUtils.trimToEmpty(folderName), "");
    FileEntry folder = fileEntryService.getOne(user, path);
    return new ResponseEntity<FileEntry>(folder, HttpStatus.CREATED);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) FileEntry(org.ngrinder.script.model.FileEntry) RestAPI(org.ngrinder.common.controller.RestAPI) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with FileEntry

use of org.ngrinder.script.model.FileEntry in project Sermant by huaweicloud.

the class RestFileEntryController method download.

/**
 * Download file entry of given path.
 *
 * @param user     current user
 * @param path     user
 * @param response response
 */
@RequestMapping("/download")
public void download(User user, @RequestParam String path, HttpServletResponse response) {
    FileEntry fileEntry = fileEntryService.getOne(user, path);
    if (fileEntry == null) {
        LOG.error("{} requested to download not existing file entity {}", user.getUserId(), path);
        return;
    }
    response.reset();
    try {
        response.addHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(FilenameUtils.getName(fileEntry.getPath()), "utf8"));
    } catch (UnsupportedEncodingException e1) {
        LOG.error(e1.getMessage(), e1);
    }
    response.setContentType("application/octet-stream; charset=UTF-8");
    response.addHeader("Content-Length", "" + fileEntry.getFileSize());
    byte[] buffer = new byte[4096];
    ByteArrayInputStream fis = null;
    OutputStream toClient = null;
    try {
        fis = new ByteArrayInputStream(fileEntry.getContentBytes());
        toClient = new BufferedOutputStream(response.getOutputStream());
        int readLength;
        while (((readLength = fis.read(buffer)) != -1)) {
            toClient.write(buffer, 0, readLength);
        }
    } catch (IOException e) {
        throw processException("error while download file", e);
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(toClient);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FileEntry(org.ngrinder.script.model.FileEntry) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with FileEntry

use of org.ngrinder.script.model.FileEntry in project Sermant by huaweicloud.

the class RestFileEntryController method createFormApi.

@RestAPI
@ResponseBody
@RequestMapping(value = "/api/new/script", /**
 *params = "type=script",*
 */
method = RequestMethod.POST)
public HttpEntity<String> createFormApi(User user, @RequestParam String path, @RequestParam(value = "testUrl", required = false) String testUrl, @RequestParam("fileName") String fileName, @RequestParam(value = "scriptType", required = false) String scriptType, @RequestParam(value = "createLibAndResource", defaultValue = "false") boolean createLibAndResources, @RequestParam(value = "options", required = false) String options, RedirectAttributes redirectAttributes, ModelMap model) {
    fileName = StringUtils.trimToEmpty(fileName);
    String name = "Test1";
    if (StringUtils.isEmpty(testUrl)) {
        testUrl = StringUtils.defaultIfBlank(testUrl, "http://please_modify_this.com");
    } else {
        name = UrlUtils.getHost(testUrl);
    }
    ScriptHandler scriptHandler = fileEntryService.getScriptHandler(scriptType);
    FileEntry entry = new FileEntry();
    entry.setPath(fileName);
    if (scriptHandler instanceof ProjectHandler) {
        if (!fileEntryService.hasFileEntry(user, PathUtils.join(path, fileName))) {
            fileEntryService.prepareNewEntry(user, path, fileName, name, testUrl, scriptHandler, createLibAndResources, options);
            redirectAttributes.addFlashAttribute("message", fileName + " project is created.");
            return toJsonHttpEntity(model);
        } else {
            redirectAttributes.addFlashAttribute("exception", fileName + " is already existing. Please choose the different name");
            return toJsonHttpEntity(model);
        }
    } else {
        String fullPath = PathUtils.join(path, fileName);
        if (fileEntryService.hasFileEntry(user, fullPath)) {
            model.addAttribute("file", fileEntryService.getOne(user, fullPath));
        } else {
            model.addAttribute("file", fileEntryService.prepareNewEntry(user, path, fileName, name, testUrl, scriptHandler, createLibAndResources, options));
        }
    }
    model.addAttribute("breadcrumbPath", getScriptPathBreadcrumbs(PathUtils.join(path, fileName)));
    model.addAttribute("scriptHandler", scriptHandler);
    model.addAttribute("createLibAndResource", createLibAndResources);
    return toJsonHttpEntity(model);
}
Also used : FileEntry(org.ngrinder.script.model.FileEntry) ProjectHandler(org.ngrinder.script.handler.ProjectHandler) ScriptHandler(org.ngrinder.script.handler.ScriptHandler) RestAPI(org.ngrinder.common.controller.RestAPI) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

FileEntry (org.ngrinder.script.model.FileEntry)67 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)20 Test (org.junit.Test)13 AbstractNGrinderTransactionalTest (org.ngrinder.AbstractNGrinderTransactionalTest)12 ClassPathResource (org.springframework.core.io.ClassPathResource)11 File (java.io.File)9 RestAPI (org.ngrinder.common.controller.RestAPI)8 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)7 Predicate (com.google.common.base.Predicate)6 Nullable (javax.annotation.Nullable)6 ExceptionUtils.processException (org.ngrinder.common.util.ExceptionUtils.processException)6 ProjectHandler (org.ngrinder.script.handler.ProjectHandler)6 ScriptHandler (org.ngrinder.script.handler.ScriptHandler)6 JSONObject (com.alibaba.fastjson.JSONObject)5 IOException (java.io.IOException)5 MultipartFile (org.springframework.web.multipart.MultipartFile)5 URL (java.net.URL)4 MutableInt (org.apache.commons.lang.mutable.MutableInt)4 ModelMap (org.springframework.ui.ModelMap)4 RedirectAttributesModelMap (org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap)4