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.restapi;
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.annotations.MCRRequireTransaction;
29  
30  import jakarta.ws.rs.container.ResourceInfo;
31  import jakarta.ws.rs.core.FeatureContext;
32  import jakarta.ws.rs.ext.Provider;
33  
34  /**
35   * Jersey configuration 
36   * @author Matthias Eichner
37   * 
38   * @see MCRJerseyDefaultFeature
39   * 
40   * @version $Revision: $ $Date: $
41   * 
42   */
43  @Provider
44  public class MCRRestFeature 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 Stream.concat(
70              MCRConfiguration2.getString("MCR.RestAPI.Resource.Packages").map(MCRConfiguration2::splitValue)
71                  .orElse(Stream.empty()),
72              MCRConfiguration2.getString("MCR.RestAPI.V2.Resource.Packages").map(MCRConfiguration2::splitValue)
73                  .orElse(Stream.empty()))
74              .collect(Collectors.toList());
75      }
76  
77      @Override
78      protected void registerSessionHookFilter(FeatureContext context) {
79          // don't register transaction filter, is already implemented by MCRSessionFilter
80      }
81  
82      @Override
83      protected void registerTransactionFilter(FeatureContext context) {
84          // don't register transaction filter, is already implemented by MCRSessionFilter
85      }
86  
87      @Override
88      protected void registerAccessFilter(FeatureContext context, Class<?> resourceClass, Method resourceMethod) {
89          if (resourceClass.getPackageName().contains(".v1")) {
90              context.register(org.mycore.restapi.v1.MCRRestAuthorizationFilter.class);
91          } else {
92              context.register(org.mycore.restapi.v2.MCRRestAuthorizationFilter.class);
93          }
94          super.registerAccessFilter(context, resourceClass, resourceMethod);
95      }
96  
97  }