View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.wa.starter.services;
20  
21  import java.util.Collection;
22  import java.util.List;
23  import java.util.Objects;
24  import java.util.stream.Collectors;
25  import org.apache.syncope.common.lib.types.ClientAppType;
26  import org.apache.syncope.common.rest.api.service.wa.WAClientAppService;
27  import org.apache.syncope.wa.bootstrap.WARestClient;
28  import org.apache.syncope.wa.starter.mapping.RegisteredServiceMapper;
29  import org.apereo.cas.services.AbstractServiceRegistry;
30  import org.apereo.cas.services.OidcRegisteredService;
31  import org.apereo.cas.services.RegisteredService;
32  import org.apereo.cas.services.ServiceRegistryListener;
33  import org.apereo.cas.support.saml.services.SamlRegisteredService;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  import org.springframework.context.ConfigurableApplicationContext;
37  
38  public class WAServiceRegistry extends AbstractServiceRegistry {
39  
40      private static final Logger LOG = LoggerFactory.getLogger(WAServiceRegistry.class);
41  
42      protected final WARestClient waRestClient;
43  
44      protected final RegisteredServiceMapper registeredServiceMapper;
45  
46      public WAServiceRegistry(
47              final WARestClient restClient,
48              final RegisteredServiceMapper registeredServiceMapper,
49              final ConfigurableApplicationContext applicationContext,
50              final Collection<ServiceRegistryListener> serviceRegistryListeners) {
51  
52          super(applicationContext, serviceRegistryListeners);
53          this.waRestClient = restClient;
54          this.registeredServiceMapper = registeredServiceMapper;
55      }
56  
57      @Override
58      public RegisteredService save(final RegisteredService registeredService) {
59          throw new UnsupportedOperationException("Saving registered services from WA is not supported");
60      }
61  
62      @Override
63      public boolean delete(final RegisteredService registeredService) {
64          throw new UnsupportedOperationException("Deleting registered services from WA is not supported");
65      }
66  
67      @Override
68      public void deleteAll() {
69          throw new UnsupportedOperationException("Bulk deleting registered services from WA is not supported");
70      }
71  
72      @Override
73      public Collection<RegisteredService> load() {
74          if (!waRestClient.isReady()) {
75              LOG.debug("Syncope client is not yet ready to fetch application definitions");
76              return List.of();
77          }
78  
79          LOG.info("Loading application definitions");
80          return waRestClient.getService(WAClientAppService.class).list().stream().
81                  map(registeredServiceMapper::toRegisteredService).
82                  filter(Objects::nonNull).
83                  collect(Collectors.toList());
84      }
85  
86      @Override
87      public RegisteredService findServiceById(final long id) {
88          if (!waRestClient.isReady()) {
89              LOG.debug("Syncope client is not yet ready to fetch application definitions");
90              return null;
91          }
92  
93          LOG.info("Searching for application definition by id {}", id);
94          return registeredServiceMapper.toRegisteredService(
95                  waRestClient.getService(WAClientAppService.class).read(id, null));
96      }
97  
98      @SuppressWarnings("unchecked")
99      @Override
100     public <T extends RegisteredService> T findServiceById(final long id, final Class<T> clazz) {
101         if (!waRestClient.isReady()) {
102             LOG.debug("Syncope client is not yet ready to fetch application definitions");
103             return null;
104         }
105 
106         LOG.info("Searching for application definition by id {} and type {}", id, clazz);
107         if (clazz.isInstance(OidcRegisteredService.class)) {
108             return (T) registeredServiceMapper.toRegisteredService(
109                     waRestClient.getService(WAClientAppService.class).read(id, ClientAppType.OIDCRP));
110         }
111         if (clazz.isInstance(SamlRegisteredService.class)) {
112             return (T) registeredServiceMapper.toRegisteredService(
113                     waRestClient.getService(WAClientAppService.class).read(id, ClientAppType.SAML2SP));
114         }
115         return (T) registeredServiceMapper.toRegisteredService(
116                 waRestClient.getService(WAClientAppService.class).read(id, ClientAppType.CASSP));
117     }
118 
119     @SuppressWarnings("unchecked")
120     @Override
121     public <T extends RegisteredService> T findServiceByExactServiceName(final String name, final Class<T> clazz) {
122         if (!waRestClient.isReady()) {
123             LOG.debug("Syncope client is not yet ready to fetch application definitions");
124             return null;
125         }
126 
127         LOG.info("Searching for application definition by name {} and type {}", name, clazz);
128         if (clazz.isInstance(OidcRegisteredService.class)) {
129             return (T) registeredServiceMapper.toRegisteredService(waRestClient.
130                     getService(WAClientAppService.class).read(name, ClientAppType.OIDCRP));
131         }
132         if (clazz.isInstance(SamlRegisteredService.class)) {
133             return (T) registeredServiceMapper.toRegisteredService(waRestClient.
134                     getService(WAClientAppService.class).read(name, ClientAppType.SAML2SP));
135         }
136         return (T) registeredServiceMapper.toRegisteredService(waRestClient.
137                 getService(WAClientAppService.class).read(name, ClientAppType.CASSP));
138     }
139 
140     @Override
141     public RegisteredService findServiceByExactServiceName(final String name) {
142         if (!waRestClient.isReady()) {
143             LOG.debug("Syncope client is not yet ready to fetch application definitions");
144             return null;
145         }
146 
147         LOG.info("Searching for application definition by name {}", name);
148         return registeredServiceMapper.toRegisteredService(
149                 waRestClient.getService(WAClientAppService.class).read(name, null));
150     }
151 }