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