14
14
import java .io .FileReader ;
15
15
import java .io .IOException ;
16
16
import java .net .URISyntaxException ;
17
- import java .net .URL ;
18
17
import java .util .ArrayList ;
19
18
import java .util .Collection ;
20
19
import java .util .HashMap ;
46
45
* @author Bart Hanssens
47
46
*/
48
47
public class TriplePatternFragmentServlet extends HttpServlet {
48
+
49
49
private final static long serialVersionUID = 1L ;
50
50
51
51
// Parameters
@@ -55,17 +55,15 @@ public class TriplePatternFragmentServlet extends HttpServlet {
55
55
public final static String OBJ = "object" ;
56
56
public final static String PAGE = "page" ;
57
57
58
-
59
- private final static Pattern STRINGPATTERN =
60
- Pattern .compile ("^\" (.*)\" (?:@(.*)|\\ ^\\ ^<?([^<>]*)>?)?$" );
58
+ private final static Pattern STRINGPATTERN
59
+ = Pattern .compile ("^\" (.*)\" (?:@(.*)|\\ ^\\ ^<?([^<>]*)>?)?$" );
61
60
private final static TypeMapper TYPES = TypeMapper .getInstance ();
62
61
private final static long TRIPLESPERPAGE = 100 ;
63
62
64
63
private ConfigReader config ;
65
64
private final HashMap <String , IDataSource > dataSources = new HashMap <>();
66
65
private final Collection <String > mimeTypes = new ArrayList <>();
67
66
68
-
69
67
private File getConfigFile (ServletConfig config ) throws IOException {
70
68
String path = config .getServletContext ().getRealPath ("/" );
71
69
if (path == null ) {
@@ -96,11 +94,11 @@ public void init(ServletConfig servletConfig) throws ServletException {
96
94
dataSources .put (dataSource .getKey (), DataSourceFactory .create (dataSource .getValue ()));
97
95
}
98
96
99
- // register content types
97
+ // register content types
100
98
mimeTypes .add (Lang .TTL .getHeaderString ());
101
99
mimeTypes .add (Lang .JSONLD .getHeaderString ());
102
100
mimeTypes .add (Lang .NTRIPLES .getHeaderString ());
103
- mimeTypes .add (Lang .RDFXML .getHeaderString () );
101
+ mimeTypes .add (Lang .RDFXML .getHeaderString ());
104
102
} catch (IOException | DataSourceException e ) {
105
103
throw new ServletException (e );
106
104
}
@@ -118,9 +116,9 @@ private IDataSource getDataSource(HttpServletRequest request) throws IOException
118
116
String requestURI = request .getRequestURI ();
119
117
120
118
String path = contextPath == null
121
- ? requestURI
122
- : requestURI .substring (contextPath .length ());
123
-
119
+ ? requestURI
120
+ : requestURI .substring (contextPath .length ());
121
+
124
122
if (path .equals ("/" ) || path .isEmpty ()) {
125
123
return new IndexDataSource (getBaseUrl (request ), dataSources );
126
124
}
@@ -142,11 +140,17 @@ private IDataSource getDataSource(HttpServletRequest request) throws IOException
142
140
private String getDatasetUrl (HttpServletRequest request ) {
143
141
return getBaseUrl (request ) + request .getRequestURI ();
144
142
}
145
-
143
+
144
+ /**
145
+ * Get Base url
146
+ *
147
+ * @param request
148
+ * @return
149
+ */
146
150
private String getBaseUrl (HttpServletRequest request ) {
147
- if (config .getBaseURL () != null )
151
+ if (config .getBaseURL () != null ) {
148
152
return config .getBaseURL ();
149
- else if ((request .getServerPort () == 80 )
153
+ } else if ((request .getServerPort () == 80 )
150
154
|| (request .getServerPort () == 443 )) {
151
155
return request .getScheme () + "://"
152
156
+ request .getServerName ();
@@ -165,7 +169,7 @@ else if ((request.getServerPort() == 80)
165
169
* @param limit
166
170
*/
167
171
private void addMeta (Model output , Resource datasetId , Resource fragmentId ,
168
- long total , long limit ) {
172
+ long total , long limit ) {
169
173
output .add (datasetId , CommonResources .RDF_TYPE , CommonResources .VOID_DATASET );
170
174
output .add (datasetId , CommonResources .RDF_TYPE , CommonResources .HYDRA_COLLECTION );
171
175
output .add (datasetId , CommonResources .VOID_SUBSET , fragmentId );
@@ -181,7 +185,6 @@ private void addMeta(Model output, Resource datasetId, Resource fragmentId,
181
185
output .add (fragmentId , CommonResources .HYDRA_ITEMSPERPAGE , limitTyped );
182
186
}
183
187
184
-
185
188
/**
186
189
* Add reference to first/previous/next page
187
190
*
@@ -195,21 +198,21 @@ private void addMeta(Model output, Resource datasetId, Resource fragmentId,
195
198
* @throws URISyntaxException
196
199
*/
197
200
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 {
199
202
URIBuilder pagedUrl = new URIBuilder (fragmentUrl );
200
203
201
204
pagedUrl .setParameter (PAGE , "1" );
202
205
output .add (fragmentId , CommonResources .HYDRA_FIRSTPAGE ,
203
- output .createResource (pagedUrl .toString ()));
206
+ output .createResource (pagedUrl .toString ()));
204
207
if (offset > 0 ) {
205
208
pagedUrl .setParameter (PAGE , Long .toString (page - 1 ));
206
209
output .add (fragmentId , CommonResources .HYDRA_PREVIOUSPAGE ,
207
- output .createResource (pagedUrl .toString ()));
210
+ output .createResource (pagedUrl .toString ()));
208
211
}
209
212
if (offset + limit < total ) {
210
213
pagedUrl .setParameter (PAGE , Long .toString (page + 1 ));
211
214
output .add (fragmentId , CommonResources .HYDRA_NEXTPAGE ,
212
- output .createResource (pagedUrl .toString ()));
215
+ output .createResource (pagedUrl .toString ()));
213
216
}
214
217
}
215
218
@@ -243,7 +246,6 @@ private void addControls(Model output, Resource datasetId, String datasetUrl) {
243
246
output .add (objectMapping , CommonResources .HYDRA_PROPERTY , CommonResources .RDF_OBJECT );
244
247
}
245
248
246
-
247
249
@ Override
248
250
public void doGet (HttpServletRequest request , HttpServletResponse response ) throws ServletException {
249
251
try {
@@ -258,8 +260,8 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
258
260
long limit = TRIPLESPERPAGE ;
259
261
long offset = limit * (page - 1 );
260
262
261
- TriplePatternFragment fragment =
262
- dataSource .getFragment (subject , predicate , object , offset , limit );
263
+ TriplePatternFragment fragment
264
+ = dataSource .getFragment (subject , predicate , object , offset , limit );
263
265
264
266
// fill the output model
265
267
Model output = fragment .getTriples ();
@@ -278,7 +280,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
278
280
addMeta (output , datasetId , fragmentId , total , limit );
279
281
addPages (output , fragmentId , fragmentUrl , total , limit , offset , page );
280
282
addControls (output , datasetId , datasetUrl );
281
-
283
+
282
284
// do conneg
283
285
String bestMatch = MIMEParse .bestMatch (mimeTypes , request .getHeader ("Accept" ));
284
286
Lang contentType = RDFLanguages .contentTypeToLang (bestMatch );
@@ -287,7 +289,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
287
289
response .setHeader ("Server" , "Linked Data Fragments Server" );
288
290
response .setContentType (bestMatch );
289
291
response .setCharacterEncoding ("utf-8" );
290
-
292
+
291
293
RDFDataMgr .write (response .getOutputStream (), output , contentType );
292
294
} catch (IOException | URISyntaxException e ) {
293
295
e .printStackTrace ();
0 commit comments