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.lod;
20  
21  import java.lang.reflect.Method;
22  import java.util.List;
23  import java.util.stream.Collectors;
24  import java.util.stream.Stream;
25  
26  import org.mycore.common.config.MCRConfiguration2;
27  import org.mycore.frontend.jersey.feature.MCRJerseyDefaultFeature;
28  import org.mycore.restapi.MCREnableTransactionFilter;
29  import org.mycore.restapi.annotations.MCRRequireTransaction;
30  
31  import jakarta.ws.rs.container.ResourceInfo;
32  import jakarta.ws.rs.core.FeatureContext;
33  import jakarta.ws.rs.ext.Provider;
34  
35  /**
36   * Jersey configuration for Linked Open Data Endpoint
37   * 
38   * @author Robert Stephan
39   * 
40   * @see MCRJerseyDefaultFeature
41   * 
42   */
43  @Provider
44  public class MCRLodFeature extends MCRJerseyDefaultFeature {
45      @Override
46      public void configure(ResourceInfo resourceInfo, FeatureContext context) {
47          Class<?> resourceClass = resourceInfo.getResourceClass();
48          Method resourceMethod = resourceInfo.getResourceMethod();
49          if (requiresTransaction(resourceClass, resourceMethod)) {
50              context.register(MCREnableTransactionFilter.class);
51          }
52          super.configure(resourceInfo, context);
53      }
54  
55      /**
56       * Checks if the class/method is annotated by {@link MCRRequireTransaction}.
57       *
58       * @param resourceClass the class to check
59       * @param resourceMethod the method to check
60       * @return true if one ore both is annotated and requires transaction
61       */
62      protected boolean requiresTransaction(Class<?> resourceClass, Method resourceMethod) {
63          return resourceClass.getAnnotation(MCRRequireTransaction.class) != null
64              || resourceMethod.getAnnotation(MCRRequireTransaction.class) != null;
65      }
66  
67      @Override
68      protected List<String> getPackages() {
69          return MCRConfiguration2.getString("MCR.LOD.Resource.Packages").map(MCRConfiguration2::splitValue)
70              .orElse(Stream.empty())
71              .collect(Collectors.toList());
72      }
73  
74      @Override
75      protected void registerSessionHookFilter(FeatureContext context) {
76          // don't register transaction filter, is already implemented by MCRSessionFilter
77      }
78  
79      @Override
80      protected void registerTransactionFilter(FeatureContext context) {
81          // don't register transaction filter, is already implemented by MCRSessionFilter
82      }
83  
84      @Override
85      protected void registerAccessFilter(FeatureContext context, Class<?> resourceClass, Method resourceMethod) {
86          //don't have to register access filter yet
87          // example: context.register(org.mycore.restapi.v1.MCRRestAuthorizationFilter.class);
88          super.registerAccessFilter(context, resourceClass, resourceMethod);
89      }
90  
91  }