Project

General

Profile

« Previous | Next » 

Revision 5851

Added by berkley over 13 years ago

refactor checksum and some other stuff

View differences:

src/edu/ucsb/nceas/metacat/restservice/ResourceHandler.java
58 58
import org.dataone.service.exceptions.UnsupportedType;
59 59
import org.dataone.service.exceptions.NotFound;
60 60
import org.dataone.service.types.*;
61
import org.dataone.service.types.util.ServiceTypeUtil;
61 62
import org.jibx.runtime.BindingDirectory;
62 63
import org.jibx.runtime.IBindingFactory;
63 64
import org.jibx.runtime.IMarshallingContext;
......
634 635
            File tmpSMFile = new File(tmpDir + 
635 636
                    ".sysmeta." + new Date().getTime() + ".tmp");
636 637
            System.out.println("temp dir: " + tmpDir.getAbsolutePath());
637
            MultipartRequestResolver mrr = new MultipartRequestResolver(tmpDir.getAbsolutePath());
638
            MultipartRequestResolver mrr = new MultipartRequestResolver(
639
                    tmpDir.getAbsolutePath(), 1000000000, 0);
638 640
            MultipartRequest mr = mrr.resolveMultipart(request);
639 641
            Map<String, File> files = mr.getMultipartFiles();
640 642
            Iterator keys = files.keySet().iterator();
......
710 712
            System.out.println("verifying checksum");
711 713
            String docChecksumStr = CrudService.checksum(
712 714
                    new FileInputStream(outputTmpFile), 
713
                    sm.getChecksum().getAlgorithm().name());
715
                    sm.getChecksum().getAlgorithm().toString());
714 716
            System.out.println("original checksum: " + sm.getChecksum().getValue());
715 717
            System.out.println(" created checksum: " + docChecksumStr);
716 718
            
719
            
717 720
            //insert the document in local db
718 721
            System.out.println("creating new doc");
719 722
            CrudService.getInstance().create(token, 
720 723
                    sm.getIdentifier(), new FileInputStream(outputTmpFile), sm);
721 724
            //call cn.setReplicationStatus(guid, COMPLETE)
722 725
            
723
            /*Questions:
724
             * Call is now supposed to look like replicate(token, SystemMetadata, SourceNode)
725
             * how is SystemMetadata encoded?  Multipart?
726
             * 
727
             * Is the token passed used to access MN_A?  How does MN_A
728
             * know it's valid?
729
             * 
730
             * What exactly is happening when setReplicationStatus is called
731
             * to the CN?  Which systemMetadata is being updated?  MN_A or MN_B or both?
732
             * 
733
             * 
734
             * 
735
             */
736
            
737 726
        }
738 727
    }
739 728
    
......
1374 1363
    private void serializeServiceType(Class type, Object object, OutputStream out)
1375 1364
      throws JiBXException
1376 1365
    {
1377
        IBindingFactory bfact = BindingDirectory.getFactory(type);
1378
        IMarshallingContext mctx = bfact.createMarshallingContext();
1379
        mctx.marshalDocument(object, "UTF-8", null, out);
1366
        ServiceTypeUtil.serializeServiceType(type, object, out);
1380 1367
    }
1381 1368
    
1382 1369
    /**
......
1388 1375
    private Object deserializeServiceType(Class type, InputStream is)
1389 1376
      throws JiBXException
1390 1377
    {
1391
        IBindingFactory bfact = BindingDirectory.getFactory(type);
1392
        IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
1393
        Object o = (Object) uctx.unmarshalDocument(is, null);
1394
        return o;
1378
        return ServiceTypeUtil.deserializeServiceType(type, is);
1395 1379
    }
1396 1380
    
1397 1381
    /**
src/edu/ucsb/nceas/metacat/dataone/CrudService.java
63 63
import org.jibx.runtime.JiBXException;
64 64

  
65 65
import org.dataone.service.types.Identifier;
66
import org.dataone.service.types.util.ServiceTypeUtil;
66 67

  
67 68
import com.gc.iotools.stream.is.InputStreamFromOutputStream;
68 69

  
......
1859 1860
    public static String checksum(InputStream is, String algorithm)
1860 1861
      throws Exception
1861 1862
    {        
1862
        byte[] buffer = new byte[1024];
1863
        MessageDigest complete = MessageDigest.getInstance(algorithm);
1864
        int numRead;
1865
        
1866
        do 
1867
        {
1868
          numRead = is.read(buffer);
1869
          if (numRead > 0) 
1870
          {
1871
            complete.update(buffer, 0, numRead);
1872
          }
1873
        } while (numRead != -1);
1874
        
1875
        
1876
        return getHex(complete.digest());
1863
        return ServiceTypeUtil.checksum(is, ChecksumAlgorithm.convert(algorithm)).getValue();
1877 1864
    }
1878 1865
    
1879 1866
    /**
1880
     * convert a byte array to a hex string
1881
     */
1882
    private static String getHex( byte [] raw ) 
1883
    {
1884
        final String HEXES = "0123456789ABCDEF";
1885
        if ( raw == null ) {
1886
          return null;
1887
        }
1888
        final StringBuilder hex = new StringBuilder( 2 * raw.length );
1889
        for ( final byte b : raw ) {
1890
          hex.append(HEXES.charAt((b & 0xF0) >> 4))
1891
             .append(HEXES.charAt((b & 0x0F)));
1892
        }
1893
        return hex.toString();
1894
    }
1895
    
1896
    /**
1897 1867
     * parse the metacat date which looks like 2010-06-08 (YYYY-MM-DD) into
1898 1868
     * a proper date object
1899 1869
     * @param date

Also available in: Unified diff