11package org .linkeddatafragments .servlet ;
22
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 ;
313import java .io .File ;
414import java .io .FileReader ;
515import java .io .IOException ;
6-
716import java .net .URISyntaxException ;
8-
917import java .util .ArrayList ;
1018import java .util .Collection ;
1119import java .util .HashMap ;
1220import java .util .Map .Entry ;
1321import java .util .regex .Matcher ;
1422import java .util .regex .Pattern ;
15-
1623import javax .servlet .ServletConfig ;
1724import javax .servlet .ServletException ;
1825import javax .servlet .http .HttpServlet ;
1926import javax .servlet .http .HttpServletRequest ;
2027import javax .servlet .http .HttpServletResponse ;
21-
22- import org .apache .commons .codec .CharEncoding ;
23-
24- import org .apache .http .HttpHeaders ;
2528import org .apache .http .client .utils .URIBuilder ;
26-
2729import org .apache .jena .riot .Lang ;
2830import org .apache .jena .riot .RDFDataMgr ;
2931import 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-
4332import org .linkeddatafragments .config .ConfigReader ;
4433import org .linkeddatafragments .datasource .DataSourceFactory ;
4534import org .linkeddatafragments .datasource .IDataSource ;
35+ import org .linkeddatafragments .datasource .IndexDataSource ;
4636import org .linkeddatafragments .datasource .TriplePatternFragment ;
4737import org .linkeddatafragments .exceptions .DataSourceException ;
4838import org .linkeddatafragments .util .CommonResources ;
5646 */
5747public class TriplePatternFragmentServlet extends HttpServlet {
5848 private final static long serialVersionUID = 1L ;
59-
49+
6050 // Parameters
6151 public final static String CFGFILE = "configFile" ;
6252 public final static String SUBJ = "subject" ;
6353 public final static String PRED = "predicate" ;
6454 public final static String OBJ = "object" ;
6555 public final static String PAGE = "page" ;
66-
67-
68- private final static Pattern STRINGPATTERN =
56+
57+
58+ private final static Pattern STRINGPATTERN =
6959 Pattern .compile ("^\" (.*)\" (?:@(.*)|\\ ^\\ ^<?([^<>]*)>?)?$" );
7060 private final static TypeMapper TYPES = TypeMapper .getInstance ();
7161 private final static long TRIPLESPERPAGE = 100 ;
@@ -74,7 +64,7 @@ public class TriplePatternFragmentServlet extends HttpServlet {
7464 private final HashMap <String , IDataSource > dataSources = new HashMap <>();
7565 private final Collection <String > mimeTypes = new ArrayList <>();
7666
77-
67+
7868 private File getConfigFile (ServletConfig config ) throws IOException {
7969 String path = config .getServletContext ().getRealPath ("/" );
8070 if (path == null ) {
@@ -93,21 +83,26 @@ private File getConfigFile(ServletConfig config) throws IOException {
9383 }
9484 return cfg ;
9585 }
96-
86+
9787 @ Override
9888 public void init (ServletConfig servletConfig ) throws ServletException {
9989 try {
10090 // load the configuration
10191 File configFile = getConfigFile (servletConfig );
10292 config = new ConfigReader (new FileReader (configFile ));
103-
93+
10494 for (Entry <String , JsonObject > dataSource : config .getDataSources ().entrySet ()) {
10595 dataSources .put (dataSource .getKey (), DataSourceFactory .create (dataSource .getValue ()));
10696 }
97+
98+ String baseURL = config .getBaseURL () != null ? config .getBaseURL () : "http://localhost:8080" ;
99+
100+ IndexDataSource index = new IndexDataSource (baseURL , dataSources );
101+
107102 // register content types
108103 mimeTypes .add (Lang .TTL .getHeaderString ());
109104 mimeTypes .add (Lang .JSONLD .getHeaderString ());
110- mimeTypes .add (Lang .NTRIPLES .getHeaderString ());
105+ mimeTypes .add (Lang .NTRIPLES .getHeaderString ());
111106 mimeTypes .add (Lang .RDFXML .getHeaderString () );
112107 } catch (IOException | DataSourceException e ) {
113108 throw new ServletException (e );
@@ -116,17 +111,17 @@ public void init(ServletConfig servletConfig) throws ServletException {
116111
117112 /**
118113 * Get the datasource
119- *
114+ *
120115 * @param request
121116 * @return
122- * @throws IOException
117+ * @throws IOException
123118 */
124119 private IDataSource getDataSource (HttpServletRequest request ) throws IOException {
125120 String contextPath = request .getContextPath ();
126121 String requestURI = request .getRequestURI ();
127122
128- String path = contextPath == null
129- ? requestURI
123+ String path = contextPath == null
124+ ? requestURI
130125 : requestURI .substring (contextPath .length ());
131126 String dataSourceName = path .substring (1 );
132127 IDataSource dataSource = dataSources .get (dataSourceName );
@@ -135,12 +130,12 @@ private IDataSource getDataSource(HttpServletRequest request) throws IOException
135130 }
136131 return dataSource ;
137132 }
138-
133+
139134 /**
140135 * Get dataset url
141- *
136+ *
142137 * @param request
143- * @return
138+ * @return
144139 */
145140 private String getDatasetUrl (HttpServletRequest request ) {
146141 if ((request .getServerPort () == 80 )
@@ -156,70 +151,70 @@ private String getDatasetUrl(HttpServletRequest request) {
156151
157152 //return request.getScheme() + "://" + hostName + request.getRequestURI();
158153 }
159-
154+
160155 /**
161156 * Add total and limit
162- *
157+ *
163158 * @param output
164159 * @param fragmentId
165160 * @param total
166- * @param limit
161+ * @param limit
167162 */
168- private void addMeta (Model output , Resource datasetId , Resource fragmentId ,
163+ private void addMeta (Model output , Resource datasetId , Resource fragmentId ,
169164 long total , long limit ) {
170165 output .add (datasetId , CommonResources .RDF_TYPE , CommonResources .VOID_DATASET );
171166 output .add (datasetId , CommonResources .RDF_TYPE , CommonResources .HYDRA_COLLECTION );
172167 output .add (datasetId , CommonResources .VOID_SUBSET , fragmentId );
173-
168+
174169 output .add (fragmentId , CommonResources .RDF_TYPE , CommonResources .HYDRA_COLLECTION );
175170 output .add (fragmentId , CommonResources .RDF_TYPE , CommonResources .HYDRA_PAGEDCOLLECTION );
176-
171+
177172 Literal totalTyped = output .createTypedLiteral (total , XSDDatatype .XSDinteger );
178173 Literal limitTyped = output .createTypedLiteral (limit , XSDDatatype .XSDinteger );
179174
180175 output .add (fragmentId , CommonResources .VOID_TRIPLES , totalTyped );
181176 output .add (fragmentId , CommonResources .HYDRA_TOTALITEMS , totalTyped );
182177 output .add (fragmentId , CommonResources .HYDRA_ITEMSPERPAGE , limitTyped );
183178 }
184-
179+
185180
186181 /**
187182 * Add reference to first/previous/next page
188- *
183+ *
189184 * @param output
190185 * @param fragmentId
191186 * @param fragmentUrl
192187 * @param total
193188 * @param limit
194189 * @param offset
195190 * @param page
196- * @throws URISyntaxException
191+ * @throws URISyntaxException
197192 */
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 {
200195 URIBuilder pagedUrl = new URIBuilder (fragmentUrl );
201-
196+
202197 pagedUrl .setParameter (PAGE , "1" );
203- output .add (fragmentId , CommonResources .HYDRA_FIRSTPAGE ,
198+ output .add (fragmentId , CommonResources .HYDRA_FIRSTPAGE ,
204199 output .createResource (pagedUrl .toString ()));
205200 if (offset > 0 ) {
206201 pagedUrl .setParameter (PAGE , Long .toString (page - 1 ));
207- output .add (fragmentId , CommonResources .HYDRA_PREVIOUSPAGE ,
202+ output .add (fragmentId , CommonResources .HYDRA_PREVIOUSPAGE ,
208203 output .createResource (pagedUrl .toString ()));
209204 }
210205 if (offset + limit < total ) {
211206 pagedUrl .setParameter (PAGE , Long .toString (page + 1 ));
212- output .add (fragmentId , CommonResources .HYDRA_NEXTPAGE ,
207+ output .add (fragmentId , CommonResources .HYDRA_NEXTPAGE ,
213208 output .createResource (pagedUrl .toString ()));
214209 }
215210 }
216-
211+
217212 /**
218213 * Add controls to output
219- *
214+ *
220215 * @param output
221216 * @param datasetId
222- * @param datasetUrl
217+ * @param datasetUrl
223218 */
224219 private void addControls (Model output , Resource datasetId , String datasetUrl ) {
225220 // add controls
@@ -233,33 +228,33 @@ private void addControls(Model output, Resource datasetId, String datasetUrl) {
233228 output .add (triplePattern , CommonResources .HYDRA_MAPPING , subjectMapping );
234229 output .add (triplePattern , CommonResources .HYDRA_MAPPING , predicateMapping );
235230 output .add (triplePattern , CommonResources .HYDRA_MAPPING , objectMapping );
236-
231+
237232 output .add (subjectMapping , CommonResources .HYDRA_VARIABLE , output .createLiteral (SUBJ ));
238233 output .add (subjectMapping , CommonResources .HYDRA_PROPERTY , CommonResources .RDF_SUBJECT );
239-
234+
240235 output .add (predicateMapping , CommonResources .HYDRA_VARIABLE , output .createLiteral (PRED ));
241236 output .add (predicateMapping , CommonResources .HYDRA_PROPERTY , CommonResources .RDF_PREDICATE );
242237 output .add (objectMapping , CommonResources .HYDRA_VARIABLE , output .createLiteral (OBJ ));
243-
238+
244239 output .add (objectMapping , CommonResources .HYDRA_PROPERTY , CommonResources .RDF_OBJECT );
245240 }
246-
247-
241+
242+
248243 @ Override
249244 public void doGet (HttpServletRequest request , HttpServletResponse response ) throws ServletException {
250245 try {
251246 IDataSource dataSource = getDataSource (request );
252-
247+
253248 // query the fragment
254249 Resource subject = parseAsResource (request .getParameter (SUBJ ));
255250 Property predicate = parseAsProperty (request .getParameter (PRED ));
256251 RDFNode object = parseAsNode (request .getParameter (OBJ ));
257-
252+
258253 long page = Math .max (1 , parseAsInteger (request .getParameter (PAGE )));
259254 long limit = TRIPLESPERPAGE ;
260255 long offset = limit * (page - 1 );
261-
262- TriplePatternFragment fragment =
256+
257+ TriplePatternFragment fragment =
263258 dataSource .getFragment (subject , predicate , object , offset , limit );
264259
265260 // fill the output model
@@ -269,15 +264,15 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
269264 // add dataset metadata
270265 String datasetUrl = getDatasetUrl (request );
271266 Resource datasetId = output .createResource (datasetUrl + "#dataset" );
272-
267+
273268 String query = request .getQueryString ();
274269 String fragmentUrl = query == null ? datasetUrl : (datasetUrl + "?" + query );
275270 Resource fragmentId = output .createResource (fragmentUrl );
276271
277272 long total = fragment .getTotalSize ();
278-
273+
279274 addMeta (output , datasetId , fragmentId , total , limit );
280- addPages (output , fragmentId , fragmentUrl , total , limit , offset , page );
275+ addPages (output , fragmentId , fragmentUrl , total , limit , offset , page );
281276 addControls (output , datasetId , datasetUrl );
282277
283278 // do conneg
@@ -317,8 +312,8 @@ private int parseAsInteger(String value) {
317312 */
318313 private Resource parseAsResource (String value ) {
319314 RDFNode subject = parseAsNode (value );
320- return subject == null || subject instanceof Resource
321- ? (Resource ) subject
315+ return subject == null || subject instanceof Resource
316+ ? (Resource ) subject
322317 : CommonResources .INVALID_URI ;
323318 }
324319
0 commit comments