Search in sources :

Example 1 with DomContent

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);
        }
    }
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) StringUtils(org.apache.commons.lang.StringUtils) SortedSet(java.util.SortedSet) LogviewerConstant(org.apache.storm.daemon.logviewer.LogviewerConstant) TagCreator.h3(j2html.TagCreator.h3) TagCreator.head(j2html.TagCreator.head) Matcher(java.util.regex.Matcher) TagCreator.link(j2html.TagCreator.link) Map(java.util.Map) ExceptionMeterNames(org.apache.storm.daemon.logviewer.utils.ExceptionMeterNames) UIHelpers(org.apache.storm.daemon.ui.UIHelpers) TagCreator.input(j2html.TagCreator.input) Path(java.nio.file.Path) UrlBuilder(org.apache.storm.daemon.utils.UrlBuilder) Unchecked(org.jooq.lambda.Unchecked) LogviewerResponseBuilder(org.apache.storm.daemon.logviewer.utils.LogviewerResponseBuilder) FileNotFoundException(java.io.FileNotFoundException) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) ConfigUtils(org.apache.storm.utils.ConfigUtils) Response(javax.ws.rs.core.Response) TagCreator.title(j2html.TagCreator.title) Pattern(java.util.regex.Pattern) InvalidRequestException(org.apache.storm.daemon.ui.InvalidRequestException) TagCreator.text(j2html.TagCreator.text) TagCreator.body(j2html.TagCreator.body) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TagCreator.html(j2html.TagCreator.html) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) TagCreator.pre(j2html.TagCreator.pre) ArrayList(java.util.ArrayList) TagCreator.option(j2html.TagCreator.option) Collectors.toCollection(java.util.stream.Collectors.toCollection) Meter(com.codahale.metrics.Meter) DirectoryCleaner(org.apache.storm.daemon.logviewer.utils.DirectoryCleaner) ServerUtils(org.apache.storm.utils.ServerUtils) ResourceAuthorizer(org.apache.storm.daemon.logviewer.utils.ResourceAuthorizer) StormMetricsRegistry(org.apache.storm.metric.StormMetricsRegistry) TagCreator.p(j2html.TagCreator.p) Files(java.nio.file.Files) TagCreator.select(j2html.TagCreator.select) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TagCreator.form(j2html.TagCreator.form) TagCreator.a(j2html.TagCreator.a) WorkerLogs(org.apache.storm.daemon.logviewer.utils.WorkerLogs) File(java.io.File) StreamUtil(org.apache.storm.daemon.utils.StreamUtil) DomContent(j2html.tags.DomContent) Collectors.toList(java.util.stream.Collectors.toList) StringEscapeUtils.escapeHtml(org.apache.commons.lang.StringEscapeUtils.escapeHtml) Paths(java.nio.file.Paths) TagCreator.div(j2html.TagCreator.div) Collections(java.util.Collections) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) DomContent(j2html.tags.DomContent) UncheckedIOException(java.io.UncheckedIOException) WorkerLogs(org.apache.storm.daemon.logviewer.utils.WorkerLogs) TreeSet(java.util.TreeSet) Files(java.nio.file.Files)

Example 2 with DomContent

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();
    }
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) StringUtils(org.apache.commons.lang.StringUtils) SortedSet(java.util.SortedSet) LogviewerConstant(org.apache.storm.daemon.logviewer.LogviewerConstant) TagCreator.h3(j2html.TagCreator.h3) TagCreator.head(j2html.TagCreator.head) Matcher(java.util.regex.Matcher) TagCreator.link(j2html.TagCreator.link) Map(java.util.Map) ExceptionMeterNames(org.apache.storm.daemon.logviewer.utils.ExceptionMeterNames) UIHelpers(org.apache.storm.daemon.ui.UIHelpers) TagCreator.input(j2html.TagCreator.input) Path(java.nio.file.Path) UrlBuilder(org.apache.storm.daemon.utils.UrlBuilder) Unchecked(org.jooq.lambda.Unchecked) LogviewerResponseBuilder(org.apache.storm.daemon.logviewer.utils.LogviewerResponseBuilder) FileNotFoundException(java.io.FileNotFoundException) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) ConfigUtils(org.apache.storm.utils.ConfigUtils) Response(javax.ws.rs.core.Response) TagCreator.title(j2html.TagCreator.title) Pattern(java.util.regex.Pattern) InvalidRequestException(org.apache.storm.daemon.ui.InvalidRequestException) TagCreator.text(j2html.TagCreator.text) TagCreator.body(j2html.TagCreator.body) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TagCreator.html(j2html.TagCreator.html) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) TagCreator.pre(j2html.TagCreator.pre) ArrayList(java.util.ArrayList) TagCreator.option(j2html.TagCreator.option) Collectors.toCollection(java.util.stream.Collectors.toCollection) Meter(com.codahale.metrics.Meter) DirectoryCleaner(org.apache.storm.daemon.logviewer.utils.DirectoryCleaner) ServerUtils(org.apache.storm.utils.ServerUtils) ResourceAuthorizer(org.apache.storm.daemon.logviewer.utils.ResourceAuthorizer) StormMetricsRegistry(org.apache.storm.metric.StormMetricsRegistry) TagCreator.p(j2html.TagCreator.p) Files(java.nio.file.Files) TagCreator.select(j2html.TagCreator.select) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TagCreator.form(j2html.TagCreator.form) TagCreator.a(j2html.TagCreator.a) WorkerLogs(org.apache.storm.daemon.logviewer.utils.WorkerLogs) File(java.io.File) StreamUtil(org.apache.storm.daemon.utils.StreamUtil) DomContent(j2html.tags.DomContent) Collectors.toList(java.util.stream.Collectors.toList) StringEscapeUtils.escapeHtml(org.apache.commons.lang.StringEscapeUtils.escapeHtml) Paths(java.nio.file.Paths) TagCreator.div(j2html.TagCreator.div) Collections(java.util.Collections) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) DomContent(j2html.tags.DomContent) File(java.io.File)

Aggregations

Meter (com.codahale.metrics.Meter)2 TagCreator.a (j2html.TagCreator.a)2 TagCreator.body (j2html.TagCreator.body)2 TagCreator.div (j2html.TagCreator.div)2 TagCreator.form (j2html.TagCreator.form)2 TagCreator.h3 (j2html.TagCreator.h3)2 TagCreator.head (j2html.TagCreator.head)2 TagCreator.html (j2html.TagCreator.html)2 TagCreator.input (j2html.TagCreator.input)2 TagCreator.link (j2html.TagCreator.link)2 TagCreator.option (j2html.TagCreator.option)2 TagCreator.p (j2html.TagCreator.p)2 TagCreator.pre (j2html.TagCreator.pre)2 TagCreator.select (j2html.TagCreator.select)2 TagCreator.text (j2html.TagCreator.text)2 TagCreator.title (j2html.TagCreator.title)2 DomContent (j2html.tags.DomContent)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2