Project

General

Profile

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.properties.PropertyService;
38
import edu.ucsb.nceas.metacat.shared.HandlerException;
39
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
40
import edu.ucsb.nceas.metacat.util.ErrorSendingErrorException;
41
import edu.ucsb.nceas.metacat.util.ResponseUtil;
42
import edu.ucsb.nceas.utilities.FileUtil;
43
import edu.ucsb.nceas.utilities.LSIDUtil;
44
import edu.ucsb.nceas.utilities.ParseLSIDException;
45
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
46
import edu.ucsb.nceas.utilities.UtilException;
47

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

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

    
183
}
(3-3/65)