Project

General

Profile

« Previous | Next » 

Revision 5838

Added by berkley almost 14 years ago

replicate works on metacat now. just waiting for roberts changes to the mmp clients

View differences:

src/edu/ucsb/nceas/metacat/restservice/ResourceHandler.java
42 42
import org.apache.commons.io.IOUtils;
43 43
import org.apache.log4j.Logger;
44 44
import org.apache.maven.artifact.ant.shaded.IOUtil;
45
import org.dataone.client.MNode;
45 46
import org.dataone.mimemultipart.MultipartResponseHandler;
46 47
import org.dataone.service.NodeListParser;
47 48
import org.dataone.service.exceptions.BaseException;
......
598 599
                        System.out.println("input: " + input);
599 600
                        is = IOUtils.toInputStream(input);*/
600 601
                        
602
                        File tmpDir = getTempDirectory();
603
                        File tmpSMFile = new File(tmpDir + 
604
                                ".sysmeta." + new Date().getTime() + ".tmp");
601 605
                        MultipartResponseHandler mpHandler = 
602
                            new MultipartResponseHandler(request, new File("/tmp/sysmeta"));
606
                            new MultipartResponseHandler(request, tmpSMFile);
603 607
                        Hashtable parts = mpHandler.getParts();
604 608
                        File f = (File)parts.get("sysmeta");
605 609
                        String sourceNode = (String)parts.get("sourceNode");
606 610
                        FileInputStream fis = new FileInputStream(f);
607
                        String s;
608
                        s = "sysmeta: " + IOUtils.toString(fis);
609
                        s += "\n\n";
610 611
                        
611
                        
612
                        
613
                        //get the system metadata from the request
614
                       
615
                        /*String[] searchStrings = {
616
                                "Content-Disposition: attachment; name=sysmeta\n\n",
617
                                "Content-Disposition: form-data; name=sourceNode\n\n"};
618
                        String[] names = {
619
                                "systemmetadata",
620
                                "sourceNode"
621
                        };
622
                        InputStream is = request.getInputStream();
623
                        //Hashtable<String,File> mmpFiles = writeMMPPartsToFiles(is, searchStrings, names);
624
                        String s = IOUtils.toString(is);
625
                        System.out.println("s: " + s);*/
626
                        /*int startIndex = s.indexOf(searchStrings[0]);
627
                        startIndex = s.indexOf("\n\n", startIndex);
628
                        int endIndex = s.indexOf("\n--", startIndex);
629
                        System.out.println("startIndex: " + startIndex);
630
                        System.out.println("endIndex: " + endIndex);
631
                        String smStr = s.substring(startIndex, endIndex);
632
                        System.out.println("smStr: " + smStr);
633
                        SystemMetadata sm = (SystemMetadata)
634
                            deserializeServiceType(SystemMetadata.class, new ByteArrayInputStream(smStr.getBytes()));
635
                        //String nodeId = sm.getAuthoritativeMemberNode().getValue();
636
                        startIndex = s.indexOf(searchStrings[1], endIndex);
637
                        endIndex = s.indexOf("\n--", startIndex);
638
                        String sourceNode = s.substring(startIndex, endIndex);
639
                        System.out.println("sourceNode: " + sourceNode);
640
                        //lookup nodeId from the registry
612
                        //lookup the id in the registry
641 613
                        URL url = new URL("http://cn.dataone.org/cn/node");
642 614
                        InputStream urlis = url.openStream();
643 615
                        Map<String,String> m = NodeListParser.parseNodeListFile(urlis);
644 616
                        String nodeUrl = m.get(sourceNode);
645
                        System.out.println("nodeUrl: " + nodeUrl);
617
                        System.out.println("sourceNodeId: " + sourceNode);
618
                        System.out.println("resolved sourceNodeUrl: " + nodeUrl);
619
                        
646 620
                        if(nodeUrl == null)
647 621
                        {
648 622
                            response.setStatus(500);
......
651 625
                            response.getOutputStream().close();
652 626
                        }
653 627
                        
654
                        //we at least have enough information to continue so
655
                        //tell the cn we're ok
656
                        //respond to cn with 200/OK*/
628
                        //respond to cn with 200/OK
629
                        //String s;
630
                        //s = "sysmeta: " + IOUtils.toString(fis);
631
                        //s += "\n\n";
657 632
                        response.setStatus(200);
658
                        response.getOutputStream().write(("sourceNode: " + sourceNode + "\n\n").getBytes());
659
                        response.getOutputStream().write(("s: " + s).getBytes());
660
                        response.getOutputStream().close();
633
                        //response.getOutputStream().write(("sourceNode: " + sourceNode + "\n\n").getBytes());
634
                        //response.getOutputStream().write(("s: " + s).getBytes());
635
                        OutputStream out = response.getOutputStream();
636
                        out.write("OK\n".getBytes());
637
                        out.write(("sourceNodeId: " + sourceNode + "\n").getBytes());
638
                        out.write(("sourceNodeUrl: " + nodeUrl + "\n").getBytes());
639
                        out.close();
661 640
                        
641
                        //parse the systemMetadata
642
                        SystemMetadata sm = (SystemMetadata)deserializeServiceType(SystemMetadata.class, fis);
643
                        NodeReference nr = sm.getOriginMemberNode();
644
                        nr.setValue(sourceNode);
645
                        sm.setOriginMemberNode(nr);
662 646
                        //get the document
647
                        AuthToken token = new AuthToken(sessionId);
648
                        MNode mnode = new MNode(nodeUrl);
649
                        //get the doc from the remote host
650
                        InputStream docStream = mnode.get(new AuthToken("public"), sm.getIdentifier());
651
                        File outputTmpFile = getTempFile();
652
                        FileOutputStream outputTmpFileStream = new FileOutputStream(outputTmpFile);
653
                        IOUtils.copy(docStream, outputTmpFileStream);
654
                        
663 655
                        //verify checksum
656
                        System.out.println("verifying checksum");
657
                        String docChecksumStr = CrudService.checksum(
658
                                new FileInputStream(outputTmpFile), 
659
                                sm.getChecksum().getAlgorithm().name());
660
                        System.out.println("original checksum: " + sm.getChecksum().getValue());
661
                        System.out.println(" created checksum: " + docChecksumStr);
662
                        
664 663
                        //insert the document in local db
664
                        System.out.println("creating new doc");
665
                        CrudService.getInstance().create(token, 
666
                                sm.getIdentifier(), new FileInputStream(outputTmpFile), sm);
665 667
                        //call cn.setReplicationStatus(guid, COMPLETE)
666 668
                        
667 669
                        /*Questions:
......
1685 1687
        return h;
1686 1688
    }
1687 1689
    
1690
    /**
1691
     * return temp files for the MMP processing
1692
     * @param searchStrings
1693
     * @param names
1694
     * @return
1695
     * @throws IOException
1696
     */
1688 1697
    private static File[] getMMPTempFiles(String[] searchStrings, String[] names)
1689 1698
        throws IOException
1690 1699
    {
1691 1700
        Logger logMetacat = Logger.getLogger(ResourceHandler.class);
1692
        File tmpDir;
1701
        File tmpDir = getTempDirectory();
1702
        long datetimetag = new Date().getTime();
1703
        File[] fileArr = new File[searchStrings.length];
1704
        for(int i=0; i<searchStrings.length; i++)
1705
        {
1706
            File f = new File(tmpDir, names[i] + "." + datetimetag + ".tmp");
1707
            fileArr[i] = f;
1708
        }
1709
        
1710
        return fileArr;
1711
    }
1712
    
1713
    /**
1714
     * return the directory where temp files are stored
1715
     * @return
1716
     */
1717
    private static File getTempDirectory()
1718
    {
1719
        File tmpDir = null;
1720
        Logger logMetacat = Logger.getLogger(ResourceHandler.class);
1693 1721
        try
1694 1722
        {
1695 1723
            tmpDir = new File(PropertyService.getProperty("application.tempDir"));
......
1700 1728
                    "application.tmpDir not found.  Using /tmp instead.");
1701 1729
            tmpDir = new File("/tmp");
1702 1730
        }
1703
        long datetimetag = new Date().getTime();
1704
        File[] fileArr = new File[searchStrings.length];
1705
        for(int i=0; i<searchStrings.length; i++)
1706
        {
1707
            File f = new File(tmpDir, names[i] + "." + datetimetag + ".tmp");
1708
            fileArr[i] = f;
1709
        }
1710
        
1711
        return fileArr;
1731
        return tmpDir;
1712 1732
    }
1713 1733
    
1714 1734
    /**
1735
     * return a tmp file with a given name
1736
     * @param name
1737
     * @return
1738
     */
1739
    private static File getTempFile(String name)
1740
    {
1741
        File tmpDir = getTempDirectory();
1742
        File f = new File(tmpDir, name);
1743
        return f;
1744
    }
1745
    
1746
    /**
1747
     * return a temp file with a default name
1748
     * @return
1749
     */
1750
    private static File getTempFile()
1751
    {
1752
        return getTempFile(new Date().getTime() + ".tmp");
1753
    }
1754
    
1755
    /**
1715 1756
     * return a vector where the first element is a string that represents the system
1716 1757
     * metadata and the 2nd element is an InputStream that is the object
1717 1758
     */
src/edu/ucsb/nceas/metacat/dataone/CrudService.java
1847 1847
     * @param is
1848 1848
     * @return
1849 1849
     */
1850
    private String checksum(InputStream is)
1850
    public static String checksum(InputStream is)
1851 1851
        throws Exception
1852 1852
    {
1853 1853
        return checksum(is, "MD5");
......
1856 1856
    /**
1857 1857
     * produce a checksum for item using the given algorithm
1858 1858
     */
1859
    private String checksum(InputStream is, String algorithm)
1859
    public static String checksum(InputStream is, String algorithm)
1860 1860
      throws Exception
1861 1861
    {        
1862 1862
        byte[] buffer = new byte[1024];

Also available in: Unified diff