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.config;
20  
21  import java.util.Optional;
22  import org.apache.commons.lang3.StringUtils;
23  import org.apache.syncope.wa.bootstrap.WARestClient;
24  import org.apereo.cas.support.saml.idp.metadata.generator.SamlIdPMetadataGenerator;
25  import org.apereo.cas.support.saml.services.idp.metadata.SamlIdPMetadataDocument;
26  import org.apereo.cas.util.AsciiArtUtils;
27  import org.quartz.Job;
28  import org.quartz.JobExecutionContext;
29  import org.quartz.JobExecutionException;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  import org.springframework.beans.factory.annotation.Autowired;
33  import org.springframework.cloud.context.refresh.ContextRefresher;
34  
35  public class WARefreshContextJob implements Job {
36  
37      private static final Logger LOG = LoggerFactory.getLogger(WARefreshContextJob.class);
38  
39      @Autowired
40      private WARestClient waRestClient;
41  
42      @Autowired
43      private ContextRefresher contextRefresher;
44  
45      @Autowired
46      private SamlIdPMetadataGenerator metadataGenerator;
47  
48      @Override
49      public void execute(final JobExecutionContext jobExecutionContext) throws JobExecutionException {
50          try {
51              LOG.debug("Attempting to refresh WA application context");
52              if (!waRestClient.isReady()) {
53                  LOG.debug("Syncope client is not yet ready");
54                  throw new IllegalStateException("Syncope core is not yet ready to access requests");
55              }
56              contextRefresher.refresh();
57              LOG.info("Refreshed application context to bootstrap property sources, etc...");
58  
59              LOG.info("Generating SAML2 IdP metadata metadata");
60              SamlIdPMetadataDocument document = metadataGenerator.generate(Optional.empty());
61              LOG.info("Generated SAML2 IdP metadata for {}", document.getAppliesTo());
62  
63              advertiseReady();
64          } catch (final Exception e) {
65              throw new JobExecutionException("While generating SAML2 IdP metadata", e);
66          }
67      }
68  
69      private static void advertiseReady() {
70          AsciiArtUtils.printAsciiArtReady(LOG, StringUtils.EMPTY);
71          LOG.info("Ready to process requests");
72      }
73  }