Skip to content

Commit

Permalink
Added conneg
Browse files Browse the repository at this point in the history
  • Loading branch information
Miel Vander Sande committed Dec 1, 2015
1 parent e3ceeeb commit 586bfd9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 26 deletions.
6 changes: 6 additions & 0 deletions config-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 16 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>2.11.1</version>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.11.1</version>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down Expand Up @@ -57,6 +57,18 @@
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
Expand All @@ -65,8 +77,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -46,7 +49,8 @@ public class TriplePatternFragmentServlet extends HttpServlet {
private final static long TRIPLESPERPAGE = 100;

private ConfigReader config;
private HashMap<String, IDataSource> dataSources = new HashMap<>();
private final HashMap<String, IDataSource> dataSources = new HashMap<>();
private final Collection<String> mimeTypes = new ArrayList<>();

@Override
public void init(ServletConfig servletConfig) throws ServletException {
Expand Down Expand Up @@ -76,6 +80,12 @@ public void init(ServletConfig servletConfig) throws ServletException {
for (Entry<String, JsonObject> 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);
}
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/linkeddatafragments/util/CommonResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

3 comments on commit 586bfd9

@mielvds
Copy link
Contributor

@mielvds mielvds commented on 586bfd9 Dec 1, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some trailing commit I still needed to push. Note that this is the last Jena version we can support without having to change the rdfhdt library. Jena 3 introduces breaking package changes

@RubenVerborgh
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool. Maybe we should consider changing the rdfhdt library in time (for JSON-LD support etc.).

I wonder if we can bring the Java server more closer in functionality to the JavaScript server. Preferably without too much duplication. Would be awesome if we could reuse the templating mechanism. Maybe there is room for that with Java 8 and Nashhorn.

@mielvds
Copy link
Contributor

@mielvds mielvds commented on 586bfd9 Dec 1, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking exactly the same thing, also on the templates. I was already looking around, but no straightforward EJS library for java. Using JSP would be rather easy though, but let's try Nashhorn first.

Please sign in to comment.