View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.app.web.api.admin.log;
17  
18  import static org.codelibs.fess.app.web.admin.log.AdminLogAction.getLogFileItems;
19  import static org.codelibs.fess.app.web.admin.log.AdminLogAction.isLogFilename;
20  
21  import java.io.InputStream;
22  import java.nio.charset.StandardCharsets;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.nio.file.Paths;
26  import java.util.Base64;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.codelibs.core.lang.StringUtil;
31  import org.codelibs.fess.app.web.api.ApiResult;
32  import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
33  import org.lastaflute.web.Execute;
34  import org.lastaflute.web.response.JsonResponse;
35  import org.lastaflute.web.response.StreamResponse;
36  
37  /**
38   * API action for admin log management.
39   *
40   */
41  public class ApiAdminLogAction extends FessApiAdminAction {
42  
43      // ===================================================================================
44      //                                                                         Constructor
45      //                                                                         ===========
46      /**
47       * Default constructor.
48       */
49      public ApiAdminLogAction() {
50          super();
51      }
52  
53      // ===================================================================================
54      //                                                                      Search Execute
55      //                                                                      ==============
56  
57      /**
58       * Retrieves the list of available log files.
59       *
60       * @return JSON response containing log file list
61       */
62      // GET /api/admin/log/files
63      @Execute
64      public JsonResponse<ApiResult> files() {
65          final List<Map<String, Object>> list = getLogFileItems();
66          return asJson(new ApiResult.ApiLogFilesResponse().files(list).total(list.size()).status(ApiResult.Status.OK).result());
67      }
68  
69      /**
70       * Downloads a specific log file by ID.
71       *
72       * @param id the base64-encoded filename of the log file to download
73       * @return stream response containing the log file content
74       */
75      // GET /api/admin/log/file/{id}
76      @Execute
77      public StreamResponse get$file(final String id) {
78          final String filename = new String(Base64.getDecoder().decode(id), StandardCharsets.UTF_8).replace("..", "").replaceAll("\\s", "");
79          final String logFilePath = systemHelper.getLogFilePath();
80          if (StringUtil.isNotBlank(logFilePath) && isLogFilename(filename)) {
81              final Path path = Paths.get(logFilePath, filename);
82              return asStream(filename).contentTypeOctetStream().stream(out -> {
83                  try (InputStream in = Files.newInputStream(path)) {
84                      out.write(in);
85                  }
86              });
87          }
88          return StreamResponse.asEmptyBody();
89      }
90  }