001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.camel.ldpath;
019
020import static java.util.Collections.emptyList;
021
022import java.util.List;
023
024import org.apache.http.auth.AuthScope;
025import org.apache.http.auth.Credentials;
026import org.apache.http.client.CredentialsProvider;
027import org.apache.http.impl.client.BasicCredentialsProvider;
028import org.apache.http.impl.client.HttpClients;
029import org.apache.marmotta.ldclient.api.endpoint.Endpoint;
030import org.apache.marmotta.ldclient.api.provider.DataProvider;
031import org.apache.marmotta.ldclient.endpoint.rdf.LinkedDataEndpoint;
032import org.apache.marmotta.ldclient.model.ClientConfiguration;
033import org.apache.marmotta.ldclient.provider.rdf.CacheProvider;
034import org.apache.marmotta.ldclient.provider.rdf.LinkedDataProvider;
035import org.apache.marmotta.ldclient.provider.rdf.RegexUriProvider;
036import org.apache.marmotta.ldclient.provider.rdf.SPARQLProvider;
037
038/**
039 * A convenience factory for creating a ClientConfiguration object
040 * @author acoburn
041 * @since Aug 5, 2016
042 */
043public class ClientFactory {
044
045    /**
046     * Configure a linked data client suitable for use with a Fedora Repository.
047     * @param fedoraEndpoint a FedoraEndpoint configuration
048     * @return a configuration for use with an LDClient
049     */
050    public static ClientConfiguration createClient(final Endpoint fedoraEndpoint) {
051        return createClient(null, null, fedoraEndpoint, emptyList(), emptyList());
052    }
053
054    /**
055     * Configure a linked data client suitable for use with a Fedora Repository.
056     * @param fedoraEndpoint a FedoraEndpoint configuration
057     * @param endpoints additional endpoints to enable on the client
058     * @param providers additional providers to enable on the client
059     * @return a configuration for use with an LDClient
060     */
061    public static ClientConfiguration createClient(final Endpoint fedoraEndpoint, final List<Endpoint> endpoints,
062            final List<DataProvider> providers) {
063        return createClient(null, null, fedoraEndpoint, endpoints, providers);
064    }
065
066    /**
067     * Configure a linked data client suitable for use with a Fedora Repository.
068     * @param authScope the authentication scope
069     * @param credentials the credentials
070     * @param fedoraEndpoint a FedoraEndpoint configuration
071     * @return a configuration for use with an LDClient
072     */
073    public static ClientConfiguration createClient(final AuthScope authScope, final Credentials credentials,
074            final Endpoint fedoraEndpoint) {
075        return createClient(authScope, credentials, fedoraEndpoint, emptyList(), emptyList());
076    }
077
078    /**
079     * Create a linked data client suitable for use with a Fedora Repository.
080     * @param authScope the authentication scope
081     * @param credentials the credentials
082     * @param fedoraEndpoint a FedoraEndpoint configuration
083     * @param endpoints additional endpoints to enable on the client
084     * @param providers additional providers to enable on the client
085     * @return a configuration for use with an LDClient
086     */
087    public static ClientConfiguration createClient(final AuthScope authScope, final Credentials credentials,
088            final Endpoint fedoraEndpoint, final List<Endpoint> endpoints, final List<DataProvider> providers) {
089
090        final ClientConfiguration client = new ClientConfiguration();
091
092        if (credentials != null && authScope != null) {
093            final CredentialsProvider credsProvider = new BasicCredentialsProvider();
094            credsProvider.setCredentials(authScope, credentials);
095            client.setHttpClient(HttpClients.custom()
096                    .setDefaultCredentialsProvider(credsProvider)
097                    .useSystemProperties().build());
098        }
099        client.addEndpoint(fedoraEndpoint);
100
101        // manually add default Providers and Endpoints
102        client.addProvider(new LinkedDataProvider());
103        client.addProvider(new CacheProvider());
104        client.addProvider(new RegexUriProvider());
105        client.addProvider(new SPARQLProvider());
106        client.addEndpoint(new LinkedDataEndpoint());
107
108        // add any injected endpoints/providers
109        endpoints.forEach(client::addEndpoint);
110        providers.forEach(client::addProvider);
111
112        return client;
113    }
114
115    private ClientFactory() {
116        // prevent instantiation
117    }
118
119}