From 586bfd979d19c6e970f4e58cdbbab6097829118b Mon Sep 17 00:00:00 2001 From: Miel Vander Sande Date: Tue, 1 Dec 2015 11:53:57 +0100 Subject: [PATCH] Added conneg --- config-example.json | 6 ++ pom.xml | 20 +++++-- .../servlet/TriplePatternFragmentServlet.java | 56 ++++++++++++------- .../util/CommonResources.java | 2 +- 4 files changed, 58 insertions(+), 26 deletions(-) diff --git a/config-example.json b/config-example.json index 3716a1f..4a5a721 100644 --- a/config-example.json +++ b/config-example.json @@ -3,6 +3,12 @@ "datasources": { "dbpedia": { + "title": "DBPedia", + "type": "HdtDatasource", + "description": "DBPedia with an HDT back-end", + "settings": { "file": "data/dbpedia.hdt" } + }, + "swdf": { "title": "Semantic Web Dog Food", "type": "HdtDatasource", "description": "Semantic Web Dog Food with an HDT back-end", diff --git a/pom.xml b/pom.xml index f48625a..09a004f 100644 --- a/pom.xml +++ b/pom.xml @@ -19,12 +19,12 @@ org.apache.jena jena-core - 2.11.1 + 2.13.0 org.apache.jena jena-arq - 2.11.1 + 2.13.0 org.apache.httpcomponents @@ -57,6 +57,18 @@ commons-cli 1.2 + + org.apache.commons + commons-lang3 + 3.3.2 + jar + + + commons-lang + commons-lang + 2.6 + jar + src @@ -65,8 +77,8 @@ maven-compiler-plugin 3.1 - 1.7 - 1.7 + 1.8 + 1.8 diff --git a/src/org/linkeddatafragments/servlet/TriplePatternFragmentServlet.java b/src/org/linkeddatafragments/servlet/TriplePatternFragmentServlet.java index 51ba8d8..9764cde 100644 --- a/src/org/linkeddatafragments/servlet/TriplePatternFragmentServlet.java +++ b/src/org/linkeddatafragments/servlet/TriplePatternFragmentServlet.java @@ -1,37 +1,40 @@ package org.linkeddatafragments.servlet; import com.google.gson.JsonObject; +import com.hp.hpl.jena.datatypes.TypeMapper; +import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; +import com.hp.hpl.jena.rdf.model.Literal; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.Property; +import com.hp.hpl.jena.rdf.model.RDFNode; +import com.hp.hpl.jena.rdf.model.Resource; +import com.hp.hpl.jena.rdf.model.ResourceFactory; +import com.hp.hpl.jena.shared.InvalidPropertyURIException; import java.io.File; import java.io.FileReader; +import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern; - import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; - import org.apache.http.client.utils.URIBuilder; +import org.apache.jena.riot.Lang; +import org.apache.jena.riot.RDFDataMgr; +import org.apache.jena.riot.RDFLanguages; import org.linkeddatafragments.config.ConfigReader; -import org.linkeddatafragments.datasource.TriplePatternFragment; +import org.linkeddatafragments.datasource.DataSourceFactory; import org.linkeddatafragments.datasource.IDataSource; -import org.linkeddatafragments.datasource.HdtDataSource; +import org.linkeddatafragments.datasource.TriplePatternFragment; import static org.linkeddatafragments.util.CommonResources.*; -import com.hp.hpl.jena.datatypes.TypeMapper; -import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; -import com.hp.hpl.jena.rdf.model.Literal; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.Property; -import com.hp.hpl.jena.rdf.model.RDFNode; -import com.hp.hpl.jena.rdf.model.Resource; -import com.hp.hpl.jena.rdf.model.ResourceFactory; -import com.hp.hpl.jena.shared.InvalidPropertyURIException; -import org.linkeddatafragments.datasource.DataSourceFactory; +import org.linkeddatafragments.util.MIMEParse; /** * Servlet that responds with a Basic Linked Data Fragment. @@ -46,7 +49,8 @@ public class TriplePatternFragmentServlet extends HttpServlet { private final static long TRIPLESPERPAGE = 100; private ConfigReader config; - private HashMap dataSources = new HashMap<>(); + private final HashMap dataSources = new HashMap<>(); + private final Collection mimeTypes = new ArrayList<>(); @Override public void init(ServletConfig servletConfig) throws ServletException { @@ -76,6 +80,12 @@ public void init(ServletConfig servletConfig) throws ServletException { for (Entry dataSource : config.getDataSources().entrySet()) { dataSources.put(dataSource.getKey(), DataSourceFactory.create(dataSource.getValue())); } + + // register content types + mimeTypes.add(Lang.TTL.getHeaderString()); + mimeTypes.add(Lang.JSONLD.getHeaderString()); + mimeTypes.add(Lang.NTRIPLES.getHeaderString()); + mimeTypes.add(Lang.RDFXML.getHeaderString() ); } catch (Exception e) { throw new ServletException(e); } @@ -106,6 +116,16 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro // fill the output model final Model output = fragment.getTriples(); output.setNsPrefixes(config.getPrefixes()); + + // do conneg + String bestMatch = MIMEParse.bestMatch(mimeTypes, request.getHeader("Accept")); + Lang contentType = RDFLanguages.contentTypeToLang(bestMatch); + + // serialize the output + response.setHeader("Server", "Linked Data Fragments Server"); + response.setContentType(bestMatch); + response.setCharacterEncoding("utf-8"); + RDFDataMgr.write(response.getOutputStream(), output, contentType); // add dataset metadata final String hostName = request.getHeader("Host"); @@ -155,12 +175,6 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro output.add(predicateMapping, HYDRA_PROPERTY, RDF_PREDICATE); output.add(objectMapping, HYDRA_VARIABLE, output.createLiteral("object")); output.add(objectMapping, HYDRA_PROPERTY, RDF_OBJECT); - - // serialize the output as Turtle - response.setHeader("Server", "Linked Data Fragments Server"); - response.setContentType("text/turtle"); - response.setCharacterEncoding("utf-8"); - output.write(response.getWriter(), "Turtle", fragmentUrl); } catch (Exception e) { throw new ServletException(e); } diff --git a/src/org/linkeddatafragments/util/CommonResources.java b/src/org/linkeddatafragments/util/CommonResources.java index fbd58af..f7b4f2c 100644 --- a/src/org/linkeddatafragments/util/CommonResources.java +++ b/src/org/linkeddatafragments/util/CommonResources.java @@ -33,7 +33,7 @@ public class CommonResources { public final static Property INVALID_URI = createProperty("urn:invalid"); - private final static Property createProperty(String uri) { + private static Property createProperty(String uri) { return ResourceFactory.createProperty(uri); } }