Project

General

Profile

« Previous | Next » 

Revision 4341

Added by daigle over 15 years ago

Created a method in DocumentImpl to read a document file from disk. Created a support method to strip inline data for users that don't have read access.

View differences:

src/edu/ucsb/nceas/metacat/MetaCatServlet.java
1762 1762
							FileWriter fileWriter = new FileWriter(documentPath);
1763 1763
							doc.toXml(fileWriter, user, groups, withInlineData);
1764 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();					
1765
						
1766
						PrintWriter out = new PrintWriter(streamOut);
1767
						doc.readFromFileSystem(out, user, groups, documentPath);				
1773 1768
					} catch (Exception e) {
1774 1769
						// any exceptions in reading the xml from disc, and we go back to the
1775 1770
						// old way of creating the xml directly.
......
1778 1773
						PrintWriter out = new PrintWriter(streamOut);
1779 1774
						doc.toXml(out, user, groups, withInlineData);
1780 1775
					}
1781
                    	
1782
                    PrintWriter out = new PrintWriter(streamOut);
1783
                    doc.toXml(out, user, groups, withInlineData);
1784 1776
                } else {
1777
                	// TODO MCD, this should read from disk as well?
1785 1778
                    //*** This is a metadata doc, to be returned in a skin/custom format.
1786 1779
                    //*** Add param to indicate if public has read access or not.
1787 1780
                    if (!user.equals("public")) {
src/edu/ucsb/nceas/metacat/DocumentImpl.java
28 28

  
29 29
import java.io.BufferedInputStream;
30 30
import java.io.BufferedOutputStream;
31
import java.io.BufferedReader;
31 32
import java.io.File;
32 33
import java.io.FileOutputStream;
34
import java.io.FileReader;
33 35
import java.io.IOException;
34 36
import java.io.InputStream;
35 37
import java.io.PrintWriter;
......
45 47
import java.util.HashMap;
46 48
import java.util.Iterator;
47 49
import java.util.Stack;
50
import java.util.Set;
48 51
import java.util.TreeSet;
49 52
import java.util.Vector;
53
import java.util.regex.Matcher;
54
import java.util.regex.Pattern;
50 55

  
51 56
import edu.ucsb.nceas.metacat.service.DatabaseService;
52 57
import edu.ucsb.nceas.metacat.service.PropertyService;
......
1272 1277
        }
1273 1278
        out.flush();
1274 1279
    }
1280
    
1281
    /**
1282
	 * Read the XML document from the file system and write to a Writer. Strip
1283
	 * out any inline data that the user does not have permission to read.
1284
	 * 
1285
	 * @param pw
1286
	 *            the Writer to which we print the document
1287
	 * @param user
1288
	 *            the user we will use to verify inline data access
1289
	 * @param groups
1290
	 *            the groups we will use to verify inline data access
1291
	 * @param documentPath
1292
	 *            the location of the document on disk
1293
	 * 
1294
	 */
1295
    public void readFromFileSystem(Writer pw, String user, String[] groups,
1296
            String documentPath) throws McdbException {
1297
    	
1298
    	boolean proccessEml2 = false;
1299
        boolean readInlinedata = false;
1300
    	
1301
        // if this is not EML version 2.x, we do not need to worry about 
1302
        // inline data
1303
        if (doctype != null
1304
                && (doctype.equals(EML2_0_0NAMESPACE)
1305
                        || doctype.equals(EML2_0_1NAMESPACE) 
1306
                        || doctype.equals(EML2_1_0NAMESPACE))) {
1307
            proccessEml2 = true;
1308
        }
1309
        
1310
        // create a printwriter
1311
        PrintWriter out = null;
1312
        if (pw instanceof PrintWriter) {
1313
            out = (PrintWriter) pw;
1314
        } else {
1315
            out = new PrintWriter(pw);
1316
        }
1317
        
1275 1318

  
1319
		String xmlFileLine;
1320
		String xmlFileContents = "";
1321
		
1322
		try {
1323
			// read the file from disk
1324
			FileReader fileReader = new FileReader(documentPath);
1325
			BufferedReader br = new BufferedReader(fileReader);
1326
			while ((xmlFileLine = br.readLine()) != null) {
1327
				xmlFileContents += xmlFileLine;
1328
			}
1329
            
1330
			// get a list of inline data sections that are not readable
1331
			// by this user
1332
            Hashtable<String, String> unReadableInlineDataList =
1333
                PermissionController
1334
                    .getUnReadableInlineDataIdList(docid,
1335
                            user, groups, true);
1336
            
1337
            // for each unreadable section, strip the inline data from the doc
1338
            if (proccessEml2 && unReadableInlineDataList.size() > 0) {
1339
                Set<String> inlineKeySet = unReadableInlineDataList.keySet();
1340
            	for(String inlineKey : inlineKeySet) {
1341
            		String inlineValue = unReadableInlineDataList.get(inlineKey);
1342
            		if (inlineValue.startsWith(docid)) {
1343
            			xmlFileContents = stripInlineData(xmlFileContents, inlineKey);
1344
            		}
1345
            	}
1346
            }
1347
            
1348
            // write the doc
1349
			pw.write(xmlFileContents);
1350
			pw.close();	
1351
			
1352
            
1353
        } catch (Exception e) {
1354
            throw new McdbException("Error reading document " + documentPath 
1355
            		+ " from disk: " + e.getMessage());
1356
        }
1357
        
1358
    }
1359
    
1276 1360
    /**
1361
	 * Strip out an inline data section from a document. This assumes that the
1362
	 * inline element is within a distribution element and the key for the
1363
	 * distribution is the same as the subtreeid in the xml_access table.
1364
	 * 
1365
	 * @param xmlFileContents
1366
	 *            the contents of the file
1367
	 * @param inLineKey
1368
	 *            the unique key for this inline element
1369
	 */
1370
    private String stripInlineData(String xmlFileContents, String inLineKey) {
1371
    	String changedString = xmlFileContents;
1372
    	String regex = "(<distribution .*id=\"" + inLineKey + "\">.*)<inline>.*</inline>";
1373
    	Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); 
1374
    	Matcher matcher = pattern.matcher(xmlFileContents);
1375
    	if (matcher.find()) {
1376
    		changedString = matcher.replaceFirst(matcher.group(1) + "<inline></inline>");
1377
    	}	
1378
    	
1379
    	return changedString;
1380
    }
1381

  
1382
    /**
1277 1383
     * Build the index records for this document.  For each node, all absolute
1278 1384
     * and relative paths to the root of the document are created and inserted
1279 1385
     * into the xml_index table.  This requires that the DocumentImpl instance

Also available in: Unified diff