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.bootstrap;
20  
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.Optional;
26  import org.apache.syncope.client.lib.AnonymousAuthenticationHandler;
27  import org.apache.syncope.client.lib.SyncopeClient;
28  import org.apache.syncope.client.lib.SyncopeClientFactoryBean;
29  import org.apache.syncope.common.keymaster.client.api.KeymasterException;
30  import org.apache.syncope.common.keymaster.client.api.ServiceOps;
31  import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
32  import org.apereo.cas.util.spring.ApplicationContextProvider;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  import org.springframework.context.ApplicationContext;
36  
37  public class WARestClient {
38  
39      protected static final Logger LOG = LoggerFactory.getLogger(WARestClient.class);
40  
41      protected final String anonymousUser;
42  
43      protected final String anonymousKey;
44  
45      protected final boolean useGZIPCompression;
46  
47      protected final String serviceDiscoveryAddress;
48  
49      protected final Map<Class<?>, Object> services = Collections.synchronizedMap(new HashMap<>());
50  
51      private SyncopeClient client;
52  
53      public WARestClient(
54              final String anonymousUser,
55              final String anonymousKey,
56              final boolean useGZIPCompression,
57              final String serviceDiscoveryAddress) {
58  
59          this.anonymousUser = anonymousUser;
60          this.anonymousKey = anonymousKey;
61          this.useGZIPCompression = useGZIPCompression;
62          this.serviceDiscoveryAddress = serviceDiscoveryAddress;
63      }
64  
65      protected Optional<NetworkService> getCore() {
66          try {
67              ApplicationContext ctx = ApplicationContextProvider.getApplicationContext();
68              if (ctx == null) {
69                  return Optional.empty();
70              }
71  
72              Collection<ServiceOps> serviceOpsList = ctx.getBeansOfType(ServiceOps.class).values();
73              if (serviceOpsList.isEmpty()) {
74                  return Optional.empty();
75              }
76  
77              ServiceOps serviceOps = serviceOpsList.iterator().next();
78  
79              if (serviceOps.list(NetworkService.Type.WA).
80                      stream().anyMatch(s -> s.getAddress().equals(serviceDiscoveryAddress))) {
81  
82                  return Optional.of(serviceOps.get(NetworkService.Type.CORE));
83              }
84  
85              return Optional.empty();
86          } catch (KeymasterException e) {
87              LOG.trace(e.getMessage());
88          }
89          return Optional.empty();
90      }
91  
92      public SyncopeClient getSyncopeClient() {
93          synchronized (this) {
94              if (client == null) {
95                  getCore().ifPresent(core -> {
96                      try {
97                          client = new SyncopeClientFactoryBean().
98                                  setAddress(core.getAddress()).
99                                  setUseCompression(useGZIPCompression).
100                                 create(new AnonymousAuthenticationHandler(anonymousUser, anonymousKey));
101                     } catch (Exception e) {
102                         LOG.error("Could not init SyncopeClient", e);
103                     }
104                 });
105             }
106 
107             return client;
108         }
109     }
110 
111     @SuppressWarnings("unchecked")
112     public <T> T getService(final Class<T> serviceClass) {
113         if (!isReady()) {
114             throw new IllegalStateException("Syncope core is not yet ready");
115         }
116 
117         T service;
118         if (services.containsKey(serviceClass)) {
119             service = (T) services.get(serviceClass);
120         } else {
121             service = getSyncopeClient().getService(serviceClass);
122             services.put(serviceClass, service);
123         }
124 
125         return service;
126     }
127 
128     public boolean isReady() {
129         try {
130             return getCore().isPresent();
131         } catch (Exception e) {
132             LOG.trace("While checking Core's availability: {}", e.getMessage());
133         }
134         return false;
135     }
136 }