Project

General

Profile

« Previous | Next » 

Revision 5619

Added by berkley over 13 years ago

fixed redmine task 864. Metacat will now download an http:// referenced schema when a reference is put in the xml_catalog table.

View differences:

src/edu/ucsb/nceas/metacat/service/XMLSchemaService.java
26 26

  
27 27
package edu.ucsb.nceas.metacat.service;
28 28

  
29
import java.io.File;
30
import java.io.FileWriter;
29 31
import java.io.IOException;
32
import java.io.OutputStream;
30 33
import java.io.StringReader;
34
import java.net.HttpURLConnection;
35
import java.net.MalformedURLException;
36
import java.net.URL;
31 37
import java.sql.PreparedStatement;
32 38
import java.sql.ResultSet;
33 39
import java.sql.SQLException;
......
35 41
import java.util.regex.Matcher;
36 42
import java.util.regex.Pattern;
37 43

  
44
import org.apache.commons.io.IOUtils;
38 45
import org.apache.log4j.Logger;
39 46

  
40 47
import edu.ucsb.nceas.metacat.DocumentImpl;
......
208 215
				String fileLocation = resultSet.getString(2);
209 216
				logMetacat.debug("XMLService.populateRegisteredSchemaList - Registering schema: " + fileNamespace + " " + fileLocation);
210 217
				XMLSchema xmlSchema = new XMLSchema(fileNamespace);
211
				xmlSchema.setFileName(fileLocation);
212

  
213
				if (FileUtil.getFileStatus(xmlSchema.getLocalFileDir()) >= FileUtil.EXISTS_READABLE) {
218
				if(fileLocation.startsWith("http://") || fileLocation.startsWith("https://"))
219
				{
220
				    //System.out.println("processing an http schemal location");
221
				    logMetacat.debug("XMLService.populateRegisteredSchemaList - Processing http schema location: " + fileLocation);
222
				    xmlSchema.setExternalFileUri(fileLocation);
223
				    //cache the file
224
				    try
225
				    {
226
				        URL u = new URL(fileLocation);
227
				        //System.out.println("downloading " + fileLocation);
228
				        logMetacat.debug("XMLService.populateRegisteredSchemaList - Downloading http based schema...");
229
				        HttpURLConnection connection = (HttpURLConnection) u.openConnection();
230
				        connection.setDoOutput(true);
231
			            connection.setRequestMethod("GET");
232
			            connection.connect();
233
			            String schema = IOUtils.toString(connection.getInputStream());
234
			            
235
			            String deployDir = PropertyService.getProperty("application.deployDir");
236
			            String contextName = PropertyService.getProperty("application.context");
237
			            String filename = fileLocation.substring(fileLocation.lastIndexOf("/"), 
238
                                fileLocation.length());
239
			            File schemaFile = new File(deployDir + "/" + contextName + "/" +
240
			                    "schema/" + filename);
241
			            //System.out.println("writing schema to " + schemaFile.getAbsolutePath());
242
			            FileWriter fw = new FileWriter(schemaFile);
243
			            fw.write(schema);
244
			            fw.flush();
245
			            fw.close();
246
			            logMetacat.debug("XMLService.populateRegisteredSchemaList - Schema downloaded to " + schemaFile.getAbsolutePath());
247
			            fileLocation = "/schema/" + filename;
248
			            //System.out.println("fileLocation set to " + fileLocation);
249
			            logMetacat.debug("XMLService.populateRegisteredSchemaList - fileLocation set to " + fileLocation);
250
			            xmlSchema.setFileName(fileLocation);
251
				    }
252
				    catch(MalformedURLException me)
253
				    {
254
				        logMetacat.warn("Could not cache a registered schema at " + fileLocation +
255
				                " because a connection could not be made to the given url: " + 
256
				                me.getMessage());
257
				    }
258
                    catch (IOException ioe)
259
                    {
260
                        logMetacat.warn("Could not cache a registered schema at " + fileLocation +
261
                        " because an IOException occured: " + ioe.getMessage());
262
                    }
263
                    catch(PropertyNotFoundException pnfe)
264
                    {
265
                        logMetacat.warn("Could not cache a registered schema at " + fileLocation +
266
                                " because the property 'application.tempDir' could not be found.");
267
                    }
268
				    
269
				    xmlSchema.setFileName(fileLocation);
270
				}
271
				else
272
				{
273
				    xmlSchema.setFileName(fileLocation);
274
				}
275
								
276
				if (FileUtil.getFileStatus(xmlSchema.getLocalFileDir()) >= FileUtil.EXISTS_READABLE) 
277
				{
214 278
					registeredSchemaList.add(xmlSchema);
215
				} else {
279
				}
280
				else if(fileLocation.startsWith("http://") || fileLocation.startsWith("https://"))
281
                {  //the schema resides on a different server, to validate, we need to go get it 
282
                    registeredSchemaList.add(xmlSchema);
283
                }
284
				else 
285
				{
216 286
					logMetacat.warn("XMLService.populateRegisteredSchemaList - Schema file: " + xmlSchema.getLocalFileDir() + " is registered "
217 287
							+ " in the database but does not exist on the file system.");
218 288
				}

Also available in: Unified diff