You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Kevin Dreßler <kv...@gmail.com> on 2019/01/14 14:06:14 UTC
Fuseki 2: Serving RDF/XML over HTTP
Hello,
for unit testing I need to mock up a HTTP server delivering RDF/XML per content negotiation (Accept: application/rdf+xml). For a HTTP request on http://myEndpoint:myPort/test with the "Accept" header set as above it should output DESCRIBE(http://myEndpoint:myPort/test) as RDF/XML, so basically I want to mimic DBpedia. Is there a way to do this with a minimal amount of configuration inside my tests using FUSEKI 2?
Right now I am using the following code but I get the output only as TURTLE.
String EX = "http://localhost:32537/";
Model lookup = ModelFactory.createDefaultModel();
lookup.add(lookup.createResource(EX + "birch"),
lookup.createProperty(EX + "brinellHardness"),
lookup.createTypedLiteral(27));
FusekiServer server = FusekiServer.create()
.add("/birch", DatasetFactory.create(lookup))
.port(32537)
.build();
server.start();
Thanks in advance,
Kevin
Re: Fuseki 2: Serving RDF/XML over HTTP
Posted by Kevin Dreßler <kv...@gmail.com>.
Thanks Andy, that helped a lot indeed!
Best,
Kevin
> On 14. Jan 2019, at 17:13, Andy Seaborne <an...@apache.org> wrote:
>
> The problem is that you are asking the dataset for RDF/XML.
>
> Datasets include the named graphs. It needs TriG or N-Quads.
>
> So you ask for RDF/XML, and content negotiation says "can't - here's nmy default (which is N-Quads) and then the code ignores the Content-type and forces RDF/XML. It then fails to parse.
>
>
> This works for me - ask for the default graph:
>
> "http://localhost:"+PORT+"/birch?default"
>
> Below is that done twice - using RDFConnection and as in the example.
>
> Fuseki logging is turned on which can help debug conneg.
>
> Andy
>
> public static void main(String[] args) throws Exception {
>
> FusekiLogging.setLogging();
> int PORT = WebLib.choosePort();
>
> FusekiServer server = FusekiServer.create()
> .port(PORT)
> .verbose(true)
> .add("/birch", DatasetFactory.createTxnMem())
> .build();
> server.start();
> // Server startup.
> Lib.sleep(100);
>
> // Version 1
> try (RDFConnection conn = RDFConnectionRemote
> .create()
> .destination("http://localhost:"+PORT+"/birch")
> .acceptHeaderGraph("application/rdf+xml")
> .build()) {
> Model model = conn.fetch();
> RDFDataMgr.write(System.out, model, Lang.RDFXML);
> }
>
> // Version 2
> URL url = new URL("http://localhost:"+PORT+"/birch?default");
>
> URLConnection conn = url.openConnection();
> conn.setRequestProperty("Accept", "application/rdf+xml");
> Model model = ModelFactory.createDefaultModel();
> model.read(conn.getInputStream(), null);
> // RDFDataMgr.read(model, conn.getInputStream(), Lang.RDFXML);
> RDFDataMgr.write(System.out, model, Lang.RDFXML);
> System.exit(0);
> }
Re: Fuseki 2: Serving RDF/XML over HTTP
Posted by Andy Seaborne <an...@apache.org>.
The problem is that you are asking the dataset for RDF/XML.
Datasets include the named graphs. It needs TriG or N-Quads.
So you ask for RDF/XML, and content negotiation says "can't - here's nmy
default (which is N-Quads) and then the code ignores the Content-type
and forces RDF/XML. It then fails to parse.
This works for me - ask for the default graph:
"http://localhost:"+PORT+"/birch?default"
Below is that done twice - using RDFConnection and as in the example.
Fuseki logging is turned on which can help debug conneg.
Andy
public static void main(String[] args) throws Exception {
FusekiLogging.setLogging();
int PORT = WebLib.choosePort();
FusekiServer server = FusekiServer.create()
.port(PORT)
.verbose(true)
.add("/birch", DatasetFactory.createTxnMem())
.build();
server.start();
// Server startup.
Lib.sleep(100);
// Version 1
try (RDFConnection conn = RDFConnectionRemote
.create()
.destination("http://localhost:"+PORT+"/birch")
.acceptHeaderGraph("application/rdf+xml")
.build()) {
Model model = conn.fetch();
RDFDataMgr.write(System.out, model, Lang.RDFXML);
}
// Version 2
URL url = new URL("http://localhost:"+PORT+"/birch?default");
URLConnection conn = url.openConnection();
conn.setRequestProperty("Accept", "application/rdf+xml");
Model model = ModelFactory.createDefaultModel();
model.read(conn.getInputStream(), null);
// RDFDataMgr.read(model, conn.getInputStream(), Lang.RDFXML);
RDFDataMgr.write(System.out, model, Lang.RDFXML);
System.exit(0);
}
Re: Fuseki 2: Serving RDF/XML over HTTP
Posted by Martynas Jusevičius <ma...@atomgraph.com>.
You might want to check if URLConnection really sends the HTTP headers
you think it does. I had issues with it adding some headers by
default.
On Mon, Jan 14, 2019 at 4:53 PM Kevin Dreßler <kv...@gmail.com> wrote:
>
> Sorry for double posting but I might add that with this code I get the following Exception:
>
> java.lang.RuntimeException: org.apache.jena.riot.RiotException: [line: 1, col: 7 ] Element or attribute do not match QName production: QName::=(NCName':')?NCName.
> at org.aksw.deer.enrichments.DereferencingEnrichmentOperator.queryResourceModel(DereferencingEnrichmentOperator.java:231)
>
> And when I change
>
> ModelFactory.createDefaultModel().read(conn.getInputStream(), null)
>
> to
>
> ModelFactory.createDefaultModel().read(conn.getInputStream(), null, "TURTLE")
>
> but leave
>
> conn.setRequestProperty("Accept", "application/rdf+xml")
>
> unchanged, it works as expected.
>
> Using curl I see that Fuseki indeed delivers TURTLE:
>
> $ curl -H "Accept: application/rdf+xml" http://localhost:32537/birch
> <http://localhost:32537/birch> <http://localhost:32537/brinellHardness> "27"^^<http://www.w3.org/2001/XMLSchema#int> .
>
> I am on Jena 3.9.0.
>
> Hope thats enough information.
>
> Best,
> Kevin
>
> > On 14. Jan 2019, at 16:29, Kevin Dreßler <kv...@gmail.com> wrote:
> >
> > This is the method where I do the request and which I effectively want to test:
> >
> > private Model queryResourceModel(Resource o) {
> > URL url;
> > URLConnection conn;
> > Model result = ModelFactory.createDefaultModel();
> > try {
> > url = new URL(o.getURI());
> > } catch (MalformedURLException e) {
> > e.printStackTrace();
> > return result;
> > }
> > try {
> > conn = url.openConnection();
> > conn.setRequestProperty("Accept", "application/rdf+xml");
> > conn.setRequestProperty("Accept-Language", "en");
> > return ModelFactory.createDefaultModel()
> > .read(conn.getInputStream(), null);
> > } catch (ConnectException e) {
> > if (e.getMessage().contains("refused")) {
> > throw new RuntimeException(e);
> > }
> > } catch (Exception e) {
> > throw new RuntimeException(e);
> > }
> > return result;
> > }
> >
> >
> >> On 14. Jan 2019, at 15:09, ajs6f <aj...@apache.org> wrote:
> >>
> >> Please show the code you are actually using to make requests.
> >>
> >> ajs6f
> >>
> >>> On Jan 14, 2019, at 9:06 AM, Kevin Dreßler <kv...@gmail.com> wrote:
> >>>
> >>> Hello,
> >>>
> >>> for unit testing I need to mock up a HTTP server delivering RDF/XML per content negotiation (Accept: application/rdf+xml). For a HTTP request on http://myEndpoint:myPort/test with the "Accept" header set as above it should output DESCRIBE(http://myEndpoint:myPort/test) as RDF/XML, so basically I want to mimic DBpedia. Is there a way to do this with a minimal amount of configuration inside my tests using FUSEKI 2?
> >>> Right now I am using the following code but I get the output only as TURTLE.
> >>>
> >>> String EX = "http://localhost:32537/";
> >>>
> >>> Model lookup = ModelFactory.createDefaultModel();
> >>> lookup.add(lookup.createResource(EX + "birch"),
> >>> lookup.createProperty(EX + "brinellHardness"),
> >>> lookup.createTypedLiteral(27));
> >>>
> >>> FusekiServer server = FusekiServer.create()
> >>> .add("/birch", DatasetFactory.create(lookup))
> >>> .port(32537)
> >>> .build();
> >>> server.start();
> >>>
> >>> Thanks in advance,
> >>> Kevin
> >>
> >
>
Re: Fuseki 2: Serving RDF/XML over HTTP
Posted by Kevin Dreßler <kv...@gmail.com>.
Sorry for double posting but I might add that with this code I get the following Exception:
java.lang.RuntimeException: org.apache.jena.riot.RiotException: [line: 1, col: 7 ] Element or attribute do not match QName production: QName::=(NCName':')?NCName.
at org.aksw.deer.enrichments.DereferencingEnrichmentOperator.queryResourceModel(DereferencingEnrichmentOperator.java:231)
And when I change
ModelFactory.createDefaultModel().read(conn.getInputStream(), null)
to
ModelFactory.createDefaultModel().read(conn.getInputStream(), null, "TURTLE")
but leave
conn.setRequestProperty("Accept", "application/rdf+xml")
unchanged, it works as expected.
Using curl I see that Fuseki indeed delivers TURTLE:
$ curl -H "Accept: application/rdf+xml" http://localhost:32537/birch
<http://localhost:32537/birch> <http://localhost:32537/brinellHardness> "27"^^<http://www.w3.org/2001/XMLSchema#int> .
I am on Jena 3.9.0.
Hope thats enough information.
Best,
Kevin
> On 14. Jan 2019, at 16:29, Kevin Dreßler <kv...@gmail.com> wrote:
>
> This is the method where I do the request and which I effectively want to test:
>
> private Model queryResourceModel(Resource o) {
> URL url;
> URLConnection conn;
> Model result = ModelFactory.createDefaultModel();
> try {
> url = new URL(o.getURI());
> } catch (MalformedURLException e) {
> e.printStackTrace();
> return result;
> }
> try {
> conn = url.openConnection();
> conn.setRequestProperty("Accept", "application/rdf+xml");
> conn.setRequestProperty("Accept-Language", "en");
> return ModelFactory.createDefaultModel()
> .read(conn.getInputStream(), null);
> } catch (ConnectException e) {
> if (e.getMessage().contains("refused")) {
> throw new RuntimeException(e);
> }
> } catch (Exception e) {
> throw new RuntimeException(e);
> }
> return result;
> }
>
>
>> On 14. Jan 2019, at 15:09, ajs6f <aj...@apache.org> wrote:
>>
>> Please show the code you are actually using to make requests.
>>
>> ajs6f
>>
>>> On Jan 14, 2019, at 9:06 AM, Kevin Dreßler <kv...@gmail.com> wrote:
>>>
>>> Hello,
>>>
>>> for unit testing I need to mock up a HTTP server delivering RDF/XML per content negotiation (Accept: application/rdf+xml). For a HTTP request on http://myEndpoint:myPort/test with the "Accept" header set as above it should output DESCRIBE(http://myEndpoint:myPort/test) as RDF/XML, so basically I want to mimic DBpedia. Is there a way to do this with a minimal amount of configuration inside my tests using FUSEKI 2?
>>> Right now I am using the following code but I get the output only as TURTLE.
>>>
>>> String EX = "http://localhost:32537/";
>>>
>>> Model lookup = ModelFactory.createDefaultModel();
>>> lookup.add(lookup.createResource(EX + "birch"),
>>> lookup.createProperty(EX + "brinellHardness"),
>>> lookup.createTypedLiteral(27));
>>>
>>> FusekiServer server = FusekiServer.create()
>>> .add("/birch", DatasetFactory.create(lookup))
>>> .port(32537)
>>> .build();
>>> server.start();
>>>
>>> Thanks in advance,
>>> Kevin
>>
>
Re: Fuseki 2: Serving RDF/XML over HTTP
Posted by Kevin Dreßler <kv...@gmail.com>.
This is the method where I do the request and which I effectively want to test:
private Model queryResourceModel(Resource o) {
URL url;
URLConnection conn;
Model result = ModelFactory.createDefaultModel();
try {
url = new URL(o.getURI());
} catch (MalformedURLException e) {
e.printStackTrace();
return result;
}
try {
conn = url.openConnection();
conn.setRequestProperty("Accept", "application/rdf+xml");
conn.setRequestProperty("Accept-Language", "en");
return ModelFactory.createDefaultModel()
.read(conn.getInputStream(), null);
} catch (ConnectException e) {
if (e.getMessage().contains("refused")) {
throw new RuntimeException(e);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return result;
}
> On 14. Jan 2019, at 15:09, ajs6f <aj...@apache.org> wrote:
>
> Please show the code you are actually using to make requests.
>
> ajs6f
>
>> On Jan 14, 2019, at 9:06 AM, Kevin Dreßler <kv...@gmail.com> wrote:
>>
>> Hello,
>>
>> for unit testing I need to mock up a HTTP server delivering RDF/XML per content negotiation (Accept: application/rdf+xml). For a HTTP request on http://myEndpoint:myPort/test with the "Accept" header set as above it should output DESCRIBE(http://myEndpoint:myPort/test) as RDF/XML, so basically I want to mimic DBpedia. Is there a way to do this with a minimal amount of configuration inside my tests using FUSEKI 2?
>> Right now I am using the following code but I get the output only as TURTLE.
>>
>> String EX = "http://localhost:32537/";
>>
>> Model lookup = ModelFactory.createDefaultModel();
>> lookup.add(lookup.createResource(EX + "birch"),
>> lookup.createProperty(EX + "brinellHardness"),
>> lookup.createTypedLiteral(27));
>>
>> FusekiServer server = FusekiServer.create()
>> .add("/birch", DatasetFactory.create(lookup))
>> .port(32537)
>> .build();
>> server.start();
>>
>> Thanks in advance,
>> Kevin
>
Re: Fuseki 2: Serving RDF/XML over HTTP
Posted by ajs6f <aj...@apache.org>.
Please show the code you are actually using to make requests.
ajs6f
> On Jan 14, 2019, at 9:06 AM, Kevin Dreßler <kv...@gmail.com> wrote:
>
> Hello,
>
> for unit testing I need to mock up a HTTP server delivering RDF/XML per content negotiation (Accept: application/rdf+xml). For a HTTP request on http://myEndpoint:myPort/test with the "Accept" header set as above it should output DESCRIBE(http://myEndpoint:myPort/test) as RDF/XML, so basically I want to mimic DBpedia. Is there a way to do this with a minimal amount of configuration inside my tests using FUSEKI 2?
> Right now I am using the following code but I get the output only as TURTLE.
>
> String EX = "http://localhost:32537/";
>
> Model lookup = ModelFactory.createDefaultModel();
> lookup.add(lookup.createResource(EX + "birch"),
> lookup.createProperty(EX + "brinellHardness"),
> lookup.createTypedLiteral(27));
>
> FusekiServer server = FusekiServer.create()
> .add("/birch", DatasetFactory.create(lookup))
> .port(32537)
> .build();
> server.start();
>
> Thanks in advance,
> Kevin