Project

General

Profile

« Previous | Next » 

Revision 4334

Added by daigle over 15 years ago

Add functionality to write and read metadata documents to and from the file system.

View differences:

MetaCatServlet.java
27 27
package edu.ucsb.nceas.metacat;
28 28

  
29 29
import java.io.BufferedInputStream;
30
import java.io.BufferedReader;
30 31
import java.io.File;
31 32
import java.io.FileInputStream;
32 33
import java.io.FileReader;
34
import java.io.FileWriter;
33 35
import java.io.IOException;
34 36
import java.io.OutputStreamWriter;
35 37
import java.io.PrintWriter;
......
72 74
import com.oreilly.servlet.multipart.Part;
73 75

  
74 76
import edu.ucsb.nceas.metacat.dataquery.DataQuery;
77
import edu.ucsb.nceas.metacat.service.DatabaseService;
75 78
import edu.ucsb.nceas.metacat.service.PropertyService;
76 79
import edu.ucsb.nceas.metacat.service.ServiceException;
77 80
import edu.ucsb.nceas.metacat.service.SessionService;
......
214 217
		Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
215 218

  
216 219
		try {
220
            DatabaseService databaseService = DatabaseService.getInstance();
221
            logMetacat.debug("DatabaseService singleton created " + databaseService);
222
			
217 223
			// initialize DBConnection pool
218 224
			DBConnectionPool connPool = DBConnectionPool.getInstance();
219 225
			logMetacat.debug("DBConnection pool initialized: " + connPool.toString());
......
1664 1670
            ZipOutputStream zout, boolean withInlineData, Hashtable params)
1665 1671
            throws ClassNotFoundException, IOException, SQLException,
1666 1672
            McdbException, Exception {
1673
    	String documentDir = PropertyService.getProperty("application.documentfilepath");
1674
    	
1667 1675
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
1668 1676
        try {
1669 1677
            
......
1744 1752
                    response.setContentType("text/xml"); //MIME type
1745 1753
                    response.setHeader("Content-Disposition",
1746 1754
                            "attachment; filename=" + docid + ".xml");
1755
                    
1756
                    // Try to get the metadata file from disk. If it isn't
1757
					// found, create it from the
1758
					// db and write it to disk then.
1759
					String documentPath = documentDir + FileUtil.getFS() + docid;
1760
					try {
1761
						if (FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST) {
1762
							FileWriter fileWriter = new FileWriter(documentPath);
1763
							doc.toXml(fileWriter, user, groups, withInlineData);
1764
						}
1765
						FileReader fileReader = new FileReader(documentPath);
1766
						BufferedReader br = new BufferedReader(fileReader);
1767
						PrintWriter pw = new PrintWriter(streamOut);
1768
						String nextLine;
1769
						while ((nextLine = br.readLine()) != null) {
1770
							pw.println(nextLine);
1771
						}
1772
						pw.flush();					
1773
					} catch (Exception e) {
1774
						// any exceptions in reading the xml from disc, and we go back to the
1775
						// old way of creating the xml directly.
1776
						logMetacat.error("could not read from document file " + documentPath 
1777
								+ ": " + e.getMessage());
1778
						PrintWriter out = new PrintWriter(streamOut);
1779
						doc.toXml(out, user, groups, withInlineData);
1780
					}
1781
                    	
1747 1782
                    PrintWriter out = new PrintWriter(streamOut);
1748 1783
                    doc.toXml(out, user, groups, withInlineData);
1749 1784
                } else {
......
2097 2132
                            .getDBConnection("MetaCatServlet.handleInsertOrUpdateAction");
2098 2133
                    serialNumber = dbConn.getCheckOutSerialNumber();
2099 2134
                    
2100
                    // write the document to the database
2135
                    // write the document to the database and disk
2101 2136
                    try {
2102 2137
                        String accNumber = docid[0];
2103 2138
                        logMetacat.debug("" + doAction + " "
......
2108 2143

  
2109 2144
                        newdocid = documentWrapper.write(dbConn, xml, pub, dtd,
2110 2145
                                doAction, accNumber, user, groups);
2146
                        
2147
                        // write the document to disk
2148
						FileWriter fileWriter = null;
2149
						try {
2150
							String documentDir = 
2151
								PropertyService.getProperty("application.documentfilepath");
2152
							String documentPath = documentDir + FileUtil.getFS() + accNumber;
2153

  
2154
							if (accNumber != null
2155
									&& FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST) {
2156
								fileWriter = new FileWriter(documentPath);
2157
								fileWriter.write(doctext[0]);
2158
							}
2159
						} catch (IOException ioe) {
2160
							logMetacat.error("Did not write " + accNumber
2161
									+ " to file system: " + ioe.getMessage());
2162
						} finally {
2163
							if (fileWriter != null) {
2164
								fileWriter.close();
2165
							}
2166
						}
2167
                                
2111 2168
                        EventLog.getInstance().log(request.getRemoteAddr(),
2112 2169
                                user, accNumber, action[0]);
2113 2170
                    } catch (NullPointerException npe) {
2171
                      // MCD this seems a bit dubious, since many things can throw npe
2114 2172
                      System.out.println("writing with null acnumber");
2115 2173
                        newdocid = documentWrapper.write(dbConn, xml, pub, dtd,
2116 2174
                                doAction, null, user, groups);

Also available in: Unified diff