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.dict;
17  
18  import java.util.stream.Collectors;
19  import java.util.stream.Stream;
20  
21  import org.codelibs.fess.app.web.api.ApiResult;
22  import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
23  import org.codelibs.fess.dict.DictionaryFile;
24  import org.codelibs.fess.dict.DictionaryItem;
25  import org.codelibs.fess.dict.DictionaryManager;
26  import org.lastaflute.web.Execute;
27  import org.lastaflute.web.response.JsonResponse;
28  
29  import jakarta.annotation.Resource;
30  
31  /**
32   * API action for admin dictionary management.
33   * Provides REST endpoints for managing dictionaries in the Fess search engine.
34   */
35  public class ApiAdminDictAction extends FessApiAdminAction {
36  
37      /**
38       * Default constructor.
39       */
40      public ApiAdminDictAction() {
41          super();
42      }
43  
44      /** Dictionary manager for handling dictionary file operations */
45      @Resource
46      protected DictionaryManager dictionaryManager;
47  
48      /**
49       * Retrieves all available dictionary files.
50       *
51       * @return JSON response containing list of dictionary files
52       */
53      // GET /api/admin/dict
54      @Execute
55      public JsonResponse<ApiResult> get$index() {
56          final DictionaryFile<? extends DictionaryItem>[] dictFiles = dictionaryManager.getDictionaryFiles();
57          return asJson(new ApiResult.ApiConfigsResponse<ListBody>()
58                  .settings(Stream.of(dictFiles).map(this::createListBody).collect(Collectors.toList()))
59                  .status(ApiResult.Status.OK)
60                  .result());
61      }
62  
63      /**
64       * Creates a ListBody from a DictionaryFile for API responses.
65       *
66       * @param dictionaryFile the dictionary file to convert
67       * @return the converted ListBody object
68       */
69      protected ListBody createListBody(final DictionaryFile<? extends DictionaryItem> dictionaryFile) {
70          final ListBody body = new ListBody();
71          body.id = dictionaryFile.getId();
72          body.type = dictionaryFile.getType();
73          body.path = dictionaryFile.getPath();
74          body.timestamp = dictionaryFile.getTimestamp();
75          return body;
76      }
77  }