1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that implements administrative 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-06-12 16:41:43 -0700 (Fri, 12 Jun 2009) $'
|
10
|
* '$Revision: 4951 $'
|
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.util;
|
28
|
|
29
|
import java.io.FileInputStream;
|
30
|
import java.io.FileNotFoundException;
|
31
|
import java.io.IOException;
|
32
|
import java.io.InputStream;
|
33
|
import java.io.OutputStream;
|
34
|
|
35
|
import javax.servlet.http.HttpServletRequest;
|
36
|
import javax.servlet.http.HttpServletResponse;
|
37
|
|
38
|
import org.apache.log4j.Logger;
|
39
|
|
40
|
import edu.ucsb.nceas.utilities.FileUtil;
|
41
|
|
42
|
public class ResponseUtil {
|
43
|
|
44
|
private static Logger logMetacat = Logger.getLogger(ResponseUtil.class);
|
45
|
|
46
|
private static int DEFAULT_BUFFER_SIZE = 4 * 1024; // 4K buffer
|
47
|
|
48
|
/**
|
49
|
* private constructor - all methods are static so there is no
|
50
|
* no need to instantiate.
|
51
|
*/
|
52
|
private ResponseUtil() {}
|
53
|
|
54
|
/**
|
55
|
* Redirect a response.
|
56
|
*
|
57
|
* @param response
|
58
|
* that is to be redirected
|
59
|
* @param destination
|
60
|
* the context-relative URL to which the request is forwarded
|
61
|
*/
|
62
|
public static void redirectResponse(HttpServletRequest request,
|
63
|
HttpServletResponse response, String destination) throws MetacatUtilException {
|
64
|
try {
|
65
|
logMetacat.debug("Redirecting response to " + request.getContextPath() + destination);
|
66
|
response.sendRedirect(request.getContextPath() + destination);
|
67
|
} catch (IOException ioe) {
|
68
|
throw new MetacatUtilException("I/O error when redirecting response to: " + destination);
|
69
|
}
|
70
|
}
|
71
|
|
72
|
public static void writeFileToOutput(HttpServletResponse response, String fileDir, String fileName)
|
73
|
throws MetacatUtilException {
|
74
|
|
75
|
writeFileToOutput(response, fileDir, fileName, DEFAULT_BUFFER_SIZE);
|
76
|
}
|
77
|
|
78
|
public static void writeFileToOutput(HttpServletResponse response, String fileDir, String fileName, int bufferSize)
|
79
|
throws MetacatUtilException {
|
80
|
String filePath = "";
|
81
|
try {
|
82
|
filePath = fileDir + FileUtil.getFS() + fileName;
|
83
|
|
84
|
int lastFileSep = fileName.lastIndexOf(FileUtil.getFS());
|
85
|
String shortFileName = fileName.substring(lastFileSep + 1, fileName.length());
|
86
|
response.setHeader("Content-Disposition", "attachment; filename=\"" + shortFileName + "\"");
|
87
|
|
88
|
InputStream inputStream = new FileInputStream(filePath);
|
89
|
OutputStream outputStream = response.getOutputStream();
|
90
|
|
91
|
byte[] byteBuffer = new byte[bufferSize];
|
92
|
|
93
|
int b = 0;
|
94
|
while ((b = inputStream.read(byteBuffer)) != -1) {
|
95
|
outputStream.write(byteBuffer, 0, b);
|
96
|
}
|
97
|
|
98
|
} catch (FileNotFoundException fnfe) {
|
99
|
throw new MetacatUtilException("Error finding file: " + filePath
|
100
|
+ " when writing to output");
|
101
|
} catch (IOException ioe) {
|
102
|
throw new MetacatUtilException("I/O Error when writing: " + filePath
|
103
|
+ " to output");
|
104
|
}
|
105
|
}
|
106
|
}
|