Project

General

Profile

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: leinfelder $'
7
 *     '$Date: 2015-07-08 17:03:35 -0700 (Wed, 08 Jul 2015) $'
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.v2;
24

    
25
import java.io.File;
26
import java.io.FileInputStream;
27
import java.io.FileNotFoundException;
28
import java.io.IOException;
29
import java.io.InputStream;
30
import java.io.OutputStream;
31
import java.util.Date;
32
import java.util.Enumeration;
33
import java.util.Iterator;
34
import java.util.Map;
35
import java.util.concurrent.ExecutorService;
36
import java.util.concurrent.Executors;
37

    
38
import javax.servlet.ServletContext;
39
import javax.servlet.http.HttpServletRequest;
40
import javax.servlet.http.HttpServletResponse;
41
import javax.xml.parsers.ParserConfigurationException;
42

    
43
import org.apache.commons.fileupload.FileUploadException;
44
import org.apache.commons.io.IOUtils;
45
import org.apache.log4j.Logger;
46
import org.dataone.client.v2.formats.ObjectFormatCache;
47
import org.dataone.client.v2.formats.ObjectFormatInfo;
48
import org.dataone.mimemultipart.MultipartRequest;
49
import org.dataone.mimemultipart.MultipartRequestResolver;
50
import org.dataone.portal.TokenGenerator;
51
import org.dataone.service.exceptions.BaseException;
52
import org.dataone.service.exceptions.IdentifierNotUnique;
53
import org.dataone.service.exceptions.InsufficientResources;
54
import org.dataone.service.exceptions.InvalidRequest;
55
import org.dataone.service.exceptions.InvalidSystemMetadata;
56
import org.dataone.service.exceptions.InvalidToken;
57
import org.dataone.service.exceptions.NotAuthorized;
58
import org.dataone.service.exceptions.NotFound;
59
import org.dataone.service.exceptions.NotImplemented;
60
import org.dataone.service.exceptions.ServiceFailure;
61
import org.dataone.service.exceptions.SynchronizationFailed;
62
import org.dataone.service.exceptions.UnsupportedType;
63
import org.dataone.service.types.v1.Checksum;
64
import org.dataone.service.types.v1.DescribeResponse;
65
import org.dataone.service.types.v1.Identifier;
66
import org.dataone.service.types.v1.NodeReference;
67
import org.dataone.service.types.v1.ObjectFormatIdentifier;
68
import org.dataone.service.types.v1.ObjectList;
69
import org.dataone.service.types.v1.Permission;
70
import org.dataone.service.types.v1.Person;
71
import org.dataone.service.types.v1.Subject;
72
import org.dataone.service.types.v1.SubjectInfo;
73
import org.dataone.service.types.v1_1.QueryEngineDescription;
74
import org.dataone.service.types.v1_1.QueryEngineList;
75
import org.dataone.service.types.v2.Log;
76
import org.dataone.service.types.v2.MediaType;
77
import org.dataone.service.types.v2.MediaTypeProperty;
78
import org.dataone.service.types.v2.Node;
79
import org.dataone.service.types.v2.OptionList;
80
import org.dataone.service.types.v2.SystemMetadata;
81
import org.dataone.service.util.Constants;
82
import org.dataone.service.util.DateTimeMarshaller;
83
import org.dataone.service.util.ExceptionHandler;
84
import org.dataone.service.util.TypeMarshaller;
85
import org.jibx.runtime.JiBXException;
86
import org.xml.sax.SAXException;
87

    
88
import edu.ucsb.nceas.metacat.MetaCatServlet;
89
import edu.ucsb.nceas.metacat.common.query.stream.ContentTypeInputStream;
90
import edu.ucsb.nceas.metacat.dataone.MNodeService;
91
import edu.ucsb.nceas.metacat.properties.PropertyService;
92
import edu.ucsb.nceas.metacat.restservice.D1ResourceHandler;
93
import edu.ucsb.nceas.metacat.util.DeleteOnCloseFileInputStream;
94
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
95

    
96
/**
97
 * MN REST service implementation handler
98
 * 
99
 * ******************
100
 * MNCore -- DONE
101
 * 		ping() - GET /d1/mn/monitor/ping
102
 * 		log() - GET /d1/mn/log
103
 * 		**getObjectStatistics() - GET /d1/mn/monitor/object
104
 * 		getOperationsStatistics - GET /d1/mn/monitor/event
105
 * 		**getStatus - GET /d1/mn/monitor/status
106
 * 		getCapabilities() - GET /d1/mn/ and /d1/mn/node
107
 * 	
108
 * 	MNRead -- DONE
109
 * 		get() - GET /d1/mn/object/PID
110
 * 		getSystemMetadata() - GET /d1/mn/meta/PID
111
 * 		describe() - HEAD /d1/mn/object/PID
112
 * 		getChecksum() - GET /d1/mn/checksum/PID
113
 * 		listObjects() - GET /d1/mn/object
114
 * 		synchronizationFailed() - POST /d1/mn/error
115
 * 	
116
 * 	MNAuthorization -- DONE
117
 * 		isAuthorized() - GET /d1/mn/isAuthorized/PID
118
 * 		setAccessPolicy() - PUT /d1/mn/accessRules/PID
119
 * 		
120
 * 	MNStorage - DONE
121
 * 		create() - POST /d1/mn/object/PID
122
 * 		update() - PUT /d1/mn/object/PID
123
 * 		delete() - DELETE /d1/mn/object/PID
124
 * 		archive() - PUT /d1/mn/archive/PID
125
 *      updateSystemMetadata() - PUT /d1/mn/meta
126

    
127
 *    systemMetadataChanged() - POST /dirtySystemMetadata/PID
128
 * 	
129
 * 	MNReplication
130
 * 		replicate() - POST /d1/mn/replicate
131
 *    getReplica() - GET /d1/mn/replica
132
 * 
133
 * ******************
134
 * @author leinfelder
135
 *
136
 */
137
public class MNResourceHandler extends D1ResourceHandler {
138

    
139
    // MN-specific API Resources
140
    protected static final String RESOURCE_MONITOR = "monitor";
141
    protected static final String RESOURCE_REPLICATE = "replicate";
142
    protected static final String RESOURCE_REPLICAS = "replica";
143
    protected static final String RESOURCE_NODE = "node";
144
    protected static final String RESOURCE_ERROR = "error";
145
    protected static final String RESOURCE_META_CHANGED = "dirtySystemMetadata";
146
    protected static final String RESOURCE_GENERATE_ID = "generate";
147
    protected static final String RESOURCE_PUBLISH = "publish";
148
    protected static final String RESOURCE_PACKAGE = "package";
149
    protected static final String RESOURCE_TOKEN = "token";
150
    protected static final String RESOURCE_WHOAMI = "whoami";
151

    
152

    
153

    
154
    
155
    // shared executor
156
	private static ExecutorService executor = null;
157

    
158
	static {
159
		// use a shared executor service with nThreads == one less than available processors
160
    	int availableProcessors = Runtime.getRuntime().availableProcessors();
161
        int nThreads = availableProcessors * 1;
162
        nThreads--;
163
        nThreads = Math.max(1, nThreads);
164
    	executor = Executors.newFixedThreadPool(nThreads);	
165
	}
166
	
167
    /**
168
     * Initializes new instance by setting servlet context,request and response
169
     * */
170
    public MNResourceHandler(ServletContext servletContext,
171
            HttpServletRequest request, HttpServletResponse response) {
172
    	super(servletContext, request, response);
173
        logMetacat = Logger.getLogger(MNResourceHandler.class);
174
    }
175
    
176
    @Override
177
    protected boolean isD1Enabled() {
178
    	
179
    	boolean enabled = false;
180
    	try {
181
			enabled = Boolean.parseBoolean(PropertyService.getProperty("dataone.mn.services.enabled"));
182
		} catch (PropertyNotFoundException e) {
183
			logMetacat.error("Could not check if DataONE is enabled: " + e.getMessage());
184
		}
185
    	
186
    	return enabled;	
187
    }
188

    
189
    /**
190
     * This function is called from REST API servlet and handles each request to the servlet 
191
     * 
192
     * @param httpVerb (GET, POST, PUT or DELETE)
193
     */
194
    @Override
195
    public void handle(byte httpVerb) {
196
    	// prepare the handler
197
    	super.handle(httpVerb);
198
    	
199
        try {
200
        	
201
        	// only service requests if we have D1 configured
202
        	if (!isD1Enabled()) {
203
        		ServiceFailure se = new ServiceFailure("0000", "DataONE services are not enabled on this node");
204
                serializeException(se, response.getOutputStream());
205
                return;
206
        	}
207
        	
208
        	// get the resource
209
            String resource = request.getPathInfo();
210
            resource = resource.substring(resource.indexOf("/") + 1);
211
            
212
            // default to node info
213
            if (resource.equals("")) {
214
                resource = RESOURCE_NODE;
215
            }
216
            
217
            // get the rest of the path info
218
            String extra = null;
219
                        
220
            logMetacat.debug("handling verb " + httpVerb + " request with resource '" + resource + "'");
221
            logMetacat.debug("resource: '" + resource + "'");
222
            boolean status = false;
223
            
224
            if (resource != null) {
225

    
226
                if (resource.startsWith(RESOURCE_NODE)) {
227
                    // node response
228
                    node();
229
                    status = true;
230
                } else if (resource.startsWith(RESOURCE_TOKEN)) {
231
                    logMetacat.debug("Using resource 'token'");
232
                    // get
233
                    if (httpVerb == GET) {
234
                    	// after the command
235
                        getToken();
236
                        status = true;
237
                    }
238
                    
239
                } else if (resource.startsWith(RESOURCE_WHOAMI)) {
240
                    logMetacat.debug("Using resource 'whoami'");
241
                    // get
242
                    if (httpVerb == GET) {
243
                    	// after the command
244
                        whoami();
245
                        status = true;
246
                    }
247
                    
248
                } else if (resource.startsWith(RESOURCE_IS_AUTHORIZED)) {
249
                    if (httpVerb == GET) {
250
                    	// after the command
251
                        extra = parseTrailing(resource, RESOURCE_IS_AUTHORIZED);
252
	                	// check the access rules
253
	                    isAuthorized(extra);
254
	                    status = true;
255
	                    logMetacat.debug("done getting access");
256
                    }
257
                } else if (resource.startsWith(RESOURCE_META)) {
258
                    logMetacat.debug("Using resource 'meta'");
259
                    // get
260
                    if (httpVerb == GET) {
261
                        logMetacat.debug("Using resource 'meta' for GET");
262
                    	// after the command
263
                        extra = parseTrailing(resource, RESOURCE_META);
264
                        getSystemMetadataObject(extra);
265
                        status = true;
266
                    } else if (httpVerb == PUT) {
267
                        logMetacat.debug("Using resource 'meta' for PUT");
268
                        updateSystemMetadata();
269
                        status = true;
270
                    }
271
                    
272
                } else if (resource.startsWith(RESOURCE_OBJECTS)) {
273
                    logMetacat.debug("Using resource 'object'");
274
                    // after the command
275
                    extra = parseTrailing(resource, RESOURCE_OBJECTS);
276
                    logMetacat.debug("objectId: " + extra);
277
                    logMetacat.debug("verb:" + httpVerb);
278

    
279
                    if (httpVerb == GET) {
280
                        getObject(extra);
281
                        status = true;
282
                    } else if (httpVerb == POST) {
283
                    	// part of the params, not the URL
284
                        putObject(null, FUNCTION_NAME_INSERT);
285
                        status = true;
286
                    } else if (httpVerb == PUT) {
287
                        putObject(extra, FUNCTION_NAME_UPDATE);
288
                        status = true;
289
                    } else if (httpVerb == DELETE) {
290
                        deleteObject(extra);
291
                        status = true;
292
                    } else if (httpVerb == HEAD) {
293
                        describeObject(extra);
294
                        status = true;
295
                    }
296
                  
297
                } else if (resource.startsWith(RESOURCE_LOG)) {
298
                    logMetacat.debug("Using resource 'log'");
299
                    // handle log events
300
                    if (httpVerb == GET) {
301
                        getLog();
302
                        status = true;
303
                    }
304
                } else if (resource.startsWith(Constants.RESOURCE_ARCHIVE)) {
305
                    logMetacat.debug("Using resource " + Constants.RESOURCE_ARCHIVE);
306
                    // handle archive events
307
                    if (httpVerb == PUT) {
308
                        extra = parseTrailing(resource, Constants.RESOURCE_ARCHIVE);
309
                        archive(extra);
310
                        status = true;
311
                    }
312
                } else if (resource.startsWith(Constants.RESOURCE_CHECKSUM)) {
313
                    logMetacat.debug("Using resource 'checksum'");
314
                    // handle checksum requests
315
                    if (httpVerb == GET) {
316
                    	// after the command
317
                        extra = parseTrailing(resource, Constants.RESOURCE_CHECKSUM);
318
                        checksum(extra);
319
                        status = true;
320
                    }
321
                } else if (resource.startsWith(RESOURCE_MONITOR)) {
322
                    // there are various parts to monitoring
323
                    if (httpVerb == GET) {
324
                    	// after the command
325
                        extra = parseTrailing(resource, RESOURCE_MONITOR);
326
                        
327
                        // ping
328
                        if (extra.toLowerCase().equals("ping")) {
329
                            logMetacat.debug("processing ping request");
330
                            Date result = MNodeService.getInstance(request).ping();
331
                            // TODO: send to output	
332
                            status = true;
333
                            
334
                        }
335
                        
336
                    }
337
                } else if (resource.startsWith(RESOURCE_REPLICATE)) {
338
                	if (httpVerb == POST) {
339
	                    logMetacat.debug("processing replicate request");
340
	                    replicate();
341
	                    status = true;
342
                	}
343
                } else if (resource.startsWith(RESOURCE_ERROR)) {
344
	                // sync error
345
	                if (httpVerb == POST) {
346
	                    syncError();
347
	                    status = true;
348
	                }
349
                } else if (resource.startsWith(RESOURCE_META_CHANGED)) {
350
                    // system metadata changed
351
                    if (httpVerb == POST) {
352
                        systemMetadataChanged();
353
                        status = true;
354
                    }
355
                } else if (resource.startsWith(RESOURCE_REPLICAS)) {
356
                    // get replica
357
                    if (httpVerb == GET) {
358
                        extra = parseTrailing(resource, RESOURCE_REPLICAS);
359
                        getReplica(extra);
360
                        status = true;
361
                    }
362
                } else if (resource.startsWith(RESOURCE_QUERY)) {
363
	                logMetacat.debug("Using resource " + RESOURCE_QUERY);
364
	                // after the command
365
	                extra = parseTrailing(resource, RESOURCE_QUERY);
366
	                logMetacat.debug("query extra: " + extra);
367

    
368
	                String engine = null;
369
	                String query = null;
370

    
371
	                if (extra != null) {
372
		                // get the engine
373
		                int engineIndex = extra.length();
374
		                if (extra.indexOf("/") > -1) {
375
		                	engineIndex = extra.indexOf("/");
376
		                }
377
		                engine = extra.substring(0, engineIndex);
378
		                logMetacat.debug("query engine: " + engine);
379
		                
380
		                // get the query if it is there
381
		                query = extra.substring(engineIndex, extra.length());
382
		                if (query != null && query.length() == 0) {
383
		                	query = null;
384
		                } else {
385
		                	if (query.startsWith("/")) {
386
		                		query = query.substring(1);
387
		                    }
388
		                	// remove the query delimiter if it exists
389
		                	if (query.startsWith("?")) {
390
		                		query = query.substring(1);
391
		                    }
392
		                }
393
		                logMetacat.debug("query: " + query);
394

    
395
	                }
396
	                logMetacat.debug("verb:" + httpVerb);
397
	                if (httpVerb == GET) {
398
	                    doQuery(engine, query);
399
	                    status = true;
400
	                }
401
                } else if (resource.startsWith(RESOURCE_GENERATE_ID)) {
402
                	// generate an id
403
                    if (httpVerb == POST) {
404
                        generateIdentifier();
405
                        status = true;
406
                    }
407
                } else if (resource.startsWith(RESOURCE_PUBLISH)) {
408
                    logMetacat.debug("Using resource: " + RESOURCE_PUBLISH);
409
                    // PUT
410
                    if (httpVerb == PUT) {
411
                    	// after the command
412
                        extra = parseTrailing(resource, RESOURCE_PUBLISH);
413
                        publish(extra);
414
                        status = true;
415
                    }  
416
                } else if (resource.startsWith(RESOURCE_PACKAGE)) {
417
                    logMetacat.debug("Using resource: " + RESOURCE_PACKAGE);
418
                    // after the command
419
                    extra = parseTrailing(resource, RESOURCE_PACKAGE);
420
                    
421
                    String format = null;
422
	                String pid = null;
423

    
424
	                if (extra != null) {
425
		                // get the format
426
		                int formatIndex = extra.length();
427
		                if (extra.indexOf("/") > -1) {
428
		                	formatIndex = extra.indexOf("/");
429
		                }
430
		                format = extra.substring(0, formatIndex);
431
		                logMetacat.debug("package format: " + format);
432
		                
433
		                // get the pid if it is there
434
		                pid = extra.substring(formatIndex, extra.length());
435
		                if (pid != null && pid.length() == 0) {
436
		                	pid = null;
437
		                } else {
438
		                	if (pid.startsWith("/")) {
439
		                		pid = pid.substring(1);
440
		                    }
441
		                }
442
		                logMetacat.debug("pid: " + pid);
443

    
444
	                }
445
                    
446
                    // get
447
                    if (httpVerb == GET) {
448
                        
449
                        getPackage(format, pid);
450
                        status = true;
451
                    }  
452
                } else if (resource.startsWith(RESOURCE_VIEWS)) {
453
	                logMetacat.debug("Using resource " + RESOURCE_VIEWS);
454
	                // after the command
455
	                extra = parseTrailing(resource, RESOURCE_VIEWS);
456
	                logMetacat.debug("view extra: " + extra);
457

    
458
	                String format = null;
459
	                String pid = null;
460

    
461
	                if (extra != null) {
462
		                // get the format
463
		                int formatIndex = extra.length();
464
		                if (extra.indexOf("/") > -1) {
465
		                	formatIndex = extra.indexOf("/");
466
		                }
467
		                format = extra.substring(0, formatIndex);
468
		                logMetacat.debug("view format: " + format);
469
		                
470
		                // get the pid if it is there
471
		                pid = extra.substring(formatIndex, extra.length());
472
		                if (pid != null && pid.length() == 0) {
473
		                	pid = null;
474
		                } else {
475
		                	if (pid.startsWith("/")) {
476
		                		pid = pid.substring(1);
477
		                    }
478
		                }
479
		                logMetacat.debug("pid: " + pid);
480

    
481
	                }
482
	                logMetacat.debug("verb:" + httpVerb);
483
	                if (httpVerb == GET) {
484
	                    doViews(format, pid);
485
	                    status = true;
486
	                }
487
                } 
488
                
489
                if (!status) {
490
                	throw new ServiceFailure("0000", "Unknown error, status = " + status);
491
                }
492
            } else {
493
            	throw new InvalidRequest("0000", "No resource matched for " + resource);
494
            }
495
        } catch (BaseException be) {
496
        	// report Exceptions as clearly as possible
497
        	OutputStream out = null;
498
			try {
499
				out = response.getOutputStream();
500
			} catch (IOException e) {
501
				logMetacat.error("Could not get output stream from response", e);
502
			}
503
            serializeException(be, out);
504
        } catch (Exception e) {
505
            // report Exceptions as clearly and generically as possible
506
            logMetacat.error(e.getClass() + ": " + e.getMessage(), e);
507
        	OutputStream out = null;
508
			try {
509
				out = response.getOutputStream();
510
			} catch (IOException ioe) {
511
				logMetacat.error("Could not get output stream from response", ioe);
512
			}
513
			ServiceFailure se = new ServiceFailure("0000", e.getMessage());
514
            serializeException(se, out);
515
        }
516
    }
517
    
518

    
519
    private void doQuery(String engine, String query) {
520
    	
521
		OutputStream out = null;
522

    
523
    	try {
524
    		// NOTE: we set the session explicitly for the MNode instance since these methods do not provide a parameter			
525
	    	if (engine == null) {
526
	    		// just looking for list of engines
527
	    		MNodeService mnode = MNodeService.getInstance(request);
528
    			mnode.setSession(session);
529
	    		QueryEngineList qel = mnode.listQueryEngines(session);
530
	    		response.setContentType("text/xml");
531
	            response.setStatus(200);
532
	            out = response.getOutputStream();
533
	            TypeMarshaller.marshalTypeToOutputStream(qel, out);
534
	            return;
535
	    	} else {
536
	    		if (query != null) {
537
	    			MNodeService mnode = MNodeService.getInstance(request);
538
	    			mnode.setSession(session);
539
	    			InputStream stream = mnode.query(session, engine, query);
540

    
541
	    			// set the content-type if we have it from the implementation
542
	    			if (stream instanceof ContentTypeInputStream) {
543
		    			//response.setContentType("application/octet-stream");
544
		    			//response.setContentType("text/xml");
545
	    				response.setContentType(((ContentTypeInputStream) stream).getContentType());
546
	    			}
547
	                response.setStatus(200);
548
	                out = response.getOutputStream();
549
	                // write the results to the output stream
550
	                IOUtils.copyLarge(stream, out);
551
	                return;
552
	    		} else {
553
	    			MNodeService mnode = MNodeService.getInstance(request);
554
	    			mnode.setSession(session);
555
	    			QueryEngineDescription qed = mnode.getQueryEngineDescription(session, engine);
556
	    			response.setContentType("text/xml");
557
		            response.setStatus(200);
558
		            out = response.getOutputStream();
559
		            TypeMarshaller.marshalTypeToOutputStream(qed, out);
560
		            return;
561
	    		}
562
	    	}
563
	        
564
	        
565
    	} catch (BaseException be) {
566
        	// report Exceptions as clearly as possible
567
			try {
568
				out = response.getOutputStream();
569
			} catch (IOException e) {
570
				logMetacat.error("Could not get output stream from response", e);
571
			}
572
            serializeException(be, out);
573
        } catch (Exception e) {
574
            // report Exceptions as clearly and generically as possible
575
            logMetacat.error(e.getClass() + ": " + e.getMessage(), e);
576
			try {
577
				out = response.getOutputStream();
578
			} catch (IOException ioe) {
579
				logMetacat.error("Could not get output stream from response", ioe);
580
			}
581
			ServiceFailure se = new ServiceFailure("0000", e.getMessage());
582
            serializeException(se, out);
583
        }
584
    }
585
    
586
    private void doViews(String format, String pid) {
587
    	
588
		OutputStream out = null;
589
		MNodeService mnode = MNodeService.getInstance(request);
590

    
591
    	try {
592
    		// get a list of views
593
    		if (pid != null) {
594
    			Identifier identifier = new Identifier();
595
    			identifier.setValue(pid);
596
				InputStream stream = mnode.view(session, format, identifier);
597

    
598
    			// set the content-type if we have it from the implementation
599
    			if (stream instanceof ContentTypeInputStream) {
600
    				response.setContentType(((ContentTypeInputStream) stream).getContentType());
601
    			}
602
                response.setStatus(200);
603
                out = response.getOutputStream();
604
                // write the results to the output stream
605
                IOUtils.copyLarge(stream, out);
606
                return;
607
    		} else {
608
    			// TODO: list the registered views
609
                //BaseException ni = new NotImplemented("9999", "MN.listViews() is not implemented at this node");
610
				//throw ni;
611
    		    OptionList list = mnode.listViews(session);
612
    	        
613
    	        response.setContentType("text/xml");
614
    	        response.setStatus(200);
615
    	        TypeMarshaller.marshalTypeToOutputStream(list, response.getOutputStream());
616
    		}
617
	    	
618
	        
619
    	} catch (BaseException be) {
620
        	// report Exceptions as clearly as possible
621
			try {
622
				out = response.getOutputStream();
623
			} catch (IOException e) {
624
				logMetacat.error("Could not get output stream from response", e);
625
			}
626
            serializeException(be, out);
627
        } catch (Exception e) {
628
            // report Exceptions as clearly and generically as possible
629
            logMetacat.error(e.getClass() + ": " + e.getMessage(), e);
630
			try {
631
				out = response.getOutputStream();
632
			} catch (IOException ioe) {
633
				logMetacat.error("Could not get output stream from response", ioe);
634
			}
635
			ServiceFailure se = new ServiceFailure("0000", e.getMessage());
636
            serializeException(se, out);
637
        }
638
    }
639
    
640
    /**
641
     * Handles notification of system metadata changes for the given identifier
642
     * 
643
     * @param id  the identifier for the object
644
     * @throws InvalidToken 
645
     * @throws InvalidRequest 
646
     * @throws NotAuthorized 
647
     * @throws ServiceFailure 
648
     * @throws NotImplemented 
649
     */
650
    private void systemMetadataChanged() 
651
        throws NotImplemented, ServiceFailure, NotAuthorized, InvalidRequest, 
652
        InvalidToken {
653

    
654
        long serialVersion = 0L;
655
        String serialVersionStr = null;
656
        Date dateSysMetaLastModified = null;
657
        String dateSysMetaLastModifiedStr = null;
658
        Identifier pid = null;
659
        
660
        // mkae sure we have the multipart params
661
        try {
662
			initMultipartParams();
663
		} catch (Exception e1) {
664
			throw new ServiceFailure("1333", "Could not collect the multipart params for the request");
665
		}
666
        
667
        // get the pid
668
        try {
669
        	String id = multipartparams.get("pid").get(0);
670
        	pid = new Identifier();
671
            pid.setValue(id);            
672
        } catch (NullPointerException e) {
673
            String msg = "The 'pid' must be provided as a parameter and was not.";
674
            logMetacat.error(msg);
675
            throw new InvalidRequest("1334", msg);
676
        }      
677
        
678
        // get the serialVersion
679
        try {
680
            serialVersionStr = multipartparams.get("serialVersion").get(0);
681
            serialVersion = new Long(serialVersionStr).longValue();
682
            
683
        } catch (NullPointerException e) {
684
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
685
            logMetacat.error(msg);
686
            throw new InvalidRequest("1334", msg);
687
            
688
        }       
689
        
690
        // get the dateSysMetaLastModified
691
        try {
692
            dateSysMetaLastModifiedStr = multipartparams.get("dateSysMetaLastModified").get(0);
693
            dateSysMetaLastModified = DateTimeMarshaller.deserializeDateToUTC(dateSysMetaLastModifiedStr);
694
            
695
        } catch (NullPointerException e) {
696
            String msg = 
697
                "The 'dateSysMetaLastModified' must be provided as a " + 
698
                "parameter and was not, or was an invalid representation of the timestamp.";
699
            logMetacat.error(msg);
700
            throw new InvalidRequest("1334", msg);
701
            
702
        }       
703
        
704
        // call the service
705
        MNodeService.getInstance(request).systemMetadataChanged(session, pid, serialVersion, dateSysMetaLastModified);
706
        response.setStatus(200);
707
    }
708
    
709
    /**
710
     * Handles identifier generation calls
711
     * 
712
     * @throws InvalidRequest 
713
     * @throws NotImplemented 
714
     * @throws NotAuthorized 
715
     * @throws ServiceFailure 
716
     * @throws InvalidToken 
717
     * @throws IOException 
718
     * @throws JiBXException 
719
     */
720
    private void generateIdentifier() throws InvalidToken, ServiceFailure, NotAuthorized, NotImplemented, InvalidRequest, IOException, JiBXException {
721
        
722
        // make sure we have the multipart params
723
        try {
724
			initMultipartParams();
725
		} catch (Exception e1) {
726
			throw new ServiceFailure("1333", "Could not collect the multipart params for the request");
727
		}
728
        
729
        // get the scheme
730
		String scheme = null;
731
        try {
732
        	scheme = multipartparams.get("scheme").get(0);	           
733
        } catch (NullPointerException e) {
734
            String msg = "The 'scheme' parameter was not provided, using default";
735
            logMetacat.warn(msg);
736
        }
737
        
738
        // get the fragment
739
		String fragment = null;
740
        try {
741
        	fragment = multipartparams.get("fragment").get(0);	           
742
        } catch (NullPointerException e) {
743
            String msg = "The 'fragment' parameter was not provided, using default";
744
            logMetacat.warn(msg);
745
        }
746

    
747
        // call the service
748
        Identifier identifier = MNodeService.getInstance(request).generateIdentifier(session, scheme, fragment);
749
        response.setStatus(200);
750
        response.setContentType("text/xml");
751
        OutputStream out = response.getOutputStream();
752
        TypeMarshaller.marshalTypeToOutputStream(identifier, out);
753
    }
754

    
755
    /**
756
     * Checks the access policy
757
     * @param id
758
     * @return
759
     * @throws ServiceFailure
760
     * @throws InvalidToken
761
     * @throws NotFound
762
     * @throws NotAuthorized
763
     * @throws NotImplemented
764
     * @throws InvalidRequest
765
     */
766
    private boolean isAuthorized(String id) throws ServiceFailure, InvalidToken, NotFound, NotAuthorized, NotImplemented, InvalidRequest {
767
		Identifier pid = new Identifier();
768
		pid.setValue(id);
769
		Permission permission = null;
770
		try {
771
			String perm = params.get("action")[0];
772
			permission = Permission.convert(perm);
773
		} catch (Exception e) {
774
			logMetacat.warn("No permission specified");
775
		}
776
		boolean result = MNodeService.getInstance(request).isAuthorized(session, pid, permission);
777
		response.setStatus(200);
778
		response.setContentType("text/xml");
779
		return result;
780
    }
781
    
782
    private void getToken() throws Exception {
783
		
784
		if (this.session != null) {
785
			String userId = this.session.getSubject().getValue();
786
			String fullName = null;
787
			try {
788
				Person person = this.session.getSubjectInfo().getPerson(0);
789
				fullName = person.getGivenName(0) + " " + person.getFamilyName();
790
			} catch (Exception e) {
791
				logMetacat.warn(e.getMessage(), e);
792
			}
793
			
794
			String token = null;
795
			token = TokenGenerator.getInstance().getJWT(userId, fullName);
796
			
797
			response.setStatus(200);
798
			response.setContentType("text/plain");
799
	        OutputStream out = response.getOutputStream();
800
	        out.write(token.getBytes(MetaCatServlet.DEFAULT_ENCODING));
801
	        out.close();
802
		} else {
803
			response.setStatus(401);
804
			response.setContentType("text/plain");
805
			OutputStream out = response.getOutputStream();
806
	        out.write("No session information found".getBytes(MetaCatServlet.DEFAULT_ENCODING));
807
	        out.close();
808
		}
809
		
810
    }
811
    
812
    private void whoami() throws Exception {
813
		
814
		if (this.session != null) {
815
			Subject subject = this.session.getSubject();
816
			SubjectInfo subjectInfo = null;
817
			try {
818
				subjectInfo = this.session.getSubjectInfo();
819
			} catch (Exception e) {
820
				logMetacat.warn(e.getMessage(), e);
821
			}
822
			
823
			response.setStatus(200);
824
			response.setContentType("text/plain");
825
	        OutputStream out = response.getOutputStream();
826
	        
827
	        if (subjectInfo != null) {
828
		        TypeMarshaller.marshalTypeToOutputStream(subjectInfo, out);
829
	        } else {
830
		        TypeMarshaller.marshalTypeToOutputStream(subject, out);
831
	        }
832
	        
833
	        out.close();
834
		} else {
835
			response.setStatus(401);
836
			response.setContentType("text/plain");
837
			OutputStream out = response.getOutputStream();
838
	        out.write("No session information found".getBytes(MetaCatServlet.DEFAULT_ENCODING));
839
	        out.close();
840
		}
841
		
842
    }
843
    
844
    /**
845
     * Processes failed synchronization message
846
     * @throws NotImplemented
847
     * @throws ServiceFailure
848
     * @throws NotAuthorized
849
     * @throws InvalidRequest
850
     * @throws JiBXException
851
     * @throws IllegalAccessException 
852
     * @throws InstantiationException 
853
     * @throws IOException 
854
     */
855
    private void syncError() throws NotImplemented, ServiceFailure, NotAuthorized, InvalidRequest, JiBXException, IOException, InstantiationException, IllegalAccessException {
856
    	SynchronizationFailed syncFailed = null;
857
		try {
858
			syncFailed = collectSynchronizationFailed();
859
		} catch (ParserConfigurationException e) {
860
			throw new ServiceFailure("2161", e.getMessage());
861
		} catch (SAXException e) {
862
			throw new ServiceFailure("2161", e.getMessage());
863
		}
864
		
865
		MNodeService.getInstance(request).synchronizationFailed(session, syncFailed);
866
    }
867

    
868
	/**
869
     * Calculate the checksum 
870
     * @throws NotImplemented
871
     * @throws JiBXException
872
     * @throws IOException
873
     * @throws InvalidToken
874
     * @throws ServiceFailure
875
     * @throws NotAuthorized
876
     * @throws NotFound
877
     * @throws InvalidRequest
878
     */
879
    private void checksum(String pid) throws NotImplemented, JiBXException, IOException, InvalidToken, ServiceFailure, NotAuthorized, NotFound, InvalidRequest {
880
    	String checksumAlgorithm = "MD5";
881
    	try {
882
    		checksumAlgorithm = PropertyService.getProperty("dataone.checksumAlgorithm.default");
883
        } catch(Exception e) {
884
        	logMetacat.warn("Could not lookup configured default checksum algorithm, using: " + checksumAlgorithm);
885
        }    
886
        
887
        Identifier pidid = new Identifier();
888
        pidid.setValue(pid);
889
        try {
890
            checksumAlgorithm = params.get("checksumAlgorithm")[0];
891
        } catch(Exception e) {
892
            //do nothing.  use the default
893
        	logMetacat.warn("No algorithm specified, using default: " + checksumAlgorithm);
894
        }
895
        logMetacat.debug("getting checksum for object " + pid + " with algorithm " + checksumAlgorithm);
896
        
897
        Checksum c = MNodeService.getInstance(request).getChecksum(session, pidid, checksumAlgorithm);
898
        logMetacat.debug("got checksum " + c.getValue());
899
        response.setStatus(200);
900
        logMetacat.debug("serializing response");
901
        TypeMarshaller.marshalTypeToOutputStream(c, response.getOutputStream());
902
        logMetacat.debug("done serializing response.");
903
        
904
    }
905
    
906
	/**
907
     * handle the replicate action for MN
908
	 * @throws JiBXException 
909
	 * @throws FileUploadException 
910
	 * @throws IOException 
911
	 * @throws InvalidRequest 
912
	 * @throws ServiceFailure 
913
	 * @throws UnsupportedType 
914
	 * @throws InsufficientResources 
915
	 * @throws NotAuthorized 
916
	 * @throws NotImplemented 
917
	 * @throws IllegalAccessException 
918
	 * @throws InstantiationException 
919
	 * @throws InvalidToken 
920
     */
921
    private void replicate() 
922
        throws ServiceFailure, InvalidRequest, IOException, FileUploadException, 
923
        JiBXException, NotImplemented, NotAuthorized, InsufficientResources, 
924
        UnsupportedType, InstantiationException, IllegalAccessException, InvalidToken {
925

    
926
        logMetacat.debug("in POST replicate()");
927
        
928
        // somewhat unorthodox, but the call is asynchronous and we'd like to return this info sooner
929
        boolean allowed = false;
930
        if (session == null) {
931
        	String msg = "No session was provided.";
932
            NotAuthorized failure = new NotAuthorized("2152", msg);
933
        	throw failure;
934
        } else {
935
        	allowed = MNodeService.getInstance(request).isAdminAuthorized(session);
936
        	if (!allowed) {
937
        		String msg = "User is not an admin user";
938
                NotAuthorized failure = new NotAuthorized("2152", msg);
939
            	throw failure;
940
        	}
941
        }
942
        
943
        // parse the systemMetadata
944
        Map<String, File> files = collectMultipartFiles();        
945
        final SystemMetadata sysmeta = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, files.get("sysmeta"));
946
        
947
        String sn = multipartparams.get("sourceNode").get(0);
948
        logMetacat.debug("sourceNode: " + sn);
949
        final NodeReference sourceNode = new NodeReference();
950
        sourceNode.setValue(sn);
951
        
952
        // run it in a thread to avoid connection timeout
953
        Runnable runner = new Runnable() {
954
			@Override
955
			public void run() {
956
				try {
957
			        MNodeService.getInstance(request).replicate(session, sysmeta, sourceNode);
958
				} catch (Exception e) {
959
					logMetacat.error("Error running replication: " + e.getMessage(), e);
960
					throw new RuntimeException(e.getMessage(), e);
961
				}
962
			}
963
    	};
964
    	// submit the task, and that's it
965
    	executor.submit(runner);
966
        
967
    	// thread was started, so we return success
968
        response.setStatus(200);
969
        
970
    }
971

    
972
    /**
973
     * Handle the getReplica action for the MN
974
     * @param id  the identifier for the object
975
     * @throws NotFound 
976
     * @throws ServiceFailure 
977
     * @throws NotImplemented 
978
     * @throws NotAuthorized 
979
     * @throws InvalidToken 
980
     * @throws InvalidRequest 
981
     */
982
    private void getReplica(String id) 
983
        throws InvalidRequest, InvalidToken, NotAuthorized, NotImplemented, 
984
        ServiceFailure, NotFound {
985
        
986
        Identifier pid = new Identifier();
987
        pid.setValue(id);
988
        OutputStream out = null;
989
        InputStream dataBytes = null;
990
                
991
        try {
992
            // call the service
993
            dataBytes = MNodeService.getInstance(request).getReplica(session, pid);
994

    
995
            response.setContentType("application/octet-stream");
996
            response.setStatus(200);
997
            out = response.getOutputStream();
998
            // write the object to the output stream
999
            IOUtils.copyLarge(dataBytes, out);
1000
            
1001
        } catch (IOException e) {
1002
            String msg = "There was an error writing the output: " + e.getMessage();
1003
            logMetacat.error(msg);
1004
            throw new ServiceFailure("2181", msg);
1005
        
1006
        }
1007

    
1008
    }
1009

    
1010
    /**
1011
     * Get the Node information
1012
     * 
1013
     * @throws JiBXException
1014
     * @throws IOException
1015
     * @throws InvalidRequest 
1016
     * @throws ServiceFailure 
1017
     * @throws NotAuthorized 
1018
     * @throws NotImplemented 
1019
     */
1020
    private void node() 
1021
        throws JiBXException, IOException, NotImplemented, NotAuthorized, ServiceFailure, InvalidRequest {
1022
        
1023
        Node n = MNodeService.getInstance(request).getCapabilities();
1024
        
1025
        response.setContentType("text/xml");
1026
        response.setStatus(200);
1027
        TypeMarshaller.marshalTypeToOutputStream(n, response.getOutputStream());
1028
        
1029
    }
1030
    
1031
    /**
1032
     * MN_crud.describe()
1033
     * http://mule1.dataone.org/ArchitectureDocs/mn_api_crud.html#MN_crud.describe
1034
     * @param pid
1035
     * @throws InvalidRequest 
1036
     * @throws NotImplemented 
1037
     * @throws NotFound 
1038
     * @throws NotAuthorized 
1039
     * @throws ServiceFailure 
1040
     * @throws InvalidToken 
1041
     */
1042
    private void describeObject(String pid) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented, InvalidRequest
1043
    {
1044
        
1045
        response.setContentType("text/xml");
1046

    
1047
        Identifier id = new Identifier();
1048
        id.setValue(pid);
1049

    
1050
        DescribeResponse dr = null;
1051
        try {
1052
        	dr = MNodeService.getInstance(request).describe(session, id);
1053
        } catch (BaseException e) {
1054
        	response.setStatus(e.getCode());
1055
        	response.addHeader("DataONE-Exception-Name", e.getClass().getName());
1056
            response.addHeader("DataONE-Exception-DetailCode", e.getDetail_code());
1057
            response.addHeader("DataONE-Exception-Description", e.getDescription());
1058
            response.addHeader("DataONE-Exception-PID", id.getValue());
1059
            return;
1060
		}
1061
        
1062
        response.setStatus(200);
1063
        
1064
        //response.addHeader("pid", pid);
1065
        response.addHeader("DataONE-Checksum", dr.getDataONE_Checksum().getAlgorithm() + "," + dr.getDataONE_Checksum().getValue());
1066
        response.addHeader("Content-Length", dr.getContent_Length() + "");
1067
        response.addHeader("Last-Modified", DateTimeMarshaller.serializeDateToUTC(dr.getLast_Modified()));
1068
        response.addHeader("DataONE-ObjectFormat", dr.getDataONE_ObjectFormatIdentifier().getValue());
1069
        response.addHeader("DataONE-SerialVersion", dr.getSerialVersion().toString());
1070

    
1071
        
1072
    }
1073
    
1074
    /**
1075
     * get the logs based on passed params.  Available 
1076
     * See http://mule1.dataone.org/ArchitectureDocs/mn_api_crud.html#MN_crud.getLogRecords
1077
     * for more info
1078
     * @throws NotImplemented 
1079
     * @throws InvalidRequest 
1080
     * @throws NotAuthorized 
1081
     * @throws ServiceFailure 
1082
     * @throws InvalidToken 
1083
     * @throws IOException 
1084
     * @throws JiBXException 
1085
     */
1086
    private void getLog() throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest, NotImplemented, IOException, JiBXException
1087
    {
1088
            
1089
        Date fromDate = null;
1090
        Date toDate = null;
1091
        String event = null;
1092
        Integer start = null;
1093
        Integer count = null;
1094
        String pidFilter = null;
1095
        
1096
        try {
1097
        	String fromDateS = params.get("fromDate")[0];
1098
            logMetacat.debug("param fromDateS: " + fromDateS);
1099
            fromDate = DateTimeMarshaller.deserializeDateToUTC(fromDateS);
1100
        } catch (Exception e) {
1101
        	logMetacat.warn("Could not parse fromDate: " + e.getMessage());
1102
        }
1103
        try {
1104
        	String toDateS = params.get("toDate")[0];
1105
            logMetacat.debug("param toDateS: " + toDateS);
1106
            toDate = DateTimeMarshaller.deserializeDateToUTC(toDateS);
1107
        } catch (Exception e) {
1108
        	logMetacat.warn("Could not parse toDate: " + e.getMessage());
1109
		}
1110
        try {
1111
        	event = params.get("event")[0];
1112
        } catch (Exception e) {
1113
        	logMetacat.warn("Could not parse event: " + e.getMessage());
1114
		}
1115
        logMetacat.debug("fromDate: " + fromDate + " toDate: " + toDate);
1116
        
1117
        try {
1118
        	start =  Integer.parseInt(params.get("start")[0]);
1119
        } catch (Exception e) {
1120
			logMetacat.warn("Could not parse start: " + e.getMessage());
1121
		}
1122
        try {
1123
        	count =  Integer.parseInt(params.get("count")[0]);
1124
        } catch (Exception e) {
1125
			logMetacat.warn("Could not parse count: " + e.getMessage());
1126
		}
1127
        
1128
        try {
1129
            pidFilter = params.get("pidFilter")[0];
1130
        } catch (Exception e) {
1131
            logMetacat.warn("Could not parse pidFilter: " + e.getMessage());
1132
        }
1133
        
1134
        logMetacat.debug("calling getLogRecords");
1135
        Log log = MNodeService.getInstance(request).getLogRecords(session, fromDate, toDate, event, pidFilter, start, count);
1136
        
1137
        OutputStream out = response.getOutputStream();
1138
        response.setStatus(200);
1139
        response.setContentType("text/xml");
1140
        
1141
        TypeMarshaller.marshalTypeToOutputStream(log, out);
1142
        
1143
    }
1144
    
1145
    
1146
    
1147
    /**
1148
     * Implements REST version of DataONE CRUD API --> get
1149
     * @param pid ID of data object to be read
1150
     * @throws NotImplemented 
1151
     * @throws InvalidRequest 
1152
     * @throws NotFound 
1153
     * @throws NotAuthorized 
1154
     * @throws ServiceFailure 
1155
     * @throws InvalidToken 
1156
     * @throws IOException 
1157
     * @throws JiBXException 
1158
     */
1159
    protected void getObject(String pid) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, InvalidRequest, NotImplemented, IOException, JiBXException {
1160
        OutputStream out = null;
1161
        
1162
        if (pid != null) { //get a specific document                
1163
            Identifier id = new Identifier();
1164
            id.setValue(pid);
1165
                
1166
            SystemMetadata sm = MNodeService.getInstance(request).getSystemMetadata(session, id);
1167
            
1168
            // set the headers for the content
1169
            String mimeType = null;
1170
            String charset = null;
1171
            
1172
            // do we have mediaType/encoding in SM?
1173
            MediaType mediaType = sm.getMediaType();
1174
            if (mediaType == null) {
1175
            	try {
1176
            		mediaType = ObjectFormatCache.getInstance().getFormat(sm.getFormatId()).getMediaType();
1177
            	} catch (BaseException be) {
1178
            		logMetacat.warn("Could not lookup ObjectFormat MediaType for: " + sm.getFormatId(), be);
1179
            	}
1180
            }
1181
            if (mediaType != null) {
1182
                mimeType = sm.getMediaType().getName();
1183
                if (sm.getMediaType().getPropertyList() != null) {
1184
                	Iterator<MediaTypeProperty> iter = sm.getMediaType().getPropertyList().iterator();
1185
                	while (iter.hasNext()) {
1186
                		MediaTypeProperty mtp = iter.next();
1187
                		if (mtp.getName().equalsIgnoreCase("charset")) {
1188
                			charset = mtp.getValue();
1189
                			mimeType += "; charset=" + charset;
1190
                			break;
1191
                		}
1192
                	}
1193
                }
1194
            }
1195
            // check object format
1196
            
1197
            // use the fallback from v1 impl
1198
            if (mimeType == null) {
1199
	            mimeType = ObjectFormatInfo.instance().getMimeType(sm.getFormatId().getValue());
1200
	            
1201
	            // still null?
1202
	            if (mimeType == null) {
1203
	            	mimeType = "application/octet-stream";
1204
	            }
1205
            }
1206
            
1207
            // check for filename in SM first
1208
            String filename = sm.getFileName();
1209
            // then fallback to using id and extension
1210
            if (filename == null) {
1211
	            String extension = ObjectFormatInfo.instance().getExtension(sm.getFormatId().getValue());
1212
	            filename = id.getValue();
1213
	            if (extension != null) {
1214
	            	filename = id.getValue() + extension;
1215
	            }
1216
            }
1217
            response.setContentType(mimeType);
1218
            response.setHeader("Content-Disposition", "inline; filename=" + filename);
1219
            
1220
            InputStream data = MNodeService.getInstance(request).get(session, id);
1221

    
1222
            out = response.getOutputStream();  
1223
            IOUtils.copyLarge(data, out);
1224
            
1225
        }
1226
        else
1227
        { //call listObjects with specified params
1228
            Date startTime = null;
1229
            Date endTime = null;
1230
            ObjectFormatIdentifier formatId = null;
1231
            Identifier identifier = null;
1232
            boolean replicaStatus = true;
1233
            int start = 0;
1234
            //TODO: make the max count into a const
1235
            int count = 1000;
1236
            Enumeration paramlist = request.getParameterNames();
1237
            while (paramlist.hasMoreElements()) 
1238
            { //parse the params and make the crud call
1239
                String name = (String) paramlist.nextElement();
1240
                String[] value = (String[])request.getParameterValues(name);
1241

    
1242
                if (name.equals("fromDate") && value != null)
1243
                {
1244
                    try
1245
                    {
1246
                      //startTime = dateFormat.parse(value[0]);
1247
                    	startTime = DateTimeMarshaller.deserializeDateToUTC(value[0]);
1248
                        //startTime = parseDateAndConvertToGMT(value[0]);
1249
                    }
1250
                    catch(Exception e)
1251
                    {  //if we can't parse it, just don't use the fromDate param
1252
                        logMetacat.warn("Could not parse fromDate: " + value[0]);
1253
                        startTime = null;
1254
                    }
1255
                }
1256
                else if(name.equals("toDate") && value != null)
1257
                {
1258
                    try
1259
                    {
1260
                    	endTime = DateTimeMarshaller.deserializeDateToUTC(value[0]);
1261
                    }
1262
                    catch(Exception e)
1263
                    {  //if we can't parse it, just don't use the toDate param
1264
                        logMetacat.warn("Could not parse toDate: " + value[0]);
1265
                        endTime = null;
1266
                    }
1267
                }
1268
                else if(name.equals("formatId") && value != null) 
1269
                {
1270
                	formatId = new ObjectFormatIdentifier();
1271
                	formatId.setValue(value[0]);
1272
                }
1273
                else if(name.equals("identifier") && value != null) 
1274
                {
1275
                	identifier = new Identifier();
1276
                	identifier.setValue(value[0]);
1277
                }
1278
                else if(name.equals("replicaStatus") && value != null)
1279
                {
1280
                    if(value != null && 
1281
                       value.length > 0 && 
1282
                       (value[0].equalsIgnoreCase("false") || value[0].equalsIgnoreCase("no")))
1283
                    {
1284
                        replicaStatus = false;
1285
                    }
1286
                }
1287
                else if(name.equals("start") && value != null)
1288
                {
1289
                    start = new Integer(value[0]).intValue();
1290
                }
1291
                else if(name.equals("count") && value != null)
1292
                {
1293
                    count = new Integer(value[0]).intValue();
1294
                }
1295
            }
1296
            //make the crud call
1297
            logMetacat.debug("session: " + session + " startTime: " + startTime +
1298
                    " endTime: " + endTime + " formatId: " + 
1299
                    formatId + " replicaStatus: " + replicaStatus + 
1300
                    " start: " + start + " count: " + count);
1301
           
1302
            ObjectList ol = 
1303
           	 MNodeService.getInstance(request).listObjects(session, startTime, endTime, 
1304
           			formatId, identifier, replicaStatus, start, count);
1305
           
1306
            out = response.getOutputStream();  
1307
            response.setStatus(200);
1308
            response.setContentType("text/xml");
1309
            // Serialize and write it to the output stream
1310
            TypeMarshaller.marshalTypeToOutputStream(ol, out);
1311
            
1312
        }
1313
        
1314
    }
1315
    
1316

    
1317
    /**
1318
     * Retrieve data package as Bagit zip
1319
     * @param pid
1320
     * @throws NotImplemented 
1321
     * @throws NotFound 
1322
     * @throws NotAuthorized 
1323
     * @throws ServiceFailure 
1324
     * @throws InvalidToken 
1325
     * @throws IOException 
1326
     * @throws InvalidRequest 
1327
     */
1328
    protected void getPackage(String format, String pid) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented, IOException, InvalidRequest {
1329

    
1330
        Identifier id = new Identifier();
1331
        id.setValue(pid);
1332
        ObjectFormatIdentifier formatId = null;
1333
        if (format != null) {
1334
        	formatId = new ObjectFormatIdentifier();
1335
        	formatId.setValue(format);
1336
        }
1337
		InputStream is = MNodeService.getInstance(request).getPackage(session, formatId , id);
1338
        
1339
        // use the provided filename
1340
        String filename = null;
1341
        if (is instanceof DeleteOnCloseFileInputStream) {
1342
            filename = ((DeleteOnCloseFileInputStream)is).getFile().getName();
1343
        } else {
1344
        	filename = "dataPackage-" + System.currentTimeMillis() + ".zip";
1345
        }
1346
        response.setHeader("Content-Disposition", "inline; filename=" + filename);
1347
        response.setContentType("application/zip");
1348
        response.setStatus(200);
1349
        OutputStream out = response.getOutputStream();
1350
        
1351
        // write it to the output stream
1352
        IOUtils.copyLarge(is, out);
1353
   }
1354
    
1355
	protected void publish(String pid) throws InvalidToken, ServiceFailure,
1356
			NotAuthorized, NotFound, NotImplemented, IOException,
1357
			JiBXException, InvalidRequest, IdentifierNotUnique,
1358
			UnsupportedType, InsufficientResources, InvalidSystemMetadata {
1359

    
1360
		// publish the object
1361
		Identifier originalIdentifier = new Identifier();
1362
		originalIdentifier.setValue(pid);
1363
		Identifier newIdentifier = MNodeService.getInstance(request).publish(session, originalIdentifier);
1364

    
1365
		response.setStatus(200);
1366
		response.setContentType("text/xml");
1367
		OutputStream out = response.getOutputStream();
1368

    
1369
		// write new identifier to the output stream
1370
		TypeMarshaller.marshalTypeToOutputStream(newIdentifier, out);
1371
	}
1372
    
1373
    /**
1374
     * Retrieve System Metadata
1375
     * @param pid
1376
     * @throws InvalidToken
1377
     * @throws ServiceFailure
1378
     * @throws NotAuthorized
1379
     * @throws NotFound
1380
     * @throws InvalidRequest
1381
     * @throws NotImplemented
1382
     * @throws IOException
1383
     * @throws JiBXException
1384
     */
1385
    protected void getSystemMetadataObject(String pid) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, InvalidRequest, NotImplemented, IOException, JiBXException {
1386

    
1387
        Identifier id = new Identifier();
1388
        id.setValue(pid);
1389
        SystemMetadata sysmeta = MNodeService.getInstance(request).getSystemMetadata(session, id);
1390
        
1391
        response.setContentType("text/xml");
1392
        response.setStatus(200);
1393
        OutputStream out = response.getOutputStream();
1394
        
1395
        // Serialize and write it to the output stream
1396
        TypeMarshaller.marshalTypeToOutputStream(sysmeta, out);
1397
   }
1398
    
1399
    
1400
    /**
1401
     * Inserts or updates the object
1402
     * 
1403
     * @param pid - ID of data object to be inserted or updated.  If action is update, the pid
1404
     *               is the existing pid.  If insert, the pid is the new one
1405
     * @throws InvalidRequest 
1406
     * @throws ServiceFailure 
1407
     * @throws JiBXException 
1408
     * @throws NotImplemented 
1409
     * @throws InvalidSystemMetadata 
1410
     * @throws InsufficientResources 
1411
     * @throws UnsupportedType 
1412
     * @throws IdentifierNotUnique 
1413
     * @throws NotAuthorized 
1414
     * @throws InvalidToken 
1415
     * @throws NotFound 
1416
     * @throws IOException 
1417
     * @throws IllegalAccessException 
1418
     * @throws InstantiationException 
1419
     */
1420
    protected void putObject(String trailingPid, String action) throws ServiceFailure, InvalidRequest, JiBXException, InvalidToken, NotAuthorized, IdentifierNotUnique, UnsupportedType, InsufficientResources, InvalidSystemMetadata, NotImplemented, NotFound, IOException, InstantiationException, IllegalAccessException {
1421
       
1422
    	// Read the incoming data from its Mime Multipart encoding
1423
    	Map<String, File> files = collectMultipartFiles();
1424
               
1425
    	Identifier pid = new Identifier();
1426
        if (trailingPid == null) {
1427
	        // get the pid string from the body and set the value
1428
	        String pidString = multipartparams.get("pid").get(0);
1429
	        if (pidString != null) {
1430
            pid.setValue(pidString);
1431
            
1432
          } else {
1433
              throw new InvalidRequest("1102", "The pid param must be included and contain the identifier.");
1434
              
1435
          }
1436
        } else {
1437
        	// use the pid included in the URL
1438
        	pid.setValue(trailingPid);
1439
        }
1440
        logMetacat.debug("putObject with pid " + pid.getValue());
1441
        logMetacat.debug("Entering putObject: " + pid.getValue() + "/" + action);
1442

    
1443
        InputStream object = null;
1444
        InputStream sysmeta = null;
1445
        File smFile = files.get("sysmeta");
1446
        sysmeta = new FileInputStream(smFile);
1447
        File objFile = files.get("object");
1448
        object = new FileInputStream(objFile);
1449
        
1450
        // ensure we have the object bytes
1451
        if  ( objFile == null ) {
1452
            throw new InvalidRequest("1102", "The object param must contain the object bytes.");
1453
            
1454
        }
1455
        
1456
        // ensure we have the system metadata
1457
        if  ( smFile == null ) {
1458
            throw new InvalidRequest("1102", "The sysmeta param must contain the system metadata document.");
1459
            
1460
        }
1461
        
1462
        response.setStatus(200);
1463
        response.setContentType("text/xml");
1464
        OutputStream out = response.getOutputStream();
1465
        
1466
        if (action.equals(FUNCTION_NAME_INSERT)) { 
1467
            // handle inserts
1468
            logMetacat.debug("Commence creation...");
1469
            SystemMetadata smd = TypeMarshaller.unmarshalTypeFromStream(SystemMetadata.class, sysmeta);
1470

    
1471
            logMetacat.debug("creating object with pid " + pid.getValue());
1472
            Identifier rId = MNodeService.getInstance(request).create(session, pid, object, smd);
1473
            TypeMarshaller.marshalTypeToOutputStream(rId, out);
1474
            
1475
        } else if (action.equals(FUNCTION_NAME_UPDATE)) {
1476
        	// handle updates
1477
        	
1478
            // construct pids
1479
            Identifier newPid = null;
1480
            try {
1481
            	String newPidString = multipartparams.get("newPid").get(0);
1482
            	newPid = new Identifier();
1483
            	newPid.setValue(newPidString);
1484
            } catch (Exception e) {
1485
				logMetacat.error("Could not get newPid from request");
1486
			}
1487
            logMetacat.debug("Commence update...");
1488
            
1489
            // get the systemmetadata object
1490
            SystemMetadata smd = TypeMarshaller.unmarshalTypeFromStream(SystemMetadata.class, sysmeta);
1491

    
1492
            Identifier rId = MNodeService.getInstance(request).update(session, pid, object, newPid, smd);
1493
            TypeMarshaller.marshalTypeToOutputStream(rId, out);
1494
        } else {
1495
            throw new InvalidRequest("1000", "Operation must be create or update.");
1496
        }
1497
   
1498
    }
1499

    
1500
    /**
1501
     * Handle delete 
1502
     * @param pid ID of data object to be deleted
1503
     * @throws IOException
1504
     * @throws InvalidRequest 
1505
     * @throws NotImplemented 
1506
     * @throws NotFound 
1507
     * @throws NotAuthorized 
1508
     * @throws ServiceFailure 
1509
     * @throws InvalidToken 
1510
     * @throws JiBXException 
1511
     */
1512
    private void deleteObject(String pid) throws IOException, InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented, InvalidRequest, JiBXException 
1513
    {
1514

    
1515
        OutputStream out = response.getOutputStream();
1516
        response.setStatus(200);
1517
        response.setContentType("text/xml");
1518

    
1519
        Identifier id = new Identifier();
1520
        id.setValue(pid);
1521

    
1522
        logMetacat.debug("Calling delete");
1523
        MNodeService.getInstance(request).delete(session, id);
1524
        TypeMarshaller.marshalTypeToOutputStream(id, out);
1525
        
1526
    }
1527
    
1528
    /**
1529
     * Archives the given pid
1530
     * @param pid
1531
     * @throws InvalidToken
1532
     * @throws ServiceFailure
1533
     * @throws NotAuthorized
1534
     * @throws NotFound
1535
     * @throws NotImplemented
1536
     * @throws IOException
1537
     * @throws JiBXException
1538
     */
1539
    private void archive(String pid) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented, IOException, JiBXException {
1540

    
1541
        OutputStream out = response.getOutputStream();
1542
        response.setStatus(200);
1543
        response.setContentType("text/xml");
1544

    
1545
        Identifier id = new Identifier();
1546
        id.setValue(pid);
1547

    
1548
        logMetacat.debug("Calling archive");
1549
        MNodeService.getInstance(request).archive(session, id);
1550
        
1551
        TypeMarshaller.marshalTypeToOutputStream(id, out);
1552
        
1553
    }
1554

    
1555
	protected SynchronizationFailed collectSynchronizationFailed() throws IOException, ServiceFailure, InvalidRequest, JiBXException, InstantiationException, IllegalAccessException, ParserConfigurationException, SAXException  {
1556
		
1557
		// Read the incoming data from its Mime Multipart encoding
1558
		logMetacat.debug("Disassembling MIME multipart form");
1559
		InputStream sf = null;
1560
	
1561
		// handle MMP inputs
1562
		File tmpDir = getTempDirectory();
1563
		logMetacat.debug("temp dir: " + tmpDir.getAbsolutePath());
1564
		MultipartRequestResolver mrr = 
1565
			new MultipartRequestResolver(tmpDir.getAbsolutePath(), MAX_UPLOAD_SIZE, 0);
1566
		MultipartRequest mr = null;
1567
		try {
1568
			mr = mrr.resolveMultipart(request);
1569
		} catch (Exception e) {
1570
			throw new ServiceFailure("2161", 
1571
					"Could not resolve multipart: " + e.getMessage());
1572
		}
1573
		logMetacat.debug("resolved multipart request");
1574
		Map<String, File> files = mr.getMultipartFiles();
1575
		if (files == null || files.keySet() == null) {
1576
			throw new InvalidRequest("2163",
1577
					"must have multipart file with name 'message'");
1578
		}
1579
		logMetacat.debug("got multipart files");
1580
	
1581
		multipartparams = mr.getMultipartParameters();
1582
	
1583
		File sfFile = files.get("message");
1584
		if (sfFile == null) {
1585
			throw new InvalidRequest("2163",
1586
					"Missing the required file-part 'message' from the multipart request.");
1587
		}
1588
		logMetacat.debug("sfFile: " + sfFile.getAbsolutePath());
1589
		sf = new FileInputStream(sfFile);
1590
	
1591
		SynchronizationFailed syncFailed = (SynchronizationFailed) ExceptionHandler.deserializeXml(sf, "Error deserializing exception");
1592
		return syncFailed;
1593
	}
1594
	
1595
	/**
1596
	 * Update the system metadata for a specified identifier
1597
	 * @throws ServiceFailure
1598
	 * @throws InvalidRequest
1599
	 * @throws InstantiationException
1600
	 * @throws IllegalAccessException
1601
	 * @throws IOException
1602
	 * @throws JiBXException
1603
	 * @throws NotImplemented
1604
	 * @throws NotAuthorized
1605
	 * @throws InvalidSystemMetadata
1606
	 * @throws InvalidToken
1607
	 */
1608
	protected void updateSystemMetadata() throws ServiceFailure, InvalidRequest, 
1609
	                        InstantiationException, IllegalAccessException, IOException, JiBXException, NotImplemented, 
1610
	                        NotAuthorized, InvalidSystemMetadata, InvalidToken {
1611
	    // Read the incoming data from its Mime Multipart encoding
1612
        Map<String, File> files = collectMultipartFiles();
1613
        
1614
        // get the encoded pid string from the body and make the object
1615
        String pidString = multipartparams.get("pid").get(0);
1616
        Identifier pid = new Identifier();
1617
        pid.setValue(pidString);
1618
        
1619
        logMetacat.debug("updateSystemMetadata: " + pid);
1620

    
1621
        // get the system metadata from the request
1622
        File smFile = files.get("sysmeta");
1623
        FileInputStream sysmeta = new FileInputStream(smFile);
1624
        SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromStream(SystemMetadata.class, sysmeta);
1625

    
1626
        logMetacat.debug("updating system metadata with pid " + pid.getValue());
1627
        
1628
        MNodeService.getInstance(request).updateSystemMetadata(session, pid, systemMetadata);
1629
	}
1630

    
1631
}
(3-3/4)