001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.camel.ldpath;
007
008import org.apache.http.HttpHost;
009import org.apache.http.auth.AuthScope;
010import org.apache.http.auth.Credentials;
011import org.apache.http.auth.UsernamePasswordCredentials;
012import org.apache.marmotta.ldcache.api.LDCachingBackend;
013import org.apache.marmotta.ldcache.backend.file.LDCachingFileBackend;
014import org.apache.marmotta.ldcache.model.CacheConfiguration;
015import org.apache.marmotta.ldcache.services.LDCache;
016import org.apache.marmotta.ldclient.api.endpoint.Endpoint;
017import org.apache.marmotta.ldclient.api.provider.DataProvider;
018import org.apache.marmotta.ldpath.api.functions.SelectorFunction;
019import org.apache.marmotta.ldpath.backend.linkeddata.LDCacheBackend;
020import org.fcrepo.camel.common.config.BasePropsConfig;
021import org.fcrepo.client.FcrepoHttpClientBuilder;
022import org.springframework.beans.factory.annotation.Value;
023import org.springframework.context.annotation.Bean;
024import org.springframework.context.annotation.Configuration;
025
026import java.nio.file.Path;
027import java.util.List;
028import java.util.Set;
029
030import static java.util.Collections.EMPTY_SET;
031
032/**
033 * A configuration class for the LDPath service
034 *
035 * @author dbernstein
036 */
037@Configuration
038public class FcrepoLdPathConfig extends BasePropsConfig {
039
040    @Value("${ldpath.fcrepo.cache.timeout:0}")
041    private long fcrepoCacheTimeout;
042
043    @Value("${ldpath.rest.prefix:/ldpath}")
044    private String restPrefix;
045
046    @Value("${ldpath.rest.host:localhost}")
047    private String restHost;
048
049    @Value("${ldpath.rest.port:9085}")
050    private int restPort;
051
052    @Value("${ldpath.cache.timeout:86400}")
053    private int cacheTimeout;
054
055    @Value("${ldpath.ldcache.directory:ldcache/}")
056    private Path ldCacheDirectory;
057
058    @Value("${ldpath.transform.path:classpath:org/fcrepo/camel/ldpath/default.ldpath}")
059    private String ldpathTransformPath;
060
061    public String getRestHost() {
062        return restHost;
063    }
064
065    public int getRestPort() {
066        return restPort;
067    }
068
069    public String getRestPrefix() {
070        return restPrefix;
071    }
072
073    public String getLdpathTransformPath() {
074        return ldpathTransformPath;
075    }
076
077    @Bean("ldpath")
078    public LDPathWrapper ldpath() throws Exception {
079        final var fcrepoBaseUrl = getFcrepoBaseUrl();
080        final var fcrepoAuthHost = getFcrepoAuthHost();
081        final var fcrepoUsername = getFcrepoUsername();
082        final var fcrepoPassword = getFcrepoPassword();
083
084        final AuthScope authScope;
085        if (fcrepoAuthHost == null || fcrepoAuthHost.isBlank()) {
086            authScope = new AuthScope(AuthScope.ANY);
087        } else {
088            authScope = new AuthScope(new HttpHost(fcrepoAuthHost));
089        }
090        final Credentials credentials = new UsernamePasswordCredentials(fcrepoUsername, fcrepoPassword);
091        final List<Endpoint> endpoints = List.of(new FedoraEndpoint(fcrepoBaseUrl, fcrepoCacheTimeout));
092        final var fcrepoHttpClientBuilder = new FcrepoHttpClientBuilder(fcrepoUsername, fcrepoPassword, fcrepoAuthHost);
093        final List<DataProvider> providers = List.of(new FedoraProvider(fcrepoHttpClientBuilder));
094        final var client = ClientFactory.createClient(authScope, credentials, endpoints, providers);
095        final var config = new CacheConfiguration(client);
096        config.setDefaultExpiry(cacheTimeout);
097        final LDCachingBackend ldCachingBackend = new LDCachingFileBackend(ldCacheDirectory.toFile());
098        ldCachingBackend.initialize();
099        final LDCache ldCache = new LDCache(config, ldCachingBackend);
100        final var backend = new LDCacheBackend(ldCache);
101        return new LDPathWrapper(backend, createSelectorFunctions());
102    }
103
104    protected Set<SelectorFunction> createSelectorFunctions() {
105        return EMPTY_SET;
106    }
107
108    @Bean
109    public LDPathRouter ldPathRouter() {
110        return new LDPathRouter();
111    }
112
113}