1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.app.web.admin.log;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.nio.charset.StandardCharsets;
21 import java.nio.file.Files;
22 import java.nio.file.Path;
23 import java.nio.file.Paths;
24 import java.util.ArrayList;
25 import java.util.Base64;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.stream.Stream;
31
32 import org.codelibs.core.lang.StringUtil;
33 import org.codelibs.fess.app.web.base.FessAdminAction;
34 import org.codelibs.fess.exception.FessSystemException;
35 import org.codelibs.fess.helper.SystemHelper;
36 import org.codelibs.fess.util.ComponentUtil;
37 import org.codelibs.fess.util.RenderDataUtil;
38 import org.lastaflute.di.exception.IORuntimeException;
39 import org.lastaflute.web.Execute;
40 import org.lastaflute.web.response.ActionResponse;
41 import org.lastaflute.web.response.HtmlResponse;
42 import org.lastaflute.web.ruts.process.ActionRuntime;
43
44
45
46
47
48 public class AdminLogAction extends FessAdminAction {
49
50 @Override
51 protected void setupHtmlData(final ActionRuntime runtime) {
52 super.setupHtmlData(runtime);
53 runtime.registerData("helpLink", systemHelper.getHelpLink(fessConfig.getOnlineHelpNameLog()));
54 }
55
56 @Execute
57 public HtmlResponse index() {
58 return asIndexHtml();
59 }
60
61 @Execute
62 public ActionResponse download(final String id) {
63 final String filename = new String(Base64.getDecoder().decode(id), StandardCharsets.UTF_8).replace("..", "").replaceAll("\\s", "");
64 final String logFilePath = systemHelper.getLogFilePath();
65 if (StringUtil.isNotBlank(logFilePath)) {
66 final Path path = Paths.get(logFilePath, filename);
67 return asStream(filename).contentTypeOctetStream().stream(out -> {
68 try (InputStream in = Files.newInputStream(path)) {
69 out.write(in);
70 }
71 });
72 }
73 throwValidationError(messages -> messages.addErrorsCouldNotFindLogFile(GLOBAL, filename), () -> {
74 return asIndexHtml();
75 });
76 return redirect(getClass());
77 }
78
79 public static List<Map<String, Object>> getLogFileItems() {
80 final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
81 final List<Map<String, Object>> logFileItems = new ArrayList<>();
82 final String logFilePath = systemHelper.getLogFilePath();
83 if (StringUtil.isNotBlank(logFilePath)) {
84 final Path logDirPath = Paths.get(logFilePath);
85 try (Stream<Path> stream = Files.list(logDirPath)) {
86 stream.filter(entry -> entry.getFileName().toString().endsWith(".log")).forEach(filePath -> {
87 final Map<String, Object> map = new HashMap<>();
88 final String name = filePath.getFileName().toString();
89 map.put("id", Base64.getUrlEncoder().encodeToString(name.getBytes(StandardCharsets.UTF_8)));
90 map.put("name", name);
91 try {
92 map.put("lastModified", new Date(Files.getLastModifiedTime(filePath).toMillis()));
93 } catch (final IOException e) {
94 throw new IORuntimeException(e);
95 }
96 logFileItems.add(map);
97 });
98 } catch (final Exception e) {
99 throw new FessSystemException("Failed to access log files.", e);
100 }
101 }
102 return logFileItems;
103 }
104
105 private HtmlResponse asIndexHtml() {
106 return asHtml(path_AdminLog_AdminLogJsp).renderWith(data -> {
107 RenderDataUtil.register(data, "logFileItems", getLogFileItems());
108 });
109 }
110
111 }