use of j2html.tags.DomContent in project storm by apache.
the class LogviewerLogPageHandler method logPage.
/**
* Provides a worker log file to view, starting from the specified position
* or default starting position of the most recent page.
*
* @param fileName file to view
* @param start start offset, or null if the most recent page is desired
* @param length length to read in this page, or null if default page length is desired
* @param grep search string if request is a result of the search, can be null
* @param user username
* @return HTML view page of worker log
*/
public Response logPage(String fileName, Integer start, Integer length, String grep, String user) throws IOException, InvalidRequestException {
Path rawFile = logRoot.resolve(fileName);
Path absFile = rawFile.toAbsolutePath().normalize();
if (!absFile.startsWith(logRoot) || !rawFile.normalize().toString().equals(rawFile.toString())) {
// Ensure filename doesn't contain ../ parts
return LogviewerResponseBuilder.buildResponsePageNotFound();
}
if (resourceAuthorizer.isUserAllowedToAccessFile(user, fileName)) {
workerLogs.setLogFilePermission(fileName);
Path topoDir = absFile.getParent().getParent();
if (absFile.toFile().exists()) {
SortedSet<Path> logFiles;
try {
logFiles = Arrays.stream(topoDir.toFile().listFiles()).flatMap(Unchecked.function(portDir -> directoryCleaner.getFilesForDir(portDir.toPath()).stream())).filter(Files::isRegularFile).collect(toCollection(TreeSet::new));
} catch (UncheckedIOException e) {
throw e.getCause();
}
List<String> reorderedFilesStr = logFiles.stream().map(WorkerLogs::getTopologyPortWorkerLog).filter(fileStr -> !StringUtils.equals(fileName, fileStr)).collect(toList());
reorderedFilesStr.add(fileName);
length = length != null ? Math.min(10485760, length) : LogviewerConstant.DEFAULT_BYTES_PER_PAGE;
final boolean isZipFile = absFile.getFileName().toString().endsWith(".gz");
long fileLength = getFileLength(absFile.toFile(), isZipFile);
if (start == null) {
start = Long.valueOf(fileLength - length).intValue();
}
String logString = isTxtFile(fileName) ? escapeHtml(pageFile(absFile.toString(), isZipFile, fileLength, start, length)) : escapeHtml("This is a binary file and cannot display! You may download the full file.");
List<DomContent> bodyContents = new ArrayList<>();
if (StringUtils.isNotEmpty(grep)) {
String matchedString = String.join("\n", Arrays.stream(logString.split("\n")).filter(str -> str.contains(grep)).collect(toList()));
bodyContents.add(pre(matchedString).withId("logContent"));
} else {
DomContent pagerData = null;
if (isTxtFile(fileName)) {
pagerData = pagerLinks(fileName, start, length, Long.valueOf(fileLength).intValue(), "log");
}
bodyContents.add(searchFileForm(fileName, "no"));
// list all files for this topology
bodyContents.add(logFileSelectionForm(reorderedFilesStr, fileName, "log"));
if (pagerData != null) {
bodyContents.add(pagerData);
}
bodyContents.add(downloadLink(fileName));
bodyContents.add(pre(logString).withClass("logContent"));
if (pagerData != null) {
bodyContents.add(pagerData);
}
}
String content = logTemplate(bodyContents, fileName, user).render();
return LogviewerResponseBuilder.buildSuccessHtmlResponse(content);
} else {
return LogviewerResponseBuilder.buildResponsePageNotFound();
}
} else {
if (resourceAuthorizer.getLogUserGroupWhitelist(fileName) == null) {
return LogviewerResponseBuilder.buildResponsePageNotFound();
} else {
return LogviewerResponseBuilder.buildResponseUnauthorizedUser(user);
}
}
}
use of j2html.tags.DomContent in project storm by apache.
the class LogviewerLogPageHandler method daemonLogPage.
/**
* Provides a daemon log file to view.
*
* @param fileName file to view
* @param start start offset, or null if the most recent page is desired
* @param length length to read in this page, or null if default page length is desired
* @param grep search string if request is a result of the search, can be null
* @param user username
* @return HTML view page of daemon log
*/
public Response daemonLogPage(String fileName, Integer start, Integer length, String grep, String user) throws IOException, InvalidRequestException {
Path file = daemonLogRoot.resolve(fileName).toAbsolutePath().normalize();
if (!file.startsWith(daemonLogRoot) || Paths.get(fileName).getNameCount() != 1) {
// Prevent fileName from pathing into worker logs, or outside daemon log root
return LogviewerResponseBuilder.buildResponsePageNotFound();
}
if (file.toFile().exists()) {
// all types of files included
List<File> logFiles = Arrays.stream(daemonLogRoot.toFile().listFiles()).filter(File::isFile).collect(toList());
List<String> reorderedFilesStr = logFiles.stream().map(File::getName).filter(fName -> !StringUtils.equals(fileName, fName)).collect(toList());
reorderedFilesStr.add(fileName);
length = length != null ? Math.min(10485760, length) : LogviewerConstant.DEFAULT_BYTES_PER_PAGE;
final boolean isZipFile = file.getFileName().toString().endsWith(".gz");
long fileLength = getFileLength(file.toFile(), isZipFile);
if (start == null) {
start = Long.valueOf(fileLength - length).intValue();
}
String logString = isTxtFile(fileName) ? escapeHtml(pageFile(file.toString(), isZipFile, fileLength, start, length)) : escapeHtml("This is a binary file and cannot display! You may download the full file.");
List<DomContent> bodyContents = new ArrayList<>();
if (StringUtils.isNotEmpty(grep)) {
String matchedString = String.join("\n", Arrays.stream(logString.split("\n")).filter(str -> str.contains(grep)).collect(toList()));
bodyContents.add(pre(matchedString).withId("logContent"));
} else {
DomContent pagerData = null;
if (isTxtFile(fileName)) {
pagerData = pagerLinks(fileName, start, length, Long.valueOf(fileLength).intValue(), "daemonlog");
}
bodyContents.add(searchFileForm(fileName, "yes"));
// list all daemon logs
bodyContents.add(logFileSelectionForm(reorderedFilesStr, fileName, "daemonlog"));
if (pagerData != null) {
bodyContents.add(pagerData);
}
bodyContents.add(daemonDownloadLink(fileName));
bodyContents.add(pre(logString).withClass("logContent"));
if (pagerData != null) {
bodyContents.add(pagerData);
}
}
String content = logTemplate(bodyContents, fileName, user).render();
return LogviewerResponseBuilder.buildSuccessHtmlResponse(content);
} else {
return LogviewerResponseBuilder.buildResponsePageNotFound();
}
}
Aggregations