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 |
5177
|
daigle
|
logMetacat.debug("DBEntityResolver.resolveEntity - in 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 |
5177
|
daigle
|
logMetacat.debug("DBEntityResolver.resolveEntity - 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 |
5177
|
daigle
|
logMetacat.info("DBEntityResolver.resolveEntity - get systemId from doctype: " + doctype);
|
140 |
598
|
bojilova
|
dbSystemID = getDTDSystemID(doctype);
|
141 |
5177
|
daigle
|
logMetacat.info("DBEntityResolver.resolveEntity - 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 |
5177
|
daigle
|
} catch (SAXException se) {
|
146 |
4978
|
daigle
|
// 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 |
5177
|
daigle
|
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 |
4978
|
daigle
|
InputStream istream = checkURLConnection(systemId);
|
152 |
|
|
uploadDTDFromURL(istream, systemId);
|
153 |
|
|
try {
|
154 |
5177
|
daigle
|
Thread.currentThread().sleep(6000);
|
155 |
4978
|
daigle
|
checkURLConnection(dbSystemID);
|
156 |
|
|
} catch (Exception e2) {
|
157 |
5177
|
daigle
|
logMetacat.error("DBEntityResolver.resolveEntity - still could not find dtd for doc type " + doctype + " at "
|
158 |
4978
|
daigle
|
+ dbSystemID + " : " + e2.getMessage());
|
159 |
|
|
dbSystemID = null;
|
160 |
|
|
}
|
161 |
5177
|
daigle
|
}
|
162 |
598
|
bojilova
|
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 |
5177
|
daigle
|
logMetacat.info("DBEntityResolver.resolveEntity - If above Systemid is null, then get "
|
171 |
|
|
+ "system id from file: " + dbSystemID);
|
172 |
598
|
bojilova
|
}
|
173 |
|
|
// there are dtd text provided; try to upload on Metacat
|
174 |
|
|
if ( dtdtext != null ) {
|
175 |
|
|
dbSystemID = uploadDTD(dbSystemID);
|
176 |
|
|
}
|
177 |
|
|
|
178 |
694
|
bojilova
|
// open URLConnection to check first
|
179 |
598
|
bojilova
|
InputStream istream = checkURLConnection(dbSystemID);
|
180 |
|
|
|
181 |
|
|
// need to register System ID in db XML Catalog if not yet
|
182 |
|
|
if ( !doctypeIsInDB ) {
|
183 |
694
|
bojilova
|
// new DTD from outside URL location; try to upload on Metacat
|
184 |
|
|
if ( dtdtext == null ) {
|
185 |
|
|
dbSystemID = uploadDTDFromURL(istream, dbSystemID);
|
186 |
|
|
}
|
187 |
598
|
bojilova
|
registerDTD(doctype, dbSystemID);
|
188 |
|
|
}
|
189 |
|
|
// return a byte-input stream for use
|
190 |
1358
|
tao
|
InputSource is = new InputSource(dbSystemID);
|
191 |
694
|
bojilova
|
|
192 |
|
|
// close and open URLConnection again
|
193 |
|
|
try {
|
194 |
|
|
istream.close();
|
195 |
|
|
} catch (IOException e) {
|
196 |
1358
|
tao
|
throw new SAXException
|
197 |
4967
|
daigle
|
("DBEntityResolver.resolveEntity - I/O issue when resolving entity: " + e.getMessage());
|
198 |
1358
|
tao
|
}
|
199 |
694
|
bojilova
|
istream = checkURLConnection(dbSystemID);
|
200 |
598
|
bojilova
|
is.setByteStream(istream);
|
201 |
|
|
return is;
|
202 |
|
|
} else {
|
203 |
694
|
bojilova
|
// use provided systemId for the other cases
|
204 |
5177
|
daigle
|
logMetacat.info("DBEntityResolver.resolveEntity - doctype is null and using system id from file");
|
205 |
598
|
bojilova
|
InputStream istream = checkURLConnection(systemId);
|
206 |
|
|
return null;
|
207 |
1358
|
tao
|
|
208 |
598
|
bojilova
|
}
|
209 |
1358
|
tao
|
|
210 |
598
|
bojilova
|
}
|
211 |
|
|
|
212 |
1358
|
tao
|
/**
|
213 |
598
|
bojilova
|
* 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 |
1895
|
tao
|
public static String getDTDSystemID( String doctype )
|
217 |
598
|
bojilova
|
throws SAXException
|
218 |
|
|
{
|
219 |
|
|
String systemid = null;
|
220 |
6606
|
leinfelder
|
PreparedStatement pstmt = null;
|
221 |
1217
|
tao
|
DBConnection conn = null;
|
222 |
|
|
int serialNumber = -1;
|
223 |
598
|
bojilova
|
try {
|
224 |
1217
|
tao
|
//check out DBConnection
|
225 |
|
|
conn=DBConnectionPool.getDBConnection("DBEntityResolver.getDTDSystemID");
|
226 |
|
|
serialNumber=conn.getCheckOutSerialNumber();
|
227 |
1358
|
tao
|
|
228 |
6606
|
leinfelder
|
String sql = "SELECT system_id FROM xml_catalog " +
|
229 |
|
|
"WHERE entry_type = 'DTD' AND public_id = ?";
|
230 |
|
|
|
231 |
|
|
pstmt = conn.prepareStatement(sql);
|
232 |
|
|
pstmt.setString(1, doctype);
|
233 |
|
|
|
234 |
|
|
pstmt.execute();
|
235 |
|
|
ResultSet rs = pstmt.getResultSet();
|
236 |
598
|
bojilova
|
boolean tableHasRows = rs.next();
|
237 |
|
|
if (tableHasRows) {
|
238 |
|
|
systemid = rs.getString(1);
|
239 |
4080
|
daigle
|
// system id may not have server url on front. Add it if not.
|
240 |
|
|
if (!systemid.startsWith("http://")) {
|
241 |
4123
|
daigle
|
systemid = SystemUtil.getContextURL() + systemid;
|
242 |
4080
|
daigle
|
}
|
243 |
598
|
bojilova
|
}
|
244 |
6606
|
leinfelder
|
pstmt.close();
|
245 |
598
|
bojilova
|
} catch (SQLException e) {
|
246 |
|
|
throw new SAXException
|
247 |
4967
|
daigle
|
("DBEntityResolver.getDTDSystemID - SQL error when getting DTD system ID: " + e.getMessage());
|
248 |
4080
|
daigle
|
} catch (PropertyNotFoundException pnfe) {
|
249 |
|
|
throw new SAXException
|
250 |
4967
|
daigle
|
("DBEntityResolver.getDTDSystemID - Property error when getting DTD system ID: " + pnfe.getMessage());
|
251 |
4080
|
daigle
|
}
|
252 |
1217
|
tao
|
finally
|
253 |
|
|
{
|
254 |
|
|
try
|
255 |
|
|
{
|
256 |
6606
|
leinfelder
|
pstmt.close();
|
257 |
1217
|
tao
|
}//try
|
258 |
|
|
catch (SQLException sqlE)
|
259 |
|
|
{
|
260 |
5177
|
daigle
|
logMetacat.error("DBEntityResolver.getDTDSystemId - SQL error: " + sqlE.getMessage());
|
261 |
1217
|
tao
|
}//catch
|
262 |
|
|
finally
|
263 |
|
|
{
|
264 |
|
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
265 |
|
|
}//finally
|
266 |
|
|
}//finally
|
267 |
598
|
bojilova
|
|
268 |
|
|
// return the selected System ID
|
269 |
|
|
return systemid;
|
270 |
|
|
}
|
271 |
|
|
|
272 |
1358
|
tao
|
/**
|
273 |
|
|
* Register new DTD identified by @systemId in Metacat XML Catalog
|
274 |
598
|
bojilova
|
* . make a reference with @systemId for @doctype in Metacat DB
|
275 |
|
|
*/
|
276 |
|
|
private void registerDTD ( String doctype, String systemId )
|
277 |
|
|
throws SAXException
|
278 |
|
|
{
|
279 |
4803
|
leinfelder
|
String existingSystemId = getDTDSystemID(doctype);
|
280 |
|
|
if (existingSystemId != null && existingSystemId.equals(systemId)) {
|
281 |
5177
|
daigle
|
logMetacat.warn("DBEntityResolver.registerDTD - doctype/systemId already registered in DB: " + doctype);
|
282 |
4803
|
leinfelder
|
return;
|
283 |
|
|
}
|
284 |
1217
|
tao
|
//DBConnection conn = null;
|
285 |
|
|
//int serialNumber = -1;
|
286 |
|
|
PreparedStatement pstmt = null;
|
287 |
598
|
bojilova
|
// make a reference in db catalog table with @systemId for @doctype
|
288 |
|
|
try {
|
289 |
1217
|
tao
|
//check out DBConnection
|
290 |
|
|
//conn=DBConnectionPool.getDBConnection("DBEntityResolver.registerDTD");
|
291 |
|
|
//serialNumber=conn.getCheckOutSerialNumber();
|
292 |
1358
|
tao
|
|
293 |
|
|
|
294 |
1217
|
tao
|
pstmt = connection.prepareStatement(
|
295 |
243
|
jones
|
"INSERT INTO xml_catalog " +
|
296 |
4800
|
leinfelder
|
"(entry_type, public_id, system_id) " +
|
297 |
|
|
"VALUES ('DTD', ?, ?)");
|
298 |
1217
|
tao
|
// Increase usage count
|
299 |
|
|
connection.increaseUsageCount(1);
|
300 |
598
|
bojilova
|
// Bind the values to the query
|
301 |
|
|
pstmt.setString(1, doctype);
|
302 |
|
|
pstmt.setString(2, systemId);
|
303 |
|
|
// Do the insertion
|
304 |
|
|
pstmt.execute();
|
305 |
4803
|
leinfelder
|
int updateCnt = pstmt.getUpdateCount();
|
306 |
5177
|
daigle
|
logMetacat.debug("DBEntityReolver.registerDTD - DTDs registered: " + updateCnt);
|
307 |
598
|
bojilova
|
pstmt.close();
|
308 |
|
|
} catch (SQLException e) {
|
309 |
|
|
throw new SAXException
|
310 |
4967
|
daigle
|
("DBEntityResolver.registerDTD - SQL issue when registering DTD: " + e.getMessage());
|
311 |
598
|
bojilova
|
}
|
312 |
1217
|
tao
|
finally
|
313 |
|
|
{
|
314 |
|
|
try
|
315 |
|
|
{
|
316 |
|
|
pstmt.close();
|
317 |
|
|
}//try
|
318 |
|
|
catch (SQLException sqlE)
|
319 |
|
|
{
|
320 |
5177
|
daigle
|
logMetacat.error("DBEntityResolver.registerDTD - SQL error: " + sqlE.getMessage());
|
321 |
1217
|
tao
|
}//catch
|
322 |
|
|
//DBConnectionPool.returnDBConnection(conn, serialNumber);
|
323 |
|
|
}//finally
|
324 |
1358
|
tao
|
|
325 |
598
|
bojilova
|
}
|
326 |
243
|
jones
|
|
327 |
1358
|
tao
|
/**
|
328 |
4080
|
daigle
|
* Upload new DTD text identified by
|
329 |
|
|
*
|
330 |
|
|
* @systemId to Metacat file system
|
331 |
|
|
*/
|
332 |
|
|
private String uploadDTD(String systemId) throws SAXException {
|
333 |
|
|
String dtdPath = null;
|
334 |
|
|
String dtdURL = null;
|
335 |
|
|
try {
|
336 |
|
|
dtdPath = SystemUtil.getContextDir() + "/dtd/";
|
337 |
|
|
dtdURL = SystemUtil.getContextURL() + "/dtd/";
|
338 |
|
|
} catch (PropertyNotFoundException pnfe) {
|
339 |
|
|
throw new SAXException("DBEntityResolver.uploadDTD: " + pnfe.getMessage());
|
340 |
|
|
}
|
341 |
1358
|
tao
|
|
342 |
4080
|
daigle
|
// get filename from systemId
|
343 |
|
|
String filename = systemId;
|
344 |
|
|
int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
|
345 |
|
|
if (slash > -1) {
|
346 |
|
|
filename = filename.substring(slash + 1);
|
347 |
|
|
}
|
348 |
245
|
jones
|
|
349 |
4080
|
daigle
|
// writing dtd text on Metacat file system as filename
|
350 |
|
|
try {
|
351 |
|
|
// create a buffering character-input stream
|
352 |
|
|
// that uses a default-sized input buffer
|
353 |
|
|
BufferedReader in = new BufferedReader(dtdtext);
|
354 |
245
|
jones
|
|
355 |
4080
|
daigle
|
// open file writer to write the input into it
|
356 |
|
|
// String dtdPath = "/opt/tomcat/webapps/bojilova/dtd/";
|
357 |
|
|
File f = new File(dtdPath, filename);
|
358 |
|
|
synchronized (f) {
|
359 |
|
|
try {
|
360 |
|
|
if (f.exists()) {
|
361 |
|
|
throw new IOException("File already exist: "
|
362 |
|
|
+ f.getCanonicalFile());
|
363 |
|
|
// if ( f.exists() && !f.canWrite() ) {
|
364 |
|
|
// throw new IOException("Not writable: " +
|
365 |
|
|
// f.getCanonicalFile());
|
366 |
|
|
}
|
367 |
|
|
} catch (SecurityException se) {
|
368 |
|
|
// if a security manager exists,
|
369 |
|
|
// its checkRead method is called for f.exist()
|
370 |
|
|
// or checkWrite method is called for f.canWrite()
|
371 |
|
|
throw se;
|
372 |
|
|
}
|
373 |
|
|
// create a buffered character-output stream
|
374 |
|
|
// that uses a default-sized output buffer
|
375 |
|
|
FileWriter fw = new FileWriter(f);
|
376 |
|
|
BufferedWriter out = new BufferedWriter(fw);
|
377 |
598
|
bojilova
|
|
378 |
4080
|
daigle
|
// read the input and write into the file writer
|
379 |
|
|
String inputLine;
|
380 |
|
|
while ((inputLine = in.readLine()) != null) {
|
381 |
|
|
out.write(inputLine, 0, inputLine.length());
|
382 |
|
|
out.newLine(); // instead of out.write('\r\n');
|
383 |
|
|
}
|
384 |
598
|
bojilova
|
|
385 |
4080
|
daigle
|
// the input and the output streams must be closed
|
386 |
|
|
in.close();
|
387 |
|
|
out.flush();
|
388 |
|
|
out.close();
|
389 |
|
|
fw.close();
|
390 |
|
|
} // end of synchronized
|
391 |
|
|
} catch (MalformedURLException e) {
|
392 |
4967
|
daigle
|
throw new SAXException("DBEntityResolver.uploadDTD() - Malformed URL when uploading DTD: " + e.getMessage());
|
393 |
4080
|
daigle
|
} catch (IOException e) {
|
394 |
4967
|
daigle
|
throw new SAXException("DBEntityResolver.uploadDTD - I/O issue when uploading DTD: " + e.getMessage());
|
395 |
4080
|
daigle
|
} catch (SecurityException e) {
|
396 |
4967
|
daigle
|
throw new SAXException("DBEntityResolver.uploadDTD() - Security issue when uploading DTD: " + e.getMessage());
|
397 |
4080
|
daigle
|
}
|
398 |
1358
|
tao
|
|
399 |
4080
|
daigle
|
// String dtdURL = "http://dev.nceas.ucsb.edu/bojilova/dtd/";
|
400 |
|
|
return dtdURL + filename;
|
401 |
|
|
}
|
402 |
598
|
bojilova
|
|
403 |
694
|
bojilova
|
|
404 |
1358
|
tao
|
/**
|
405 |
4080
|
daigle
|
* Upload new DTD located at outside URL to Metacat file system
|
406 |
|
|
*/
|
407 |
|
|
private String uploadDTDFromURL(InputStream istream, String systemId)
|
408 |
|
|
throws SAXException {
|
409 |
|
|
String dtdPath = null;
|
410 |
|
|
String dtdURL = null;
|
411 |
|
|
try {
|
412 |
|
|
dtdPath = SystemUtil.getContextDir() + "/dtd/";
|
413 |
|
|
dtdURL = SystemUtil.getContextURL() + "/dtd/";
|
414 |
|
|
} catch (PropertyNotFoundException pnfe) {
|
415 |
4967
|
daigle
|
throw new SAXException("DBEntityResolver.uploadDTDFromURL - Property issue when uploading DTD from URL: "
|
416 |
4080
|
daigle
|
+ pnfe.getMessage());
|
417 |
|
|
}
|
418 |
1358
|
tao
|
|
419 |
4080
|
daigle
|
// get filename from systemId
|
420 |
|
|
String filename = systemId;
|
421 |
|
|
int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
|
422 |
|
|
if (slash > -1) {
|
423 |
|
|
filename = filename.substring(slash + 1);
|
424 |
|
|
}
|
425 |
694
|
bojilova
|
|
426 |
4080
|
daigle
|
// writing dtd text on Metacat file system as filename
|
427 |
|
|
try {
|
428 |
|
|
// create a buffering character-input stream
|
429 |
|
|
// that uses a default-sized input buffer
|
430 |
|
|
BufferedInputStream in = new BufferedInputStream(istream);
|
431 |
694
|
bojilova
|
|
432 |
4080
|
daigle
|
// open file writer to write the input into it
|
433 |
|
|
//String dtdPath = "/opt/tomcat/webapps/bojilova/dtd/";
|
434 |
|
|
File f = new File(dtdPath, filename);
|
435 |
|
|
synchronized (f) {
|
436 |
|
|
try {
|
437 |
|
|
if (f.exists()) {
|
438 |
5177
|
daigle
|
logMetacat.warn("DBEntityResolver.uploadDTDFromURL - File already exists: " + f.getCanonicalFile());
|
439 |
4803
|
leinfelder
|
//return dtdURL + filename;
|
440 |
|
|
//throw new IOException("File already exist: "
|
441 |
|
|
// + f.getCanonicalFile());
|
442 |
4080
|
daigle
|
//if ( f.exists() && !f.canWrite() ) {
|
443 |
|
|
// throw new IOException("Not writable: " + f.getCanonicalFile());
|
444 |
|
|
}
|
445 |
|
|
} catch (SecurityException se) {
|
446 |
|
|
// if a security manager exists,
|
447 |
|
|
// its checkRead method is called for f.exist()
|
448 |
|
|
// or checkWrite method is called for f.canWrite()
|
449 |
|
|
throw se;
|
450 |
|
|
}
|
451 |
|
|
// create a buffered character-output stream
|
452 |
|
|
// that uses a default-sized output buffer
|
453 |
|
|
FileWriter fw = new FileWriter(f);
|
454 |
|
|
BufferedWriter out = new BufferedWriter(fw);
|
455 |
694
|
bojilova
|
|
456 |
4080
|
daigle
|
// read the input and write into the file writer
|
457 |
|
|
int inputByte;
|
458 |
|
|
while ((inputByte = in.read()) != -1) {
|
459 |
|
|
out.write(inputByte);
|
460 |
|
|
//out.newLine(); //instead of out.write('\r\n');
|
461 |
|
|
}
|
462 |
694
|
bojilova
|
|
463 |
4080
|
daigle
|
// the input and the output streams must be closed
|
464 |
|
|
in.close();
|
465 |
|
|
out.flush();
|
466 |
|
|
out.close();
|
467 |
|
|
fw.close();
|
468 |
|
|
} // end of synchronized
|
469 |
|
|
} catch (MalformedURLException e) {
|
470 |
4967
|
daigle
|
throw new SAXException("DBEntityResolver.uploadDTDFromURL - Malformed URL when uploading DTD from URL: "
|
471 |
4080
|
daigle
|
+ e.getMessage());
|
472 |
|
|
} catch (IOException e) {
|
473 |
4967
|
daigle
|
throw new SAXException("DBEntityResolver.uploadDTDFromURL - I/O issue when uploading DTD from URL: "
|
474 |
4080
|
daigle
|
+ e.getMessage());
|
475 |
|
|
} catch (SecurityException e) {
|
476 |
4967
|
daigle
|
throw new SAXException("DBEntityResolver.uploadDTDFromURL - Security issue when uploading DTD from URL: "
|
477 |
4080
|
daigle
|
+ e.getMessage());
|
478 |
|
|
}
|
479 |
1358
|
tao
|
|
480 |
4080
|
daigle
|
//String dtdURL = "http://dev.nceas.ucsb.edu/bojilova/dtd/";
|
481 |
|
|
return dtdURL + filename;
|
482 |
|
|
}
|
483 |
694
|
bojilova
|
|
484 |
4080
|
daigle
|
/**
|
485 |
|
|
* Check URL Connection for @systemId, and return an InputStream
|
486 |
|
|
* that can be used to read from the systemId URL. The parser ends
|
487 |
|
|
* up using this via the InputSource to read the DTD.
|
488 |
|
|
*
|
489 |
|
|
* @param systemId a URI (in practice URL) to be checked and opened
|
490 |
|
|
*/
|
491 |
|
|
public static InputStream checkURLConnection(String systemId) throws SAXException {
|
492 |
|
|
try {
|
493 |
|
|
return (new URL(systemId).openStream());
|
494 |
598
|
bojilova
|
|
495 |
4080
|
daigle
|
} catch (MalformedURLException e) {
|
496 |
4967
|
daigle
|
throw new SAXException("DBEntityResolver.checkURLConnection - Malformed URL when checking URL Connection: "
|
497 |
4080
|
daigle
|
+ e.getMessage());
|
498 |
|
|
} catch (IOException e) {
|
499 |
4967
|
daigle
|
throw new SAXException("DBEntityResolver.checkURLConnection - I/O issue when checking URL Connection: "
|
500 |
4080
|
daigle
|
+ e.getMessage());
|
501 |
|
|
}
|
502 |
|
|
}
|
503 |
72
|
bojilova
|
}
|