Project

General

Profile

« Previous | Next » 

Revision 5843

Added by berkley almost 14 years ago

updating commons-fileupload to 1.2.2

View differences:

src/edu/ucsb/nceas/metacat/restservice/ResourceHandler.java
43 43
import org.apache.log4j.Logger;
44 44
import org.apache.maven.artifact.ant.shaded.IOUtil;
45 45
import org.dataone.client.MNode;
46
import org.dataone.mimemultipart.MultipartResponseHandler;
46
import org.dataone.mimemultipart.MultipartRequest;
47
import org.dataone.mimemultipart.MultipartRequestResolver;
47 48
import org.dataone.service.NodeListParser;
48 49
import org.dataone.service.exceptions.BaseException;
49 50
import org.dataone.service.exceptions.IdentifierNotUnique;
......
591 592
                    status = true;
592 593
                } else if(resource.equals(RESOURCE_REPLICATE)) {
593 594
                    System.out.println("processing replicate request");
594
                    
595
                    if(httpVerb == POST)
596
                    {
597
                        InputStream is = request.getInputStream();
598
                        /*String input = IOUtils.toString(is);
599
                        System.out.println("input: " + input);
600
                        is = IOUtils.toInputStream(input);*/
601
                        
602
                        File tmpDir = getTempDirectory();
603
                        File tmpSMFile = new File(tmpDir + 
604
                                ".sysmeta." + new Date().getTime() + ".tmp");
605
                        MultipartResponseHandler mpHandler = 
606
                            new MultipartResponseHandler(request, tmpSMFile);
607
                        Hashtable parts = mpHandler.getParts();
608
                        File f = (File)parts.get("sysmeta");
609
                        String sourceNode = (String)parts.get("sourceNode");
610
                        FileInputStream fis = new FileInputStream(f);
611
                        
612
                        //lookup the id in the registry
613
                        URL url = new URL("http://cn.dataone.org/cn/node");
614
                        InputStream urlis = url.openStream();
615
                        Map<String,String> m = NodeListParser.parseNodeListFile(urlis);
616
                        String nodeUrl = m.get(sourceNode);
617
                        System.out.println("sourceNodeId: " + sourceNode);
618
                        System.out.println("resolved sourceNodeUrl: " + nodeUrl);
619
                        
620
                        if(nodeUrl == null)
621
                        {
622
                            response.setStatus(500);
623
                            response.getOutputStream().write(("Member Node id " + 
624
                                    sourceNode + " not found in node registry.").getBytes());
625
                            response.getOutputStream().close();
626
                        }
627
                        
628
                        //respond to cn with 200/OK
629
                        //String s;
630
                        //s = "sysmeta: " + IOUtils.toString(fis);
631
                        //s += "\n\n";
632
                        response.setStatus(200);
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();
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);
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
                        
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
                        
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);
667
                        //call cn.setReplicationStatus(guid, COMPLETE)
668
                        
669
                        /*Questions:
670
                         * Call is now supposed to look like replicate(token, SystemMetadata, SourceNode)
671
                         * how is SystemMetadata encoded?  Multipart?
672
                         * 
673
                         * Is the token passed used to access MN_A?  How does MN_A
674
                         * know it's valid?
675
                         * 
676
                         * What exactly is happening when setReplicationStatus is called
677
                         * to the CN?  Which systemMetadata is being updated?  MN_A or MN_B or both?
678
                         * 
679
                         * 
680
                         * 
681
                         */
682
                        
683
                    }
684
                    
595
                    replicate(httpVerb, request, response);
685 596
                    status = true;
686 597
                }
687 598
                    
......
696 607
            }
697 608
        } catch (Exception e) {
698 609
            logMetacat.error(e.getMessage());
610
            System.out.println("Error in ResourceHandler.handle(): " + e.getMessage());
699 611
            e.printStackTrace();
700 612
        }
701 613
    }
702 614
    
703 615
    /**
616
     * handle the /replicate action
617
     * @param httpVerb
618
     * @param request
619
     * @param response
620
     * @throws Exception
621
     */
622
    private void replicate(int httpVerb, HttpServletRequest request, HttpServletResponse response)
623
        throws Exception
624
    {
625
        if(httpVerb == POST)
626
        {
627
            System.out.println("in POST replicate()");
628
            /*InputStream is = request.getInputStream();
629
            String input = IOUtils.toString(is);
630
            System.out.println("input: " + input);
631
            is = IOUtils.toInputStream(input);*/
632
            
633
            File tmpDir = getTempDirectory();
634
            File tmpSMFile = new File(tmpDir + 
635
                    ".sysmeta." + new Date().getTime() + ".tmp");
636
            System.out.println("1");
637
            MultipartRequestResolver mrr = new MultipartRequestResolver(tmpDir.getAbsolutePath());
638
            System.out.println("2");
639
            MultipartRequest mr = mrr.resolveMultipart(request);
640
            System.out.println("3");
641
            Map<String, File> files = mr.getMultipartFiles();
642
            System.out.println("4");
643
            Map<String, List<String>> params = mr.getMultipartParameters();
644
            File f = files.get("sysmeta");
645
            System.out.println("file: " + f.getAbsolutePath());
646
            String sourceNode = params.get("sourceNode").get(0);
647
            System.out.println("sourceNode: " + sourceNode);
648
            FileInputStream fis = new FileInputStream(f);
649
            
650
            //lookup the id in the registry
651
            URL url = new URL("http://cn.dataone.org/cn/node");
652
            InputStream urlis = url.openStream();
653
            Map<String,String> m = NodeListParser.parseNodeListFile(urlis);
654
            String nodeUrl = m.get(sourceNode);
655
            System.out.println("sourceNodeId: " + sourceNode);
656
            System.out.println("resolved sourceNodeUrl: " + nodeUrl);
657
            
658
            if(nodeUrl == null)
659
            {
660
                response.setStatus(500);
661
                response.getOutputStream().write(("Member Node id " + 
662
                        sourceNode + " not found in node registry.").getBytes());
663
                response.getOutputStream().close();
664
            }
665
            
666
            //respond to cn with 200/OK
667
            //String s;
668
            //s = "sysmeta: " + IOUtils.toString(fis);
669
            //s += "\n\n";
670
            response.setStatus(200);
671
            //response.getOutputStream().write(("sourceNode: " + sourceNode + "\n\n").getBytes());
672
            //response.getOutputStream().write(("s: " + s).getBytes());
673
            OutputStream out = response.getOutputStream();
674
            out.write("OK\n".getBytes());
675
            out.write(("sourceNodeId: " + sourceNode + "\n").getBytes());
676
            out.write(("sourceNodeUrl: " + nodeUrl + "\n").getBytes());
677
            out.close();
678
            
679
            //parse the systemMetadata
680
            SystemMetadata sm = (SystemMetadata)deserializeServiceType(SystemMetadata.class, fis);
681
            NodeReference nr = sm.getOriginMemberNode();
682
            nr.setValue(sourceNode);
683
            sm.setOriginMemberNode(nr);
684
            //get the document
685
            AuthToken token = new AuthToken(sessionId);
686
            MNode mnode = new MNode(nodeUrl);
687
            //get the doc from the remote host
688
            InputStream docStream = mnode.get(new AuthToken("public"), sm.getIdentifier());
689
            File outputTmpFile = getTempFile();
690
            FileOutputStream outputTmpFileStream = new FileOutputStream(outputTmpFile);
691
            IOUtils.copy(docStream, outputTmpFileStream);
692
            
693
            //verify checksum
694
            System.out.println("verifying checksum");
695
            String docChecksumStr = CrudService.checksum(
696
                    new FileInputStream(outputTmpFile), 
697
                    sm.getChecksum().getAlgorithm().name());
698
            System.out.println("original checksum: " + sm.getChecksum().getValue());
699
            System.out.println(" created checksum: " + docChecksumStr);
700
            
701
            //insert the document in local db
702
            System.out.println("creating new doc");
703
            CrudService.getInstance().create(token, 
704
                    sm.getIdentifier(), new FileInputStream(outputTmpFile), sm);
705
            //call cn.setReplicationStatus(guid, COMPLETE)
706
            
707
            /*Questions:
708
             * Call is now supposed to look like replicate(token, SystemMetadata, SourceNode)
709
             * how is SystemMetadata encoded?  Multipart?
710
             * 
711
             * Is the token passed used to access MN_A?  How does MN_A
712
             * know it's valid?
713
             * 
714
             * What exactly is happening when setReplicationStatus is called
715
             * to the CN?  Which systemMetadata is being updated?  MN_A or MN_B or both?
716
             * 
717
             * 
718
             * 
719
             */
720
            
721
        }
722
    }
723
    
724
    /**
704 725
     * create the root node registry response.  
705 726
     * @throws JiBXException
706 727
     * @throws IOException
......
1201 1222
                            endTime = null;
1202 1223
                        }
1203 1224
                    }
1204
                    else if(name.equals("objectFormat") && value != null)
1225
                    else if(name.equals("objectFormat") && value != null) 
1205 1226
                    {
1206 1227
                        objectFormat = ObjectFormat.convert(value[0]);
1207 1228
                    }

Also available in: Unified diff