Project

General

Profile

« Previous | Next » 

Revision 5959

Modified MetacatHandler, added read() - Read a document from metacat and return an InputStream. The XML or data document should be on disk, but if not, read from the metacat database. This method should be optimized, along with others, to not write stream data to disk for performance reasons.

View differences:

src/edu/ucsb/nceas/metacat/MetacatHandler.java
1006 1006
        //System.out.println("response from MetacatHandler.setAccess: " + resp);
1007 1007
    }
1008 1008
    
1009
    public InputStream read(String docid, String username, String[] groups)
1010
        throws InsufficientKarmaException, ParseLSIDException,
1011
        PropertyNotFoundException, McdbException, SQLException, 
1012
        ClassNotFoundException, IOException
1013
    {
1014
        logMetacat.debug("MetacatHandler.read() called.");
1015
        
1016
        InputStream fileInputStream = null;
1017
        File tmpDir = null;
1018
        
1019
        // be sure we have a local ID from an LSID
1020
        if (docid.startsWith("urn:")) {
1021
          try {
1022
            docid = LSIDUtil.getDocId(docid, true);                 
1023
            
1024
          } catch ( ParseLSIDException ple ) {
1025
            logMetacat.debug("There was a problem parsing the LSID. The " +
1026
                             "error message was: " + ple.getMessage());
1027
            throw ple;
1028
          }
1029
        }
1030
                
1031
        // accomodate old clients that send docids without revision numbers
1032
        docid = appendRev(docid);
1033
        
1034
        DocumentImpl doc = new DocumentImpl(docid, false);
1035
        //check the permission for read
1036
        if (!DocumentImpl.hasReadPermission(username, groups, docid)) {
1037
            
1038
            throw new InsufficientKarmaException(
1039
              "User " + username +
1040
              " does not have permission" +
1041
              " to read the document with the docid " + docid);
1042
        }
1043
        
1044
        // deal with data or metadata cases
1045
        if (doc.getRootNodeID() == 0) {
1046
          
1047
          // this is a data file
1048
          
1049
          // get the path to the file to read
1050
          try {
1051
            String filepath = PropertyService.getProperty("application.datafilepath");
1052
          
1053
            // ensure it is a directory path
1054
            if ( !(filepath.endsWith("/")) ) {
1055
                filepath += "/";
1056
                
1057
            }
1058
            String filename = filepath + docid;
1059
            fileInputStream = (InputStream) readFromFilesystem(filename);
1060
          
1061
          } catch ( PropertyNotFoundException pnf ) {
1062
            logMetacat.debug("There was a problem finding the " +
1063
                             "application.datafilepath property. The error " + 
1064
                             "message was: " + pnf.getMessage());
1065
            throw pnf;
1066
            
1067
          } // end try()
1068

  
1069
        } else {
1070
          
1071
          // this is an metadata document
1072
          
1073
          // get a temporary file handle to write to as an OutputStream
1074
          try {
1075
            tmpDir = new File(PropertyService.getProperty("application.tempDir"));
1076

  
1077
          } catch ( PropertyNotFoundException pnfe ) {
1078
            logMetacat.debug("There was a problem getting the" + 
1079
                             "application.tempDir property. Using /tmp " + 
1080
                             "instead. The error was: " + pnfe.getMessage());
1081
            tmpDir = new File("/tmp");
1082

  
1083
          } //end try()
1084
          
1085
          Date d = new Date();
1086
          final File outputFile = new File(tmpDir, "metacat.output." + d.getTime());
1087
          OutputStream out = new FileOutputStream(outputFile);
1088

  
1089
          // Try to get the metadata file from disk. If it isn't
1090
          // found, create it from the db and write it to disk then.
1091
          try {
1092
              doc.toXml(out, username, groups, true);
1093
                             
1094
          } catch (McdbException e) {
1095
              // any exceptions in reading the xml from disc, and we go back to the
1096
              // old way of creating the xml directly.
1097
              logMetacat.error("MetaCatServlet.readFromMetacat() " +
1098
                "- could not read from document file " + 
1099
                docid + ": " + e.getMessage());
1100
              e.printStackTrace(System.out);
1101
              doc.toXmlFromDb(out, username, groups, true);
1102
          }
1103
          
1104
          // set the input stream
1105
          fileInputStream = new FileInputStream(outputFile);
1106
          
1107
          //set a timer to clean up the temp files
1108
          Timer t = new Timer();
1109
          TimerTask tt = new TimerTask() {
1110
              @Override
1111
              public void run()
1112
              {
1113
                  outputFile.delete();
1114
              }
1115
          };
1116
          // destroy the file after 30 minutes (possibly large files)
1117
          t.schedule(tt, 18000000);
1118

  
1119
        }
1120
                
1121
        return fileInputStream;
1122
    }
1123
    
1009 1124
    /**
1010 1125
     * Read a file from Metacat's configured file system data directory.
1011 1126
     *

Also available in: Unified diff