1
1
package org .linkeddatafragments .servlets ;
2
2
3
+ import java .util .HashMap ;
4
+ import java .util .Map .Entry ;
3
5
import java .util .regex .Matcher ;
4
6
import java .util .regex .Pattern ;
5
7
9
11
import javax .servlet .http .HttpServletRequest ;
10
12
import javax .servlet .http .HttpServletResponse ;
11
13
14
+ import org .linkeddatafragments .config .ConfigReader ;
12
15
import org .rdfhdt .hdt .hdt .HDT ;
13
16
import org .rdfhdt .hdt .hdt .HDTManager ;
14
17
import org .rdfhdt .hdtjena .HDTGraph ;
@@ -29,15 +32,18 @@ public class BasicLdfServlet extends HttpServlet {
29
32
private final static int TRIPLES_PER_PAGE = 100 ;
30
33
private final static Pattern STRINGPATTERN = Pattern .compile ("^\" (.*)\" (?:\\ ^\\ ^<(.*)>|@(.*))?$" );
31
34
32
- private Model model ;
35
+ private ConfigReader config ;
36
+ private HashMap <String , Model > dataSources = new HashMap <String , Model >();
33
37
34
38
@ Override
35
- public void init (ServletConfig config ) throws ServletException {
39
+ public void init (ServletConfig servletConfig ) throws ServletException {
36
40
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
+ config = new ConfigReader (servletConfig .getInitParameter ("configFile" ));
42
+ for (Entry <String , String > dataSource : config .getDataSources ().entrySet ()) {
43
+ final HDT hdt = HDTManager .mapIndexedHDT (dataSource .getValue (), null );
44
+ final Model model = ModelFactory .createModelForGraph (new HDTGraph (hdt ));
45
+ dataSources .put (dataSource .getKey (), model );
46
+ }
41
47
}
42
48
catch (Exception e ) {
43
49
throw new ServletException (e );
@@ -47,16 +53,24 @@ public void init(ServletConfig config) throws ServletException {
47
53
@ Override
48
54
public void doGet (HttpServletRequest request , HttpServletResponse response ) throws ServletException {
49
55
try {
56
+ // find the data source
50
57
final String path = request .getRequestURI ().substring (request .getContextPath ().length ());
58
+ final String dataSourceName = path .substring (1 );
59
+ final Model dataSource = dataSources .get (dataSourceName );
60
+ if (dataSource == null )
61
+ throw new Exception ("data source not found" );
51
62
52
- // parse the subject, predicate, and object parameters
63
+ // create the output model
53
64
final Model output = ModelFactory .createDefaultModel ();
65
+ output .setNsPrefixes (config .getPrefixes ());
66
+
67
+ // parse the subject, predicate, and object parameters
54
68
final Resource subject = parseAsResource (request .getParameter ("subject" ), output );
55
69
final Property predicate = parseAsProperty (request .getParameter ("predicate" ), output );
56
70
final RDFNode object = parseAsNode (request .getParameter ("object" ), output );
57
71
58
72
// add all statements with the given parameters to the output model
59
- final StmtIterator statements = model .listStatements (subject , predicate , object );
73
+ final StmtIterator statements = dataSource .listStatements (subject , predicate , object );
60
74
for (int i = 0 ; i < TRIPLES_PER_PAGE && statements .hasNext (); i ++)
61
75
output .add (statements .next ());
62
76
0 commit comments