View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.solr.index.strategy;
20  
21  import static org.mycore.solr.MCRSolrConstants.SOLR_CONFIG_PREFIX;
22  
23  import java.nio.file.Path;
24  import java.nio.file.attribute.BasicFileAttributes;
25  import java.util.regex.Pattern;
26  
27  import org.mycore.common.config.MCRConfiguration2;
28  import org.mycore.common.xml.MCRXMLFunctions;
29  
30  /**
31   * Strategy that depends on a files mime type. By default images are
32   * ignored. You can use the MCR.Solr.MimeTypeStrategy.Pattern property to
33   * set an application specific pattern. Be aware that this is the ignore
34   * pattern, the {@link #check(Path, BasicFileAttributes)} method will return false if it
35   * matches.
36   * 
37   * @author Matthias Eichner
38   * @author Thomas Scheffler (yagee)
39   */
40  public class MCRSolrMimeTypeStrategy implements MCRSolrFileStrategy {
41  
42      private static final Pattern IGNORE_PATTERN;
43  
44      static {
45          String acceptPattern = MCRConfiguration2.getStringOrThrow(SOLR_CONFIG_PREFIX + "MimeTypeStrategy.Pattern");
46          IGNORE_PATTERN = Pattern.compile(acceptPattern);
47      }
48  
49      @Override
50      public boolean check(Path file, BasicFileAttributes attrs) {
51          String mimeType = MCRXMLFunctions.getMimeType(file.getFileName().toString());
52          return !IGNORE_PATTERN.matcher(mimeType).matches();
53      }
54  
55  }