|
1 |
/**
|
|
2 |
* '$RCSfile$'
|
|
3 |
* Copyright: 2011 Regents of the University of California and the
|
|
4 |
* National Center for Ecological Analysis and Synthesis
|
|
5 |
*
|
|
6 |
* '$Author$'
|
|
7 |
* '$Date$'
|
|
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 |
import java.io.ByteArrayInputStream;
|
|
26 |
import java.io.File;
|
|
27 |
import java.io.FileInputStream;
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.io.InputStream;
|
|
30 |
import java.io.OutputStream;
|
|
31 |
import java.io.PrintWriter;
|
|
32 |
import java.io.StringReader;
|
|
33 |
import java.io.UnsupportedEncodingException;
|
|
34 |
import java.text.DateFormat;
|
|
35 |
import java.text.ParseException;
|
|
36 |
import java.text.SimpleDateFormat;
|
|
37 |
import java.util.Date;
|
|
38 |
import java.util.Enumeration;
|
|
39 |
import java.util.Hashtable;
|
|
40 |
import java.util.Iterator;
|
|
41 |
import java.util.List;
|
|
42 |
import java.util.Map;
|
|
43 |
import java.util.Timer;
|
|
44 |
|
|
45 |
import javax.servlet.ServletContext;
|
|
46 |
import javax.servlet.http.HttpServletRequest;
|
|
47 |
import javax.servlet.http.HttpServletResponse;
|
|
48 |
|
|
49 |
import org.apache.commons.fileupload.FileUploadException;
|
|
50 |
import org.apache.commons.io.IOUtils;
|
|
51 |
import org.apache.log4j.Logger;
|
|
52 |
import org.dataone.client.ObjectFormatCache;
|
|
53 |
import org.dataone.client.auth.CertificateManager;
|
|
54 |
import org.dataone.mimemultipart.MultipartRequest;
|
|
55 |
import org.dataone.mimemultipart.MultipartRequestResolver;
|
|
56 |
import org.dataone.service.exceptions.BaseException;
|
|
57 |
import org.dataone.service.exceptions.IdentifierNotUnique;
|
|
58 |
import org.dataone.service.exceptions.InsufficientResources;
|
|
59 |
import org.dataone.service.exceptions.InvalidRequest;
|
|
60 |
import org.dataone.service.exceptions.InvalidSystemMetadata;
|
|
61 |
import org.dataone.service.exceptions.InvalidToken;
|
|
62 |
import org.dataone.service.exceptions.NotAuthorized;
|
|
63 |
import org.dataone.service.exceptions.NotFound;
|
|
64 |
import org.dataone.service.exceptions.NotImplemented;
|
|
65 |
import org.dataone.service.exceptions.ServiceFailure;
|
|
66 |
import org.dataone.service.exceptions.SynchronizationFailed;
|
|
67 |
import org.dataone.service.exceptions.UnsupportedType;
|
|
68 |
import org.dataone.service.types.AccessPolicy;
|
|
69 |
import org.dataone.service.types.Checksum;
|
|
70 |
import org.dataone.service.types.DescribeResponse;
|
|
71 |
import org.dataone.service.types.Event;
|
|
72 |
import org.dataone.service.types.Identifier;
|
|
73 |
import org.dataone.service.types.Log;
|
|
74 |
import org.dataone.service.types.MonitorList;
|
|
75 |
import org.dataone.service.types.Node;
|
|
76 |
import org.dataone.service.types.NodeList;
|
|
77 |
import org.dataone.service.types.NodeReference;
|
|
78 |
import org.dataone.service.types.ObjectFormat;
|
|
79 |
import org.dataone.service.types.ObjectList;
|
|
80 |
import org.dataone.service.types.Permission;
|
|
81 |
import org.dataone.service.types.Subject;
|
|
82 |
import org.dataone.service.types.SystemMetadata;
|
|
83 |
import org.jibx.runtime.JiBXException;
|
|
84 |
|
|
85 |
import edu.ucsb.nceas.metacat.MetacatHandler;
|
|
86 |
import edu.ucsb.nceas.metacat.dataone.MNodeService;
|
|
87 |
|
|
88 |
/**
|
|
89 |
* MN REST service implementation handler
|
|
90 |
*
|
|
91 |
* ******************
|
|
92 |
* MNCore -- DONE
|
|
93 |
* ping() - GET /d1/mn/monitor/ping
|
|
94 |
* log() - GET /d1/mn/log
|
|
95 |
* **getObjectStatistics() - GET /d1/mn/monitor/object
|
|
96 |
* getOperationsStatistics - GET /d1/mn/monitor/event
|
|
97 |
* **getStatus - GET /d1/mn/monitor/status
|
|
98 |
* getCapabilities() - GET /d1/mn/ and /d1/mn/node
|
|
99 |
*
|
|
100 |
* MNRead -- DONE
|
|
101 |
* get() - GET /d1/mn/object/PID
|
|
102 |
* getSystemMetadata() - GET /d1/mn/meta/PID
|
|
103 |
* describe() - HEAD /d1/mn/object/PID
|
|
104 |
* getChecksum() - GET /d1/mn/checksum/PID
|
|
105 |
* listObjects() - GET /d1/mn/object
|
|
106 |
* synchronizationFailed() - POST /d1/mn/error
|
|
107 |
*
|
|
108 |
* MNAuthorization -- DONE
|
|
109 |
* isAuthorized() - GET /d1/mn/isAuthorized/PID
|
|
110 |
* setAccessPolicy() - PUT /d1/mn/accessRules/PID
|
|
111 |
*
|
|
112 |
* MNStorage
|
|
113 |
* create() - POST /d1/mn/object/PID
|
|
114 |
* update() - PUT /d1/mn/object/PID
|
|
115 |
* delete() - DELETE /d1/mn/object/PID
|
|
116 |
*
|
|
117 |
* MNReplication
|
|
118 |
* replicate() - POST /d1/mn/replicate
|
|
119 |
*
|
|
120 |
* ******************
|
|
121 |
* @author leinfelder
|
|
122 |
*
|
|
123 |
*/
|
|
124 |
public class MNResourceHandler extends ResourceHandler{
|
|
125 |
|
|
126 |
// MN-specific API Resources
|
|
127 |
protected static final String RESOURCE_MONITOR = "monitor";
|
|
128 |
protected static final String RESOURCE_REPLICATE = "replicate";
|
|
129 |
protected static final String RESOURCE_NODE = "node";
|
|
130 |
protected static final String RESOURCE_ERROR = "error";
|
|
131 |
|
|
132 |
|
|
133 |
/**
|
|
134 |
* Initializes new instance by setting servlet context,request and response
|
|
135 |
* */
|
|
136 |
public MNResourceHandler(ServletContext servletContext,
|
|
137 |
HttpServletRequest request, HttpServletResponse response) {
|
|
138 |
super(servletContext, request, response);
|
|
139 |
logMetacat = Logger.getLogger(MNResourceHandler.class);
|
|
140 |
}
|
|
141 |
|
|
142 |
/**
|
|
143 |
* This function is called from REST APU servlet and handles each request to the servlet
|
|
144 |
*
|
|
145 |
* @param httpVerb (GET, POST, PUT or DELETE)
|
|
146 |
*/
|
|
147 |
public void handle(byte httpVerb) {
|
|
148 |
logMetacat = Logger.getLogger(MNResourceHandler.class);
|
|
149 |
try {
|
|
150 |
String resourcePrefix = RESOURCE_BASE_URL + "/mn/";
|
|
151 |
String resource = request.getServletPath();
|
|
152 |
|
|
153 |
if (resource.endsWith(resourcePrefix)) {
|
|
154 |
resource = RESOURCE_NODE;
|
|
155 |
}
|
|
156 |
else {
|
|
157 |
// remove prefix
|
|
158 |
resource = resource.substring(resource.indexOf(resourcePrefix) + resourcePrefix.length());
|
|
159 |
resource = resource.trim();
|
|
160 |
}
|
|
161 |
|
|
162 |
System.out.println("handling verb " + httpVerb + " request with resource '" + resource + "'");
|
|
163 |
System.out.println("resource: '" + resource + "'");
|
|
164 |
boolean status = false;
|
|
165 |
|
|
166 |
// load session from certificate in request
|
|
167 |
session = CertificateManager.getInstance().getSession(request);
|
|
168 |
|
|
169 |
if (resource != null) {
|
|
170 |
//resource = request.getServletPath().substring(1);
|
|
171 |
|
|
172 |
params = new Hashtable<String, String[]>();
|
|
173 |
initParams();
|
|
174 |
|
|
175 |
Timer timer = new Timer();
|
|
176 |
handler = new MetacatHandler(timer);
|
|
177 |
|
|
178 |
if (resource.equals(RESOURCE_NODE)) {
|
|
179 |
// node response
|
|
180 |
node();
|
|
181 |
status = true;
|
|
182 |
|
|
183 |
} else if (resource.equals(RESOURCE_ACCESS_RULES)) {
|
|
184 |
if (httpVerb == POST) {
|
|
185 |
// set the access rules
|
|
186 |
setaccess();
|
|
187 |
status = true;
|
|
188 |
System.out.println("done setting access");
|
|
189 |
}
|
|
190 |
} else if (resource.equals(RESOURCE_IS_AUTHORIZED)) {
|
|
191 |
if (httpVerb == GET) {
|
|
192 |
// check the access rules
|
|
193 |
String objectId = request.getPathInfo();
|
|
194 |
if (objectId != null && objectId.length() > 1) {
|
|
195 |
objectId = request.getPathInfo().substring(1);
|
|
196 |
}
|
|
197 |
isAuthorized(objectId);
|
|
198 |
status = true;
|
|
199 |
System.out.println("done getting access");
|
|
200 |
}
|
|
201 |
} else if (resource.equals(RESOURCE_META)) {
|
|
202 |
System.out.println("Using resource 'meta'");
|
|
203 |
String objectId = request.getPathInfo();
|
|
204 |
if (objectId != null && objectId.length() > 1) {
|
|
205 |
objectId = request.getPathInfo().substring(1);
|
|
206 |
}
|
|
207 |
|
|
208 |
// get
|
|
209 |
if (httpVerb == GET) {
|
|
210 |
getSystemMetadataObject(objectId);
|
|
211 |
status = true;
|
|
212 |
}
|
|
213 |
|
|
214 |
// post to register system metadata
|
|
215 |
if (httpVerb == POST) {
|
|
216 |
registerSystemMetadata(objectId);
|
|
217 |
status = true;
|
|
218 |
}
|
|
219 |
|
|
220 |
} else if (resource.equals(RESOURCE_OBJECTS)) {
|
|
221 |
System.out.println("Using resource 'object'");
|
|
222 |
|
|
223 |
String objectId = request.getPathInfo();
|
|
224 |
if (objectId != null && objectId.length() > 1) {
|
|
225 |
objectId = request.getPathInfo().substring(1);
|
|
226 |
} else {
|
|
227 |
objectId = null;
|
|
228 |
}
|
|
229 |
|
|
230 |
System.out.println("objectId: " + objectId);
|
|
231 |
logMetacat.debug("verb:" + httpVerb);
|
|
232 |
|
|
233 |
if (httpVerb == GET) {
|
|
234 |
getObject(objectId);
|
|
235 |
status = true;
|
|
236 |
} else if (httpVerb == POST) {
|
|
237 |
putObject(objectId, FUNCTION_NAME_INSERT);
|
|
238 |
status = true;
|
|
239 |
} else if (httpVerb == PUT) {
|
|
240 |
putObject(objectId, FUNCTION_NAME_UPDATE);
|
|
241 |
status = true;
|
|
242 |
} else if (httpVerb == DELETE) {
|
|
243 |
deleteObject(objectId);
|
|
244 |
status = true;
|
|
245 |
} else if (httpVerb == HEAD) {
|
|
246 |
describeObject(objectId);
|
|
247 |
status = true;
|
|
248 |
}
|
|
249 |
|
|
250 |
} else if (resource.equals(RESOURCE_LOG)) {
|
|
251 |
System.out.println("Using resource 'log'");
|
|
252 |
// handle log events
|
|
253 |
if (httpVerb == GET) {
|
|
254 |
getLog();
|
|
255 |
status = true;
|
|
256 |
}
|
|
257 |
} else if (resource.equals(RESOURCE_CHECKSUM)) {
|
|
258 |
System.out.println("Using resource 'checksum'");
|
|
259 |
// handle checksum requests
|
|
260 |
if (httpVerb == GET) {
|
|
261 |
checksum();
|
|
262 |
status = true;
|
|
263 |
}
|
|
264 |
} else if (resource.startsWith(RESOURCE_MONITOR)) {
|
|
265 |
// there are various parts to monitoring
|
|
266 |
if (httpVerb == GET) {
|
|
267 |
// health monitoring calls
|
|
268 |
monitor();
|
|
269 |
}
|
|
270 |
status = true;
|
|
271 |
} else if (resource.equals(RESOURCE_REPLICATE)) {
|
|
272 |
if (httpVerb == POST) {
|
|
273 |
System.out.println("processing replicate request");
|
|
274 |
replicate();
|
|
275 |
status = true;
|
|
276 |
}
|
|
277 |
} else if (resource.equals(RESOURCE_ERROR)) {
|
|
278 |
// sync error
|
|
279 |
if (httpVerb == POST) {
|
|
280 |
syncError();
|
|
281 |
status = true;
|
|
282 |
}
|
|
283 |
}
|
|
284 |
|
|
285 |
if (!status) {
|
|
286 |
response.setStatus(400);
|
|
287 |
printError("Incorrect parameters!", response);
|
|
288 |
}
|
|
289 |
} else {
|
|
290 |
response.setStatus(400);
|
|
291 |
printError("Incorrect resource!", response);
|
|
292 |
}
|
|
293 |
} catch (BaseException be) {
|
|
294 |
// report Exceptions as clearly as possible
|
|
295 |
OutputStream out = null;
|
|
296 |
try {
|
|
297 |
out = response.getOutputStream();
|
|
298 |
} catch (IOException e) {
|
|
299 |
logMetacat.error("Could not get output stream from response", e);
|
|
300 |
}
|
|
301 |
serializeException(be, out);
|
|
302 |
} catch (Exception e) {
|
|
303 |
logMetacat.error(e.getClass() + ": " + e.getMessage());
|
|
304 |
System.out.println("Error in ResourceHandler.handle(): " + e.getClass() + ": " + e.getMessage());
|
|
305 |
e.printStackTrace();
|
|
306 |
}
|
|
307 |
}
|
|
308 |
|
|
309 |
private boolean isAuthorized(String id) throws ServiceFailure, InvalidToken, NotFound, NotAuthorized, NotImplemented, InvalidRequest {
|
|
310 |
Identifier pid = new Identifier();
|
|
311 |
pid.setValue(id);
|
|
312 |
Permission permission = null;
|
|
313 |
try {
|
|
314 |
String perm = params.get("permission")[0];
|
|
315 |
permission = Permission.valueOf(perm);
|
|
316 |
} catch (Exception e) {
|
|
317 |
logMetacat.warn("No permission specified");
|
|
318 |
}
|
|
319 |
boolean result = MNodeService.getInstance().isAuthorized(session, pid, permission);
|
|
320 |
response.setStatus(200);
|
|
321 |
response.setContentType("text/xml");
|
|
322 |
return result;
|
|
323 |
}
|
|
324 |
|
|
325 |
private void syncError() throws NotImplemented, ServiceFailure, NotAuthorized, InvalidRequest, UnsupportedEncodingException, JiBXException {
|
|
326 |
SynchronizationFailed syncFailed = null;
|
|
327 |
if (params.containsKey("message")) {
|
|
328 |
String message = params.get("message")[0];
|
|
329 |
syncFailed = (SynchronizationFailed) deserializeServiceType(SynchronizationFailed.class, new ByteArrayInputStream(message.getBytes("UTF-8")));
|
|
330 |
}
|
|
331 |
MNodeService.getInstance().synchronizationFailed(session, syncFailed);
|
|
332 |
}
|
|
333 |
|
|
334 |
private boolean monitor() throws NotFound, ParseException, NotImplemented, ServiceFailure, NotAuthorized, InvalidRequest, InsufficientResources, UnsupportedType, IOException, JiBXException {
|
|
335 |
System.out.println("processing monitor request");
|
|
336 |
String pathInfo = request.getPathInfo();
|
|
337 |
|
|
338 |
System.out.println("verb is GET");
|
|
339 |
System.out.println("pathInfo is " + pathInfo);
|
|
340 |
pathInfo = pathInfo.substring(1);
|
|
341 |
|
|
342 |
if (pathInfo.toLowerCase().equals("ping")) {
|
|
343 |
System.out.println("processing ping request");
|
|
344 |
boolean result = MNodeService.getInstance().ping();
|
|
345 |
return result;
|
|
346 |
|
|
347 |
} else if (pathInfo.toLowerCase().equals("status")) {
|
|
348 |
System.out.println("processing status request");
|
|
349 |
// TODO: implement in MNCore
|
|
350 |
//MNodeService.getInstance().getStatus();
|
|
351 |
return false;
|
|
352 |
|
|
353 |
} else if (pathInfo.toLowerCase().equals("object")) {
|
|
354 |
System.out.println("processing object request");
|
|
355 |
Identifier pid = null;
|
|
356 |
ObjectFormat format = null;
|
|
357 |
if (params.containsKey("format")) {
|
|
358 |
String f = params.get("format")[0];
|
|
359 |
format = ObjectFormatCache.getInstance().getFormat(f);
|
|
360 |
}
|
|
361 |
if (params.containsKey("pid")) {
|
|
362 |
String id = params.get("pid")[0];
|
|
363 |
pid = new Identifier();
|
|
364 |
pid.setValue(id);
|
|
365 |
}
|
|
366 |
|
|
367 |
// TODO: implement in MNCore
|
|
368 |
//ObjectStatistics objectStats = MNodeService.getInstance().getObjectStatistics(format, pid);
|
|
369 |
|
|
370 |
} else if (pathInfo.toLowerCase().equals("event")) {
|
|
371 |
System.out.println("processing event request");
|
|
372 |
ObjectFormat format = null;
|
|
373 |
Integer period = null;
|
|
374 |
String requestor = null;
|
|
375 |
Subject subject = null;
|
|
376 |
String eventName = null;
|
|
377 |
Event event = null;
|
|
378 |
|
|
379 |
if (params.containsKey("format")) {
|
|
380 |
String f = params.get("format")[0];
|
|
381 |
format = ObjectFormatCache.getInstance().getFormat(f);
|
|
382 |
}
|
|
383 |
if (params.containsKey("period")) {
|
|
384 |
String t = params.get("period")[0];
|
|
385 |
period = Integer.valueOf(t);
|
|
386 |
}
|
|
387 |
if (params.containsKey("requestor")) {
|
|
388 |
requestor = params.get("requestor")[0];
|
|
389 |
subject = new Subject();
|
|
390 |
subject.setValue(requestor);
|
|
391 |
}
|
|
392 |
if (params.containsKey("event")) {
|
|
393 |
eventName = params.get("event")[0];
|
|
394 |
event = Event.valueOf(eventName);
|
|
395 |
}
|
|
396 |
|
|
397 |
MonitorList monitorList = MNodeService.getInstance().getOperationStatistics(session, period, subject, event, format);
|
|
398 |
|
|
399 |
OutputStream out = response.getOutputStream();
|
|
400 |
response.setStatus(200);
|
|
401 |
response.setContentType("text/xml");
|
|
402 |
|
|
403 |
serializeServiceType(MonitorList.class, monitorList, out);
|
|
404 |
|
|
405 |
}
|
|
406 |
|
|
407 |
return true;
|
|
408 |
}
|
|
409 |
|
|
410 |
private void checksum() throws NotImplemented, JiBXException, IOException {
|
|
411 |
String checksumAlgorithm = "MD5";
|
|
412 |
String guid = request.getPathInfo();
|
|
413 |
if (guid != null && guid.length() > 1) {
|
|
414 |
guid = request.getPathInfo().substring(1); // trim the slash
|
|
415 |
}
|
|
416 |
Identifier guidid = new Identifier();
|
|
417 |
guidid.setValue(guid);
|
|
418 |
try {
|
|
419 |
checksumAlgorithm = params.get("checksumAlgorithm")[0];
|
|
420 |
} catch(Exception e) {
|
|
421 |
//do nothing. default to MD5
|
|
422 |
logMetacat.warn("No algorithm specified, using default: " + checksumAlgorithm);
|
|
423 |
}
|
|
424 |
System.out.println("getting checksum for object " + guid + " with algorithm " + checksumAlgorithm);
|
|
425 |
try {
|
|
426 |
Checksum c = MNodeService.getInstance().getChecksum(session, guidid, checksumAlgorithm);
|
|
427 |
System.out.println("got checksum " + c.getValue());
|
|
428 |
response.setStatus(200);
|
|
429 |
System.out.println("serializing response");
|
|
430 |
serializeServiceType(Checksum.class, c, response.getOutputStream());
|
|
431 |
System.out.println("done serializing response.");
|
|
432 |
}
|
|
433 |
catch(NotAuthorized na)
|
|
434 |
{
|
|
435 |
na.setDetail_code("1400");
|
|
436 |
serializeException(na, response.getOutputStream());
|
|
437 |
}
|
|
438 |
catch(NotFound nf)
|
|
439 |
{
|
|
440 |
nf.setDetail_code("1420");
|
|
441 |
serializeException(nf, response.getOutputStream());
|
|
442 |
}
|
|
443 |
catch(InvalidRequest ir)
|
|
444 |
{
|
|
445 |
ir.setDetail_code("1402");
|
|
446 |
serializeException(ir, response.getOutputStream());
|
|
447 |
}
|
|
448 |
catch(ServiceFailure sf)
|
|
449 |
{
|
|
450 |
sf.setDetail_code("1410");
|
|
451 |
serializeException(sf, response.getOutputStream());
|
|
452 |
}
|
|
453 |
catch(InvalidToken it)
|
|
454 |
{
|
|
455 |
it.setDetail_code("1430");
|
|
456 |
serializeException(it, response.getOutputStream());
|
|
457 |
}
|
|
458 |
}
|
|
459 |
|
|
460 |
/**
|
|
461 |
* handle the replicate action for MN
|
|
462 |
* @throws Exception
|
|
463 |
*/
|
|
464 |
private void replicate() throws Exception {
|
|
465 |
|
|
466 |
System.out.println("in POST replicate()");
|
|
467 |
|
|
468 |
File tmpDir = getTempDirectory();
|
|
469 |
System.out.println("temp dir: " + tmpDir.getAbsolutePath());
|
|
470 |
MultipartRequestResolver mrr = new MultipartRequestResolver(
|
|
471 |
tmpDir.getAbsolutePath(), 1000000000, 0);
|
|
472 |
MultipartRequest mr = mrr.resolveMultipart(request);
|
|
473 |
Map<String, File> files = mr.getMultipartFiles();
|
|
474 |
Iterator keys = files.keySet().iterator();
|
|
475 |
while(keys.hasNext()) {
|
|
476 |
String key = (String)keys.next();
|
|
477 |
System.out.println("files key: " + key);
|
|
478 |
System.out.println("files value: " + files.get(key));
|
|
479 |
}
|
|
480 |
|
|
481 |
Map<String, List<String>> params = mr.getMultipartParameters();
|
|
482 |
keys = params.keySet().iterator();
|
|
483 |
while(keys.hasNext()) {
|
|
484 |
String key = (String)keys.next();
|
|
485 |
System.out.println("params key: " + key);
|
|
486 |
System.out.println("params value: " + params.get(key));
|
|
487 |
}
|
|
488 |
|
|
489 |
// File f = files.get("sysmeta");
|
|
490 |
// the files are not being keyed by the part name, but rather the filename
|
|
491 |
File f = files.get(files.keySet().iterator().next());
|
|
492 |
|
|
493 |
System.out.println("file: " + f.getAbsolutePath());
|
|
494 |
FileInputStream fis = new FileInputStream(f);
|
|
495 |
|
|
496 |
String sn = params.get("sourceNode").get(0);
|
|
497 |
System.out.println("sourceNode: " + sn);
|
|
498 |
NodeReference sourceNode = (NodeReference) deserializeServiceType(NodeReference.class, new ByteArrayInputStream(sn.getBytes("UTF-8")));
|
|
499 |
|
|
500 |
//parse the systemMetadata
|
|
501 |
SystemMetadata sysmeta = (SystemMetadata) deserializeServiceType(SystemMetadata.class, fis);
|
|
502 |
|
|
503 |
MNodeService.getInstance().replicate(session, sysmeta, sourceNode);
|
|
504 |
|
|
505 |
response.setStatus(200);
|
|
506 |
|
|
507 |
}
|
|
508 |
|
|
509 |
/**
|
|
510 |
* Get the Node information
|
|
511 |
*
|
|
512 |
* @throws JiBXException
|
|
513 |
* @throws IOException
|
|
514 |
* @throws InvalidRequest
|
|
515 |
* @throws ServiceFailure
|
|
516 |
* @throws NotAuthorized
|
|
517 |
* @throws NotImplemented
|
|
518 |
*/
|
|
519 |
private void node()
|
|
520 |
throws JiBXException, IOException, NotImplemented, NotAuthorized, ServiceFailure, InvalidRequest {
|
|
521 |
|
|
522 |
Node n = MNodeService.getInstance().getCapabilities();
|
|
523 |
|
|
524 |
NodeList nl = new NodeList();
|
|
525 |
nl.addNode(n);
|
|
526 |
|
|
527 |
response.setContentType("text/xml");
|
|
528 |
response.setStatus(200);
|
|
529 |
serializeServiceType(NodeList.class, nl, response.getOutputStream());
|
|
530 |
|
|
531 |
}
|
|
532 |
|
|
533 |
/**
|
|
534 |
* MN_crud.describe()
|
|
535 |
* http://mule1.dataone.org/ArchitectureDocs/mn_api_crud.html#MN_crud.describe
|
|
536 |
* @param guid
|
|
537 |
*/
|
|
538 |
private void describeObject(String guid)
|
|
539 |
{
|
|
540 |
Logger logMetacat = Logger.getLogger(MNResourceHandler.class);
|
|
541 |
OutputStream out = null;
|
|
542 |
try
|
|
543 |
{
|
|
544 |
out = response.getOutputStream();
|
|
545 |
}
|
|
546 |
catch(Exception e)
|
|
547 |
{
|
|
548 |
logMetacat.error("Error getting output stream in ResourceHandler.describeObject: " + e.getClass() + ": " + e.getMessage());
|
|
549 |
return;
|
|
550 |
}
|
|
551 |
response.setStatus(200);
|
|
552 |
response.setContentType("text/xml");
|
|
553 |
Identifier id = new Identifier();
|
|
554 |
id.setValue(guid);
|
|
555 |
try
|
|
556 |
{
|
|
557 |
DescribeResponse dr = MNodeService.getInstance().describe(session, id);
|
|
558 |
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SZ");
|
|
559 |
response.addHeader("guid", guid);
|
|
560 |
response.addHeader("checksum", dr.getDataONE_Checksum().getValue());
|
|
561 |
response.addHeader("checksum_algorithm", dr.getDataONE_Checksum().getAlgorithm().name());
|
|
562 |
response.addHeader("content_length", dr.getContent_Length() + "");
|
|
563 |
response.addHeader("last_modified", dateFormat.format(dr.getLast_Modified()));
|
|
564 |
response.addHeader("format", dr.getDataONE_ObjectFormat().toString());
|
|
565 |
}
|
|
566 |
catch(InvalidRequest ir)
|
|
567 |
{
|
|
568 |
serializeException(ir, out);
|
|
569 |
}
|
|
570 |
catch(NotImplemented ni)
|
|
571 |
{
|
|
572 |
serializeException(ni, out);
|
|
573 |
}
|
|
574 |
catch(NotAuthorized na)
|
|
575 |
{
|
|
576 |
serializeException(na, out);
|
|
577 |
}
|
|
578 |
catch(ServiceFailure sf)
|
|
579 |
{
|
|
580 |
serializeException(sf, out);
|
|
581 |
}
|
|
582 |
catch(NotFound nf)
|
|
583 |
{
|
|
584 |
serializeException(nf, out);
|
|
585 |
}
|
|
586 |
catch(InvalidToken it)
|
|
587 |
{
|
|
588 |
serializeException(it, out);
|
|
589 |
}
|
|
590 |
}
|
|
591 |
|
|
592 |
/**
|
|
593 |
* get the logs based on passed params. Available
|
|
594 |
* See http://mule1.dataone.org/ArchitectureDocs/mn_api_crud.html#MN_crud.getLogRecords
|
|
595 |
* for more info
|
|
596 |
*/
|
|
597 |
private void getLog()
|
|
598 |
{
|
|
599 |
OutputStream out = null;
|
|
600 |
try {
|
|
601 |
|
|
602 |
Date fromDate = null;
|
|
603 |
Date toDate = null;
|
|
604 |
Event event = null;
|
|
605 |
Integer start = null;
|
|
606 |
Integer count = null;
|
|
607 |
|
|
608 |
try {
|
|
609 |
String fromDateS = params.get("fromDate")[0];
|
|
610 |
System.out.println("param fromDateS: " + fromDateS);
|
|
611 |
fromDate = parseDateAndConvertToGMT(fromDateS);
|
|
612 |
} catch (Exception e) {
|
|
613 |
logMetacat.warn("Could not parse fromDate: " + e.getMessage());
|
|
614 |
}
|
|
615 |
try {
|
|
616 |
String toDateS = params.get("toDate")[0];
|
|
617 |
System.out.println("param toDateS: " + toDateS);
|
|
618 |
toDate = parseDateAndConvertToGMT(toDateS);
|
|
619 |
} catch (Exception e) {
|
|
620 |
logMetacat.warn("Could not parse toDate: " + e.getMessage());
|
|
621 |
}
|
|
622 |
try {
|
|
623 |
String eventS = params.get("event")[0];
|
|
624 |
event = Event.convert(eventS);
|
|
625 |
} catch (Exception e) {
|
|
626 |
logMetacat.warn("Could not parse event: " + e.getMessage());
|
|
627 |
}
|
|
628 |
System.out.println("fromDate: " + fromDate + " toDate: " + toDate);
|
|
629 |
|
|
630 |
try {
|
|
631 |
start = Integer.parseInt(params.get("start")[0]);
|
|
632 |
} catch (Exception e) {
|
|
633 |
logMetacat.warn("Could not parse start: " + e.getMessage());
|
|
634 |
}
|
|
635 |
try {
|
|
636 |
count = Integer.parseInt(params.get("count")[0]);
|
|
637 |
} catch (Exception e) {
|
|
638 |
logMetacat.warn("Could not parse count: " + e.getMessage());
|
|
639 |
}
|
|
640 |
|
|
641 |
System.out.println("calling getLogRecords");
|
|
642 |
Log log = MNodeService.getInstance().getLogRecords(session, fromDate, toDate, event, start, count);
|
|
643 |
|
|
644 |
out = response.getOutputStream();
|
|
645 |
response.setStatus(200);
|
|
646 |
response.setContentType("text/xml");
|
|
647 |
|
|
648 |
serializeServiceType(Log.class, log, out);
|
|
649 |
}
|
|
650 |
catch(Exception e) {
|
|
651 |
String msg = "Could not get logs: " + e.getClass() + ": " + e.getMessage();
|
|
652 |
response.setStatus(500);
|
|
653 |
ServiceFailure sf = new ServiceFailure("1490", msg);
|
|
654 |
logMetacat.error(msg);
|
|
655 |
e.printStackTrace();
|
|
656 |
serializeException(sf, out);
|
|
657 |
}
|
|
658 |
}
|
|
659 |
|
|
660 |
/**
|
|
661 |
* Implements REST version of DataONE CRUD API --> get
|
|
662 |
* @param guid ID of data object to be read
|
|
663 |
*/
|
|
664 |
@Override
|
|
665 |
protected void getObject(String guid) {
|
|
666 |
OutputStream out = null;
|
|
667 |
try {
|
|
668 |
out = response.getOutputStream();
|
|
669 |
response.setStatus(200);
|
|
670 |
|
|
671 |
if(guid != null)
|
|
672 |
{ //get a specific document
|
|
673 |
Identifier id = new Identifier();
|
|
674 |
id.setValue(guid);
|
|
675 |
try
|
|
676 |
{
|
|
677 |
|
|
678 |
SystemMetadata sm = MNodeService.getInstance().getSystemMetadata(session, id);
|
|
679 |
|
|
680 |
//set the content type
|
|
681 |
if(sm.getObjectFormat().getFmtid().getValue().trim().equals(
|
|
682 |
ObjectFormatCache.getInstance().getFormat("text/csv").getFmtid().getValue()))
|
|
683 |
{
|
|
684 |
response.setContentType("text/csv");
|
|
685 |
response.setHeader("Content-Disposition", "inline; filename=" + id.getValue() + ".csv");
|
|
686 |
}
|
|
687 |
else if(sm.getObjectFormat().getFmtid().getValue().trim().equals(
|
|
688 |
ObjectFormatCache.getInstance().getFormat("text/plain").getFmtid().getValue()))
|
|
689 |
{
|
|
690 |
response.setContentType("text/plain");
|
|
691 |
response.setHeader("Content-Disposition", "inline; filename=" + id.getValue() + ".txt");
|
|
692 |
}
|
|
693 |
else if(sm.getObjectFormat().getFmtid().getValue().trim().equals(
|
|
694 |
ObjectFormatCache.getInstance().getFormat("application/octet-stream").getFmtid().getValue()))
|
|
695 |
{
|
|
696 |
response.setContentType("application/octet-stream");
|
|
697 |
}
|
|
698 |
else
|
|
699 |
{
|
|
700 |
response.setContentType("text/xml");
|
|
701 |
response.setHeader("Content-Disposition", "inline; filename=" + id.getValue() + ".xml");
|
|
702 |
}
|
|
703 |
|
|
704 |
InputStream data = MNodeService.getInstance().get(session, id);
|
|
705 |
IOUtils.copyLarge(data, response.getOutputStream());
|
|
706 |
}
|
|
707 |
catch(InvalidToken it)
|
|
708 |
{
|
|
709 |
response.setStatus(500);
|
|
710 |
serializeException(it, out);
|
|
711 |
}
|
|
712 |
catch(ServiceFailure sf)
|
|
713 |
{
|
|
714 |
response.setStatus(500);
|
|
715 |
serializeException(sf, out);
|
|
716 |
}
|
|
717 |
catch(NotAuthorized na)
|
|
718 |
{
|
|
719 |
response.setStatus(500);
|
|
720 |
serializeException(na, out);
|
|
721 |
}
|
|
722 |
catch(NotFound nf)
|
|
723 |
{
|
|
724 |
response.setStatus(500);
|
|
725 |
serializeException(nf, out);
|
|
726 |
}
|
|
727 |
catch(NotImplemented ni)
|
|
728 |
{
|
|
729 |
response.setStatus(500);
|
|
730 |
serializeException(ni, out);
|
|
731 |
}
|
|
732 |
catch(Exception e)
|
|
733 |
{
|
|
734 |
response.setStatus(500);
|
|
735 |
System.out.println("Error with Crud.get(). " +
|
|
736 |
"If this is an 'Exception producing data' error, " +
|
|
737 |
"go to CrudService.get() for better debugging. " +
|
|
738 |
"Here's the error: " + e.getClass() + ": " + e.getMessage());
|
|
739 |
e.printStackTrace();
|
|
740 |
ServiceFailure sf = new ServiceFailure("1030",
|
|
741 |
"IO Error in ResourceHandler.getObject: " + e.getClass() + ": " + e.getMessage());
|
|
742 |
serializeException(sf, out);
|
|
743 |
}
|
|
744 |
}
|
|
745 |
else
|
|
746 |
{ //call listObjects with specified params
|
|
747 |
Date startTime = null;
|
|
748 |
Date endTime = null;
|
|
749 |
ObjectFormat objectFormat = null;
|
|
750 |
boolean replicaStatus = false;
|
|
751 |
int start = 0;
|
|
752 |
//TODO: make the max count into a const
|
|
753 |
int count = 1000;
|
|
754 |
Enumeration paramlist = request.getParameterNames();
|
|
755 |
while (paramlist.hasMoreElements())
|
|
756 |
{ //parse the params and make the crud call
|
|
757 |
String name = (String) paramlist.nextElement();
|
|
758 |
String[] value = (String[])request.getParameterValues(name);
|
|
759 |
/*for(int i=0; i<value.length; i++)
|
|
760 |
{
|
|
761 |
System.out.println("name: " + name + " value: " + value[i]);
|
|
762 |
}*/
|
|
763 |
if(name.equals("startTime") && value != null)
|
|
764 |
{
|
|
765 |
try
|
|
766 |
{
|
|
767 |
//startTime = dateFormat.parse(value[0]);
|
|
768 |
startTime = parseDateAndConvertToGMT(value[0]);
|
|
769 |
}
|
|
770 |
catch(Exception e)
|
|
771 |
{ //if we can't parse it, just don't use the startTime param
|
|
772 |
System.out.println("Could not parse startTime: " + value[0]);
|
|
773 |
startTime = null;
|
|
774 |
}
|
|
775 |
}
|
|
776 |
else if(name.equals("endTime") && value != null)
|
|
777 |
{
|
|
778 |
try
|
|
779 |
{
|
|
780 |
//endTime = dateFormat.parse(value[0]);
|
|
781 |
endTime = parseDateAndConvertToGMT(value[0]);
|
|
782 |
}
|
|
783 |
catch(Exception e)
|
|
784 |
{ //if we can't parse it, just don't use the endTime param
|
|
785 |
System.out.println("Could not parse endTime: " + value[0]);
|
|
786 |
endTime = null;
|
|
787 |
}
|
|
788 |
}
|
|
789 |
else if(name.equals("objectFormat") && value != null)
|
|
790 |
{
|
|
791 |
objectFormat = ObjectFormatCache.getInstance().getFormat(value[0]);
|
|
792 |
}
|
|
793 |
else if(name.equals("replicaStatus") && value != null)
|
|
794 |
{
|
|
795 |
if(value != null &&
|
|
796 |
value.length > 0 &&
|
|
797 |
(value[0].equals("true") || value[0].equals("TRUE") || value[0].equals("YES")))
|
|
798 |
{
|
|
799 |
replicaStatus = true;
|
|
800 |
}
|
|
801 |
}
|
|
802 |
else if(name.equals("start") && value != null)
|
|
803 |
{
|
|
804 |
start = new Integer(value[0]).intValue();
|
|
805 |
}
|
|
806 |
else if(name.equals("count") && value != null)
|
|
807 |
{
|
|
808 |
count = new Integer(value[0]).intValue();
|
|
809 |
}
|
|
810 |
}
|
|
811 |
//make the crud call
|
|
812 |
System.out.println("session: " + session + " startTime: " + startTime +
|
|
813 |
" endtime: " + endTime + " objectFormat: " +
|
|
814 |
objectFormat + " replicaStatus: " + replicaStatus +
|
|
815 |
" start: " + start + " count: " + count);
|
|
816 |
|
|
817 |
ObjectList ol = MNodeService.getInstance().listObjects(session, startTime, endTime,
|
|
818 |
objectFormat, replicaStatus, start, count);
|
|
819 |
|
|
820 |
StringReader sr = new StringReader(ol.toString());
|
|
821 |
out = response.getOutputStream();
|
|
822 |
response.setStatus(200);
|
|
823 |
response.setContentType("text/xml");
|
|
824 |
// Serialize and write it to the output stream
|
|
825 |
|
|
826 |
try {
|
|
827 |
serializeServiceType(ObjectList.class, ol, out);
|
|
828 |
} catch (JiBXException e) {
|
|
829 |
throw new ServiceFailure("1190", "Failed to serialize ObjectList: " + e.getMessage());
|
|
830 |
}
|
|
831 |
}
|
|
832 |
} catch (BaseException e) {
|
|
833 |
response.setStatus(500);
|
|
834 |
serializeException(e, out);
|
|
835 |
} catch (IOException e) {
|
|
836 |
e.printStackTrace();
|
|
837 |
response.setStatus(500);
|
|
838 |
ServiceFailure sf = new ServiceFailure("1030",
|
|
839 |
"IO Error in ResourceHandler.getObject: " + e.getMessage());
|
|
840 |
serializeException(sf, out);
|
|
841 |
} catch(NumberFormatException ne) {
|
|
842 |
response.setStatus(500);
|
|
843 |
InvalidRequest ir = new InvalidRequest("1030", "Invalid format for parameter: " + ne.getMessage());
|
|
844 |
serializeException(ir, out);
|
|
845 |
} catch (Exception e) {
|
|
846 |
e.printStackTrace();
|
|
847 |
response.setStatus(500);
|
|
848 |
ServiceFailure sf = new ServiceFailure("1030",
|
|
849 |
"Exception " + e.getClass().getName() + " raised while handling listObjects request: " +
|
|
850 |
e.getMessage());
|
|
851 |
serializeException(sf, out);
|
|
852 |
}
|
|
853 |
}
|
|
854 |
|
|
855 |
/**
|
|
856 |
* Implements REST version of DataONE CRUD API --> getSystemMetadata
|
|
857 |
* @param guid ID of data object to be read
|
|
858 |
*/
|
|
859 |
@Override
|
|
860 |
protected void getSystemMetadataObject(String guid) {
|
|
861 |
OutputStream out = null;
|
|
862 |
try {
|
|
863 |
response.setContentType("text/xml");
|
|
864 |
response.setStatus(200);
|
|
865 |
out = response.getOutputStream();
|
|
866 |
Identifier id = new Identifier();
|
|
867 |
id.setValue(guid);
|
|
868 |
SystemMetadata sysmeta = MNodeService.getInstance().getSystemMetadata(session, id);
|
|
869 |
|
|
870 |
// Serialize and write it to the output stream
|
|
871 |
try {
|
|
872 |
serializeServiceType(SystemMetadata.class, sysmeta, out);
|
|
873 |
} catch (JiBXException e) {
|
|
874 |
throw new ServiceFailure("1190", "Failed to serialize SystemMetadata: " + e.getMessage());
|
|
875 |
}
|
|
876 |
} catch (BaseException e) {
|
|
877 |
response.setStatus(500);
|
|
878 |
serializeException(e, out);
|
|
879 |
} catch (IOException e) {
|
|
880 |
response.setStatus(500);
|
|
881 |
ServiceFailure sf = new ServiceFailure("1030",
|
|
882 |
"IO Error in ResourceHandler.getSystemMetadataObject: " + e.getMessage());
|
|
883 |
serializeException(sf, out);
|
|
884 |
} finally {
|
|
885 |
IOUtils.closeQuietly(out);
|
|
886 |
}
|
|
887 |
}
|
|
888 |
|
|
889 |
/**
|
|
890 |
* Earthgrid API > Query Service > Query Function : translates ecogrid query document to metacat query
|
|
891 |
* then calls DBQuery > createResultDocument function and then again translate resultset to ecogrid resultset
|
|
892 |
*
|
|
893 |
* NOTE:
|
|
894 |
* This is the only method that uses EcoGrid classes for its implementation.
|
|
895 |
* It does so because it takes an EcoGrid Query as input, and outputs an
|
|
896 |
* EcoGrid ResultSet document. These documents are parsed by the auto-generated
|
|
897 |
* EcoGrid classes from axis, and so we link to them here rather than re-inventing them.
|
|
898 |
* This creates a circular dependency, because the Metacat classes are needed
|
|
899 |
* to build the EcoGrid implementation, and the EcoGrid jars are needed to build this query()
|
|
900 |
* method. This circularity could be resolved by moving the EcoGrid classes
|
|
901 |
* to Metacat directly. As we transition away from EcoGrid SOAP methods in
|
|
902 |
* favor of these REST interfaces, this circular dependency can be eliminated.
|
|
903 |
*
|
|
904 |
* @throws Exception
|
|
905 |
*/
|
|
906 |
private void query() throws Exception {
|
|
907 |
/* This block commented out because of the EcoGrid circular dependency.
|
|
908 |
* For now, query will not be supported until the circularity can be
|
|
909 |
* resolved, probably by moving the ecogrid query syntax transformers
|
|
910 |
* directly into the Metacat codebase. MBJ 2010-02-03
|
|
911 |
|
|
912 |
try {
|
|
913 |
EcogridQueryParser parser = new EcogridQueryParser(request
|
|
914 |
.getReader());
|
|
915 |
parser.parseXML();
|
|
916 |
QueryType queryType = parser.getEcogridQuery();
|
|
917 |
EcogridJavaToMetacatJavaQueryTransformer queryTransformer =
|
|
918 |
new EcogridJavaToMetacatJavaQueryTransformer();
|
|
919 |
QuerySpecification metacatQuery = queryTransformer
|
|
920 |
.transform(queryType);
|
|
921 |
|
|
922 |
DBQuery metacat = new DBQuery();
|
|
923 |
|
|
924 |
boolean useXMLIndex = (new Boolean(PropertyService
|
|
925 |
.getProperty("database.usexmlindex"))).booleanValue();
|
|
926 |
String xmlquery = "query"; // we don't care the query in resultset,
|
|
927 |
// the query can be anything
|
|
928 |
PrintWriter out = null; // we don't want metacat result, so set out null
|
|
929 |
|
|
930 |
// parameter: queryspecification, user, group, usingIndexOrNot
|
|
931 |
StringBuffer result = metacat.createResultDocument(xmlquery,
|
|
932 |
metacatQuery, out, username, groupNames, useXMLIndex);
|
|
933 |
|
|
934 |
// create result set transfer
|
|
935 |
String saxparser = PropertyService.getProperty("xml.saxparser");
|
|
936 |
MetacatResultsetParser metacatResultsetParser = new MetacatResultsetParser(
|
|
937 |
new StringReader(result.toString()), saxparser, queryType
|
|
938 |
.getNamespace().get_value());
|
|
939 |
ResultsetType records = metacatResultsetParser.getEcogridResult();
|
|
940 |
|
|
941 |
System.out
|
|
942 |
.println(EcogridResultsetTransformer.toXMLString(records));
|
|
943 |
response.setContentType("text/xml");
|
|
944 |
out = response.getWriter();
|
|
945 |
out.print(EcogridResultsetTransformer.toXMLString(records));
|
|
946 |
|
|
947 |
} catch (Exception e) {
|
|
948 |
e.printStackTrace();
|
|
949 |
}*/
|
|
950 |
response.setContentType("text/xml");
|
|
951 |
response.setStatus(501);
|
|
952 |
PrintWriter out = response.getWriter();
|
|
953 |
out.print("<error>Query operation not yet supported by Metacat.</error>");
|
|
954 |
out.close();
|
|
955 |
}
|
|
956 |
|
|
957 |
/**
|
|
958 |
* return a tmp file with a given name
|
|
959 |
* @param name
|
|
960 |
* @return
|
|
961 |
*/
|
|
962 |
private static File getTempFile(String name)
|
|
963 |
{
|
|
964 |
File tmpDir = getTempDirectory();
|
|
965 |
File f = new File(tmpDir, name);
|
|
966 |
return f;
|
|
967 |
}
|
|
968 |
|
|
969 |
/**
|
|
970 |
* return a temp file with a default name
|
|
971 |
* @return
|
|
972 |
*/
|
|
973 |
private static File getTempFile()
|
|
974 |
{
|
|
975 |
return getTempFile(new Date().getTime() + ".tmp");
|
|
976 |
}
|
|
977 |
|
|
978 |
/**
|
|
979 |
* Inserts or updates the object
|
|
980 |
*
|
|
981 |
* @param guid - ID of data object to be inserted or updated. If action is update, the pid
|
|
982 |
* is the existing pid. If insert, the pid is the new one
|
|
983 |
*/
|
|
984 |
@Override
|
|
985 |
protected void putObject(String pid, String action) {
|
|
986 |
System.out.println("ResourceHandler: putObject with pid " + pid);
|
|
987 |
logMetacat.debug("Entering putObject: " + pid + "/" + action);
|
|
988 |
OutputStream out = null;
|
|
989 |
try {
|
|
990 |
out = response.getOutputStream();
|
|
991 |
response.setStatus(200);
|
|
992 |
response.setContentType("text/xml");
|
|
993 |
} catch (IOException e1) {
|
|
994 |
logMetacat.error("Could not get the output stream for writing in putObject");
|
|
995 |
}
|
|
996 |
try {
|
|
997 |
|
|
998 |
// Read the incoming data from its Mime Multipart encoding
|
|
999 |
logMetacat.debug("Disassembling MIME multipart form");
|
|
1000 |
InputStream object = null;
|
|
1001 |
InputStream sysmeta = null;
|
|
1002 |
Map<String, List<String>> multipartparams;
|
|
1003 |
|
|
1004 |
try {
|
|
1005 |
|
|
1006 |
// handle MMP inputs
|
|
1007 |
File tmpDir = getTempDirectory();
|
|
1008 |
System.out.println("temp dir: " + tmpDir.getAbsolutePath());
|
|
1009 |
MultipartRequestResolver mrr =
|
|
1010 |
new MultipartRequestResolver(tmpDir.getAbsolutePath(), 1000000000, 0);
|
|
1011 |
MultipartRequest mr = mrr.resolveMultipart(request);
|
|
1012 |
System.out.println("resolved multipart request");
|
|
1013 |
Map<String, File> files = mr.getMultipartFiles();
|
|
1014 |
if (files == null) {
|
|
1015 |
throw new ServiceFailure("1202", "create/update must have multipart files with names 'object' and 'sysmeta'");
|
|
1016 |
}
|
|
1017 |
System.out.println("got multipart files");
|
|
1018 |
|
|
1019 |
if (files.keySet() == null) {
|
|
1020 |
System.out.println("No file keys in MMP request.");
|
|
1021 |
throw new ServiceFailure("1202", "No file keys found in MMP. " +
|
|
1022 |
"create/update must have multipart files with names 'object' and 'sysmeta'");
|
|
1023 |
}
|
|
1024 |
|
|
1025 |
// for logging purposes, dump out the key-value pairs that constitute the request
|
|
1026 |
// 3 types exist: request params, multipart params, and multipart files
|
|
1027 |
Iterator it = files.keySet().iterator();
|
|
1028 |
System.out.println("iterating through files");
|
|
1029 |
while (it.hasNext()) {
|
|
1030 |
String key = (String)it.next();
|
|
1031 |
System.out.println("files key: " + key);
|
|
1032 |
System.out.println("files value: " + files.get(key));
|
|
1033 |
}
|
|
1034 |
|
|
1035 |
multipartparams = mr.getMultipartParameters();
|
|
1036 |
it = multipartparams.keySet().iterator();
|
|
1037 |
System.out.println("iterating through multipartparams");
|
|
1038 |
while (it.hasNext()) {
|
|
1039 |
String key = (String)it.next();
|
|
1040 |
System.out.println("multipartparams key: " + key);
|
|
1041 |
System.out.println("multipartparams value: " + multipartparams.get(key));
|
|
1042 |
}
|
|
1043 |
|
|
1044 |
it = params.keySet().iterator();
|
|
1045 |
System.out.println("iterating through params");
|
|
1046 |
while (it.hasNext()) {
|
|
1047 |
String key = (String)it.next();
|
|
1048 |
System.out.println("param key: " + key);
|
|
1049 |
System.out.println("param value: " + params.get(key));
|
|
1050 |
}
|
|
1051 |
System.out.println("done iterating the request...");
|
|
1052 |
|
|
1053 |
File smFile = files.get("sysmeta");
|
|
1054 |
if (smFile == null) {
|
|
1055 |
throw new InvalidRequest("1102", "Missing the required file-part 'sysmeta' from the multipart request.");
|
|
1056 |
}
|
|
1057 |
System.out.println("smFile: " + smFile.getAbsolutePath());
|
|
1058 |
sysmeta = new FileInputStream(smFile);
|
|
1059 |
File objFile = files.get("object");
|
|
1060 |
if (objFile == null) {
|
|
1061 |
throw new InvalidRequest("1102", "Missing the required file-part 'object' from the multipart request.");
|
|
1062 |
}
|
|
1063 |
System.out.println("objectfile: " + objFile.getAbsolutePath());
|
|
1064 |
object = new FileInputStream(objFile);
|
|
1065 |
|
|
1066 |
}
|
|
1067 |
catch (FileUploadException fue)
|
|
1068 |
{
|
|
1069 |
throw new ServiceFailure("1202", "Could not upload MMP files: " + fue.getMessage());
|
|
1070 |
}
|
|
1071 |
catch (IOException ioe)
|
|
1072 |
{
|
|
1073 |
throw new ServiceFailure("1202",
|
|
1074 |
"IOException when processing Mime Multipart: " + ioe.getMessage());
|
|
1075 |
}
|
|
1076 |
catch (Exception e)
|
|
1077 |
{
|
|
1078 |
throw new ServiceFailure("1202", "Error handling MMP upload: " + e.getClass() + ": " + e.getMessage());
|
|
1079 |
}
|
|
1080 |
|
|
1081 |
// handle inserts
|
|
1082 |
if (action.equals(FUNCTION_NAME_INSERT)) {
|
|
1083 |
|
|
1084 |
logMetacat.debug("Commence creation...");
|
|
1085 |
SystemMetadata smd = (SystemMetadata) deserializeServiceType(SystemMetadata.class, sysmeta);
|
|
1086 |
|
|
1087 |
Identifier id = new Identifier();
|
|
1088 |
id.setValue(pid);
|
|
1089 |
System.out.println("creating object with pid " + pid);
|
|
1090 |
Identifier rId = MNodeService.getInstance().create(session, id, object, smd);
|
|
1091 |
serializeServiceType(Identifier.class, rId, out);
|
|
1092 |
|
|
1093 |
} else if (action.equals(FUNCTION_NAME_UPDATE)) {
|
|
1094 |
|
|
1095 |
// check that the newPid parameter was provided
|
|
1096 |
if (multipartparams.get("newPid") == null) {
|
|
1097 |
throw new InvalidRequest("1202", "'newPid' must be contained in the request parameters.");
|
|
1098 |
}
|
|
1099 |
String newPidString = multipartparams.get("newPid").get(0);
|
|
1100 |
|
|
1101 |
Identifier newPid = new Identifier();
|
|
1102 |
Identifier obsoletedPid = new Identifier();
|
|
1103 |
newPid.setValue(newPidString);
|
|
1104 |
obsoletedPid.setValue(pid);
|
|
1105 |
|
|
1106 |
logMetacat.debug("Commence update...");
|
|
1107 |
|
|
1108 |
//get the systemmetadata object
|
|
1109 |
SystemMetadata smd = (SystemMetadata) deserializeServiceType(SystemMetadata.class, sysmeta);
|
|
1110 |
|
|
1111 |
Identifier rId = MNodeService.getInstance().update(session, newPid, object, obsoletedPid, smd);
|
|
1112 |
serializeServiceType(Identifier.class, rId, out);
|
|
1113 |
} else {
|
|
1114 |
throw new InvalidRequest("1000", "Operation must be create or update.");
|
|
1115 |
}
|
|
1116 |
|
|
1117 |
//clean up the MMP files
|
|
1118 |
//parts.get("systemmetadata").delete();
|
|
1119 |
//parts.get("object").delete();
|
|
1120 |
} catch (NotAuthorized e) {
|
|
1121 |
response.setStatus(500);
|
|
1122 |
serializeException(e, out);
|
|
1123 |
} catch (InvalidToken e) {
|
|
1124 |
response.setStatus(500);
|
|
1125 |
serializeException(e, out);
|
|
1126 |
} catch (ServiceFailure e) {
|
|
1127 |
response.setStatus(500);
|
|
1128 |
serializeException(e, out);
|
|
1129 |
} catch (NotFound e) {
|
|
1130 |
response.setStatus(500);
|
|
1131 |
serializeException(e, out);
|
|
1132 |
} catch (IdentifierNotUnique e) {
|
|
1133 |
response.setStatus(500);
|
|
1134 |
serializeException(e, out);
|
|
1135 |
} catch (UnsupportedType e) {
|
|
1136 |
response.setStatus(500);
|
|
1137 |
serializeException(e, out);
|
|
1138 |
} catch (InsufficientResources e) {
|
|
1139 |
response.setStatus(500);
|
|
1140 |
serializeException(e, out);
|
|
1141 |
} catch (InvalidSystemMetadata e) {
|
|
1142 |
response.setStatus(500);
|
|
1143 |
serializeException(e, out);
|
|
1144 |
} catch (NotImplemented e) {
|
|
1145 |
response.setStatus(500);
|
|
1146 |
serializeException(e, out);
|
|
1147 |
} catch (InvalidRequest e) {
|
|
1148 |
response.setStatus(500);
|
|
1149 |
serializeException(e, out);
|
|
1150 |
} catch (JiBXException e) {
|
|
1151 |
response.setStatus(500);
|
|
1152 |
e.printStackTrace(System.out);
|
|
1153 |
InvalidSystemMetadata ism = new InvalidSystemMetadata("1080", e.getMessage());
|
|
1154 |
serializeException(ism, out);
|
|
1155 |
}
|
|
1156 |
}
|
|
1157 |
|
|
1158 |
/**
|
|
1159 |
* Handle delete
|
|
1160 |
* @param guid ID of data object to be deleted
|
|
1161 |
* @throws IOException
|
|
1162 |
*/
|
|
1163 |
private void deleteObject(String guid) throws IOException
|
|
1164 |
{
|
|
1165 |
|
|
1166 |
OutputStream out = response.getOutputStream();
|
|
1167 |
response.setStatus(200);
|
|
1168 |
response.setContentType("text/xml");
|
|
1169 |
|
|
1170 |
Identifier id = new Identifier();
|
|
1171 |
id.setValue(guid);
|
|
1172 |
try {
|
|
1173 |
System.out.println("Calling delete");
|
|
1174 |
MNodeService.getInstance().delete(session, id);
|
|
1175 |
serializeServiceType(Identifier.class, id, out);
|
|
1176 |
}
|
|
1177 |
catch (NotAuthorized e) {
|
|
1178 |
response.setStatus(500);
|
|
1179 |
serializeException(e, out);
|
|
1180 |
} catch (InvalidToken e) {
|
|
1181 |
response.setStatus(500);
|
|
1182 |
serializeException(e, out);
|
|
1183 |
} catch (ServiceFailure e) {
|
|
1184 |
response.setStatus(500);
|
|
1185 |
serializeException(e, out);
|
|
1186 |
} catch (NotImplemented e) {
|
|
1187 |
response.setStatus(500);
|
|
1188 |
serializeException(e, out);
|
|
1189 |
} catch (InvalidRequest e) {
|
|
1190 |
response.setStatus(500);
|
|
1191 |
serializeException(e, out);
|
|
1192 |
} catch(NotFound e) {
|
|
1193 |
response.setStatus(500);
|
|
1194 |
serializeException(e, out);
|
|
1195 |
} catch(JiBXException e) {
|
|
1196 |
response.setStatus(500);
|
|
1197 |
serializeException(new ServiceFailure("1350", "JiBXException: " + e.getMessage()), out);
|
|
1198 |
}
|
|
1199 |
out.close();
|
|
1200 |
}
|
|
1201 |
|
|
1202 |
/**
|
|
1203 |
* set the access perms on a document
|
|
1204 |
* @throws Exception
|
|
1205 |
*/
|
|
1206 |
@Override
|
|
1207 |
protected void setaccess() throws Exception
|
|
1208 |
{
|
|
1209 |
try
|
|
1210 |
{
|
|
1211 |
String guid = params.get("guid")[0];
|
|
1212 |
Identifier id = new Identifier();
|
|
1213 |
id.setValue(guid);
|
|
1214 |
String accesspolicy = params.get("accesspolicy")[0];
|
|
1215 |
AccessPolicy accessPolicy = (AccessPolicy) deserializeServiceType(AccessPolicy.class, new ByteArrayInputStream(accesspolicy.getBytes("UTF-8")));
|
|
1216 |
MNodeService.getInstance().setAccessPolicy(session, id, accessPolicy);
|
|
1217 |
}
|
|
1218 |
catch(Exception e)
|
|
1219 |
{
|
|
1220 |
response.setStatus(500);
|
|
1221 |
printError("Error setting access in ResourceHandler: " + e.getClass() + ": " + e.getMessage(), response);
|
|
1222 |
throw e;
|
|
1223 |
}
|
|
1224 |
}
|
|
1225 |
|
|
1226 |
}
|
0 |
1227 |
|
MN rest servlet, handler and mappings