Project

General

Profile

« Previous | Next » 

Revision 5857

Added by berkley almost 14 years ago

added more code for new mmp requests

View differences:

src/edu/ucsb/nceas/metacat/restservice/ResourceHandler.java
1518 1518
    }
1519 1519
    
1520 1520
    /**
1521
     * find the part marked by the boundary and the searchstring and write it to f
1522
     * @param beginSearch
1523
     * @param is
1524
     * @param boundary
1525
     * @param searchString
1526
     * @param f
1527
     * @return
1528
     * @throws IOException
1529
     */
1530
    protected String writeMMPPartToFile(String beginSearch, 
1531
            InputStream is, String boundary, String searchString, File f)
1532
        throws IOException
1533
    {
1534
        Logger logMetacat = Logger.getLogger(ResourceHandler.class);
1535
        logMetacat.info("writing MMP parts");
1536
        //String s = beginSearch;
1537
        String s = null;
1538
        FileOutputStream fos = new FileOutputStream(f);
1539
        int numread = 0;
1540
        byte[] b = new byte[1024];
1541
        String writeString = "";
1542
        
1543
        if(s == null)
1544
        {   //starting with the first part of the stream 
1545
            numread = is.read(b, 0, 1024);
1546
            s = new String(b, 0, numread);
1547
        }
1548
        
1549
        if(beginSearch != null)
1550
        {
1551
            s = beginSearch + s;
1552
        }
1553
        
1554
        boolean useCurrentS = true;
1555
        boolean searchForBoundary = false;
1556
        String seekString = searchString;
1557
        
1558
        while(numread != -1)
1559
        {
1560
            logMetacat.info("////////////////////////iterating");
1561
            logMetacat.info("searchForBoundary: " + searchForBoundary);
1562
            logMetacat.info("useCurrentS: " + useCurrentS);
1563
            logMetacat.info("seekString: " + seekString);
1564
            logMetacat.info("in string: " + s);
1565
            if(searchForBoundary)
1566
            {
1567
                seekString = boundary;
1568
            }
1569
            else
1570
            {
1571
                seekString = searchString;
1572
            }
1573
            
1574
            int[] result = StreamUtil.lookForMatch(seekString, s);
1575
            if(!useCurrentS)
1576
            {
1577
                numread = is.read(b, 0, 1024);
1578
                if(numread != -1)
1579
                {
1580
                    s = new String(b, 0, numread);
1581
                }
1582
                else
1583
                {
1584
                    break;
1585
                }
1586
            }
1587
            
1588
            logMetacat.info("2seekString: " + seekString);
1589
            logMetacat.info("2in string: " + s);
1590
            
1591
            if(result[0] >= 0 && result[1] == seekString.length())
1592
            {
1593
                //searchString is full in s
1594
                logMetacat.info("seekstring is fully in s");
1595
                if(!searchForBoundary)
1596
                {   //we're looking for searchString and we found it
1597
                    //chop off the searchString itself and start writing
1598
                    //until we find boundary
1599
                    s = s.substring(result[0] + result[1], s.length());
1600
                    if(s.length() > 0)
1601
                    {
1602
                        useCurrentS = true;
1603
                    }
1604
                    else
1605
                    {
1606
                        useCurrentS = false;
1607
                    }
1608
                    searchForBoundary = true;
1609
                }
1610
                else
1611
                {   //we're writing, but we found the boundary in this chunk
1612
                    
1613
                    writeString = s.substring(0, result[0]);
1614
                    //System.out.println("writing1: " + writeString);
1615
                    fos.write(writeString.getBytes(MetaCatServlet.DEFAULT_ENCODING));
1616
                    //we're done.  break and return;
1617
                    return s.substring(result[0] + result[1], s.length());
1618
                }
1619
            }
1620
            else if(result[0] > 0 && result[1] != seekString.length())
1621
            {
1622
                logMetacat.info("seekstring is partially in s");
1623
                //seekString is partially in s
1624
                //more specifically, the beginning of seekString is at the end
1625
                //of s
1626
                
1627
                //get the next chunk right now, see if the beginning matches
1628
                numread = is.read(b, 0, 1024);
1629
                String s2 = new String(b, 0, numread);
1630
                s += s2;
1631
                useCurrentS = true;
1632
            }
1633
            else
1634
            {
1635
                logMetacat.info("seekstring is not in s");
1636
                //searchString is not in s 
1637
                if(searchForBoundary)
1638
                {
1639
                    //System.out.println("writing2: " + s);
1640
                    fos.write(s.getBytes(MetaCatServlet.DEFAULT_ENCODING));
1641
                }
1642
                numread = is.read(b, 0, 1024);
1643
                if(numread != -1)
1644
                {
1645
                    s = new String(b, 0, numread);
1646
                }
1647
                else
1648
                {
1649
                    break;
1650
                }
1651
                useCurrentS = true;
1652
            }
1653
        }
1654
        return "";
1655
    }
1656
    
1657
    /**
1658
     * write mime multipart parts to files.  this defaults ot getting 
1659
     * the parts "systemmetadata" and "object"
1660
     * @param is
1661
     * @return
1662
     * @throws IOException
1663
     */
1664
    protected Hashtable<String, File> writeMMPPartsToFiles(InputStream is)
1665
        throws IOException
1666
    {
1667
        String[] searchStrings = {
1668
                "Content-Disposition: attachment; filename=systemmetadata\n\n",
1669
                "Content-Disposition: attachment; filename=object\n\n"};
1670
        String[] names = {
1671
                "systemmetadata",
1672
                "object"
1673
        };
1674
        return writeMMPPartsToFiles(is, searchStrings, names);
1675
    }
1676
    
1677
    /**
1678
     * write the mime multipart parts to files.  search for the parts by the names
1679
     * of the searchStrings in order
1680
     * @param is the inputstream of the MMP
1681
     * @param searchStrings the string to search for for the part in the MMP
1682
     * @param names the name to give the indexed part in the returned hashtable
1683
     * @return
1684
     * @throws IOException
1685
     */
1686
    protected Hashtable<String, File> writeMMPPartsToFiles(InputStream is, 
1687
            String[] searchStrings, String[] names)
1688
        throws IOException
1689
    {
1690
        Logger logMetacat = Logger.getLogger(ResourceHandler.class);
1691
        logMetacat.info("Processing Mime Multipart");
1692
        //String isStr = IOUtils.toString(is);
1693
        //System.out.println("isStr: " + isStr);
1694
        //is = IOUtils.toInputStream(isStr);
1695
        String[] boundaryResults = findBoundaryString(is);
1696
        String boundary = boundaryResults[0];
1697
        String s = boundaryResults[1];
1698
        
1699
        
1700
        File[] fileArr = getMMPTempFiles(searchStrings, names);
1701
        Hashtable<String, File> h = new Hashtable<String, File>();
1702
        for(int i=0; i<searchStrings.length; i++)
1703
        {
1704
            System.out.println("searchStrings: " + searchStrings[i]);
1705
            System.out.println("file: " + fileArr[0].getAbsolutePath());
1706
            logMetacat.info("writing mime part " + searchStrings[i] + " to " + fileArr[0].getAbsolutePath());
1707
            System.out.println("s: " + s);
1708
            System.out.println("is: " + is);
1709
            
1710
            s = writeMMPPartToFile(s.trim(), is, boundary, searchStrings[i], fileArr[i]);
1711
            logMetacat.info("writeMMPPartToFile returned '" + s.trim() + "' after processing part " + searchStrings[i]);
1712
            h.put(names[i], fileArr[i]);
1713
        }
1714
        
1715
        return h;
1716
    }
1717
    
1718
    /**
1719
     * return temp files for the MMP processing
1720
     * @param searchStrings
1721
     * @param names
1722
     * @return
1723
     * @throws IOException
1724
     */
1725
    private static File[] getMMPTempFiles(String[] searchStrings, String[] names)
1726
        throws IOException
1727
    {
1728
        Logger logMetacat = Logger.getLogger(ResourceHandler.class);
1729
        File tmpDir = getTempDirectory();
1730
        long datetimetag = new Date().getTime();
1731
        File[] fileArr = new File[searchStrings.length];
1732
        for(int i=0; i<searchStrings.length; i++)
1733
        {
1734
            File f = new File(tmpDir, names[i] + "." + datetimetag + ".tmp");
1735
            fileArr[i] = f;
1736
        }
1737
        
1738
        return fileArr;
1739
    }
1740
    
1741
    /**
1742 1521
     * return the directory where temp files are stored
1743 1522
     * @return
1744 1523
     */
......
1781 1560
    }
1782 1561
    
1783 1562
    /**
1784
     * return a vector where the first element is a string that represents the system
1785
     * metadata and the 2nd element is an InputStream that is the object
1786
     */
1787
    protected Hashtable<String, File> processMMP(final InputStream is)
1788
      throws IOException
1789
    {
1790
        return writeMMPPartsToFiles(is);
1791
    }
1792
    
1793
    /**
1794 1563
     * Earthgrid API > Put Service >Put Function : calls MetacatHandler > handleInsertOrUpdateAction 
1795 1564
     * 
1796 1565
     * @param guid ID of data object to be inserted or updated
1797 1566
     * @throws IOException
1798 1567
     */
1799 1568
    private void putObject(String guid, String action) {
1569
        System.out.println("ResourceHandler: putObject with guid " + guid);
1800 1570
        logMetacat.debug("Entering putObject: " + guid + "/" + action);
1801 1571
        OutputStream out = null;
1802
        Hashtable<String, File> parts = null;
1803 1572
        try {
1804 1573
            out = response.getOutputStream();
1805 1574
            response.setStatus(200);
......
1820 1589
                //System.out.println("request: " + req);
1821 1590
                //InputStream reqStr = IOUtils.toInputStream(req);
1822 1591
                InputStream reqStr = request.getInputStream();
1823
                parts = processMMP(reqStr);
1824
                object = new FileInputStream(parts.get("object"));
1825
                sysmeta = new FileInputStream(parts.get("systemmetadata"));
1826 1592
                
1593
                //handle MMP inputs
1594
                File tmpDir = getTempDirectory();
1595
                File tmpSMFile = new File(tmpDir + 
1596
                        ".sysmeta." + new Date().getTime() + ".tmp");
1597
                System.out.println("temp dir: " + tmpDir.getAbsolutePath());
1598
                MultipartRequestResolver mrr = new MultipartRequestResolver(
1599
                        tmpDir.getAbsolutePath(), 1000000000, 0);
1600
                MultipartRequest mr = mrr.resolveMultipart(request);
1601
                Map<String, File> files = mr.getMultipartFiles();
1602
                Iterator keys = files.keySet().iterator();
1603
                while(keys.hasNext())
1604
                {
1605
                    String key = (String)keys.next();
1606
                    System.out.println("files key: " + key);
1607
                    System.out.println("files value: " + files.get(key));
1608
                }
1609
                
1610
                Map<String, List<String>> params = mr.getMultipartParameters();
1611
                keys = params.keySet().iterator();
1612
                while(keys.hasNext())
1613
                {
1614
                    String key = (String)keys.next();
1615
                    System.out.println("params key: " + key);
1616
                    System.out.println("params value: " + params.get(key));
1617
                }
1618
                
1619
                File smFile = files.get("sysmeta");
1620
                System.out.println("smFile: " + smFile.getAbsolutePath());
1621
                sysmeta = new FileInputStream(smFile);
1622
                File objFile = files.get("object");
1623
                System.out.println("objectfile: " + objFile.getAbsolutePath());
1624
                object = new FileInputStream(objFile);
1625
                
1827 1626
                /*String obj = IOUtils.toString(object);
1828 1627
                String sm = IOUtils.toString(sysmeta);
1829 1628
                System.out.println("object: " + obj);
......
1832 1631
                sysmeta = IOUtils.toInputStream(sm);*/
1833 1632
                
1834 1633
            }
1634
            catch(org.apache.commons.fileupload.FileUploadException fue)
1635
            {
1636
                throw new ServiceFailure("1202", "Could not upload MMP files: " + fue.getMessage());
1637
            }
1835 1638
            catch(IOException ioe)
1836 1639
            {
1837 1640
                throw new ServiceFailure("1202", 
1838 1641
                        "IOException when processing Mime Multipart: " + ioe.getMessage());
1839 1642
            }
1643
            catch(Exception e)
1644
            {
1645
                throw new ServiceFailure("1202", "Error handling MMP upload: " + e.getMessage());
1646
            }
1840 1647
            
1841 1648
            if ( action.equals(FUNCTION_NAME_INSERT)) { //handle inserts
1842 1649

  
......
1909 1716
            }
1910 1717
            
1911 1718
            //clean up the MMP files
1912
            parts.get("systemmetadata").delete();
1913
            parts.get("object").delete();
1719
            //parts.get("systemmetadata").delete();
1720
            //parts.get("object").delete();
1914 1721
        } catch (NotAuthorized e) {
1915 1722
            response.setStatus(500);
1916 1723
            serializeException(e, out);
......
1953 1760
        }
1954 1761
        finally
1955 1762
        {
1956
            if(parts != null)
1763
            /*if(parts != null)
1957 1764
            {
1958 1765
                Enumeration keys = parts.keys();
1959 1766
                while(keys.hasMoreElements())
......
1962 1769
                    File f = parts.get(key);
1963 1770
                    f.delete();
1964 1771
                }
1965
            }
1772
            }*/
1966 1773
        }
1967 1774
    }
1968 1775

  

Also available in: Unified diff