1
1
package org .linkeddatafragments .servlet ;
2
2
3
+ import com .google .gson .JsonObject ;
4
+ import com .hp .hpl .jena .datatypes .TypeMapper ;
5
+ import com .hp .hpl .jena .datatypes .xsd .XSDDatatype ;
6
+ import com .hp .hpl .jena .rdf .model .Literal ;
7
+ import com .hp .hpl .jena .rdf .model .Model ;
8
+ import com .hp .hpl .jena .rdf .model .Property ;
9
+ import com .hp .hpl .jena .rdf .model .RDFNode ;
10
+ import com .hp .hpl .jena .rdf .model .Resource ;
11
+ import com .hp .hpl .jena .rdf .model .ResourceFactory ;
12
+ import com .hp .hpl .jena .shared .InvalidPropertyURIException ;
3
13
import java .io .File ;
4
14
import java .io .FileReader ;
5
15
import java .io .IOException ;
6
-
7
16
import java .net .URISyntaxException ;
8
-
9
17
import java .util .ArrayList ;
10
18
import java .util .Collection ;
11
19
import java .util .HashMap ;
12
20
import java .util .Map .Entry ;
13
21
import java .util .regex .Matcher ;
14
22
import java .util .regex .Pattern ;
15
-
16
23
import javax .servlet .ServletConfig ;
17
24
import javax .servlet .ServletException ;
18
25
import javax .servlet .http .HttpServlet ;
19
26
import javax .servlet .http .HttpServletRequest ;
20
27
import javax .servlet .http .HttpServletResponse ;
21
-
22
- import org .apache .commons .codec .CharEncoding ;
23
-
24
- import org .apache .http .HttpHeaders ;
25
28
import org .apache .http .client .utils .URIBuilder ;
26
-
27
29
import org .apache .jena .riot .Lang ;
28
30
import org .apache .jena .riot .RDFDataMgr ;
29
31
import org .apache .jena .riot .RDFLanguages ;
30
-
31
- import com .google .gson .JsonObject ;
32
-
33
- import com .hp .hpl .jena .datatypes .TypeMapper ;
34
- import com .hp .hpl .jena .datatypes .xsd .XSDDatatype ;
35
- import com .hp .hpl .jena .rdf .model .Literal ;
36
- import com .hp .hpl .jena .rdf .model .Model ;
37
- import com .hp .hpl .jena .rdf .model .Property ;
38
- import com .hp .hpl .jena .rdf .model .RDFNode ;
39
- import com .hp .hpl .jena .rdf .model .Resource ;
40
- import com .hp .hpl .jena .rdf .model .ResourceFactory ;
41
- import com .hp .hpl .jena .shared .InvalidPropertyURIException ;
42
-
43
32
import org .linkeddatafragments .config .ConfigReader ;
44
33
import org .linkeddatafragments .datasource .DataSourceFactory ;
45
34
import org .linkeddatafragments .datasource .IDataSource ;
35
+ import org .linkeddatafragments .datasource .IndexDataSource ;
46
36
import org .linkeddatafragments .datasource .TriplePatternFragment ;
47
37
import org .linkeddatafragments .exceptions .DataSourceException ;
48
38
import org .linkeddatafragments .util .CommonResources ;
56
46
*/
57
47
public class TriplePatternFragmentServlet extends HttpServlet {
58
48
private final static long serialVersionUID = 1L ;
59
-
49
+
60
50
// Parameters
61
51
public final static String CFGFILE = "configFile" ;
62
52
public final static String SUBJ = "subject" ;
63
53
public final static String PRED = "predicate" ;
64
54
public final static String OBJ = "object" ;
65
55
public final static String PAGE = "page" ;
66
-
67
-
68
- private final static Pattern STRINGPATTERN =
56
+
57
+
58
+ private final static Pattern STRINGPATTERN =
69
59
Pattern .compile ("^\" (.*)\" (?:@(.*)|\\ ^\\ ^<?([^<>]*)>?)?$" );
70
60
private final static TypeMapper TYPES = TypeMapper .getInstance ();
71
61
private final static long TRIPLESPERPAGE = 100 ;
@@ -74,7 +64,7 @@ public class TriplePatternFragmentServlet extends HttpServlet {
74
64
private final HashMap <String , IDataSource > dataSources = new HashMap <>();
75
65
private final Collection <String > mimeTypes = new ArrayList <>();
76
66
77
-
67
+
78
68
private File getConfigFile (ServletConfig config ) throws IOException {
79
69
String path = config .getServletContext ().getRealPath ("/" );
80
70
if (path == null ) {
@@ -93,21 +83,26 @@ private File getConfigFile(ServletConfig config) throws IOException {
93
83
}
94
84
return cfg ;
95
85
}
96
-
86
+
97
87
@ Override
98
88
public void init (ServletConfig servletConfig ) throws ServletException {
99
89
try {
100
90
// load the configuration
101
91
File configFile = getConfigFile (servletConfig );
102
92
config = new ConfigReader (new FileReader (configFile ));
103
-
93
+
104
94
for (Entry <String , JsonObject > dataSource : config .getDataSources ().entrySet ()) {
105
95
dataSources .put (dataSource .getKey (), DataSourceFactory .create (dataSource .getValue ()));
106
96
}
97
+
98
+ String baseURL = config .getBaseURL () != null ? config .getBaseURL () : "http://localhost:8080" ;
99
+
100
+ IndexDataSource index = new IndexDataSource (baseURL , dataSources );
101
+
107
102
// register content types
108
103
mimeTypes .add (Lang .TTL .getHeaderString ());
109
104
mimeTypes .add (Lang .JSONLD .getHeaderString ());
110
- mimeTypes .add (Lang .NTRIPLES .getHeaderString ());
105
+ mimeTypes .add (Lang .NTRIPLES .getHeaderString ());
111
106
mimeTypes .add (Lang .RDFXML .getHeaderString () );
112
107
} catch (IOException | DataSourceException e ) {
113
108
throw new ServletException (e );
@@ -116,17 +111,17 @@ public void init(ServletConfig servletConfig) throws ServletException {
116
111
117
112
/**
118
113
* Get the datasource
119
- *
114
+ *
120
115
* @param request
121
116
* @return
122
- * @throws IOException
117
+ * @throws IOException
123
118
*/
124
119
private IDataSource getDataSource (HttpServletRequest request ) throws IOException {
125
120
String contextPath = request .getContextPath ();
126
121
String requestURI = request .getRequestURI ();
127
122
128
- String path = contextPath == null
129
- ? requestURI
123
+ String path = contextPath == null
124
+ ? requestURI
130
125
: requestURI .substring (contextPath .length ());
131
126
String dataSourceName = path .substring (1 );
132
127
IDataSource dataSource = dataSources .get (dataSourceName );
@@ -135,12 +130,12 @@ private IDataSource getDataSource(HttpServletRequest request) throws IOException
135
130
}
136
131
return dataSource ;
137
132
}
138
-
133
+
139
134
/**
140
135
* Get dataset url
141
- *
136
+ *
142
137
* @param request
143
- * @return
138
+ * @return
144
139
*/
145
140
private String getDatasetUrl (HttpServletRequest request ) {
146
141
if ((request .getServerPort () == 80 )
@@ -156,70 +151,70 @@ private String getDatasetUrl(HttpServletRequest request) {
156
151
157
152
//return request.getScheme() + "://" + hostName + request.getRequestURI();
158
153
}
159
-
154
+
160
155
/**
161
156
* Add total and limit
162
- *
157
+ *
163
158
* @param output
164
159
* @param fragmentId
165
160
* @param total
166
- * @param limit
161
+ * @param limit
167
162
*/
168
- private void addMeta (Model output , Resource datasetId , Resource fragmentId ,
163
+ private void addMeta (Model output , Resource datasetId , Resource fragmentId ,
169
164
long total , long limit ) {
170
165
output .add (datasetId , CommonResources .RDF_TYPE , CommonResources .VOID_DATASET );
171
166
output .add (datasetId , CommonResources .RDF_TYPE , CommonResources .HYDRA_COLLECTION );
172
167
output .add (datasetId , CommonResources .VOID_SUBSET , fragmentId );
173
-
168
+
174
169
output .add (fragmentId , CommonResources .RDF_TYPE , CommonResources .HYDRA_COLLECTION );
175
170
output .add (fragmentId , CommonResources .RDF_TYPE , CommonResources .HYDRA_PAGEDCOLLECTION );
176
-
171
+
177
172
Literal totalTyped = output .createTypedLiteral (total , XSDDatatype .XSDinteger );
178
173
Literal limitTyped = output .createTypedLiteral (limit , XSDDatatype .XSDinteger );
179
174
180
175
output .add (fragmentId , CommonResources .VOID_TRIPLES , totalTyped );
181
176
output .add (fragmentId , CommonResources .HYDRA_TOTALITEMS , totalTyped );
182
177
output .add (fragmentId , CommonResources .HYDRA_ITEMSPERPAGE , limitTyped );
183
178
}
184
-
179
+
185
180
186
181
/**
187
182
* Add reference to first/previous/next page
188
- *
183
+ *
189
184
* @param output
190
185
* @param fragmentId
191
186
* @param fragmentUrl
192
187
* @param total
193
188
* @param limit
194
189
* @param offset
195
190
* @param page
196
- * @throws URISyntaxException
191
+ * @throws URISyntaxException
197
192
*/
198
- private void addPages (Model output , Resource fragmentId , String fragmentUrl ,
199
- long total , long limit , long offset , long page ) throws URISyntaxException {
193
+ private void addPages (Model output , Resource fragmentId , String fragmentUrl ,
194
+ long total , long limit , long offset , long page ) throws URISyntaxException {
200
195
URIBuilder pagedUrl = new URIBuilder (fragmentUrl );
201
-
196
+
202
197
pagedUrl .setParameter (PAGE , "1" );
203
- output .add (fragmentId , CommonResources .HYDRA_FIRSTPAGE ,
198
+ output .add (fragmentId , CommonResources .HYDRA_FIRSTPAGE ,
204
199
output .createResource (pagedUrl .toString ()));
205
200
if (offset > 0 ) {
206
201
pagedUrl .setParameter (PAGE , Long .toString (page - 1 ));
207
- output .add (fragmentId , CommonResources .HYDRA_PREVIOUSPAGE ,
202
+ output .add (fragmentId , CommonResources .HYDRA_PREVIOUSPAGE ,
208
203
output .createResource (pagedUrl .toString ()));
209
204
}
210
205
if (offset + limit < total ) {
211
206
pagedUrl .setParameter (PAGE , Long .toString (page + 1 ));
212
- output .add (fragmentId , CommonResources .HYDRA_NEXTPAGE ,
207
+ output .add (fragmentId , CommonResources .HYDRA_NEXTPAGE ,
213
208
output .createResource (pagedUrl .toString ()));
214
209
}
215
210
}
216
-
211
+
217
212
/**
218
213
* Add controls to output
219
- *
214
+ *
220
215
* @param output
221
216
* @param datasetId
222
- * @param datasetUrl
217
+ * @param datasetUrl
223
218
*/
224
219
private void addControls (Model output , Resource datasetId , String datasetUrl ) {
225
220
// add controls
@@ -233,33 +228,33 @@ private void addControls(Model output, Resource datasetId, String datasetUrl) {
233
228
output .add (triplePattern , CommonResources .HYDRA_MAPPING , subjectMapping );
234
229
output .add (triplePattern , CommonResources .HYDRA_MAPPING , predicateMapping );
235
230
output .add (triplePattern , CommonResources .HYDRA_MAPPING , objectMapping );
236
-
231
+
237
232
output .add (subjectMapping , CommonResources .HYDRA_VARIABLE , output .createLiteral (SUBJ ));
238
233
output .add (subjectMapping , CommonResources .HYDRA_PROPERTY , CommonResources .RDF_SUBJECT );
239
-
234
+
240
235
output .add (predicateMapping , CommonResources .HYDRA_VARIABLE , output .createLiteral (PRED ));
241
236
output .add (predicateMapping , CommonResources .HYDRA_PROPERTY , CommonResources .RDF_PREDICATE );
242
237
output .add (objectMapping , CommonResources .HYDRA_VARIABLE , output .createLiteral (OBJ ));
243
-
238
+
244
239
output .add (objectMapping , CommonResources .HYDRA_PROPERTY , CommonResources .RDF_OBJECT );
245
240
}
246
-
247
-
241
+
242
+
248
243
@ Override
249
244
public void doGet (HttpServletRequest request , HttpServletResponse response ) throws ServletException {
250
245
try {
251
246
IDataSource dataSource = getDataSource (request );
252
-
247
+
253
248
// query the fragment
254
249
Resource subject = parseAsResource (request .getParameter (SUBJ ));
255
250
Property predicate = parseAsProperty (request .getParameter (PRED ));
256
251
RDFNode object = parseAsNode (request .getParameter (OBJ ));
257
-
252
+
258
253
long page = Math .max (1 , parseAsInteger (request .getParameter (PAGE )));
259
254
long limit = TRIPLESPERPAGE ;
260
255
long offset = limit * (page - 1 );
261
-
262
- TriplePatternFragment fragment =
256
+
257
+ TriplePatternFragment fragment =
263
258
dataSource .getFragment (subject , predicate , object , offset , limit );
264
259
265
260
// fill the output model
@@ -269,15 +264,15 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
269
264
// add dataset metadata
270
265
String datasetUrl = getDatasetUrl (request );
271
266
Resource datasetId = output .createResource (datasetUrl + "#dataset" );
272
-
267
+
273
268
String query = request .getQueryString ();
274
269
String fragmentUrl = query == null ? datasetUrl : (datasetUrl + "?" + query );
275
270
Resource fragmentId = output .createResource (fragmentUrl );
276
271
277
272
long total = fragment .getTotalSize ();
278
-
273
+
279
274
addMeta (output , datasetId , fragmentId , total , limit );
280
- addPages (output , fragmentId , fragmentUrl , total , limit , offset , page );
275
+ addPages (output , fragmentId , fragmentUrl , total , limit , offset , page );
281
276
addControls (output , datasetId , datasetUrl );
282
277
283
278
// do conneg
@@ -317,8 +312,8 @@ private int parseAsInteger(String value) {
317
312
*/
318
313
private Resource parseAsResource (String value ) {
319
314
RDFNode subject = parseAsNode (value );
320
- return subject == null || subject instanceof Resource
321
- ? (Resource ) subject
315
+ return subject == null || subject instanceof Resource
316
+ ? (Resource ) subject
322
317
: CommonResources .INVALID_URI ;
323
318
}
324
319
0 commit comments