Revision 3514
Added by barteau about 17 years ago
src/edu/ucsb/nceas/metacat/MetaCatServlet.java | ||
---|---|---|
122 | 122 |
* action value is quite specific. This documentation will be reorganized to |
123 | 123 |
* reflect this information. |
124 | 124 |
*/ |
125 |
public class MetaCatServlet extends HttpServlet |
|
126 |
{ |
|
125 |
public class MetaCatServlet extends HttpServlet { |
|
127 | 126 |
private static Hashtable sessionHash = new Hashtable(); |
128 | 127 |
private Timer timer = null; |
129 | 128 |
private static boolean sitemapScheduled; |
... | ... | |
140 | 139 |
public static final String EML2KEYWORD = ":eml"; |
141 | 140 |
public static final String XMLFORMAT = "xml"; |
142 | 141 |
private static final String CONFIG_DIR = "WEB-INF"; |
143 |
private static final String CONFIG_NAME = "metacat.properties";
|
|
142 |
private static final String CONFIG_NAME = "metacat.properties"; |
|
144 | 143 |
|
145 | 144 |
/** |
146 | 145 |
* Initialize the servlet by creating appropriate database connections |
147 | 146 |
*/ |
148 |
public void init(ServletConfig config) throws ServletException |
|
149 |
{ |
|
147 |
public void init(ServletConfig config) throws ServletException { |
|
150 | 148 |
try { |
151 | 149 |
super.init(config); |
152 | 150 |
Logger logMetacat = Logger.getLogger(MetaCatServlet.class); |
153 | 151 |
|
154 | 152 |
ServletContext context = config.getServletContext(); |
155 |
|
|
153 |
|
|
156 | 154 |
// Initialize the properties file for our options |
157 | 155 |
String dirPath = context.getRealPath(CONFIG_DIR); |
158 | 156 |
File propertyFile = new File(dirPath, CONFIG_NAME); |
... | ... | |
167 | 165 |
logMetacat.error("Error in loading options: " |
168 | 166 |
+ ioe.getMessage()); |
169 | 167 |
} |
170 |
|
|
168 |
|
|
171 | 169 |
// Check if the servlet was configured, and if not stop the |
172 |
// initialization and skip to the configuration web form when
|
|
170 |
// initialization and skip to the configuration web form when |
|
173 | 171 |
// metacat is called |
174 | 172 |
boolean configured = options.getOption("configured").equals("true"); |
175 | 173 |
if (!configured) { |
... | ... | |
184 | 182 |
|
185 | 183 |
// Index the paths specified in the metacat.properties |
186 | 184 |
checkIndexPaths(); |
187 |
|
|
185 |
|
|
188 | 186 |
// initiate the indexing Queue |
189 | 187 |
IndexingQueue.getInstance(); |
190 | 188 |
|
191 |
// start the IndexingThread if indexingTimerTaskTime more than 0.
|
|
192 |
// It will index all the documents not yet indexed in the database
|
|
189 |
// start the IndexingThread if indexingTimerTaskTime more than 0. |
|
190 |
// It will index all the documents not yet indexed in the database |
|
193 | 191 |
int indexingTimerTaskTime = Integer.parseInt(MetaCatUtil.getOption("indexingTimerTaskTime")); |
194 | 192 |
int delayTime = Integer.parseInt(MetaCatUtil.getOption("indexingInitialDelay")); |
195 |
|
|
193 |
|
|
196 | 194 |
if(indexingTimerTaskTime > 0){ |
197 |
timer = new Timer();
|
|
198 |
timer.schedule(new IndexingTimerTask(), delayTime, indexingTimerTaskTime);
|
|
195 |
timer = new Timer();
|
|
196 |
timer.schedule(new IndexingTimerTask(), delayTime, indexingTimerTaskTime);
|
|
199 | 197 |
} |
200 | 198 |
|
201 |
// read the config files:
|
|
199 |
// read the config files: |
|
202 | 200 |
Vector skins = MetaCatUtil.getOptionList(MetaCatUtil.getOption("skinconfigfiles")); |
203 | 201 |
String skinName, skinDirPath = null; |
204 | 202 |
File skinPropertyFile = null; |
205 |
|
|
203 |
|
|
206 | 204 |
for (int i = 0; i < skins.size(); i++) { |
207 |
skinName = (String) skins.elementAt(i);
|
|
205 |
skinName = (String) skins.elementAt(i);
|
|
208 | 206 |
skinDirPath = context.getRealPath(CONFIG_DIR + "/skin.configs/" + skinName); |
209 |
skinPropertyFile = new File(skinDirPath, skinName + ".properties");
|
|
207 |
skinPropertyFile = new File(skinDirPath, skinName + ".properties"); |
|
210 | 208 |
Properties skinOption = null; |
211 | 209 |
try { |
212 |
skinOption = new Properties();
|
|
210 |
skinOption = new Properties();
|
|
213 | 211 |
FileInputStream fis = new FileInputStream(skinPropertyFile); |
214 | 212 |
skinOption.load(fis); |
215 | 213 |
fis.close(); |
216 | 214 |
} catch (IOException ioe) { |
217 | 215 |
logMetacat.error("Error in loading options for skin " + "skinName" +" : " |
218 | 216 |
+ ioe.getMessage()); |
219 |
}
|
|
217 |
} |
|
220 | 218 |
MetaCatUtil.skinconfigs.put(skinName, skinOption); |
221 | 219 |
} |
222 |
|
|
220 |
|
|
223 | 221 |
/* |
224 | 222 |
* If spatial option is turned on and set to regenerate the |
225 |
* spatial cache on restart, trigger the harvester regeneratation method
|
|
223 |
* spatial cache on restart, trigger the harvester regeneratation method |
|
226 | 224 |
*/ |
227 | 225 |
if ( MetaCatUtil.getOption("runSpatialOption").equals("true") && |
228 |
MetaCatUtil.getOption("regenerateCacheOnRestart").equals("true") ) { |
|
229 |
|
|
226 |
MetaCatUtil.getOption("regenerateCacheOnRestart").equals("true") ) {
|
|
227 |
|
|
230 | 228 |
// Begin timer |
231 | 229 |
long before = System.currentTimeMillis(); |
232 |
|
|
230 |
|
|
233 | 231 |
// regenerate the entire spatial cache |
234 | 232 |
// may be expensive with many documents |
235 | 233 |
SpatialHarvester sh = new SpatialHarvester(); |
236 | 234 |
sh.regenerate(); |
237 | 235 |
sh.destroy(); |
238 |
|
|
239 |
// After running the first time, we want to to set regenerateCacheOnRestart to false
|
|
240 |
// so that it does not regenerate the cache every time tomcat is restarted
|
|
241 |
MetaCatUtil.setOption("regenerateCacheOnRestart","false");
|
|
242 |
|
|
236 |
|
|
237 |
// After running the first time, we want to to set regenerateCacheOnRestart to false
|
|
238 |
// so that it does not regenerate the cache every time tomcat is restarted
|
|
239 |
MetaCatUtil.setOption("regenerateCacheOnRestart","false");
|
|
240 |
|
|
243 | 241 |
// End timer |
244 | 242 |
long after = System.currentTimeMillis(); |
245 | 243 |
logMetacat.info(" ------ Spatial Harvester Time " + (after - before) + "ms"); |
246 |
|
|
247 |
} else {
|
|
244 |
|
|
245 |
} else {
|
|
248 | 246 |
logMetacat.info(" \n **** Spatial cache is not set to regenerate on restart"); |
249 | 247 |
} |
250 | 248 |
|
251 | 249 |
sitemapScheduled = false; |
252 | 250 |
|
253 | 251 |
logMetacat.info("Metacat (" + Version.getVersion() |
254 |
+ ") initialized.");
|
|
255 |
|
|
252 |
+ ") initialized."); |
|
253 |
|
|
256 | 254 |
} catch (ServletException ex) { |
257 | 255 |
throw ex; |
258 | 256 |
} catch (SQLException e) { |
... | ... | |
265 | 263 |
+ ie.getMessage()); |
266 | 264 |
} |
267 | 265 |
} |
268 |
|
|
266 |
|
|
269 | 267 |
/** |
270 | 268 |
* Close all db connections from the pool |
271 | 269 |
*/ |
272 |
public void destroy() |
|
273 |
{ |
|
270 |
public void destroy() { |
|
274 | 271 |
// Close all db connection |
275 | 272 |
System.out.println("Destroying MetacatServlet"); |
276 | 273 |
timer.cancel(); |
277 | 274 |
IndexingQueue.getInstance().setMetacatRunning(false); |
278 | 275 |
DBConnectionPool.release(); |
279 | 276 |
} |
280 |
|
|
277 |
|
|
281 | 278 |
/** Handle "GET" method requests from HTTP clients */ |
282 | 279 |
public void doGet(HttpServletRequest request, HttpServletResponse response) |
283 |
throws ServletException, IOException |
|
284 |
{ |
|
285 |
|
|
280 |
throws ServletException, IOException { |
|
281 |
|
|
286 | 282 |
// Process the data and send back the response |
287 | 283 |
handleGetOrPost(request, response); |
288 | 284 |
} |
289 |
|
|
285 |
|
|
290 | 286 |
/** Handle "POST" method requests from HTTP clients */ |
291 | 287 |
public void doPost(HttpServletRequest request, HttpServletResponse response) |
292 |
throws ServletException, IOException |
|
293 |
{ |
|
294 |
|
|
288 |
throws ServletException, IOException { |
|
289 |
|
|
295 | 290 |
// Process the data and send back the response |
296 | 291 |
handleGetOrPost(request, response); |
297 | 292 |
} |
298 |
|
|
293 |
|
|
299 | 294 |
/** |
300 | 295 |
* Index the paths specified in the metacat.properties |
301 | 296 |
*/ |
302 | 297 |
private void checkIndexPaths() { |
303 | 298 |
Logger logMetacat = Logger.getLogger(MetaCatServlet.class); |
304 | 299 |
MetaCatUtil.pathsForIndexing |
305 |
= MetaCatUtil.getOptionList(MetaCatUtil.getOption("indexPaths")); |
|
306 |
|
|
300 |
= MetaCatUtil.getOptionList(MetaCatUtil.getOption("indexPaths"));
|
|
301 |
|
|
307 | 302 |
if (MetaCatUtil.pathsForIndexing != null) { |
308 |
|
|
303 |
|
|
309 | 304 |
logMetacat.debug("Indexing paths specified in metacat.properties...."); |
310 |
|
|
305 |
|
|
311 | 306 |
DBConnection conn = null; |
312 | 307 |
int serialNumber = -1; |
313 | 308 |
PreparedStatement pstmt = null; |
314 | 309 |
PreparedStatement pstmt1 = null; |
315 | 310 |
ResultSet rs = null; |
316 |
|
|
311 |
|
|
317 | 312 |
for (int i = 0; i < MetaCatUtil.pathsForIndexing.size(); i++) { |
318 | 313 |
logMetacat.debug("Checking if '" |
319 |
+ (String) MetaCatUtil.pathsForIndexing.elementAt(i)
|
|
320 |
+ "' is indexed.... ");
|
|
321 |
|
|
314 |
+ (String) MetaCatUtil.pathsForIndexing.elementAt(i) |
|
315 |
+ "' is indexed.... "); |
|
316 |
|
|
322 | 317 |
try { |
323 | 318 |
//check out DBConnection |
324 | 319 |
conn = DBConnectionPool. |
325 |
getDBConnection("MetaCatServlet.checkIndexPaths"); |
|
320 |
getDBConnection("MetaCatServlet.checkIndexPaths");
|
|
326 | 321 |
serialNumber = conn.getCheckOutSerialNumber(); |
327 |
|
|
322 |
|
|
328 | 323 |
pstmt = conn.prepareStatement( |
329 |
"SELECT * FROM xml_path_index " + "WHERE path = ?"); |
|
324 |
"SELECT * FROM xml_path_index " + "WHERE path = ?");
|
|
330 | 325 |
pstmt.setString(1, (String) MetaCatUtil.pathsForIndexing |
331 |
.elementAt(i));
|
|
332 |
|
|
326 |
.elementAt(i)); |
|
327 |
|
|
333 | 328 |
pstmt.execute(); |
334 | 329 |
rs = pstmt.getResultSet(); |
335 |
|
|
330 |
|
|
336 | 331 |
if (!rs.next()) { |
337 | 332 |
logMetacat.debug(".....not indexed yet."); |
338 | 333 |
rs.close(); |
339 | 334 |
pstmt.close(); |
340 | 335 |
conn.increaseUsageCount(1); |
341 |
|
|
336 |
|
|
342 | 337 |
logMetacat.debug( |
343 |
"Inserting following path in xml_path_index: " |
|
344 |
+ (String)MetaCatUtil.pathsForIndexing |
|
345 |
.elementAt(i));
|
|
346 |
if(((String)MetaCatUtil.pathsForIndexing.elementAt(i)).indexOf("@")<0){
|
|
347 |
pstmt = conn.prepareStatement("SELECT DISTINCT n.docid, "
|
|
348 |
+ "n.nodedata, n.nodedatanumerical, n.parentnodeid"
|
|
349 |
+ " FROM xml_nodes n, xml_index i WHERE"
|
|
350 |
+ " i.path = ? and n.parentnodeid=i.nodeid and"
|
|
351 |
+ " n.nodetype LIKE 'TEXT' order by n.parentnodeid");
|
|
352 |
} else {
|
|
353 |
pstmt = conn.prepareStatement("SELECT DISTINCT n.docid, "
|
|
354 |
+ "n.nodedata, n.nodedatanumerical, n.parentnodeid"
|
|
355 |
+ " FROM xml_nodes n, xml_index i WHERE"
|
|
356 |
+ " i.path = ? and n.nodeid=i.nodeid and"
|
|
357 |
+ " n.nodetype LIKE 'ATTRIBUTE' order by n.parentnodeid");
|
|
358 |
}
|
|
338 |
"Inserting following path in xml_path_index: "
|
|
339 |
+ (String)MetaCatUtil.pathsForIndexing
|
|
340 |
.elementAt(i)); |
|
341 |
if(((String)MetaCatUtil.pathsForIndexing.elementAt(i)).indexOf("@")<0){
|
|
342 |
pstmt = conn.prepareStatement("SELECT DISTINCT n.docid, "
|
|
343 |
+ "n.nodedata, n.nodedatanumerical, n.parentnodeid"
|
|
344 |
+ " FROM xml_nodes n, xml_index i WHERE"
|
|
345 |
+ " i.path = ? and n.parentnodeid=i.nodeid and"
|
|
346 |
+ " n.nodetype LIKE 'TEXT' order by n.parentnodeid");
|
|
347 |
} else {
|
|
348 |
pstmt = conn.prepareStatement("SELECT DISTINCT n.docid, "
|
|
349 |
+ "n.nodedata, n.nodedatanumerical, n.parentnodeid"
|
|
350 |
+ " FROM xml_nodes n, xml_index i WHERE"
|
|
351 |
+ " i.path = ? and n.nodeid=i.nodeid and"
|
|
352 |
+ " n.nodetype LIKE 'ATTRIBUTE' order by n.parentnodeid");
|
|
353 |
}
|
|
359 | 354 |
pstmt.setString(1, (String) MetaCatUtil. |
360 |
pathsForIndexing.elementAt(i));
|
|
355 |
pathsForIndexing.elementAt(i)); |
|
361 | 356 |
pstmt.execute(); |
362 | 357 |
rs = pstmt.getResultSet(); |
363 |
|
|
358 |
|
|
364 | 359 |
int count = 0; |
365 | 360 |
logMetacat.debug( |
366 |
"Executed the select statement for: "
|
|
367 |
+ (String) MetaCatUtil.pathsForIndexing
|
|
368 |
.elementAt(i));
|
|
369 |
|
|
361 |
"Executed the select statement for: " |
|
362 |
+ (String) MetaCatUtil.pathsForIndexing |
|
363 |
.elementAt(i)); |
|
364 |
|
|
370 | 365 |
try { |
371 | 366 |
while (rs.next()) { |
372 |
|
|
367 |
|
|
373 | 368 |
String docid = rs.getString(1); |
374 | 369 |
String nodedata = rs.getString(2); |
375 | 370 |
float nodedatanumerical = rs.getFloat(3); |
376 | 371 |
int parentnodeid = rs.getInt(4); |
377 |
|
|
372 |
|
|
378 | 373 |
if (!nodedata.trim().equals("")) { |
379 | 374 |
pstmt1 = conn.prepareStatement( |
380 |
"INSERT INTO xml_path_index" |
|
381 |
+ " (docid, path, nodedata, " |
|
382 |
+ "nodedatanumerical, parentnodeid)" |
|
383 |
+ " VALUES (?, ?, ?, ?, ?)"); |
|
384 |
|
|
375 |
"INSERT INTO xml_path_index"
|
|
376 |
+ " (docid, path, nodedata, "
|
|
377 |
+ "nodedatanumerical, parentnodeid)"
|
|
378 |
+ " VALUES (?, ?, ?, ?, ?)");
|
|
379 |
|
|
385 | 380 |
pstmt1.setString(1, docid); |
386 | 381 |
pstmt1.setString(2, (String) MetaCatUtil. |
387 |
pathsForIndexing.elementAt(i));
|
|
382 |
pathsForIndexing.elementAt(i)); |
|
388 | 383 |
pstmt1.setString(3, nodedata); |
389 | 384 |
pstmt1.setFloat(4, nodedatanumerical); |
390 | 385 |
pstmt1.setInt(5, parentnodeid); |
391 |
|
|
386 |
|
|
392 | 387 |
pstmt1.execute(); |
393 | 388 |
pstmt1.close(); |
394 |
|
|
389 |
|
|
395 | 390 |
count++; |
396 |
|
|
391 |
|
|
397 | 392 |
} |
398 | 393 |
} |
399 |
} |
|
400 |
catch (Exception e) { |
|
394 |
} catch (Exception e) { |
|
401 | 395 |
System.out.println("Exception:" + e.getMessage()); |
402 | 396 |
e.printStackTrace(); |
403 | 397 |
} |
404 |
|
|
398 |
|
|
405 | 399 |
rs.close(); |
406 | 400 |
pstmt.close(); |
407 | 401 |
conn.increaseUsageCount(1); |
408 |
|
|
402 |
|
|
409 | 403 |
logMetacat.info("Indexed " + count |
410 | 404 |
+ " records from xml_nodes for '" |
411 | 405 |
+ (String) MetaCatUtil.pathsForIndexing.elementAt(i) |
412 | 406 |
+ "'"); |
413 |
|
|
407 |
|
|
414 | 408 |
} else { |
415 |
logMetacat.debug(".....already indexed.");
|
|
409 |
logMetacat.debug(".....already indexed.");
|
|
416 | 410 |
} |
417 |
|
|
411 |
|
|
418 | 412 |
rs.close(); |
419 | 413 |
pstmt.close(); |
420 | 414 |
conn.increaseUsageCount(1); |
421 |
|
|
415 |
|
|
422 | 416 |
} catch (Exception e) { |
423 | 417 |
logMetacat.error("Error in MetaCatServlet.checkIndexPaths: " |
424 |
+ e.getMessage());
|
|
418 |
+ e.getMessage()); |
|
425 | 419 |
}finally { |
426 | 420 |
//check in DBonnection |
427 | 421 |
DBConnectionPool.returnDBConnection(conn, serialNumber); |
428 | 422 |
} |
429 |
|
|
430 |
|
|
423 |
|
|
424 |
|
|
431 | 425 |
} |
432 |
|
|
426 |
|
|
433 | 427 |
logMetacat.debug("Path Indexing Completed"); |
434 | 428 |
} |
435 |
}
|
|
436 |
|
|
429 |
} |
|
430 |
|
|
437 | 431 |
/** |
438 | 432 |
* Control servlet response depending on the action parameter specified |
439 | 433 |
*/ |
440 | 434 |
private void handleGetOrPost(HttpServletRequest request, |
441 |
HttpServletResponse response) throws ServletException, IOException |
|
442 |
{ |
|
435 |
HttpServletResponse response) throws ServletException, IOException { |
|
443 | 436 |
MetaCatUtil util = new MetaCatUtil(); |
444 | 437 |
Logger logMetacat = Logger.getLogger(MetaCatServlet.class); |
445 | 438 |
|
... | ... | |
460 | 453 |
//size is greater than initial value, shrink the connection pool |
461 | 454 |
//size to initial value |
462 | 455 |
DBConnectionPool.shrinkDBConnectionPoolSize(); |
463 |
|
|
456 |
|
|
464 | 457 |
//Debug message to print out the method which have a busy DBConnection |
465 | 458 |
try { |
466 | 459 |
DBConnectionPool pool = DBConnectionPool.getInstance(); |
... | ... | |
470 | 463 |
+ e.getMessage()); |
471 | 464 |
e.printStackTrace(); |
472 | 465 |
} |
473 |
|
|
466 |
|
|
474 | 467 |
String ctype = request.getContentType(); |
475 | 468 |
if (ctype != null && ctype.startsWith("multipart/form-data")) { |
476 | 469 |
handleMultipartForm(request, response); |
477 | 470 |
} else { |
478 |
|
|
471 |
|
|
479 | 472 |
String name = null; |
480 | 473 |
String[] value = null; |
481 | 474 |
String[] docid = new String[3]; |
... | ... | |
487 | 480 |
// e.g., http://localhost:8180/knb/metacat/test.1.1/knb |
488 | 481 |
String pathInfo = request.getPathInfo(); |
489 | 482 |
if (pathInfo != null) { |
490 |
String[] path = pathInfo.split("/");
|
|
491 |
if (path.length > 1) {
|
|
492 |
String docidToRead = path[1];
|
|
493 |
String docs[] = new String[1];
|
|
494 |
docs[0] = docidToRead;
|
|
495 |
logMetacat.debug("READING DOCID FROM PATHINFO: " + docs[0]);
|
|
496 |
params.put("docid", docs);
|
|
497 |
String skin = null;
|
|
498 |
if (path.length > 2) {
|
|
499 |
skin = path[2];
|
|
500 |
String skins[] = new String[1];
|
|
501 |
skins[0] = skin;
|
|
502 |
params.put("qformat", skins);
|
|
503 |
}
|
|
483 |
String[] path = pathInfo.split("/");
|
|
484 |
if (path.length > 1) {
|
|
485 |
String docidToRead = path[1];
|
|
486 |
String docs[] = new String[1];
|
|
487 |
docs[0] = docidToRead;
|
|
488 |
logMetacat.debug("READING DOCID FROM PATHINFO: " + docs[0]);
|
|
489 |
params.put("docid", docs);
|
|
490 |
String skin = null;
|
|
491 |
if (path.length > 2) {
|
|
492 |
skin = path[2];
|
|
493 |
String skins[] = new String[1];
|
|
494 |
skins[0] = skin;
|
|
495 |
params.put("qformat", skins);
|
|
496 |
}
|
|
504 | 497 |
handleReadAction(params, request, response, "public", null, null); |
505 | 498 |
return; |
506 |
}
|
|
499 |
}
|
|
507 | 500 |
} |
508 | 501 |
|
509 | 502 |
Enumeration paramlist = request.getParameterNames(); |
510 | 503 |
while (paramlist.hasMoreElements()) { |
511 |
|
|
504 |
|
|
512 | 505 |
name = (String) paramlist.nextElement(); |
513 | 506 |
value = request.getParameterValues(name); |
514 |
|
|
507 |
|
|
515 | 508 |
// Decode the docid and mouse click information |
516 | 509 |
// THIS IS OBSOLETE -- I THINK -- REMOVE THIS BLOCK 4/12/2007 MBJ |
517 | 510 |
if (name.endsWith(".y")) { |
... | ... | |
522 | 515 |
if (name.endsWith(".x")) { |
523 | 516 |
name = "xpos"; |
524 | 517 |
} |
525 |
|
|
518 |
|
|
526 | 519 |
params.put(name, value); |
527 | 520 |
} |
528 |
|
|
521 |
|
|
529 | 522 |
//handle param is emptpy |
530 | 523 |
if (params.isEmpty() || params == null) { return; } |
531 |
|
|
524 |
|
|
532 | 525 |
//if the user clicked on the input images, decode which image |
533 | 526 |
//was clicked then set the action. |
534 | 527 |
if(params.get("action") == null){ |
... | ... | |
541 | 534 |
out.close(); |
542 | 535 |
return; |
543 | 536 |
} |
544 |
|
|
537 |
|
|
545 | 538 |
String action = ((String[]) params.get("action"))[0]; |
546 | 539 |
logMetacat.info("Action is: " + action); |
547 |
|
|
540 |
|
|
548 | 541 |
// This block handles session management for the servlet |
549 | 542 |
// by looking up the current session information for all actions |
550 | 543 |
// other than "login" and "logout" |
... | ... | |
559 | 552 |
PrintWriter out = response.getWriter(); |
560 | 553 |
handleLoginAction(out, params, request, response); |
561 | 554 |
out.close(); |
562 |
|
|
555 |
|
|
563 | 556 |
// handle logout action |
564 | 557 |
} else if (action.equals("logout")) { |
565 | 558 |
PrintWriter out = response.getWriter(); |
566 | 559 |
handleLogoutAction(out, params, request, response); |
567 | 560 |
out.close(); |
568 |
|
|
561 |
|
|
569 | 562 |
// handle shrink DBConnection request |
570 | 563 |
} else if (action.equals("shrink")) { |
571 | 564 |
PrintWriter out = response.getWriter(); |
... | ... | |
585 | 578 |
} |
586 | 579 |
//close out put |
587 | 580 |
out.close(); |
588 |
|
|
581 |
|
|
589 | 582 |
// aware of session expiration on every request |
590 | 583 |
} else { |
591 | 584 |
HttpSession sess = request.getSession(true); |
... | ... | |
597 | 590 |
username = "public"; |
598 | 591 |
sess.setAttribute("username", username); |
599 | 592 |
sess_id = (String)sess.getId(); |
600 |
sess.setAttribute("sessionid", sess_id);
|
|
593 |
sess.setAttribute("sessionid", sess_id); |
|
601 | 594 |
sessionHash.put(sess_id, sess); |
602 | 595 |
} else { |
603 | 596 |
logMetacat.info("The session is either old or " |
... | ... | |
613 | 606 |
sess = (HttpSession) sessionHash.get(sess_id); |
614 | 607 |
} |
615 | 608 |
} else { |
616 |
//this makes sure there is a session object for public sessions |
|
617 |
sess_id = sess.getId(); |
|
618 |
sessionHash.put(sess_id, sess); |
|
609 |
//this makes sure there is a session object for public sessions
|
|
610 |
sess_id = sess.getId();
|
|
611 |
sessionHash.put(sess_id, sess);
|
|
619 | 612 |
} |
620 | 613 |
} catch (IllegalStateException ise) { |
621 | 614 |
logMetacat.error( |
... | ... | |
623 | 616 |
+ "happen: the session should be valid: " |
624 | 617 |
+ ise.getMessage()); |
625 | 618 |
} |
626 |
|
|
619 |
|
|
627 | 620 |
username = (String) sess.getAttribute("username"); |
628 | 621 |
logMetacat.info("The user name from session is: " |
629 | 622 |
+ username); |
... | ... | |
658 | 651 |
out.close(); |
659 | 652 |
} |
660 | 653 |
} else if ( action.trim().equals("spatial_query")) { |
661 |
|
|
662 |
logMetacat.debug("******************* SPATIAL QUERY ********************"); |
|
663 |
PrintWriter out = response.getWriter(); |
|
664 |
handleSpatialQuery(out, params, response, username, groupnames, sess_id); |
|
665 |
out.close(); |
|
666 |
|
|
654 |
|
|
655 |
logMetacat.debug("******************* SPATIAL QUERY ********************");
|
|
656 |
PrintWriter out = response.getWriter();
|
|
657 |
handleSpatialQuery(out, params, response, username, groupnames, sess_id);
|
|
658 |
out.close();
|
|
659 |
|
|
667 | 660 |
} else if (action.equals("export")) { |
668 |
|
|
661 |
|
|
669 | 662 |
handleExportAction(params, response, username, |
670 | 663 |
groupnames, password); |
671 | 664 |
} else if (action.equals("read")) { |
... | ... | |
758 | 751 |
out.println(username); |
759 | 752 |
out.println("\n</username>\n"); |
760 | 753 |
if(name!=null){ |
761 |
out.println("\n<name>\n");
|
|
762 |
out.println(name);
|
|
763 |
out.println("\n</name>\n");
|
|
754 |
out.println("\n<name>\n");
|
|
755 |
out.println(name);
|
|
756 |
out.println("\n</name>\n");
|
|
764 | 757 |
} |
765 | 758 |
if(MetaCatUtil.isAdministrator(username, groupnames)){ |
766 |
out.println("<isAdministrator></isAdministrator>\n");
|
|
759 |
out.println("<isAdministrator></isAdministrator>\n");
|
|
767 | 760 |
} |
768 | 761 |
if(MetaCatUtil.isModerator(username, groupnames)){ |
769 |
out.println("<isModerator></isModerator>\n");
|
|
762 |
out.println("<isModerator></isModerator>\n");
|
|
770 | 763 |
} |
771 | 764 |
out.println("\n</user>\n"); |
772 | 765 |
out.close(); |
... | ... | |
819 | 812 |
mue.printStackTrace(out); |
820 | 813 |
out.close(); |
821 | 814 |
} |
822 |
*/ |
|
815 |
*/
|
|
823 | 816 |
} else { |
824 | 817 |
PrintWriter out = response.getWriter(); |
825 | 818 |
out.println("<?xml version=\"1.0\"?>"); |
826 | 819 |
out.println("<error>"); |
827 | 820 |
out.println( |
828 |
"Error: action not registered. Please report this error."); |
|
821 |
"Error: action not registered. Please report this error.");
|
|
829 | 822 |
out.println("</error>"); |
830 | 823 |
out.close(); |
831 | 824 |
} |
832 |
|
|
825 |
|
|
833 | 826 |
//util.closeConnections(); |
834 | 827 |
// Close the stream to the client |
835 | 828 |
//out.close(); |
... | ... | |
838 | 831 |
scheduleSitemapGeneration(request); |
839 | 832 |
} |
840 | 833 |
} |
841 |
|
|
834 |
|
|
842 | 835 |
/////////////////////////////// METACAT SPATIAL /////////////////////////// |
843 |
|
|
836 |
|
|
844 | 837 |
/** |
845 |
* handles all spatial queries -- these queries may include any of the
|
|
838 |
* handles all spatial queries -- these queries may include any of the |
|
846 | 839 |
* queries supported by the WFS / WMS standards |
847 |
*
|
|
840 |
* |
|
848 | 841 |
* handleSQuery(out, params, response, username, groupnames, |
849 | 842 |
* sess_id); |
850 | 843 |
*/ |
851 |
private void handleSpatialQuery(PrintWriter out, Hashtable params,
|
|
852 |
HttpServletResponse response,
|
|
853 |
String username, String[] groupnames,
|
|
854 |
String sess_id) {
|
|
855 |
|
|
844 |
private void handleSpatialQuery(PrintWriter out, Hashtable params, |
|
845 |
HttpServletResponse response, |
|
846 |
String username, String[] groupnames,
|
|
847 |
String sess_id) { |
|
848 |
|
|
856 | 849 |
Logger logMetacat = Logger.getLogger(MetaCatServlet.class); |
857 |
|
|
858 |
if ( !MetaCatUtil.getOption("runSpatialOption").equals("true") ) {
|
|
859 |
response.setContentType("text/html");
|
|
860 |
out.println("<html> Metacat Spatial Option is turned off </html>");
|
|
861 |
out.close();
|
|
862 |
return ;
|
|
863 |
}
|
|
864 |
|
|
865 |
/*
|
|
850 |
|
|
851 |
if ( !MetaCatUtil.getOption("runSpatialOption").equals("true") ) {
|
|
852 |
response.setContentType("text/html");
|
|
853 |
out.println("<html> Metacat Spatial Option is turned off </html>");
|
|
854 |
out.close();
|
|
855 |
return ;
|
|
856 |
}
|
|
857 |
|
|
858 |
/* |
|
866 | 859 |
* Perform spatial query against spatial cache |
867 | 860 |
*/ |
868 | 861 |
float _xmax = Float.valueOf( ((String[]) params.get("xmax"))[0] ).floatValue(); |
... | ... | |
873 | 866 |
Vector docids = sq.filterByBbox( _xmin, _ymin, _xmax, _ymax ); |
874 | 867 |
// logMetacat.info(" --- Spatial Query completed. Passing on the SQuery handler"); |
875 | 868 |
// logMetacat.warn("\n\n ******* after spatial query, we've got " + docids.size() + " docids \n\n"); |
876 |
|
|
869 |
|
|
877 | 870 |
/* |
878 | 871 |
* Create an array matching docids |
879 | 872 |
*/ |
880 | 873 |
String [] docidArray = new String[docids.size()]; |
881 | 874 |
docids.toArray(docidArray); |
882 |
|
|
875 |
|
|
883 | 876 |
/* |
884 | 877 |
* Create squery string |
885 | 878 |
*/ |
... | ... | |
888 | 881 |
String[] queryArray = new String[1]; |
889 | 882 |
queryArray[0] = squery; |
890 | 883 |
params.put("query", queryArray); |
891 |
|
|
884 |
|
|
892 | 885 |
/* |
893 | 886 |
* Determine qformat |
894 | 887 |
*/ |
... | ... | |
902 | 895 |
qformatArray[0] = "knp"; |
903 | 896 |
} |
904 | 897 |
params.put("qformat", qformatArray); |
905 |
|
|
898 |
|
|
906 | 899 |
// change the action |
907 | 900 |
String[] actionArray = new String[1]; |
908 | 901 |
actionArray[0] = "squery"; |
909 | 902 |
params.put("action", actionArray); |
910 |
|
|
903 |
|
|
911 | 904 |
/* |
912 | 905 |
* Pass the docids to the DBQuery contructor |
913 | 906 |
*/ |
... | ... | |
915 | 908 |
// Otherwise dbquery sees no docidOverrides and does a full % percent query |
916 | 909 |
if (docids.size() == 0) |
917 | 910 |
docids.add(""); |
918 |
|
|
911 |
|
|
919 | 912 |
DBQuery queryobj = new DBQuery(docids); |
920 | 913 |
queryobj.findDocuments(response, out, params, username, groupnames, sess_id); |
921 |
|
|
922 |
} |
|
923 |
|
|
914 |
|
|
915 |
}
|
|
916 |
|
|
924 | 917 |
// LOGIN & LOGOUT SECTION |
925 | 918 |
/** |
926 | 919 |
* Handle the login request. Create a new session object. Do user |
927 | 920 |
* authentication through the session. |
928 | 921 |
*/ |
929 | 922 |
private void handleLoginAction(PrintWriter out, Hashtable params, |
930 |
HttpServletRequest request, HttpServletResponse response) |
|
931 |
{ |
|
923 |
HttpServletRequest request, HttpServletResponse response) { |
|
932 | 924 |
Logger logMetacat = Logger.getLogger(MetaCatServlet.class); |
933 | 925 |
AuthSession sess = null; |
934 |
|
|
926 |
|
|
935 | 927 |
if(params.get("username") == null){ |
936 | 928 |
response.setContentType("text/xml"); |
937 | 929 |
out.println("<?xml version=\"1.0\"?>"); |
... | ... | |
940 | 932 |
out.println("</error>"); |
941 | 933 |
return; |
942 | 934 |
} |
943 |
|
|
935 |
|
|
944 | 936 |
//} |
945 |
|
|
937 |
|
|
946 | 938 |
if(params.get("password") == null){ |
947 | 939 |
response.setContentType("text/xml"); |
948 | 940 |
out.println("<?xml version=\"1.0\"?>"); |
... | ... | |
951 | 943 |
out.println("</error>"); |
952 | 944 |
return; |
953 | 945 |
} |
954 |
|
|
946 |
|
|
955 | 947 |
String un = ((String[]) params.get("username"))[0]; |
956 | 948 |
logMetacat.info("user " + un + " is trying to login"); |
957 | 949 |
String pw = ((String[]) params.get("password"))[0]; |
958 |
|
|
950 |
|
|
959 | 951 |
String qformat = "xml"; |
960 | 952 |
if(params.get("qformat") != null){ |
961 | 953 |
qformat = ((String[]) params.get("qformat"))[0]; |
962 | 954 |
} |
963 |
|
|
955 |
|
|
964 | 956 |
try { |
965 | 957 |
sess = new AuthSession(); |
966 | 958 |
} catch (Exception e) { |
... | ... | |
970 | 962 |
return; |
971 | 963 |
} |
972 | 964 |
boolean isValid = sess.authenticate(request, un, pw); |
973 |
|
|
965 |
|
|
974 | 966 |
//if it is authernticate is true, store the session |
975 | 967 |
if (isValid) { |
976 | 968 |
HttpSession session = sess.getSessions(); |
... | ... | |
980 | 972 |
+ " into hash in login method"); |
981 | 973 |
sessionHash.put(id, session); |
982 | 974 |
} |
983 |
|
|
975 |
|
|
984 | 976 |
// format and transform the output |
985 | 977 |
if (qformat.equals("xml")) { |
986 | 978 |
response.setContentType("text/xml"); |
... | ... | |
993 | 985 |
"-//NCEAS//login//EN", "-//W3C//HTML//EN", qformat, |
994 | 986 |
out, null); |
995 | 987 |
} catch (Exception e) { |
996 |
|
|
988 |
|
|
997 | 989 |
logMetacat.error( |
998 | 990 |
"Error in MetaCatServlet.handleLoginAction: " |
999 |
+ e.getMessage());
|
|
991 |
+ e.getMessage()); |
|
1000 | 992 |
} |
1001 | 993 |
} |
1002 | 994 |
} |
1003 |
|
|
995 |
|
|
1004 | 996 |
/** |
1005 | 997 |
* Handle the logout request. Close the connection. |
1006 | 998 |
*/ |
1007 | 999 |
private void handleLogoutAction(PrintWriter out, Hashtable params, |
1008 |
HttpServletRequest request, HttpServletResponse response) |
|
1009 |
{ |
|
1000 |
HttpServletRequest request, HttpServletResponse response) { |
|
1010 | 1001 |
Logger logMetacat = Logger.getLogger(MetaCatServlet.class); |
1011 | 1002 |
String qformat = "xml"; |
1012 | 1003 |
if(params.get("qformat") != null){ |
1013 | 1004 |
qformat = ((String[]) params.get("qformat"))[0]; |
1014 | 1005 |
} |
1015 |
|
|
1006 |
|
|
1016 | 1007 |
// close the connection |
1017 | 1008 |
HttpSession sess = request.getSession(false); |
1018 | 1009 |
logMetacat.info("After get session in logout request"); |
1019 | 1010 |
if (sess != null) { |
1020 | 1011 |
logMetacat.info("The session id " + sess.getId() |
1021 |
+ " will be invalidate in logout action");
|
|
1012 |
+ " will be invalidate in logout action"); |
|
1022 | 1013 |
logMetacat.info("The session contains user " |
1023 | 1014 |
+ sess.getAttribute("username") |
1024 | 1015 |
+ " will be invalidate in logout action"); |
1025 | 1016 |
sess.invalidate(); |
1026 | 1017 |
} |
1027 |
|
|
1018 |
|
|
1028 | 1019 |
// produce output |
1029 | 1020 |
StringBuffer output = new StringBuffer(); |
1030 | 1021 |
output.append("<?xml version=\"1.0\"?>"); |
1031 | 1022 |
output.append("<logout>"); |
1032 | 1023 |
output.append("User logged out"); |
1033 | 1024 |
output.append("</logout>"); |
1034 |
|
|
1025 |
|
|
1035 | 1026 |
//format and transform the output |
1036 | 1027 |
if (qformat.equals("xml")) { |
1037 | 1028 |
response.setContentType("text/xml"); |
... | ... | |
1046 | 1037 |
} catch (Exception e) { |
1047 | 1038 |
logMetacat.error( |
1048 | 1039 |
"Error in MetaCatServlet.handleLogoutAction" |
1049 |
+ e.getMessage());
|
|
1040 |
+ e.getMessage()); |
|
1050 | 1041 |
} |
1051 | 1042 |
} |
1052 | 1043 |
} |
1053 |
|
|
1044 |
|
|
1054 | 1045 |
// END OF LOGIN & LOGOUT SECTION |
1055 |
|
|
1046 |
|
|
1056 | 1047 |
// SQUERY & QUERY SECTION |
1057 | 1048 |
/** |
1058 | 1049 |
* Retreive the squery xml, execute it and display it |
... | ... | |
1065 | 1056 |
*/ |
1066 | 1057 |
private void handleSQuery(PrintWriter out, Hashtable params, |
1067 | 1058 |
HttpServletResponse response, String user, String[] groups, |
1068 |
String sessionid) |
|
1069 |
{ |
|
1059 |
String sessionid) { |
|
1070 | 1060 |
double startTime = System.currentTimeMillis() / 1000; |
1071 | 1061 |
DBQuery queryobj = new DBQuery(); |
1072 | 1062 |
queryobj.findDocuments(response, out, params, user, groups, sessionid); |
... | ... | |
1075 | 1065 |
logMetacat.warn("Total search time for action 'squery': " |
1076 | 1066 |
+ (outPutTime - startTime)); |
1077 | 1067 |
MetaCatUtil.writeDebugToFile("---------------------------------------------------------------------------------"+ |
1078 |
"-------------------------------------------------------------------------------Total search time for "
|
|
1068 |
"-------------------------------------------------------------------------------Total search time for "
|
|
1079 | 1069 |
+ (outPutTime - startTime)); |
1080 | 1070 |
MetaCatUtil.writeDebugToDelimiteredFile(" "+ (outPutTime - startTime), true); |
1081 | 1071 |
} |
1082 |
|
|
1072 |
|
|
1083 | 1073 |
/** |
1084 | 1074 |
* Create the xml query, execute it and display the results. |
1085 | 1075 |
* |
... | ... | |
1090 | 1080 |
*/ |
1091 | 1081 |
private void handleQuery(PrintWriter out, Hashtable params, |
1092 | 1082 |
HttpServletResponse response, String user, String[] groups, |
1093 |
String sessionid) |
|
1094 |
{ |
|
1083 |
String sessionid) { |
|
1095 | 1084 |
//create the query and run it |
1096 | 1085 |
String xmlquery = DBQuery.createSQuery(params); |
1097 | 1086 |
String[] queryArray = new String[1]; |
... | ... | |
1105 | 1094 |
logMetacat.warn("Total search time for action 'query': " |
1106 | 1095 |
+ (outPutTime - startTime)); |
1107 | 1096 |
MetaCatUtil.writeDebugToFile("---------------------------------------------------------------------------------"+ |
1108 |
"-------------------------------------------------------------------------------Total search time is "
|
|
1097 |
"-------------------------------------------------------------------------------Total search time is "
|
|
1109 | 1098 |
+ (outPutTime - startTime)); |
1110 | 1099 |
MetaCatUtil.writeDebugToDelimiteredFile(" "+ (outPutTime - startTime), true); |
1111 |
|
|
1100 |
|
|
1112 | 1101 |
//handleSQuery(out, params, response,user, groups, sessionid); |
1113 | 1102 |
} |
1114 |
|
|
1103 |
|
|
1115 | 1104 |
// END OF SQUERY & QUERY SECTION |
1116 |
|
|
1105 |
|
|
1117 | 1106 |
//Exoport section |
1118 | 1107 |
/** |
1119 | 1108 |
* Handle the "export" request of data package from Metacat in zip format |
... | ... | |
1125 | 1114 |
*/ |
1126 | 1115 |
private void handleExportAction(Hashtable params, |
1127 | 1116 |
HttpServletResponse response, |
1128 |
String user, String[] groups, String passWord) |
|
1129 |
{ |
|
1117 |
String user, String[] groups, String passWord) { |
|
1130 | 1118 |
Logger logMetacat = Logger.getLogger(MetaCatServlet.class); |
1131 | 1119 |
// Output stream |
1132 | 1120 |
ServletOutputStream out = null; |
1133 | 1121 |
// Zip output stream |
1134 | 1122 |
ZipOutputStream zOut = null; |
1135 | 1123 |
DBQuery queryObj = null; |
1136 |
|
|
1124 |
|
|
1137 | 1125 |
String[] docs = new String[10]; |
1138 | 1126 |
String docId = ""; |
1139 |
|
|
1127 |
|
|
1140 | 1128 |
try { |
1141 | 1129 |
// read the params |
1142 | 1130 |
if (params.containsKey("docid")) { |
... | ... | |
1163 | 1151 |
// Get output stream |
1164 | 1152 |
out = response.getOutputStream(); |
1165 | 1153 |
response.setContentType("application/zip"); //MIME type |
1166 |
response.setHeader("Content-Disposition",
|
|
1167 |
"attachment; filename="
|
|
1168 |
+ docId + ".zip"); // Set the name of the zip file
|
|
1169 |
|
|
1154 |
response.setHeader("Content-Disposition", |
|
1155 |
"attachment; filename="
|
|
1156 |
+ docId + ".zip"); // Set the name of the zip file
|
|
1157 |
|
|
1170 | 1158 |
zOut = new ZipOutputStream(out); |
1171 | 1159 |
zOut = queryObj |
1172 | 1160 |
.getZippedPackage(docId, out, user, groups, passWord); |
1173 | 1161 |
zOut.finish(); //terminate the zip file |
1174 | 1162 |
zOut.close(); //close the zip stream |
1175 |
|
|
1163 |
|
|
1176 | 1164 |
} catch (Exception e) { |
1177 | 1165 |
try { |
1178 | 1166 |
response.setContentType("text/xml"); //MIME type |
... | ... | |
1197 | 1185 |
+ "in MetacatServlet.handleExportAction: " |
1198 | 1186 |
+ ioe.getMessage()); |
1199 | 1187 |
} |
1200 |
|
|
1188 |
|
|
1201 | 1189 |
logMetacat.error( |
1202 | 1190 |
"Error in MetacatServlet.handleExportAction: " |
1203 |
+ e.getMessage());
|
|
1191 |
+ e.getMessage()); |
|
1204 | 1192 |
e.printStackTrace(System.out); |
1205 |
|
|
1193 |
|
|
1206 | 1194 |
} |
1207 |
|
|
1195 |
|
|
1208 | 1196 |
} |
1209 |
|
|
1197 |
|
|
1210 | 1198 |
/** |
1211 | 1199 |
* In eml2 document, the xml can have inline data and data was stripped off |
1212 | 1200 |
* and store in file system. This action can be used to read inline data |
... | ... | |
1219 | 1207 |
*/ |
1220 | 1208 |
private void handleReadInlineDataAction(Hashtable params, |
1221 | 1209 |
HttpServletRequest request, HttpServletResponse response, |
1222 |
String user, String passWord, String[] groups) |
|
1223 |
{ |
|
1210 |
String user, String passWord, String[] groups) { |
|
1224 | 1211 |
Logger logMetacat = Logger.getLogger(MetaCatServlet.class); |
1225 | 1212 |
String[] docs = new String[10]; |
1226 | 1213 |
String inlineDataId = null; |
1227 | 1214 |
String docId = ""; |
1228 | 1215 |
ServletOutputStream out = null; |
1229 |
|
|
1216 |
|
|
1230 | 1217 |
try { |
1231 | 1218 |
// read the params |
1232 | 1219 |
if (params.containsKey("inlinedataid")) { |
... | ... | |
1237 | 1224 |
// Make sure the client specify docid |
1238 | 1225 |
if (inlineDataId == null || inlineDataId.equals("")) { |
1239 | 1226 |
throw new Exception("You didn't specify requested inlinedataid"); } |
1240 |
|
|
1227 |
|
|
1241 | 1228 |
// check for permission |
1242 | 1229 |
docId = MetaCatUtil |
1243 | 1230 |
.getDocIdWithoutRevFromInlineDataID(inlineDataId); |
1244 | 1231 |
PermissionController controller = new PermissionController(docId); |
1245 | 1232 |
// check top level read permission |
1246 | 1233 |
if (!controller.hasPermission(user, groups, |
1247 |
AccessControlInterface.READSTRING)) |
|
1248 |
{ |
|
1234 |
AccessControlInterface.READSTRING)) { |
|
1249 | 1235 |
throw new Exception("User " + user |
1250 | 1236 |
+ " doesn't have permission " + " to read document " |
1251 | 1237 |
+ docId); |
1252 |
} |
|
1253 |
else |
|
1254 |
{ |
|
1255 |
//check data access level |
|
1256 |
try |
|
1257 |
{ |
|
1258 |
Hashtable unReadableInlineDataList = |
|
1259 |
PermissionController.getUnReadableInlineDataIdList(docId, |
|
1260 |
user, groups, false); |
|
1261 |
if (unReadableInlineDataList.containsValue( |
|
1262 |
MetaCatUtil.getInlineDataIdWithoutRev(inlineDataId))) |
|
1263 |
{ |
|
1264 |
throw new Exception("User " + user |
|
1265 |
+ " doesn't have permission " + " to read inlinedata " |
|
1266 |
+ inlineDataId); |
|
1267 |
|
|
1268 |
}//if |
|
1269 |
}//try |
|
1270 |
catch (Exception e) |
|
1271 |
{ |
|
1272 |
throw e; |
|
1273 |
}//catch |
|
1238 |
} else { |
|
1239 |
//check data access level |
|
1240 |
try { |
|
1241 |
Hashtable unReadableInlineDataList = |
|
1242 |
PermissionController.getUnReadableInlineDataIdList(docId, |
|
1243 |
user, groups, false); |
|
1244 |
if (unReadableInlineDataList.containsValue( |
|
1245 |
MetaCatUtil.getInlineDataIdWithoutRev(inlineDataId))) { |
|
1246 |
throw new Exception("User " + user |
|
1247 |
+ " doesn't have permission " + " to read inlinedata " |
|
1248 |
+ inlineDataId); |
|
1249 |
|
|
1250 |
}//if |
|
1251 |
}//try |
|
1252 |
catch (Exception e) { |
|
1253 |
throw e; |
|
1254 |
}//catch |
|
1274 | 1255 |
}//else |
1275 |
|
|
1256 |
|
|
1276 | 1257 |
// Get output stream |
1277 | 1258 |
out = response.getOutputStream(); |
1278 | 1259 |
// read the inline data from the file |
... | ... | |
1286 | 1267 |
bytes = input.read(buffer); |
1287 | 1268 |
} |
1288 | 1269 |
out.close(); |
1289 |
|
|
1270 |
|
|
1290 | 1271 |
EventLog.getInstance().log(request.getRemoteAddr(), user, |
1291 | 1272 |
inlineDataId, "readinlinedata"); |
1292 | 1273 |
} catch (Exception e) { |
... | ... | |
1315 | 1296 |
} |
1316 | 1297 |
logMetacat.error( |
1317 | 1298 |
"Error in MetacatServlet.handleReadInlineDataAction: " |
1318 |
+ e.getMessage());
|
|
1299 |
+ e.getMessage()); |
|
1319 | 1300 |
} |
1320 | 1301 |
} |
1321 |
|
|
1302 |
|
|
1322 | 1303 |
/* |
1323 | 1304 |
* Get the nodeid from xml_nodes for the inlinedataid |
1324 | 1305 |
*/ |
1325 | 1306 |
private long getInlineDataNodeId(String inLineDataId, String docId) |
1326 |
throws SQLException |
|
1327 |
{ |
|
1307 |
throws SQLException { |
|
1328 | 1308 |
Logger logMetacat = Logger.getLogger(MetaCatServlet.class); |
1329 | 1309 |
long nodeId = 0; |
1330 | 1310 |
String INLINE = "inline"; |
... | ... | |
1336 | 1316 |
+ "AND nodetype='TEXT' AND parentnodeid IN " |
1337 | 1317 |
+ "(SELECT nodeid FROM xml_nodes WHERE docid=? AND " |
1338 | 1318 |
+ "nodetype='ELEMENT' AND nodename='" + INLINE + "')"; |
1339 |
|
|
1319 |
|
|
1340 | 1320 |
try { |
1341 | 1321 |
//check out DBConnection |
1342 | 1322 |
conn = DBConnectionPool |
1343 | 1323 |
.getDBConnection("AccessControlList.isAllowFirst"); |
1344 | 1324 |
serialNumber = conn.getCheckOutSerialNumber(); |
1345 |
|
|
1325 |
|
|
1346 | 1326 |
pStmt = conn.prepareStatement(sql); |
1347 | 1327 |
//bind value |
1348 | 1328 |
pStmt.setString(1, docId);//docid |
... | ... | |
1356 | 1336 |
if (hasRow) { |
1357 | 1337 |
nodeId = rs.getLong(1); |
1358 | 1338 |
}//if |
1359 |
|
|
1339 |
|
|
1360 | 1340 |
} catch (SQLException e) { |
1361 | 1341 |
throw e; |
1362 | 1342 |
} finally { |
... | ... | |
1370 | 1350 |
+ " is: " + nodeId); |
1371 | 1351 |
return nodeId; |
1372 | 1352 |
} |
1373 |
|
|
1353 |
|
|
1374 | 1354 |
/** |
1375 | 1355 |
* Handle the "read" request of metadata/data files from Metacat or any |
1376 | 1356 |
* files from Internet; transformed metadata XML document into HTML |
... | ... | |
1384 | 1364 |
*/ |
1385 | 1365 |
private void handleReadAction(Hashtable params, HttpServletRequest request, |
1386 | 1366 |
HttpServletResponse response, String user, String passWord, |
1387 |
String[] groups) |
|
1388 |
{ |
|
1367 |
String[] groups) { |
|
1389 | 1368 |
Logger logMetacat = Logger.getLogger(MetaCatServlet.class); |
1390 | 1369 |
ServletOutputStream out = null; |
1391 | 1370 |
ZipOutputStream zout = null; |
1392 | 1371 |
PrintWriter pw = null; |
1393 | 1372 |
boolean zip = false; |
1394 | 1373 |
boolean withInlineData = true; |
1395 |
|
|
1374 |
|
|
1396 | 1375 |
try { |
1397 | 1376 |
String[] docs = new String[0]; |
1398 | 1377 |
String docid = ""; |
1399 | 1378 |
String qformat = ""; |
1400 | 1379 |
String abstrpath = null; |
1401 |
|
|
1380 |
|
|
1402 | 1381 |
// read the params |
1403 | 1382 |
if (params.containsKey("docid")) { |
1404 | 1383 |
docs = (String[]) params.get("docid"); |
... | ... | |
1409 | 1388 |
// the param for only metadata (eml) |
1410 | 1389 |
// we don't support read a eml document without inline data now. |
1411 | 1390 |
/*if (params.containsKey("inlinedata")) { |
1412 |
|
|
1391 |
|
|
1413 | 1392 |
String inlineData = ((String[]) params.get("inlinedata"))[0]; |
1414 | 1393 |
if (inlineData.equalsIgnoreCase("false")) { |
1415 | 1394 |
withInlineData = false; |
... | ... | |
1424 | 1403 |
// go through the list of docs to read |
1425 | 1404 |
for (int i = 0; i < docs.length; i++) { |
1426 | 1405 |
try { |
1427 |
|
|
1406 |
|
|
1428 | 1407 |
URL murl = new URL(docs[i]); |
1429 | 1408 |
Hashtable murlQueryStr = MetaCatUtil.parseQuery( |
1430 | 1409 |
murl.getQuery()); |
... | ... | |
1440 | 1419 |
abstrpath, user, groups, zip, zout, |
1441 | 1420 |
withInlineData, params); |
1442 | 1421 |
} |
1443 |
|
|
1422 |
|
|
1444 | 1423 |
// case docid="http://.../filename" |
1445 | 1424 |
} else { |
1446 | 1425 |
docid = docs[i]; |
... | ... | |
1450 | 1429 |
readFromURLConnection(response, docid); |
1451 | 1430 |
} |
1452 | 1431 |
} |
1453 |
|
|
1432 |
|
|
1454 | 1433 |
} catch (MalformedURLException mue) { |
1455 | 1434 |
docid = docs[i]; |
1456 | 1435 |
if (zip) { |
... | ... | |
1462 | 1441 |
} |
1463 | 1442 |
} |
1464 | 1443 |
} |
1465 |
|
|
1444 |
|
|
1466 | 1445 |
if (zip) { |
1467 | 1446 |
zout.finish(); //terminate the zip file |
1468 | 1447 |
zout.close(); //close the zip stream |
1469 | 1448 |
} |
1470 |
|
|
1449 |
|
|
1471 | 1450 |
} catch (McdbDocNotFoundException notFoundE) { |
1472 | 1451 |
// To handle doc not found exception |
1473 | 1452 |
// the docid which didn't be found |
... | ... | |
1487 | 1466 |
if (out != null) { |
1488 | 1467 |
out.close(); |
1489 | 1468 |
} |
1490 |
|
|
1469 |
|
|
1491 | 1470 |
} catch (Exception exc) { |
1492 | 1471 |
logMetacat.error( |
1493 | 1472 |
"Erorr in MetacatServlet.hanldReadAction: " |
1494 |
+ exc.getMessage());
|
|
1473 |
+ exc.getMessage()); |
|
1495 | 1474 |
try { |
1496 | 1475 |
if (out != null) { |
1497 | 1476 |
response.setContentType("text/xml"); |
... | ... | |
1503 | 1482 |
pw.println("</error>"); |
1504 | 1483 |
pw.close(); |
1505 | 1484 |
out.close(); |
1506 |
|
|
1485 |
|
|
1507 | 1486 |
} else { |
1508 | 1487 |
response.setContentType("text/xml"); //MIME type |
1509 | 1488 |
// Send back error message if out = null |
... | ... | |
1529 | 1508 |
} |
1530 | 1509 |
} catch (Exception e) { |
1531 | 1510 |
try { |
1532 |
|
|
1511 |
|
|
1533 | 1512 |
if (out != null) { |
1534 | 1513 |
response.setContentType("text/xml"); //MIME type |
1535 | 1514 |
pw = new PrintWriter(out); |
... | ... | |
1550 | 1529 |
pw.println(e.getMessage()); |
1551 | 1530 |
pw.println("</error>"); |
1552 | 1531 |
pw.close(); |
1553 |
|
|
1532 |
|
|
1554 | 1533 |
} |
1555 | 1534 |
// Close zip output stream |
1556 | 1535 |
if (zout != null) { |
1557 | 1536 |
zout.close(); |
1558 | 1537 |
} |
1559 |
|
|
1538 |
|
|
1560 | 1539 |
} catch (IOException ioe) { |
1561 | 1540 |
logMetacat.error("Problem with the servlet output " |
1562 | 1541 |
+ "in MetacatServlet.handleReadAction: " |
1563 | 1542 |
+ ioe.getMessage()); |
1564 | 1543 |
ioe.printStackTrace(System.out); |
1565 |
|
|
1544 |
|
|
1566 | 1545 |
} |
1567 |
|
|
1546 |
|
|
1568 | 1547 |
logMetacat.error( |
1569 | 1548 |
"Error in MetacatServlet.handleReadAction: " |
1570 |
+ e.getMessage());
|
|
1549 |
+ e.getMessage()); |
|
1571 | 1550 |
//e.printStackTrace(System.out); |
1572 | 1551 |
} |
1573 | 1552 |
} |
1574 |
|
|
1553 |
|
|
1575 | 1554 |
/** read metadata or data from Metacat |
1576 | 1555 |
*/ |
1577 | 1556 |
private void readFromMetacat(HttpServletRequest request, |
... | ... | |
1579 | 1558 |
String abstrpath, String user, String[] groups, boolean zip, |
1580 | 1559 |
ZipOutputStream zout, boolean withInlineData, Hashtable params) |
1581 | 1560 |
throws ClassNotFoundException, IOException, SQLException, |
1582 |
McdbException, Exception |
|
1583 |
{ |
|
1561 |
McdbException, Exception { |
|
1584 | 1562 |
Logger logMetacat = Logger.getLogger(MetaCatServlet.class); |
1585 | 1563 |
try { |
1586 | 1564 |
|
... | ... | |
1591 | 1569 |
// seperator here, if only one, we will append the rev in xml_documents |
1592 | 1570 |
// to the id. |
1593 | 1571 |
docid = appendRev(docid); |
1594 |
|
|
1572 |
|
|
1595 | 1573 |
DocumentImpl doc = new DocumentImpl(docid); |
1596 |
|
|
1574 |
|
|
1597 | 1575 |
//check the permission for read |
1598 | 1576 |
if (!DocumentImpl.hasReadPermission(user, groups, docid)) { |
1599 | 1577 |
Exception e = new Exception("User " + user |
1600 | 1578 |
+ " does not have permission" |
1601 | 1579 |
+ " to read the document with the docid " + docid); |
1602 |
|
|
1580 |
|
|
1603 | 1581 |
throw e; |
1604 | 1582 |
} |
1605 |
|
|
1583 |
|
|
1606 | 1584 |
if (doc.getRootNodeID() == 0) { |
1607 | 1585 |
// this is data file |
1608 | 1586 |
String filepath = MetaCatUtil.getOption("datafilepath"); |
... | ... | |
1612 | 1590 |
String filename = filepath + docid; |
1613 | 1591 |
FileInputStream fin = null; |
1614 | 1592 |
fin = new FileInputStream(filename); |
1615 |
|
|
1593 |
|
|
1616 | 1594 |
//MIME type |
1617 | 1595 |
String contentType = getServletContext().getMimeType(filename); |
1618 | 1596 |
if (contentType == null) { |
... | ... | |
1622 | 1600 |
logMetacat.info("Final contenttype is: " |
1623 | 1601 |
+ contentType); |
1624 | 1602 |
} |
1625 |
|
|
1603 |
|
|
1626 | 1604 |
response.setContentType(contentType); |
1627 | 1605 |
// if we decide to use "application/octet-stream" for all data |
1628 | 1606 |
// returns |
1629 | 1607 |
// response.setContentType("application/octet-stream"); |
1630 |
|
|
1631 |
// Set the name of the data file to the entity name plus docid,
|
|
1608 |
|
|
1609 |
// Set the name of the data file to the entity name plus docid, |
|
1632 | 1610 |
// or if that is unavailable, use the docid alone |
1633 | 1611 |
String docname = doc.getDocname(); |
1634 | 1612 |
if (docname == null || docname.equals("")) { |
... | ... | |
1636 | 1614 |
} else { |
1637 | 1615 |
docname = docid + "-" + docname; |
1638 | 1616 |
} |
1639 |
response.setHeader("Content-Disposition",
|
|
1617 |
response.setHeader("Content-Disposition", |
|
1640 | 1618 |
"attachment; filename=" + docname); |
1641 | 1619 |
|
1642 | 1620 |
try { |
... | ... | |
1650 | 1628 |
} finally { |
1651 | 1629 |
if (fin != null) fin.close(); |
1652 | 1630 |
} |
1653 |
|
|
1631 |
|
|
1654 | 1632 |
} else { |
1655 | 1633 |
// this is metadata doc |
1656 | 1634 |
if (qformat.equals("xml") || qformat.equals("")) { |
... | ... | |
1658 | 1636 |
// by default the document should be returned in xml format |
1659 | 1637 |
// set content type first |
1660 | 1638 |
response.setContentType("text/xml"); //MIME type |
1661 |
response.setHeader("Content-Disposition",
|
|
1639 |
response.setHeader("Content-Disposition", |
|
1662 | 1640 |
"attachment; filename=" + docid + ".xml"); |
1663 | 1641 |
PrintWriter out = response.getWriter(); |
1664 | 1642 |
doc.toXml(out, user, groups, withInlineData); |
1665 | 1643 |
} else { |
1644 |
//*** This is a metadata doc, to be returned in a skin/custom format. |
|
1645 |
|
|
1646 |
//*** Add param to indicate if public has read access or not. |
|
1647 |
if (!user.equals("public")) { |
|
1648 |
if (DocumentImpl.hasReadPermission("public", null, docid)) |
|
1649 |
params.put("publicRead", new String[] {"true"}); |
|
1650 |
else |
|
1651 |
params.put("publicRead", new String[] {"false"}); |
|
1652 |
} |
|
1653 |
|
|
1666 | 1654 |
response.setContentType("text/html"); //MIME type |
1667 | 1655 |
PrintWriter out = response.getWriter(); |
1668 |
|
|
1656 |
|
|
1669 | 1657 |
// Look up the document type |
1670 | 1658 |
String doctype = doc.getDoctype(); |
1671 | 1659 |
// Transform the document to the new doctype |
... | ... | |
1674 | 1662 |
withInlineData), doctype, "-//W3C//HTML//EN", |
1675 | 1663 |
qformat, out, params); |
1676 | 1664 |
} |
1677 |
|
|
1665 |
|
|
1678 | 1666 |
} |
1679 | 1667 |
EventLog.getInstance().log(request.getRemoteAddr(), user, |
1680 | 1668 |
docid, "read"); |
... | ... | |
1682 | 1670 |
throw except; |
1683 | 1671 |
} |
1684 | 1672 |
} |
1685 |
|
|
1673 |
|
|
1686 | 1674 |
/** |
1687 | 1675 |
* read data from URLConnection |
1688 | 1676 |
*/ |
1689 | 1677 |
private void readFromURLConnection(HttpServletResponse response, |
1690 |
String docid) throws IOException, MalformedURLException |
|
1691 |
{ |
|
1678 |
String docid) throws IOException, MalformedURLException { |
|
1692 | 1679 |
ServletOutputStream out = response.getOutputStream(); |
1693 | 1680 |
String contentType = getServletContext().getMimeType(docid); //MIME |
1694 |
// type
|
|
1681 |
// type |
|
1695 | 1682 |
if (contentType == null) { |
1696 | 1683 |
if (docid.endsWith(".xml")) { |
1697 | 1684 |
contentType = "text/xml"; |
... | ... | |
1715 | 1702 |
response.setContentType(contentType); |
1716 | 1703 |
// if we decide to use "application/octet-stream" for all data returns |
1717 | 1704 |
// response.setContentType("application/octet-stream"); |
1718 |
|
|
1705 |
|
|
1719 | 1706 |
// this is http url |
1720 | 1707 |
URL url = new URL(docid); |
1721 | 1708 |
BufferedInputStream bis = null; |
... | ... | |
1730 | 1717 |
} finally { |
1731 | 1718 |
if (bis != null) bis.close(); |
1732 | 1719 |
} |
1733 |
|
|
1720 |
|
|
1734 | 1721 |
} |
1735 |
|
|
1722 |
|
|
1736 | 1723 |
/** |
1737 | 1724 |
* read file/doc and write to ZipOutputStream |
1738 | 1725 |
* |
... | ... | |
1749 | 1736 |
private void addDocToZip(HttpServletRequest request, String docid, |
1750 | 1737 |
ZipOutputStream zout, String user, String[] groups) throws |
1751 | 1738 |
ClassNotFoundException, IOException, SQLException, McdbException, |
1752 |
Exception |
|
1753 |
{ |
|
1739 |
Exception { |
|
1754 | 1740 |
byte[] bytestring = null; |
1755 | 1741 |
ZipEntry zentry = null; |
1756 |
|
|
1742 |
|
|
1757 | 1743 |
try { |
1758 | 1744 |
URL url = new URL(docid); |
1759 |
|
|
1745 |
|
|
1760 | 1746 |
// this http url; read from URLConnection; add to zip |
1761 | 1747 |
zentry = new ZipEntry(docid); |
1762 | 1748 |
zout.putNextEntry(zentry); |
... | ... | |
1773 | 1759 |
if (bis != null) bis.close(); |
1774 | 1760 |
} |
1775 | 1761 |
zout.closeEntry(); |
1776 |
|
|
1762 |
|
|
1777 | 1763 |
} catch (MalformedURLException mue) { |
1778 |
|
|
1764 |
|
|
1779 | 1765 |
// this is metacat doc (data file or metadata doc) |
1780 | 1766 |
try { |
1781 | 1767 |
DocumentImpl doc = new DocumentImpl(docid); |
1782 |
|
|
1768 |
|
|
1783 | 1769 |
//check the permission for read |
1784 | 1770 |
if (!DocumentImpl.hasReadPermission(user, groups, docid)) { |
1785 | 1771 |
Exception e = new Exception("User " + user |
... | ... | |
1788 | 1774 |
+ docid); |
1789 | 1775 |
throw e; |
1790 | 1776 |
} |
1791 |
|
|
1777 |
|
|
1792 | 1778 |
if (doc.getRootNodeID() == 0) { |
1793 | 1779 |
// this is data file; add file to zip |
1794 | 1780 |
String filepath = MetaCatUtil.getOption("datafilepath"); |
... | ... | |
1799 | 1785 |
FileInputStream fin = null; |
1800 | 1786 |
fin = new FileInputStream(filename); |
1801 | 1787 |
try { |
1802 |
|
|
1788 |
|
|
1803 | 1789 |
zentry = new ZipEntry(docid); |
1804 | 1790 |
zout.putNextEntry(zentry); |
1805 | 1791 |
byte[] buf = new byte[4 * 1024]; // 4K buffer |
... | ... | |
1812 | 1798 |
if (fin != null) fin.close(); |
1813 | 1799 |
} |
1814 | 1800 |
zout.closeEntry(); |
1815 |
|
|
1801 |
|
|
1816 | 1802 |
} else { |
1817 | 1803 |
// this is metadata doc; add doc to zip |
1818 | 1804 |
bytestring = doc.toString().getBytes(); |
... | ... | |
1829 | 1815 |
} |
Also available in: Unified diff
Added param to indicate if public has read access or not, in the method "readFromMetacat". It only does this if reading a metadata document, for a skin-format,
and the user is not public. This is useful to the skin's metadata display/editing form, for indicating public accessiblity (or inaccessibility).
Also, my "auto-format" moved a bunch of curly braces to the typical position.