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: jones $'
|
9
|
* '$Date: 2013-10-09 23:52:11 -0700 (Wed, 09 Oct 2013) $'
|
10
|
* '$Revision: 8304 $'
|
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
|
import java.io.PrintWriter;
|
35
|
import java.util.Hashtable;
|
36
|
|
37
|
import javax.servlet.http.HttpServletRequest;
|
38
|
import javax.servlet.http.HttpServletResponse;
|
39
|
|
40
|
import org.apache.commons.io.IOUtils;
|
41
|
import org.apache.log4j.Logger;
|
42
|
|
43
|
import edu.ucsb.nceas.metacat.shared.BaseException;
|
44
|
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
|
45
|
import edu.ucsb.nceas.utilities.FileUtil;
|
46
|
|
47
|
public class ResponseUtil {
|
48
|
|
49
|
// 101XXX - general errors
|
50
|
public static Long GENERAL_UTILITY_ERROR = new Long(101001);
|
51
|
public static Long METACAT_UTILITY_ERROR = new Long(101002);
|
52
|
// 104XXX - property errors
|
53
|
public static Long PROPERTY_NOT_FOUND = new Long(104001);
|
54
|
// 105XXX - permission errors
|
55
|
public static Long NO_READ_PERMISSION = new Long(105001);
|
56
|
// 130XXX - scheduler errors
|
57
|
public static Long SCHEDULE_WORKFLOW_ERROR = new Long(130001);
|
58
|
public static Long UNSCHEDULE_WORKFLOW_ERROR = new Long(130002);
|
59
|
public static Long RESCHEDULE_WORKFLOW_ERROR = new Long(130003);
|
60
|
public static Long GET_SCHEDULED_WORKFLOW_ERROR = new Long(130004);
|
61
|
public static Long DELETE_SCHEDULED_WORKFLOW_ERROR = new Long(130005);
|
62
|
|
63
|
|
64
|
// errorCodes is a lookup table for generic messages for each code. It
|
65
|
// is better to use the sendError() versions that accept an explicit
|
66
|
// error message;
|
67
|
static Hashtable<Long, String> errorCodes = new Hashtable<Long, String>();
|
68
|
static {
|
69
|
errorCodes.put(GENERAL_UTILITY_ERROR, "General utility error");
|
70
|
errorCodes.put(METACAT_UTILITY_ERROR, "Metacat utility error");
|
71
|
errorCodes.put(PROPERTY_NOT_FOUND, "Property not found");
|
72
|
errorCodes.put(NO_READ_PERMISSION, "Read permission denied for user");
|
73
|
errorCodes.put(SCHEDULE_WORKFLOW_ERROR, "Schedule workflow error");
|
74
|
errorCodes.put(UNSCHEDULE_WORKFLOW_ERROR, "Unschedule workflow error");
|
75
|
errorCodes.put(RESCHEDULE_WORKFLOW_ERROR, "Reschedule workflow error");
|
76
|
errorCodes.put(GET_SCHEDULED_WORKFLOW_ERROR, "Get scheduled workflow error");
|
77
|
errorCodes.put(DELETE_SCHEDULED_WORKFLOW_ERROR, "Delete scheduled workflow error");
|
78
|
}
|
79
|
|
80
|
private static Logger logMetacat = Logger.getLogger(ResponseUtil.class);
|
81
|
|
82
|
private static int DEFAULT_BUFFER_SIZE = 4 * 1024; // 4K buffer
|
83
|
|
84
|
/**
|
85
|
* private constructor - all methods are static so there is no
|
86
|
* no need to instantiate.
|
87
|
*/
|
88
|
private ResponseUtil() {}
|
89
|
|
90
|
/**
|
91
|
* Redirect a response.
|
92
|
*
|
93
|
* @param response
|
94
|
* that is to be redirected
|
95
|
* @param destination
|
96
|
* the context-relative URL to which the request is forwarded
|
97
|
*/
|
98
|
public static void redirectResponse(HttpServletRequest request,
|
99
|
HttpServletResponse response, String destination) throws MetacatUtilException {
|
100
|
try {
|
101
|
logMetacat.debug("Redirecting response to " + request.getContextPath() + destination);
|
102
|
response.sendRedirect(request.getContextPath() + destination);
|
103
|
} catch (IOException ioe) {
|
104
|
throw new MetacatUtilException("I/O error when redirecting response to: " + destination);
|
105
|
}
|
106
|
}
|
107
|
|
108
|
public static void writeFileToOutput(HttpServletResponse response, String fileDir, String fileName)
|
109
|
throws MetacatUtilException {
|
110
|
|
111
|
writeFileToOutput(response, fileDir, fileName, DEFAULT_BUFFER_SIZE);
|
112
|
}
|
113
|
|
114
|
public static void writeFileToOutput(HttpServletResponse response, String fileDir, String fileName, int bufferSize)
|
115
|
throws MetacatUtilException {
|
116
|
String filePath = "";
|
117
|
InputStream inputStream = null;
|
118
|
OutputStream outputStream = null;
|
119
|
try {
|
120
|
filePath = fileDir + FileUtil.getFS() + fileName;
|
121
|
|
122
|
int lastFileSep = fileName.lastIndexOf(FileUtil.getFS());
|
123
|
String shortFileName = fileName.substring(lastFileSep + 1, fileName.length());
|
124
|
response.setHeader("Content-Disposition", "attachment; filename=\"" + shortFileName + "\"");
|
125
|
|
126
|
inputStream = new FileInputStream(filePath);
|
127
|
outputStream = response.getOutputStream();
|
128
|
|
129
|
byte[] byteBuffer = new byte[bufferSize];
|
130
|
|
131
|
int b = 0;
|
132
|
while ((b = inputStream.read(byteBuffer)) != -1) {
|
133
|
outputStream.write(byteBuffer, 0, b);
|
134
|
}
|
135
|
outputStream.close();
|
136
|
inputStream.close();
|
137
|
|
138
|
} catch (FileNotFoundException fnfe) {
|
139
|
throw new MetacatUtilException("Error finding file: " + filePath
|
140
|
+ " when writing to output");
|
141
|
} catch (IOException ioe) {
|
142
|
throw new MetacatUtilException("I/O Error when writing: " + filePath
|
143
|
+ " to output");
|
144
|
} finally {
|
145
|
IOUtils.closeQuietly(inputStream);
|
146
|
IOUtils.closeQuietly(outputStream);
|
147
|
}
|
148
|
}
|
149
|
|
150
|
public static void send(HttpServletResponse response, String content) throws ErrorSendingErrorException {
|
151
|
|
152
|
PrintWriter out = null;
|
153
|
try {
|
154
|
out = response.getWriter();
|
155
|
response.setContentType("text/xml");
|
156
|
|
157
|
out.print(content);
|
158
|
|
159
|
} catch (IOException ioe) {
|
160
|
throw new ErrorSendingErrorException("I/O error when sending content: "
|
161
|
+ content + " : " + ioe.getMessage());
|
162
|
} finally {
|
163
|
if (out != null) {
|
164
|
out.close();
|
165
|
}
|
166
|
}
|
167
|
}
|
168
|
|
169
|
public static void sendErrorXML(HttpServletResponse response, Long errorCode,
|
170
|
BaseException be) throws ErrorSendingErrorException {
|
171
|
|
172
|
logMetacat.error(errorCodes.get(errorCode) + " : " + be.getMessage());
|
173
|
|
174
|
PrintWriter out = null;
|
175
|
try {
|
176
|
out = response.getWriter();
|
177
|
response.setContentType("text/xml");
|
178
|
|
179
|
out.println("<?xml version=\"1.0\"?>");
|
180
|
out.println("<error>");
|
181
|
out.println("<code>" + errorCode + "</code>");
|
182
|
out.println("<defaultMessage>" + errorCodes.get(errorCode) + "</defaultMessage>");
|
183
|
out.println("<coreMessage>" + be.getCoreMessage() + "</coreMessage>");
|
184
|
out.println("<chainedMessage>" + be.getMessage() + "</chainedMessage>");
|
185
|
out.println("</error>");
|
186
|
|
187
|
} catch (IOException ioe) {
|
188
|
throw new ErrorSendingErrorException("I/O error when returning error: "
|
189
|
+ errorCode + " : " + ioe.getMessage());
|
190
|
} finally {
|
191
|
if (out != null) {
|
192
|
out.close();
|
193
|
}
|
194
|
}
|
195
|
}
|
196
|
|
197
|
public static void sendErrorXML(HttpServletResponse response, Long errorCode,
|
198
|
String message) throws ErrorSendingErrorException {
|
199
|
|
200
|
logMetacat.error(errorCodes.get(errorCode) + " : " + message);
|
201
|
|
202
|
PrintWriter out = null;
|
203
|
try {
|
204
|
out = response.getWriter();
|
205
|
response.setContentType("text/xml");
|
206
|
|
207
|
out.println("<?xml version=\"1.0\"?>");
|
208
|
out.println("<error>");
|
209
|
out.println("<code>" + errorCode + "</code>");
|
210
|
out.println("<defaultMessage>" + errorCodes.get(errorCode) + "</defaultMessage>");
|
211
|
out.println("<coreMessage>" + message + "</coreMessage>");
|
212
|
out.println("<chainedMessage></chainedMessage>");
|
213
|
out.println("</error>");
|
214
|
|
215
|
} catch (IOException ioe) {
|
216
|
throw new ErrorSendingErrorException("I/O error when returning error: "
|
217
|
+ errorCode + " : " + ioe.getMessage());
|
218
|
} finally {
|
219
|
if (out != null) {
|
220
|
out.close();
|
221
|
}
|
222
|
}
|
223
|
}
|
224
|
|
225
|
public static void sendSuccessXML(HttpServletResponse response, String message)
|
226
|
throws ErrorSendingErrorException {
|
227
|
|
228
|
PrintWriter out = null;
|
229
|
try {
|
230
|
out = response.getWriter();
|
231
|
response.setContentType("text/xml");
|
232
|
|
233
|
out.println("<?xml version=\"1.0\"?>");
|
234
|
out.println("<success>" + message + "</success>");
|
235
|
} catch (IOException ioe) {
|
236
|
throw new ErrorSendingErrorException("I/O error when returning success XML: "
|
237
|
+ message + " : " + ioe.getMessage());
|
238
|
} finally {
|
239
|
if (out != null) {
|
240
|
out.close();
|
241
|
}
|
242
|
}
|
243
|
}
|
244
|
|
245
|
public static boolean isSuccessXML(String message) {
|
246
|
|
247
|
return message.indexOf("<success>") != -1;
|
248
|
}
|
249
|
|
250
|
}
|