Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements org.xml.sax.EntityResolver interface
4
 *             for resolving external entities
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Jivka Bojilova, Matt Jones
8
 *
9
 *   '$Author: tao $'
10
 *     '$Date: 2016-01-15 17:17:08 -0800 (Fri, 15 Jan 2016) $'
11
 * '$Revision: 9492 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27

    
28
package edu.ucsb.nceas.metacat;
29

    
30
import org.apache.log4j.Logger;
31
import org.xml.sax.*;
32
import org.xml.sax.helpers.DefaultHandler;
33

    
34
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlList;
35
import edu.ucsb.nceas.metacat.database.DBConnection;
36
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
37
import edu.ucsb.nceas.metacat.util.SystemUtil;
38
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
39

    
40
import java.sql.*;
41
import java.io.File;
42
import java.io.Reader;
43
import java.io.BufferedReader;
44
import java.io.BufferedInputStream;
45
import java.io.FileWriter;
46
import java.io.BufferedWriter;
47
import java.io.InputStream;
48
import java.io.IOException;
49
import java.net.URL;
50
import java.net.MalformedURLException;
51

    
52
/**
53
 * A database aware Class implementing EntityResolver interface for the SAX
54
 * parser to call when processing the XML stream and intercepting any
55
 * external entities (including the external DTD subset and external
56
 * parameter entities, if any) before including them.
57
 */
58
public class DBEntityResolver implements EntityResolver
59
{
60
  private DBConnection connection = null;
61
  private DefaultHandler handler = null;
62
  private String docname = null;
63
  private String doctype = null;
64
  private String systemid = null;
65
  private Reader dtdtext = null;
66
  private static Logger logMetacat = Logger.getLogger(DBEntityResolver.class);
67
  
68
  /**
69
   * Construct an instance of the DBEntityResolver class
70
   *
71
   * @param conn the JDBC connection to which information is written
72
   */
73
  public DBEntityResolver(DBConnection conn)
74
  {
75
    this.connection= conn;
76
  }
77
  /**
78
   * Construct an instance of the DBEntityResolver class
79
   *
80
   * @param conn the JDBC connection to which information is written
81
   * @param handler the SAX handler to determine parsing context
82
   * @param dtd Reader of new dtd to be uploaded on server's file system
83
   */
84
  public DBEntityResolver(DBConnection conn, DefaultHandler handler, Reader dtd)
85
  {
86
    this.connection = conn;
87
    this.handler = handler;
88
    this.dtdtext = dtd;
89
  }
90

    
91
  /**
92
   * The Parser call this method before opening any external entity
93
   * except the top-level document entity (including the external DTD subset,
94
   * external entities referenced within the DTD, and external entities
95
   * referenced within the document element)
96
   */
97
  public InputSource resolveEntity (String publicId, String systemId)
98
                     throws SAXException
99
  {
100
    logMetacat.debug("DBEntityResolver.resolveEntity - in resolveEntity");
101
    String dbSystemID;
102
    String doctype = null;
103

    
104
    // Won't have a handler under all cases
105
    if ( handler != null ) {
106
      if ( handler instanceof DBSAXHandler ) {
107
        DBSAXHandler dhandler = null;
108
        dhandler = (DBSAXHandler)handler;
109
        if ( dhandler.processingDTD() ) {
110
         
111
          // public ID is doctype
112
          if (publicId != null) {
113
            doctype = publicId;
114
            logMetacat.debug("DBEntityResolver.resolveEntity - in get type from publicId and doctype is: "
115
                                     + doctype);
116
          // assume public ID (doctype) is docname
117
          } else if (systemId != null) {
118
            doctype = dhandler.getDocname();
119
          }
120
        }
121
      } else if ( handler instanceof AccessControlList ) {
122
        AccessControlList ahandler = null;
123
        ahandler = (AccessControlList)handler;
124
        //if ( ahandler.processingDTD() ) {
125
          // public ID is doctype
126
          if (publicId != null) {
127
            doctype = publicId;
128
          // assume public ID (doctype) is docname
129
          } else if (systemId != null) {
130
            doctype = ahandler.getDocname();
131
          }
132
        //}
133
      }
134
    }
135

    
136
    // get System ID for doctype
137
    if (doctype != null) {
138
      // look at db XML Catalog for System ID
139
      logMetacat.info("DBEntityResolver.resolveEntity - get systemId from doctype: " + doctype);
140
      dbSystemID = getDTDSystemID(doctype);
141
      logMetacat.info("DBEntityResolver.resolveEntity - The Systemid is: " + dbSystemID);
142
      // check that it is accessible on our system before getting too far
143
      try {
144
    	  InputStream in = checkURLConnection(dbSystemID);
145
	  } catch (SAXException se) {
146
		  // after an upgrade, the dtd will not exist on disk, but it is in xml catalog.  The db system id may be pointing 
147
		  // back at this system  Try and download it from the original system id and see if we still have a problem
148
		  // checking the URL connection.
149
		  logMetacat.warn("DBEntityResolver.resolveEntity - Problem when checking URL Connection: " + se.getMessage());
150
		  logMetacat.warn("DBEntityResolver.resolveEntity - Probably, dtd for doc type " + doctype + " existed in xml catalog, but not on disk.  Uploading from: " + systemId);
151
		  InputStream istream = checkURLConnection(systemId);
152
		  uploadDTDFromURL(istream, systemId);
153
		  try {
154
			  Thread.currentThread().sleep(6000);
155
			  checkURLConnection(dbSystemID);
156
		  } catch (Exception e2) {
157
			  logMetacat.error("DBEntityResolver.resolveEntity - still could not find dtd for doc type " + doctype + " at " 
158
					  + dbSystemID + " : " + e2.getMessage());
159
			  dbSystemID = null;
160
		  }
161
	  } 
162
      boolean doctypeIsInDB = true;
163
      // no System ID found in db XML Catalog
164
      if (dbSystemID == null) {
165
        doctypeIsInDB = false;
166
        // use the provided System ID
167
        if (systemId != null) {
168
          dbSystemID = systemId;
169
        }
170
        logMetacat.info("DBEntityResolver.resolveEntity - If above Systemid is null, then get "
171
                                 + "system id from file: " + dbSystemID);
172
      }
173
      // there are dtd text provided; try to upload on Metacat
174
      if ( dtdtext != null ) {
175
        dbSystemID = uploadDTD(dbSystemID);
176
      }
177

    
178
      // open URLConnection to check first
179
      InputStream istream = checkURLConnection(dbSystemID);
180

    
181
      // need to register System ID in db XML Catalog if not yet
182
      if ( !doctypeIsInDB ) {
183
        // new DTD from outside URL location; try to upload on Metacat
184
        if ( dtdtext == null ) {
185
          dbSystemID = uploadDTDFromURL(istream, dbSystemID);
186
        }
187
        registerDTD(doctype, dbSystemID);
188
      }
189
      // return a byte-input stream for use
190
      InputSource is = new InputSource(dbSystemID);
191

    
192
      // close and open URLConnection again
193
      try {
194
        istream.close();
195
      } catch (IOException e) {
196
        throw new SAXException
197
        ("DBEntityResolver.resolveEntity - I/O issue when resolving entity: " + e.getMessage());
198
      }
199
      istream = checkURLConnection(dbSystemID);
200
      is.setByteStream(istream);
201
      return is;
202
    } else {
203
      // use provided systemId for the other cases
204
      logMetacat.info("DBEntityResolver.resolveEntity - doctype is null and using system id from file");
205
      InputStream istream = checkURLConnection(systemId);
206
      return null;
207

    
208
    }
209

    
210
  }
211

    
212
  /**
213
   * Look at db XML Catalog to get System ID (if any) for @doctype.
214
   * Return null if there are no System ID found for @doctype
215
   */
216
  public static String getDTDSystemID( String doctype )
217
                 throws SAXException
218
  {
219
    String systemid = null;
220
    PreparedStatement pstmt = null;
221
    DBConnection conn = null;
222
    int serialNumber = -1;
223
    ResultSet rs = null;
224
    try {
225
      //check out DBConnection
226
      conn=DBConnectionPool.getDBConnection("DBEntityResolver.getDTDSystemID");
227
      serialNumber=conn.getCheckOutSerialNumber();
228

    
229
      String sql = "SELECT system_id FROM xml_catalog " +
230
      "WHERE entry_type = 'DTD' AND public_id = ?";
231
      
232
      pstmt = conn.prepareStatement(sql);
233
      pstmt.setString(1, doctype);
234
      
235
      pstmt.execute();
236
      rs = pstmt.getResultSet();
237
      boolean tableHasRows = rs.next();
238
      if (tableHasRows) {
239
        systemid = rs.getString(1);
240
        // system id may not have server url on front.  Add it if not.
241
        if (!systemid.startsWith("http://")) {
242
        	systemid = SystemUtil.getContextURL() + systemid;
243
        }
244
      }
245
      //pstmt.close();
246
    } catch (SQLException e) {
247
      throw new SAXException
248
      ("DBEntityResolver.getDTDSystemID - SQL error when getting DTD system ID: " + e.getMessage());
249
    } catch (PropertyNotFoundException pnfe) {
250
        throw new SAXException
251
        ("DBEntityResolver.getDTDSystemID - Property error when getting DTD system ID:  " + pnfe.getMessage());
252
      }
253
    finally
254
    {
255
      try
256
      {
257
          if(rs != null) {
258
              rs.close();
259
          }
260
          if(pstmt != null) {
261
              pstmt.close();
262
          }
263
        
264
      }//try
265
      catch (SQLException sqlE)
266
      {
267
        logMetacat.error("DBEntityResolver.getDTDSystemId - SQL error: " + sqlE.getMessage());
268
      }//catch
269
      finally
270
      {
271
        DBConnectionPool.returnDBConnection(conn, serialNumber);
272
      }//finally
273
    }//finally
274

    
275
    // return the selected System ID
276
    return systemid;
277
  }
278

    
279
  /**
280
   * Register new DTD identified by @systemId in Metacat XML Catalog
281
   * . make a reference with @systemId for @doctype in Metacat DB
282
   */
283
  private void registerDTD ( String doctype, String systemId )
284
                 throws SAXException
285
  {
286
	  String existingSystemId = getDTDSystemID(doctype);
287
	  if (existingSystemId != null && existingSystemId.equals(systemId)) {
288
		  logMetacat.warn("DBEntityResolver.registerDTD - doctype/systemId already registered in DB: " + doctype);
289
		  return;
290
	  }
291
    //DBConnection conn = null;
292
    //int serialNumber = -1;
293
    PreparedStatement pstmt = null;
294
    // make a reference in db catalog table with @systemId for @doctype
295
    try {
296
      //check out DBConnection
297
      //conn=DBConnectionPool.getDBConnection("DBEntityResolver.registerDTD");
298
      //serialNumber=conn.getCheckOutSerialNumber();
299

    
300

    
301
      pstmt = connection.prepareStatement(
302
             "INSERT INTO xml_catalog " +
303
             "(entry_type, public_id, system_id) " +
304
             "VALUES ('DTD', ?, ?)");
305
      // Increase usage count
306
      connection.increaseUsageCount(1);
307
      // Bind the values to the query
308
      pstmt.setString(1, doctype);
309
      pstmt.setString(2, systemId);
310
      // Do the insertion
311
      pstmt.execute();
312
      int updateCnt = pstmt.getUpdateCount();
313
      logMetacat.debug("DBEntityReolver.registerDTD - DTDs registered: " + updateCnt);
314
      pstmt.close();
315
    } catch (SQLException e) {
316
      throw new SAXException
317
      ("DBEntityResolver.registerDTD - SQL issue when registering DTD: " + e.getMessage());
318
    }
319
    finally
320
    {
321
      try
322
      {
323
        pstmt.close();
324
      }//try
325
      catch (SQLException sqlE)
326
      {
327
        logMetacat.error("DBEntityResolver.registerDTD - SQL error: " + sqlE.getMessage());
328
      }//catch
329
      //DBConnectionPool.returnDBConnection(conn, serialNumber);
330
    }//finally
331

    
332
  }
333

    
334
  /**
335
	 * Upload new DTD text identified by
336
	 * 
337
	 * @systemId to Metacat file system
338
	 */
339
	private String uploadDTD(String systemId) throws SAXException {
340
		String dtdPath = null;
341
		String dtdURL = null;
342
		try {
343
			dtdPath = SystemUtil.getContextDir() + "/dtd/";
344
			dtdURL = SystemUtil.getContextURL() + "/dtd/";
345
		} catch (PropertyNotFoundException pnfe) {
346
			throw new SAXException("DBEntityResolver.uploadDTD: " + pnfe.getMessage());
347
		}
348

    
349
		// get filename from systemId
350
		String filename = systemId;
351
		int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
352
		if (slash > -1) {
353
			filename = filename.substring(slash + 1);
354
		}
355

    
356
		// writing dtd text on Metacat file system as filename
357
		try {
358
			// create a buffering character-input stream
359
			// that uses a default-sized input buffer
360
			BufferedReader in = new BufferedReader(dtdtext);
361

    
362
			// open file writer to write the input into it
363
			// String dtdPath = "/opt/tomcat/webapps/bojilova/dtd/";
364
			File f = new File(dtdPath, filename);
365
			synchronized (f) {
366
				try {
367
					if (f.exists()) {
368
						throw new IOException("File already exist: "
369
								+ f.getCanonicalFile());
370
						// if ( f.exists() && !f.canWrite() ) {
371
						// throw new IOException("Not writable: " +
372
						// f.getCanonicalFile());
373
					}
374
				} catch (SecurityException se) {
375
					// if a security manager exists,
376
					// its checkRead method is called for f.exist()
377
					// or checkWrite method is called for f.canWrite()
378
					throw se;
379
				}
380
				// create a buffered character-output stream
381
				// that uses a default-sized output buffer
382
				FileWriter fw = new FileWriter(f);
383
				BufferedWriter out = new BufferedWriter(fw);
384

    
385
				// read the input and write into the file writer
386
				String inputLine;
387
				while ((inputLine = in.readLine()) != null) {
388
					out.write(inputLine, 0, inputLine.length());
389
					out.newLine(); // instead of out.write('\r\n');
390
				}
391

    
392
				// the input and the output streams must be closed
393
				in.close();
394
				out.flush();
395
				out.close();
396
				fw.close();
397
			} // end of synchronized
398
		} catch (MalformedURLException e) {
399
			throw new SAXException("DBEntityResolver.uploadDTD() - Malformed URL when uploading DTD: " + e.getMessage());
400
		} catch (IOException e) {
401
			throw new SAXException("DBEntityResolver.uploadDTD - I/O issue when uploading DTD: " + e.getMessage());
402
		} catch (SecurityException e) {
403
			throw new SAXException("DBEntityResolver.uploadDTD() - Security issue when uploading DTD: " + e.getMessage());
404
		}
405

    
406
		// String dtdURL = "http://dev.nceas.ucsb.edu/bojilova/dtd/";
407
		return dtdURL + filename;
408
	}
409

    
410

    
411
  /**
412
	 * Upload new DTD located at outside URL to Metacat file system
413
	 */
414
	private String uploadDTDFromURL(InputStream istream, String systemId)
415
			throws SAXException {
416
		String dtdPath = null;
417
		String dtdURL = null;
418
		try {
419
			dtdPath = SystemUtil.getContextDir() + "/dtd/";
420
			dtdURL = SystemUtil.getContextURL() + "/dtd/";
421
		} catch (PropertyNotFoundException pnfe) {
422
			throw new SAXException("DBEntityResolver.uploadDTDFromURL - Property issue when uploading DTD from URL: "
423
					+ pnfe.getMessage());
424
		}
425

    
426
		// get filename from systemId
427
		String filename = systemId;
428
		int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
429
		if (slash > -1) {
430
			filename = filename.substring(slash + 1);
431
		}
432

    
433
		// writing dtd text on Metacat file system as filename
434
		try {
435
			// create a buffering character-input stream
436
			// that uses a default-sized input buffer
437
			BufferedInputStream in = new BufferedInputStream(istream);
438

    
439
			// open file writer to write the input into it
440
			//String dtdPath = "/opt/tomcat/webapps/bojilova/dtd/";
441
			File f = new File(dtdPath, filename);
442
			synchronized (f) {
443
				try {
444
					if (f.exists()) {
445
						logMetacat.warn("DBEntityResolver.uploadDTDFromURL - File already exists: " + f.getCanonicalFile());
446
						//return dtdURL + filename;
447
						//throw new IOException("File already exist: "
448
						//		+ f.getCanonicalFile());
449
						//if ( f.exists() && !f.canWrite() ) {
450
						//  throw new IOException("Not writable: " + f.getCanonicalFile());
451
					}
452
				} catch (SecurityException se) {
453
					// if a security manager exists,
454
					// its checkRead method is called for f.exist()
455
					// or checkWrite method is called for f.canWrite()
456
					throw se;
457
				}
458
				// create a buffered character-output stream
459
				// that uses a default-sized output buffer
460
				FileWriter fw = new FileWriter(f);
461
				BufferedWriter out = new BufferedWriter(fw);
462

    
463
				// read the input and write into the file writer
464
				int inputByte;
465
				while ((inputByte = in.read()) != -1) {
466
					out.write(inputByte);
467
					//out.newLine(); //instead of out.write('\r\n');
468
				}
469

    
470
				// the input and the output streams must be closed
471
				in.close();
472
				out.flush();
473
				out.close();
474
				fw.close();
475
			} // end of synchronized
476
		} catch (MalformedURLException e) {
477
			throw new SAXException("DBEntityResolver.uploadDTDFromURL - Malformed URL when uploading DTD from URL: "
478
					+ e.getMessage());
479
		} catch (IOException e) {
480
			throw new SAXException("DBEntityResolver.uploadDTDFromURL - I/O issue when uploading DTD from URL:  "
481
					+ e.getMessage());
482
		} catch (SecurityException e) {
483
			throw new SAXException("DBEntityResolver.uploadDTDFromURL - Security issue when uploading DTD from URL:  "
484
					+ e.getMessage());
485
		}
486

    
487
		//String dtdURL = "http://dev.nceas.ucsb.edu/bojilova/dtd/";
488
		return dtdURL + filename;
489
	}
490

    
491
	/**
492
	 * Check URL Connection for @systemId, and return an InputStream
493
	 * that can be used to read from the systemId URL.  The parser ends
494
	 * up using this via the InputSource to read the DTD.
495
	 *
496
	 * @param systemId a URI (in practice URL) to be checked and opened
497
	 */
498
	public static InputStream checkURLConnection(String systemId) throws SAXException {
499
		try {
500
			return (new URL(systemId).openStream());
501

    
502
		} catch (MalformedURLException e) {
503
			throw new SAXException("DBEntityResolver.checkURLConnection - Malformed URL when checking URL Connection: "
504
					+ e.getMessage());
505
		} catch (IOException e) {
506
			throw new SAXException("DBEntityResolver.checkURLConnection - I/O issue when checking URL Connection: "
507
					+ e.getMessage());
508
		}
509
	}
510
}
(16-16/64)