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: 2014-07-23 16:19:48 -0700 (Wed, 23 Jul 2014) $'
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.IOException;
28
import java.io.InputStream;
29
import java.io.OutputStream;
30
import java.util.Date;
31
import java.util.Enumeration;
32
import java.util.Map;
33

    
34
import javax.servlet.ServletContext;
35
import javax.servlet.http.HttpServletRequest;
36
import javax.servlet.http.HttpServletResponse;
37
import javax.xml.parsers.ParserConfigurationException;
38

    
39
import org.apache.commons.fileupload.FileUploadException;
40
import org.apache.commons.io.IOUtils;
41
import org.apache.log4j.Logger;
42
import org.dataone.client.v2.formats.ObjectFormatInfo;
43
import org.dataone.service.exceptions.BaseException;
44
import org.dataone.service.exceptions.IdentifierNotUnique;
45
import org.dataone.service.exceptions.InsufficientResources;
46
import org.dataone.service.exceptions.InvalidRequest;
47
import org.dataone.service.exceptions.InvalidSystemMetadata;
48
import org.dataone.service.exceptions.InvalidToken;
49
import org.dataone.service.exceptions.NotAuthorized;
50
import org.dataone.service.exceptions.NotFound;
51
import org.dataone.service.exceptions.NotImplemented;
52
import org.dataone.service.exceptions.ServiceFailure;
53
import org.dataone.service.exceptions.UnsupportedType;
54
import org.dataone.service.exceptions.VersionMismatch;
55
import org.dataone.service.types.v1.AccessPolicy;
56
import org.dataone.service.types.v1.Checksum;
57
import org.dataone.service.types.v1.ChecksumAlgorithmList;
58
import org.dataone.service.types.v1.DescribeResponse;
59
import org.dataone.service.types.v1.Event;
60
import org.dataone.service.types.v1.Identifier;
61
import org.dataone.service.types.v2.Log;
62
import org.dataone.service.types.v1.NodeReference;
63
import org.dataone.service.types.v2.ObjectFormat;
64
import org.dataone.service.types.v1.ObjectFormatIdentifier;
65
import org.dataone.service.types.v2.ObjectFormatList;
66
import org.dataone.service.types.v1.ObjectList;
67
import org.dataone.service.types.v1.ObjectLocationList;
68
import org.dataone.service.types.v1.Permission;
69
import org.dataone.service.types.v1.Replica;
70
import org.dataone.service.types.v1.ReplicationPolicy;
71
import org.dataone.service.types.v1.ReplicationStatus;
72
import org.dataone.service.types.v1.Subject;
73
import org.dataone.service.types.v2.SystemMetadata;
74
import org.dataone.service.util.Constants;
75
import org.dataone.service.util.DateTimeMarshaller;
76
import org.dataone.service.util.EncodingUtilities;
77
import org.dataone.service.util.ExceptionHandler;
78
import org.dataone.service.util.TypeMarshaller;
79
import org.jibx.runtime.JiBXException;
80
import org.xml.sax.SAXException;
81

    
82
import edu.ucsb.nceas.metacat.dataone.CNodeService;
83
import edu.ucsb.nceas.metacat.properties.PropertyService;
84
import edu.ucsb.nceas.metacat.restservice.D1ResourceHandler;
85
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
86

    
87
/**
88
 * CN REST service implementation handler
89
 * 
90
 * ****************** CNCore -- DONE create() - POST /d1/cn/object/PID
91
 * listFormats() - GET /d1/cn/formats getFormat() - GET /d1/cn/formats/FMTID
92
 * getLogRecords - GET /d1/cn/log reserveIdentifier() - POST /d1/cn/reserve
93
 * listNodes() - Not implemented registerSystemMetadata() - POST /d1/meta/PID
94
 * 
95
 * CNRead -- DONE get() - GET /d1/cn/object/PID getSystemMetadata() - GET
96
 * /d1/cn/meta/PID resolve() - GET /d1/cn/resolve/PID assertRelation() - GET
97
 * /d1/cn/assertRelation/PID getChecksum() - GET /d1/cn/checksum search() - Not
98
 * implemented in Metacat
99
 * 
100
 * CNAuthorization setOwner() - PUT /d1/cn/owner/PID isAuthorized() - GET
101
 * /d1/cn/isAuthorized/PID setAccessPolicy() - POST /d1/cn/accessRules
102
 * 
103
 * CNIdentity - not implemented at all on Metacat
104
 * 
105
 * CNReplication setReplicationStatus() - PUT /replicaNotifications/PID
106
 * updateReplicationMetadata() - PUT /replicaMetadata/PID setReplicationPolicy()
107
 * - PUT /replicaPolicies/PID isNodeAuthorized() - GET
108
 * /replicaAuthorizations/PID
109
 * 
110
 * CNRegister -- not implemented at all in Metacat ******************
111
 * 
112
 * @author leinfelder
113
 * 
114
 */
115
public class CNResourceHandler extends D1ResourceHandler {
116

    
117
    /** CN-specific operations **/
118
    protected static final String RESOURCE_RESERVE = "reserve";
119
    protected static final String RESOURCE_FORMATS = "formats";
120
    protected static final String RESOURCE_RESOLVE = "resolve";
121
    protected static final String RESOURCE_OWNER = "owner";
122
    protected static final String RESOURCE_REPLICATION_POLICY = "replicaPolicies";
123
    protected static final String RESOURCE_REPLICATION_META = "replicaMetadata";
124
    protected static final String RESOURCE_REPLICATION_AUTHORIZED = "replicaAuthorizations";
125
    protected static final String RESOURCE_REPLICATION_NOTIFY = "replicaNotifications";
126

    
127
    public CNResourceHandler(ServletContext servletContext,
128
            HttpServletRequest request, HttpServletResponse response) {
129
        super(servletContext, request, response);
130
        logMetacat = Logger.getLogger(CNResourceHandler.class);
131
    }
132

    
133
    /**
134
     * This function is called from REST API servlet and handles each request to
135
     * the servlet
136
     * 
137
     * @param httpVerb
138
     *            (GET, POST, PUT or DELETE)
139
     */
140
    @Override
141
    public void handle(byte httpVerb) {
142
        // prepare the handler
143
        super.handle(httpVerb);
144

    
145
        try {
146

    
147
        	// only service requests if we have D1 configured
148
        	if (!isD1Enabled()) {
149
        		ServiceFailure se = new ServiceFailure("0000", "DataONE services are not enabled on this node");
150
                serializeException(se, response.getOutputStream());
151
                return;
152
        	}
153
        	
154
            // get the resource
155
            String resource = request.getPathInfo();
156
            resource = resource.substring(resource.indexOf("/") + 1);
157

    
158
            // for the rest of the resouce
159
            String extra = null;
160

    
161
            logMetacat.debug("handling verb " + httpVerb
162
                    + " request with resource '" + resource + "'");
163
            boolean status = false;
164

    
165
            if (resource != null) {
166

    
167
                if (resource.startsWith(RESOURCE_ACCESS_RULES)
168
                        && httpVerb == PUT) {
169
                    logMetacat.debug("Setting access policy");
170
                    // after the command
171
                    extra = parseTrailing(resource, RESOURCE_ACCESS_RULES);
172
                    setAccess(extra);
173
                    status = true;
174
                    logMetacat.debug("done setting access");
175

    
176
                } else if (resource.startsWith(RESOURCE_META)) {
177
                    logMetacat.debug("Using resource: " + RESOURCE_META);
178

    
179
                    // after the command
180
                    extra = parseTrailing(resource, RESOURCE_META);
181

    
182
                    // get
183
                    if (httpVerb == GET) {
184
                        getSystemMetadataObject(extra);
185
                        status = true;
186
                    }
187
                    // post to register system metadata
188
                    if (httpVerb == POST) {
189
                        registerSystemMetadata();
190
                        status = true;
191
                    }
192

    
193
                } else if (resource.startsWith(RESOURCE_RESERVE)) {
194
                    // reserve the ID (in params)
195
                    if (httpVerb == POST) {
196
                        reserve();
197
                        status = true;
198
                    }
199
                } else if (resource.startsWith(RESOURCE_RESOLVE)) {
200

    
201
                    // after the command
202
                    extra = parseTrailing(resource, RESOURCE_RESOLVE);
203

    
204
                    // resolve the object location
205
                    if (httpVerb == GET) {
206
                        resolve(extra);
207
                        status = true;
208
                    }
209
                } else if (resource.startsWith(RESOURCE_OWNER)) {
210

    
211
                    // after the command
212
                    extra = parseTrailing(resource, RESOURCE_OWNER);
213

    
214
                    // set the owner
215
                    if (httpVerb == PUT) {
216
                        owner(extra);
217
                        status = true;
218
                    }
219
                } else if (resource.startsWith(RESOURCE_IS_AUTHORIZED)) {
220

    
221
                    // after the command
222
                    extra = parseTrailing(resource, RESOURCE_IS_AUTHORIZED);
223

    
224
                    // authorized?
225
                    if (httpVerb == GET) {
226
                        isAuthorized(extra);
227
                        status = true;
228
                    }
229
                } else if (resource.startsWith(RESOURCE_OBJECTS)) {
230
                    logMetacat.debug("Using resource 'object'");
231
                    logMetacat
232
                            .debug("D1 Rest: Starting resource processing...");
233

    
234
                    // after the command
235
                    extra = parseTrailing(resource, RESOURCE_OBJECTS);
236

    
237
                    logMetacat.debug("objectId: " + extra);
238
                    logMetacat.debug("verb:" + httpVerb);
239

    
240
                    if (httpVerb == GET) {
241
                        if (extra != null) {
242
                            getObject(extra);
243
                        } else {
244
                            listObjects();
245
                        }
246
                        status = true;
247
                    } else if (httpVerb == POST) {
248
                        putObject(FUNCTION_NAME_INSERT);
249
                        status = true;
250
                    } else if (httpVerb == HEAD) {
251
                        describeObject(extra);
252
                        status = true;
253
                    } else if (httpVerb == DELETE) {
254
                        deleteObject(extra);
255
                        status = true;
256
                    } 
257

    
258
                } else if (resource.startsWith(RESOURCE_FORMATS)) {
259
                    logMetacat.debug("Using resource: " + RESOURCE_FORMATS);
260

    
261
                    // after the command
262
                    extra = parseTrailing(resource, RESOURCE_FORMATS);
263

    
264
                    // handle each verb
265
                    if (httpVerb == GET) {
266
                        if (extra == null) {
267
                            // list the formats collection
268
                            listFormats();
269
                        } else {
270
                            // get the specified format
271
                            getFormat(extra);
272
                        }
273
                        status = true;
274
                    }
275

    
276
                } else if (resource.startsWith(RESOURCE_LOG)) {
277
                    logMetacat.debug("Using resource: " + RESOURCE_LOG);
278
                    // handle log events
279
                    if (httpVerb == GET) {
280
                        getLog();
281
                        status = true;
282
                    }
283

    
284
                } else if (resource.startsWith(Constants.RESOURCE_ARCHIVE)) {
285
                    logMetacat.debug("Using resource " + Constants.RESOURCE_ARCHIVE);
286
                    // handle archive events
287
                    if (httpVerb == PUT) {
288
                        extra = parseTrailing(resource, Constants.RESOURCE_ARCHIVE);
289
                        archive(extra);
290
                        status = true;
291
                    }
292
                } else if (resource.startsWith(Constants.RESOURCE_CHECKSUM)) {
293
                    logMetacat.debug("Using resource: " + Constants.RESOURCE_CHECKSUM);
294

    
295
                    // after the command
296
                    extra = parseTrailing(resource, Constants.RESOURCE_CHECKSUM);
297

    
298
                    // handle checksum requests
299
                    if (httpVerb == GET) {
300

    
301
                    	if (extra != null && extra.length() > 0) {
302
	                        checksum(extra);
303
	                        status = true;
304
                    	} else {
305
                    		listChecksumAlgorithms();
306
                    		status = true;
307
                    	}
308

    
309
                    }
310

    
311
                } else if (resource.startsWith(RESOURCE_REPLICATION_POLICY)
312
                        && httpVerb == PUT) {
313

    
314
                    logMetacat.debug("Using resource: "
315
                            + RESOURCE_REPLICATION_POLICY);
316
                    // get the trailing pid
317
                    extra = parseTrailing(resource, RESOURCE_REPLICATION_POLICY);
318
                    setReplicationPolicy(extra);
319
                    status = true;
320

    
321
                } else if (resource.startsWith(RESOURCE_REPLICATION_META)
322
                        && httpVerb == PUT) {
323

    
324
                    logMetacat.debug("Using resource: "
325
                            + RESOURCE_REPLICATION_META);
326
                    // get the trailing pid
327
                    extra = parseTrailing(resource, RESOURCE_REPLICATION_META);
328
                    updateReplicationMetadata(extra);
329
                    status = true;
330

    
331
                } else if (resource.startsWith(RESOURCE_REPLICATION_NOTIFY)
332
                        && httpVerb == PUT) {
333

    
334
                    logMetacat.debug("Using resource: "
335
                            + RESOURCE_REPLICATION_NOTIFY);
336
                    // get the trailing pid
337
                    extra = parseTrailing(resource, RESOURCE_REPLICATION_NOTIFY);
338
                    setReplicationStatus(extra);
339
                    status = true;
340

    
341
                } else if (resource.startsWith(RESOURCE_REPLICATION_AUTHORIZED)
342
                        && httpVerb == GET) {
343

    
344
                    logMetacat.debug("Using resource: "
345
                            + RESOURCE_REPLICATION_AUTHORIZED);
346
                    // get the trailing pid
347
                    extra = parseTrailing(resource,
348
                            RESOURCE_REPLICATION_AUTHORIZED);
349
                    isNodeAuthorized(extra);
350
                    status = true;
351

    
352
                } else if (resource.startsWith(Constants.RESOURCE_MONITOR_PING)) {
353
                    if (httpVerb == GET) {
354
                    	// after the command
355
                        extra = parseTrailing(resource, Constants.RESOURCE_MONITOR_PING);
356
                        
357
                        logMetacat.debug("processing ping request");
358
                        Date result = CNodeService.getInstance(request).ping();
359
                        // TODO: send to output	
360
                        status = true;
361
                    }
362
                } else if (resource.startsWith(Constants.RESOURCE_META_OBSOLETEDBY)
363
                        && httpVerb == PUT) {
364

    
365
                    logMetacat.debug("Using resource: "
366
                            + Constants.RESOURCE_META_OBSOLETEDBY);
367
                    // get the trailing pid
368
                    extra = parseTrailing(resource, Constants.RESOURCE_META_OBSOLETEDBY);
369
                    setObsoletedBy(extra);
370
                    status = true;
371
                } else if (resource.startsWith(Constants.RESOURCE_REPLICATION_DELETE_REPLICA)
372
                        && httpVerb == PUT) {
373

    
374
                    logMetacat.debug("Using resource: "
375
                            + Constants.RESOURCE_REPLICATION_DELETE_REPLICA);
376
                    // get the trailing pid
377
                    extra = parseTrailing(resource, Constants.RESOURCE_REPLICATION_DELETE_REPLICA);
378
                    deleteReplica(extra);
379
                    status = true;
380
                }
381

    
382
                if (!status) {
383
                    throw new ServiceFailure("0000", "Unknown error, status = "
384
                            + status);
385
                }
386
            } else {
387
                throw new InvalidRequest("0000", "No resource matched for "
388
                        + resource);
389
            }
390
        } catch (BaseException be) {
391
            // report Exceptions as clearly and generically as possible
392
            OutputStream out = null;
393
            try {
394
                out = response.getOutputStream();
395
            } catch (IOException ioe) {
396
                logMetacat.error("Could not get output stream from response",
397
                        ioe);
398
            }
399
            serializeException(be, out);
400
        } catch (Exception e) {
401
            // report Exceptions as clearly and generically as possible
402
            logMetacat.error(e.getClass() + ": " + e.getMessage(), e);
403
            OutputStream out = null;
404
            try {
405
                out = response.getOutputStream();
406
            } catch (IOException ioe) {
407
                logMetacat.error("Could not get output stream from response",
408
                        ioe);
409
            }
410
            ServiceFailure se = new ServiceFailure("0000", e.getMessage());
411
            serializeException(se, out);
412
        }
413
    }
414

    
415
    /**
416
     * Get the checksum for the given guid
417
     * 
418
     * @param guid
419
     * @throws NotImplemented
420
     * @throws InvalidRequest
421
     * @throws NotFound
422
     * @throws NotAuthorized
423
     * @throws ServiceFailure
424
     * @throws InvalidToken
425
     * @throws IOException
426
     * @throws JiBXException
427
     */
428
    private void checksum(String guid) throws InvalidToken, ServiceFailure,
429
            NotAuthorized, NotFound, InvalidRequest, NotImplemented,
430
            JiBXException, IOException {
431
        Identifier guidid = new Identifier();
432
        guidid.setValue(guid);
433
        logMetacat.debug("getting checksum for object " + guid);
434
        Checksum c = CNodeService.getInstance(request).getChecksum(session,
435
                guidid);
436
        logMetacat.debug("got checksum " + c.getValue());
437
        response.setStatus(200);
438
        logMetacat.debug("serializing response");
439
        TypeMarshaller.marshalTypeToOutputStream(c, response.getOutputStream());
440
        logMetacat.debug("done serializing response.");
441

    
442
    }
443

    
444
    /**
445
     * get the logs based on passed params. Available params are token,
446
     * fromDate, toDate, event. See
447
     * http://mule1.dataone.org/ArchitectureDocs/mn_api_crud
448
     * .html#MN_crud.getLogRecords for more info
449
     * 
450
     * @throws NotImplemented
451
     * @throws InvalidRequest
452
     * @throws NotAuthorized
453
     * @throws ServiceFailure
454
     * @throws InvalidToken
455
     * @throws IOException
456
     * @throws JiBXException
457
     */
458
    private void getLog() throws InvalidToken, ServiceFailure, NotAuthorized,
459
            InvalidRequest, NotImplemented, IOException, JiBXException {
460

    
461
        Date fromDate = null;
462
        Date toDate = null;
463
        String event = null;
464
        Integer start = null;
465
        Integer count = null;
466
        String pidFilter = null;
467

    
468
        try {
469
            String fromDateS = params.get("fromDate")[0];
470
            logMetacat.debug("param fromDateS: " + fromDateS);
471
            fromDate = DateTimeMarshaller.deserializeDateToUTC(fromDateS);
472
        } catch (Exception e) {
473
            logMetacat.warn("Could not parse fromDate: " + e.getMessage());
474
        }
475
        try {
476
            String toDateS = params.get("toDate")[0];
477
            logMetacat.debug("param toDateS: " + toDateS);
478
            toDate = DateTimeMarshaller.deserializeDateToUTC(toDateS);
479
        } catch (Exception e) {
480
            logMetacat.warn("Could not parse toDate: " + e.getMessage());
481
        }
482
        try {
483
            event = params.get("event")[0];
484
        } catch (Exception e) {
485
            logMetacat.warn("Could not parse event: " + e.getMessage());
486
        }
487
        logMetacat.debug("fromDate: " + fromDate + " toDate: " + toDate);
488

    
489
        try {
490
            start = Integer.parseInt(params.get("start")[0]);
491
        } catch (Exception e) {
492
            logMetacat.warn("Could not parse start: " + e.getMessage());
493
        }
494
        try {
495
            count = Integer.parseInt(params.get("count")[0]);
496
        } catch (Exception e) {
497
            logMetacat.warn("Could not parse count: " + e.getMessage());
498
        }
499

    
500
        try {
501
            pidFilter = params.get("pidFilter")[0];
502
        } catch (Exception e) {
503
            logMetacat.warn("Could not parse pidFilter: " + e.getMessage());
504
        }
505
        
506
        logMetacat.debug("calling getLogRecords");
507
        Log log = CNodeService.getInstance(request).getLogRecords(session,
508
                fromDate, toDate, event, pidFilter, start, count);
509

    
510
        OutputStream out = response.getOutputStream();
511
        response.setStatus(200);
512
        response.setContentType("text/xml");
513

    
514
        TypeMarshaller.marshalTypeToOutputStream(log, out);
515

    
516
    }
517

    
518
    /**
519
     * Implements REST version of DataONE CRUD API --> get
520
     * 
521
     * @param guid
522
     *            ID of data object to be read
523
     * @throws NotImplemented
524
     * @throws InvalidRequest
525
     * @throws NotFound
526
     * @throws NotAuthorized
527
     * @throws ServiceFailure
528
     * @throws InvalidToken
529
     * @throws IOException
530
     */
531
    protected void getObject(String guid) throws InvalidToken, ServiceFailure,
532
            NotAuthorized, NotFound, InvalidRequest, NotImplemented,
533
            IOException {
534

    
535
        Identifier id = new Identifier();
536
        id.setValue(guid);
537

    
538
        SystemMetadata sm = CNodeService.getInstance(request)
539
                .getSystemMetadata(session, id);
540

    
541
        // set the headers for the content
542
        String mimeType = ObjectFormatInfo.instance().getMimeType(sm.getFormatId().getValue());
543
        if (mimeType == null) {
544
        	mimeType = "application/octet-stream";
545
        }
546
        String extension = ObjectFormatInfo.instance().getExtension(sm.getFormatId().getValue());
547
        String filename = id.getValue();
548
        if (extension != null) {
549
        	filename = id.getValue() + extension;
550
        }
551
        response.setContentType(mimeType);
552
        response.setHeader("Content-Disposition", "inline; filename=" + filename);
553

    
554
        InputStream data = CNodeService.getInstance(request).get(session, id);
555

    
556
        OutputStream out = response.getOutputStream();
557
        response.setStatus(200);
558
        IOUtils.copyLarge(data, out);
559

    
560
    }
561

    
562
    /**
563
     * Implements REST version of DataONE CRUD API --> getSystemMetadata
564
     * 
565
     * @param guid
566
     *            ID of data object to be read
567
     * @throws NotImplemented
568
     * @throws InvalidRequest
569
     * @throws NotFound
570
     * @throws NotAuthorized
571
     * @throws ServiceFailure
572
     * @throws InvalidToken
573
     * @throws IOException
574
     * @throws JiBXException
575
     */
576
    protected void getSystemMetadataObject(String guid) throws InvalidToken,
577
            ServiceFailure, NotAuthorized, NotFound, InvalidRequest,
578
            NotImplemented, IOException, JiBXException {
579

    
580
        Identifier id = new Identifier();
581
        id.setValue(guid);
582
        SystemMetadata sysmeta = CNodeService.getInstance(request)
583
                .getSystemMetadata(session, id);
584

    
585
        response.setContentType("text/xml");
586
        response.setStatus(200);
587
        OutputStream out = response.getOutputStream();
588

    
589
        // Serialize and write it to the output stream
590
        TypeMarshaller.marshalTypeToOutputStream(sysmeta, out);
591
    }
592

    
593
    /**
594
     * Earthgrid API > Put Service >Put Function : calls MetacatHandler >
595
     * handleInsertOrUpdateAction
596
     * 
597
     * @param guid
598
     *            - ID of data object to be inserted or updated. If action is
599
     *            update, the pid is the existing pid. If insert, the pid is the
600
     *            new one
601
     * @throws InvalidRequest
602
     * @throws ServiceFailure
603
     * @throws IdentifierNotUnique
604
     * @throws JiBXException
605
     * @throws NotImplemented
606
     * @throws InvalidSystemMetadata
607
     * @throws InsufficientResources
608
     * @throws UnsupportedType
609
     * @throws NotAuthorized
610
     * @throws InvalidToken
611
     * @throws IOException
612
     * @throws IllegalAccessException
613
     * @throws InstantiationException
614
     */
615
    protected void putObject(String action) throws ServiceFailure,
616
            InvalidRequest, IdentifierNotUnique, JiBXException, InvalidToken,
617
            NotAuthorized, UnsupportedType, InsufficientResources,
618
            InvalidSystemMetadata, NotImplemented, IOException,
619
            InstantiationException, IllegalAccessException {
620
    	
621
        // Read the incoming data from its Mime Multipart encoding
622
        Map<String, File> files = collectMultipartFiles();
623
        
624
	    // get the encoded pid string from the body and make the object
625
        String pidString = multipartparams.get("pid").get(0);
626
        Identifier pid = new Identifier();
627
        pid.setValue(pidString);
628
        
629
        logMetacat.debug("putObject: " + pid.getValue() + "/" + action);
630
        
631
        InputStream object = null;
632
        InputStream sysmeta = null;
633

    
634
        File smFile = files.get("sysmeta");
635
        sysmeta = new FileInputStream(smFile);
636
        File objFile = files.get("object");
637
        object = new FileInputStream(objFile);
638

    
639
        if (action.equals(FUNCTION_NAME_INSERT)) { // handle inserts
640

    
641
            logMetacat.debug("Commence creation...");
642
            SystemMetadata smd = TypeMarshaller.unmarshalTypeFromStream(
643
                    SystemMetadata.class, sysmeta);
644

    
645
           
646
            logMetacat.debug("creating object with pid " + pid.getValue());
647
            Identifier rId = CNodeService.getInstance(request).create(session, pid, object, smd);
648

    
649
            OutputStream out = response.getOutputStream();
650
            response.setStatus(200);
651
            response.setContentType("text/xml");
652

    
653
            TypeMarshaller.marshalTypeToOutputStream(rId, out);
654

    
655
        } else {
656
            throw new InvalidRequest("1000", "Operation must be create.");
657
        }
658
    }
659

    
660
    /**
661
     * List the object formats registered with the system
662
     * 
663
     * @throws NotImplemented
664
     * @throws InsufficientResources
665
     * @throws NotFound
666
     * @throws ServiceFailure
667
     * @throws InvalidRequest
668
     * @throws IOException
669
     * @throws JiBXException
670
     */
671
    private void listFormats() throws InvalidRequest, ServiceFailure, NotFound,
672
            InsufficientResources, NotImplemented, IOException, JiBXException {
673
        logMetacat.debug("Entering listFormats()");
674

    
675
        ObjectFormatList objectFormatList = CNodeService.getInstance(request)
676
                .listFormats();
677
        // get the response output stream
678
        OutputStream out = response.getOutputStream();
679
        response.setStatus(200);
680
        response.setContentType("text/xml");
681

    
682
        // style the object with a processing directive
683
        String stylesheet = null;
684
        try {
685
            stylesheet = PropertyService.getProperty("dataone.types.xsl");
686
        } catch (PropertyNotFoundException e) {
687
            logMetacat.warn("Could not locate DataONE types XSLT: "
688
                    + e.getMessage());
689
        }
690

    
691
        TypeMarshaller.marshalTypeToOutputStream(objectFormatList, out,
692
                stylesheet);
693

    
694
    }
695
    
696
    private void listChecksumAlgorithms() throws IOException, ServiceFailure,
697
			NotImplemented, JiBXException {
698
		logMetacat.debug("Entering listFormats()");
699

    
700
		ChecksumAlgorithmList result = CNodeService.getInstance(request).listChecksumAlgorithms();
701

    
702
		// get the response output stream
703
		OutputStream out = response.getOutputStream();
704
		response.setStatus(200);
705
		response.setContentType("text/xml");
706

    
707
		TypeMarshaller.marshalTypeToOutputStream(result, out);
708

    
709
	}
710
    
711
    /**
712
     * http://mule1.dataone.org/ArchitectureDocs-current/apis/CN_APIs.html#CNRead.describe
713
     * @param pid
714
     * @throws InvalidToken
715
     * @throws ServiceFailure
716
     * @throws NotAuthorized
717
     * @throws NotFound
718
     * @throws NotImplemented
719
     * @throws InvalidRequest
720
     */
721
    private void describeObject(String pid) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented, InvalidRequest
722
    {
723
        response.setContentType("text/xml");
724
        
725
        Identifier id = new Identifier();
726
        id.setValue(pid);
727
        
728
        DescribeResponse dr = null;
729
        try {
730
            dr = CNodeService.getInstance(request).describe(session, id);
731
        } catch (BaseException e) {
732
        	response.setStatus(e.getCode());
733
        	response.addHeader("DataONE-Exception-Name", e.getClass().getName());
734
            response.addHeader("DataONE-Exception-DetailCode", e.getDetail_code());
735
            response.addHeader("DataONE-Exception-Description", e.getDescription());
736
            response.addHeader("DataONE-Exception-PID", id.getValue());
737
            return;
738
		}
739
        
740
        response.setStatus(200);
741
        //response.addHeader("pid", pid);
742
        response.addHeader("DataONE-Checksum", dr.getDataONE_Checksum().getAlgorithm() + "," + dr.getDataONE_Checksum().getValue());
743
        response.addHeader("Content-Length", dr.getContent_Length() + "");
744
        response.addHeader("Last-Modified", DateTimeMarshaller.serializeDateToUTC(dr.getLast_Modified()));
745
        response.addHeader("DataONE-ObjectFormat", dr.getDataONE_ObjectFormatIdentifier().getValue());
746
        response.addHeader("DataONE-SerialVersion", dr.getSerialVersion().toString());
747

    
748
    }
749
    
750
    /**
751
     * Handle delete 
752
     * @param pid ID of data object to be deleted
753
     * @throws IOException
754
     * @throws InvalidRequest 
755
     * @throws NotImplemented 
756
     * @throws NotFound 
757
     * @throws NotAuthorized 
758
     * @throws ServiceFailure 
759
     * @throws InvalidToken 
760
     * @throws JiBXException 
761
     */
762
    private void deleteObject(String pid) throws IOException, InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented, InvalidRequest, JiBXException 
763
    {
764

    
765
        OutputStream out = response.getOutputStream();
766
        response.setStatus(200);
767
        response.setContentType("text/xml");
768

    
769
        Identifier id = new Identifier();
770
        id.setValue(pid);
771

    
772
        logMetacat.debug("Calling delete for identifier " + pid);
773
        CNodeService.getInstance(request).delete(session, id);
774
        TypeMarshaller.marshalTypeToOutputStream(id, out);
775
        
776
    }
777
    
778
    /**
779
     * Archives the given pid
780
     * @param pid
781
     * @throws InvalidToken
782
     * @throws ServiceFailure
783
     * @throws NotAuthorized
784
     * @throws NotFound
785
     * @throws NotImplemented
786
     * @throws IOException
787
     * @throws JiBXException
788
     */
789
    private void archive(String pid) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented, IOException, JiBXException {
790

    
791
        OutputStream out = response.getOutputStream();
792
        response.setStatus(200);
793
        response.setContentType("text/xml");
794

    
795
        Identifier id = new Identifier();
796
        id.setValue(pid);
797

    
798
        logMetacat.debug("Calling archive");
799
        CNodeService.getInstance(request).archive(session, id);
800
        
801
        TypeMarshaller.marshalTypeToOutputStream(id, out);
802
        
803
    }
804

    
805
    /**
806
     * Return the requested object format
807
     * 
808
     * @param fmtidStr
809
     *            the requested format identifier as a string
810
     * @throws NotImplemented
811
     * @throws InsufficientResources
812
     * @throws NotFound
813
     * @throws ServiceFailure
814
     * @throws InvalidRequest
815
     * @throws IOException
816
     * @throws JiBXException
817
     */
818
    private void getFormat(String fmtidStr) throws InvalidRequest,
819
            ServiceFailure, NotFound, InsufficientResources, NotImplemented,
820
            IOException, JiBXException {
821
        logMetacat.debug("Entering listFormats()");
822

    
823
        ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
824
        fmtid.setValue(fmtidStr);
825

    
826
        // get the specified object format
827
        ObjectFormat objectFormat = CNodeService.getInstance(request)
828
                .getFormat(fmtid);
829

    
830
        OutputStream out = response.getOutputStream();
831
        response.setStatus(200);
832
        response.setContentType("text/xml");
833

    
834
        TypeMarshaller.marshalTypeToOutputStream(objectFormat, out);
835

    
836
    }
837

    
838
    /**
839
     * Reserve the given Identifier
840
     * 
841
     * @throws InvalidToken
842
     * @throws ServiceFailure
843
     * @throws NotAuthorized
844
     * @throws IdentifierNotUnique
845
     * @throws NotImplemented
846
     * @throws InvalidRequest
847
     */
848
    private void reserve() 
849
        throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, 
850
        NotImplemented, InvalidRequest {
851
        Identifier pid = null;
852
        String scope = null;
853
        String format = null;
854

    
855
        // Parse the params out of the multipart form data
856
        logMetacat.debug("Parsing reserve parameters from the mime multipart entity");
857
        try {
858
            collectMultipartParams();
859
            
860
        } catch (FileUploadException e1) {
861
            String msg = "FileUploadException: Couldn't parse the mime multipart information: " +
862
            e1.getMessage();
863
            logMetacat.debug(msg);
864
            throw new ServiceFailure("4210", msg);
865

    
866
        } catch (IOException e1) {
867
            String msg = "IOException: Couldn't parse the mime multipart information: " +
868
            e1.getMessage();
869
            logMetacat.debug(msg);
870
            throw new ServiceFailure("4210", msg);
871
        
872
        } catch (Exception e1) {
873
            String msg = "Exception: Couldn't parse the mime multipart information: " +
874
            e1.getMessage();
875
            logMetacat.debug(msg);
876
            throw new ServiceFailure("4210", msg);
877

    
878
        }
879
        
880
        // gather the params
881
        try {
882
            String id = multipartparams.get("pid").get(0);
883
            pid = new Identifier();
884
            pid.setValue(id);
885
            
886
        } catch (NullPointerException e) {
887
            String msg = "The 'pid' must be provided as a parameter and was not.";
888
            logMetacat.error(msg);
889
            throw new InvalidRequest("4200", msg);
890
 
891
        }
892
        
893
        // call the implementation
894
        try {
895
            Identifier resultPid = CNodeService.getInstance(request).reserveIdentifier(session, pid);
896
            OutputStream out = response.getOutputStream();
897
            response.setStatus(200);
898
            response.setContentType("text/xml");
899
            // send back the reserved pid
900
            TypeMarshaller.marshalTypeToOutputStream(resultPid, out);
901
            
902
        } catch (IOException e) {
903
            String msg = "Couldn't write the identifier to the response output stream: " +
904
                e.getMessage();
905
            logMetacat.debug(msg);
906
            throw new ServiceFailure("4210", msg);
907
        
908
        } catch (JiBXException e) {
909
            String msg = "Couldn't marshall the identifier to the response output stream: " +
910
            e.getMessage();
911
            logMetacat.debug(msg);
912
            throw new ServiceFailure("4210", msg);
913
            
914
        }
915
    }
916

    
917
    /**
918
     * 
919
     * @param id
920
     * @throws InvalidRequest
921
     * @throws InvalidToken
922
     * @throws ServiceFailure
923
     * @throws NotAuthorized
924
     * @throws NotFound
925
     * @throws NotImplemented
926
     * @throws IOException
927
     * @throws JiBXException
928
     */
929
    private void resolve(String id) throws InvalidRequest, InvalidToken,
930
            ServiceFailure, NotAuthorized, NotFound, NotImplemented,
931
            IOException, JiBXException {
932
        Identifier pid = new Identifier();
933
        pid.setValue(id);
934
        ObjectLocationList locationList = CNodeService.getInstance(request)
935
                .resolve(session, pid);
936
        OutputStream out = response.getOutputStream();
937
        response.setStatus(200);
938
        response.setContentType("text/xml");
939
        TypeMarshaller.marshalTypeToOutputStream(locationList, out);
940

    
941
    }
942

    
943
    /**
944
     * Set the owner of a resource
945
     * 
946
     * @param id
947
     * @throws InvalidToken
948
     * @throws ServiceFailure
949
     * @throws NotFound
950
     * @throws NotAuthorized
951
     * @throws NotImplemented
952
     * @throws InvalidRequest
953
     * @throws IllegalAccessException
954
     * @throws InstantiationException
955
     * @throws VersionMismatch 
956
     */
957
    private void owner(String id) 
958
        throws InvalidToken, ServiceFailure, NotFound, NotAuthorized, 
959
        NotImplemented, InvalidRequest, InstantiationException, 
960
        IllegalAccessException, VersionMismatch {
961

    
962
        Identifier pid = new Identifier();
963
        pid.setValue(id);
964

    
965
        long serialVersion = 0L;
966
        String serialVersionStr = null;
967
        String userIdStr = null;
968
        Subject userId = null;
969
        
970
        // Parse the params out of the multipart form data
971
        // Read the incoming data from its Mime Multipart encoding
972
        logMetacat.debug("Parsing rights holder parameters from the mime multipart entity");
973
        try {
974
            collectMultipartParams();
975
            
976
        } catch (FileUploadException e1) {
977
            String msg = "FileUploadException: Couldn't parse the mime multipart information: " +
978
            e1.getMessage();
979
            logMetacat.debug(msg);
980
            throw new ServiceFailure("4490", msg);
981

    
982
        } catch (IOException e1) {
983
            String msg = "IOException: Couldn't parse the mime multipart information: " +
984
            e1.getMessage();
985
            logMetacat.debug(msg);
986
            throw new ServiceFailure("4490", msg);
987
        
988
        } catch (Exception e1) {
989
            String msg = "Exception: Couldn't parse the mime multipart information: " +
990
            e1.getMessage();
991
            logMetacat.debug(msg);
992
            throw new ServiceFailure("4490", msg);
993

    
994
        }
995
        
996
        // get the serialVersion
997
        try {
998
            serialVersionStr = multipartparams.get("serialVersion").get(0);
999
            serialVersion = new Long(serialVersionStr).longValue();
1000
            
1001
        } catch (NumberFormatException nfe) {
1002
            String msg = "The 'serialVersion' must be provided as a positive integer and was not.";
1003
            logMetacat.error(msg);
1004
            throw new InvalidRequest("4442", msg);
1005
                        
1006
        } catch (NullPointerException e) {
1007
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
1008
            logMetacat.error(msg);
1009
            throw new InvalidRequest("4442", msg);
1010
            
1011
        }
1012

    
1013
        // get the subject userId that will become the rights holder
1014
        try {
1015
            userIdStr = multipartparams.get("userId").get(0);
1016
            userId = new Subject();
1017
            userId.setValue(userIdStr);
1018
                                    
1019
        } catch (NullPointerException e) {
1020
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
1021
            logMetacat.error(msg);
1022
            throw new InvalidRequest("4442", msg);
1023
            
1024
        }
1025

    
1026
        // set the rights holder
1027
        Identifier retPid = CNodeService.getInstance(request).setRightsHolder(session, pid, userId, serialVersion);
1028
        
1029
        try {
1030
            OutputStream out = response.getOutputStream();
1031
            response.setStatus(200);
1032
            response.setContentType("text/xml");
1033
            TypeMarshaller.marshalTypeToOutputStream(retPid, out);
1034
            
1035
        } catch (IOException e) {
1036
            String msg = "Couldn't write the identifier to the response output stream: " +
1037
                e.getMessage();
1038
            logMetacat.debug(msg);
1039
            throw new ServiceFailure("4490", msg);
1040
        
1041
        } catch (JiBXException e) {
1042
            String msg = "Couldn't marshall the identifier to the response output stream: " +
1043
            e.getMessage();
1044
            logMetacat.debug(msg);
1045
            throw new ServiceFailure("4490", msg);
1046
            
1047
        }
1048
    }
1049

    
1050
    /**
1051
     * Processes the authorization check for given id
1052
     * 
1053
     * @param id
1054
     * @return
1055
     * @throws ServiceFailure
1056
     * @throws InvalidToken
1057
     * @throws NotFound
1058
     * @throws NotAuthorized
1059
     * @throws NotImplemented
1060
     * @throws InvalidRequest
1061
     */
1062
    private boolean isAuthorized(String id) throws ServiceFailure,
1063
            InvalidToken, NotFound, NotAuthorized, NotImplemented,
1064
            InvalidRequest {
1065
        Identifier pid = new Identifier();
1066
        pid.setValue(id);
1067
        String permission = params.get("action")[0];
1068
        boolean result = CNodeService.getInstance(request).isAuthorized(
1069
                session, pid, Permission.convert(permission));
1070
        response.setStatus(200);
1071
        response.setContentType("text/xml");
1072
        return result;
1073
    }
1074

    
1075
    /**
1076
     * Register System Metadata without data or metadata object
1077
     * 
1078
     * @param pid
1079
     *            identifier for System Metadata entry
1080
     * @throws JiBXException
1081
     * @throws FileUploadException
1082
     * @throws IOException
1083
     * @throws InvalidRequest
1084
     * @throws ServiceFailure
1085
     * @throws InvalidSystemMetadata
1086
     * @throws NotAuthorized
1087
     * @throws NotImplemented
1088
     * @throws IllegalAccessException
1089
     * @throws InstantiationException
1090
     */
1091
    protected void registerSystemMetadata()
1092
            throws ServiceFailure, InvalidRequest, IOException,
1093
            FileUploadException, JiBXException, NotImplemented, NotAuthorized,
1094
            InvalidSystemMetadata, InstantiationException,
1095
            IllegalAccessException {
1096
    	
1097
    	// Read the incoming data from its Mime Multipart encoding
1098
        Map<String, File> files = collectMultipartFiles();
1099
        
1100
    	// get the encoded pid string from the body and make the object
1101
        String pidString = multipartparams.get("pid").get(0);
1102
        Identifier pid = new Identifier();
1103
        pid.setValue(pidString);
1104
        
1105
        logMetacat.debug("registerSystemMetadata: " + pid);
1106

    
1107
        // get the system metadata from the request
1108
        File smFile = files.get("sysmeta");
1109
        FileInputStream sysmeta = new FileInputStream(smFile);
1110
        SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromStream(SystemMetadata.class, sysmeta);
1111

    
1112
        logMetacat.debug("registering system metadata with pid " + pid.getValue());
1113
        Identifier retGuid = CNodeService.getInstance(request).registerSystemMetadata(session, pid, systemMetadata);
1114

    
1115
        OutputStream out = response.getOutputStream();
1116
        response.setStatus(200);
1117
        response.setContentType("text/xml");
1118
        
1119
        TypeMarshaller.marshalTypeToOutputStream(retGuid, out);
1120

    
1121
    }
1122

    
1123
    /**
1124
     * set the access perms on a document
1125
     * 
1126
     * @throws JiBXException
1127
     * @throws InvalidRequest
1128
     * @throws NotImplemented
1129
     * @throws NotAuthorized
1130
     * @throws NotFound
1131
     * @throws ServiceFailure
1132
     * @throws InvalidToken
1133
     * @throws IllegalAccessException
1134
     * @throws InstantiationException
1135
     * @throws IOException
1136
     * @throws SAXException
1137
     * @throws ParserConfigurationException
1138
     * @throws VersionMismatch 
1139
     */
1140
    protected void setAccess(String pid) throws JiBXException, InvalidToken,
1141
            ServiceFailure, NotFound, NotAuthorized, NotImplemented,
1142
            InvalidRequest, IOException, InstantiationException,
1143
            IllegalAccessException, ParserConfigurationException, SAXException, VersionMismatch {
1144

    
1145
        long serialVersion = 0L;
1146
        String serialVersionStr = null;
1147
        
1148
        // parse the accessPolicy
1149
        Map<String, File> files = collectMultipartFiles();        
1150
        AccessPolicy accessPolicy = TypeMarshaller.unmarshalTypeFromFile(AccessPolicy.class, files.get("accessPolicy"));;
1151

    
1152
        // get the serialVersion
1153
        try {
1154
            serialVersionStr = multipartparams.get("serialVersion").get(0);
1155
            serialVersion = new Long(serialVersionStr).longValue();
1156

    
1157
        } catch (NumberFormatException nfe) {
1158
            String msg = "The 'serialVersion' must be provided as a positive integer and was not.";
1159
            logMetacat.error(msg);
1160
            throw new InvalidRequest("4402", msg);
1161
            
1162
        } catch (NullPointerException e) {
1163
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
1164
            logMetacat.error(msg);
1165
            throw new InvalidRequest("4402", msg);
1166

    
1167
        }
1168

    
1169
        Identifier id = new Identifier();
1170
        id.setValue(pid);
1171

    
1172
        CNodeService.getInstance(request).setAccessPolicy(session, id,
1173
                accessPolicy, serialVersion);
1174

    
1175
    }
1176

    
1177
    /**
1178
     * List the objects
1179
     * 
1180
     * @throws NotImplemented
1181
     * @throws InvalidRequest
1182
     * @throws NotAuthorized
1183
     * @throws ServiceFailure
1184
     * @throws InvalidToken
1185
     * @throws NotFound
1186
     * @throws IOException
1187
     * @throws JiBXException
1188
     * @throws Exception
1189
     */
1190
    private void listObjects() throws InvalidToken, ServiceFailure,
1191
            NotAuthorized, InvalidRequest, NotImplemented, NotFound,
1192
            IOException, JiBXException {
1193

    
1194
        Date startTime = null;
1195
        Date endTime = null;
1196
        ObjectFormatIdentifier formatId = null;
1197
        Identifier identifier = null;
1198
        boolean replicaStatus = true;
1199
        int start = 0;
1200
        int count = 1000;
1201
        Enumeration<String> paramlist = request.getParameterNames();
1202
        while (paramlist.hasMoreElements()) {
1203
            // parse the params and make the call
1204
            String name = paramlist.nextElement();
1205
            String[] values = request.getParameterValues(name);
1206
            String value = null;
1207
            if (values != null && values.length > 0) {
1208
            	value = values[0];
1209
            	value = EncodingUtilities.decodeString(value);
1210
            }
1211

    
1212
            if (name.equals("fromDate") && value != null) {
1213
                try {
1214
                    startTime = DateTimeMarshaller.deserializeDateToUTC(value);
1215
                } catch (Exception e) {
1216
                    // if we can't parse it, just don't use the startTime param
1217
                    logMetacat.warn("Could not parse fromDate: " + value);
1218
                    startTime = null;
1219
                }
1220
            } else if (name.equals("toDate") && value != null) {
1221
                try {
1222
                    endTime = DateTimeMarshaller.deserializeDateToUTC(value);
1223
                } catch (Exception e) {
1224
                    // if we can't parse it, just don't use the endTime param
1225
                    logMetacat.warn("Could not parse toDate: " + value);
1226
                    endTime = null;
1227
                }
1228
            } else if (name.equals("formatId") && value != null) {
1229
            	formatId = new ObjectFormatIdentifier();
1230
            	formatId.setValue(value);
1231
            } else if (name.equals("identifier") && value != null) {
1232
            	identifier = new Identifier();
1233
            	identifier.setValue(value);
1234
            } else if (name.equals("replicaStatus") && value != null) {
1235
                replicaStatus = Boolean.parseBoolean(value);
1236
            } else if (name.equals("start") && value != null) {
1237
                start = Integer.valueOf(value);
1238
            } else if (name.equals("count") && value != null) {
1239
                count = Integer.valueOf(value);
1240
            }
1241
        }
1242
        // make the call
1243
        logMetacat.debug("session: " + session + " fromDate: " + startTime
1244
                + " toDate: " + endTime + " formatId: " + formatId
1245
                + " replicaStatus: " + replicaStatus + " start: " + start
1246
                + " count: " + count);        
1247

    
1248
        // get the list
1249
        ObjectList ol = CNodeService.getInstance(request).listObjects(session,
1250
                startTime, endTime, formatId, identifier, replicaStatus, start, count);
1251

    
1252
        // send it
1253
        OutputStream out = response.getOutputStream();
1254
        response.setStatus(200);
1255
        response.setContentType("text/xml");
1256

    
1257
        // style the object with a processing directive
1258
        String stylesheet = null;
1259
        try {
1260
            stylesheet = PropertyService.getProperty("dataone.types.xsl");
1261
        } catch (PropertyNotFoundException e) {
1262
            logMetacat.warn("Could not locate DataONE types XSLT: "
1263
                    + e.getMessage());
1264
        }
1265

    
1266
        // Serialize and write it to the output stream
1267
        TypeMarshaller.marshalTypeToOutputStream(ol, out, stylesheet);
1268
    }
1269

    
1270
    /**
1271
     * Pass the request to get node replication authorization to CNodeService
1272
     * 
1273
     * @param pid
1274
     *            the identifier of the object to get authorization to replicate
1275
     * 
1276
     * @throws NotImplemented
1277
     * @throws NotAuthorized
1278
     * @throws InvalidToken
1279
     * @throws ServiceFailure
1280
     * @throws NotFound
1281
     * @throws InvalidRequest
1282
     */
1283
    public boolean isNodeAuthorized(String pid) throws NotImplemented,
1284
            NotAuthorized, InvalidToken, ServiceFailure, NotFound,
1285
            InvalidRequest {
1286

    
1287
        boolean result = false;
1288
        Subject targetNodeSubject = new Subject();
1289
        String nodeSubject = null;
1290
        String replPermission = null;
1291

    
1292
        // get the pid
1293
        Identifier identifier = new Identifier();
1294
        identifier.setValue(pid);
1295

    
1296
        // get the target node subject
1297
        try {
1298
            nodeSubject = params.get("targetNodeSubject")[0];
1299
            targetNodeSubject.setValue(nodeSubject);
1300

    
1301
        } catch (NullPointerException e) {
1302
            String msg = "The 'targetNodeSubject' must be provided as a parameter and was not.";
1303
            logMetacat.error(msg);
1304
            throw new InvalidRequest("4873", msg);
1305

    
1306
        }
1307

    
1308
        result = CNodeService.getInstance(request).isNodeAuthorized(session, targetNodeSubject, identifier);
1309

    
1310
        response.setStatus(200);
1311
        response.setContentType("text/xml");
1312
        return result;
1313

    
1314
    }
1315

    
1316
    /**
1317
     * Pass the request to set the replication policy to CNodeService
1318
     * 
1319
     * @param pid
1320
     *            the identifier of the object to set the replication policy on
1321
     * 
1322
     * @throws NotImplemented
1323
     * @throws NotFound
1324
     * @throws NotAuthorized
1325
     * @throws ServiceFailure
1326
     * @throws InvalidRequest
1327
     * @throws InvalidToken
1328
     * @throws IOException
1329
     * @throws InstantiationException
1330
     * @throws IllegalAccessException
1331
     * @throws JiBXException
1332
     * @throws VersionMismatch 
1333
     */
1334
    public boolean setReplicationPolicy(String pid) throws NotImplemented,
1335
            NotFound, NotAuthorized, ServiceFailure, InvalidRequest,
1336
            InvalidToken, IOException, InstantiationException,
1337
            IllegalAccessException, JiBXException, VersionMismatch {
1338

    
1339
        boolean result = false;
1340
        long serialVersion = 0L;
1341
        String serialVersionStr = null;
1342

    
1343
        Identifier identifier = new Identifier();
1344
        identifier.setValue(pid);
1345

    
1346
        // parse the policy
1347
        Map<String, File> files = collectMultipartFiles();        
1348
        ReplicationPolicy policy = TypeMarshaller.unmarshalTypeFromFile(ReplicationPolicy.class, files.get("policy"));
1349

    
1350
        // get the serialVersion
1351
        try {
1352
            serialVersionStr = multipartparams.get("serialVersion").get(0);
1353
            serialVersion = new Long(serialVersionStr).longValue();
1354

    
1355
        } catch (NullPointerException e) {
1356
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
1357
            logMetacat.error(msg);
1358
            throw new InvalidRequest("4883", msg);
1359

    
1360
        }
1361
        result = CNodeService.getInstance(request).setReplicationPolicy(
1362
                session, identifier, policy, serialVersion);
1363
        response.setStatus(200);
1364
        response.setContentType("text/xml");
1365
        return result;
1366

    
1367
    }
1368
    
1369
    /**
1370
     * Update the system metadata for a given pid, setting it to be obsoleted
1371
     * by the obsoletedByPid
1372
     *  
1373
     * @param pid
1374
     * @return
1375
     * @throws NotImplemented
1376
     * @throws NotFound
1377
     * @throws NotAuthorized
1378
     * @throws ServiceFailure
1379
     * @throws InvalidRequest
1380
     * @throws InvalidToken
1381
     * @throws InstantiationException
1382
     * @throws IllegalAccessException
1383
     * @throws VersionMismatch
1384
     */
1385
    public boolean setObsoletedBy(String pid) 
1386
        throws NotImplemented, NotFound, NotAuthorized, ServiceFailure, 
1387
        InvalidRequest, InvalidToken, InstantiationException, 
1388
        IllegalAccessException, VersionMismatch {
1389

    
1390
        boolean result = false;
1391
        long serialVersion = 0L;
1392
        String serialVersionStr = null;
1393

    
1394
        Identifier identifier = new Identifier();
1395
        identifier.setValue(pid);
1396
        String obsoletedByPidString = null;
1397
        Identifier obsoletedByPid = null;
1398

    
1399
        // Parse the params out of the multipart form data
1400
        // Read the incoming data from its Mime Multipart encoding
1401
        logMetacat.debug("Parsing rights holder parameters from the mime multipart entity");
1402
        try {
1403
            collectMultipartParams();
1404
            
1405
        } catch (FileUploadException e1) {
1406
            String msg = "FileUploadException: Couldn't parse the mime multipart information: " +
1407
            e1.getMessage();
1408
            logMetacat.debug(msg);
1409
            throw new ServiceFailure("4941", msg);
1410

    
1411
        } catch (IOException e1) {
1412
            String msg = "IOException: Couldn't parse the mime multipart information: " +
1413
            e1.getMessage();
1414
            logMetacat.debug(msg);
1415
            throw new ServiceFailure("4941", msg);
1416
        
1417
        } catch (Exception e1) {
1418
            String msg = "Exception: Couldn't parse the mime multipart information: " +
1419
            e1.getMessage();
1420
            logMetacat.debug(msg);
1421
            throw new ServiceFailure("4941", msg);
1422

    
1423
        }
1424

    
1425
        // get the obsoletedByPid
1426
        try {
1427
            obsoletedByPidString = multipartparams.get("obsoletedByPid").get(0);
1428
            obsoletedByPid = new Identifier();
1429
            obsoletedByPid.setValue(obsoletedByPidString);
1430
        } catch (NullPointerException e) {
1431
            String msg = "The 'obsoletedByPid' must be provided as a parameter and was not.";
1432
            logMetacat.error(msg);
1433
            throw new InvalidRequest("4883", msg);
1434
        }
1435

    
1436
        // get the serialVersion
1437
        try {
1438
            serialVersionStr = multipartparams.get("serialVersion").get(0);
1439
            serialVersion = new Long(serialVersionStr).longValue();
1440
            
1441
        } catch (NumberFormatException nfe) {
1442
            String msg = "The 'serialVersion' must be provided as a positive integer and was not.";
1443
            logMetacat.error(msg);
1444
            throw new InvalidRequest("4942", msg);
1445
                        
1446
        } catch (NullPointerException e) {
1447
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
1448
            logMetacat.error(msg);
1449
            throw new InvalidRequest("4942", msg);
1450
            
1451
        }
1452
        result = CNodeService.getInstance(request).setObsoletedBy(session,
1453
            identifier, obsoletedByPid, serialVersion);
1454
        response.setStatus(200);
1455
        response.setContentType("text/xml");
1456
        return result;
1457

    
1458
    }
1459
    
1460
    /**
1461
     * Delete the replica entry with the given nodeId for the given pid
1462
     * 
1463
     * @param pid
1464
     * @return
1465
     * @throws NotImplemented
1466
     * @throws NotFound
1467
     * @throws NotAuthorized
1468
     * @throws ServiceFailure
1469
     * @throws InvalidRequest
1470
     * @throws InvalidToken
1471
     * @throws InstantiationException
1472
     * @throws IllegalAccessException
1473
     * @throws VersionMismatch
1474
     */
1475
    public boolean deleteReplica(String pid) 
1476
        throws NotImplemented, NotFound, NotAuthorized, ServiceFailure, 
1477
        InvalidRequest, InvalidToken, InstantiationException, 
1478
        IllegalAccessException, VersionMismatch {
1479

    
1480
        boolean result = false;
1481
        long serialVersion = 0L;
1482
        String serialVersionStr = null;
1483

    
1484
        Identifier identifier = new Identifier();
1485
        identifier.setValue(pid);
1486

    
1487
        NodeReference nodeId = null;
1488
        
1489
        // Parse the params out of the multipart form data
1490
        // Read the incoming data from its Mime Multipart encoding
1491
        logMetacat.debug("Parsing delete replica parameters from the mime multipart entity");
1492
        try {
1493
            collectMultipartParams();
1494
            
1495
        } catch (FileUploadException e1) {
1496
            String msg = "FileUploadException: Couldn't parse the mime multipart information: " +
1497
            e1.getMessage();
1498
            logMetacat.debug(msg);
1499
            throw new ServiceFailure("4951", msg);
1500

    
1501
        } catch (IOException e1) {
1502
            String msg = "IOException: Couldn't parse the mime multipart information: " +
1503
            e1.getMessage();
1504
            logMetacat.debug(msg);
1505
            throw new ServiceFailure("4951", msg);
1506
        
1507
        } catch (Exception e1) {
1508
            String msg = "Exception: Couldn't parse the mime multipart information: " +
1509
            e1.getMessage();
1510
            logMetacat.debug(msg);
1511
            throw new ServiceFailure("4951", msg);
1512

    
1513
        }
1514
        
1515
        // get the nodeId param
1516
        try {
1517
            String nodeIdString = multipartparams.get("nodeId").get(0);
1518
            nodeId = new NodeReference();
1519
            nodeId.setValue(nodeIdString);
1520
            
1521
        } catch (NullPointerException e) {
1522
            String msg = "The 'nodeId' must be provided as a parameter and was not.";
1523
            logMetacat.error(msg);
1524
            throw new InvalidRequest("4952", msg);
1525
        }
1526

    
1527
        // get the serialVersion
1528
        try {
1529
            serialVersionStr = multipartparams.get("serialVersion").get(0);
1530
            serialVersion = new Long(serialVersionStr).longValue();
1531
            
1532
        } catch (NumberFormatException nfe) {
1533
            String msg = "The 'serialVersion' must be provided as a positive integer and was not.";
1534
            logMetacat.error(msg);
1535
            throw new InvalidRequest("4952", msg);
1536
                        
1537
        } catch (NullPointerException e) {
1538
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
1539
            logMetacat.error(msg);
1540
            throw new InvalidRequest("4952", msg);
1541
            
1542
        }
1543
        result = CNodeService.getInstance(request).deleteReplicationMetadata(
1544
                session, identifier, nodeId, serialVersion);
1545
        response.setStatus(200);
1546
        response.setContentType("text/xml");
1547
        return result;
1548

    
1549
    }
1550

    
1551
    /**
1552
     * Pass the request to set the replication status to CNodeService
1553
     * 
1554
     * @param pid
1555
     *            the identifier of the object to set the replication status on
1556
     * 
1557
     * @throws ServiceFailure
1558
     * @throws NotImplemented
1559
     * @throws InvalidToken
1560
     * @throws NotAuthorized
1561
     * @throws InvalidRequest
1562
     * @throws NotFound
1563
     * @throws JiBXException 
1564
     * @throws IllegalAccessException 
1565
     * @throws InstantiationException 
1566
     * @throws IOException 
1567
     */
1568
    public boolean setReplicationStatus(String pid) throws ServiceFailure,
1569
            NotImplemented, InvalidToken, NotAuthorized, InvalidRequest,
1570
            NotFound {
1571
        
1572
        boolean result = false;
1573
        Identifier identifier = new Identifier();
1574
        identifier.setValue(pid);
1575
        BaseException failure = null;
1576
        ReplicationStatus status = null;
1577
        String replicationStatus = null;
1578
        NodeReference targetNodeRef = null;
1579
        String targetNode = null;
1580

    
1581
        // Parse the params out of the multipart form data
1582
        // Read the incoming data from its Mime Multipart encoding
1583
        logMetacat.debug("Parsing ReplicaStatus from the mime multipart entity");
1584

    
1585
        try {
1586
        	// parse the policy
1587
            Map<String, File> files = collectMultipartFiles();        
1588
            failure = ExceptionHandler.deserializeXml(new FileInputStream(files.get("failure")), 
1589
                    "Replication failed for an unknown reason.");
1590
            
1591
        } catch (Exception e2) {
1592
            throw new ServiceFailure("4700", "Couldn't resolve the multipart request: " +
1593
                e2.getMessage());
1594
            
1595
        }
1596
        
1597
        // get the replication status param
1598
        try {
1599
            replicationStatus = multipartparams.get("status").get(0);
1600
            status = ReplicationStatus.convert(replicationStatus);
1601

    
1602
        } catch (NullPointerException npe) {
1603

    
1604
            logMetacat.debug("The 'status' parameter was not found in the "
1605
                    + "multipartparams map.  Trying the params map.");
1606

    
1607
            try {
1608
                replicationStatus = params.get("status")[0];
1609
                status = ReplicationStatus.convert(replicationStatus
1610
                        .toLowerCase());
1611

    
1612
            } catch (Exception e) {
1613
                String msg = "The 'status' must be provided as a parameter and was not.";
1614
                logMetacat.error(msg);
1615
                throw new InvalidRequest("4730", msg);
1616

    
1617
            }
1618

    
1619
        }
1620

    
1621
        // get the target node reference param
1622
        try {
1623
            targetNode = multipartparams.get("nodeRef").get(0);
1624
            targetNodeRef = new NodeReference();
1625
            targetNodeRef.setValue(targetNode);
1626

    
1627
        } catch (NullPointerException e) {
1628
            logMetacat.debug("The 'nodeRef' parameter was not found in the "
1629
                    + "multipartparams map.  Trying the params map.");
1630

    
1631
            try {
1632
                targetNode = params.get("nodeRef")[0];
1633
                targetNodeRef = new NodeReference();
1634
                targetNodeRef.setValue(targetNode);
1635

    
1636
            } catch (Exception e1) {
1637
                String msg = "The 'nodeRef' must be provided as a parameter and was not.";
1638
                logMetacat.error(msg);
1639
                throw new InvalidRequest("4730", msg);
1640

    
1641
            }
1642

    
1643
        }
1644

    
1645
        result = CNodeService.getInstance(request).setReplicationStatus(
1646
                session, identifier, targetNodeRef, status, failure);
1647
        response.setStatus(200);
1648
        response.setContentType("text/xml");
1649
        return result;
1650

    
1651
    }
1652

    
1653
    /**
1654
     * Pass the request to update the replication metadata to CNodeService
1655
     * 
1656
     * @param pid
1657
     *            the identifier of the object to update the replication
1658
     *            metadata on
1659
     * 
1660
     * @throws ServiceFailure
1661
     * @throws NotImplemented
1662
     * @throws InvalidToken
1663
     * @throws NotAuthorized
1664
     * @throws InvalidRequest
1665
     * @throws NotFound
1666
     * @throws VersionMismatch 
1667
     * @throws JiBXException 
1668
     * @throws IOException 
1669
     * @throws IllegalAccessException 
1670
     * @throws InstantiationException 
1671
     */
1672
    public boolean updateReplicationMetadata(String pid) throws ServiceFailure,
1673
            NotImplemented, InvalidToken, NotAuthorized, InvalidRequest,
1674
            NotFound, VersionMismatch, InstantiationException, IllegalAccessException, IOException, JiBXException {
1675

    
1676
        boolean result = false;
1677
        long serialVersion = 0L;
1678
        String serialVersionStr = null;
1679
        Identifier identifier = new Identifier();
1680
        identifier.setValue(pid);
1681

    
1682
        // parse the replica
1683
        Map<String, File> files = collectMultipartFiles();        
1684
        Replica replica = TypeMarshaller.unmarshalTypeFromFile(Replica.class, files.get("replicaMetadata"));
1685

    
1686
        // get the serialVersion
1687
        try {
1688
            serialVersionStr = multipartparams.get("serialVersion").get(0);
1689
            serialVersion = new Long(serialVersionStr).longValue();
1690

    
1691
        } catch (NullPointerException e) {
1692
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
1693
            logMetacat.error(msg);
1694
            throw new InvalidRequest("4853", msg);
1695

    
1696
        }
1697

    
1698
        result = CNodeService.getInstance(request).updateReplicationMetadata(
1699
                session, identifier, replica, serialVersion);
1700
        response.setStatus(200);
1701
        response.setContentType("text/xml");
1702
        return result;
1703

    
1704
    }
1705

    
1706
}
(1-1/4)