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.helper;
17  
18  import java.util.LinkedHashMap;
19  import java.util.Map;
20  
21  import javax.annotation.PostConstruct;
22  
23  import org.apache.commons.lang3.StringUtils;
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.Logger;
26  import org.codelibs.core.lang.StringUtil;
27  import org.codelibs.core.stream.StreamUtil;
28  import org.codelibs.fess.util.ComponentUtil;
29  
30  public class FileTypeHelper {
31      private static final Logger logger = LogManager.getLogger(FileTypeHelper.class);
32  
33      protected String defaultValue = "others";
34  
35      protected Map<String, String> mimetypeMap = new LinkedHashMap<>();
36  
37      @PostConstruct
38      public void init() {
39          StreamUtil.split(ComponentUtil.getFessConfig().getIndexFiletype(), "\n")
40                  .of(stream -> stream.filter(StringUtil::isNotBlank).forEach(s -> {
41                      final String[] values = StringUtils.split(s, "=", 2);
42                      if (values.length == 2) {
43                          mimetypeMap.put(values[0], values[1]);
44                      }
45                  }));
46          if (logger.isDebugEnabled()) {
47              logger.debug("loaded filetype: {}", mimetypeMap);
48          }
49      }
50  
51      public void add(final String mimetype, final String filetype) {
52          mimetypeMap.put(mimetype, filetype);
53      }
54  
55      public String get(final String mimetype) {
56          final String filetype = mimetypeMap.get(mimetype);
57          if (StringUtil.isBlank(filetype)) {
58              return defaultValue;
59          }
60          return filetype;
61      }
62  
63      public String getDefaultValue() {
64          return defaultValue;
65      }
66  
67      public void setDefaultValue(final String defaultValue) {
68          this.defaultValue = defaultValue;
69      }
70  
71      public String[] getTypes() {
72          return mimetypeMap.values().stream().distinct().toArray(n -> new String[n]);
73      }
74  }