Project

General

Profile

« Previous | Next » 

Revision 3483

Added by barteau about 17 years ago

Created method "private void download(ClientView bean, HttpServletResponse response)", along with several other additions to support downloading.
Includes functionality for FGDC-related package downloads (zip file) and individual data file downloads, with original filenames.

View differences:

src/edu/ucsb/nceas/metacat/clientview/ClientViewHelper.java
19 19
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
20 20
import edu.ucsb.nceas.utilities.XMLUtilities;
21 21
import java.io.BufferedReader;
22
import java.io.ByteArrayOutputStream;
22 23
import java.io.IOException;
23 24
import java.io.InputStream;
24 25
import java.io.InputStreamReader;
......
56 57
    
57 58
    private static final String                         LDAP_TEMPLATE = "uid=%1s,o=%2s,dc=ecoinformatics,dc=org";
58 59
    
60
    public static final String                          DOWNLOAD_ACTION = "Download";
61
    
59 62
    /**
60 63
     * Creates a new instance of ClientViewHelper
61 64
     */
......
94 97
                } else {
95 98
                    action = request.getParameter("action");
96 99
                    clientViewBean.setAction(action);
97
                    if (action != null && action.equals("Delete")) {
98
                        clientViewBean.setDocId(request.getParameter("docid"));
99
                    }
100
                    clientViewBean.setDocId(request.getParameter("docid"));
101
                    clientViewBean.setMetaFileDocId(request.getParameter("metadataDocId"));
102
                    clientViewBean.setQformat(request.getParameter("qformat"));
100 103
                }
101 104
            }
102 105
            //*** END: manual bind params to bean.
......
154 157
                } else if (action.equals("Scope")) {
155 158
                    message = handleDocIdSelect();
156 159
                    clientViewBean.setMessage(ClientView.SELECT_MESSAGE, message);
160
                } else if (action.equals("Download")) {
161
                    System.out.println("ClientViewHelper.clientRequest: action = " + action);
162
                    download(clientViewBean, response);
157 163
                }
158 164
                
159 165
            } catch (Exception ex) {
......
600 606
        if (response != null) {
601 607
            buffy = new BufferedReader(new InputStreamReader(response));
602 608
            doc = XMLUtilities.getXMLReaderAsDOMDocument(buffy);
609
            response.close();
603 610
        }
604 611
        setMetadataDoc(doc);
605 612
    }
......
745 752
        
746 753
    }
747 754
    
755
    public static String getNodeText(XPath xPath, String expression, Node root) {
756
        Node                        node;
757
        String                      result = null;
758
        
759
        node = getNode(xPath, expression, root);
760
        if (node != null && !node.equals(""))
761
            result = node.getTextContent();
762
        return(result);
763
    }
764
    
765
    public static String[] getNodeTextList(XPath xPath, String expression, Node root) {
766
        NodeList                    nodes;
767
        String                      result[] = null;
768
        int                         size;
769
        
770
        try {
771
            nodes = (NodeList) xPath.evaluate(expression, root, XPathConstants.NODESET);
772
            if (nodes != null && (size = nodes.getLength()) > 0) {
773
                result = new String[size];
774
                for(int i = 0; i < size; i++)
775
                    result[i] = nodes.item(i).getTextContent();
776
            }
777
        } catch (XPathExpressionException ex) {
778
            ex.printStackTrace();
779
        }
780
        return(result);
781
    }
782
    
748 783
    public static String getStringFromInputStream(InputStream input) {
749 784
        StringBuffer result = new StringBuffer();
750 785
        BufferedReader in = new BufferedReader(new InputStreamReader(input));
......
762 797
    //*** END: Static utility methods ***
763 798
    
764 799
    public String makeRedirectUrl() {
765
        String                      result = "", docId;
800
        String                      result, docId;
766 801
        
767 802
        docId = clientViewBean.getMetaFileDocId();
768
        if (docId != null && !docId.equals("")) {
769
            result = "metacat?action=" + clientViewBean.getAction()
770
            + "&qformat=" +clientViewBean.getQformat()
803
        if (clientViewBean.getAction().equals(DOWNLOAD_ACTION)) {
804
            result = null;
805
        } else if (docId != null && !docId.equals("")) {
806
            result = "metacat?action=read&qformat=" +clientViewBean.getQformat()
771 807
            + "&docid=" + docId
772
            + "&sessionid=" + clientViewBean.getSessionid();
808
                    + "&sessionid=" + clientViewBean.getSessionid();
773 809
            result += "&msg=" + clientViewBean.getMessage(ClientView.UPDATE_MESSAGE);
774 810
        } else {
775
//            result = "metacat?action=query&qformat=" + clientViewBean.getQformat()
776
//            + "&anyfield=" + clientViewBean.getAnyfield()
777
//            + "&sessionid=" + clientViewBean.getSessionid();
778 811
            result = "style/common/confirm.jspx";
779 812
        }
780 813
        //*** Reset bean action property.
......
782 815
        return(result);
783 816
    }
784 817
    
818
    private void download(ClientView bean, HttpServletResponse response) {
819
        Properties                      args;
820
        InputStream                     inStream;
821
        String                          docId, metaId, fNm = null, pth, txtLst[];
822
        String                          msg = "File '~' (~) downloaded";
823
        Node                            branchRoot, metaRoot;
824
        ByteArrayOutputStream           outStream;
825
        int                             intMe;
826
        
827
        docId = bean.getDocId();
828
        metaId = bean.getMetaFileDocId();
829
        if (docId != null && metaId != null && !docId.equals("") && !metaId.equals("")) {
830
            //*** Properties args: key=param_value, value=param_name.
831
            args = new Properties();
832
            args.put("read", "action");
833
            try {
834
                //*** First, retrieve the metadata and get the original filename.
835
                //*** Also, if this is the metadata, get a list of docId's for the package.
836
                setMetadataDoc(metaId);
837
                metaRoot = getMetadataDoc().getDocumentElement();
838
                if (ClientFgdcHelper.isFGDC(getMetadataDoc())) {
839
                    //*** FGDC
840
                    if (docId.equals(metaId)) { //*** This is the metadata file.
841
                        pth = ClientFgdcHelper.FGDC_DOCID_ROOT_XPATH.replaceFirst("%1s", docId);
842
                        branchRoot = getNode(xpath, pth, getMetadataDoc());
843
                        fNm = getNodeText(xpath, ClientFgdcHelper.FGDC_FILE_NAME_XPATH, branchRoot);
844
                        fNm = toZipFileName(fNm);
845
                        response.setContentType("application/zip");
846
                        //*** Get the list of docId's for the entire package.
847
                        args.put(metaId, "docid");
848
                        txtLst = getNodeTextList(xpath, ClientFgdcHelper.FGDC_DATA_FILE_NODES_XPATH, branchRoot);
849
                        for (int i = 0; i < txtLst.length; i++)
850
                            args.put(txtLst[i], "docid");
851
                        args.put("zip", "qformat");
852
                    } else { //*** This is a data file.
853
                        pth = ClientFgdcHelper.PATH4ANCESTOR.replaceFirst("%1s", docId);
854
                        pth = pth.replaceFirst("%2s", "digform");
855
                        branchRoot = getNode(xpath, pth, getMetadataDoc());
856
                        fNm = getNodeText(xpath, ClientFgdcHelper.FGDC_DATA_FILE_NAME_XPATH, branchRoot);
857
                        response.setContentType("application/octet-stream");
858
                        args.put(docId, "docid");
859
                        args.put("xml", "qformat");
860
                    }
861
                    System.out.println("ClientViewHelper.download: docId = " + docId + "  metaId= " + metaId + "\npth = " + pth + "\nfNm = " + fNm);
862
                } else {
863
                    //*** TODO: EML -  this is just some basic code to start with.
864
                    if (docId.equals(metaId)) {
865
                        fNm = "emlMetadata.xml";
866
                        txtLst = new String[] {docId};
867
                        args.put(txtLst[0], "docid");
868
                        args.put("zip", "qformat");
869
                        response.setContentType("application/zip");
870
                    } else {
871
                        fNm = "emlData.dat";
872
                        args.put("xml", "qformat");
873
                        args.put(docId, "docid");
874
                        response.setContentType("application/octet-stream");
875
                    }
876
                }
877
                
878
                //*** Set the filename in the response.
879
                response.setHeader("Content-Disposition", "attachment; filename=" + fNm);
880
                
881
                //*** Next, read the file from metacat.
882
                inStream = metacatClient.sendParameters(args);
883
                
884
                //*** Then, convert the input stream into an output stream.
885
                outStream = new ByteArrayOutputStream();
886
                while ((intMe = inStream.read()) != -1) {
887
                    outStream.write(intMe);
888
                }
889
                
890
                //*** Now, write the output stream to the response.
891
                response.setContentLength(outStream.size());
892
                outStream.writeTo(response.getOutputStream());
893
                response.flushBuffer();
894
                
895
                //*** Finally, set the message for the user interface to display.
896
                msg = msg.replaceFirst("~", fNm);
897
                msg = msg.replaceFirst("~", docId);
898
                bean.setMessage(ClientView.SELECT_MESSAGE, msg);
899
            } catch (Exception ex) {
900
                ex.printStackTrace();
901
                bean.setMessage(ClientView.SELECT_MESSAGE, ex.getMessage());
902
            }
903
        }
904
    }
905
    
906
    public static String toZipFileName(String fileName) {
907
        String                      result = "metacat";
908
        int                         idx;
909
        
910
        if (fileName != null && !fileName.equals("") && !fileName.equals(".")) {
911
            idx = fileName.indexOf('.');
912
            if (idx > -1)
913
                result = fileName.substring(0, idx);
914
            else
915
                result = fileName;
916
        }
917
        result += ".zip";
918
        return(result);
919
    }
785 920
}

Also available in: Unified diff