diff --git a/src/main/java/org/codelibs/fess/app/web/admin/log/AdminLogAction.java b/src/main/java/org/codelibs/fess/app/web/admin/log/AdminLogAction.java index c258aedd2..297f7c433 100644 --- a/src/main/java/org/codelibs/fess/app/web/admin/log/AdminLogAction.java +++ b/src/main/java/org/codelibs/fess/app/web/admin/log/AdminLogAction.java @@ -15,18 +15,33 @@ */ package org.codelibs.fess.app.web.admin.log; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Base64; +import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Stream; import javax.annotation.Resource; +import org.codelibs.core.lang.StringUtil; import org.codelibs.fess.app.web.base.FessAdminAction; +import org.codelibs.fess.exception.FessSystemException; import org.codelibs.fess.helper.SystemHelper; +import org.lastaflute.di.exception.IORuntimeException; import org.lastaflute.web.Execute; import org.lastaflute.web.callback.ActionRuntime; +import org.lastaflute.web.response.ActionResponse; import org.lastaflute.web.response.HtmlResponse; +import com.google.common.base.Charsets; + /** * @author codelibs * @author Keiichi Watanabe @@ -43,46 +58,57 @@ public class AdminLogAction extends FessAdminAction { } @Execute - public HtmlResponse index(final LogForm form) { + public HtmlResponse index() { + return toIndexPage(); + } + + @Execute + public ActionResponse download(final String id) { + String filename = new String(Base64.getDecoder().decode(id), Charsets.UTF_8).replace("..", "").replaceAll("\\s", ""); + final String logFilePath = systemHelper.getLogFilePath(); + if (StringUtil.isNotBlank(logFilePath)) { + Path path = Paths.get(logFilePath, filename); + return asStream(filename).contentType("text/plain; charset=UTF-8").stream(out -> { + try (InputStream in = Files.newInputStream(path)) { + out.write(in); + } + }); + } + throwValidationError(messages -> messages.addErrorsCouldNotFindLogFile(GLOBAL, filename), () -> { + return toIndexPage(); + }); + return redirect(getClass()); + } + + public List> getLogFileItems() { + final List> logFileItems = new ArrayList>(); + final String logFilePath = systemHelper.getLogFilePath(); + if (StringUtil.isNotBlank(logFilePath)) { + Path logDirPath = Paths.get(logFilePath); + try (Stream stream = Files.list(logDirPath)) { + stream.filter(entry -> entry.getFileName().toString().endsWith(".log")).forEach(filePath -> { + Map map = new HashMap<>(); + String name = filePath.getFileName().toString(); + map.put("id", Base64.getEncoder().encodeToString(name.getBytes(Charsets.UTF_8))); + map.put("name", name); + try { + map.put("lastModified", new Date(Files.getLastModifiedTime(filePath).toMillis())); + } catch (IOException e) { + throw new IORuntimeException(e); + } + logFileItems.add(map); + }); + } catch (Exception e) { + throw new FessSystemException("Failed to access log files.", e); + } + } + return logFileItems; + } + + private HtmlResponse toIndexPage() { return asHtml(path_AdminLog_IndexJsp).renderWith(data -> { data.register("logFileItems", getLogFileItems()); }); } - //@Execute(validator = true, input = "index", urlPattern = "download/{logFileName}") - public HtmlResponse download(final LogForm form) { - // TODO - return redirect(getClass()); - /* - final String logFilePath = ComponentUtil.getSystemHelper().getLogFilePath(); - if (StringUtil.isNotBlank(logFilePath)) { - final File file = new File(logFilePath); - final File parentDir = file.getParentFile(); - String fileName; - try { - fileName = new String(Base64.decodeBase64(logForm.logFileName.getBytes(Constants.UTF_8)), Constants.UTF_8); - } catch (final UnsupportedEncodingException e1) { - fileName = - new String(Base64.decodeBase64(logForm.logFileName.getBytes(Charset.defaultCharset())), Charset.defaultCharset()); - } - final File logFile = new File(parentDir, fileName); - if (logFile.isFile()) { - try { - LaResponseUtil.download(fileName, new FileInputStream(logFile)); - return null; - } catch (final FileNotFoundException e) { - logger.warn("Could not find " + logFile.getAbsolutePath(), e); - } - } - } - throw new SSCActionMessagesException("errors.could_not_find_log_file", new Object[] { logForm.logFileName }); - */ - } - - public List> getLogFileItems() { - // TODO - final List> logFileItems = new ArrayList>(); - return logFileItems; - } - } diff --git a/src/main/java/org/codelibs/fess/app/web/admin/log/LogForm.java b/src/main/java/org/codelibs/fess/app/web/admin/log/LogForm.java deleted file mode 100644 index 8f41add0c..000000000 --- a/src/main/java/org/codelibs/fess/app/web/admin/log/LogForm.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2012-2015 CodeLibs Project and the Others. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, - * either express or implied. See the License for the specific language - * governing permissions and limitations under the License. - */ -package org.codelibs.fess.app.web.admin.log; - -import java.io.Serializable; - -/** - * @author codelibs - * @author Keiichi Watanabe - */ -public class LogForm implements Serializable { - - private static final long serialVersionUID = 1L; - - public String logFileName; -} diff --git a/src/main/java/org/codelibs/fess/helper/SystemHelper.java b/src/main/java/org/codelibs/fess/helper/SystemHelper.java index 967a3944f..e1c6471b0 100644 --- a/src/main/java/org/codelibs/fess/helper/SystemHelper.java +++ b/src/main/java/org/codelibs/fess/helper/SystemHelper.java @@ -15,6 +15,7 @@ */ package org.codelibs.fess.helper; +import java.io.File; import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; @@ -60,8 +61,6 @@ public class SystemHelper implements Serializable { "-XX:CompressedClassSpaceSize=32m", "-XX:-UseGCOverheadLimit", "-XX:+UseConcMarkSweepGC", "-XX:CMSInitiatingOccupancyFraction=75", "-XX:+UseParNewGC", "-XX:+UseTLAB", "-XX:+DisableExplicitGC" }; - private String logFilePath = System.getProperty("fess.log.path", "target/logs"); - private String javaCommandPath = "java"; private String filterPathEncoding = Constants.UTF_8; @@ -133,11 +132,14 @@ public class SystemHelper implements Serializable { } public String getLogFilePath() { - return logFilePath; - } - - public void setLogFilePath(final String logFilePath) { - this.logFilePath = logFilePath; + final String value = System.getProperty("fess.log.path"); + if (value != null) { + return value; + } else { + final String userDir = System.getProperty("user.dir"); + final File targetDir = new File(userDir, "target"); + return new File(targetDir, "logs").getAbsolutePath(); + } } public String encodeUrlFilter(final String path) { diff --git a/src/main/java/org/codelibs/fess/job/CrawlJob.java b/src/main/java/org/codelibs/fess/job/CrawlJob.java index 0cf54c192..27ee3bbb0 100644 --- a/src/main/java/org/codelibs/fess/job/CrawlJob.java +++ b/src/main/java/org/codelibs/fess/job/CrawlJob.java @@ -269,11 +269,7 @@ public class CrawlJob { } crawlerCmdList.add("-Dfess.crawler.process=true"); - if (logFilePath == null) { - final String value = System.getProperty("fess.log.path"); - logFilePath = value != null ? value : new File(targetDir, "logs").getAbsolutePath(); - } - crawlerCmdList.add("-Dfess.log.path=" + logFilePath); + crawlerCmdList.add("-Dfess.log.path=" + (logFilePath != null ? logFilePath : systemHelper.getLogFilePath())); addSystemProperty(crawlerCmdList, "lasta.env", null, null); addSystemProperty(crawlerCmdList, "fess.log.name", "fess-crawler", "-crawler"); addSystemProperty(crawlerCmdList, "fess.log.level", null, null); diff --git a/src/main/resources/fess.xml b/src/main/resources/fess.xml index 184a89f9a..88025bebd 100644 --- a/src/main/resources/fess.xml +++ b/src/main/resources/fess.xml @@ -33,11 +33,6 @@ "hi", "hu", "hy", "id", "it", "ja", "lv", "ko", "nl", "no", "pt", "ro", "ru", "sv", "th", "tr", "zh_CN", "zh_TW", "zh" } --> - - java.lang.System.getProperty("fess.log.file") == null ? - java.lang.System.getProperty("java.io.tmpdir") + "/fess_crawler.out" : - java.lang.System.getProperty("fess.log.file").replaceAll(".out", "_crawler.out") - [ "-Djava.awt.headless=true", diff --git a/src/main/webapp/WEB-INF/view/admin/log/index.jsp b/src/main/webapp/WEB-INF/view/admin/log/index.jsp index d5cccb470..d3c63d431 100644 --- a/src/main/webapp/WEB-INF/view/admin/log/index.jsp +++ b/src/main/webapp/WEB-INF/view/admin/log/index.jsp @@ -57,7 +57,7 @@ - ${f:h(logFile.name)} + ${f:h(logFile.name)}