View Javadoc
1   /*
2    * Copyright 2012-2017 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  
20  import java.io.InputStream;
21  import java.nio.charset.StandardCharsets;
22  import java.nio.file.Files;
23  import java.nio.file.Path;
24  import java.nio.file.Paths;
25  import java.util.Base64;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.codelibs.core.lang.StringUtil;
30  import org.codelibs.fess.app.web.api.ApiResult;
31  import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
32  import org.lastaflute.web.Execute;
33  import org.lastaflute.web.response.JsonResponse;
34  import org.lastaflute.web.response.StreamResponse;
35  
36  /**
37   * @author Keiichi Watanabe
38   */
39  public class ApiAdminLogAction extends FessApiAdminAction {
40  
41      // ===================================================================================
42      //                                                                      Search Execute
43      //                                                                      ==============
44  
45      // GET /api/admin/log/files
46      @Execute
47      public JsonResponse<ApiResult> files() {
48          final List<Map<String, Object>> list = getLogFileItems();
49          return asJson(new ApiResult.ApiLogFilesResponse().files(list).total(list.size()).status(ApiResult.Status.OK).result());
50      }
51  
52      // GET /api/admin/log/file/{id}
53      @Execute
54      public StreamResponse get$file(final String id) {
55          final String filename = new String(Base64.getDecoder().decode(id), StandardCharsets.UTF_8).replace("..", "").replaceAll("\\s", "");
56          final String logFilePath = systemHelper.getLogFilePath();
57          if (StringUtil.isNotBlank(logFilePath)) {
58              final Path path = Paths.get(logFilePath, filename);
59              return asStream(filename).contentTypeOctetStream().stream(out -> {
60                  try (InputStream in = Files.newInputStream(path)) {
61                      out.write(in);
62                  }
63              });
64          }
65          return StreamResponse.asEmptyBody();
66      }
67  }