1 |
5211
|
jones
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
5805
|
berkley
|
* Copyright: 2011 Regents of the University of California and the
|
4 |
5211
|
jones
|
* National Center for Ecological Analysis and Synthesis
|
5 |
|
|
*
|
6 |
|
|
* '$Author: Serhan AKIN $'
|
7 |
|
|
* '$Date: 2009-06-13 15:28:13 +0300 $'
|
8 |
|
|
*
|
9 |
|
|
* This program is free software; you can redistribute it and/or modify
|
10 |
|
|
* it under the terms of the GNU General Public License as published by
|
11 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
12 |
|
|
* (at your option) any later version.
|
13 |
|
|
*
|
14 |
|
|
* This program is distributed in the hope that it will be useful,
|
15 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17 |
|
|
* GNU General Public License for more details.
|
18 |
|
|
*
|
19 |
|
|
* You should have received a copy of the GNU General Public License
|
20 |
|
|
* along with this program; if not, write to the Free Software
|
21 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
22 |
|
|
*/
|
23 |
|
|
package edu.ucsb.nceas.metacat.restservice;
|
24 |
|
|
|
25 |
6130
|
leinfelder
|
import java.io.File;
|
26 |
|
|
import java.io.IOException;
|
27 |
|
|
import java.io.InputStream;
|
28 |
|
|
import java.io.OutputStream;
|
29 |
|
|
import java.io.PrintWriter;
|
30 |
9297
|
tao
|
import java.io.UnsupportedEncodingException;
|
31 |
|
|
import java.net.URLDecoder;
|
32 |
6130
|
leinfelder
|
import java.util.Enumeration;
|
33 |
|
|
import java.util.Hashtable;
|
34 |
6269
|
leinfelder
|
import java.util.Iterator;
|
35 |
|
|
import java.util.List;
|
36 |
|
|
import java.util.Map;
|
37 |
6130
|
leinfelder
|
import java.util.Timer;
|
38 |
5211
|
jones
|
|
39 |
|
|
import javax.servlet.ServletContext;
|
40 |
|
|
import javax.servlet.http.HttpServletRequest;
|
41 |
|
|
import javax.servlet.http.HttpServletResponse;
|
42 |
6067
|
rnahf
|
|
43 |
6269
|
leinfelder
|
import org.apache.commons.fileupload.FileUploadException;
|
44 |
5299
|
jones
|
import org.apache.commons.io.IOUtils;
|
45 |
5211
|
jones
|
import org.apache.log4j.Logger;
|
46 |
6269
|
leinfelder
|
import org.dataone.mimemultipart.MultipartRequest;
|
47 |
|
|
import org.dataone.mimemultipart.MultipartRequestResolver;
|
48 |
7737
|
leinfelder
|
import org.dataone.portal.PortalCertificateManager;
|
49 |
5299
|
jones
|
import org.dataone.service.exceptions.BaseException;
|
50 |
6269
|
leinfelder
|
import org.dataone.service.exceptions.InvalidRequest;
|
51 |
|
|
import org.dataone.service.exceptions.ServiceFailure;
|
52 |
7773
|
leinfelder
|
import org.dataone.service.types.v1.Group;
|
53 |
|
|
import org.dataone.service.types.v1.Person;
|
54 |
6366
|
leinfelder
|
import org.dataone.service.types.v1.Session;
|
55 |
7773
|
leinfelder
|
import org.dataone.service.types.v1.Subject;
|
56 |
|
|
import org.dataone.service.types.v1.SubjectInfo;
|
57 |
5211
|
jones
|
|
58 |
|
|
import edu.ucsb.nceas.metacat.MetacatHandler;
|
59 |
5637
|
berkley
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
60 |
7778
|
leinfelder
|
import edu.ucsb.nceas.metacat.service.SessionService;
|
61 |
7773
|
leinfelder
|
import edu.ucsb.nceas.metacat.util.RequestUtil;
|
62 |
|
|
import edu.ucsb.nceas.metacat.util.SessionData;
|
63 |
5637
|
berkley
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
64 |
5211
|
jones
|
/**
|
65 |
|
|
*
|
66 |
6268
|
leinfelder
|
* Base class for handling D1 REST calls in Metacat
|
67 |
5211
|
jones
|
*
|
68 |
6268
|
leinfelder
|
* @author leinfelder
|
69 |
5211
|
jones
|
*/
|
70 |
6267
|
leinfelder
|
public class D1ResourceHandler {
|
71 |
5211
|
jones
|
|
72 |
|
|
/**HTTP Verb GET*/
|
73 |
|
|
public static final byte GET = 1;
|
74 |
|
|
/**HTTP Verb POST*/
|
75 |
|
|
public static final byte POST = 2;
|
76 |
|
|
/**HTTP Verb PUT*/
|
77 |
|
|
public static final byte PUT = 3;
|
78 |
|
|
/**HTTP Verb DELETE*/
|
79 |
|
|
public static final byte DELETE = 4;
|
80 |
5651
|
berkley
|
/**HTTP Verb HEAD*/
|
81 |
|
|
public static final byte HEAD = 5;
|
82 |
5211
|
jones
|
|
83 |
9094
|
jones
|
/** Maximum size of uploads, defaults to 1GB if not set in property file */
|
84 |
9099
|
jones
|
protected static int MAX_UPLOAD_SIZE = 1000000000;
|
85 |
9094
|
jones
|
|
86 |
5211
|
jones
|
/*
|
87 |
|
|
* API Resources
|
88 |
|
|
*/
|
89 |
6271
|
leinfelder
|
protected static final String RESOURCE_BASE_URL = "d1";
|
90 |
|
|
|
91 |
6247
|
leinfelder
|
protected static final String RESOURCE_OBJECTS = "object";
|
92 |
|
|
protected static final String RESOURCE_META = "meta";
|
93 |
|
|
protected static final String RESOURCE_LOG = "log";
|
94 |
6249
|
leinfelder
|
|
95 |
7417
|
leinfelder
|
protected static final String RESOURCE_QUERY = "query";
|
96 |
|
|
|
97 |
6249
|
leinfelder
|
protected static final String RESOURCE_IS_AUTHORIZED = "isAuthorized";
|
98 |
|
|
protected static final String RESOURCE_ACCESS_RULES = "accessRules";
|
99 |
9148
|
tao
|
|
100 |
|
|
protected static final String RESOURCE_VIEWS = "views";
|
101 |
5211
|
jones
|
|
102 |
6590
|
cjones
|
|
103 |
5211
|
jones
|
/*
|
104 |
|
|
* API Functions used as URL parameters
|
105 |
|
|
*/
|
106 |
6247
|
leinfelder
|
protected static final String FUNCTION_NAME_INSERT = "insert";
|
107 |
|
|
protected static final String FUNCTION_NAME_UPDATE = "update";
|
108 |
5414
|
berkley
|
|
109 |
6247
|
leinfelder
|
protected ServletContext servletContext;
|
110 |
|
|
protected Logger logMetacat;
|
111 |
|
|
protected MetacatHandler handler;
|
112 |
|
|
protected HttpServletRequest request;
|
113 |
|
|
protected HttpServletResponse response;
|
114 |
5211
|
jones
|
|
115 |
6247
|
leinfelder
|
protected Hashtable<String, String[]> params;
|
116 |
6269
|
leinfelder
|
protected Map<String, List<String>> multipartparams;
|
117 |
6244
|
leinfelder
|
|
118 |
|
|
// D1 certificate-based authentication
|
119 |
6247
|
leinfelder
|
protected Session session;
|
120 |
5211
|
jones
|
|
121 |
|
|
/**Initializes new instance by setting servlet context,request and response*/
|
122 |
6267
|
leinfelder
|
public D1ResourceHandler(ServletContext servletContext,
|
123 |
5211
|
jones
|
HttpServletRequest request, HttpServletResponse response) {
|
124 |
|
|
this.servletContext = servletContext;
|
125 |
|
|
this.request = request;
|
126 |
|
|
this.response = response;
|
127 |
9094
|
jones
|
logMetacat = Logger.getLogger(D1ResourceHandler.class);
|
128 |
|
|
try {
|
129 |
|
|
MAX_UPLOAD_SIZE = Integer.parseInt(PropertyService.getProperty("dataone.max_upload_size"));
|
130 |
|
|
} catch (PropertyNotFoundException e) {
|
131 |
|
|
// Just use our default as no max size is set in the properties file
|
132 |
|
|
logMetacat.warn("Property not found: " + "dataone.max_upload_size");
|
133 |
|
|
}
|
134 |
|
|
|
135 |
5211
|
jones
|
}
|
136 |
|
|
|
137 |
|
|
/**
|
138 |
6268
|
leinfelder
|
* This function is called from REST API servlet and handles each request
|
139 |
5211
|
jones
|
*
|
140 |
|
|
* @param httpVerb (GET, POST, PUT or DELETE)
|
141 |
|
|
*/
|
142 |
|
|
public void handle(byte httpVerb) {
|
143 |
6267
|
leinfelder
|
logMetacat = Logger.getLogger(D1ResourceHandler.class);
|
144 |
5211
|
jones
|
try {
|
145 |
6268
|
leinfelder
|
|
146 |
9182
|
leinfelder
|
// first try the usual methods
|
147 |
|
|
session = PortalCertificateManager.getInstance().getSession(request);
|
148 |
|
|
|
149 |
7773
|
leinfelder
|
// last resort, check for Metacat sessionid
|
150 |
|
|
if (session == null) {
|
151 |
|
|
SessionData sessionData = RequestUtil.getSessionData(request);
|
152 |
|
|
if (sessionData != null) {
|
153 |
7778
|
leinfelder
|
// is it not the public session?
|
154 |
|
|
if (!SessionService.getInstance().getPublicSession().getUserName().equals(sessionData.getUserName())) {
|
155 |
|
|
session = new Session();
|
156 |
|
|
String userName = sessionData.getUserName();
|
157 |
|
|
String[] groupNames = sessionData.getGroupNames();
|
158 |
|
|
Subject userSubject = new Subject();
|
159 |
|
|
userSubject.setValue(userName);
|
160 |
|
|
session.setSubject(userSubject);
|
161 |
|
|
SubjectInfo subjectInfo = new SubjectInfo();
|
162 |
|
|
Person person = new Person();
|
163 |
|
|
person.setSubject(userSubject);
|
164 |
|
|
if (groupNames != null && groupNames.length > 0) {
|
165 |
|
|
for (String groupName: groupNames) {
|
166 |
|
|
Group group = new Group();
|
167 |
|
|
group.setGroupName(groupName);
|
168 |
|
|
Subject groupSubject = new Subject();
|
169 |
|
|
groupSubject.setValue(groupName);
|
170 |
|
|
group.setSubject(groupSubject);
|
171 |
|
|
subjectInfo.addGroup(group);
|
172 |
|
|
person.addIsMemberOf(groupSubject);
|
173 |
|
|
}
|
174 |
7773
|
leinfelder
|
}
|
175 |
7778
|
leinfelder
|
subjectInfo.addPerson(person);
|
176 |
|
|
session.setSubjectInfo(subjectInfo);
|
177 |
7773
|
leinfelder
|
}
|
178 |
|
|
}
|
179 |
|
|
}
|
180 |
|
|
|
181 |
6268
|
leinfelder
|
// initialize the parameters
|
182 |
|
|
params = new Hashtable<String, String[]>();
|
183 |
|
|
initParams();
|
184 |
5211
|
jones
|
|
185 |
6268
|
leinfelder
|
// create the handler for interacting with Metacat
|
186 |
|
|
Timer timer = new Timer();
|
187 |
|
|
handler = new MetacatHandler(timer);
|
188 |
5211
|
jones
|
|
189 |
|
|
} catch (Exception e) {
|
190 |
6268
|
leinfelder
|
// TODO: more D1 structure when failing here
|
191 |
|
|
response.setStatus(400);
|
192 |
|
|
printError("Incorrect resource!", response);
|
193 |
|
|
logMetacat.error(e.getClass() + ": " + e.getMessage(), e);
|
194 |
5211
|
jones
|
}
|
195 |
|
|
}
|
196 |
6994
|
leinfelder
|
|
197 |
|
|
/**
|
198 |
|
|
* subclasses should provide a more useful implementation
|
199 |
|
|
* @return
|
200 |
|
|
*/
|
201 |
|
|
protected boolean isD1Enabled() {
|
202 |
|
|
|
203 |
|
|
return true;
|
204 |
|
|
}
|
205 |
5374
|
berkley
|
|
206 |
6514
|
leinfelder
|
protected String parseTrailing(String resource, String token) {
|
207 |
|
|
// get the rest
|
208 |
|
|
String extra = null;
|
209 |
|
|
if (resource.indexOf(token) != -1) {
|
210 |
|
|
// what comes after the token?
|
211 |
|
|
extra = resource.substring(resource.indexOf(token) + token.length());
|
212 |
|
|
// remove the slash
|
213 |
|
|
if (extra.startsWith("/")) {
|
214 |
|
|
extra = extra.substring(1);
|
215 |
|
|
}
|
216 |
|
|
// is there anything left?
|
217 |
|
|
if (extra.length() == 0) {
|
218 |
|
|
extra = null;
|
219 |
|
|
}
|
220 |
|
|
}
|
221 |
|
|
return extra;
|
222 |
|
|
}
|
223 |
6590
|
cjones
|
|
224 |
|
|
/**
|
225 |
7092
|
cjones
|
* Parse string parameters from the mime multipart entity of the request.
|
226 |
|
|
* Populates the multipartparams map
|
227 |
|
|
*
|
228 |
|
|
* @throws IOException
|
229 |
|
|
* @throws FileUploadException
|
230 |
|
|
* @throws Exception
|
231 |
|
|
*/
|
232 |
|
|
protected void collectMultipartParams()
|
233 |
|
|
throws IOException, FileUploadException, Exception {
|
234 |
|
|
|
235 |
|
|
File tmpDir = getTempDirectory();
|
236 |
|
|
MultipartRequest mr = null;
|
237 |
|
|
|
238 |
|
|
// Read the incoming data from its Mime Multipart encoding
|
239 |
|
|
logMetacat.debug("Parsing rights holder info from the mime multipart entity");
|
240 |
|
|
|
241 |
|
|
// handle MMP inputs
|
242 |
|
|
MultipartRequestResolver mrr =
|
243 |
9094
|
jones
|
new MultipartRequestResolver(tmpDir.getAbsolutePath(), MAX_UPLOAD_SIZE, 0);
|
244 |
7092
|
cjones
|
|
245 |
|
|
mr = mrr.resolveMultipart(request);
|
246 |
|
|
logMetacat.debug("Resolved the rights holder info from the mime multipart entity.");
|
247 |
|
|
|
248 |
|
|
// we only have params in this MMP entity
|
249 |
|
|
multipartparams = mr.getMultipartParameters();
|
250 |
|
|
|
251 |
|
|
}
|
252 |
6793
|
cjones
|
|
253 |
|
|
/**
|
254 |
6972
|
leinfelder
|
* Process the MMP request that includes files for each param
|
255 |
7092
|
cjones
|
* @return map of param key and the temp file that contains the encoded information
|
256 |
6972
|
leinfelder
|
* @throws ServiceFailure
|
257 |
|
|
* @throws InvalidRequest
|
258 |
|
|
*/
|
259 |
6590
|
cjones
|
protected Map<String, File> collectMultipartFiles()
|
260 |
|
|
throws ServiceFailure, InvalidRequest {
|
261 |
9094
|
jones
|
|
262 |
6269
|
leinfelder
|
// Read the incoming data from its Mime Multipart encoding
|
263 |
|
|
logMetacat.debug("Disassembling MIME multipart form");
|
264 |
|
|
|
265 |
|
|
// handle MMP inputs
|
266 |
|
|
File tmpDir = getTempDirectory();
|
267 |
6272
|
leinfelder
|
logMetacat.debug("temp dir: " + tmpDir.getAbsolutePath());
|
268 |
6269
|
leinfelder
|
MultipartRequestResolver mrr =
|
269 |
9094
|
jones
|
new MultipartRequestResolver(tmpDir.getAbsolutePath(), MAX_UPLOAD_SIZE, 0);
|
270 |
6269
|
leinfelder
|
MultipartRequest mr = null;
|
271 |
7092
|
cjones
|
try {
|
272 |
|
|
mr = mrr.resolveMultipart(request);
|
273 |
|
|
|
274 |
|
|
} catch (Exception e) {
|
275 |
|
|
throw new ServiceFailure("1202",
|
276 |
|
|
"Could not resolve multipart files: " + e.getMessage());
|
277 |
|
|
}
|
278 |
6272
|
leinfelder
|
logMetacat.debug("resolved multipart request");
|
279 |
6269
|
leinfelder
|
Map<String, File> files = mr.getMultipartFiles();
|
280 |
|
|
if (files == null) {
|
281 |
6972
|
leinfelder
|
throw new ServiceFailure("1202", "no multipart files found");
|
282 |
6269
|
leinfelder
|
}
|
283 |
6272
|
leinfelder
|
logMetacat.debug("got multipart files");
|
284 |
6269
|
leinfelder
|
|
285 |
|
|
if (files.keySet() == null) {
|
286 |
6272
|
leinfelder
|
logMetacat.error("No file keys in MMP request.");
|
287 |
6972
|
leinfelder
|
throw new ServiceFailure("1202", "No file keys found in MMP.");
|
288 |
6269
|
leinfelder
|
}
|
289 |
6995
|
leinfelder
|
|
290 |
|
|
multipartparams = mr.getMultipartParameters();
|
291 |
6269
|
leinfelder
|
|
292 |
7092
|
cjones
|
// for logging purposes, dump out the key-value pairs that constitute the request
|
293 |
|
|
// 3 types exist: request params, multipart params, and multipart files
|
294 |
6972
|
leinfelder
|
if (logMetacat.isDebugEnabled()) {
|
295 |
|
|
Iterator<String> it = files.keySet().iterator();
|
296 |
|
|
logMetacat.debug("iterating through files");
|
297 |
|
|
while (it.hasNext()) {
|
298 |
|
|
String key = it.next();
|
299 |
|
|
logMetacat.debug("files key: " + key);
|
300 |
|
|
logMetacat.debug("files value: " + files.get(key));
|
301 |
|
|
}
|
302 |
|
|
|
303 |
|
|
it = multipartparams.keySet().iterator();
|
304 |
|
|
logMetacat.debug("iterating through multipartparams");
|
305 |
|
|
while (it.hasNext()) {
|
306 |
|
|
String key = (String)it.next();
|
307 |
|
|
logMetacat.debug("multipartparams key: " + key);
|
308 |
|
|
logMetacat.debug("multipartparams value: " + multipartparams.get(key));
|
309 |
|
|
}
|
310 |
|
|
|
311 |
|
|
it = params.keySet().iterator();
|
312 |
|
|
logMetacat.debug("iterating through params");
|
313 |
|
|
while (it.hasNext()) {
|
314 |
|
|
String key = (String)it.next();
|
315 |
|
|
logMetacat.debug("param key: " + key);
|
316 |
|
|
logMetacat.debug("param value: " + params.get(key));
|
317 |
|
|
}
|
318 |
|
|
logMetacat.debug("done iterating the request...");
|
319 |
6269
|
leinfelder
|
}
|
320 |
|
|
|
321 |
|
|
return files;
|
322 |
|
|
}
|
323 |
|
|
|
324 |
6140
|
cjones
|
/**
|
325 |
6590
|
cjones
|
* copies request parameters to a hashtable which is given as argument to
|
326 |
|
|
* native metacathandler functions
|
327 |
5374
|
berkley
|
*/
|
328 |
6247
|
leinfelder
|
protected void initParams() {
|
329 |
5211
|
jones
|
|
330 |
5374
|
berkley
|
String name = null;
|
331 |
|
|
String[] value = null;
|
332 |
|
|
Enumeration paramlist = request.getParameterNames();
|
333 |
|
|
while (paramlist.hasMoreElements()) {
|
334 |
|
|
name = (String) paramlist.nextElement();
|
335 |
|
|
value = request.getParameterValues(name);
|
336 |
|
|
params.put(name, value);
|
337 |
|
|
}
|
338 |
|
|
}
|
339 |
6660
|
leinfelder
|
|
340 |
|
|
/**
|
341 |
|
|
* Collect the multipart params from the request
|
342 |
|
|
* @throws Exception
|
343 |
|
|
*/
|
344 |
|
|
protected void initMultipartParams() throws Exception {
|
345 |
|
|
|
346 |
|
|
// Read the incoming data from its Mime Multipart encoding
|
347 |
|
|
logMetacat.debug("Disassembling MIME multipart form");
|
348 |
|
|
|
349 |
|
|
// handle MMP inputs
|
350 |
|
|
File tmpDir = getTempDirectory();
|
351 |
|
|
logMetacat.debug("temp dir: " + tmpDir.getAbsolutePath());
|
352 |
|
|
MultipartRequestResolver mrr =
|
353 |
9094
|
jones
|
new MultipartRequestResolver(tmpDir.getAbsolutePath(), MAX_UPLOAD_SIZE, 0);
|
354 |
6660
|
leinfelder
|
MultipartRequest mr = mrr.resolveMultipart(request);
|
355 |
|
|
|
356 |
|
|
multipartparams = mr.getMultipartParameters();
|
357 |
|
|
}
|
358 |
6469
|
leinfelder
|
|
359 |
5211
|
jones
|
/**
|
360 |
5808
|
berkley
|
* locate the boundary marker for an MMP
|
361 |
5462
|
berkley
|
* @param is
|
362 |
|
|
* @return
|
363 |
5808
|
berkley
|
* @throws IOException
|
364 |
5462
|
berkley
|
*/
|
365 |
5639
|
berkley
|
protected static String[] findBoundaryString(InputStream is)
|
366 |
6268
|
leinfelder
|
throws IOException {
|
367 |
5639
|
berkley
|
String[] endResult = new String[2];
|
368 |
5637
|
berkley
|
String boundary = "";
|
369 |
5639
|
berkley
|
String searchString = "boundary=";
|
370 |
5637
|
berkley
|
byte[] b = new byte[1024];
|
371 |
5639
|
berkley
|
int numbytes = is.read(b, 0, 1024);
|
372 |
5826
|
berkley
|
|
373 |
5639
|
berkley
|
while(numbytes != -1)
|
374 |
5636
|
berkley
|
{
|
375 |
5639
|
berkley
|
String s = new String(b, 0, numbytes);
|
376 |
5826
|
berkley
|
int searchStringIndex = s.indexOf(searchString);
|
377 |
5639
|
berkley
|
|
378 |
|
|
if(s.indexOf("\"", searchStringIndex + searchString.length() + 1) == -1)
|
379 |
|
|
{ //the end of the boundary is in the next byte array
|
380 |
|
|
boundary = s.substring(searchStringIndex + searchString.length() + 1, s.length());
|
381 |
|
|
}
|
382 |
|
|
else if(!boundary.startsWith("--"))
|
383 |
|
|
{ //we can read the whole boundary from this byte array
|
384 |
|
|
boundary = s.substring(searchStringIndex + searchString.length() + 1,
|
385 |
|
|
s.indexOf("\"", searchStringIndex + searchString.length() + 1));
|
386 |
|
|
boundary = "--" + boundary;
|
387 |
|
|
endResult[0] = boundary;
|
388 |
|
|
endResult[1] = s.substring(s.indexOf("\"", searchStringIndex + searchString.length() + 1) + 1,
|
389 |
|
|
s.length());
|
390 |
|
|
break;
|
391 |
|
|
}
|
392 |
|
|
else
|
393 |
|
|
{ //we're now reading the 2nd byte array to get the rest of the boundary
|
394 |
|
|
searchString = "\"";
|
395 |
|
|
searchStringIndex = s.indexOf(searchString);
|
396 |
|
|
boundary += s.substring(0, searchStringIndex);
|
397 |
|
|
boundary = "--" + boundary;
|
398 |
|
|
endResult[0] = boundary;
|
399 |
|
|
endResult[1] = s.substring(s.indexOf("\"", searchStringIndex + searchString.length() + 1) + 1,
|
400 |
|
|
s.length());
|
401 |
|
|
break;
|
402 |
|
|
}
|
403 |
5637
|
berkley
|
}
|
404 |
5639
|
berkley
|
return endResult;
|
405 |
|
|
}
|
406 |
|
|
|
407 |
5808
|
berkley
|
/**
|
408 |
5838
|
berkley
|
* return the directory where temp files are stored
|
409 |
|
|
* @return
|
410 |
|
|
*/
|
411 |
6248
|
leinfelder
|
protected static File getTempDirectory()
|
412 |
5838
|
berkley
|
{
|
413 |
|
|
File tmpDir = null;
|
414 |
6267
|
leinfelder
|
Logger logMetacat = Logger.getLogger(D1ResourceHandler.class);
|
415 |
6268
|
leinfelder
|
try {
|
416 |
5639
|
berkley
|
tmpDir = new File(PropertyService.getProperty("application.tempDir"));
|
417 |
|
|
}
|
418 |
6268
|
leinfelder
|
catch(PropertyNotFoundException pnfe) {
|
419 |
6267
|
leinfelder
|
logMetacat.error("D1ResourceHandler.writeMMPPartstoFiles: " +
|
420 |
5639
|
berkley
|
"application.tmpDir not found. Using /tmp instead.");
|
421 |
|
|
tmpDir = new File("/tmp");
|
422 |
|
|
}
|
423 |
5838
|
berkley
|
return tmpDir;
|
424 |
5639
|
berkley
|
}
|
425 |
|
|
|
426 |
5637
|
berkley
|
/**
|
427 |
5211
|
jones
|
* Prints xml response
|
428 |
|
|
* @param message Message to be displayed
|
429 |
|
|
* @param response Servlet response that xml message will be printed
|
430 |
|
|
* */
|
431 |
6247
|
leinfelder
|
protected void printError(String message, HttpServletResponse response) {
|
432 |
5211
|
jones
|
try {
|
433 |
6267
|
leinfelder
|
logMetacat.error("D1ResourceHandler: Printing error to servlet response: " + message);
|
434 |
5211
|
jones
|
PrintWriter out = response.getWriter();
|
435 |
|
|
response.setContentType("text/xml");
|
436 |
|
|
out.println("<?xml version=\"1.0\"?>");
|
437 |
|
|
out.println("<error>");
|
438 |
|
|
out.println(message);
|
439 |
|
|
out.println("</error>");
|
440 |
|
|
out.close();
|
441 |
|
|
} catch (IOException e) {
|
442 |
|
|
e.printStackTrace();
|
443 |
|
|
}
|
444 |
|
|
}
|
445 |
5370
|
berkley
|
|
446 |
5692
|
berkley
|
/**
|
447 |
|
|
* serialize a D1 exception using jibx
|
448 |
|
|
* @param e
|
449 |
|
|
* @param out
|
450 |
|
|
*/
|
451 |
6247
|
leinfelder
|
protected void serializeException(BaseException e, OutputStream out) {
|
452 |
5319
|
jones
|
// TODO: Use content negotiation to determine which return format to use
|
453 |
|
|
response.setContentType("text/xml");
|
454 |
|
|
response.setStatus(e.getCode());
|
455 |
5512
|
berkley
|
|
456 |
10207
|
tao
|
logMetacat.error("D1ResourceHandler: Serializing exception with code " + e.getCode() + ": " + e.getMessage(), e);
|
457 |
|
|
//e.printStackTrace();
|
458 |
5512
|
berkley
|
|
459 |
5319
|
jones
|
try {
|
460 |
|
|
IOUtils.write(e.serialize(BaseException.FMT_XML), out);
|
461 |
|
|
} catch (IOException e1) {
|
462 |
|
|
logMetacat.error("Error writing exception to stream. "
|
463 |
|
|
+ e1.getMessage());
|
464 |
|
|
}
|
465 |
|
|
}
|
466 |
9297
|
tao
|
|
467 |
|
|
/**
|
468 |
|
|
* A method to decode the given string which is a part of a uri.
|
469 |
|
|
* The default encoding is utf-8. If the utf-8 is not support in this system, the default one in the systme will be used.
|
470 |
|
|
* @param s
|
471 |
|
|
* @return null if the given string is null
|
472 |
|
|
*/
|
473 |
|
|
public static String decode(String s) {
|
474 |
|
|
String result = null;
|
475 |
|
|
if(s != null) {
|
476 |
|
|
try
|
477 |
|
|
{
|
478 |
|
|
result = URLDecoder.decode(s, "UTF-8");
|
479 |
|
|
}
|
480 |
|
|
catch (UnsupportedEncodingException e)
|
481 |
|
|
{
|
482 |
|
|
result = URLDecoder.decode(s);
|
483 |
|
|
}
|
484 |
|
|
System.out.println("After decoded: " + result);
|
485 |
|
|
}
|
486 |
|
|
|
487 |
|
|
return result;
|
488 |
|
|
}
|
489 |
5211
|
jones
|
}
|