<commit_before>/*
 * Copyright 2012 SURFnet bv, The Netherlands
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nl.surfnet.coin.selfservice.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import nl.surfnet.coin.selfservice.domain.FederatieConfig;
import nl.surfnet.coin.selfservice.domain.IdentityProvider;
import nl.surfnet.coin.selfservice.domain.ServiceProvider;
import nl.surfnet.coin.selfservice.service.IdentityProviderService;
import nl.surfnet.coin.selfservice.service.ServiceProviderService;
import nl.surfnet.coin.selfservice.util.XStreamFedConfigBuilder;

/**
 * Provider Service seeded with xml config from federation.
 */
public class FederationProviderService implements ServiceProviderService, IdentityProviderService {

  private static final Logger LOG = LoggerFactory.getLogger(FederationProviderService.class);
  private FederatieConfig federatieConfig;

  /**
   * Constructor
   * @param configurationFilename Filename on classpath that contains federation configuration to parse and use.
   *
   */

  public FederationProviderService(String configurationFilename) {
    final XStream xStream = XStreamFedConfigBuilder.getXStreamForFedConfig(true);
    Resource resource = new ClassPathResource(configurationFilename);
    try {
      federatieConfig = (FederatieConfig) xStream.fromXML(resource.getInputStream());
    } catch (IOException e) {
      LOG.error("While reading file '" + configurationFilename + "'", e);
    }
  }

  @Override
  @Cacheable(value = { "sps-federation" })
  public List<ServiceProvider> getLinkedServiceProviders(String idpId) {
    List<ServiceProvider> providers = new ArrayList<ServiceProvider>();
    for (ServiceProvider sp : federatieConfig.getSps()) {
      if (isLinked(idpId, sp)) {
        sp.setLinked(true);
        providers.add(sp);
      }
    }<commit_msg>Add TODO for cronjob to update the config file from an http resource<commit_after>/*
 * Copyright 2012 SURFnet bv, The Netherlands
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nl.surfnet.coin.selfservice.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import nl.surfnet.coin.selfservice.domain.FederatieConfig;
import nl.surfnet.coin.selfservice.domain.IdentityProvider;
import nl.surfnet.coin.selfservice.domain.ServiceProvider;
import nl.surfnet.coin.selfservice.service.IdentityProviderService;
import nl.surfnet.coin.selfservice.service.ServiceProviderService;
import nl.surfnet.coin.selfservice.util.XStreamFedConfigBuilder;

/**
 * Provider Service seeded with xml config from federation.
 */
public class FederationProviderService implements ServiceProviderService, IdentityProviderService {

  private static final Logger LOG = LoggerFactory.getLogger(FederationProviderService.class);
  private FederatieConfig federatieConfig;

  /**
   * Constructor
   * @param configurationFilename Filename on classpath that contains federation configuration to parse and use.
   *
   */
  // TODO: http://static.springsource.org/spring/docs/3.1.1.RELEASE/spring-framework-reference/html/scheduling.html
  public FederationProviderService(String configurationFilename) {
    final XStream xStream = XStreamFedConfigBuilder.getXStreamForFedConfig(true);
    Resource resource = new ClassPathResource(configurationFilename);
    try {
      federatieConfig = (FederatieConfig) xStream.fromXML(resource.getInputStream());
    } catch (IOException e) {
      LOG.error("While reading file '" + configurationFilename + "'", e);
    }
  }

  @Override
  @Cacheable(value = { "sps-federation" })
  public List<ServiceProvider> getLinkedServiceProviders(String idpId) {
    List<ServiceProvider> providers = new ArrayList<ServiceProvider>();
    for (ServiceProvider sp : federatieConfig.getSps()) {
      if (isLinked(idpId, sp)) {
        sp.setLinked(true);
        providers.add(sp);
      }
    }
{'fraction_non_alphanumeric': 0.09002520705797623, 'fraction_numerical': 0.0041411595246669064, 'mean_word_length': 4.697435897435898, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4050454', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1511, 'n_words': 412}