Project

General

Profile

Revision 3772

Enable file naming when returning a zip file of the requested documents.
File names for each docid are provided as additional parameters named by the actual docid value that they are providing a name for. An example query string might look like:

action=read&docid=doc.1.1&docid=doc.2.1&qformat=zip&doc.1.1=fileName1.txt&doc.2.1=fileName2.jpg

and would create a zip file containing files named:
fileName1.txt
fileName2.jpg

This is enhancement was motivated by the SANParks/SAEON skin use of FGDC groups of files and will be useful in the NCEAS and KNB skins when they support FGDC metadata display and download.
See bug #3146 for additional information.

View differences:

src/edu/ucsb/nceas/metacat/MetaCatServlet.java
1406 1406
            }
1407 1407
            // go through the list of docs to read
1408 1408
            for (int i = 0; i < docs.length; i++) {
1409
            	String providedFileName = null;
1410
            	if (params.containsKey(docs[i])) {
1411
                    providedFileName = ((String[]) params.get(docs[i]))[0];
1412
                }
1409 1413
                try {
1410 1414
                    
1411 1415
                    URL murl = new URL(docs[i]);
......
1417 1421
                        // get only docid, eliminate the rest
1418 1422
                        docid = (String) murlQueryStr.get("docid");
1419 1423
                        if (zip) {
1420
                            addDocToZip(request, docid, zout, user, groups);
1424
                            addDocToZip(request, docid, providedFileName, zout, user, groups);
1421 1425
                        } else {
1422 1426
                            readFromMetacat(request, response, docid, qformat,
1423 1427
                                    abstrpath, user, groups, zip, zout,
......
1428 1432
                    } else {
1429 1433
                        docid = docs[i];
1430 1434
                        if (zip) {
1431
                            addDocToZip(request, docid, zout, user, groups);
1435
                            addDocToZip(request, docid, providedFileName, zout, user, groups);
1432 1436
                        } else {
1433 1437
                            readFromURLConnection(response, docid);
1434 1438
                        }
......
1437 1441
                } catch (MalformedURLException mue) {
1438 1442
                    docid = docs[i];
1439 1443
                    if (zip) {
1440
                        addDocToZip(request, docid, zout, user, groups);
1444
                        addDocToZip(request, docid, providedFileName, zout, user, groups);
1441 1445
                    } else {
1442 1446
                        readFromMetacat(request, response, docid, qformat,
1443 1447
                                abstrpath, user, groups, zip, zout,
......
1737 1741
     * @throws McdbException
1738 1742
     * @throws Exception
1739 1743
     */
1740
    private void addDocToZip(HttpServletRequest request, String docid,
1744
    private void addDocToZip(HttpServletRequest request, String docid, String providedFileName,
1741 1745
            ZipOutputStream zout, String user, String[] groups) throws
1742 1746
            ClassNotFoundException, IOException, SQLException, McdbException,
1743 1747
            Exception {
......
1748 1752
            URL url = new URL(docid);
1749 1753
            
1750 1754
            // this http url; read from URLConnection; add to zip
1751
            zentry = new ZipEntry(docid);
1755
            //use provided file name if we have one
1756
            if (providedFileName != null && providedFileName.length() > 1) {
1757
            	zentry = new ZipEntry(providedFileName);
1758
            }
1759
            else {
1760
            	zentry = new ZipEntry(docid);
1761
            }
1752 1762
            zout.putNextEntry(zentry);
1753 1763
            BufferedInputStream bis = null;
1754 1764
            try {
......
1789 1799
                    FileInputStream fin = null;
1790 1800
                    fin = new FileInputStream(filename);
1791 1801
                    try {
1792
                        
1793
                        zentry = new ZipEntry(docid);
1802
                    	//use provided file name if we have one
1803
                        if (providedFileName != null && providedFileName.length() > 1) {
1804
                        	zentry = new ZipEntry(providedFileName);
1805
                        }
1806
                        else {
1807
                        	zentry = new ZipEntry(docid);
1808
                        }
1794 1809
                        zout.putNextEntry(zentry);
1795 1810
                        byte[] buf = new byte[4 * 1024]; // 4K buffer
1796 1811
                        int b = fin.read(buf);
......
1806 1821
                } else {
1807 1822
                    // this is metadata doc; add doc to zip
1808 1823
                    bytestring = doc.toString().getBytes();
1809
                    zentry = new ZipEntry(docid + ".xml");
1824
                    //use provided file name if given
1825
                    if (providedFileName != null && providedFileName.length() > 1) {
1826
                    	zentry = new ZipEntry(providedFileName);
1827
                    }
1828
                    else {
1829
                    	zentry = new ZipEntry(docid + ".xml");
1830
                    }
1810 1831
                    zentry.setSize(bytestring.length);
1811 1832
                    zout.putNextEntry(zentry);
1812 1833
                    zout.write(bytestring, 0, bytestring.length);
src/edu/ucsb/nceas/metacat/clientview/ClientViewHelper.java
1000 1000
                        pth = ClientFgdcHelper.FGDC_DOCID_ROOT_XPATH.replaceFirst("%1s", docId);
1001 1001
                        branchRoot = getNode(xpath, pth, getMetadataDoc());
1002 1002
                        fNm = getNodeText(xpath, ClientFgdcHelper.FGDC_FILE_NAME_XPATH, branchRoot);
1003
                        //include the filename for the docid
1004
                        args.put(fNm, docId);
1003 1005
                        fNm = toZipFileName(fNm);
1004 1006
                        responseMap.put("contentType", "application/zip");
1005 1007
                        //*** Get the list of docId's for the entire package.
1006 1008
                        args.put(metaId, "docid");
1007 1009
                        txtLst = getNodeTextList(xpath, ClientFgdcHelper.FGDC_DATA_FILE_NODES_XPATH, branchRoot);
1008
                        for (int i = 0; i < txtLst.length; i++)
1009
                            args.put(txtLst[i], "docid");
1010
                        for (int i = 0; i < txtLst.length; i++) {
1011
                        	String additionalDocId = txtLst[i];
1012
                        	if (additionalDocId != null && additionalDocId.length() > 1) {
1013
                        		//look up the filename from the metadata
1014
                        		String tempPath = ClientFgdcHelper.PATH4ANCESTOR.replaceFirst("%1s", additionalDocId);
1015
                        		tempPath = tempPath.replaceFirst("%2s", "digform");
1016
                                Node tempBranchRoot = getNode(xpath, tempPath, getMetadataDoc());
1017
                                String tempFileName = getNodeText(xpath, ClientFgdcHelper.FGDC_DATA_FILE_NAME_XPATH, tempBranchRoot);
1018
                                //include the docid
1019
                        		args.put(additionalDocId, "docid");
1020
                        		//include the filename for the docid
1021
                        		args.put(tempFileName, additionalDocId);
1022
                        	}
1023
                        }
1010 1024
                        args.put("zip", "qformat");
1011 1025
                    } else { //*** This is a data file.
1012 1026
                        pth = ClientFgdcHelper.PATH4ANCESTOR.replaceFirst("%1s", docId);

Also available in: Unified diff