1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }