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-09-13 13:07:09 -0700 (Tue, 13 Sep 2016) $'
11
 * '$Revision: 9974 $'
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
    InputSource dtdSource= null;
101
    logMetacat.debug("DBEntityResolver.resolveEntity - in resolveEntity "+" the public id is "+publicId+" and systemId is "+systemId);
102
    String dbSystemID;
103
    String doctype = null;
104

    
105
    // Won't have a handler under all cases
106
    if ( handler != null ) {
107
        String message = "Metacat can't determine the public id or the name of the root element of the document, so the validation can't be applied and the document is rejected";
108
        logMetacat.debug("DBEntityResolver.resolveEntity - the handler class is "+handler.getClass().getCanonicalName());
109
      if ( handler instanceof DBSAXHandler ) {
110
        DBSAXHandler dhandler = null;
111
        dhandler = (DBSAXHandler)handler;
112
        if ( dhandler.processingDTD() ) {
113
            logMetacat.debug("DBEntityResolver.resolveEntity - in the branch of the handler class is  DBSAXHandler");
114
          // public ID is doctype
115
          if (publicId != null) {
116
            doctype = publicId;
117
            logMetacat.debug("DBEntityResolver.resolveEntity - the publicId is not null, so the publicId is the doctype. The doctype is: "
118
                                     + doctype);
119
          // assume public ID (doctype) is docname
120
          } else {
121
            doctype = dhandler.getDocname();
122
            logMetacat.debug("DBEntityResolver.resolveEntity - the publicId is null and we treat the doc name(the root element name) as the doc type. The doctype is: "
123
                    + doctype);
124
          }
125
          
126
          if(doctype == null || doctype.trim().equals("")) {
127
              //we can't determine the public id or the name of the root element in for this dtd defined xml document
128
              logMetacat.error("DBEntityResolver.resolveEntity - "+message);
129
              throw new SAXException(message);
130
          } else {
131
              logMetacat.debug("DBEntityResolver.resolveEntity - the final doctype for DBSAXHandler "+doctype);
132
          }
133
        }
134
      } else if ( handler instanceof AccessControlList ) {
135
          logMetacat.debug("DBEntityResolver.resolveEntity - in the branch of the handler class is AccessControlList");
136
        AccessControlList ahandler = null;
137
        ahandler = (AccessControlList)handler;
138
        if ( ahandler.processingDTD() ) {
139
          // public ID is doctype
140
          if (publicId != null) {
141
            doctype = publicId;
142
            logMetacat.debug("DBEntityResolver.resolveEntity - the publicId is not null, so the publicId is the doctype. The doctype in AccessControlList is: "
143
                    + doctype);
144
          // assume public ID (doctype) is docname
145
          } else {
146
            doctype = ahandler.getDocname();
147
            logMetacat.debug("DBEntityResolver.resolveEntity - the publicId is null and we treat the doc name(the root element name) as the doc type. The doctype in AccessControlList is: "
148
                    + doctype);
149
          }
150
          if(doctype == null || doctype.trim().equals("")) {
151
              //we can't determine the public id or the name of the root element in for this dtd defined xml document
152
              logMetacat.error("DBEntityResolver.resolveEntity - "+message);
153
              throw new SAXException(message);
154
          } else {
155
              logMetacat.debug("DBEntityResolver.resolveEntity - the final doctype for AccessControList "+doctype);
156
          }
157
        } else {
158
            logMetacat.debug("DBEntityResolver.resolveEntity - the method resolverEntity for the AccessControList class is not processing a dtd");
159
        }
160
      } else {
161
          logMetacat.debug("DBEntityResolver.resolveEntity - in the branch of the other handler class");
162
      }
163
    } else {
164
        logMetacat.debug("DBEntityResolver.resolveEntity - the xml handler is null. So we can't find the doctype.");
165
    }
166

    
167
    // get System ID for doctype
168
    if (doctype != null) {
169
      // look at db XML Catalog for System ID
170
      logMetacat.info("DBEntityResolver.resolveEntity - get systemId from doctype: " + doctype);
171
      dbSystemID = getDTDSystemID(doctype);
172
      logMetacat.info("DBEntityResolver.resolveEntity - The Systemid from xml_catalog table is: " + dbSystemID);
173
      if(dbSystemID == null) {
174
          logMetacat.error("DBEntityResolver.resolveEntity - "+"The doctype: "+doctype+" , which was defined by a DTD document, isn't registered in Metacat. Please contact the operator of the Metacat");
175
          throw new SAXException("The doctype: "+doctype+" , which was defined by a DTD document, isn't registered in Metacat. Please contact the operator of the Metacat");
176
      }
177
      // check that it is accessible on our system before getting too far
178
      try {
179
    	  InputStream in = checkURLConnection(dbSystemID);
180
    	  dtdSource = new InputSource(in);
181
	  } catch (SAXException se) {
182
	      se.printStackTrace();
183
	      throw se;
184
		  // after an upgrade, the dtd will not exist on disk, but it is in xml catalog.  The db system id may be pointing 
185
		  // back at this system  Try and download it from the original system id and see if we still have a problem
186
		  // checking the URL connection.
187
		  /*logMetacat.warn("DBEntityResolver.resolveEntity - Problem when checking URL Connection: " + se.getMessage());
188
		  logMetacat.warn("DBEntityResolver.resolveEntity - Probably, dtd for doc type " + doctype + " existed in xml catalog, but not on disk.  Uploading from: " + systemId);
189
		  InputStream istream = checkURLConnection(systemId);
190
		  uploadDTDFromURL(istream, systemId);
191
		  try {
192
			  Thread.currentThread().sleep(6000);
193
			  checkURLConnection(dbSystemID);
194
		  } catch (Exception e2) {
195
			  logMetacat.error("DBEntityResolver.resolveEntity - still could not find dtd for doc type " + doctype + " at " 
196
					  + dbSystemID + " : " + e2.getMessage());
197
			  dbSystemID = null;
198
		  }*/
199
	  } 
200
      /*boolean doctypeIsInDB = true;
201
      // no System ID found in db XML Catalog
202
      if (dbSystemID == null) {
203
        doctypeIsInDB = false;
204
        // use the provided System ID
205
        if (systemId != null) {
206
          dbSystemID = systemId;
207
        }
208
        logMetacat.info("DBEntityResolver.resolveEntity - If above Systemid is null, then get "
209
                                 + "system id from file: " + dbSystemID);
210
      }
211
      // there are dtd text provided; try to upload on Metacat
212
      if ( dtdtext != null ) {
213
        dbSystemID = uploadDTD(dbSystemID);
214
      }
215

    
216
      // open URLConnection to check first
217
      InputStream istream = checkURLConnection(dbSystemID);
218

    
219
      // need to register System ID in db XML Catalog if not yet
220
      if ( !doctypeIsInDB ) {
221
        // new DTD from outside URL location; try to upload on Metacat
222
        if ( dtdtext == null ) {
223
          dbSystemID = uploadDTDFromURL(istream, dbSystemID);
224
        }
225
        registerDTD(doctype, dbSystemID);
226
      }
227
      // return a byte-input stream for use
228
      InputSource is = new InputSource(dbSystemID);
229

    
230
      // close and open URLConnection again
231
      try {
232
        istream.close();
233
      } catch (IOException e) {
234
        throw new SAXException
235
        ("DBEntityResolver.resolveEntity - I/O issue when resolving entity: " + e.getMessage());
236
      }
237
      istream = checkURLConnection(dbSystemID);
238
      is.setByteStream(istream);
239
      return is;*/
240
    } else {
241
    
242
      //InputStream istream = checkURLConnection(systemId);
243
      //return null;
244
    }
245
    return dtdSource;
246

    
247
  }
248

    
249
  /**
250
   * Look at db XML Catalog to get System ID (if any) for @doctype.
251
   * Return null if there are no System ID found for @doctype
252
   */
253
  public static String getDTDSystemID( String doctype )
254
                 throws SAXException
255
  {
256
    String systemid = null;
257
    PreparedStatement pstmt = null;
258
    DBConnection conn = null;
259
    int serialNumber = -1;
260
    ResultSet rs = null;
261
    try {
262
      //check out DBConnection
263
      conn=DBConnectionPool.getDBConnection("DBEntityResolver.getDTDSystemID");
264
      serialNumber=conn.getCheckOutSerialNumber();
265

    
266
      String sql = "SELECT system_id FROM xml_catalog " +
267
      "WHERE entry_type = 'DTD' AND public_id = ?";
268
      
269
      pstmt = conn.prepareStatement(sql);
270
      pstmt.setString(1, doctype);
271
      
272
      pstmt.execute();
273
      rs = pstmt.getResultSet();
274
      boolean tableHasRows = rs.next();
275
      if (tableHasRows) {
276
        systemid = rs.getString(1);
277
        // system id may not have server url on front.  Add it if not.
278
        if (!systemid.startsWith("http://")) {
279
        	systemid = SystemUtil.getContextURL() + systemid;
280
        }
281
      }
282
      //pstmt.close();
283
    } catch (SQLException e) {
284
      throw new SAXException
285
      ("DBEntityResolver.getDTDSystemID - SQL error when getting DTD system ID: " + e.getMessage());
286
    } catch (PropertyNotFoundException pnfe) {
287
        throw new SAXException
288
        ("DBEntityResolver.getDTDSystemID - Property error when getting DTD system ID:  " + pnfe.getMessage());
289
      }
290
    finally
291
    {
292
      try
293
      {
294
          if(rs != null) {
295
              rs.close();
296
          }
297
          if(pstmt != null) {
298
              pstmt.close();
299
          }
300
        
301
      }//try
302
      catch (SQLException sqlE)
303
      {
304
        logMetacat.error("DBEntityResolver.getDTDSystemId - SQL error: " + sqlE.getMessage());
305
      }//catch
306
      finally
307
      {
308
        DBConnectionPool.returnDBConnection(conn, serialNumber);
309
      }//finally
310
    }//finally
311

    
312
    // return the selected System ID
313
    return systemid;
314
  }
315

    
316
  /**
317
   * Register new DTD identified by @systemId in Metacat XML Catalog
318
   * . make a reference with @systemId for @doctype in Metacat DB
319
   */
320
  private void registerDTD ( String doctype, String systemId )
321
                 throws SAXException
322
  {
323
	  String existingSystemId = getDTDSystemID(doctype);
324
	  if (existingSystemId != null && existingSystemId.equals(systemId)) {
325
		  logMetacat.warn("DBEntityResolver.registerDTD - doctype/systemId already registered in DB: " + doctype);
326
		  return;
327
	  }
328
    //DBConnection conn = null;
329
    //int serialNumber = -1;
330
    PreparedStatement pstmt = null;
331
    // make a reference in db catalog table with @systemId for @doctype
332
    try {
333
      //check out DBConnection
334
      //conn=DBConnectionPool.getDBConnection("DBEntityResolver.registerDTD");
335
      //serialNumber=conn.getCheckOutSerialNumber();
336

    
337

    
338
      pstmt = connection.prepareStatement(
339
             "INSERT INTO xml_catalog " +
340
             "(entry_type, public_id, system_id) " +
341
             "VALUES ('DTD', ?, ?)");
342
      // Increase usage count
343
      connection.increaseUsageCount(1);
344
      // Bind the values to the query
345
      pstmt.setString(1, doctype);
346
      pstmt.setString(2, systemId);
347
      // Do the insertion
348
      pstmt.execute();
349
      int updateCnt = pstmt.getUpdateCount();
350
      logMetacat.debug("DBEntityReolver.registerDTD - DTDs registered: " + updateCnt);
351
      pstmt.close();
352
    } catch (SQLException e) {
353
      throw new SAXException
354
      ("DBEntityResolver.registerDTD - SQL issue when registering DTD: " + e.getMessage());
355
    }
356
    finally
357
    {
358
      try
359
      {
360
        pstmt.close();
361
      }//try
362
      catch (SQLException sqlE)
363
      {
364
        logMetacat.error("DBEntityResolver.registerDTD - SQL error: " + sqlE.getMessage());
365
      }//catch
366
      //DBConnectionPool.returnDBConnection(conn, serialNumber);
367
    }//finally
368

    
369
  }
370

    
371
  /**
372
	 * Upload new DTD text identified by
373
	 * 
374
	 * @systemId to Metacat file system
375
	 */
376
	private String uploadDTD(String systemId) throws SAXException {
377
		String dtdPath = null;
378
		String dtdURL = null;
379
		try {
380
			dtdPath = SystemUtil.getContextDir() + "/dtd/";
381
			dtdURL = SystemUtil.getContextURL() + "/dtd/";
382
		} catch (PropertyNotFoundException pnfe) {
383
			throw new SAXException("DBEntityResolver.uploadDTD: " + pnfe.getMessage());
384
		}
385

    
386
		// get filename from systemId
387
		String filename = systemId;
388
		int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
389
		if (slash > -1) {
390
			filename = filename.substring(slash + 1);
391
		}
392

    
393
		// writing dtd text on Metacat file system as filename
394
		try {
395
			// create a buffering character-input stream
396
			// that uses a default-sized input buffer
397
			BufferedReader in = new BufferedReader(dtdtext);
398

    
399
			// open file writer to write the input into it
400
			// String dtdPath = "/opt/tomcat/webapps/bojilova/dtd/";
401
			File f = new File(dtdPath, filename);
402
			synchronized (f) {
403
				try {
404
					if (f.exists()) {
405
						throw new IOException("File already exist: "
406
								+ f.getCanonicalFile());
407
						// if ( f.exists() && !f.canWrite() ) {
408
						// throw new IOException("Not writable: " +
409
						// f.getCanonicalFile());
410
					}
411
				} catch (SecurityException se) {
412
					// if a security manager exists,
413
					// its checkRead method is called for f.exist()
414
					// or checkWrite method is called for f.canWrite()
415
					throw se;
416
				}
417
				// create a buffered character-output stream
418
				// that uses a default-sized output buffer
419
				FileWriter fw = new FileWriter(f);
420
				BufferedWriter out = new BufferedWriter(fw);
421

    
422
				// read the input and write into the file writer
423
				String inputLine;
424
				while ((inputLine = in.readLine()) != null) {
425
					out.write(inputLine, 0, inputLine.length());
426
					out.newLine(); // instead of out.write('\r\n');
427
				}
428

    
429
				// the input and the output streams must be closed
430
				in.close();
431
				out.flush();
432
				out.close();
433
				fw.close();
434
			} // end of synchronized
435
		} catch (MalformedURLException e) {
436
			throw new SAXException("DBEntityResolver.uploadDTD() - Malformed URL when uploading DTD: " + e.getMessage());
437
		} catch (IOException e) {
438
			throw new SAXException("DBEntityResolver.uploadDTD - I/O issue when uploading DTD: " + e.getMessage());
439
		} catch (SecurityException e) {
440
			throw new SAXException("DBEntityResolver.uploadDTD() - Security issue when uploading DTD: " + e.getMessage());
441
		}
442

    
443
		// String dtdURL = "http://dev.nceas.ucsb.edu/bojilova/dtd/";
444
		return dtdURL + filename;
445
	}
446

    
447

    
448
  /**
449
	 * Upload new DTD located at outside URL to Metacat file system
450
	 */
451
	private String uploadDTDFromURL(InputStream istream, String systemId)
452
			throws SAXException {
453
		String dtdPath = null;
454
		String dtdURL = null;
455
		try {
456
			dtdPath = SystemUtil.getContextDir() + "/dtd/";
457
			dtdURL = SystemUtil.getContextURL() + "/dtd/";
458
		} catch (PropertyNotFoundException pnfe) {
459
			throw new SAXException("DBEntityResolver.uploadDTDFromURL - Property issue when uploading DTD from URL: "
460
					+ pnfe.getMessage());
461
		}
462

    
463
		// get filename from systemId
464
		String filename = systemId;
465
		int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
466
		if (slash > -1) {
467
			filename = filename.substring(slash + 1);
468
		}
469

    
470
		// writing dtd text on Metacat file system as filename
471
		try {
472
			// create a buffering character-input stream
473
			// that uses a default-sized input buffer
474
			BufferedInputStream in = new BufferedInputStream(istream);
475

    
476
			// open file writer to write the input into it
477
			//String dtdPath = "/opt/tomcat/webapps/bojilova/dtd/";
478
			File f = new File(dtdPath, filename);
479
			synchronized (f) {
480
				try {
481
					if (f.exists()) {
482
						logMetacat.warn("DBEntityResolver.uploadDTDFromURL - File already exists: " + f.getCanonicalFile());
483
						//return dtdURL + filename;
484
						//throw new IOException("File already exist: "
485
						//		+ f.getCanonicalFile());
486
						//if ( f.exists() && !f.canWrite() ) {
487
						//  throw new IOException("Not writable: " + f.getCanonicalFile());
488
					}
489
				} catch (SecurityException se) {
490
					// if a security manager exists,
491
					// its checkRead method is called for f.exist()
492
					// or checkWrite method is called for f.canWrite()
493
					throw se;
494
				}
495
				// create a buffered character-output stream
496
				// that uses a default-sized output buffer
497
				FileWriter fw = new FileWriter(f);
498
				BufferedWriter out = new BufferedWriter(fw);
499

    
500
				// read the input and write into the file writer
501
				int inputByte;
502
				while ((inputByte = in.read()) != -1) {
503
					out.write(inputByte);
504
					//out.newLine(); //instead of out.write('\r\n');
505
				}
506

    
507
				// the input and the output streams must be closed
508
				in.close();
509
				out.flush();
510
				out.close();
511
				fw.close();
512
			} // end of synchronized
513
		} catch (MalformedURLException e) {
514
			throw new SAXException("DBEntityResolver.uploadDTDFromURL - Malformed URL when uploading DTD from URL: "
515
					+ e.getMessage());
516
		} catch (IOException e) {
517
			throw new SAXException("DBEntityResolver.uploadDTDFromURL - I/O issue when uploading DTD from URL:  "
518
					+ e.getMessage());
519
		} catch (SecurityException e) {
520
			throw new SAXException("DBEntityResolver.uploadDTDFromURL - Security issue when uploading DTD from URL:  "
521
					+ e.getMessage());
522
		}
523

    
524
		//String dtdURL = "http://dev.nceas.ucsb.edu/bojilova/dtd/";
525
		return dtdURL + filename;
526
	}
527

    
528
	/**
529
	 * Check URL Connection for @systemId, and return an InputStream
530
	 * that can be used to read from the systemId URL.  The parser ends
531
	 * up using this via the InputSource to read the DTD.
532
	 *
533
	 * @param systemId a URI (in practice URL) to be checked and opened
534
	 */
535
	public static InputStream checkURLConnection(String systemId) throws SAXException {
536
		try {
537
			return (new URL(systemId).openStream());
538

    
539
		} catch (MalformedURLException e) {
540
			throw new SAXException("DBEntityResolver.checkURLConnection - Malformed URL when checking URL Connection: "
541
					+ e.getMessage());
542
		} catch (IOException e) {
543
			throw new SAXException("DBEntityResolver.checkURLConnection - I/O issue when checking URL Connection: "
544
					+ e.getMessage());
545
		}
546
	}
547
}
(16-16/64)