Project

General

Profile

« Previous | Next » 

Revision 4950

Added by daigle over 15 years ago

Add archival read funtionality (jar/kar/war files)

View differences:

src/edu/ucsb/nceas/metacat/ArchiveHandler.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements session utility methods 
4
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Michael Daigle
7
 * 
8
 *   '$Author: daigle $'
9
 *     '$Date: 2009-03-25 13:41:15 -0800 (Wed, 25 Mar 2009) $'
10
 * '$Revision: 4861 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26

  
27
package edu.ucsb.nceas.metacat;
28

  
29
import java.sql.SQLException;
30
import java.util.Hashtable;
31

  
32
import javax.servlet.http.HttpServletRequest;
33
import javax.servlet.http.HttpServletResponse;
34

  
35
import org.apache.log4j.Logger;
36

  
37
import edu.ucsb.nceas.metacat.service.PropertyService;
38
import edu.ucsb.nceas.metacat.util.ErrorSendingErrorException;
39
import edu.ucsb.nceas.metacat.util.ErrorUtil;
40
import edu.ucsb.nceas.metacat.util.MetacatUtilException;
41
import edu.ucsb.nceas.metacat.util.ResponseUtil;
42
import edu.ucsb.nceas.utilities.FileUtil;
43
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
44
import edu.ucsb.nceas.utilities.UtilException;
45

  
46
public class ArchiveHandler  {
47
	
48
	private static ArchiveHandler archiveHandler = null;
49
	
50
	private static Logger logMetacat = Logger.getLogger(ArchiveHandler.class);
51

  
52
	/**
53
	 * private constructor since this is a singleton
54
	 */
55
	private ArchiveHandler() throws HandlerException {}
56
	
57
	/**
58
	 * Get the single instance of SessionService.
59
	 * 
60
	 * @return the single instance of SessionService
61
	 */
62
	public static ArchiveHandler getInstance() throws HandlerException {
63
		if (archiveHandler == null) {
64
			archiveHandler = new ArchiveHandler();
65
		}
66
		return archiveHandler;
67
	}
68
	
69
	public void readArchiveEntry(Hashtable<String, String[]> params, HttpServletRequest request,
70
            HttpServletResponse response, String user, 
71
            String passWord, String[] groups) throws HandlerException, ErrorSendingErrorException, ErrorHandledException {
72
        	
73
		String[] docs = new String[0];
74
        String docid = "";
75
        String qformat = "";
76
        String[] archiveEntryNames = new String[0];
77
        String archiveEntryName = "";
78
        
79
        try {          
80
            // read the docid from params
81
            if (params.containsKey("docid")) {
82
                docs = params.get("docid");
83
                docid = docs[0];
84
            } else { 
85
            	throw new HandlerException("Could not find doc id in params when reading archive entry");
86
            } 
87
            
88
            // read the archive file entry name from params
89
            if (params.containsKey("archiveEntryName")) {
90
            	archiveEntryNames = params.get("archiveEntryName");
91
            	archiveEntryName = archiveEntryNames[0];
92
            } else { 
93
            	throw new HandlerException("Could not find archiveEntryName in params when reading archive entry");
94
            } 
95
            
96
            // qformat is used to hold the type of archive and the format of the content that we wish to 
97
            // extract from the archive.  The default is jar-file for streaming a file from a jar.  If 
98
            // we wanted to do some special processing for specific archive or content types, we would use 
99
            // a different qformat (gzip-pdf for instance).
100
            if (params.containsKey("qformat")) {
101
                qformat = params.get("qformat")[0];
102
            } else { 
103
            	qformat = "jar-file";
104
            }       
105
            
106
            //check the permission for read
107
            try {
108
            	if (!DocumentImpl.hasReadPermission(user, groups, docid)) {
109
            		String errorString = "User " + user + " does not have permission"
110
    					+ " to read the document with the docid " + docid;
111
            		ErrorUtil.sendError(response, ErrorUtil.NO_READ_PERMISSION, errorString);
112
            		return;
113
            	}
114
            } catch (McdbException mcdbe) {
115
            	throw new HandlerException("Error getting permissions for docid: " 
116
            			+ docid + " for user: " + user + " : " + mcdbe.getMessage());
117
            } catch (SQLException sqle) {
118
            	throw new HandlerException("SQL error getting permissions for docid: " 
119
            			+ docid + " for user: " + user + " : " + sqle.getMessage());
120
            } 
121
            
122
            // Get the path to the archive file
123
            String archiveFileBasePath = PropertyService.getProperty("application.datafilepath");
124
            String archiveFilePath = archiveFileBasePath + FileUtil.getFS() + docid;
125
            
126
            // Get the paths to the expanded archive directory and to the content file within
127
            // that directory
128
            String expandedArchiveBasePath = PropertyService.getProperty("application.expandedArchivePath");
129
            String expandedArchivePath = expandedArchiveBasePath + FileUtil.getFS() + docid;
130
            String entryFilePath = expandedArchivePath + FileUtil.getFS() + archiveEntryName;
131
            
132
            // If the expanded archive directory does not exist, create it.
133
            if (!FileUtil.isDirectory(expandedArchivePath)) {
134
            	FileUtil.createDirectory(expandedArchivePath);
135
            }
136
            
137
            // If the entry we want does not exist, make sure it is actually in the archive
138
            // file.  If so, go ahead and expand the archive into the expanded archive directory.
139
            if (FileUtil.getFileStatus(entryFilePath) == FileUtil.DOES_NOT_EXIST) {
140
            	if (FileUtil.getFileStatus(archiveFilePath) < FileUtil.EXISTS_READABLE) {
141
            		throw new HandlerException("Could not find archive: " + archiveFilePath 
142
            				+ " in order to get content file: " + archiveEntryName);
143
            	}
144
            	
145
            	// extract the jar file to the expanded archive directory
146
            	FileUtil.extractJarFile(archiveFilePath, expandedArchivePath);
147
            }
148
            
149
            ResponseUtil.writeFileToOutput(response, expandedArchivePath, archiveEntryName);            
150
                        
151
        } catch (UtilException ue) {
152
        	String errorString = "Utility error reading archive entry for docid: " 
153
    			+ docid + " : " + ue.getMessage();
154
        	ErrorUtil.sendError(response, ErrorUtil.GENERAL_UTILITY_ERROR, errorString);
155
        	throw new ErrorHandledException(null);
156
        } catch (PropertyNotFoundException pnfe) {
157
        	String errorString = "Property error reading archive entry for docid: " 
158
        		+ docid + " : " + pnfe.getMessage();
159
        	ErrorUtil.sendError(response, ErrorUtil.PROPERTY_NOT_FOUND, errorString);
160
        	throw new ErrorHandledException(null);
161
        } catch (MetacatUtilException mue) {
162
        	String errorString = "Metacat utility error reading archive entry for docid: " 
163
        		+ docid + " : " + mue.getMessage();
164
        	ErrorUtil.sendError(response, ErrorUtil.METACAT_UTILITY_ERROR, errorString);
165
        	throw new ErrorHandledException(null);
166
        } 
167
	}
168

  
169
}
0 170

  
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
47 47
import java.util.HashMap;
48 48
import java.util.Hashtable;
49 49
import java.util.Iterator;
50
import java.util.List;
51 50
import java.util.Map;
52 51
import java.util.Timer;
53 52
import java.util.Vector;
......
76 75

  
77 76
import edu.ucsb.nceas.metacat.cart.CartManager;
78 77
import edu.ucsb.nceas.metacat.dataquery.DataQuery;
78
import edu.ucsb.nceas.metacat.scheduler.SchedulerService;
79 79
import edu.ucsb.nceas.metacat.service.DatabaseService;
80 80
import edu.ucsb.nceas.metacat.service.PropertyService;
81 81
import edu.ucsb.nceas.metacat.service.ServiceException;
......
86 86
import edu.ucsb.nceas.metacat.spatial.SpatialHarvester;
87 87
import edu.ucsb.nceas.metacat.spatial.SpatialQuery;
88 88
import edu.ucsb.nceas.metacat.util.AuthUtil;
89
import edu.ucsb.nceas.metacat.util.ErrorSendingErrorException;
89 90
import edu.ucsb.nceas.metacat.util.MetacatUtil;
91
import edu.ucsb.nceas.metacat.util.MetacatUtilException;
90 92
import edu.ucsb.nceas.metacat.util.RequestUtil;
91 93
import edu.ucsb.nceas.metacat.util.SystemUtil;
92 94
import edu.ucsb.nceas.metacat.util.SessionData;
93
import edu.ucsb.nceas.metacat.util.MetacatUtilException;
94 95
import edu.ucsb.nceas.utilities.FileUtil;
95 96
import edu.ucsb.nceas.utilities.GeneralPropertyException;
96 97
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
......
233 234
			logMetacat.debug("DBConnection pool initialized: " + connPool.toString());
234 235
			
235 236
			ServiceService.registerService("XMLSchemaService", XMLSchemaService.getInstance());
237
			ServiceService.registerService("SchedulerService", SchedulerService.getInstance());
236 238

  
237 239
			// check if eml201 document were corrected or not. if not, correct
238 240
			// eml201 documents.
......
715 717

  
716 718
					handleExportAction(params, response, userName, groupNames, password);
717 719
				} else if (action.equals("read")) {
718
					handleReadAction(params, request, response, userName, password,
719
							groupNames);
720
					if (params.get("archiveEntryName") != null) {
721
						ArchiveHandler.getInstance().readArchiveEntry(params, request, response, 
722
								userName, password,groupNames);
723
					} else {
724
						handleReadAction(params, request, response, userName, password,
725
								groupNames);
726
					}
720 727
				} else if (action.equals("readinlinedata")) {
721 728
					handleReadInlineDataAction(params, request, response, userName,
722 729
							password, groupNames);
......
858 865
					// MetacatAdminServlet
859 866
					ServiceService.refreshService("XMLSchemaService");
860 867
					return;
861
				}else {
868
				} else if (action.equals("schedule")) {
869
					SchedulerService.getInstance().scheduleHandler(params, request, response, 
870
							userName, groupNames);
871
					return;
872
				} else {
862 873
					PrintWriter out = response.getWriter();
863 874
					out.println("<?xml version=\"1.0\"?>");
864 875
					out.println("<error>");
......
887 898
			String errorString = "Service error: " + ue.getMessage();
888 899
			logMetacat.error(errorString);
889 900
			throw new ServletException(errorString);
901
		} catch (HandlerException he) {
902
			String errorString = "Handler error: " + he.getMessage();
903
			logMetacat.error(errorString);
904
			throw new ServletException(errorString);
905
		} catch (ErrorSendingErrorException esee) {
906
			String errorString = "Error sending error message: " + esee.getMessage();
907
			logMetacat.error(errorString);
908
			throw new ServletException(errorString);
909
		} catch (ErrorHandledException ehe) {
910
			// Nothing to do here.  We assume if we get here, the error has been 
911
			// written to ouput.  Continue on and let it display.
890 912
		}
891 913
	}
892 914
    
src/edu/ucsb/nceas/metacat/PermissionController.java
96 96
    * @param myPermission  permission type to check for
97 97
    */
98 98
  public boolean hasPermission(String user, String[]groups, String myPermission)
99
                              throws SQLException, Exception
99
                              throws SQLException //, Exception
100 100
  {
101 101
    boolean hasPermission=false;
102 102
    String [] userPackage=null;
src/edu/ucsb/nceas/metacat/HandlerException.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: An Exception thrown when an error occurs because an 
4
 *             AccessionNumber was invalid or used incorrectly
5
 *  Copyright: 2008 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Michael Daigle
8
 *
9
 *   '$Author: daigle $'
10
 *     '$Date: 2008-07-06 21:25:34 -0700 (Sun, 06 Jul 2008) $'
11
 * '$Revision: 4080 $'
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
/**
31
 * Exception thrown when an error occurs in a configuration administrative
32
 * class
33
 */
34
public class HandlerException extends Exception {
35

  
36
	private static final long serialVersionUID = 7997030775672963304L;
37

  
38
	/**
39
	 * Create a new AdminException.
40
	 *
41
	 * @param message The error or warning message.
42
	 */
43
	public HandlerException(String message) {
44
		super(message);
45
	}
46
}
0 47

  
src/edu/ucsb/nceas/metacat/DocumentImpl.java
3310 3310
     * @param groups, user's group
3311 3311
     */
3312 3312
    public static boolean hasReadPermission(String user, String[] groups,
3313
            String docId) throws SQLException, Exception
3313
            String docId) throws SQLException, McdbException
3314 3314
    {
3315 3315
        // Check for READ permission on @docid for @user and/or @groups
3316 3316
        PermissionController controller = new PermissionController(docId);
src/edu/ucsb/nceas/metacat/service/PropertyService.java
45 45
import edu.ucsb.nceas.utilities.PropertiesMetaData;
46 46
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
47 47
import edu.ucsb.nceas.utilities.SortedProperties;
48
import edu.ucsb.nceas.utilities.UtilException;
49 48

  
50 49
/**
51 50
 * A suite of utility classes for the metadata configuration utility
......
223 222
		} catch (GeneralPropertyException gpe) {
224 223
			throw new ServiceException("General properties problem while loading properties: "
225 224
					+ gpe.getMessage());
226
		} catch (UtilException ue) {
225
		} catch (MetacatUtilException ue) {
227 226
			throw new ServiceException("Utilities problem while loading properties: "
228 227
					+ ue.getMessage());
229 228
		} 
src/edu/ucsb/nceas/metacat/service/BaseService.java
37 37

  
38 38
	// package level method reporting if service is refreshable.  Basically,
39 39
	// we only want ServiceService calling this.
40
	abstract boolean refreshable();
40
	public abstract boolean refreshable();
41 41
	
42 42
	// subclass must define doRefresh.  It is only called from the refresh() method.
43 43
	protected abstract void doRefresh() throws ServiceException;
src/edu/ucsb/nceas/metacat/admin/PropertiesAdmin.java
47 47
import edu.ucsb.nceas.utilities.GeneralPropertyException;
48 48
import edu.ucsb.nceas.utilities.PropertiesMetaData;
49 49
import edu.ucsb.nceas.utilities.SortedProperties;
50
import edu.ucsb.nceas.utilities.UtilException;
50 51

  
51 52
/**
52 53
 * Control the display of the main properties configuration page and the 
......
203 204
				String dataDir = PropertyService.getProperty("application.datafilepath");
204 205
				try {
205 206
					FileUtil.createDirectory(dataDir);
206
				} catch (IOException ioe) {
207
				} catch (UtilException ue) {
207 208
					String errorString = "Could not create directory: " + dataDir +
208
						" : " + ioe.getMessage();
209
					" : " + ue.getMessage();
209 210
					logMetacat.error(errorString);
210 211
					validationErrors.add(errorString);
211 212
				}
......
214 215
				String inlineDataDir = PropertyService.getProperty("application.inlinedatafilepath");
215 216
				try {
216 217
					FileUtil.createDirectory(inlineDataDir);
217
				} catch (IOException ioe) {
218
				} catch (UtilException ue) {
218 219
					String errorString = "Could not create directory: " + inlineDataDir +
219
						" : " + ioe.getMessage();
220
						" : " + ue.getMessage();
220 221
					logMetacat.error(errorString);
221 222
					validationErrors.add(errorString);
222 223
				}
......
225 226
				String documentfilepath = PropertyService.getProperty("application.documentfilepath");
226 227
				try {
227 228
					FileUtil.createDirectory(documentfilepath);
228
				} catch (IOException ioe) {	
229
				} catch (UtilException ue) {	
229 230
					String errorString = "Could not create directory: " + documentfilepath +
230
						" : " + ioe.getMessage();
231
						" : " + ue.getMessage();
231 232
					logMetacat.error(errorString);
232 233
					validationErrors.add(errorString);
233 234
				}
......
236 237
				String tempDir = PropertyService.getProperty("application.tempDir");
237 238
				try {
238 239
					FileUtil.createDirectory(tempDir);
239
				} catch (IOException ioe) {		
240
				} catch (UtilException ue) {		
240 241
					String errorString = "Could not create directory: " + tempDir +
241
						" : " + ioe.getMessage();
242
						" : " + ue.getMessage();
242 243
					logMetacat.error(errorString);
243 244
					validationErrors.add(errorString);
244 245
				}
......
247 248
				String replLogDir = PropertyService.getProperty("replication.logdir");
248 249
				try {
249 250
					FileUtil.createDirectory(replLogDir);
250
				} catch (IOException ioe) {		
251
				} catch (UtilException ue) {		
251 252
					String errorString = "Could not create directory: " + replLogDir +
252
						" : " + ioe.getMessage();
253
						" : " + ue.getMessage();
253 254
					logMetacat.error(errorString);
254 255
					validationErrors.add(errorString);
255 256
				}
src/edu/ucsb/nceas/metacat/admin/BackupAdmin.java
38 38
import edu.ucsb.nceas.metacat.service.PropertyService;
39 39
import edu.ucsb.nceas.metacat.service.ServiceException;
40 40
import edu.ucsb.nceas.metacat.service.ServiceService;
41
import edu.ucsb.nceas.metacat.util.MetacatUtilException;
41 42
import edu.ucsb.nceas.metacat.util.RequestUtil;
42 43
import edu.ucsb.nceas.metacat.util.SystemUtil;
43 44
import edu.ucsb.nceas.utilities.FileUtil;
......
119 120
			} catch (ServletException se) {
120 121
				throw new AdminException("Problem forwarding request while "
121 122
						+ "initializing backup configuration page: " + se.getMessage());
122
			} catch (UtilException ue) {
123
			} catch (MetacatUtilException mue) {
123 124
				throw new AdminException("Problem discovering backup directory while "
124
						+ "initializing backup configuration page: " + ue.getMessage());
125
						+ "initializing backup configuration page: " + mue.getMessage());
125 126
			}
126 127
		} else {
127 128
			// The configuration form is being submitted and needs to be
......
151 152
					
152 153
					ServiceService.refreshService("SkinPropertyService");
153 154
					SystemUtil.storeExternalDirLocation(backupDir);
154
				} catch (IOException ue) {
155
				} catch (UtilException ue) {
155 156
					String errorMessage = "Could not create directory: " + hiddenBackupDir
156 157
							+ " : " + ue.getMessage() + ". Please try again";
157 158
					processingErrors.add(errorMessage);
src/edu/ucsb/nceas/metacat/ErrorHandledException.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: An Exception thrown when an error occurs because an 
4
 *             AccessionNumber was invalid or used incorrectly
5
 *  Copyright: 2009 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Michael Daigle
8
 *
9
 *   '$Author: daigle $'
10
 *     '$Date: 2008-07-06 21:25:34 -0700 (Sun, 06 Jul 2008) $'
11
 * '$Revision: 4080 $'
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
/**
31
 * Exception thrown when an error occurs in a configuration administrative
32
 * class
33
 */
34
public class ErrorHandledException extends Exception {
35

  
36
	private static final long serialVersionUID = 6155049982476516738L;
37

  
38
	/**
39
	 * Create a new AdminException.
40
	 *
41
	 * @param message The error or warning message.
42
	 */
43
	public ErrorHandledException(String message) {
44
		super(message);
45
	}
46
}
0 47

  

Also available in: Unified diff