1+ import java .io .ByteArrayInputStream ;
2+ import java .io .ByteArrayOutputStream ;
3+ import java .io .File ;
4+ import java .io .IOException ;
5+ import java .io .PrintWriter ;
6+ import java .util .Enumeration ;
7+ import java .util .HashMap ;
8+ import java .util .UUID ;
9+
10+ import javax .servlet .http .HttpServlet ;
11+ import javax .servlet .http .HttpServletRequest ;
12+ import javax .servlet .http .HttpServletResponse ;
13+
14+ import org .codehaus .jackson .map .ObjectMapper ;
15+
16+ import rdf .RdfStoreManager ;
17+ import util .HttpUtils ;
18+ import util .JsonResponse ;
19+ import accounts .FrameworkUserManager ;
20+ import authentication .FrameworkConfiguration ;
21+
22+ import com .hp .hpl .jena .rdf .model .Model ;
23+ import com .hp .hpl .jena .rdf .model .ModelFactory ;
24+ import com .hp .hpl .jena .rdf .model .RDFNode ;
25+ import com .hp .hpl .jena .rdf .model .Resource ;
26+ import com .hp .hpl .jena .rdf .model .Statement ;
27+ import com .hp .hpl .jena .rdf .model .StmtIterator ;
28+
29+
30+ public class ImportRDFString extends HttpServlet {
31+
32+ /**
33+ *
34+ */
35+ private static final long serialVersionUID = 1L ;
36+ private String endpoint ;
37+ private static String uriBase ;
38+ private String graph ;
39+ private String saveString ;
40+ private String username ;
41+ private String token ;
42+
43+ private String prefixes = "@prefix lgdo: <http://linkedgeodata.org/ontology/> . "
44+ + "@prefix owl: <http://www.w3.org/2002/07/owl#> . " ;
45+
46+ public void doGet (HttpServletRequest request , HttpServletResponse response ) throws IOException {
47+ doPost (request , response );
48+ }
49+
50+ public void doPost (HttpServletRequest request , HttpServletResponse response ) throws IOException {
51+
52+ response .setContentType ("application/json" );
53+
54+ PrintWriter out = response .getWriter ();
55+
56+ // get the paht where files were uploaded
57+ String filePath = getServletContext ().getRealPath (File .separator )
58+ + getServletContext ().getInitParameter ("file-upload" ).replaceFirst ("/" , "" );
59+ filePath = "file:///" + filePath .replace ("\\ " , "/" );
60+ filePath = filePath .replace (" " , "%20" );
61+ JsonResponse res = new JsonResponse ();
62+ ObjectMapper mapper = new ObjectMapper ();
63+
64+ endpoint = request .getParameter ("params[endpoint]" );
65+ graph = request .getParameter ("params[graph]" );
66+ saveString = prefixes +request .getParameter ("params[saveString]" );
67+ username = request .getParameter ("params[username]" );
68+ token = HttpUtils .getCookieValue (request , "token" );
69+
70+ try {
71+ int inserted = stringImport (endpoint , graph , saveString );
72+ res .setStatus ("SUCCESS" );
73+ res .setMessage ("Data Imported " + inserted + " triples" );
74+ } catch (Exception e ) {
75+ res .setStatus ("FAIL" );
76+ res .setMessage (e .getMessage ());
77+ e .printStackTrace ();
78+ }
79+
80+ mapper .writeValue (out , res );
81+ out .close ();
82+ }
83+
84+ private int stringImport (String destEndpoint , String graph , String saveString )
85+ throws Exception {
86+ Model model = ModelFactory .createDefaultModel ();
87+ model .read (new ByteArrayInputStream (saveString .getBytes ()), null , "N3" );
88+ int inserted = httpUpdate (destEndpoint , graph , model );
89+ return inserted ;
90+ }
91+
92+ private int httpUpdate (String endpoint , String graph , Model model ) throws Exception {
93+ RdfStoreManager rdfStoreManager = null ;
94+ if (username != null && !username .isEmpty () && token != null && !token .isEmpty ()) {
95+ FrameworkUserManager frameworkUserManager = FrameworkConfiguration .getInstance (
96+ getServletContext ()).getFrameworkUserManager ();
97+ if (frameworkUserManager .checkToken (username , token ))
98+ rdfStoreManager = frameworkUserManager .getRdfStoreManager (username );
99+ }
100+
101+ // generate queries of 100 lines each
102+ StmtIterator stmts = model .listStatements ();
103+ int linesLimit = 100 , linesCount = 0 , total = 0 ;
104+ HashMap <String , String > blancNodes = new HashMap <String , String >();
105+
106+ Model tmpModel = ModelFactory .createDefaultModel ();
107+
108+ while (stmts .hasNext ()) {
109+
110+ if (linesCount < linesLimit ) {
111+
112+ Statement stmt = stmts .next ();
113+ Resource subject = null ;
114+ RDFNode object = null ;
115+ // find bnodes to skolemise them
116+ if (stmt .getSubject ().isAnon ()) {
117+ String oldBN = stmt .getSubject ().asNode ().getBlankNodeLabel ();
118+ if (blancNodes .containsKey (oldBN )) {
119+ subject = tmpModel .getResource (blancNodes .get (oldBN ));
120+ } else {
121+ String newBN = uriBase + "bnode#" + UUID .randomUUID ();
122+ blancNodes .put (oldBN , newBN );
123+ subject = tmpModel .createResource (newBN );
124+ }
125+ } else
126+ subject = stmt .getSubject ();
127+
128+ if (stmt .getObject ().isAnon ()) {
129+ String oldBN = stmt .getObject ().asNode ().getBlankNodeLabel ();
130+ if (blancNodes .containsKey (oldBN )) {
131+ object = tmpModel .getResource (blancNodes .get (oldBN ));
132+ } else {
133+ String newBN = uriBase + "bnode#" + UUID .randomUUID ();
134+ blancNodes .put (oldBN , newBN );
135+ object = tmpModel .createResource (newBN );
136+ }
137+ } else
138+ object = stmt .getObject ();
139+
140+ tmpModel .add (subject , stmt .getPredicate (), object );
141+ linesCount ++;
142+ } else {
143+
144+ ByteArrayOutputStream os = new ByteArrayOutputStream ();
145+ tmpModel .write (os , "N-TRIPLES" );
146+
147+ if (rdfStoreManager != null ) {
148+ String queryString = "INSERT DATA { GRAPH <" + graph + "> { " + os .toString () + " } }" ;
149+ os .close ();
150+ rdfStoreManager .execute (queryString , null );
151+ } else {
152+ String queryString = "INSERT { " + os .toString () + "}" ;
153+ os .close ();
154+
155+ HttpSPARQLUpdate p = new HttpSPARQLUpdate ();
156+ p .setEndpoint (endpoint );
157+ p .setGraph (graph );
158+ p .setUpdateString (queryString );
159+
160+ if (!p .execute ())
161+ throw new Exception ("UPDATE/SPARQL failed: " + queryString );
162+ }
163+
164+ total += linesCount ;
165+ linesCount = 0 ;
166+ tmpModel .removeAll ();
167+ }
168+
169+ }
170+
171+ if (!tmpModel .isEmpty ()) {
172+
173+ ByteArrayOutputStream os = new ByteArrayOutputStream ();
174+ tmpModel .write (os , "N-TRIPLES" );
175+
176+ if (rdfStoreManager != null ) {
177+ String queryString = "INSERT DATA { GRAPH <" + graph + "> { " + os .toString () + "} }" ;
178+ os .close ();
179+ rdfStoreManager .execute (queryString , null );
180+ } else {
181+ String queryString = "INSERT { " + os .toString () + "}" ;
182+ os .close ();
183+
184+ HttpSPARQLUpdate p = new HttpSPARQLUpdate ();
185+ p .setEndpoint (endpoint );
186+ p .setGraph (graph );
187+ p .setUpdateString (queryString );
188+
189+ if (!p .execute ())
190+ throw new Exception ("UPDATE/SPARQL failed: " + queryString );
191+ }
192+
193+ total += linesCount ;
194+
195+ }
196+
197+ return total ;
198+
199+ }
200+
201+ }
0 commit comments