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.iiif.presentation.impl;
20  
21  import java.lang.reflect.Constructor;
22  import java.lang.reflect.InvocationTargetException;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.mycore.common.MCRException;
27  import org.mycore.common.config.MCRConfiguration2;
28  import org.mycore.common.config.MCRConfigurationException;
29  import org.mycore.iiif.presentation.model.basic.MCRIIIFManifest;
30  
31  public abstract class MCRIIIFPresentationImpl {
32  
33      private static final String MCR_IIIF_PRESENTATION_CONFIG_PREFIX = "MCR.IIIFPresentation.";
34  
35      private static final Map<String, MCRIIIFPresentationImpl> IMPLHOLDER = new HashMap<>();
36  
37      private final String implName;
38  
39      public MCRIIIFPresentationImpl(final String implName) {
40          this.implName = implName;
41      }
42  
43      public static synchronized MCRIIIFPresentationImpl getInstance(String implNameParameter) {
44          String implName = (implNameParameter == null || implNameParameter.isBlank())
45              ? MCRConfiguration2.getStringOrThrow("MCR.IIIFPresentation.Default")
46              : implNameParameter;
47  
48          if (IMPLHOLDER.containsKey(implName)) {
49              return IMPLHOLDER.get(implName);
50          }
51  
52          String classPropertyName = MCR_IIIF_PRESENTATION_CONFIG_PREFIX + implName;
53          Class<? extends MCRIIIFPresentationImpl> classObject = MCRConfiguration2.<MCRIIIFPresentationImpl>getClass(
54              classPropertyName)
55              .orElseThrow(() -> MCRConfiguration2.createConfigurationException(classPropertyName));
56  
57          try {
58              Constructor<? extends MCRIIIFPresentationImpl> constructor = classObject.getConstructor(String.class);
59              MCRIIIFPresentationImpl presentationImpl = constructor.newInstance(implName);
60              IMPLHOLDER.put(implName, presentationImpl);
61              return presentationImpl;
62          } catch (NoSuchMethodException e) {
63              throw new MCRConfigurationException(
64                  "Configurated class (" + classObject.getName() + ") needs a string constructor: " + classPropertyName);
65          } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
66              throw new MCRException(e);
67          }
68      }
69  
70      public String getImplName() {
71          return implName;
72      }
73  
74      protected final Map<String, String> getProperties() {
75          return MCRConfiguration2.getSubPropertiesMap(getConfigPrefix());
76      }
77  
78      private String getConfigPrefix() {
79          return MCR_IIIF_PRESENTATION_CONFIG_PREFIX + implName + ".";
80      }
81  
82      /**
83       * For consistency and security reasons it may become necessary to
84       * to cleanup the identifier, which is an otherwise unchecked URL path parameter.
85       * 
86       * Subclasses may override.
87       * 
88       * @param identifier - the IIIF manifest identifier, which should be normalized
89       * @return the normalized identifier
90       */
91      public String normalizeIdentifier(String identifier) {
92          return identifier;
93      }
94  
95      public abstract MCRIIIFManifest getManifest(String id);
96  
97  }