|
| 1 | +package org.linkeddatafragments.servlets; |
| 2 | + |
| 3 | +import java.util.regex.Matcher; |
| 4 | +import java.util.regex.Pattern; |
| 5 | + |
| 6 | +import javax.servlet.ServletConfig; |
| 7 | +import javax.servlet.ServletException; |
| 8 | +import javax.servlet.http.HttpServlet; |
| 9 | +import javax.servlet.http.HttpServletRequest; |
| 10 | +import javax.servlet.http.HttpServletResponse; |
| 11 | + |
| 12 | +import org.rdfhdt.hdt.hdt.HDT; |
| 13 | +import org.rdfhdt.hdt.hdt.HDTManager; |
| 14 | +import org.rdfhdt.hdtjena.HDTGraph; |
| 15 | + |
| 16 | +import com.hp.hpl.jena.rdf.model.Model; |
| 17 | +import com.hp.hpl.jena.rdf.model.ModelFactory; |
| 18 | +import com.hp.hpl.jena.rdf.model.Property; |
| 19 | +import com.hp.hpl.jena.rdf.model.RDFNode; |
| 20 | +import com.hp.hpl.jena.rdf.model.Resource; |
| 21 | +import com.hp.hpl.jena.rdf.model.StmtIterator; |
| 22 | + |
| 23 | +/** |
| 24 | + * Servlet that responds with a Basic Linked Data Fragment. |
| 25 | + * @author Ruben Verborgh |
| 26 | + */ |
| 27 | +public class BasicLdfServlet extends HttpServlet { |
| 28 | + private final static long serialVersionUID = 1L; |
| 29 | + private final static int TRIPLES_PER_PAGE = 100; |
| 30 | + private final static Pattern STRINGPATTERN = Pattern.compile("^\"(.*)\"(?:\\^\\^<(.*)>|@(.*))?$"); |
| 31 | + |
| 32 | + private Model model; |
| 33 | + |
| 34 | + @Override |
| 35 | + public void init(ServletConfig config) throws ServletException { |
| 36 | + try { |
| 37 | + final String dataFile = config.getInitParameter("dataFile"); |
| 38 | + final HDT hdt = HDTManager.mapIndexedHDT(dataFile, null); |
| 39 | + final HDTGraph graph = new HDTGraph(hdt); |
| 40 | + model = ModelFactory.createModelForGraph(graph); |
| 41 | + } |
| 42 | + catch (Exception e) { |
| 43 | + throw new ServletException(e); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException { |
| 49 | + try { |
| 50 | + final String path = request.getRequestURI().substring(request.getContextPath().length()); |
| 51 | + |
| 52 | + // parse the subject, predicate, and object parameters |
| 53 | + final Model output = ModelFactory.createDefaultModel(); |
| 54 | + final Resource subject = parseAsResource(request.getParameter("subject"), output); |
| 55 | + final Property predicate = parseAsProperty(request.getParameter("predicate"), output); |
| 56 | + final RDFNode object = parseAsNode(request.getParameter("object"), output); |
| 57 | + |
| 58 | + // add all statements with the given parameters to the output model |
| 59 | + final StmtIterator statements = model.listStatements(subject, predicate, object); |
| 60 | + for (int i = 0; i < TRIPLES_PER_PAGE && statements.hasNext(); i++) |
| 61 | + output.add(statements.next()); |
| 62 | + |
| 63 | + // serialize the output as Turtle |
| 64 | + response.setHeader("Server", "Linked Data Fragments Server"); |
| 65 | + response.setHeader("Content-Type", "text/turtle"); |
| 66 | + output.write(response.getWriter(), "Turtle"); |
| 67 | + } |
| 68 | + catch (Exception e) { |
| 69 | + throw new ServletException(e); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Parses the given value as an RDF resource. |
| 75 | + * @param value the value |
| 76 | + * @param model the model |
| 77 | + * @return the parsed value, or null if unspecified |
| 78 | + */ |
| 79 | + private Resource parseAsResource(String value, Model model) { |
| 80 | + final RDFNode subject = parseAsNode(value, model); |
| 81 | + return subject instanceof Resource ? (Resource)subject : null; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Parses the given value as an RDF property. |
| 86 | + * @param value the value |
| 87 | + * @param model the model |
| 88 | + * @return the parsed value, or null if unspecified |
| 89 | + */ |
| 90 | + private Property parseAsProperty(String value, Model model) { |
| 91 | + final RDFNode predicate = parseAsNode(value, model); |
| 92 | + return predicate instanceof Resource ? model.createProperty(((Resource)predicate).getURI()) : null; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Parses the given value as an RDF node. |
| 97 | + * @param value the value |
| 98 | + * @param model the model |
| 99 | + * @return the parsed value, or null if unspecified |
| 100 | + */ |
| 101 | + private RDFNode parseAsNode(String value, Model model) { |
| 102 | + // nothing or empty indicates an unknown |
| 103 | + if (value == null || value.length() == 0) |
| 104 | + return null; |
| 105 | + // find the kind of entity based on the first character |
| 106 | + final char firstChar = value.charAt(0); |
| 107 | + switch(firstChar) { |
| 108 | + // variable or blank node indicates an unknown |
| 109 | + case '?': |
| 110 | + case '_': |
| 111 | + return null; |
| 112 | + // angular brackets indicate a URI |
| 113 | + case '<': |
| 114 | + return model.createResource(value.substring(1, value.length() - 1)); |
| 115 | + // quotes indicate a string |
| 116 | + case '"': |
| 117 | + final Matcher matcher = STRINGPATTERN.matcher(value); |
| 118 | + if (matcher.matches()) { |
| 119 | + final String body = matcher.group(1); |
| 120 | + final String type = matcher.group(2); |
| 121 | + final String lang = matcher.group(3); |
| 122 | + if (type != null) |
| 123 | + return model.createTypedLiteral(body, type); |
| 124 | + if (lang != null) |
| 125 | + return model.createLiteral(body, lang); |
| 126 | + return model.createLiteral(body); |
| 127 | + } |
| 128 | + return null; |
| 129 | + // assume it's a URI without angular brackets |
| 130 | + default: |
| 131 | + return model.createResource(value); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments