1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package org.openrdf.elmo.scutter;
30
31
32
33
34
35 import info.aduna.collections.iterators.Iterators;
36
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.net.MalformedURLException;
40 import java.net.URL;
41 import java.util.List;
42
43 import javax.servlet.ServletConfig;
44 import javax.servlet.ServletContext;
45 import javax.servlet.ServletException;
46 import javax.servlet.http.HttpServletRequest;
47
48 import org.apache.commons.httpclient.HttpClient;
49 import org.apache.commons.httpclient.HttpException;
50 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
51 import org.apache.commons.httpclient.methods.GetMethod;
52 import org.apache.commons.httpclient.methods.HeadMethod;
53 import org.openrdf.OpenRDFException;
54 import org.openrdf.elmo.ElmoManager;
55 import org.openrdf.elmo.ElmoManagerFactory;
56 import org.openrdf.elmo.ElmoModule;
57 import org.openrdf.elmo.RoleMapper;
58 import org.openrdf.elmo.sesame.SesameManagerFactory;
59 import org.openrdf.model.URI;
60 import org.openrdf.repository.Repository;
61 import org.openrdf.repository.RepositoryConnection;
62 import org.openrdf.repository.RepositoryException;
63 import org.openrdf.repository.http.HTTPRepository;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66
67 public class Util {
68
69
70 public final static String SERVLET_SERVER_PARAMETER = "server";
71
72 public final static String SERVLET_REPOSITORY_PARAMETER = "repository";
73
74 public final static String SERVLET_USERNAME_PARAMETER = "username";
75
76 public final static String SERVLET_PASSWORD_PARAMETER = "password";
77
78 public final static String CONTEXT_SERVER_PARAMETER = SERVLET_SERVER_PARAMETER;
79
80 public final static String CONTEXT_REPOSITORY_PARAMETER = SERVLET_REPOSITORY_PARAMETER;
81
82 public final static String CONTEXT_USERNAME_PARAMETER = SERVLET_USERNAME_PARAMETER;
83
84 public final static String CONTEXT_PASSWORD_PARAMETER = SERVLET_PASSWORD_PARAMETER;
85
86
87 public final static String REQUEST_REPOSITORY_ATTRIBUTE = "sesame";
88
89 protected static Logger _logger = LoggerFactory.getLogger(Util.class);
90
91 private static HttpClient _httpClient;
92
93 static {
94
95 MultiThreadedHttpConnectionManager mthc = new MultiThreadedHttpConnectionManager();
96 mthc.getParams().setDefaultMaxConnectionsPerHost(50);
97 mthc.getParams().setMaxTotalConnections(150);
98 _httpClient = new HttpClient(mthc);
99 }
100
101
102
103
104
105
106
107
108
109 public static InputStream getDocumentAsInputStream(String url)
110 throws HttpException, IOException {
111
112 GetMethod get = new GetMethod(url);
113 get.setFollowRedirects(true);
114 get.addRequestHeader("User-Agent",
115 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 4.0)");
116 _httpClient.executeMethod(get);
117 return get.getResponseBodyAsStream();
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131 public static String getDocumentAsString(String url) throws HttpException,
132 IOException {
133 String result = "";
134
135 GetMethod get = new GetMethod(url);
136 get.setFollowRedirects(true);
137 get.addRequestHeader("User-Agent",
138 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 4.0)");
139 try {
140 _httpClient.executeMethod(get);
141 result = get.getResponseBodyAsString();
142 } finally {
143 get.releaseConnection();
144 }
145 return result;
146 }
147
148
149
150
151
152
153
154 public static int getContentLength(String url) throws HttpException, IOException {
155 int result = 0;
156
157 HeadMethod head = new HeadMethod(url);
158 head.setFollowRedirects(true);
159 head.addRequestHeader("User-Agent",
160 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 4.0)");
161 try {
162 _httpClient.executeMethod(head);
163
164 String length =
165 head.getResponseHeader("Content-Length").getValue();
166 result = Integer.parseInt(length);
167 } catch (NumberFormatException nfe) {
168
169 } finally {
170 head.releaseConnection();
171 }
172 return result;
173 }
174
175
176 private static Repository getLocalRepository(String name) throws RepositoryException {
177
178
179
180
181
182
183
184
185 throw new RuntimeException("Not implemented");
186 }
187
188 private static Repository getRemoteRepository(String sesameServer, String repositoryID) throws RepositoryException {
189 Repository repository = new HTTPRepository(sesameServer, repositoryID);
190 repository.initialize();
191
192 return repository;
193 }
194
195 public static ElmoManager initManager(Repository repository) {
196 ElmoManagerFactory factory = new SesameManagerFactory(new ElmoModule(), repository);
197 ElmoManager manager = factory.createElmoManager();
198
199 return manager;
200 }
201
202
203
204
205
206
207
208
209
210 public static Repository initRepository(String repositoryLocation,
211 String repositoryName) throws Exception {
212 return initRepository(repositoryLocation, repositoryName, null, null);
213 }
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233 public static Repository initRepository(String repositoryLocation,
234 String repositoryName, String username, String password)
235 throws Exception {
236 Repository repository = null;
237
238
239 if ((repositoryLocation == null || repositoryLocation.equals("")) &&
240 repositoryName != null && !repositoryName.equals("")) {
241
242
243
244 repository = getLocalRepository(repositoryName);
245
246 } else {
247
248 try {
249 URL repositoryURL = new URL(repositoryLocation);
250 } catch (MalformedURLException mue) {
251 throw new Exception("Repository location contains a malformed URL");
252 }
253 if (repositoryName == null || repositoryName.equals("")) {
254 throw new Exception("Repository name missing");
255 }
256
257 repository = getRemoteRepository(repositoryLocation, repositoryName);
258
259 }
260
261 return repository;
262 }
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 public static Repository initRepository(HttpServletRequest request)
284 throws ServletException {
285 String repositoryLocation = null;
286 String repositoryName = null;
287 String userName = null;
288 String password = null;
289
290 Repository repository = null;
291
292 if (request.getAttribute(REQUEST_REPOSITORY_ATTRIBUTE) != null) {
293 repository = (Repository) request
294 .getAttribute(REQUEST_REPOSITORY_ATTRIBUTE);
295 } else {
296
297 repositoryLocation = request.getParameter(SERVLET_SERVER_PARAMETER);
298 repositoryName = request.getParameter(SERVLET_REPOSITORY_PARAMETER);
299
300 if (request.getParameter(SERVLET_USERNAME_PARAMETER) != null
301 && request.getParameter(SERVLET_PASSWORD_PARAMETER) != null) {
302
303 userName = request.getParameter(SERVLET_USERNAME_PARAMETER);
304 password = request.getParameter(SERVLET_PASSWORD_PARAMETER);
305 }
306
307 try {
308 repository = Util.initRepository(repositoryLocation,
309 repositoryName, userName, password);
310 } catch (Exception e) {
311 throw new ServletException("Repository failed to initialize", e);
312 }
313 }
314 return repository;
315 }
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339 public static Repository initRepository(ServletConfig config)
340 throws ServletException {
341 String repositoryLocation = null;
342 String repositoryName = null;
343 String userName = null;
344 String password = null;
345
346 Repository repository = null;
347
348 repositoryLocation = config.getInitParameter(SERVLET_SERVER_PARAMETER);
349 repositoryName = config.getInitParameter(SERVLET_REPOSITORY_PARAMETER);
350
351 if (config.getInitParameter(SERVLET_USERNAME_PARAMETER) != null
352 && config.getInitParameter(SERVLET_PASSWORD_PARAMETER) != null) {
353
354 userName = config.getInitParameter(SERVLET_USERNAME_PARAMETER);
355 password = config.getInitParameter(SERVLET_PASSWORD_PARAMETER);
356 }
357
358 try {
359 repository = Util.initRepository(repositoryLocation, repositoryName,
360 userName, password);
361 return repository;
362 } catch (Exception e) {
363
364 }
365
366
367 ServletContext context = config.getServletContext();
368 repositoryLocation = context.getInitParameter(CONTEXT_SERVER_PARAMETER);
369 repositoryName = context.getInitParameter(CONTEXT_REPOSITORY_PARAMETER);
370
371 if (context.getInitParameter(CONTEXT_USERNAME_PARAMETER) != null
372 && context.getInitParameter(CONTEXT_PASSWORD_PARAMETER) != null) {
373
374 userName = context.getInitParameter(CONTEXT_USERNAME_PARAMETER);
375 password = context.getInitParameter(CONTEXT_PASSWORD_PARAMETER);
376 } else {
377 userName = null;
378 password = null;
379 }
380
381 try {
382 repository = Util.initRepository(repositoryLocation, repositoryName,
383 userName, password);
384 } catch (Exception e) {
385 throw new ServletException("Repository failed to initialize", e);
386 }
387
388 return repository;
389 }
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419 public static Repository initRepository(ServletConfig config,
420 HttpServletRequest request) throws ServletException {
421 Repository repository = null;
422
423 try {
424 repository = initRepository(request);
425
426 } catch (ServletException se) {
427
428 }
429
430 if (repository == null) {
431 repository = initRepository(config);
432 }
433 return repository;
434 }
435
436 public static void copyRepositoryContent(Repository source, Repository target)
437 throws OpenRDFException, IOException {
438
439 RepositoryConnection sourceCon = null;
440 RepositoryConnection targetCon = null;
441
442 try {
443 sourceCon = source.getConnection();
444 targetCon = target.getConnection();
445 targetCon.add(sourceCon.getStatements(null, null, null, true));
446 }
447 finally {
448 if (sourceCon != null)
449 sourceCon.close();
450 if (targetCon != null)
451 targetCon.close();
452
453 }
454 }
455
456
457
458
459
460
461
462 public static URI getType(Class c) {
463
464 SesameManagerFactory factory = new SesameManagerFactory(
465 new ElmoModule());
466 try {
467 RoleMapper<URI> mapper = factory.getRoleMapper();
468 return mapper.findType(c);
469 } finally {
470 factory.close();
471 }
472 }
473
474
475
476
477
478
479
480
481 public static <T extends org.openrdf.concepts.rdfs.Resource> List<T> getAllInstances(ElmoManager manager, Class<T> cl) {
482
483
484 Iterable<T> query = manager.findAll(cl);
485 return Iterators.asList(query.iterator());
486 }
487
488
489 }