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: daigle $'
10
 *     '$Date: 2009-07-01 10:29:18 -0700 (Wed, 01 Jul 2009) $'
11
 * '$Revision: 4967 $'
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.util.SystemUtil;
35
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
36

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

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

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

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

    
133
    // get System ID for doctype
134
    if (doctype != null) {
135
      // look at db XML Catalog for System ID
136
      logMetacat.info("get systemId from doctype: "+doctype);
137
      dbSystemID = getDTDSystemID(doctype);
138
      logMetacat.info("The Systemid is: "+dbSystemID);
139
      // check that it is accessible on our system before getting too far
140
      try {
141
    	  InputStream in = checkURLConnection(dbSystemID);
142
	  }
143
      catch (Exception e) {
144
    	  dbSystemID = null;
145
	  }
146
      boolean doctypeIsInDB = true;
147
      // no System ID found in db XML Catalog
148
      if (dbSystemID == null) {
149
        doctypeIsInDB = false;
150
        // use the provided System ID
151
        if (systemId != null) {
152
          dbSystemID = systemId;
153
        }
154
        logMetacat.info("If above Systemid is null and then get "
155
                                 +" system id from file" + dbSystemID);
156
      }
157
      // there are dtd text provided; try to upload on Metacat
158
      if ( dtdtext != null ) {
159
        dbSystemID = uploadDTD(dbSystemID);
160
      }
161

    
162
      // open URLConnection to check first
163
      InputStream istream = checkURLConnection(dbSystemID);
164

    
165
      // need to register System ID in db XML Catalog if not yet
166
      if ( !doctypeIsInDB ) {
167
        // new DTD from outside URL location; try to upload on Metacat
168
        if ( dtdtext == null ) {
169
          dbSystemID = uploadDTDFromURL(istream, dbSystemID);
170
        }
171
        registerDTD(doctype, dbSystemID);
172
      }
173
      // return a byte-input stream for use
174
      InputSource is = new InputSource(dbSystemID);
175

    
176
      // close and open URLConnection again
177
      try {
178
        istream.close();
179
      } catch (IOException e) {
180
        throw new SAXException
181
        ("DBEntityResolver.resolveEntity - I/O issue when resolving entity: " + e.getMessage());
182
      }
183
      istream = checkURLConnection(dbSystemID);
184
      is.setByteStream(istream);
185
      return is;
186
    } else {
187
      // use provided systemId for the other cases
188
      logMetacat.info("doctype is null and using system id from file");
189
      InputStream istream = checkURLConnection(systemId);
190
      return null;
191

    
192
    }
193

    
194
  }
195

    
196
  /**
197
   * Look at db XML Catalog to get System ID (if any) for @doctype.
198
   * Return null if there are no System ID found for @doctype
199
   */
200
  public static String getDTDSystemID( String doctype )
201
                 throws SAXException
202
  {
203
    String systemid = null;
204
    Statement stmt = null;
205
    DBConnection conn = null;
206
    int serialNumber = -1;
207
    try {
208
      //check out DBConnection
209
      conn=DBConnectionPool.getDBConnection("DBEntityResolver.getDTDSystemID");
210
      serialNumber=conn.getCheckOutSerialNumber();
211

    
212
      stmt = conn.createStatement();
213
      stmt.execute("SELECT system_id FROM xml_catalog " +
214
                   "WHERE entry_type = 'DTD' AND public_id = '" +
215
                   doctype + "'");
216
      ResultSet rs = stmt.getResultSet();
217
      boolean tableHasRows = rs.next();
218
      if (tableHasRows) {
219
        systemid = rs.getString(1);
220
        // system id may not have server url on front.  Add it if not.
221
        if (!systemid.startsWith("http://")) {
222
        	systemid = SystemUtil.getContextURL() + systemid;
223
        }
224
      }
225
      stmt.close();
226
    } catch (SQLException e) {
227
      throw new SAXException
228
      ("DBEntityResolver.getDTDSystemID - SQL error when getting DTD system ID: " + e.getMessage());
229
    } catch (PropertyNotFoundException pnfe) {
230
        throw new SAXException
231
        ("DBEntityResolver.getDTDSystemID - Property error when getting DTD system ID:  " + pnfe.getMessage());
232
      }
233
    finally
234
    {
235
      try
236
      {
237
        stmt.close();
238
      }//try
239
      catch (SQLException sqlE)
240
      {
241
        logMetacat.error("SQL error in DBEntityReolver.getDTDSystemId: "
242
                                  +sqlE.getMessage());
243
      }//catch
244
      finally
245
      {
246
        DBConnectionPool.returnDBConnection(conn, serialNumber);
247
      }//finally
248
    }//finally
249

    
250
    // return the selected System ID
251
    return systemid;
252
  }
253

    
254
  /**
255
   * Register new DTD identified by @systemId in Metacat XML Catalog
256
   * . make a reference with @systemId for @doctype in Metacat DB
257
   */
258
  private void registerDTD ( String doctype, String systemId )
259
                 throws SAXException
260
  {
261
	  String existingSystemId = getDTDSystemID(doctype);
262
	  if (existingSystemId != null && existingSystemId.equals(systemId)) {
263
		  logMetacat.warn("doctype/systemId already registered in DB: " + doctype);
264
		  return;
265
	  }
266
    //DBConnection conn = null;
267
    //int serialNumber = -1;
268
    PreparedStatement pstmt = null;
269
    // make a reference in db catalog table with @systemId for @doctype
270
    try {
271
      //check out DBConnection
272
      //conn=DBConnectionPool.getDBConnection("DBEntityResolver.registerDTD");
273
      //serialNumber=conn.getCheckOutSerialNumber();
274

    
275

    
276
      pstmt = connection.prepareStatement(
277
             "INSERT INTO xml_catalog " +
278
             "(entry_type, public_id, system_id) " +
279
             "VALUES ('DTD', ?, ?)");
280
      // Increase usage count
281
      connection.increaseUsageCount(1);
282
      // Bind the values to the query
283
      pstmt.setString(1, doctype);
284
      pstmt.setString(2, systemId);
285
      // Do the insertion
286
      pstmt.execute();
287
      int updateCnt = pstmt.getUpdateCount();
288
      logMetacat.debug("DBEntityReolver.registerDTD: DTDs registered: " + updateCnt);
289
      pstmt.close();
290
    } catch (SQLException e) {
291
      throw new SAXException
292
      ("DBEntityResolver.registerDTD - SQL issue when registering DTD: " + e.getMessage());
293
    }
294
    finally
295
    {
296
      try
297
      {
298
        pstmt.close();
299
      }//try
300
      catch (SQLException sqlE)
301
      {
302
        logMetacat.error("SQL error in DBEntityReolver.registerDTD: "
303
                                    +sqlE.getMessage());
304
      }//catch
305
      //DBConnectionPool.returnDBConnection(conn, serialNumber);
306
    }//finally
307

    
308
  }
309

    
310
  /**
311
	 * Upload new DTD text identified by
312
	 * 
313
	 * @systemId to Metacat file system
314
	 */
315
	private String uploadDTD(String systemId) throws SAXException {
316
		String dtdPath = null;
317
		String dtdURL = null;
318
		try {
319
			dtdPath = SystemUtil.getContextDir() + "/dtd/";
320
			dtdURL = SystemUtil.getContextURL() + "/dtd/";
321
		} catch (PropertyNotFoundException pnfe) {
322
			throw new SAXException("DBEntityResolver.uploadDTD: " + pnfe.getMessage());
323
		}
324

    
325
		// get filename from systemId
326
		String filename = systemId;
327
		int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
328
		if (slash > -1) {
329
			filename = filename.substring(slash + 1);
330
		}
331

    
332
		// writing dtd text on Metacat file system as filename
333
		try {
334
			// create a buffering character-input stream
335
			// that uses a default-sized input buffer
336
			BufferedReader in = new BufferedReader(dtdtext);
337

    
338
			// open file writer to write the input into it
339
			// String dtdPath = "/opt/tomcat/webapps/bojilova/dtd/";
340
			File f = new File(dtdPath, filename);
341
			synchronized (f) {
342
				try {
343
					if (f.exists()) {
344
						throw new IOException("File already exist: "
345
								+ f.getCanonicalFile());
346
						// if ( f.exists() && !f.canWrite() ) {
347
						// throw new IOException("Not writable: " +
348
						// f.getCanonicalFile());
349
					}
350
				} catch (SecurityException se) {
351
					// if a security manager exists,
352
					// its checkRead method is called for f.exist()
353
					// or checkWrite method is called for f.canWrite()
354
					throw se;
355
				}
356
				// create a buffered character-output stream
357
				// that uses a default-sized output buffer
358
				FileWriter fw = new FileWriter(f);
359
				BufferedWriter out = new BufferedWriter(fw);
360

    
361
				// read the input and write into the file writer
362
				String inputLine;
363
				while ((inputLine = in.readLine()) != null) {
364
					out.write(inputLine, 0, inputLine.length());
365
					out.newLine(); // instead of out.write('\r\n');
366
				}
367

    
368
				// the input and the output streams must be closed
369
				in.close();
370
				out.flush();
371
				out.close();
372
				fw.close();
373
			} // end of synchronized
374
		} catch (MalformedURLException e) {
375
			throw new SAXException("DBEntityResolver.uploadDTD() - Malformed URL when uploading DTD: " + e.getMessage());
376
		} catch (IOException e) {
377
			throw new SAXException("DBEntityResolver.uploadDTD - I/O issue when uploading DTD: " + e.getMessage());
378
		} catch (SecurityException e) {
379
			throw new SAXException("DBEntityResolver.uploadDTD() - Security issue when uploading DTD: " + e.getMessage());
380
		}
381

    
382
		// String dtdURL = "http://dev.nceas.ucsb.edu/bojilova/dtd/";
383
		return dtdURL + filename;
384
	}
385

    
386

    
387
  /**
388
	 * Upload new DTD located at outside URL to Metacat file system
389
	 */
390
	private String uploadDTDFromURL(InputStream istream, String systemId)
391
			throws SAXException {
392
		String dtdPath = null;
393
		String dtdURL = null;
394
		try {
395
			dtdPath = SystemUtil.getContextDir() + "/dtd/";
396
			dtdURL = SystemUtil.getContextURL() + "/dtd/";
397
		} catch (PropertyNotFoundException pnfe) {
398
			throw new SAXException("DBEntityResolver.uploadDTDFromURL - Property issue when uploading DTD from URL: "
399
					+ pnfe.getMessage());
400
		}
401

    
402
		// get filename from systemId
403
		String filename = systemId;
404
		int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
405
		if (slash > -1) {
406
			filename = filename.substring(slash + 1);
407
		}
408

    
409
		// writing dtd text on Metacat file system as filename
410
		try {
411
			// create a buffering character-input stream
412
			// that uses a default-sized input buffer
413
			BufferedInputStream in = new BufferedInputStream(istream);
414

    
415
			// open file writer to write the input into it
416
			//String dtdPath = "/opt/tomcat/webapps/bojilova/dtd/";
417
			File f = new File(dtdPath, filename);
418
			synchronized (f) {
419
				try {
420
					if (f.exists()) {
421
						logMetacat.warn("File already exist, overwriting: "
422
								+ f.getCanonicalFile());
423
						//return dtdURL + filename;
424
						//throw new IOException("File already exist: "
425
						//		+ f.getCanonicalFile());
426
						//if ( f.exists() && !f.canWrite() ) {
427
						//  throw new IOException("Not writable: " + f.getCanonicalFile());
428
					}
429
				} catch (SecurityException se) {
430
					// if a security manager exists,
431
					// its checkRead method is called for f.exist()
432
					// or checkWrite method is called for f.canWrite()
433
					throw se;
434
				}
435
				// create a buffered character-output stream
436
				// that uses a default-sized output buffer
437
				FileWriter fw = new FileWriter(f);
438
				BufferedWriter out = new BufferedWriter(fw);
439

    
440
				// read the input and write into the file writer
441
				int inputByte;
442
				while ((inputByte = in.read()) != -1) {
443
					out.write(inputByte);
444
					//out.newLine(); //instead of out.write('\r\n');
445
				}
446

    
447
				// the input and the output streams must be closed
448
				in.close();
449
				out.flush();
450
				out.close();
451
				fw.close();
452
			} // end of synchronized
453
		} catch (MalformedURLException e) {
454
			throw new SAXException("DBEntityResolver.uploadDTDFromURL - Malformed URL when uploading DTD from URL: "
455
					+ e.getMessage());
456
		} catch (IOException e) {
457
			throw new SAXException("DBEntityResolver.uploadDTDFromURL - I/O issue when uploading DTD from URL:  "
458
					+ e.getMessage());
459
		} catch (SecurityException e) {
460
			throw new SAXException("DBEntityResolver.uploadDTDFromURL - Security issue when uploading DTD from URL:  "
461
					+ e.getMessage());
462
		}
463

    
464
		//String dtdURL = "http://dev.nceas.ucsb.edu/bojilova/dtd/";
465
		return dtdURL + filename;
466
	}
467

    
468
	/**
469
	 * Check URL Connection for @systemId, and return an InputStream
470
	 * that can be used to read from the systemId URL.  The parser ends
471
	 * up using this via the InputSource to read the DTD.
472
	 *
473
	 * @param systemId a URI (in practice URL) to be checked and opened
474
	 */
475
	public static InputStream checkURLConnection(String systemId) throws SAXException {
476
		try {
477
			return (new URL(systemId).openStream());
478

    
479
		} catch (MalformedURLException e) {
480
			throw new SAXException("DBEntityResolver.checkURLConnection - Malformed URL when checking URL Connection: "
481
					+ e.getMessage());
482
		} catch (IOException e) {
483
			throw new SAXException("DBEntityResolver.checkURLConnection - I/O issue when checking URL Connection: "
484
					+ e.getMessage());
485
		}
486
	}
487
}
(21-21/72)