Author: rvesse
Date: Mon Jul 1 20:51:59 2013
New Revision: 1498680
URL: http://svn.apache.org/r1498680
Log:
Add authentication support to DatasetGraphAccessorHTTP, start adding some tests
for this (JENA-480)
Modified:
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/query/DatasetAccessorFactory.java
jena/trunk/jena-arq/src/main/java/org/apache/jena/web/DatasetGraphAccessorHTTP.java
jena/trunk/jena-fuseki/src/test/java/org/apache/jena/fuseki/TestAuth.java
jena/trunk/jena-text/pom.xml
Modified:
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/query/DatasetAccessorFactory.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/query/DatasetAccessorFactory.java?rev=1498680&r1=1498679&r2=1498680&view=diff
==============================================================================
---
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/query/DatasetAccessorFactory.java
(original)
+++
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/query/DatasetAccessorFactory.java
Mon Jul 1 20:51:59 2013
@@ -18,6 +18,7 @@
package com.hp.hpl.jena.query;
+import org.apache.jena.atlas.web.auth.HttpAuthenticator;
import org.apache.jena.web.DatasetAdapter ;
import org.apache.jena.web.DatasetGraphAccessor ;
import org.apache.jena.web.DatasetGraphAccessorBasic ;
@@ -25,23 +26,58 @@ import org.apache.jena.web.DatasetGraphA
import com.hp.hpl.jena.sparql.core.DatasetGraph ;
+/**
+ * Factory which produces dataset accessors
+ *
+ */
public class DatasetAccessorFactory
{
+ /**
+ * Create an accessor for a remote HTTP service
+ * @param serviceURI Service URI
+ * @return Accessor
+ */
public static DatasetAccessor createHTTP(String serviceURI)
{
return adapt(new DatasetGraphAccessorHTTP(serviceURI)) ;
}
+
+ /**
+ * Create an accessor for a remote HTTP service that requires
authentication
+ * @param serviceURI Service URI
+ * @param authenticator HTTP authenticator
+ * @return Accessor
+ */
+ public static DatasetAccessor createHTTP(String serviceURI,
HttpAuthenticator authenticator)
+ {
+ return adapt(new DatasetGraphAccessorHTTP(serviceURI, authenticator));
+ }
+ /**
+ * Create an accessor for a local dataset
+ * @param dataset Dataset
+ * @return Accessor
+ */
public static DatasetAccessor create(DatasetGraph dataset)
{
return adapt(new DatasetGraphAccessorBasic(dataset)) ;
}
+ /**
+ * Create an accessor for a local dataset
+ * @param dataset Dataset
+ * @return Accessor
+ */
public static DatasetAccessor create(Dataset dataset)
{
return adapt(new DatasetGraphAccessorBasic(dataset.asDatasetGraph())) ;
}
+ /**
+ * Makes an graph level accessor over a local dataset
+ * @param dataset Dataset
+ * @return Accessor
+ */
public static DatasetGraphAccessor make(DatasetGraph dataset)
{
return new DatasetGraphAccessorBasic(dataset) ;
Modified:
jena/trunk/jena-arq/src/main/java/org/apache/jena/web/DatasetGraphAccessorHTTP.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/web/DatasetGraphAccessorHTTP.java?rev=1498680&r1=1498679&r2=1498680&view=diff
==============================================================================
---
jena/trunk/jena-arq/src/main/java/org/apache/jena/web/DatasetGraphAccessorHTTP.java
(original)
+++
jena/trunk/jena-arq/src/main/java/org/apache/jena/web/DatasetGraphAccessorHTTP.java
Mon Jul 1 20:51:59 2013
@@ -21,9 +21,7 @@ package org.apache.jena.web;
import java.io.IOException ;
import java.io.OutputStream ;
-import org.apache.http.Header ;
import org.apache.http.HttpEntity ;
-import org.apache.http.HttpResponse ;
import org.apache.http.HttpVersion ;
import org.apache.http.client.methods.HttpHead ;
import org.apache.http.client.methods.HttpUriRequest ;
@@ -34,7 +32,8 @@ import org.apache.http.params.HttpConnec
import org.apache.http.params.HttpParams ;
import org.apache.http.params.HttpProtocolParams ;
import org.apache.jena.atlas.web.HttpException ;
-import org.apache.jena.atlas.web.TypedInputStream ;
+import org.apache.jena.atlas.web.auth.HttpAuthenticator;
+import org.apache.jena.atlas.web.auth.SimpleAuthenticator;
import org.apache.jena.riot.* ;
import org.apache.jena.riot.system.IRILib ;
import org.apache.jena.riot.web.* ;
@@ -44,22 +43,55 @@ import com.hp.hpl.jena.graph.Graph ;
import com.hp.hpl.jena.graph.Node ;
import com.hp.hpl.jena.shared.JenaException ;
-// TODO Support use of a HttpAuthenticator
-
+/**
+ * A dataset graph accessor that talks to stores that implement the SPARQL 1.1
Graph Store Protocol
+ *
+ */
public class DatasetGraphAccessorHTTP implements DatasetGraphAccessor
{
// Test for this class are in Fuseki so they can be run with a server.
private final String remote ;
private static final HttpResponseHandler noResponse =
HttpResponseLib.nullResponse ;
+ private HttpAuthenticator authenticator;
/** Format used to send a graph to the server */
private static RDFFormat sendLang = RDFFormat.RDFXML_PLAIN ;
- /** Create a DatasetUpdater for the remote URL */
+ /**
+ * Create a DatasetUpdater for the remote URL
+ * @param remote Remote URL
+ */
public DatasetGraphAccessorHTTP(String remote)
{
this.remote = remote ;
+ }
+
+ /**
+ * Create a DatasetUpdater for the remote URL
+ * @param remote Remote URL
+ * @param authenticator HTTP Authenticator
+ */
+ public DatasetGraphAccessorHTTP(String remote, HttpAuthenticator
authenticator) {
+ this(remote);
+ this.setAuthenticator(authenticator);
+ }
+
+ /**
+ * Sets authentication credentials for the remote URL
+ * @param username User name
+ * @param password Password
+ */
+ public void setAuthentication(String username, char[] password) {
+ this.setAuthenticator(new SimpleAuthenticator(username, password));
+ }
+
+ /**
+ * Sets an authenticator to use for authentication to the remote URL
+ * @param authenticator Authenticator
+ */
+ public void setAuthenticator(HttpAuthenticator authenticator) {
+ this.authenticator = authenticator;
}
@Override
@@ -72,7 +104,7 @@ public class DatasetGraphAccessorHTTP im
{
HttpCaptureResponse<Graph> graph = HttpResponseLib.graphHandler() ;
try {
- HttpOp.execHttpGet(url, WebContent.defaultGraphAcceptHeader,
graph) ;
+ HttpOp.execHttpGet(url, WebContent.defaultGraphAcceptHeader,
graph, this.authenticator) ;
} catch (HttpException ex) {
if ( ex.getResponseCode() == HttpSC.NOT_FOUND_404 )
return null ;
@@ -95,9 +127,10 @@ public class DatasetGraphAccessorHTTP im
private boolean doHead(String url)
{
+ // TODO Shouldn't this use HttpOp.execHttpHead() ?
HttpUriRequest httpHead = new HttpHead(url) ;
try {
- HttpOp.execHttpGet(url, WebContent.defaultGraphAcceptHeader,
noResponse) ;
+ HttpOp.execHttpGet(url, WebContent.defaultGraphAcceptHeader,
noResponse, this.authenticator) ;
return true ;
} catch (HttpException ex) {
if ( ex.getResponseCode() == HttpSC.NOT_FOUND_404 )
@@ -115,7 +148,7 @@ public class DatasetGraphAccessorHTTP im
private void doPut(String url, Graph data)
{
HttpEntity entity = graphToHttpEntity(data) ;
- HttpOp.execHttpPut(url, entity) ;
+ HttpOp.execHttpPut(url, entity, null, null, this.authenticator) ;
}
@Override
@@ -127,7 +160,7 @@ public class DatasetGraphAccessorHTTP im
private void doDelete(String url)
{
try {
- HttpOp.execHttpDelete(url, noResponse) ;
+ HttpOp.execHttpDelete(url, noResponse, null, null,
this.authenticator) ;
} catch (HttpException ex) {
if ( ex.getResponseCode() == HttpSC.NOT_FOUND_404 )
return ;
@@ -143,7 +176,7 @@ public class DatasetGraphAccessorHTTP im
private void doPost(String url, Graph data)
{
HttpEntity entity = graphToHttpEntity(data) ;
- HttpOp.execHttpPost(url, entity) ;
+ HttpOp.execHttpPost(url, entity, null, null, null, null,
this.authenticator) ;
}
@Override
@@ -165,7 +198,9 @@ public class DatasetGraphAccessorHTTP im
// Encode
guri = IRILib.encodeUriComponent(guri) ;
return remote+"?"+HttpNames.paramGraph+"="+guri ;
- }
+ }
+
+ // TODO: Move default parameters into HttpOp and use in ensureClient()
static private HttpParams httpParams = createHttpParams() ;
@@ -182,14 +217,6 @@ public class DatasetGraphAccessorHTTP im
return httpParams$;
}
- private static String getHeader(HttpResponse response, String headerName)
- {
- Header h = response.getLastHeader(headerName) ;
- if ( h == null )
- return null ;
- return h.getValue() ;
- }
-
private static HttpEntity graphToHttpEntity(final Graph graph) {
ContentProducer producer = new ContentProducer() {
@@ -203,17 +230,5 @@ public class DatasetGraphAccessorHTTP im
String ct = sendLang.getLang().getContentType().getContentType() ;
entity.setContentType(ct) ;
return entity ;
- }
-
- private void readGraph(Graph graph, TypedInputStream ts, String base)
- {
- // Yes - we ignore the charset.
- // Either it's XML and so the XML parser deals with it, or the
- // language determines the charset and the parsers offer InputStreams.
-
- Lang lang = WebContent.contentTypeToLang(ts.getContentType()) ;
- if ( lang == null )
- throw new RiotException("Unknown lang for "+ts.getMediaType()) ;
- RDFDataMgr.read(graph, ts, lang) ;
}
}
Modified:
jena/trunk/jena-fuseki/src/test/java/org/apache/jena/fuseki/TestAuth.java
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-fuseki/src/test/java/org/apache/jena/fuseki/TestAuth.java?rev=1498680&r1=1498679&r2=1498680&view=diff
==============================================================================
--- jena/trunk/jena-fuseki/src/test/java/org/apache/jena/fuseki/TestAuth.java
(original)
+++ jena/trunk/jena-fuseki/src/test/java/org/apache/jena/fuseki/TestAuth.java
Mon Jul 1 20:51:59 2013
@@ -31,16 +31,21 @@ import org.apache.jena.atlas.web.HttpExc
import org.apache.jena.atlas.web.auth.PreemptiveBasicAuthenticator ;
import org.apache.jena.atlas.web.auth.ScopedAuthenticator ;
import org.apache.jena.atlas.web.auth.ServiceAuthenticator ;
+import org.apache.jena.atlas.web.auth.SimpleAuthenticator;
import org.apache.jena.fuseki.server.FusekiConfig ;
import org.apache.jena.fuseki.server.SPARQLServer ;
import org.apache.jena.fuseki.server.ServerConfig ;
import org.junit.AfterClass ;
import org.junit.Assert ;
import org.junit.BeforeClass ;
+import org.junit.Ignore;
import org.junit.Test ;
import com.hp.hpl.jena.query.ARQ ;
+import com.hp.hpl.jena.query.DatasetAccessor;
+import com.hp.hpl.jena.query.DatasetAccessorFactory;
import com.hp.hpl.jena.query.QueryExecutionFactory ;
+import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.sparql.core.DatasetGraph ;
import com.hp.hpl.jena.sparql.core.DatasetGraphFactory ;
import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP ;
@@ -55,11 +60,16 @@ import com.hp.hpl.jena.update.UpdateRequ
/**
* Tests Fuseki operation with authentication enabled
*/
+@SuppressWarnings("javadoc")
public class TestAuth extends ServerTest {
private static File realmFile;
private static SPARQLServer server;
+ /**
+ * Sets up the authentication for tests
+ * @throws IOException
+ */
@BeforeClass
public static void setup() throws IOException {
realmFile = File.createTempFile("realm", ".properties");
@@ -84,6 +94,9 @@ public class TestAuth extends ServerTest
server.start();
}
+ /**
+ * Tears down authentication test setup
+ */
@AfterClass
public static void teardown() {
server.stop();
@@ -325,4 +338,43 @@ public class TestAuth extends ServerTest
ue.setAuthenticator(new PreemptiveBasicAuthenticator(authenticator));
ue.execute();
}
+
+ @Test(expected = HttpException.class)
+ public void graphstore_with_auth_01() throws URISyntaxException {
+ // No auth credentials
+ DatasetAccessor accessor =
DatasetAccessorFactory.createHTTP(serviceREST);
+ accessor.getModel();
+ }
+
+ @Test(expected = HttpException.class)
+ public void graphstore_with_auth_02() throws URISyntaxException {
+ // Incorrect auth credentials
+ DatasetAccessor accessor =
DatasetAccessorFactory.createHTTP(serviceREST, new
SimpleAuthenticator("allowed", "incorrect".toCharArray()));
+ accessor.getModel();
+ }
+
+ @Test
+ public void graphstore_with_auth_03() throws URISyntaxException {
+ // Correct auth credentials
+ DatasetAccessor accessor =
DatasetAccessorFactory.createHTTP(serviceREST, new
SimpleAuthenticator("allowed", "password".toCharArray()));
+ Model m = accessor.getModel();
+ Assert.assertTrue(m.isEmpty());
+ }
+
+ @Test(expected = HttpException.class)
+ public void graphstore_with_auth_04() throws URISyntaxException {
+ // Correct auth credentials scoped to wrong URI
+ DatasetAccessor accessor =
DatasetAccessorFactory.createHTTP(serviceREST, new ScopedAuthenticator(new
URI("http://example.org/"), "allowed", "password".toCharArray()));
+ accessor.getModel();
+ }
+
+ //TODO Currently broken because scoped authenticators aren't taking into
account derived URIs which seems like a sensible enhancement
+
+ @Test
+ @Ignore
+ public void graphstore_with_auth_05() throws URISyntaxException {
+ // Correct auth credentials scoped to correct URI
+ DatasetAccessor accessor =
DatasetAccessorFactory.createHTTP(serviceREST, new ScopedAuthenticator(new
URI(serviceREST), "allowed", "password".toCharArray()));
+ accessor.getModel();
+ }
}
Modified: jena/trunk/jena-text/pom.xml
URL:
http://svn.apache.org/viewvc/jena/trunk/jena-text/pom.xml?rev=1498680&r1=1498679&r2=1498680&view=diff
==============================================================================
--- jena/trunk/jena-text/pom.xml (original)
+++ jena/trunk/jena-text/pom.xml Mon Jul 1 20:51:59 2013
@@ -232,7 +232,15 @@
</plugin>
</plugins>
-
+
</build>
+
+ <repositories>
+ <repository>
+ <id>maven-restlet</id>
+ <name>Public online Restlet repository</name>
+ <url>http://maven.restlet.org</url>
+</repository>
+ </repositories>
</project>