Project

General

Profile

« Previous | Next » 

Revision 5808

Added by berkley almost 14 years ago

refactored MMP handling

View differences:

src/edu/ucsb/nceas/metacat/restservice/ResourceHandler.java
1397 1397
    }
1398 1398
    
1399 1399
    /**
1400
     * process a mime multipart message
1400
     * locate the boundary marker for an MMP
1401 1401
     * @param is
1402 1402
     * @return
1403
     * @throws IOException
1403 1404
     */
1404
    /*private Hashtable processMMP(InputStream is)
1405
      throws IOException
1406
    {
1407
        //TODO: verify that this is how the RFC for MMP should work
1408
        //SORTAHACK: Since mmp seems to have a bug where large object parts get truncated,
1409
        //parse the stream here.  This has the disavantage of putting the
1410
        //stream into memory.
1411
        InputStream object = null;
1412
        InputStream sysmeta = null;
1413
        String s = IOUtils.toString(is);
1414
        System.out.println("mime: " + s);
1415
        //figure out what the boundary marker is
1416
        String searchString = "boundary=";
1417
        int searchStringIndex = s.indexOf(searchString);
1418
        String boundary = s.substring(searchStringIndex + searchString.length() + 1, 
1419
                s.indexOf("\"", searchStringIndex + searchString.length() + 1));
1420
        boundary = "--" + boundary;
1421
        //System.out.println("boundary is " + boundary);
1422
        
1423
        //find the system metadata
1424
        searchString = "Content-Disposition: attachment; filename=systemmetadata";
1425
        searchStringIndex = s.indexOf(searchString);
1426
        String sm = s.substring(searchStringIndex +
1427
                searchString.length() + 2, 
1428
                s.indexOf(boundary, searchStringIndex));
1429
        sysmeta = new ByteArrayInputStream(sm.getBytes());
1430
        
1431
        //find the object
1432
        searchString = "Content-Disposition: attachment; filename=object";
1433
        searchStringIndex = s.indexOf(searchString);
1434
        String o = s.substring(searchStringIndex +
1435
                searchString.length() + 2, 
1436
                s.indexOf(boundary, searchStringIndex));
1437
        object = new ByteArrayInputStream(o.getBytes());
1438
        
1439
        Hashtable h = new Hashtable();
1440
        h.put("object", object);
1441
        h.put("systemmetadata", sysmeta);
1442
                
1443
        //System.out.println("o: \"" + o + "\"");
1444
        //System.out.println("sm: \"" + sm + "\"");
1445
        return h;
1446
    }
1447
    */
1448
    
1449 1405
    protected static String[] findBoundaryString(InputStream is)
1450 1406
        throws IOException
1451 1407
    {
......
1491 1447
        return endResult;
1492 1448
    }
1493 1449
    
1450
    /**
1451
     * find the part marked by the boundary and the searchstring and write it to f
1452
     * @param beginSearch
1453
     * @param is
1454
     * @param boundary
1455
     * @param searchString
1456
     * @param f
1457
     * @return
1458
     * @throws IOException
1459
     */
1494 1460
    protected String writeMMPPartToFile(String beginSearch, 
1495 1461
            InputStream is, String boundary, String searchString, File f)
1496 1462
        throws IOException
......
1618 1584
        return "";
1619 1585
    }
1620 1586
    
1587
    /**
1588
     * write mime multipart parts to files.  this defaults ot getting 
1589
     * the parts "systemmetadata" and "object"
1590
     * @param is
1591
     * @return
1592
     * @throws IOException
1593
     */
1621 1594
    protected Hashtable<String, File> writeMMPPartsToFiles(InputStream is)
1622 1595
        throws IOException
1623 1596
    {
1597
        String[] searchStrings = {
1598
                "Content-Disposition: attachment; filename=systemmetadata\n\n",
1599
                "Content-Disposition: attachment; filename=object\n\n"};
1600
        String[] names = {
1601
                "systemmetadata",
1602
                "object"
1603
        };
1604
        return writeMMPPartsToFiles(is, searchStrings, names);
1605
    }
1606
    
1607
    /**
1608
     * write the mime multipart parts to files.  search for the parts by the names
1609
     * of the searchStrings in order
1610
     * @param is the inputstream of the MMP
1611
     * @param searchStrings the string to search for for the part in the MMP
1612
     * @param names the name to give the indexed part in the returned hashtable
1613
     * @return
1614
     * @throws IOException
1615
     */
1616
    protected Hashtable<String, File> writeMMPPartsToFiles(InputStream is, 
1617
            String[] searchStrings, String[] names)
1618
        throws IOException
1619
    {
1624 1620
        Logger logMetacat = Logger.getLogger(ResourceHandler.class);
1625 1621
        logMetacat.info("Processing Mime Multipart");
1626 1622
        String[] boundaryResults = findBoundaryString(is);
1627 1623
        String boundary = boundaryResults[0];
1628 1624
        String s = boundaryResults[1];
1629
        String[] searchStrings = {
1630
                "Content-Disposition: attachment; filename=systemmetadata\n\n",
1631
                "Content-Disposition: attachment; filename=object\n\n"};
1632 1625
        
1633
        File[] fileArr = getMMPTempFiles();
1626
        
1627
        File[] fileArr = getMMPTempFiles(searchStrings);
1634 1628
        Hashtable<String, File> h = new Hashtable<String, File>();
1635
        //System.out.println("==========================Looking for SM");
1636
        //System.out.println("writing sm to " + fileArr[0].getAbsolutePath());
1637
        logMetacat.info("writing mime system metadata to " + fileArr[0].getAbsolutePath());
1638
        s = writeMMPPartToFile(s.trim(), is, boundary, searchStrings[0], fileArr[0]);
1639
        logMetacat.info("writeMMPPartToFile returned '" + s.trim() + "' after processing the system metadata");
1640
        //System.out.println("==========================Looking for Object");
1641
        //System.out.println("writing obj to " + fileArr[1].getAbsolutePath());
1642
        logMetacat.info("writing mime object to " + fileArr[1].getAbsolutePath());
1643
        writeMMPPartToFile(s.trim(), is, boundary, searchStrings[1], fileArr[1]);
1644
        h.put("sysmeta", fileArr[0]);
1645
        h.put("object", fileArr[1]);
1629
        for(int i=0; i<searchStrings.length; i++)
1630
        {
1631
            System.out.println("searchStrings: " + searchStrings[i]);
1632
            System.out.println("file: " + fileArr[0].getAbsolutePath());
1633
            logMetacat.info("writing mime part " + searchStrings[i] + " to " + fileArr[0].getAbsolutePath());
1634
            s = writeMMPPartToFile(s.trim(), is, boundary, searchStrings[i], fileArr[i]);
1635
            logMetacat.info("writeMMPPartToFile returned '" + s.trim() + "' after processing part " + searchStrings[i]);
1636
            h.put(names[i], fileArr[i]);
1637
        }
1638
        
1646 1639
        return h;
1647 1640
    }
1648 1641
    
1649
    private static File[] getMMPTempFiles()
1642
    private static File[] getMMPTempFiles(String[] searchStrings)
1650 1643
        throws IOException
1651 1644
    {
1652 1645
        Logger logMetacat = Logger.getLogger(ResourceHandler.class);
......
1662 1655
            tmpDir = new File("/tmp");
1663 1656
        }
1664 1657
        long datetimetag = new Date().getTime();
1665
        File smFile = new File(tmpDir, "sm." + datetimetag + ".tmp");
1666
        File objFile = new File(tmpDir, "obj." + datetimetag + ".tmp");
1667
        File[] fileArr = {smFile, objFile};
1658
        File[] fileArr = new File[searchStrings.length];
1659
        for(int i=0; i<searchStrings.length; i++)
1660
        {
1661
            File f = new File(tmpDir, searchStrings[i] + "." + datetimetag + ".tmp");
1662
            fileArr[i] = f;
1663
        }
1664
        
1668 1665
        return fileArr;
1669 1666
    }
1670 1667
    
......
1710 1707
                InputStream reqStr = request.getInputStream();
1711 1708
                parts = processMMP(reqStr);
1712 1709
                object = new FileInputStream(parts.get("object"));
1713
                sysmeta = new FileInputStream(parts.get("sysmeta"));
1710
                sysmeta = new FileInputStream(parts.get("systemmetadata"));
1714 1711
                
1715 1712
                /*String obj = IOUtils.toString(object);
1716 1713
                String sm = IOUtils.toString(sysmeta);
......
1797 1794
            }
1798 1795
            
1799 1796
            //clean up the MMP files
1800
            parts.get("sysmeta").delete();
1797
            parts.get("systemmetadata").delete();
1801 1798
            parts.get("object").delete();
1802 1799
        } catch (NotAuthorized e) {
1803 1800
            response.setStatus(500);
......
1843 1840
        {
1844 1841
            if(parts != null)
1845 1842
            {
1846
                parts.get("sysmeta").delete();
1847
                parts.get("object").delete();
1843
                Enumeration keys = parts.keys();
1844
                while(keys.hasMoreElements())
1845
                {
1846
                    String key = (String)keys.nextElement();
1847
                    File f = parts.get(key);
1848
                    f.delete();
1849
                }
1848 1850
            }
1849 1851
        }
1850 1852
    }

Also available in: Unified diff