Project

General

Profile

« Previous | Next » 

Revision 7843

add Metacat servlet action to force the reindexing of one or more or all pids in the system. https://projects.ecoinformatics.org/ecoinfo/issues/5945

View differences:

test/edu/ucsb/nceas/metacattest/MetaCatServletTest.java
27 27
package edu.ucsb.nceas.metacattest;
28 28

  
29 29
import edu.ucsb.nceas.MCTestCase;
30
import edu.ucsb.nceas.metacat.IdentifierManager;
30 31
import edu.ucsb.nceas.metacat.properties.PropertyService;
31 32
import edu.ucsb.nceas.utilities.HttpMessage;
32 33
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
......
133 134
						serial));
134 135

  
135 136
		suite.addTest(new MetaCatServletTest("testLogOut"));
137
		
138
		suite.addTest(new MetaCatServletTest("testReindexFail"));
136 139

  
137 140
		return suite;
138 141
	}
......
419 422
		assertTrue(handleLogOut());
420 423

  
421 424
	}
425
	
426
	public void testReindexFail() {
422 427

  
428
		// find a pid to reindex
429
		String identifier = null;
430
		List<String> ids = IdentifierManager.getInstance().getAllSystemMetadataGUIDs();
431
		if (ids != null && !ids.isEmpty()) {
432
			identifier = ids.get(0);
433
		}
434
		Properties prop = new Properties();
435
		prop.put("action", "reindex");
436
		prop.put("pid", identifier);
437

  
438
		String message = getMetacatString(prop);
439
		debug("Reindex Message: " + message);
440
		if (message.indexOf("<error>") != -1) {// there was an error
441
			assertTrue(true);
442
		} else if (message.indexOf("<success>") != -1) {
443
			fail("Unauthenticated user should not be able to invoke this action: " + message);
444
		} else {// something weird happened.
445
			fail("There was an unexpected error reindexing pid: " + message);
446
		}
447
	}
448

  
423 449
	/**
424 450
	 * Method to hanld login action
425 451
	 * 
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
989 989
				out.close();
990 990
			} else if (action.equals("buildindex")) {
991 991
				handler.handleBuildIndexAction(params, request, response, userName, groupNames);
992
			} else if (action.equals("reindex")) {
993
				handler.handleReindexAction(params, request, response, userName, groupNames);
992 994
			} else if (action.equals("login") || action.equals("logout")) {
993 995
				/*
994 996
				 * } else if (action.equals("protocoltest")) { String testURL =
src/edu/ucsb/nceas/metacat/MetacatHandler.java
52 52
import java.util.HashMap;
53 53
import java.util.Hashtable;
54 54
import java.util.Iterator;
55
import java.util.List;
55 56
import java.util.Map;
56 57
import java.util.Timer;
57 58
import java.util.Vector;
......
66 67

  
67 68
import org.apache.commons.io.input.XmlStreamReader;
68 69
import org.apache.log4j.Logger;
70
import org.dataone.service.types.v1.Identifier;
69 71
import org.dataone.service.types.v1.SystemMetadata;
70 72
import org.ecoinformatics.eml.EMLParser;
71 73

  
......
2587 2589
    }
2588 2590
    
2589 2591
    /**
2592
     * Rebuild the index for one or more documents. If the "pid" parameter is
2593
     * provided, rebuild for just that one document (or list of documents). If
2594
     * not, then rebuild the index for all documents in the systemMetadata table.
2595
     * 
2596
     * @param params
2597
     *            the parameters from the web request
2598
     * @param request
2599
     *            the http request object for getting request details
2600
     * @param response
2601
     *            the http response object for writing output
2602
     * @param username
2603
     *            the username of the authenticated user
2604
     */
2605
    protected void handleReindexAction(Hashtable<String, String[]> params,
2606
            HttpServletRequest request, HttpServletResponse response,
2607
            String username, String[] groups) {
2608
        Logger logMetacat = Logger.getLogger(MetacatHandler.class);
2609
        
2610
        // Get all of the parameters in the correct formats
2611
        String[] pid = params.get("pid");
2612
        
2613
        // Rebuild the indices for appropriate documents
2614
        try {
2615
            response.setContentType("text/xml");
2616
            PrintWriter out = response.getWriter();
2617
            
2618
            // Check that the user is authenticated as an administrator account
2619
            if (!AuthUtil.isAdministrator(username, groups)) {
2620
                out.print("<error>");
2621
                out.print("The user \"" + username +
2622
                        "\" is not authorized for this action.");
2623
                out.print("</error>");
2624
                return;
2625
            }
2626
            
2627
            // Process the documents
2628
            StringBuffer results = new StringBuffer();
2629
            if (pid == null || pid.length == 0) {
2630
                // Process all of the documents
2631
                logMetacat.info("queueing doc index for all documents");
2632
                try {
2633
                    List<String> allIdentifiers = IdentifierManager.getInstance().getAllSystemMetadataGUIDs();
2634
                    Iterator<String> it = allIdentifiers.iterator();
2635
                    results.append("<success>");
2636
                    while (it.hasNext()) {
2637
                        String id = it.next();
2638
                        Identifier identifier = new Identifier();
2639
                        identifier.setValue(id);
2640
						SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(identifier);
2641
						HazelcastService.getInstance().getIndexQueue().add(sysMeta);
2642
						results.append("<pid>" + id + "</pid>\n");
2643
                        logMetacat.debug("queued SystemMetadata for index on pid: " + id);
2644
                    }
2645
                    results.append("</success>");
2646

  
2647
                    logMetacat.info("done queueing index for all documents");
2648
                } catch (Exception e) {
2649
                	// report the error
2650
                	results = new StringBuffer();
2651
                	results.append("<error>");
2652
                	results.append(e.getMessage());
2653
                	results.append("</error>");
2654
                }
2655
            } else {
2656
                results.append("<success>\n");
2657
                // Only process the requested documents
2658
                for (int i = 0; i < pid.length; i++) {
2659
                    String id = pid[i];
2660
                    logMetacat.info("queueing doc index for pid " + id);
2661
                    Identifier identifier = new Identifier();
2662
                    identifier.setValue(id);
2663
					SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(identifier);
2664
					HazelcastService.getInstance().getIndexQueue().add(sysMeta);
2665
					results.append("<pid>" + id + "</pid>\n");
2666
					logMetacat.info("done queueing doc index for pid " + id);
2667
                }
2668
                results.append("</success>");
2669
            }
2670
            out.print(results.toString());
2671
            out.close();
2672
        } catch (IOException e) {
2673
            logMetacat.error("MetacatHandler.handleBuildIndexAction - " +
2674
            		         "Could not open http response for writing: " + 
2675
            		         e.getMessage());
2676
            e.printStackTrace();
2677
        } catch (MetacatUtilException ue) {
2678
            logMetacat.error("MetacatHandler.handleBuildIndexAction - " +
2679
            		         "Could not determine if user is administrator: " + 
2680
            		         ue.getMessage());
2681
            ue.printStackTrace();
2682
        }
2683
    }
2684
    
2685
    /**
2590 2686
     * Build the index for one document by reading the document and
2591 2687
     * calling its buildIndex() method.
2592 2688
     *

Also available in: Unified diff