Project

General

Profile

1 72 bojilova
/**
2 203 jones
 *  '$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 243 jones
 *    Authors: Jivka Bojilova, Matt Jones
8 72 bojilova
 *
9 203 jones
 *   '$Author$'
10
 *     '$Date$'
11
 * '$Revision$'
12 669 jones
 *
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 72 bojilova
 */
27
28 75 jones
package edu.ucsb.nceas.metacat;
29 72 bojilova
30 2663 sgarg
import org.apache.log4j.Logger;
31 72 bojilova
import org.xml.sax.*;
32 598 bojilova
import org.xml.sax.helpers.DefaultHandler;
33 72 bojilova
34 5090 daigle
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlList;
35 5015 daigle
import edu.ucsb.nceas.metacat.database.DBConnection;
36
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
37 4080 daigle
import edu.ucsb.nceas.metacat.util.SystemUtil;
38
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
39
40 72 bojilova
import java.sql.*;
41 243 jones
import java.io.File;
42 598 bojilova
import java.io.Reader;
43
import java.io.BufferedReader;
44 694 bojilova
import java.io.BufferedInputStream;
45 598 bojilova
import java.io.FileWriter;
46
import java.io.BufferedWriter;
47 243 jones
import java.io.InputStream;
48
import java.io.IOException;
49 72 bojilova
import java.net.URL;
50
import java.net.MalformedURLException;
51
52 1358 tao
/**
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 122 jones
 * parameter entities, if any) before including them.
57 72 bojilova
 */
58
public class DBEntityResolver implements EntityResolver
59
{
60 1217 tao
  private DBConnection connection = null;
61 598 bojilova
  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 2663 sgarg
  private static Logger logMetacat = Logger.getLogger(DBEntityResolver.class);
67
68 1358 tao
  /**
69 598 bojilova
   * Construct an instance of the DBEntityResolver class
70
   *
71
   * @param conn the JDBC connection to which information is written
72 599 bojilova
   */
73 1217 tao
  public DBEntityResolver(DBConnection conn)
74 599 bojilova
  {
75 1217 tao
    this.connection= conn;
76 599 bojilova
  }
77 1358 tao
  /**
78 599 bojilova
   * Construct an instance of the DBEntityResolver class
79
   *
80
   * @param conn the JDBC connection to which information is written
81 598 bojilova
   * @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 1217 tao
  public DBEntityResolver(DBConnection conn, DefaultHandler handler, Reader dtd)
85 598 bojilova
  {
86 1217 tao
    this.connection = conn;
87 598 bojilova
    this.handler = handler;
88
    this.dtdtext = dtd;
89
  }
90 1358 tao
91
  /**
92
   * The Parser call this method before opening any external entity
93 598 bojilova
   * except the top-level document entity (including the external DTD subset,
94 1358 tao
   * external entities referenced within the DTD, and external entities
95 598 bojilova
   * referenced within the document element)
96
   */
97
  public InputSource resolveEntity (String publicId, String systemId)
98
                     throws SAXException
99
  {
100 2663 sgarg
    logMetacat.debug("in DBEntityResolver.resolveEntity");
101 598 bojilova
    String dbSystemID;
102
    String doctype = null;
103 1358 tao
104 598 bojilova
    // 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 1363 tao
111 598 bojilova
          // public ID is doctype
112 1358 tao
          if (publicId != null) {
113 598 bojilova
            doctype = publicId;
114 2663 sgarg
            logMetacat.info("in get type from publicId and doctype is: "
115
                                     +doctype);
116 598 bojilova
          // assume public ID (doctype) is docname
117
          } else if (systemId != null) {
118
            doctype = dhandler.getDocname();
119 72 bojilova
          }
120
        }
121 598 bojilova
      } else if ( handler instanceof AccessControlList ) {
122
        AccessControlList ahandler = null;
123
        ahandler = (AccessControlList)handler;
124
        //if ( ahandler.processingDTD() ) {
125
          // public ID is doctype
126 1358 tao
          if (publicId != null) {
127 598 bojilova
            doctype = publicId;
128
          // assume public ID (doctype) is docname
129
          } else if (systemId != null) {
130
            doctype = ahandler.getDocname();
131
          }
132
        //}
133
      }
134
    }
135 72 bojilova
136 598 bojilova
    // get System ID for doctype
137
    if (doctype != null) {
138 1358 tao
      // look at db XML Catalog for System ID
139 2663 sgarg
      logMetacat.info("get systemId from doctype: "+doctype);
140 598 bojilova
      dbSystemID = getDTDSystemID(doctype);
141 2663 sgarg
      logMetacat.info("The Systemid is: "+dbSystemID);
142 4803 leinfelder
      // check that it is accessible on our system before getting too far
143
      try {
144
    	  InputStream in = checkURLConnection(dbSystemID);
145 4978 daigle
	  } catch (Exception e) {
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("dtd for doc type " + doctype + " existed in xml catalog, but not on disk.  Uploading from: " + systemId);
150
		  InputStream istream = checkURLConnection(systemId);
151
		  uploadDTDFromURL(istream, systemId);
152
		  try {
153
			  checkURLConnection(dbSystemID);
154
		  } catch (Exception e2) {
155
			  logMetacat.error("still could not find dtd for doc type " + doctype + " at "
156
					  + dbSystemID + " : " + e2.getMessage());
157
			  dbSystemID = null;
158
		  }
159 4803 leinfelder
	  }
160 598 bojilova
      boolean doctypeIsInDB = true;
161
      // no System ID found in db XML Catalog
162
      if (dbSystemID == null) {
163
        doctypeIsInDB = false;
164
        // use the provided System ID
165
        if (systemId != null) {
166
          dbSystemID = systemId;
167
        }
168 2663 sgarg
        logMetacat.info("If above Systemid is null and then get "
169
                                 +" system id from file" + dbSystemID);
170 598 bojilova
      }
171
      // there are dtd text provided; try to upload on Metacat
172
      if ( dtdtext != null ) {
173
        dbSystemID = uploadDTD(dbSystemID);
174
      }
175
176 694 bojilova
      // open URLConnection to check first
177 598 bojilova
      InputStream istream = checkURLConnection(dbSystemID);
178
179
      // need to register System ID in db XML Catalog if not yet
180
      if ( !doctypeIsInDB ) {
181 694 bojilova
        // new DTD from outside URL location; try to upload on Metacat
182
        if ( dtdtext == null ) {
183
          dbSystemID = uploadDTDFromURL(istream, dbSystemID);
184
        }
185 598 bojilova
        registerDTD(doctype, dbSystemID);
186
      }
187
      // return a byte-input stream for use
188 1358 tao
      InputSource is = new InputSource(dbSystemID);
189 694 bojilova
190
      // close and open URLConnection again
191
      try {
192
        istream.close();
193
      } catch (IOException e) {
194 1358 tao
        throw new SAXException
195 4967 daigle
        ("DBEntityResolver.resolveEntity - I/O issue when resolving entity: " + e.getMessage());
196 1358 tao
      }
197 694 bojilova
      istream = checkURLConnection(dbSystemID);
198 598 bojilova
      is.setByteStream(istream);
199
      return is;
200
    } else {
201 694 bojilova
      // use provided systemId for the other cases
202 2663 sgarg
      logMetacat.info("doctype is null and using system id from file");
203 598 bojilova
      InputStream istream = checkURLConnection(systemId);
204
      return null;
205 1358 tao
206 598 bojilova
    }
207 1358 tao
208 598 bojilova
  }
209
210 1358 tao
  /**
211 598 bojilova
   * Look at db XML Catalog to get System ID (if any) for @doctype.
212
   * Return null if there are no System ID found for @doctype
213
   */
214 1895 tao
  public static String getDTDSystemID( String doctype )
215 598 bojilova
                 throws SAXException
216
  {
217
    String systemid = null;
218 1217 tao
    Statement stmt = null;
219
    DBConnection conn = null;
220
    int serialNumber = -1;
221 598 bojilova
    try {
222 1217 tao
      //check out DBConnection
223
      conn=DBConnectionPool.getDBConnection("DBEntityResolver.getDTDSystemID");
224
      serialNumber=conn.getCheckOutSerialNumber();
225 1358 tao
226 598 bojilova
      stmt = conn.createStatement();
227 1358 tao
      stmt.execute("SELECT system_id FROM xml_catalog " +
228 598 bojilova
                   "WHERE entry_type = 'DTD' AND public_id = '" +
229
                   doctype + "'");
230
      ResultSet rs = stmt.getResultSet();
231
      boolean tableHasRows = rs.next();
232
      if (tableHasRows) {
233
        systemid = rs.getString(1);
234 4080 daigle
        // system id may not have server url on front.  Add it if not.
235
        if (!systemid.startsWith("http://")) {
236 4123 daigle
        	systemid = SystemUtil.getContextURL() + systemid;
237 4080 daigle
        }
238 598 bojilova
      }
239
      stmt.close();
240
    } catch (SQLException e) {
241
      throw new SAXException
242 4967 daigle
      ("DBEntityResolver.getDTDSystemID - SQL error when getting DTD system ID: " + e.getMessage());
243 4080 daigle
    } catch (PropertyNotFoundException pnfe) {
244
        throw new SAXException
245 4967 daigle
        ("DBEntityResolver.getDTDSystemID - Property error when getting DTD system ID:  " + pnfe.getMessage());
246 4080 daigle
      }
247 1217 tao
    finally
248
    {
249
      try
250
      {
251
        stmt.close();
252
      }//try
253
      catch (SQLException sqlE)
254
      {
255 4967 daigle
        logMetacat.error("SQL error in DBEntityReolver.getDTDSystemId: "
256 2663 sgarg
                                  +sqlE.getMessage());
257 1217 tao
      }//catch
258
      finally
259
      {
260
        DBConnectionPool.returnDBConnection(conn, serialNumber);
261
      }//finally
262
    }//finally
263 598 bojilova
264
    // return the selected System ID
265
    return systemid;
266
  }
267
268 1358 tao
  /**
269
   * Register new DTD identified by @systemId in Metacat XML Catalog
270 598 bojilova
   * . make a reference with @systemId for @doctype in Metacat DB
271
   */
272
  private void registerDTD ( String doctype, String systemId )
273
                 throws SAXException
274
  {
275 4803 leinfelder
	  String existingSystemId = getDTDSystemID(doctype);
276
	  if (existingSystemId != null && existingSystemId.equals(systemId)) {
277
		  logMetacat.warn("doctype/systemId already registered in DB: " + doctype);
278
		  return;
279
	  }
280 1217 tao
    //DBConnection conn = null;
281
    //int serialNumber = -1;
282
    PreparedStatement pstmt = null;
283 598 bojilova
    // make a reference in db catalog table with @systemId for @doctype
284
    try {
285 1217 tao
      //check out DBConnection
286
      //conn=DBConnectionPool.getDBConnection("DBEntityResolver.registerDTD");
287
      //serialNumber=conn.getCheckOutSerialNumber();
288 1358 tao
289
290 1217 tao
      pstmt = connection.prepareStatement(
291 243 jones
             "INSERT INTO xml_catalog " +
292 4800 leinfelder
             "(entry_type, public_id, system_id) " +
293
             "VALUES ('DTD', ?, ?)");
294 1217 tao
      // Increase usage count
295
      connection.increaseUsageCount(1);
296 598 bojilova
      // Bind the values to the query
297
      pstmt.setString(1, doctype);
298
      pstmt.setString(2, systemId);
299
      // Do the insertion
300
      pstmt.execute();
301 4803 leinfelder
      int updateCnt = pstmt.getUpdateCount();
302
      logMetacat.debug("DBEntityReolver.registerDTD: DTDs registered: " + updateCnt);
303 598 bojilova
      pstmt.close();
304
    } catch (SQLException e) {
305
      throw new SAXException
306 4967 daigle
      ("DBEntityResolver.registerDTD - SQL issue when registering DTD: " + e.getMessage());
307 598 bojilova
    }
308 1217 tao
    finally
309
    {
310
      try
311
      {
312
        pstmt.close();
313
      }//try
314
      catch (SQLException sqlE)
315
      {
316 4967 daigle
        logMetacat.error("SQL error in DBEntityReolver.registerDTD: "
317 2663 sgarg
                                    +sqlE.getMessage());
318 1217 tao
      }//catch
319
      //DBConnectionPool.returnDBConnection(conn, serialNumber);
320
    }//finally
321 1358 tao
322 598 bojilova
  }
323 243 jones
324 1358 tao
  /**
325 4080 daigle
	 * Upload new DTD text identified by
326
	 *
327
	 * @systemId to Metacat file system
328
	 */
329
	private String uploadDTD(String systemId) throws SAXException {
330
		String dtdPath = null;
331
		String dtdURL = null;
332
		try {
333
			dtdPath = SystemUtil.getContextDir() + "/dtd/";
334
			dtdURL = SystemUtil.getContextURL() + "/dtd/";
335
		} catch (PropertyNotFoundException pnfe) {
336
			throw new SAXException("DBEntityResolver.uploadDTD: " + pnfe.getMessage());
337
		}
338 1358 tao
339 4080 daigle
		// get filename from systemId
340
		String filename = systemId;
341
		int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
342
		if (slash > -1) {
343
			filename = filename.substring(slash + 1);
344
		}
345 245 jones
346 4080 daigle
		// writing dtd text on Metacat file system as filename
347
		try {
348
			// create a buffering character-input stream
349
			// that uses a default-sized input buffer
350
			BufferedReader in = new BufferedReader(dtdtext);
351 245 jones
352 4080 daigle
			// open file writer to write the input into it
353
			// String dtdPath = "/opt/tomcat/webapps/bojilova/dtd/";
354
			File f = new File(dtdPath, filename);
355
			synchronized (f) {
356
				try {
357
					if (f.exists()) {
358
						throw new IOException("File already exist: "
359
								+ f.getCanonicalFile());
360
						// if ( f.exists() && !f.canWrite() ) {
361
						// throw new IOException("Not writable: " +
362
						// f.getCanonicalFile());
363
					}
364
				} catch (SecurityException se) {
365
					// if a security manager exists,
366
					// its checkRead method is called for f.exist()
367
					// or checkWrite method is called for f.canWrite()
368
					throw se;
369
				}
370
				// create a buffered character-output stream
371
				// that uses a default-sized output buffer
372
				FileWriter fw = new FileWriter(f);
373
				BufferedWriter out = new BufferedWriter(fw);
374 598 bojilova
375 4080 daigle
				// read the input and write into the file writer
376
				String inputLine;
377
				while ((inputLine = in.readLine()) != null) {
378
					out.write(inputLine, 0, inputLine.length());
379
					out.newLine(); // instead of out.write('\r\n');
380
				}
381 598 bojilova
382 4080 daigle
				// the input and the output streams must be closed
383
				in.close();
384
				out.flush();
385
				out.close();
386
				fw.close();
387
			} // end of synchronized
388
		} catch (MalformedURLException e) {
389 4967 daigle
			throw new SAXException("DBEntityResolver.uploadDTD() - Malformed URL when uploading DTD: " + e.getMessage());
390 4080 daigle
		} catch (IOException e) {
391 4967 daigle
			throw new SAXException("DBEntityResolver.uploadDTD - I/O issue when uploading DTD: " + e.getMessage());
392 4080 daigle
		} catch (SecurityException e) {
393 4967 daigle
			throw new SAXException("DBEntityResolver.uploadDTD() - Security issue when uploading DTD: " + e.getMessage());
394 4080 daigle
		}
395 1358 tao
396 4080 daigle
		// String dtdURL = "http://dev.nceas.ucsb.edu/bojilova/dtd/";
397
		return dtdURL + filename;
398
	}
399 598 bojilova
400 694 bojilova
401 1358 tao
  /**
402 4080 daigle
	 * Upload new DTD located at outside URL to Metacat file system
403
	 */
404
	private String uploadDTDFromURL(InputStream istream, String systemId)
405
			throws SAXException {
406
		String dtdPath = null;
407
		String dtdURL = null;
408
		try {
409
			dtdPath = SystemUtil.getContextDir() + "/dtd/";
410
			dtdURL = SystemUtil.getContextURL() + "/dtd/";
411
		} catch (PropertyNotFoundException pnfe) {
412 4967 daigle
			throw new SAXException("DBEntityResolver.uploadDTDFromURL - Property issue when uploading DTD from URL: "
413 4080 daigle
					+ pnfe.getMessage());
414
		}
415 1358 tao
416 4080 daigle
		// get filename from systemId
417
		String filename = systemId;
418
		int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
419
		if (slash > -1) {
420
			filename = filename.substring(slash + 1);
421
		}
422 694 bojilova
423 4080 daigle
		// writing dtd text on Metacat file system as filename
424
		try {
425
			// create a buffering character-input stream
426
			// that uses a default-sized input buffer
427
			BufferedInputStream in = new BufferedInputStream(istream);
428 694 bojilova
429 4080 daigle
			// open file writer to write the input into it
430
			//String dtdPath = "/opt/tomcat/webapps/bojilova/dtd/";
431
			File f = new File(dtdPath, filename);
432
			synchronized (f) {
433
				try {
434
					if (f.exists()) {
435 4803 leinfelder
						logMetacat.warn("File already exist, overwriting: "
436 4080 daigle
								+ f.getCanonicalFile());
437 4803 leinfelder
						//return dtdURL + filename;
438
						//throw new IOException("File already exist: "
439
						//		+ f.getCanonicalFile());
440 4080 daigle
						//if ( f.exists() && !f.canWrite() ) {
441
						//  throw new IOException("Not writable: " + f.getCanonicalFile());
442
					}
443
				} catch (SecurityException se) {
444
					// if a security manager exists,
445
					// its checkRead method is called for f.exist()
446
					// or checkWrite method is called for f.canWrite()
447
					throw se;
448
				}
449
				// create a buffered character-output stream
450
				// that uses a default-sized output buffer
451
				FileWriter fw = new FileWriter(f);
452
				BufferedWriter out = new BufferedWriter(fw);
453 694 bojilova
454 4080 daigle
				// read the input and write into the file writer
455
				int inputByte;
456
				while ((inputByte = in.read()) != -1) {
457
					out.write(inputByte);
458
					//out.newLine(); //instead of out.write('\r\n');
459
				}
460 694 bojilova
461 4080 daigle
				// the input and the output streams must be closed
462
				in.close();
463
				out.flush();
464
				out.close();
465
				fw.close();
466
			} // end of synchronized
467
		} catch (MalformedURLException e) {
468 4967 daigle
			throw new SAXException("DBEntityResolver.uploadDTDFromURL - Malformed URL when uploading DTD from URL: "
469 4080 daigle
					+ e.getMessage());
470
		} catch (IOException e) {
471 4967 daigle
			throw new SAXException("DBEntityResolver.uploadDTDFromURL - I/O issue when uploading DTD from URL:  "
472 4080 daigle
					+ e.getMessage());
473
		} catch (SecurityException e) {
474 4967 daigle
			throw new SAXException("DBEntityResolver.uploadDTDFromURL - Security issue when uploading DTD from URL:  "
475 4080 daigle
					+ e.getMessage());
476
		}
477 1358 tao
478 4080 daigle
		//String dtdURL = "http://dev.nceas.ucsb.edu/bojilova/dtd/";
479
		return dtdURL + filename;
480
	}
481 694 bojilova
482 4080 daigle
	/**
483
	 * Check URL Connection for @systemId, and return an InputStream
484
	 * that can be used to read from the systemId URL.  The parser ends
485
	 * up using this via the InputSource to read the DTD.
486
	 *
487
	 * @param systemId a URI (in practice URL) to be checked and opened
488
	 */
489
	public static InputStream checkURLConnection(String systemId) throws SAXException {
490
		try {
491
			return (new URL(systemId).openStream());
492 598 bojilova
493 4080 daigle
		} catch (MalformedURLException e) {
494 4967 daigle
			throw new SAXException("DBEntityResolver.checkURLConnection - Malformed URL when checking URL Connection: "
495 4080 daigle
					+ e.getMessage());
496
		} catch (IOException e) {
497 4967 daigle
			throw new SAXException("DBEntityResolver.checkURLConnection - I/O issue when checking URL Connection: "
498 4080 daigle
					+ e.getMessage());
499
		}
500
	}
501 72 bojilova
}