Skip to content

Commit d36cc14

Browse files
author
Miel Vander Sande
committed
Added IndexDataSource class
1 parent 4f7047c commit d36cc14

File tree

2 files changed

+90
-24
lines changed

2 files changed

+90
-24
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.linkeddatafragments.datasource;
2+
3+
import com.hp.hpl.jena.rdf.model.Model;
4+
import com.hp.hpl.jena.rdf.model.ModelFactory;
5+
import com.hp.hpl.jena.rdf.model.Property;
6+
import com.hp.hpl.jena.rdf.model.RDFNode;
7+
import com.hp.hpl.jena.rdf.model.Resource;
8+
import com.hp.hpl.jena.rdf.model.StmtIterator;
9+
import com.hp.hpl.jena.rdf.model.impl.PropertyImpl;
10+
import com.hp.hpl.jena.rdf.model.impl.ResourceImpl;
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
/**
15+
* An Index data source provides an overview of all available datasets.
16+
*
17+
* @author Miel Vander Sande
18+
*/
19+
public class IndexDataSource extends DataSource {
20+
21+
final String RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
22+
final String RDFS = "http://www.w3.org/2000/01/rdf-schema#";
23+
final String DC = "http://purl.org/dc/terms/";
24+
final String VOID = "http://rdfs.org/ns/void#";
25+
26+
private final Model model;
27+
28+
public IndexDataSource(String baseUrl, HashMap<String, IDataSource> datasources) {
29+
super("Index", "List of all datasources");
30+
31+
this.model = ModelFactory.createDefaultModel();
32+
33+
for (Map.Entry<String, IDataSource> entry : datasources.entrySet()) {
34+
String datasourceName = entry.getKey();
35+
IDataSource datasource = entry.getValue();
36+
37+
Resource datasourceUrl = new ResourceImpl(baseUrl + "/" + datasourceName);
38+
39+
model.add(datasourceUrl, new PropertyImpl(RDF + "type"), VOID + "Dataset");
40+
model.add(datasourceUrl, new PropertyImpl(RDFS + "label"), datasource.getTitle());
41+
model.add(datasourceUrl, new PropertyImpl(DC + "title"), datasource.getTitle());
42+
model.add(datasourceUrl, new PropertyImpl(DC + "description"), datasource.getDescription());
43+
}
44+
}
45+
46+
@Override
47+
public TriplePatternFragment getFragment(Resource subject, Property predicate, RDFNode object, long offset, long limit) {
48+
StmtIterator listStatements = this.model.listStatements(subject, predicate, object);
49+
Model result = ModelFactory.createDefaultModel();
50+
51+
long index = 0;
52+
while (listStatements.hasNext() && index < offset) {
53+
listStatements.next();
54+
index++;
55+
}
56+
57+
while (listStatements.hasNext() && index < (offset + limit)) {
58+
result.add(listStatements.next());
59+
}
60+
61+
return new TriplePatternFragmentBase(result, result.size());
62+
}
63+
64+
}

src/org/linkeddatafragments/servlet/TriplePatternFragmentServlet.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.io.FileReader;
1515
import java.io.IOException;
1616
import java.net.URISyntaxException;
17-
import java.net.URL;
1817
import java.util.ArrayList;
1918
import java.util.Collection;
2019
import java.util.HashMap;
@@ -46,6 +45,7 @@
4645
* @author Bart Hanssens
4746
*/
4847
public class TriplePatternFragmentServlet extends HttpServlet {
48+
4949
private final static long serialVersionUID = 1L;
5050

5151
// Parameters
@@ -55,17 +55,15 @@ public class TriplePatternFragmentServlet extends HttpServlet {
5555
public final static String OBJ = "object";
5656
public final static String PAGE = "page";
5757

58-
59-
private final static Pattern STRINGPATTERN =
60-
Pattern.compile("^\"(.*)\"(?:@(.*)|\\^\\^<?([^<>]*)>?)?$");
58+
private final static Pattern STRINGPATTERN
59+
= Pattern.compile("^\"(.*)\"(?:@(.*)|\\^\\^<?([^<>]*)>?)?$");
6160
private final static TypeMapper TYPES = TypeMapper.getInstance();
6261
private final static long TRIPLESPERPAGE = 100;
6362

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

68-
6967
private File getConfigFile(ServletConfig config) throws IOException {
7068
String path = config.getServletContext().getRealPath("/");
7169
if (path == null) {
@@ -96,11 +94,11 @@ public void init(ServletConfig servletConfig) throws ServletException {
9694
dataSources.put(dataSource.getKey(), DataSourceFactory.create(dataSource.getValue()));
9795
}
9896

99-
// register content types
97+
// register content types
10098
mimeTypes.add(Lang.TTL.getHeaderString());
10199
mimeTypes.add(Lang.JSONLD.getHeaderString());
102100
mimeTypes.add(Lang.NTRIPLES.getHeaderString());
103-
mimeTypes.add(Lang.RDFXML.getHeaderString() );
101+
mimeTypes.add(Lang.RDFXML.getHeaderString());
104102
} catch (IOException | DataSourceException e) {
105103
throw new ServletException(e);
106104
}
@@ -118,9 +116,9 @@ private IDataSource getDataSource(HttpServletRequest request) throws IOException
118116
String requestURI = request.getRequestURI();
119117

120118
String path = contextPath == null
121-
? requestURI
122-
: requestURI.substring(contextPath.length());
123-
119+
? requestURI
120+
: requestURI.substring(contextPath.length());
121+
124122
if (path.equals("/") || path.isEmpty()) {
125123
return new IndexDataSource(getBaseUrl(request), dataSources);
126124
}
@@ -142,11 +140,17 @@ private IDataSource getDataSource(HttpServletRequest request) throws IOException
142140
private String getDatasetUrl(HttpServletRequest request) {
143141
return getBaseUrl(request) + request.getRequestURI();
144142
}
145-
143+
144+
/**
145+
* Get Base url
146+
*
147+
* @param request
148+
* @return
149+
*/
146150
private String getBaseUrl(HttpServletRequest request) {
147-
if (config.getBaseURL() != null)
151+
if (config.getBaseURL() != null) {
148152
return config.getBaseURL();
149-
else if ((request.getServerPort() == 80)
153+
} else if ((request.getServerPort() == 80)
150154
|| (request.getServerPort() == 443)) {
151155
return request.getScheme() + "://"
152156
+ request.getServerName();
@@ -165,7 +169,7 @@ else if ((request.getServerPort() == 80)
165169
* @param limit
166170
*/
167171
private void addMeta(Model output, Resource datasetId, Resource fragmentId,
168-
long total, long limit) {
172+
long total, long limit) {
169173
output.add(datasetId, CommonResources.RDF_TYPE, CommonResources.VOID_DATASET);
170174
output.add(datasetId, CommonResources.RDF_TYPE, CommonResources.HYDRA_COLLECTION);
171175
output.add(datasetId, CommonResources.VOID_SUBSET, fragmentId);
@@ -181,7 +185,6 @@ private void addMeta(Model output, Resource datasetId, Resource fragmentId,
181185
output.add(fragmentId, CommonResources.HYDRA_ITEMSPERPAGE, limitTyped);
182186
}
183187

184-
185188
/**
186189
* Add reference to first/previous/next page
187190
*
@@ -195,21 +198,21 @@ private void addMeta(Model output, Resource datasetId, Resource fragmentId,
195198
* @throws URISyntaxException
196199
*/
197200
private void addPages(Model output, Resource fragmentId, String fragmentUrl,
198-
long total, long limit, long offset, long page) throws URISyntaxException {
201+
long total, long limit, long offset, long page) throws URISyntaxException {
199202
URIBuilder pagedUrl = new URIBuilder(fragmentUrl);
200203

201204
pagedUrl.setParameter(PAGE, "1");
202205
output.add(fragmentId, CommonResources.HYDRA_FIRSTPAGE,
203-
output.createResource(pagedUrl.toString()));
206+
output.createResource(pagedUrl.toString()));
204207
if (offset > 0) {
205208
pagedUrl.setParameter(PAGE, Long.toString(page - 1));
206209
output.add(fragmentId, CommonResources.HYDRA_PREVIOUSPAGE,
207-
output.createResource(pagedUrl.toString()));
210+
output.createResource(pagedUrl.toString()));
208211
}
209212
if (offset + limit < total) {
210213
pagedUrl.setParameter(PAGE, Long.toString(page + 1));
211214
output.add(fragmentId, CommonResources.HYDRA_NEXTPAGE,
212-
output.createResource(pagedUrl.toString()));
215+
output.createResource(pagedUrl.toString()));
213216
}
214217
}
215218

@@ -243,7 +246,6 @@ private void addControls(Model output, Resource datasetId, String datasetUrl) {
243246
output.add(objectMapping, CommonResources.HYDRA_PROPERTY, CommonResources.RDF_OBJECT);
244247
}
245248

246-
247249
@Override
248250
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
249251
try {
@@ -258,8 +260,8 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
258260
long limit = TRIPLESPERPAGE;
259261
long offset = limit * (page - 1);
260262

261-
TriplePatternFragment fragment =
262-
dataSource.getFragment(subject, predicate, object, offset, limit);
263+
TriplePatternFragment fragment
264+
= dataSource.getFragment(subject, predicate, object, offset, limit);
263265

264266
// fill the output model
265267
Model output = fragment.getTriples();
@@ -278,7 +280,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
278280
addMeta(output, datasetId, fragmentId, total, limit);
279281
addPages(output, fragmentId, fragmentUrl, total, limit, offset, page);
280282
addControls(output, datasetId, datasetUrl);
281-
283+
282284
// do conneg
283285
String bestMatch = MIMEParse.bestMatch(mimeTypes, request.getHeader("Accept"));
284286
Lang contentType = RDFLanguages.contentTypeToLang(bestMatch);
@@ -287,7 +289,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
287289
response.setHeader("Server", "Linked Data Fragments Server");
288290
response.setContentType(bestMatch);
289291
response.setCharacterEncoding("utf-8");
290-
292+
291293
RDFDataMgr.write(response.getOutputStream(), output, contentType);
292294
} catch (IOException | URISyntaxException e) {
293295
e.printStackTrace();

0 commit comments

Comments
 (0)