View Javadoc
1   /*
2    * Copyright 2012-2019 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.backup;
17  
18  import static org.codelibs.core.stream.StreamUtil.stream;
19  import static org.codelibs.fess.app.web.admin.backup.AdminBackupAction.NDJSON_EXTENTION;
20  import static org.codelibs.fess.app.web.admin.backup.AdminBackupAction.getBackupItems;
21  import static org.codelibs.fess.app.web.admin.backup.AdminBackupAction.getClickLogNdjsonWriteCall;
22  import static org.codelibs.fess.app.web.admin.backup.AdminBackupAction.getFavoriteLogNdjsonWriteCall;
23  import static org.codelibs.fess.app.web.admin.backup.AdminBackupAction.getSearchLogNdjsonWriteCall;
24  import static org.codelibs.fess.app.web.admin.backup.AdminBackupAction.getUserInfoNdjsonWriteCall;
25  
26  import java.io.BufferedWriter;
27  import java.io.ByteArrayInputStream;
28  import java.io.ByteArrayOutputStream;
29  import java.io.InputStream;
30  import java.io.OutputStreamWriter;
31  import java.io.Writer;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.function.Consumer;
35  
36  import org.codelibs.curl.CurlResponse;
37  import org.codelibs.fess.Constants;
38  import org.codelibs.fess.app.web.api.ApiResult;
39  import org.codelibs.fess.app.web.api.ApiResult.ApiBackupFilesResponse;
40  import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
41  import org.codelibs.fess.mylasta.direction.FessConfig;
42  import org.codelibs.fess.util.ComponentUtil;
43  import org.lastaflute.web.Execute;
44  import org.lastaflute.web.response.JsonResponse;
45  import org.lastaflute.web.response.StreamResponse;
46  
47  /**
48   * @author Keiichi Watanabe
49   */
50  public class ApiAdminBackupAction extends FessApiAdminAction {
51  
52      // GET /api/admin/backup/files
53      @Execute
54      public JsonResponse<ApiResult> files() {
55          final List<Map<String, String>> list = getBackupItems();
56          return asJson(new ApiBackupFilesResponse().files(list).total(list.size()).status(ApiResult.Status.OK).result());
57      }
58  
59      // GET /api/admin/backup/file/{id}
60      @Execute
61      public StreamResponse get$file(final String id) {
62          final FessConfig fessConfig = ComponentUtil.getFessConfig();
63          if (stream(fessConfig.getIndexBackupAllTargets()).get(stream -> stream.anyMatch(s -> s.equals(id)))) {
64              if (id.equals("system.properties")) {
65                  return asStream(id).contentTypeOctetStream().stream(out -> {
66                      try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
67                          ComponentUtil.getSystemProperties().store(baos, id);
68                          try (final InputStream in = new ByteArrayInputStream(baos.toByteArray())) {
69                              out.write(in);
70                          }
71                      }
72                  });
73              } else if (id.endsWith(NDJSON_EXTENTION)) {
74                  final String name = id.substring(0, id.length() - NDJSON_EXTENTION.length());
75                  if ("search_log".equals(name)) {
76                      return writeNdjsonResponse(id, getSearchLogNdjsonWriteCall());
77                  } else if ("user_info".equals(name)) {
78                      return writeNdjsonResponse(id, getUserInfoNdjsonWriteCall());
79                  } else if ("click_log".equals(name)) {
80                      return writeNdjsonResponse(id, getClickLogNdjsonWriteCall());
81                  } else if ("favorite_log".equals(name)) {
82                      return writeNdjsonResponse(id, getFavoriteLogNdjsonWriteCall());
83                  }
84              } else {
85                  final String index;
86                  final String filename;
87                  if (id.endsWith(".bulk")) {
88                      index = id.substring(0, id.length() - 5);
89                      filename = id;
90                  } else {
91                      index = id;
92                      filename = id + ".bulk";
93                  }
94                  return asStream(filename).contentTypeOctetStream().stream(
95                          out -> {
96                              try (CurlResponse response =
97                                      ComponentUtil.getCurlHelper().get("/" + index + "/_data").param("format", "json").execute()) {
98                                  out.write(response.getContentAsStream());
99                              }
100                         });
101             }
102         }
103 
104         throwValidationErrorApi(messages -> messages.addErrorsCouldNotFindBackupIndex(GLOBAL));
105         return StreamResponse.asEmptyBody(); // no-op
106     }
107 
108     private StreamResponse writeNdjsonResponse(final String id, final Consumer<Writer> writeCall) {
109         return asStream(id)//
110                 .header("Pragma", "no-cache")//
111                 .header("Cache-Control", "no-cache")//
112                 .header("Expires", "Thu, 01 Dec 1994 16:00:00 GMT")//
113                 .header("Content-Type", "application/x-ndjson")//
114                 .stream(out -> {
115                     try (final Writer writer = new BufferedWriter(new OutputStreamWriter(out.stream(), Constants.CHARSET_UTF_8))) {
116                         writeCall.accept(writer);
117                         writer.flush();
118                     }
119                 });
120     }
121 }