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.common.keymaster.client.self;
20  
21  import com.fasterxml.jackson.databind.json.JsonMapper;
22  import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
23  import java.util.List;
24  import java.util.regex.Pattern;
25  import org.apache.cxf.ext.logging.LoggingFeature;
26  import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
27  import org.apache.syncope.common.keymaster.client.api.ConfParamOps;
28  import org.apache.syncope.common.keymaster.client.api.DomainOps;
29  import org.apache.syncope.common.keymaster.client.api.KeymasterProperties;
30  import org.apache.syncope.common.keymaster.client.api.ServiceOps;
31  import org.springframework.beans.factory.annotation.Qualifier;
32  import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
33  import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
34  import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
35  import org.springframework.boot.context.properties.EnableConfigurationProperties;
36  import org.springframework.context.annotation.Bean;
37  import org.springframework.context.annotation.ConditionContext;
38  import org.springframework.context.annotation.Conditional;
39  import org.springframework.context.annotation.Configuration;
40  import org.springframework.core.type.AnnotatedTypeMetadata;
41  
42  @EnableConfigurationProperties(KeymasterProperties.class)
43  @Configuration(proxyBeanMethods = false)
44  public class SelfKeymasterClientContext {
45  
46      private static final Pattern HTTP = Pattern.compile("^http.+");
47  
48      static class SelfKeymasterCondition extends SpringBootCondition {
49  
50          @Override
51          public ConditionOutcome getMatchOutcome(final ConditionContext context, final AnnotatedTypeMetadata metadata) {
52              String keymasterAddress = context.getEnvironment().getProperty("keymaster.address");
53              return new ConditionOutcome(
54                      keymasterAddress != null && HTTP.matcher(keymasterAddress).matches(),
55                      "Keymaster address not set for Self: " + keymasterAddress);
56          }
57      }
58  
59      @Conditional(SelfKeymasterCondition.class)
60      @Bean
61      @ConditionalOnMissingBean(name = "selfKeymasterRESTClientFactoryBean")
62      public JAXRSClientFactoryBean selfKeymasterRESTClientFactoryBean(final KeymasterProperties props) {
63          JAXRSClientFactoryBean restClientFactoryBean = new JAXRSClientFactoryBean();
64          restClientFactoryBean.setAddress(props.getAddress());
65          restClientFactoryBean.setUsername(props.getUsername());
66          restClientFactoryBean.setPassword(props.getPassword());
67          restClientFactoryBean.setThreadSafe(true);
68          restClientFactoryBean.setInheritHeaders(true);
69          restClientFactoryBean.getFeatures().add(new LoggingFeature());
70          restClientFactoryBean.setProviders(List.of(
71                  new JacksonJsonProvider(JsonMapper.builder().findAndAddModules().build()),
72                  new SelfKeymasterClientExceptionMapper()));
73          return restClientFactoryBean;
74      }
75  
76      @Conditional(SelfKeymasterCondition.class)
77      @Bean
78      @ConditionalOnMissingBean(name = "selfConfParamOps")
79      public ConfParamOps selfConfParamOps(@Qualifier("selfKeymasterRESTClientFactoryBean")
80              final JAXRSClientFactoryBean selfKeymasterRESTClientFactoryBean) {
81          return new SelfKeymasterConfParamOps(selfKeymasterRESTClientFactoryBean);
82      }
83  
84      @Conditional(SelfKeymasterCondition.class)
85      @Bean
86      @ConditionalOnMissingBean(name = "selfServiceOps")
87      public ServiceOps selfServiceOps(@Qualifier("selfKeymasterRESTClientFactoryBean")
88              final JAXRSClientFactoryBean selfKeymasterRESTClientFactoryBean) {
89          return new SelfKeymasterServiceOps(selfKeymasterRESTClientFactoryBean, 5);
90      }
91  
92      @Conditional(SelfKeymasterCondition.class)
93      @Bean
94      @ConditionalOnMissingBean(name = "domainOps")
95      public DomainOps domainOps(@Qualifier("selfKeymasterRESTClientFactoryBean")
96              final JAXRSClientFactoryBean selfKeymasterRESTClientFactoryBean) {
97          return new SelfKeymasterDomainOps(selfKeymasterRESTClientFactoryBean);
98      }
99  }