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 |
9913
|
tao
|
InputSource dtdSource= null;
|
101 |
|
|
logMetacat.debug("DBEntityResolver.resolveEntity - in resolveEntity "+" the public id is "+publicId+" and systemId is "+systemId);
|
102 |
598
|
bojilova
|
String dbSystemID;
|
103 |
|
|
String doctype = null;
|
104 |
1358
|
tao
|
|
105 |
598
|
bojilova
|
// Won't have a handler under all cases
|
106 |
|
|
if ( handler != null ) {
|
107 |
9974
|
tao
|
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 |
9972
|
tao
|
logMetacat.debug("DBEntityResolver.resolveEntity - the handler class is "+handler.getClass().getCanonicalName());
|
109 |
598
|
bojilova
|
if ( handler instanceof DBSAXHandler ) {
|
110 |
|
|
DBSAXHandler dhandler = null;
|
111 |
|
|
dhandler = (DBSAXHandler)handler;
|
112 |
|
|
if ( dhandler.processingDTD() ) {
|
113 |
9972
|
tao
|
logMetacat.debug("DBEntityResolver.resolveEntity - in the branch of the handler class is DBSAXHandler");
|
114 |
598
|
bojilova
|
// public ID is doctype
|
115 |
1358
|
tao
|
if (publicId != null) {
|
116 |
598
|
bojilova
|
doctype = publicId;
|
117 |
9916
|
tao
|
logMetacat.debug("DBEntityResolver.resolveEntity - the publicId is not null, so the publicId is the doctype. The doctype is: "
|
118 |
5177
|
daigle
|
+ doctype);
|
119 |
598
|
bojilova
|
// assume public ID (doctype) is docname
|
120 |
9913
|
tao
|
} else {
|
121 |
598
|
bojilova
|
doctype = dhandler.getDocname();
|
122 |
9916
|
tao
|
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 |
9913
|
tao
|
+ doctype);
|
124 |
72
|
bojilova
|
}
|
125 |
9974
|
tao
|
|
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 |
72
|
bojilova
|
}
|
134 |
598
|
bojilova
|
} else if ( handler instanceof AccessControlList ) {
|
135 |
9972
|
tao
|
logMetacat.debug("DBEntityResolver.resolveEntity - in the branch of the handler class is AccessControlList");
|
136 |
598
|
bojilova
|
AccessControlList ahandler = null;
|
137 |
|
|
ahandler = (AccessControlList)handler;
|
138 |
9974
|
tao
|
if ( ahandler.processingDTD() ) {
|
139 |
598
|
bojilova
|
// public ID is doctype
|
140 |
1358
|
tao
|
if (publicId != null) {
|
141 |
598
|
bojilova
|
doctype = publicId;
|
142 |
9974
|
tao
|
logMetacat.debug("DBEntityResolver.resolveEntity - the publicId is not null, so the publicId is the doctype. The doctype in AccessControlList is: "
|
143 |
|
|
+ doctype);
|
144 |
598
|
bojilova
|
// assume public ID (doctype) is docname
|
145 |
9913
|
tao
|
} else {
|
146 |
598
|
bojilova
|
doctype = ahandler.getDocname();
|
147 |
9974
|
tao
|
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 |
598
|
bojilova
|
}
|
150 |
9974
|
tao
|
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 |
9972
|
tao
|
} else {
|
161 |
|
|
logMetacat.debug("DBEntityResolver.resolveEntity - in the branch of the other handler class");
|
162 |
598
|
bojilova
|
}
|
163 |
9913
|
tao
|
} else {
|
164 |
9916
|
tao
|
logMetacat.debug("DBEntityResolver.resolveEntity - the xml handler is null. So we can't find the doctype.");
|
165 |
598
|
bojilova
|
}
|
166 |
72
|
bojilova
|
|
167 |
598
|
bojilova
|
// get System ID for doctype
|
168 |
|
|
if (doctype != null) {
|
169 |
1358
|
tao
|
// look at db XML Catalog for System ID
|
170 |
5177
|
daigle
|
logMetacat.info("DBEntityResolver.resolveEntity - get systemId from doctype: " + doctype);
|
171 |
598
|
bojilova
|
dbSystemID = getDTDSystemID(doctype);
|
172 |
9916
|
tao
|
logMetacat.info("DBEntityResolver.resolveEntity - The Systemid from xml_catalog table is: " + dbSystemID);
|
173 |
9913
|
tao
|
if(dbSystemID == null) {
|
174 |
9916
|
tao
|
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 |
9913
|
tao
|
}
|
177 |
4803
|
leinfelder
|
// check that it is accessible on our system before getting too far
|
178 |
|
|
try {
|
179 |
|
|
InputStream in = checkURLConnection(dbSystemID);
|
180 |
9913
|
tao
|
dtdSource = new InputSource(in);
|
181 |
5177
|
daigle
|
} catch (SAXException se) {
|
182 |
9913
|
tao
|
se.printStackTrace();
|
183 |
|
|
throw se;
|
184 |
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
|
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 |
9913
|
tao
|
/*logMetacat.warn("DBEntityResolver.resolveEntity - Problem when checking URL Connection: " + se.getMessage());
|
188 |
5177
|
daigle
|
logMetacat.warn("DBEntityResolver.resolveEntity - Probably, dtd for doc type " + doctype + " existed in xml catalog, but not on disk. Uploading from: " + systemId);
|
189 |
4978
|
daigle
|
InputStream istream = checkURLConnection(systemId);
|
190 |
|
|
uploadDTDFromURL(istream, systemId);
|
191 |
|
|
try {
|
192 |
5177
|
daigle
|
Thread.currentThread().sleep(6000);
|
193 |
4978
|
daigle
|
checkURLConnection(dbSystemID);
|
194 |
|
|
} catch (Exception e2) {
|
195 |
5177
|
daigle
|
logMetacat.error("DBEntityResolver.resolveEntity - still could not find dtd for doc type " + doctype + " at "
|
196 |
4978
|
daigle
|
+ dbSystemID + " : " + e2.getMessage());
|
197 |
|
|
dbSystemID = null;
|
198 |
9913
|
tao
|
}*/
|
199 |
5177
|
daigle
|
}
|
200 |
9913
|
tao
|
/*boolean doctypeIsInDB = true;
|
201 |
598
|
bojilova
|
// 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 |
5177
|
daigle
|
logMetacat.info("DBEntityResolver.resolveEntity - If above Systemid is null, then get "
|
209 |
|
|
+ "system id from file: " + dbSystemID);
|
210 |
598
|
bojilova
|
}
|
211 |
|
|
// there are dtd text provided; try to upload on Metacat
|
212 |
|
|
if ( dtdtext != null ) {
|
213 |
|
|
dbSystemID = uploadDTD(dbSystemID);
|
214 |
|
|
}
|
215 |
|
|
|
216 |
694
|
bojilova
|
// open URLConnection to check first
|
217 |
598
|
bojilova
|
InputStream istream = checkURLConnection(dbSystemID);
|
218 |
|
|
|
219 |
|
|
// need to register System ID in db XML Catalog if not yet
|
220 |
|
|
if ( !doctypeIsInDB ) {
|
221 |
694
|
bojilova
|
// new DTD from outside URL location; try to upload on Metacat
|
222 |
|
|
if ( dtdtext == null ) {
|
223 |
|
|
dbSystemID = uploadDTDFromURL(istream, dbSystemID);
|
224 |
|
|
}
|
225 |
598
|
bojilova
|
registerDTD(doctype, dbSystemID);
|
226 |
|
|
}
|
227 |
|
|
// return a byte-input stream for use
|
228 |
1358
|
tao
|
InputSource is = new InputSource(dbSystemID);
|
229 |
694
|
bojilova
|
|
230 |
|
|
// close and open URLConnection again
|
231 |
|
|
try {
|
232 |
|
|
istream.close();
|
233 |
|
|
} catch (IOException e) {
|
234 |
1358
|
tao
|
throw new SAXException
|
235 |
4967
|
daigle
|
("DBEntityResolver.resolveEntity - I/O issue when resolving entity: " + e.getMessage());
|
236 |
1358
|
tao
|
}
|
237 |
694
|
bojilova
|
istream = checkURLConnection(dbSystemID);
|
238 |
598
|
bojilova
|
is.setByteStream(istream);
|
239 |
9913
|
tao
|
return is;*/
|
240 |
598
|
bojilova
|
} else {
|
241 |
9974
|
tao
|
|
242 |
9913
|
tao
|
//InputStream istream = checkURLConnection(systemId);
|
243 |
|
|
//return null;
|
244 |
598
|
bojilova
|
}
|
245 |
9913
|
tao
|
return dtdSource;
|
246 |
1358
|
tao
|
|
247 |
598
|
bojilova
|
}
|
248 |
|
|
|
249 |
1358
|
tao
|
/**
|
250 |
598
|
bojilova
|
* 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 |
1895
|
tao
|
public static String getDTDSystemID( String doctype )
|
254 |
598
|
bojilova
|
throws SAXException
|
255 |
|
|
{
|
256 |
|
|
String systemid = null;
|
257 |
6606
|
leinfelder
|
PreparedStatement pstmt = null;
|
258 |
1217
|
tao
|
DBConnection conn = null;
|
259 |
|
|
int serialNumber = -1;
|
260 |
9492
|
tao
|
ResultSet rs = null;
|
261 |
598
|
bojilova
|
try {
|
262 |
1217
|
tao
|
//check out DBConnection
|
263 |
|
|
conn=DBConnectionPool.getDBConnection("DBEntityResolver.getDTDSystemID");
|
264 |
|
|
serialNumber=conn.getCheckOutSerialNumber();
|
265 |
1358
|
tao
|
|
266 |
6606
|
leinfelder
|
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 |
9492
|
tao
|
rs = pstmt.getResultSet();
|
274 |
598
|
bojilova
|
boolean tableHasRows = rs.next();
|
275 |
|
|
if (tableHasRows) {
|
276 |
|
|
systemid = rs.getString(1);
|
277 |
4080
|
daigle
|
// system id may not have server url on front. Add it if not.
|
278 |
|
|
if (!systemid.startsWith("http://")) {
|
279 |
4123
|
daigle
|
systemid = SystemUtil.getContextURL() + systemid;
|
280 |
4080
|
daigle
|
}
|
281 |
598
|
bojilova
|
}
|
282 |
9492
|
tao
|
//pstmt.close();
|
283 |
598
|
bojilova
|
} catch (SQLException e) {
|
284 |
|
|
throw new SAXException
|
285 |
4967
|
daigle
|
("DBEntityResolver.getDTDSystemID - SQL error when getting DTD system ID: " + e.getMessage());
|
286 |
4080
|
daigle
|
} catch (PropertyNotFoundException pnfe) {
|
287 |
|
|
throw new SAXException
|
288 |
4967
|
daigle
|
("DBEntityResolver.getDTDSystemID - Property error when getting DTD system ID: " + pnfe.getMessage());
|
289 |
4080
|
daigle
|
}
|
290 |
1217
|
tao
|
finally
|
291 |
|
|
{
|
292 |
|
|
try
|
293 |
|
|
{
|
294 |
9492
|
tao
|
if(rs != null) {
|
295 |
|
|
rs.close();
|
296 |
|
|
}
|
297 |
|
|
if(pstmt != null) {
|
298 |
|
|
pstmt.close();
|
299 |
|
|
}
|
300 |
|
|
|
301 |
1217
|
tao
|
}//try
|
302 |
|
|
catch (SQLException sqlE)
|
303 |
|
|
{
|
304 |
5177
|
daigle
|
logMetacat.error("DBEntityResolver.getDTDSystemId - SQL error: " + sqlE.getMessage());
|
305 |
1217
|
tao
|
}//catch
|
306 |
|
|
finally
|
307 |
|
|
{
|
308 |
|
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
309 |
|
|
}//finally
|
310 |
|
|
}//finally
|
311 |
598
|
bojilova
|
|
312 |
|
|
// return the selected System ID
|
313 |
|
|
return systemid;
|
314 |
|
|
}
|
315 |
|
|
|
316 |
1358
|
tao
|
/**
|
317 |
|
|
* Register new DTD identified by @systemId in Metacat XML Catalog
|
318 |
598
|
bojilova
|
* . make a reference with @systemId for @doctype in Metacat DB
|
319 |
|
|
*/
|
320 |
|
|
private void registerDTD ( String doctype, String systemId )
|
321 |
|
|
throws SAXException
|
322 |
|
|
{
|
323 |
4803
|
leinfelder
|
String existingSystemId = getDTDSystemID(doctype);
|
324 |
|
|
if (existingSystemId != null && existingSystemId.equals(systemId)) {
|
325 |
5177
|
daigle
|
logMetacat.warn("DBEntityResolver.registerDTD - doctype/systemId already registered in DB: " + doctype);
|
326 |
4803
|
leinfelder
|
return;
|
327 |
|
|
}
|
328 |
1217
|
tao
|
//DBConnection conn = null;
|
329 |
|
|
//int serialNumber = -1;
|
330 |
|
|
PreparedStatement pstmt = null;
|
331 |
598
|
bojilova
|
// make a reference in db catalog table with @systemId for @doctype
|
332 |
|
|
try {
|
333 |
1217
|
tao
|
//check out DBConnection
|
334 |
|
|
//conn=DBConnectionPool.getDBConnection("DBEntityResolver.registerDTD");
|
335 |
|
|
//serialNumber=conn.getCheckOutSerialNumber();
|
336 |
1358
|
tao
|
|
337 |
|
|
|
338 |
1217
|
tao
|
pstmt = connection.prepareStatement(
|
339 |
243
|
jones
|
"INSERT INTO xml_catalog " +
|
340 |
4800
|
leinfelder
|
"(entry_type, public_id, system_id) " +
|
341 |
|
|
"VALUES ('DTD', ?, ?)");
|
342 |
1217
|
tao
|
// Increase usage count
|
343 |
|
|
connection.increaseUsageCount(1);
|
344 |
598
|
bojilova
|
// Bind the values to the query
|
345 |
|
|
pstmt.setString(1, doctype);
|
346 |
|
|
pstmt.setString(2, systemId);
|
347 |
|
|
// Do the insertion
|
348 |
|
|
pstmt.execute();
|
349 |
4803
|
leinfelder
|
int updateCnt = pstmt.getUpdateCount();
|
350 |
5177
|
daigle
|
logMetacat.debug("DBEntityReolver.registerDTD - DTDs registered: " + updateCnt);
|
351 |
598
|
bojilova
|
pstmt.close();
|
352 |
|
|
} catch (SQLException e) {
|
353 |
|
|
throw new SAXException
|
354 |
4967
|
daigle
|
("DBEntityResolver.registerDTD - SQL issue when registering DTD: " + e.getMessage());
|
355 |
598
|
bojilova
|
}
|
356 |
1217
|
tao
|
finally
|
357 |
|
|
{
|
358 |
|
|
try
|
359 |
|
|
{
|
360 |
|
|
pstmt.close();
|
361 |
|
|
}//try
|
362 |
|
|
catch (SQLException sqlE)
|
363 |
|
|
{
|
364 |
5177
|
daigle
|
logMetacat.error("DBEntityResolver.registerDTD - SQL error: " + sqlE.getMessage());
|
365 |
1217
|
tao
|
}//catch
|
366 |
|
|
//DBConnectionPool.returnDBConnection(conn, serialNumber);
|
367 |
|
|
}//finally
|
368 |
1358
|
tao
|
|
369 |
598
|
bojilova
|
}
|
370 |
243
|
jones
|
|
371 |
1358
|
tao
|
/**
|
372 |
4080
|
daigle
|
* 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 |
1358
|
tao
|
|
386 |
4080
|
daigle
|
// 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 |
245
|
jones
|
|
393 |
4080
|
daigle
|
// 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 |
245
|
jones
|
|
399 |
4080
|
daigle
|
// 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 |
598
|
bojilova
|
|
422 |
4080
|
daigle
|
// 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 |
598
|
bojilova
|
|
429 |
4080
|
daigle
|
// 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 |
4967
|
daigle
|
throw new SAXException("DBEntityResolver.uploadDTD() - Malformed URL when uploading DTD: " + e.getMessage());
|
437 |
4080
|
daigle
|
} catch (IOException e) {
|
438 |
4967
|
daigle
|
throw new SAXException("DBEntityResolver.uploadDTD - I/O issue when uploading DTD: " + e.getMessage());
|
439 |
4080
|
daigle
|
} catch (SecurityException e) {
|
440 |
4967
|
daigle
|
throw new SAXException("DBEntityResolver.uploadDTD() - Security issue when uploading DTD: " + e.getMessage());
|
441 |
4080
|
daigle
|
}
|
442 |
1358
|
tao
|
|
443 |
4080
|
daigle
|
// String dtdURL = "http://dev.nceas.ucsb.edu/bojilova/dtd/";
|
444 |
|
|
return dtdURL + filename;
|
445 |
|
|
}
|
446 |
598
|
bojilova
|
|
447 |
694
|
bojilova
|
|
448 |
1358
|
tao
|
/**
|
449 |
4080
|
daigle
|
* 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 |
4967
|
daigle
|
throw new SAXException("DBEntityResolver.uploadDTDFromURL - Property issue when uploading DTD from URL: "
|
460 |
4080
|
daigle
|
+ pnfe.getMessage());
|
461 |
|
|
}
|
462 |
1358
|
tao
|
|
463 |
4080
|
daigle
|
// 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 |
694
|
bojilova
|
|
470 |
4080
|
daigle
|
// 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 |
694
|
bojilova
|
|
476 |
4080
|
daigle
|
// 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 |
5177
|
daigle
|
logMetacat.warn("DBEntityResolver.uploadDTDFromURL - File already exists: " + f.getCanonicalFile());
|
483 |
4803
|
leinfelder
|
//return dtdURL + filename;
|
484 |
|
|
//throw new IOException("File already exist: "
|
485 |
|
|
// + f.getCanonicalFile());
|
486 |
4080
|
daigle
|
//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 |
694
|
bojilova
|
|
500 |
4080
|
daigle
|
// 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 |
694
|
bojilova
|
|
507 |
4080
|
daigle
|
// 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 |
4967
|
daigle
|
throw new SAXException("DBEntityResolver.uploadDTDFromURL - Malformed URL when uploading DTD from URL: "
|
515 |
4080
|
daigle
|
+ e.getMessage());
|
516 |
|
|
} catch (IOException e) {
|
517 |
4967
|
daigle
|
throw new SAXException("DBEntityResolver.uploadDTDFromURL - I/O issue when uploading DTD from URL: "
|
518 |
4080
|
daigle
|
+ e.getMessage());
|
519 |
|
|
} catch (SecurityException e) {
|
520 |
4967
|
daigle
|
throw new SAXException("DBEntityResolver.uploadDTDFromURL - Security issue when uploading DTD from URL: "
|
521 |
4080
|
daigle
|
+ e.getMessage());
|
522 |
|
|
}
|
523 |
1358
|
tao
|
|
524 |
4080
|
daigle
|
//String dtdURL = "http://dev.nceas.ucsb.edu/bojilova/dtd/";
|
525 |
|
|
return dtdURL + filename;
|
526 |
|
|
}
|
527 |
694
|
bojilova
|
|
528 |
4080
|
daigle
|
/**
|
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 |
598
|
bojilova
|
|
539 |
4080
|
daigle
|
} catch (MalformedURLException e) {
|
540 |
4967
|
daigle
|
throw new SAXException("DBEntityResolver.checkURLConnection - Malformed URL when checking URL Connection: "
|
541 |
4080
|
daigle
|
+ e.getMessage());
|
542 |
|
|
} catch (IOException e) {
|
543 |
4967
|
daigle
|
throw new SAXException("DBEntityResolver.checkURLConnection - I/O issue when checking URL Connection: "
|
544 |
4080
|
daigle
|
+ e.getMessage());
|
545 |
|
|
}
|
546 |
|
|
}
|
547 |
72
|
bojilova
|
}
|