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.crawler.helper;
17  
18  import java.util.LinkedHashMap;
19  import java.util.Map;
20  
21  import org.apache.commons.lang3.StringUtils;
22  import org.apache.logging.log4j.LogManager;
23  import org.apache.logging.log4j.Logger;
24  import org.codelibs.core.lang.StringUtil;
25  import org.codelibs.core.stream.StreamUtil;
26  import org.codelibs.fess.crawler.helper.impl.MimeTypeHelperImpl;
27  import org.codelibs.fess.util.ComponentUtil;
28  
29  import jakarta.annotation.PostConstruct;
30  
31  /**
32   * FessMimeTypeHelper extends MimeTypeHelperImpl to provide Fess-specific
33   * MIME type detection configuration. It reads extension-to-MIME-type override
34   * mappings from FessConfig to handle cases where content-based detection
35   * produces incorrect results (e.g., SQL files starting with REM comments
36   * being misdetected as batch files).
37   */
38  public class FessMimeTypeHelper extends MimeTypeHelperImpl {
39  
40      private static final Logger logger = LogManager.getLogger(FessMimeTypeHelper.class);
41  
42      /**
43       * Default constructor for FessMimeTypeHelper.
44       */
45      public FessMimeTypeHelper() {
46          // Default constructor
47      }
48  
49      /**
50       * Initializes the extension override map by loading configuration from FessConfig.
51       * The mappings are loaded from the crawler.document.mimetype.extension.overrides property,
52       * where each line contains a mapping in the format ".ext=mime/type".
53       */
54      @PostConstruct
55      public void init() {
56          final String overrides = ComponentUtil.getFessConfig().getCrawlerDocumentMimetypeExtensionOverrides();
57          if (StringUtil.isNotBlank(overrides)) {
58              final Map<String, String> map = new LinkedHashMap<>();
59              StreamUtil.split(overrides, "\n").of(stream -> stream.filter(StringUtil::isNotBlank).forEach(s -> {
60                  final String[] values = StringUtils.split(s, "=", 2);
61                  if (values.length == 2) {
62                      map.put(values[0].trim(), values[1].trim());
63                  }
64              }));
65              setExtensionMimeTypeMap(map);
66              if (logger.isDebugEnabled()) {
67                  logger.debug("loaded mimetype extension overrides: {}", map);
68              }
69          }
70      }
71  }