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