Search in sources :

Example 1 with PathRegexPatternTreeFilter

use of org.apereo.cas.git.PathRegexPatternTreeFilter in project cas by apereo.

the class GitSamlRegisteredServiceMetadataResolver method resolve.

@Override
public Collection<? extends MetadataResolver> resolve(final SamlRegisteredService service, final CriteriaSet criteriaSet) throws Exception {
    if (gitRepository.pull()) {
        LOGGER.debug("Successfully pulled metadata changes from the remote repository");
    } else {
        LOGGER.warn("Unable to pull changes from the remote repository. Metadata files may be stale.");
    }
    val metadataFiles = gitRepository.getObjectsInRepository(new PathRegexPatternTreeFilter(PATTERN_METADATA_FILES));
    val signatureFiles = gitRepository.getObjectsInRepository(new PathRegexPatternTreeFilter(PATTERN_SIGNATURE_FILES));
    return metadataFiles.stream().filter(Objects::nonNull).map(object -> parseGitObjectContentIntoSamlMetadataDocument(object, signatureFiles)).map(doc -> buildMetadataResolverFrom(service, doc)).filter(Objects::nonNull).collect(Collectors.toCollection(ArrayList::new));
}
Also used : lombok.val(lombok.val) BooleanUtils(org.apache.commons.lang3.BooleanUtils) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) LoggingUtils(org.apereo.cas.util.LoggingUtils) SamlRegisteredService(org.apereo.cas.support.saml.services.SamlRegisteredService) BaseSamlRegisteredServiceMetadataResolver(org.apereo.cas.support.saml.services.idp.metadata.cache.resolver.BaseSamlRegisteredServiceMetadataResolver) GitRepository(org.apereo.cas.git.GitRepository) SamlMetadataDocument(org.apereo.cas.support.saml.services.idp.metadata.SamlMetadataDocument) PathRegexPatternTreeFilter(org.apereo.cas.git.PathRegexPatternTreeFilter) Collection(java.util.Collection) SamlIdPProperties(org.apereo.cas.configuration.model.support.saml.idp.SamlIdPProperties) lombok.val(lombok.val) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) MetadataResolver(org.opensaml.saml.metadata.resolver.MetadataResolver) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) RegexUtils(org.apereo.cas.util.RegexUtils) OpenSamlConfigBean(org.apereo.cas.support.saml.OpenSamlConfigBean) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) CriteriaSet(net.shibboleth.utilities.java.support.resolver.CriteriaSet) Pattern(java.util.regex.Pattern) FilenameUtils(org.apache.commons.io.FilenameUtils) PathRegexPatternTreeFilter(org.apereo.cas.git.PathRegexPatternTreeFilter) Objects(java.util.Objects)

Example 2 with PathRegexPatternTreeFilter

use of org.apereo.cas.git.PathRegexPatternTreeFilter in project cas by apereo.

the class GitServiceRegistry method load.

@Synchronized
@Override
public Collection<RegisteredService> load() {
    try {
        if (gitRepository.pull()) {
            LOGGER.debug("Successfully pulled changes from the remote repository");
        } else {
            LOGGER.info("Unable to pull changes from the remote repository. Service definition files may be stale.");
        }
        val objectPatternStr = StringUtils.isBlank(rootDirectory) ? GitRepositoryRegisteredServiceLocator.PATTEN_ACCEPTED_REPOSITORY_FILES : rootDirectory + '/' + GitRepositoryRegisteredServiceLocator.PATTEN_ACCEPTED_REPOSITORY_FILES;
        val objectPattern = RegexUtils.createPattern(objectPatternStr, Pattern.CASE_INSENSITIVE);
        val objects = gitRepository.getObjectsInRepository(new PathRegexPatternTreeFilter(objectPattern));
        registeredServices = objects.stream().filter(Objects::nonNull).map(this::parseGitObjectContentIntoRegisteredService).flatMap(Collection::stream).map(this::invokeServiceRegistryListenerPostLoad).filter(Objects::nonNull).collect(Collectors.toList());
        return registeredServices;
    } catch (final Exception e) {
        LoggingUtils.warn(LOGGER, e);
        val parentDir = StringUtils.isBlank(rootDirectory) ? gitRepository.getRepositoryDirectory() : new File(gitRepository.getRepositoryDirectory(), rootDirectory);
        val files = FileUtils.listFiles(parentDir, GitRepositoryRegisteredServiceLocator.FILE_EXTENSIONS.toArray(ArrayUtils.EMPTY_STRING_ARRAY), true);
        LOGGER.debug("Located [{}] files(s)", files.size());
        registeredServices = files.stream().filter(file -> file.isFile() && file.canRead() && file.canWrite() && file.length() > 0).map(Unchecked.function(file -> {
            try (val in = Files.newBufferedReader(file.toPath())) {
                return registeredServiceSerializers.stream().filter(s -> s.supports(file)).map(s -> s.load(in)).filter(Objects::nonNull).flatMap(Collection::stream).map(this::invokeServiceRegistryListenerPostLoad).filter(Objects::nonNull).collect(Collectors.toList());
            } catch (final Exception ex) {
                LOGGER.error("Error reading configuration file [{}]", file.toPath());
                LoggingUtils.error(LOGGER, ex);
            }
            return new ArrayList<RegisteredService>(0);
        })).flatMap(List::stream).sorted().map(this::invokeServiceRegistryListenerPostLoad).filter(Objects::nonNull).collect(Collectors.toList());
        return registeredServices;
    }
}
Also used : lombok.val(lombok.val) SneakyThrows(lombok.SneakyThrows) ArrayUtils(org.apache.commons.lang3.ArrayUtils) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) LoggingUtils(org.apereo.cas.util.LoggingUtils) FunctionUtils(org.apereo.cas.util.function.FunctionUtils) StringSerializer(org.apereo.cas.util.serialization.StringSerializer) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) GitRepository(org.apereo.cas.git.GitRepository) GitRepositoryRegisteredServiceLocator(org.apereo.cas.services.locator.GitRepositoryRegisteredServiceLocator) Synchronized(lombok.Synchronized) Unchecked(org.jooq.lambda.Unchecked) Files(java.nio.file.Files) PathRegexPatternTreeFilter(org.apereo.cas.git.PathRegexPatternTreeFilter) Collection(java.util.Collection) lombok.val(lombok.val) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) RegexUtils(org.apereo.cas.util.RegexUtils) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) StringReader(java.io.StringReader) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) PathRegexPatternTreeFilter(org.apereo.cas.git.PathRegexPatternTreeFilter) Objects(java.util.Objects) ArrayList(java.util.ArrayList) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) IOException(java.io.IOException) Synchronized(lombok.Synchronized)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Objects (java.util.Objects)2 Pattern (java.util.regex.Pattern)2 Collectors (java.util.stream.Collectors)2 Slf4j (lombok.extern.slf4j.Slf4j)2 lombok.val (lombok.val)2 FileUtils (org.apache.commons.io.FileUtils)2 StringUtils (org.apache.commons.lang3.StringUtils)2 GitRepository (org.apereo.cas.git.GitRepository)2 PathRegexPatternTreeFilter (org.apereo.cas.git.PathRegexPatternTreeFilter)2 LoggingUtils (org.apereo.cas.util.LoggingUtils)2 RegexUtils (org.apereo.cas.util.RegexUtils)2 StringReader (java.io.StringReader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 List (java.util.List)1 Optional (java.util.Optional)1