From ada8bbf576f33a18f01e690375ad50cec30deb6a Mon Sep 17 00:00:00 2001 From: Martynas Date: Wed, 12 Oct 2016 23:39:47 +0200 Subject: [PATCH] GraphStoreProvider/SPARQLClientProvider --- .../java/com/atomgraph/core/Application.java | 20 ++- .../com/atomgraph/core/model/Service.java | 4 +- .../core/model/impl/ApplicationImpl.java | 1 + .../core/model/impl/GraphStoreProxyBase.java | 12 +- .../core/model/impl/QueriedResourceBase.java | 25 ++-- .../model/impl/SPARQLEndpointProxyBase.java | 25 +--- .../core/model/impl/ServiceImpl.java | 29 ---- .../provider/GraphStoreClientProvider.java | 118 ++++++++++++++++ .../core/provider/GraphStoreProvider.java | 108 +++++++++++++++ .../core/provider/SPARQLClientProvider.java | 131 ++++++++++++++++++ .../core/provider/SPARQLEndpointProvider.java | 107 ++++++++++++++ 11 files changed, 505 insertions(+), 75 deletions(-) create mode 100644 src/main/java/com/atomgraph/core/provider/GraphStoreClientProvider.java create mode 100644 src/main/java/com/atomgraph/core/provider/GraphStoreProvider.java create mode 100644 src/main/java/com/atomgraph/core/provider/SPARQLClientProvider.java create mode 100644 src/main/java/com/atomgraph/core/provider/SPARQLEndpointProvider.java diff --git a/src/main/java/com/atomgraph/core/Application.java b/src/main/java/com/atomgraph/core/Application.java index 9be7453a..c1efa007 100644 --- a/src/main/java/com/atomgraph/core/Application.java +++ b/src/main/java/com/atomgraph/core/Application.java @@ -35,9 +35,14 @@ import com.atomgraph.core.provider.ApplicationProvider; import com.atomgraph.core.provider.ClientProvider; import com.atomgraph.core.provider.DatasetProvider; +import com.atomgraph.core.provider.GraphStoreClientProvider; +import com.atomgraph.core.provider.GraphStoreProvider; import com.atomgraph.core.provider.MediaTypesProvider; +import com.atomgraph.core.provider.SPARQLClientProvider; +import com.atomgraph.core.provider.SPARQLEndpointProvider; import com.atomgraph.core.riot.RDFLanguages; import com.atomgraph.core.riot.lang.RDFPostReaderFactory; +import javax.annotation.PostConstruct; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -73,8 +78,11 @@ public Application(@Context ServletConfig servletConfig) // add RDF/POST serialization RDFLanguages.register(RDFLanguages.RDFPOST); RDFParserRegistry.registerLangTriples(RDFLanguages.RDFPOST, new RDFPostReaderFactory()); - //IO_Jena.registerForModelRead(RDFLanguages.strLangRDFPOST, RDFPostReaderAdapter.class); - + } + + @PostConstruct + public void init() + { classes.add(QueriedResourceBase.class); // handles all classes.add(SPARQLEndpointProxyBase.class); // handles /sparql queries classes.add(GraphStoreProxyBase.class); // handles /service requests @@ -85,7 +93,11 @@ public Application(@Context ServletConfig servletConfig) singletons.add(new QueryParamProvider()); singletons.add(new UpdateRequestReader()); singletons.add(new DataManagerProvider()); - singletons.add(new ApplicationProvider(servletConfig)); + singletons.add(new ApplicationProvider(getServletConfig())); + singletons.add(new SPARQLClientProvider(getServletConfig())); + singletons.add(new SPARQLEndpointProvider(getServletConfig())); + singletons.add(new GraphStoreClientProvider()); + singletons.add(new GraphStoreProvider(getServletConfig())); singletons.add(new ClientProvider()); singletons.add(new MediaTypesProvider()); singletons.add(new ClientExceptionMapper()); @@ -135,7 +147,7 @@ public boolean getBooleanParam(ServletConfig servletConfig, Property property) boolean value = false; if (servletConfig.getInitParameter(property.getURI()) != null) - value = Boolean.parseBoolean(servletConfig.getInitParameter(property.getURI()).toString()); + value = Boolean.parseBoolean(servletConfig.getInitParameter(property.getURI())); return value; } diff --git a/src/main/java/com/atomgraph/core/model/Service.java b/src/main/java/com/atomgraph/core/model/Service.java index 18f082a1..cf0b4ba5 100644 --- a/src/main/java/com/atomgraph/core/model/Service.java +++ b/src/main/java/com/atomgraph/core/model/Service.java @@ -27,11 +27,11 @@ public interface Service org.apache.jena.rdf.model.Resource getSPARQLEndpoint(); - WebResource getSPARQLEndpointOrigin(Client client); + // WebResource getSPARQLEndpointOrigin(Client client); org.apache.jena.rdf.model.Resource getGraphStore(); - WebResource getGraphStoreOrigin(Client client); + // WebResource getGraphStoreOrigin(Client client); String getAuthUser(); diff --git a/src/main/java/com/atomgraph/core/model/impl/ApplicationImpl.java b/src/main/java/com/atomgraph/core/model/impl/ApplicationImpl.java index 5321aefe..5faf77db 100644 --- a/src/main/java/com/atomgraph/core/model/impl/ApplicationImpl.java +++ b/src/main/java/com/atomgraph/core/model/impl/ApplicationImpl.java @@ -29,6 +29,7 @@ public class ApplicationImpl implements Application public ApplicationImpl(Service service) { + if (service == null) throw new IllegalArgumentException("Service cannot be null"); this.service = service; } diff --git a/src/main/java/com/atomgraph/core/model/impl/GraphStoreProxyBase.java b/src/main/java/com/atomgraph/core/model/impl/GraphStoreProxyBase.java index a812d685..21371521 100644 --- a/src/main/java/com/atomgraph/core/model/impl/GraphStoreProxyBase.java +++ b/src/main/java/com/atomgraph/core/model/impl/GraphStoreProxyBase.java @@ -24,9 +24,7 @@ import javax.ws.rs.core.Request; import com.atomgraph.core.MediaTypes; import com.atomgraph.core.client.GraphStoreClient; -import com.atomgraph.core.model.Application; import com.atomgraph.core.model.GraphStoreProxy; -import com.sun.jersey.api.client.Client; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,9 +39,7 @@ public class GraphStoreProxyBase extends GraphStoreBase implements GraphStorePro { private static final Logger log = LoggerFactory.getLogger(GraphStoreProxyBase.class); - //private final GraphStoreOrigin origin; private final GraphStoreClient graphStoreClient; - //private final javax.ws.rs.core.MediaType[] readableMediaTypes; /** * Constructs Graph Store proxy from request metadata and origin. @@ -51,15 +47,13 @@ public class GraphStoreProxyBase extends GraphStoreBase implements GraphStorePro * @param request request * @param servletConfig servlet config * @param mediaTypes supported media types - * @param client HTTP client - * @param application LDT application + * @param graphStoreClient graph store client */ public GraphStoreProxyBase(@Context Request request, @Context ServletConfig servletConfig, @Context MediaTypes mediaTypes, - @Context Client client, @Context Application application) + @Context GraphStoreClient graphStoreClient) { super(request, servletConfig, mediaTypes); - if (application == null) throw new IllegalArgumentException("Application cannot be null"); - graphStoreClient = GraphStoreClient.create(application.getService().getSPARQLEndpointOrigin(client), mediaTypes); + this.graphStoreClient = graphStoreClient; } @Override diff --git a/src/main/java/com/atomgraph/core/model/impl/QueriedResourceBase.java b/src/main/java/com/atomgraph/core/model/impl/QueriedResourceBase.java index fcbed104..269fc2c2 100644 --- a/src/main/java/com/atomgraph/core/model/impl/QueriedResourceBase.java +++ b/src/main/java/com/atomgraph/core/model/impl/QueriedResourceBase.java @@ -31,9 +31,7 @@ import com.atomgraph.core.MediaTypes; import com.atomgraph.core.client.SPARQLClient; import com.atomgraph.core.exception.NotFoundException; -import com.atomgraph.core.model.Application; import com.atomgraph.core.model.QueriedResource; -import com.sun.jersey.api.client.Client; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,8 +47,8 @@ public class QueriedResourceBase extends ResourceBase implements QueriedResource { private static final Logger log = LoggerFactory.getLogger(QueriedResourceBase.class); - private final Client client; - private final Application application; + //private final Client client; + //private final Application application; private final SPARQLClient sparqlClient; /** @@ -60,21 +58,20 @@ public class QueriedResourceBase extends ResourceBase implements QueriedResource * @param uriInfo URI information of the request * @param request current request object * @param servletConfig webapp context - * @param application LDT application - * @param client * @param mediaTypes supported media types + * @param sparqlClient SPARQL client * @see JAX-RS UriInfo * @see ServletContext * @see Jersey ResourceContext */ public QueriedResourceBase(@Context UriInfo uriInfo, @Context Request request, @Context ServletConfig servletConfig, @Context MediaTypes mediaTypes, - @Context Client client, @Context Application application) + @Context SPARQLClient sparqlClient) { super(uriInfo, request, servletConfig, mediaTypes); - if (application == null) throw new IllegalArgumentException("Application cannot be null"); - this.client = client; - this.application = application; - this.sparqlClient = SPARQLClient.create(application.getService().getSPARQLEndpointOrigin(client)); + if (sparqlClient == null) throw new IllegalArgumentException("SPARQLClient cannot be null"); + //this.client = client; + //this.application = application; + this.sparqlClient = sparqlClient; } /** @@ -174,19 +171,23 @@ public Query getQuery(URI uri) return QueryFactory.create("DESCRIBE <" + uri.toString() + ">"); } + /* public Client getClient() { return client; } + */ public SPARQLClient getSPARQLClient() { return sparqlClient; } + /* public Application getApplication() { return application; } - + */ + } \ No newline at end of file diff --git a/src/main/java/com/atomgraph/core/model/impl/SPARQLEndpointProxyBase.java b/src/main/java/com/atomgraph/core/model/impl/SPARQLEndpointProxyBase.java index 3aa7ef63..be450c91 100644 --- a/src/main/java/com/atomgraph/core/model/impl/SPARQLEndpointProxyBase.java +++ b/src/main/java/com/atomgraph/core/model/impl/SPARQLEndpointProxyBase.java @@ -46,7 +46,7 @@ public class SPARQLEndpointProxyBase extends SPARQLEndpointBase implements SPARQ { private static final Logger log = LoggerFactory.getLogger(SPARQLEndpointProxyBase.class); - private final Application application; + //private final Application application; private final SPARQLClient sparqlClient; /** @@ -59,15 +59,11 @@ public class SPARQLEndpointProxyBase extends SPARQLEndpointBase implements SPARQ * @param application LDT application */ public SPARQLEndpointProxyBase(@Context Request request, @Context ServletConfig servletConfig, @Context MediaTypes mediaTypes, - @Context Client client, @Context Application application) + @Context SPARQLClient sparqlClient) { super(request, servletConfig, mediaTypes); - if (application == null) throw new IllegalArgumentException("Application cannot be null"); - this.application = application; - - Integer maxGetRequestSize = getMaxGetRequestSize(servletConfig, A.maxGetRequestSize); - if (maxGetRequestSize != null) sparqlClient = SPARQLClient.create(application.getService().getSPARQLEndpointOrigin(client), mediaTypes, maxGetRequestSize); - else sparqlClient = SPARQLClient.create(application.getService().getSPARQLEndpointOrigin(client), mediaTypes); + if (sparqlClient == null) throw new IllegalArgumentException("Application cannot be null"); + this.sparqlClient = sparqlClient; } @Override @@ -76,10 +72,12 @@ public SPARQLClient getSPARQLClient() return sparqlClient; } + /* public Application getApplication() { return application; } + */ @Override public Model loadModel(Query query) @@ -113,15 +111,4 @@ public void update(UpdateRequest updateRequest) getSPARQLClient().update(updateRequest); } - public final Integer getMaxGetRequestSize(ServletConfig servletConfig, DatatypeProperty property) - { - if (servletConfig == null) throw new IllegalArgumentException("ServletConfig cannot be null"); - if (property == null) throw new IllegalArgumentException("Property cannot be null"); - - Object sizeValue = servletConfig.getInitParameter(property.getURI()); - if (sizeValue != null) return Integer.parseInt(sizeValue.toString()); - - return null; - } - } \ No newline at end of file diff --git a/src/main/java/com/atomgraph/core/model/impl/ServiceImpl.java b/src/main/java/com/atomgraph/core/model/impl/ServiceImpl.java index b9d42eb2..42653395 100644 --- a/src/main/java/com/atomgraph/core/model/impl/ServiceImpl.java +++ b/src/main/java/com/atomgraph/core/model/impl/ServiceImpl.java @@ -16,9 +16,6 @@ package com.atomgraph.core.model.impl; import com.atomgraph.core.model.Service; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; import org.apache.jena.rdf.model.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,19 +53,6 @@ public Resource getSPARQLEndpoint() { return sparqlEndpoint; } - - @Override - public WebResource getSPARQLEndpointOrigin(Client client) - { - if (client == null) throw new IllegalArgumentException("Client must be not null"); - - WebResource origin = client.resource(getSPARQLEndpoint().getURI()); - - if (getAuthUser() != null && getAuthPwd() != null) - origin.addFilter(new HTTPBasicAuthFilter(getAuthUser(), getAuthPwd())); - - return origin; - } @Override public Resource getGraphStore() @@ -76,19 +60,6 @@ public Resource getGraphStore() return graphStore; } - @Override - public WebResource getGraphStoreOrigin(Client client) - { - if (client == null) throw new IllegalArgumentException("Client must be not null"); - - WebResource origin = client.resource(getGraphStore().getURI()); - - if (getAuthUser() != null && getAuthPwd() != null) - origin.addFilter(new HTTPBasicAuthFilter(getAuthUser(), getAuthPwd())); - - return origin; - } - @Override public String getAuthUser() { diff --git a/src/main/java/com/atomgraph/core/provider/GraphStoreClientProvider.java b/src/main/java/com/atomgraph/core/provider/GraphStoreClientProvider.java new file mode 100644 index 00000000..dba0006e --- /dev/null +++ b/src/main/java/com/atomgraph/core/provider/GraphStoreClientProvider.java @@ -0,0 +1,118 @@ +/* + * Copyright 2016 Martynas Jusevičius . + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.atomgraph.core.provider; + +import com.atomgraph.core.MediaTypes; +import com.atomgraph.core.client.GraphStoreClient; +import com.atomgraph.core.model.Application; +import com.atomgraph.core.model.Service; +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; +import com.sun.jersey.core.spi.component.ComponentContext; +import com.sun.jersey.spi.inject.Injectable; +import com.sun.jersey.spi.inject.PerRequestTypeInjectableProvider; +import javax.ws.rs.core.Context; +import javax.ws.rs.ext.ContextResolver; +import javax.ws.rs.ext.Provider; +import javax.ws.rs.ext.Providers; + +/** + * + * @author Martynas Jusevičius + */ +@Provider +public class GraphStoreClientProvider extends PerRequestTypeInjectableProvider implements ContextResolver +{ + + @Context Providers providers; + + public GraphStoreClientProvider() + { + super(GraphStoreClient.class); + } + + @Override + public Injectable getInjectable(ComponentContext cc, Context context) + { + return new Injectable() + { + @Override + public GraphStoreClient getValue() + { + return getGraphStoreClient(); + } + }; + } + + @Override + public GraphStoreClient getContext(Class type) + { + return getGraphStoreClient(); + } + + public GraphStoreClient getGraphStoreClient() + { + return getGraphStoreClient(getApplication().getService(), getClient()); + } + + public GraphStoreClient getGraphStoreClient(Service service, Client client) + { + if (service == null) throw new IllegalArgumentException("Service must be not null"); + if (client == null) throw new IllegalArgumentException("Client must be not null"); + + return GraphStoreClient.create(getOrigin(service, client), getMediaTypes()); + } + + public MediaTypes getMediaTypes() + { + return getProviders().getContextResolver(MediaTypes.class, null).getContext(MediaTypes.class); + } + + public Client getClient() + { + return getProviders().getContextResolver(Client.class, null).getContext(Client.class); + } + + public Application getApplication() + { + return getProviders().getContextResolver(Application.class, null).getContext(Application.class); + } + + public WebResource getOrigin(Service service) + { + return getOrigin(service, getClient()); + } + + public WebResource getOrigin(Service service, Client client) + { + if (service == null) throw new IllegalArgumentException("Service must be not null"); + if (client == null) throw new IllegalArgumentException("Client must be not null"); + + WebResource origin = client.resource(service.getGraphStore().getURI()); + + if (service.getAuthUser() != null && service.getAuthPwd() != null) + origin.addFilter(new HTTPBasicAuthFilter(service.getAuthUser(), service.getAuthPwd())); + + return origin; + } + + public Providers getProviders() + { + return providers; + } + +} diff --git a/src/main/java/com/atomgraph/core/provider/GraphStoreProvider.java b/src/main/java/com/atomgraph/core/provider/GraphStoreProvider.java new file mode 100644 index 00000000..9de42afd --- /dev/null +++ b/src/main/java/com/atomgraph/core/provider/GraphStoreProvider.java @@ -0,0 +1,108 @@ +/** + * Copyright 2014 Martynas Jusevičius + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.atomgraph.core.provider; + +import com.sun.jersey.core.spi.component.ComponentContext; +import com.sun.jersey.spi.inject.Injectable; +import com.sun.jersey.spi.inject.PerRequestTypeInjectableProvider; +import javax.servlet.ServletConfig; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Request; +import javax.ws.rs.ext.ContextResolver; +import javax.ws.rs.ext.Provider; +import javax.ws.rs.ext.Providers; +import com.atomgraph.core.MediaTypes; +import com.atomgraph.core.client.GraphStoreClient; +import com.atomgraph.core.model.GraphStore; +import com.atomgraph.core.model.impl.GraphStoreProxyBase; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * JAX-RS provider for Graph Store. + * Needs to be registered in the application. + * + * @author Martynas Jusevičius + * @see com.atomgraph.core.model.GraphStore + */ +@Provider +public class GraphStoreProvider extends PerRequestTypeInjectableProvider implements ContextResolver +{ + private static final Logger log = LoggerFactory.getLogger(GraphStoreProvider.class); + + @Context Request request; + @Context Providers providers; + + private final ServletConfig servletConfig; + + public GraphStoreProvider(ServletConfig servletConfig) + { + super(GraphStore.class); + this.servletConfig = servletConfig; + } + + public Request getRequest() + { + return request; + } + + public ServletConfig getServletConfig() + { + return servletConfig; + } + + public Providers getProviders() + { + return providers; + } + + public MediaTypes getMediaTypes() + { + return getProviders().getContextResolver(MediaTypes.class, null).getContext(MediaTypes.class); + } + + public GraphStoreClient getGraphStoreClient() + { + return getProviders().getContextResolver(GraphStoreClient.class, null).getContext(GraphStoreClient.class); + } + + @Override + public Injectable getInjectable(ComponentContext cc, Context context) + { + return new Injectable() + { + @Override + public GraphStore getValue() + { + return getGraphStore(); + } + }; + } + + @Override + public GraphStore getContext(Class type) + { + return getGraphStore(); + } + + public GraphStore getGraphStore() + { + return new GraphStoreProxyBase(getRequest(), getServletConfig(), getMediaTypes(), getGraphStoreClient()); + } + +} \ No newline at end of file diff --git a/src/main/java/com/atomgraph/core/provider/SPARQLClientProvider.java b/src/main/java/com/atomgraph/core/provider/SPARQLClientProvider.java new file mode 100644 index 00000000..3a199832 --- /dev/null +++ b/src/main/java/com/atomgraph/core/provider/SPARQLClientProvider.java @@ -0,0 +1,131 @@ +/* + * Copyright 2016 Martynas Jusevičius . + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.atomgraph.core.provider; + +import com.atomgraph.core.MediaTypes; +import com.atomgraph.core.client.SPARQLClient; +import com.atomgraph.core.model.Application; +import com.atomgraph.core.model.Service; +import com.atomgraph.core.vocabulary.A; +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; +import com.sun.jersey.core.spi.component.ComponentContext; +import com.sun.jersey.spi.inject.Injectable; +import com.sun.jersey.spi.inject.PerRequestTypeInjectableProvider; +import javax.servlet.ServletConfig; +import javax.ws.rs.core.Context; +import javax.ws.rs.ext.ContextResolver; +import javax.ws.rs.ext.Provider; +import javax.ws.rs.ext.Providers; + +/** + * + * @author Martynas Jusevičius + */ +@Provider +public class SPARQLClientProvider extends PerRequestTypeInjectableProvider implements ContextResolver +{ + @Context Providers providers; + + private final Integer maxGetRequestSize; + + public SPARQLClientProvider(ServletConfig servletConfig) + { + super(SPARQLClient.class); + + Object sizeValue = servletConfig.getInitParameter(A.maxGetRequestSize.getURI()); + if (sizeValue != null) maxGetRequestSize = Integer.parseInt(sizeValue.toString()); + else maxGetRequestSize = null; + } + + @Override + public Injectable getInjectable(ComponentContext cc, Context context) + { + return new Injectable() + { + @Override + public SPARQLClient getValue() + { + return getSPARQLClient(); + } + }; + } + + @Override + public SPARQLClient getContext(Class type) + { + return getSPARQLClient(); + } + + public SPARQLClient getSPARQLClient() + { + return getSPARQLClient(getApplication().getService(), getClient()); + } + + public SPARQLClient getSPARQLClient(Service service, Client client) + { + if (service == null) throw new IllegalArgumentException("Service must be not null"); + if (client == null) throw new IllegalArgumentException("Client must be not null"); + + if (getMaxGetRequestSize() != null) return SPARQLClient.create(getOrigin(service, client), getMediaTypes(), getMaxGetRequestSize()); + else return SPARQLClient.create(getOrigin(service, client), getMediaTypes()); + } + + public MediaTypes getMediaTypes() + { + return getProviders().getContextResolver(MediaTypes.class, null).getContext(MediaTypes.class); + } + + public Client getClient() + { + return getProviders().getContextResolver(Client.class, null).getContext(Client.class); + } + + public Application getApplication() + { + return getProviders().getContextResolver(Application.class, null).getContext(Application.class); + } + + public WebResource getOrigin(Service service) + { + return getOrigin(service, getClient()); + } + + public WebResource getOrigin(Service service, Client client) + { + if (service == null) throw new IllegalArgumentException("Service must be not null"); + if (client == null) throw new IllegalArgumentException("Client must be not null"); + + WebResource origin = client.resource(service.getSPARQLEndpoint().getURI()); + + if (service.getAuthUser() != null && service.getAuthPwd() != null) + origin.addFilter(new HTTPBasicAuthFilter(service.getAuthUser(), service.getAuthPwd())); + + return origin; + } + + public Integer getMaxGetRequestSize() + { + return maxGetRequestSize; + } + + public Providers getProviders() + { + return providers; + } + +} diff --git a/src/main/java/com/atomgraph/core/provider/SPARQLEndpointProvider.java b/src/main/java/com/atomgraph/core/provider/SPARQLEndpointProvider.java new file mode 100644 index 00000000..cf564bf4 --- /dev/null +++ b/src/main/java/com/atomgraph/core/provider/SPARQLEndpointProvider.java @@ -0,0 +1,107 @@ +/** + * Copyright 2014 Martynas Jusevičius + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package com.atomgraph.core.provider; + +import com.sun.jersey.core.spi.component.ComponentContext; +import com.sun.jersey.spi.inject.Injectable; +import com.sun.jersey.spi.inject.PerRequestTypeInjectableProvider; +import javax.servlet.ServletConfig; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Request; +import javax.ws.rs.ext.ContextResolver; +import javax.ws.rs.ext.Provider; +import javax.ws.rs.ext.Providers; +import com.atomgraph.core.MediaTypes; +import com.atomgraph.core.client.SPARQLClient; +import com.atomgraph.core.model.SPARQLEndpoint; +import com.atomgraph.core.model.impl.SPARQLEndpointProxyBase; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * JAX-RS provider for SPARQL endpoint. + * Needs to be registered in the application. + * + * @author Martynas Jusevičius + * @see com.atomgraph.core.model.SPARQLEndpoint + */ +@Provider +public class SPARQLEndpointProvider extends PerRequestTypeInjectableProvider implements ContextResolver +{ + private static final Logger log = LoggerFactory.getLogger(SPARQLEndpointProvider.class); + + @Context Request request; + @Context Providers providers; + + private final ServletConfig servletConfig; + + public SPARQLEndpointProvider(ServletConfig servletConfig) + { + super(SPARQLEndpoint.class); + this.servletConfig = servletConfig; + } + + public Providers getProviders() + { + return providers; + } + + public Request getRequest() + { + return request; + } + + public ServletConfig getServletConfig() + { + return servletConfig; + } + + public MediaTypes getMediaTypes() + { + return getProviders().getContextResolver(MediaTypes.class, null).getContext(MediaTypes.class); + } + + public SPARQLClient getSPARQLClient() + { + return getProviders().getContextResolver(SPARQLClient.class, null).getContext(SPARQLClient.class); + } + + @Override + public Injectable getInjectable(ComponentContext cc, Context context) + { + return new Injectable() + { + @Override + public SPARQLEndpoint getValue() + { + return getSPARQLEndpoint(); + } + }; + } + + @Override + public SPARQLEndpoint getContext(Class type) + { + return getSPARQLEndpoint(); + } + + public SPARQLEndpoint getSPARQLEndpoint() + { + return new SPARQLEndpointProxyBase(getRequest(), getServletConfig(), getMediaTypes(), getSPARQLClient()); + } + +} \ No newline at end of file