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 static java.util.Collections.emptyList;
009import static java.util.Collections.singletonList;
010
011import java.util.List;
012
013import org.apache.http.auth.AuthScope;
014import org.apache.http.auth.Credentials;
015import org.apache.http.client.CredentialsProvider;
016import org.apache.http.impl.client.BasicCredentialsProvider;
017import org.apache.http.impl.client.HttpClients;
018import org.apache.marmotta.ldclient.api.endpoint.Endpoint;
019import org.apache.marmotta.ldclient.api.provider.DataProvider;
020import org.apache.marmotta.ldclient.endpoint.rdf.LinkedDataEndpoint;
021import org.apache.marmotta.ldclient.model.ClientConfiguration;
022import org.apache.marmotta.ldclient.provider.rdf.CacheProvider;
023import org.apache.marmotta.ldclient.provider.rdf.LinkedDataProvider;
024import org.apache.marmotta.ldclient.provider.rdf.RegexUriProvider;
025import org.apache.marmotta.ldclient.provider.rdf.SPARQLProvider;
026
027/**
028 * A convenience factory for creating a ClientConfiguration object
029 * @author acoburn
030 * @since Aug 5, 2016
031 */
032public class ClientFactory {
033
034
035    /**
036     * Configure a linked data client suitable for use with a Fedora Repository.
037     * @param endpoint  Endpoint to enable on the client
038     * @return a configuration for use with an LDClient
039     */
040    public static ClientConfiguration createClient(final Endpoint endpoint) {
041        return createClient(singletonList(endpoint), emptyList());
042    }
043
044    /**
045     * Configure a linked data client suitable for use with a Fedora Repository.
046     * @param provider Provider to enable on the client
047     * @return a configuration for use with an LDClient
048     */
049    public static ClientConfiguration createClient(final DataProvider provider) {
050        return createClient(null, null, emptyList(), singletonList(provider));
051    }
052
053
054    /**
055     * Configure a linked data client suitable for use with a Fedora Repository.
056     * @param endpoint  Endpoint to enable on the client
057     * @param provider Provider to enable on the client
058     * @return a configuration for use with an LDClient
059     */
060    public static ClientConfiguration createClient(final Endpoint endpoint, final DataProvider provider) {
061        return createClient(singletonList(endpoint), singletonList(provider));
062    }
063
064    /**
065     * Configure a linked data client suitable for use with a Fedora Repository.
066     * @param endpoints additional endpoints to enable on the client
067     * @param providers additional providers to enable on the client
068     * @return a configuration for use with an LDClient
069     */
070    public static ClientConfiguration createClient(final List<Endpoint> endpoints, final List<DataProvider> providers) {
071        return createClient(null, null, endpoints, providers);
072    }
073
074    /**
075     * Configure a linked data client suitable for use with a Fedora Repository.
076     * @param authScope the authentication scope
077     * @param credentials the credentials
078     * @return a configuration for use with an LDClient
079     */
080    public static ClientConfiguration createClient(final AuthScope authScope, final Credentials credentials) {
081        return createClient(authScope, credentials, emptyList(), emptyList());
082    }
083
084    /**
085     * Create a linked data client suitable for use with a Fedora Repository.
086     * @param authScope the authentication scope
087     * @param credentials the credentials
088     * @param endpoints additional endpoints to enable on the client
089     * @param providers additional providers to enable on the client
090     * @return a configuration for use with an LDClient
091     */
092    public static ClientConfiguration createClient(final AuthScope authScope, final Credentials credentials,
093            final List<Endpoint> endpoints, final List<DataProvider> providers) {
094
095        final ClientConfiguration client = new ClientConfiguration();
096
097        if (credentials != null && authScope != null) {
098            final CredentialsProvider credsProvider = new BasicCredentialsProvider();
099            credsProvider.setCredentials(authScope, credentials);
100            client.setHttpClient(HttpClients.custom()
101                    .setDefaultCredentialsProvider(credsProvider)
102                    .useSystemProperties().build());
103        }
104
105        // manually add default Providers and Endpoints
106        client.addProvider(new LinkedDataProvider());
107        client.addProvider(new CacheProvider());
108        client.addProvider(new RegexUriProvider());
109        client.addProvider(new SPARQLProvider());
110        client.addEndpoint(new LinkedDataEndpoint());
111
112        // add any injected endpoints/providers
113        endpoints.forEach(client::addEndpoint);
114        providers.forEach(client::addProvider);
115
116        return client;
117    }
118
119    private ClientFactory() {
120        // prevent instantiation
121    }
122
123}