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