View Javadoc
1   /*
2    * Copyright 2012-2021 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   * @author Keiichi Watanabe
39   */
40  public class ApiAdminLogAction extends FessApiAdminAction {
41  
42      // ===================================================================================
43      //                                                                      Search Execute
44      //                                                                      ==============
45  
46      // GET /api/admin/log/files
47      @Execute
48      public JsonResponse<ApiResult> files() {
49          final List<Map<String, Object>> list = getLogFileItems();
50          return asJson(new ApiResult.ApiLogFilesResponse().files(list).total(list.size()).status(ApiResult.Status.OK).result());
51      }
52  
53      // GET /api/admin/log/file/{id}
54      @Execute
55      public StreamResponse get$file(final String id) {
56          final String filename = new String(Base64.getDecoder().decode(id), StandardCharsets.UTF_8).replace("..", "").replaceAll("\\s", "");
57          final String logFilePath = systemHelper.getLogFilePath();
58          if (StringUtil.isNotBlank(logFilePath) && isLogFilename(filename)) {
59              final Path path = Paths.get(logFilePath, filename);
60              return asStream(filename).contentTypeOctetStream().stream(out -> {
61                  try (InputStream in = Files.newInputStream(path)) {
62                      out.write(in);
63                  }
64              });
65          }
66          return StreamResponse.asEmptyBody();
67      }
68  }