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: 2012-01-12 12:18:38 -0800 (Thu, 12 Jan 2012) $'
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
 */
23
package edu.ucsb.nceas.metacat.restservice;
24

    
25
import java.io.ByteArrayInputStream;
26
import java.io.File;
27
import java.io.FileInputStream;
28
import java.io.IOException;
29
import java.io.InputStream;
30
import java.io.OutputStream;
31
import java.util.Date;
32
import java.util.Enumeration;
33
import java.util.Map;
34

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

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

    
83
import edu.ucsb.nceas.metacat.dataone.CNodeService;
84
import edu.ucsb.nceas.metacat.dataone.MNodeService;
85
import edu.ucsb.nceas.metacat.properties.PropertyService;
86
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
87

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

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

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

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

    
146
        try {
147

    
148
            // get the resource
149
            String resource = request.getPathInfo();
150
            resource = resource.substring(resource.indexOf("/") + 1);
151

    
152
            // for the rest of the resouce
153
            String extra = null;
154

    
155
            logMetacat.debug("handling verb " + httpVerb
156
                    + " request with resource '" + resource + "'");
157
            boolean status = false;
158

    
159
            if (resource != null) {
160

    
161
                if (resource.startsWith(RESOURCE_ACCESS_RULES)
162
                        && httpVerb == PUT) {
163
                    logMetacat.debug("Setting access policy");
164
                    // after the command
165
                    extra = parseTrailing(resource, RESOURCE_ACCESS_RULES);
166
                    setAccess(extra);
167
                    status = true;
168
                    logMetacat.debug("done setting access");
169

    
170
                } else if (resource.startsWith(RESOURCE_META)) {
171
                    logMetacat.debug("Using resource: " + RESOURCE_META);
172

    
173
                    // after the command
174
                    extra = parseTrailing(resource, RESOURCE_META);
175

    
176
                    // get
177
                    if (httpVerb == GET) {
178
                        getSystemMetadataObject(extra);
179
                        status = true;
180
                    }
181
                    // post to register system metadata
182
                    if (httpVerb == POST) {
183
                        registerSystemMetadata(extra);
184
                        status = true;
185
                    }
186

    
187
                } else if (resource.startsWith(RESOURCE_RESERVE)) {
188
                    // reserve the ID (in params)
189
                    if (httpVerb == POST) {
190
                        reserve();
191
                        status = true;
192
                    }
193
                } else if (resource.startsWith(RESOURCE_RESOLVE)) {
194

    
195
                    // after the command
196
                    extra = parseTrailing(resource, RESOURCE_RESOLVE);
197

    
198
                    // resolve the object location
199
                    if (httpVerb == GET) {
200
                        resolve(extra);
201
                        status = true;
202
                    }
203
                } else if (resource.startsWith(RESOURCE_OWNER)) {
204

    
205
                    // after the command
206
                    extra = parseTrailing(resource, RESOURCE_OWNER);
207

    
208
                    // set the owner
209
                    if (httpVerb == PUT) {
210
                        owner(extra);
211
                        status = true;
212
                    }
213
                } else if (resource.startsWith(RESOURCE_IS_AUTHORIZED)) {
214

    
215
                    // after the command
216
                    extra = parseTrailing(resource, RESOURCE_IS_AUTHORIZED);
217

    
218
                    // authorized?
219
                    if (httpVerb == GET) {
220
                        isAuthorized(extra);
221
                        status = true;
222
                    }
223
                } else if (resource.startsWith(RESOURCE_OBJECTS)) {
224
                    logMetacat.debug("Using resource 'object'");
225
                    logMetacat
226
                            .debug("D1 Rest: Starting resource processing...");
227

    
228
                    // after the command
229
                    extra = parseTrailing(resource, RESOURCE_OBJECTS);
230

    
231
                    logMetacat.debug("objectId: " + extra);
232
                    logMetacat.debug("verb:" + httpVerb);
233

    
234
                    if (httpVerb == GET) {
235
                        if (extra != null) {
236
                            getObject(extra);
237
                        } else {
238
                            listObjects();
239
                        }
240
                        status = true;
241
                    } else if (httpVerb == POST) {
242
                        putObject(extra, FUNCTION_NAME_INSERT);
243
                        status = true;
244
                    } else if (httpVerb == HEAD) {
245
                        describeObject(extra);
246
                        status = true;
247
                    }
248

    
249
                } else if (resource.startsWith(RESOURCE_FORMATS)) {
250
                    logMetacat.debug("Using resource: " + RESOURCE_FORMATS);
251

    
252
                    // after the command
253
                    extra = parseTrailing(resource, RESOURCE_FORMATS);
254

    
255
                    // handle each verb
256
                    if (httpVerb == GET) {
257
                        if (extra == null) {
258
                            // list the formats collection
259
                            listFormats();
260
                        } else {
261
                            // get the specified format
262
                            getFormat(extra);
263
                        }
264
                        status = true;
265
                    }
266

    
267
                } else if (resource.startsWith(RESOURCE_LOG)) {
268
                    logMetacat.debug("Using resource: " + RESOURCE_LOG);
269
                    // handle log events
270
                    if (httpVerb == GET) {
271
                        getLog();
272
                        status = true;
273
                    }
274

    
275
                } else if (resource.startsWith(RESOURCE_CHECKSUM)) {
276
                    logMetacat.debug("Using resource: " + RESOURCE_CHECKSUM);
277

    
278
                    // after the command
279
                    extra = parseTrailing(resource, RESOURCE_CHECKSUM);
280

    
281
                    // handle checksum requests
282
                    if (httpVerb == GET) {
283

    
284
                        checksum(extra);
285
                        status = true;
286

    
287
                    }
288

    
289
                } else if (resource.startsWith(RESOURCE_REPLICATION_POLICY)
290
                        && httpVerb == PUT) {
291

    
292
                    logMetacat.debug("Using resource: "
293
                            + RESOURCE_REPLICATION_POLICY);
294
                    // get the trailing pid
295
                    extra = parseTrailing(resource, RESOURCE_REPLICATION_POLICY);
296
                    setReplicationPolicy(extra);
297
                    status = true;
298

    
299
                } else if (resource.startsWith(RESOURCE_REPLICATION_META)
300
                        && httpVerb == PUT) {
301

    
302
                    logMetacat.debug("Using resource: "
303
                            + RESOURCE_REPLICATION_META);
304
                    // get the trailing pid
305
                    extra = parseTrailing(resource, RESOURCE_REPLICATION_META);
306
                    updateReplicationMetadata(extra);
307
                    status = true;
308

    
309
                } else if (resource.startsWith(RESOURCE_REPLICATION_NOTIFY)
310
                        && httpVerb == PUT) {
311

    
312
                    logMetacat.debug("Using resource: "
313
                            + RESOURCE_REPLICATION_NOTIFY);
314
                    // get the trailing pid
315
                    extra = parseTrailing(resource, RESOURCE_REPLICATION_NOTIFY);
316
                    setReplicationStatus(extra);
317
                    status = true;
318

    
319
                } else if (resource.startsWith(RESOURCE_REPLICATION_AUTHORIZED)
320
                        && httpVerb == GET) {
321

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

    
330
                } else if (resource.startsWith(Constants.RESOURCE_MONITOR_PING)) {
331
                    if (httpVerb == GET) {
332
                    	// after the command
333
                        extra = parseTrailing(resource, Constants.RESOURCE_MONITOR_PING);
334
                        
335
                        logMetacat.debug("processing ping request");
336
                        Date result = CNodeService.getInstance(request).ping();
337
                        // TODO: send to output	
338
                        status = true;
339
                    }
340
                } else if (resource.startsWith(Constants.RESOURCE_CHECKSUM)) {
341
                    if (httpVerb == GET) {
342
                        listChecksumAlgorithms();
343
                        status = true;
344
                    }
345
                } else if (resource.startsWith(Constants.RESOURCE_META_OBSOLETEDBY)
346
                        && httpVerb == PUT) {
347

    
348
                    logMetacat.debug("Using resource: "
349
                            + Constants.RESOURCE_META_OBSOLETEDBY);
350
                    // get the trailing pid
351
                    extra = parseTrailing(resource, Constants.RESOURCE_META_OBSOLETEDBY);
352
                    setObsoletedBy(extra);
353
                    status = true;
354

    
355
                }
356

    
357
                if (!status) {
358
                    throw new ServiceFailure("0000", "Unknown error, status = "
359
                            + status);
360
                }
361
            } else {
362
                throw new InvalidRequest("0000", "No resource matched for "
363
                        + resource);
364
            }
365
        } catch (BaseException be) {
366
            // report Exceptions as clearly and generically as possible
367
            OutputStream out = null;
368
            try {
369
                out = response.getOutputStream();
370
            } catch (IOException ioe) {
371
                logMetacat.error("Could not get output stream from response",
372
                        ioe);
373
            }
374
            serializeException(be, out);
375
        } catch (Exception e) {
376
            // report Exceptions as clearly and generically as possible
377
            logMetacat.error(e.getClass() + ": " + e.getMessage(), e);
378
            OutputStream out = null;
379
            try {
380
                out = response.getOutputStream();
381
            } catch (IOException ioe) {
382
                logMetacat.error("Could not get output stream from response",
383
                        ioe);
384
            }
385
            ServiceFailure se = new ServiceFailure("0000", e.getMessage());
386
            serializeException(se, out);
387
        }
388
    }
389

    
390
    /**
391
     * Get the checksum for the given guid
392
     * 
393
     * @param guid
394
     * @throws NotImplemented
395
     * @throws InvalidRequest
396
     * @throws NotFound
397
     * @throws NotAuthorized
398
     * @throws ServiceFailure
399
     * @throws InvalidToken
400
     * @throws IOException
401
     * @throws JiBXException
402
     */
403
    private void checksum(String guid) throws InvalidToken, ServiceFailure,
404
            NotAuthorized, NotFound, InvalidRequest, NotImplemented,
405
            JiBXException, IOException {
406
        Identifier guidid = new Identifier();
407
        guidid.setValue(guid);
408
        logMetacat.debug("getting checksum for object " + guid);
409
        Checksum c = CNodeService.getInstance(request).getChecksum(session,
410
                guidid);
411
        logMetacat.debug("got checksum " + c.getValue());
412
        response.setStatus(200);
413
        logMetacat.debug("serializing response");
414
        TypeMarshaller.marshalTypeToOutputStream(c, response.getOutputStream());
415
        logMetacat.debug("done serializing response.");
416

    
417
    }
418

    
419
    /**
420
     * get the logs based on passed params. Available params are token,
421
     * fromDate, toDate, event. See
422
     * http://mule1.dataone.org/ArchitectureDocs/mn_api_crud
423
     * .html#MN_crud.getLogRecords for more info
424
     * 
425
     * @throws NotImplemented
426
     * @throws InvalidRequest
427
     * @throws NotAuthorized
428
     * @throws ServiceFailure
429
     * @throws InvalidToken
430
     * @throws IOException
431
     * @throws JiBXException
432
     */
433
    private void getLog() throws InvalidToken, ServiceFailure, NotAuthorized,
434
            InvalidRequest, NotImplemented, IOException, JiBXException {
435

    
436
        Date fromDate = null;
437
        Date toDate = null;
438
        Event event = null;
439
        Integer start = null;
440
        Integer count = null;
441

    
442
        try {
443
            String fromDateS = params.get("fromDate")[0];
444
            logMetacat.debug("param fromDateS: " + fromDateS);
445
            fromDate = DateTimeMarshaller.deserializeDateToUTC(fromDateS);
446
        } catch (Exception e) {
447
            logMetacat.warn("Could not parse fromDate: " + e.getMessage());
448
        }
449
        try {
450
            String toDateS = params.get("toDate")[0];
451
            logMetacat.debug("param toDateS: " + toDateS);
452
            toDate = DateTimeMarshaller.deserializeDateToUTC(toDateS);
453
        } catch (Exception e) {
454
            logMetacat.warn("Could not parse toDate: " + e.getMessage());
455
        }
456
        try {
457
            String eventS = params.get("event")[0];
458
            event = Event.convert(eventS);
459
        } catch (Exception e) {
460
            logMetacat.warn("Could not parse event: " + e.getMessage());
461
        }
462
        logMetacat.debug("fromDate: " + fromDate + " toDate: " + toDate);
463

    
464
        try {
465
            start = Integer.parseInt(params.get("start")[0]);
466
        } catch (Exception e) {
467
            logMetacat.warn("Could not parse start: " + e.getMessage());
468
        }
469
        try {
470
            count = Integer.parseInt(params.get("count")[0]);
471
        } catch (Exception e) {
472
            logMetacat.warn("Could not parse count: " + e.getMessage());
473
        }
474

    
475
        logMetacat.debug("calling getLogRecords");
476
        Log log = CNodeService.getInstance(request).getLogRecords(session,
477
                fromDate, toDate, event, start, count);
478

    
479
        OutputStream out = response.getOutputStream();
480
        response.setStatus(200);
481
        response.setContentType("text/xml");
482

    
483
        TypeMarshaller.marshalTypeToOutputStream(log, out);
484

    
485
    }
486

    
487
    /**
488
     * Implements REST version of DataONE CRUD API --> get
489
     * 
490
     * @param guid
491
     *            ID of data object to be read
492
     * @throws NotImplemented
493
     * @throws InvalidRequest
494
     * @throws NotFound
495
     * @throws NotAuthorized
496
     * @throws ServiceFailure
497
     * @throws InvalidToken
498
     * @throws IOException
499
     */
500
    protected void getObject(String guid) throws InvalidToken, ServiceFailure,
501
            NotAuthorized, NotFound, InvalidRequest, NotImplemented,
502
            IOException {
503

    
504
        Identifier id = new Identifier();
505
        id.setValue(guid);
506

    
507
        SystemMetadata sm = CNodeService.getInstance(request)
508
                .getSystemMetadata(session, id);
509

    
510
        // set the content type
511
        if (sm.getFormatId()
512
                .getValue()
513
                .trim()
514
                .equals(ObjectFormatCache.getInstance().getFormat("text/csv")
515
                        .getFormatId().getValue())) {
516
            response.setContentType("text/csv");
517
            response.setHeader("Content-Disposition",
518
                    "inline; filename=" + id.getValue() + ".csv");
519
        } else if (sm
520
                .getFormatId()
521
                .getValue()
522
                .trim()
523
                .equals(ObjectFormatCache.getInstance().getFormat("text/plain")
524
                        .getFormatId().getValue())) {
525
            response.setContentType("text/plain");
526
            response.setHeader("Content-Disposition",
527
                    "inline; filename=" + id.getValue() + ".txt");
528
        } else if (sm
529
                .getFormatId()
530
                .getValue()
531
                .trim()
532
                .equals(ObjectFormatCache.getInstance()
533
                        .getFormat("application/octet-stream").getFormatId()
534
                        .getValue())) {
535
            response.setContentType("application/octet-stream");
536
        } else {
537
            response.setContentType("text/xml");
538
            response.setHeader("Content-Disposition",
539
                    "inline; filename=" + id.getValue() + ".xml");
540
        }
541

    
542
        InputStream data = CNodeService.getInstance(request).get(session, id);
543

    
544
        OutputStream out = response.getOutputStream();
545
        response.setStatus(200);
546
        IOUtils.copyLarge(data, out);
547

    
548
    }
549

    
550
    /**
551
     * Implements REST version of DataONE CRUD API --> getSystemMetadata
552
     * 
553
     * @param guid
554
     *            ID of data object to be read
555
     * @throws NotImplemented
556
     * @throws InvalidRequest
557
     * @throws NotFound
558
     * @throws NotAuthorized
559
     * @throws ServiceFailure
560
     * @throws InvalidToken
561
     * @throws IOException
562
     * @throws JiBXException
563
     */
564
    protected void getSystemMetadataObject(String guid) throws InvalidToken,
565
            ServiceFailure, NotAuthorized, NotFound, InvalidRequest,
566
            NotImplemented, IOException, JiBXException {
567

    
568
        Identifier id = new Identifier();
569
        id.setValue(guid);
570
        SystemMetadata sysmeta = CNodeService.getInstance(request)
571
                .getSystemMetadata(session, id);
572

    
573
        response.setContentType("text/xml");
574
        response.setStatus(200);
575
        OutputStream out = response.getOutputStream();
576

    
577
        // Serialize and write it to the output stream
578
        TypeMarshaller.marshalTypeToOutputStream(sysmeta, out);
579
    }
580

    
581
    /**
582
     * Earthgrid API > Put Service >Put Function : calls MetacatHandler >
583
     * handleInsertOrUpdateAction
584
     * 
585
     * @param guid
586
     *            - ID of data object to be inserted or updated. If action is
587
     *            update, the pid is the existing pid. If insert, the pid is the
588
     *            new one
589
     * @throws InvalidRequest
590
     * @throws ServiceFailure
591
     * @throws IdentifierNotUnique
592
     * @throws JiBXException
593
     * @throws NotImplemented
594
     * @throws InvalidSystemMetadata
595
     * @throws InsufficientResources
596
     * @throws UnsupportedType
597
     * @throws NotAuthorized
598
     * @throws InvalidToken
599
     * @throws IOException
600
     * @throws IllegalAccessException
601
     * @throws InstantiationException
602
     */
603
    protected void putObject(String pid, String action) throws ServiceFailure,
604
            InvalidRequest, IdentifierNotUnique, JiBXException, InvalidToken,
605
            NotAuthorized, UnsupportedType, InsufficientResources,
606
            InvalidSystemMetadata, NotImplemented, IOException,
607
            InstantiationException, IllegalAccessException {
608
        logMetacat.debug("Entering putObject: " + pid + "/" + action);
609

    
610
        // Read the incoming data from its Mime Multipart encoding
611
        Map<String, File> files = collectMultipartFiles();
612
        InputStream object = null;
613
        InputStream sysmeta = null;
614

    
615
        File smFile = files.get("sysmeta");
616
        sysmeta = new FileInputStream(smFile);
617
        File objFile = files.get("object");
618
        object = new FileInputStream(objFile);
619

    
620
        if (action.equals(FUNCTION_NAME_INSERT)) { // handle inserts
621

    
622
            logMetacat.debug("Commence creation...");
623
            SystemMetadata smd = TypeMarshaller.unmarshalTypeFromStream(
624
                    SystemMetadata.class, sysmeta);
625

    
626
            Identifier id = new Identifier();
627
            id.setValue(pid);
628
            logMetacat.debug("creating object with pid " + id.getValue());
629
            Identifier rId = CNodeService.getInstance(request).create(session,
630
                    id, object, smd);
631

    
632
            OutputStream out = response.getOutputStream();
633
            response.setStatus(200);
634
            response.setContentType("text/xml");
635

    
636
            TypeMarshaller.marshalTypeToOutputStream(rId, out);
637

    
638
        } else {
639
            throw new InvalidRequest("1000", "Operation must be create.");
640
        }
641
    }
642

    
643
    /**
644
     * List the object formats registered with the system
645
     * 
646
     * @throws NotImplemented
647
     * @throws InsufficientResources
648
     * @throws NotFound
649
     * @throws ServiceFailure
650
     * @throws InvalidRequest
651
     * @throws IOException
652
     * @throws JiBXException
653
     */
654
    private void listFormats() throws InvalidRequest, ServiceFailure, NotFound,
655
            InsufficientResources, NotImplemented, IOException, JiBXException {
656
        logMetacat.debug("Entering listFormats()");
657

    
658
        ObjectFormatList objectFormatList = CNodeService.getInstance(request)
659
                .listFormats();
660
        // get the response output stream
661
        OutputStream out = response.getOutputStream();
662
        response.setStatus(200);
663
        response.setContentType("text/xml");
664

    
665
        // style the object with a processing directive
666
        String stylesheet = null;
667
        try {
668
            stylesheet = PropertyService.getProperty("dataone.types.xsl");
669
        } catch (PropertyNotFoundException e) {
670
            logMetacat.warn("Could not locate DataONE types XSLT: "
671
                    + e.getMessage());
672
        }
673

    
674
        TypeMarshaller.marshalTypeToOutputStream(objectFormatList, out,
675
                stylesheet);
676

    
677
    }
678
    
679
    private void listChecksumAlgorithms() throws IOException, ServiceFailure,
680
			NotImplemented, JiBXException {
681
		logMetacat.debug("Entering listFormats()");
682

    
683
		ChecksumAlgorithmList result = CNodeService.getInstance(request).listChecksumAlgorithms();
684

    
685
		// get the response output stream
686
		OutputStream out = response.getOutputStream();
687
		response.setStatus(200);
688
		response.setContentType("text/xml");
689

    
690
		// style the object with a processing directive
691
		String stylesheet = null;
692
		try {
693
			stylesheet = PropertyService.getProperty("dataone.types.xsl");
694
		} catch (PropertyNotFoundException e) {
695
			logMetacat.warn("Could not locate DataONE types XSLT: "
696
					+ e.getMessage());
697
		}
698

    
699
		TypeMarshaller.marshalTypeToOutputStream(result, out, stylesheet);
700

    
701
	}
702
    
703
    /**
704
     * http://mule1.dataone.org/ArchitectureDocs-current/apis/CN_APIs.html#CNRead.describe
705
     * @param pid
706
     * @throws InvalidToken
707
     * @throws ServiceFailure
708
     * @throws NotAuthorized
709
     * @throws NotFound
710
     * @throws NotImplemented
711
     * @throws InvalidRequest
712
     */
713
    private void describeObject(String pid) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented, InvalidRequest
714
    {
715
        response.setStatus(200);
716
        response.setContentType("text/xml");
717
        
718
        Identifier id = new Identifier();
719
        id.setValue(pid);
720

    
721
        DescribeResponse dr = CNodeService.getInstance(request).describe(session, id);
722
        //response.addHeader("pid", pid);
723
        response.addHeader("DataONE-Checksum", dr.getDataONE_Checksum().getAlgorithm() + "," + dr.getDataONE_Checksum().getValue());
724
        response.addHeader("Content-Length", dr.getContent_Length() + "");
725
        response.addHeader("Last-Modified", DateTimeMarshaller.serializeDateToUTC(dr.getLast_Modified()));
726
        response.addHeader("DataONE-ObjectFormat", dr.getDataONE_ObjectFormatIdentifier().getValue());
727
        response.addHeader("DataONE-SerialVersion", dr.getSerialVersion().toString());
728

    
729
    }
730

    
731
    /**
732
     * Return the requested object format
733
     * 
734
     * @param fmtidStr
735
     *            the requested format identifier as a string
736
     * @throws NotImplemented
737
     * @throws InsufficientResources
738
     * @throws NotFound
739
     * @throws ServiceFailure
740
     * @throws InvalidRequest
741
     * @throws IOException
742
     * @throws JiBXException
743
     */
744
    private void getFormat(String fmtidStr) throws InvalidRequest,
745
            ServiceFailure, NotFound, InsufficientResources, NotImplemented,
746
            IOException, JiBXException {
747
        logMetacat.debug("Entering listFormats()");
748

    
749
        ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
750
        fmtid.setValue(fmtidStr);
751

    
752
        // get the specified object format
753
        ObjectFormat objectFormat = CNodeService.getInstance(request)
754
                .getFormat(fmtid);
755

    
756
        OutputStream out = response.getOutputStream();
757
        response.setStatus(200);
758
        response.setContentType("text/xml");
759

    
760
        TypeMarshaller.marshalTypeToOutputStream(objectFormat, out);
761

    
762
    }
763

    
764
    /**
765
     * Reserve the given Identifier
766
     * 
767
     * @throws InvalidToken
768
     * @throws ServiceFailure
769
     * @throws NotAuthorized
770
     * @throws IdentifierNotUnique
771
     * @throws NotImplemented
772
     * @throws InvalidRequest
773
     * @throws IOException
774
     * @throws JiBXException
775
     */
776
    private void reserve() throws InvalidToken, ServiceFailure, NotAuthorized,
777
            IdentifierNotUnique, NotImplemented, InvalidRequest, IOException,
778
            JiBXException {
779
        Identifier pid = null;
780
        String scope = null;
781
        String format = null;
782
        // gather the params
783
        try {
784
            String id = params.get("pid")[0];
785
            pid = new Identifier();
786
            pid.setValue(id);
787
        } catch (Exception e) {
788
            logMetacat.warn("pid not specified");
789
        }
790
        try {
791
            scope = params.get("scope")[0];
792
        } catch (Exception e) {
793
            logMetacat.warn("pid not specified");
794
        }
795
        try {
796
            format = params.get("format")[0];
797
        } catch (Exception e) {
798
            logMetacat.warn("pid not specified");
799
        }
800
        // call the implementation
801
        Identifier resultPid = CNodeService.getInstance(request)
802
                .reserveIdentifier(session, pid);
803
        OutputStream out = response.getOutputStream();
804
        response.setStatus(200);
805
        response.setContentType("text/xml");
806
        // send back the reserved pid
807
        TypeMarshaller.marshalTypeToOutputStream(resultPid, out);
808
    }
809

    
810
    /**
811
     * 
812
     * @param id
813
     * @throws InvalidRequest
814
     * @throws InvalidToken
815
     * @throws ServiceFailure
816
     * @throws NotAuthorized
817
     * @throws NotFound
818
     * @throws NotImplemented
819
     * @throws IOException
820
     * @throws JiBXException
821
     */
822
    private void resolve(String id) throws InvalidRequest, InvalidToken,
823
            ServiceFailure, NotAuthorized, NotFound, NotImplemented,
824
            IOException, JiBXException {
825
        Identifier pid = new Identifier();
826
        pid.setValue(id);
827
        ObjectLocationList locationList = CNodeService.getInstance(request)
828
                .resolve(session, pid);
829
        OutputStream out = response.getOutputStream();
830
        response.setStatus(200);
831
        response.setContentType("text/xml");
832
        TypeMarshaller.marshalTypeToOutputStream(locationList, out);
833

    
834
    }
835

    
836
    /**
837
     * Set the owner of a resource
838
     * 
839
     * @param id
840
     * @throws JiBXException
841
     * @throws InvalidToken
842
     * @throws ServiceFailure
843
     * @throws NotFound
844
     * @throws NotAuthorized
845
     * @throws NotImplemented
846
     * @throws InvalidRequest
847
     * @throws IOException
848
     * @throws IllegalAccessException
849
     * @throws InstantiationException
850
     * @throws VersionMismatch 
851
     */
852
    private void owner(String id) throws JiBXException, InvalidToken,
853
            ServiceFailure, NotFound, NotAuthorized, NotImplemented,
854
            InvalidRequest, IOException, InstantiationException,
855
            IllegalAccessException, VersionMismatch {
856

    
857
        Identifier pid = new Identifier();
858
        pid.setValue(id);
859

    
860
        long serialVersion = 0L;
861
        String serialVersionStr = null;
862

    
863
        // get the serialVersion
864
        try {
865
            serialVersionStr = params.get("serialVersion")[0];
866
            serialVersion = new Long(serialVersionStr).longValue();
867

    
868
        } catch (NullPointerException e) {
869
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
870
            logMetacat.error(msg);
871
            throw new InvalidRequest("4442", msg);
872

    
873
        }
874

    
875
        // get the subject
876
        String subjectStr = params.get("subject")[0];
877
        Subject subject = TypeMarshaller.unmarshalTypeFromStream(Subject.class,
878
                new ByteArrayInputStream(subjectStr.getBytes("UTF-8")));
879

    
880
        Identifier retPid = CNodeService.getInstance(request).setRightsHolder(session, pid, subject, serialVersion);
881
        OutputStream out = response.getOutputStream();
882
        response.setStatus(200);
883
        response.setContentType("text/xml");
884
        TypeMarshaller.marshalTypeToOutputStream(retPid, out);
885
    }
886

    
887
    /**
888
     * Processes the authorization check for given id
889
     * 
890
     * @param id
891
     * @return
892
     * @throws ServiceFailure
893
     * @throws InvalidToken
894
     * @throws NotFound
895
     * @throws NotAuthorized
896
     * @throws NotImplemented
897
     * @throws InvalidRequest
898
     */
899
    private boolean isAuthorized(String id) throws ServiceFailure,
900
            InvalidToken, NotFound, NotAuthorized, NotImplemented,
901
            InvalidRequest {
902
        Identifier pid = new Identifier();
903
        pid.setValue(id);
904
        String permission = params.get("action")[0];
905
        boolean result = CNodeService.getInstance(request).isAuthorized(
906
                session, pid, Permission.convert(permission));
907
        response.setStatus(200);
908
        response.setContentType("text/xml");
909
        return result;
910
    }
911

    
912
    /**
913
     * Register System Metadata without data or metadata object
914
     * 
915
     * @param pid
916
     *            identifier for System Metadata entry
917
     * @throws JiBXException
918
     * @throws FileUploadException
919
     * @throws IOException
920
     * @throws InvalidRequest
921
     * @throws ServiceFailure
922
     * @throws InvalidSystemMetadata
923
     * @throws NotAuthorized
924
     * @throws NotImplemented
925
     * @throws IllegalAccessException
926
     * @throws InstantiationException
927
     */
928
    protected void registerSystemMetadata(String pid)
929
            throws ServiceFailure, InvalidRequest, IOException,
930
            FileUploadException, JiBXException, NotImplemented, NotAuthorized,
931
            InvalidSystemMetadata, InstantiationException,
932
            IllegalAccessException {
933
        logMetacat.debug("Entering registerSystemMetadata: " + pid);
934

    
935
        // get the system metadata from the request
936
        SystemMetadata systemMetadata = collectSystemMetadata();
937

    
938
        Identifier guid = new Identifier();
939
        guid.setValue(pid);
940
        logMetacat.debug("registering system metadata with pid "
941
                + guid.getValue());
942
        Identifier retGuid = CNodeService.getInstance(request)
943
                .registerSystemMetadata(session, guid, systemMetadata);
944

    
945
        OutputStream out = response.getOutputStream();
946
        response.setStatus(200);
947
        response.setContentType("text/xml");
948
        
949
        TypeMarshaller.marshalTypeToOutputStream(retGuid, out);
950

    
951
    }
952

    
953
    /**
954
     * set the access perms on a document
955
     * 
956
     * @throws JiBXException
957
     * @throws InvalidRequest
958
     * @throws NotImplemented
959
     * @throws NotAuthorized
960
     * @throws NotFound
961
     * @throws ServiceFailure
962
     * @throws InvalidToken
963
     * @throws IllegalAccessException
964
     * @throws InstantiationException
965
     * @throws IOException
966
     * @throws SAXException
967
     * @throws ParserConfigurationException
968
     * @throws VersionMismatch 
969
     */
970
    protected void setAccess(String pid) throws JiBXException, InvalidToken,
971
            ServiceFailure, NotFound, NotAuthorized, NotImplemented,
972
            InvalidRequest, IOException, InstantiationException,
973
            IllegalAccessException, ParserConfigurationException, SAXException, VersionMismatch {
974

    
975
        long serialVersion = 0L;
976
        String serialVersionStr = null;
977

    
978
        // get the serialVersion
979
        try {
980
            serialVersionStr = params.get("serialVersion")[0];
981
            serialVersion = new Long(serialVersionStr).longValue();
982

    
983
        } catch (NullPointerException e) {
984
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
985
            logMetacat.error(msg);
986
            throw new InvalidRequest("4402", msg);
987

    
988
        }
989

    
990
        Identifier id = new Identifier();
991
        id.setValue(pid);
992

    
993
        AccessPolicy accessPolicy = collectAccessPolicy();
994
        CNodeService.getInstance(request).setAccessPolicy(session, id,
995
                accessPolicy, serialVersion);
996

    
997
    }
998

    
999
    /**
1000
     * List the objects
1001
     * 
1002
     * @throws NotImplemented
1003
     * @throws InvalidRequest
1004
     * @throws NotAuthorized
1005
     * @throws ServiceFailure
1006
     * @throws InvalidToken
1007
     * @throws NotFound
1008
     * @throws IOException
1009
     * @throws JiBXException
1010
     * @throws Exception
1011
     */
1012
    private void listObjects() throws InvalidToken, ServiceFailure,
1013
            NotAuthorized, InvalidRequest, NotImplemented, NotFound,
1014
            IOException, JiBXException {
1015

    
1016
        Date startTime = null;
1017
        Date endTime = null;
1018
        ObjectFormat objectFormat = null;
1019
        boolean replicaStatus = false;
1020
        int start = 0;
1021
        int count = -1;
1022
        Enumeration<String> paramlist = request.getParameterNames();
1023
        while (paramlist.hasMoreElements()) {
1024
            // parse the params and make the call
1025
            String name = paramlist.nextElement();
1026
            String[] value = request.getParameterValues(name);
1027

    
1028
            if (name.equals("startTime") && value != null) {
1029
                try {
1030
                    startTime = DateTimeMarshaller
1031
                            .deserializeDateToUTC(value[0]);
1032
                } catch (Exception e) {
1033
                    // if we can't parse it, just don't use the startTime param
1034
                    logMetacat.warn("Could not parse startTime: " + value[0]);
1035
                    startTime = null;
1036
                }
1037
            } else if (name.equals("endTime") && value != null) {
1038
                try {
1039
                    endTime = DateTimeMarshaller.deserializeDateToUTC(value[0]);
1040
                } catch (Exception e) {
1041
                    // if we can't parse it, just don't use the endTime param
1042
                    logMetacat.warn("Could not parse endTime: " + value[0]);
1043
                    endTime = null;
1044
                }
1045
            } else if (name.equals("objectFormat") && value != null) {
1046
                objectFormat = ObjectFormatCache.getInstance().getFormat(
1047
                        value[0]);
1048
            } else if (name.equals("replicaStatus") && value != null) {
1049
                replicaStatus = Boolean.parseBoolean(value[0]);
1050
            } else if (name.equals("start") && value != null) {
1051
                start = Integer.valueOf(value[0]);
1052
            } else if (name.equals("count") && value != null) {
1053
                count = Integer.valueOf(value[0]);
1054
            }
1055
        }
1056
        // make the call
1057
        logMetacat.debug("session: " + session + " startTime: " + startTime
1058
                + " endtime: " + endTime + " objectFormat: " + objectFormat
1059
                + " replicaStatus: " + replicaStatus + " start: " + start
1060
                + " count: " + count);
1061

    
1062
        ObjectFormatIdentifier fmtid = null;
1063
        if (objectFormat != null) {
1064
            fmtid = objectFormat.getFormatId();
1065
        }
1066

    
1067
        // get the list
1068
        ObjectList ol = CNodeService.getInstance(request).listObjects(session,
1069
                startTime, endTime, fmtid, replicaStatus, start, count);
1070

    
1071
        // send it
1072
        OutputStream out = response.getOutputStream();
1073
        response.setStatus(200);
1074
        response.setContentType("text/xml");
1075

    
1076
        // style the object with a processing directive
1077
        String stylesheet = null;
1078
        try {
1079
            stylesheet = PropertyService.getProperty("dataone.types.xsl");
1080
        } catch (PropertyNotFoundException e) {
1081
            logMetacat.warn("Could not locate DataONE types XSLT: "
1082
                    + e.getMessage());
1083
        }
1084

    
1085
        // Serialize and write it to the output stream
1086
        TypeMarshaller.marshalTypeToOutputStream(ol, out, stylesheet);
1087
    }
1088

    
1089
    /**
1090
     * Pass the request to get node replication authorization to CNodeService
1091
     * 
1092
     * @param pid
1093
     *            the identifier of the object to get authorization to replicate
1094
     * 
1095
     * @throws NotImplemented
1096
     * @throws NotAuthorized
1097
     * @throws InvalidToken
1098
     * @throws ServiceFailure
1099
     * @throws NotFound
1100
     * @throws InvalidRequest
1101
     */
1102
    public boolean isNodeAuthorized(String pid) throws NotImplemented,
1103
            NotAuthorized, InvalidToken, ServiceFailure, NotFound,
1104
            InvalidRequest {
1105

    
1106
        boolean result = false;
1107
        Subject targetNodeSubject = new Subject();
1108
        String nodeSubject = null;
1109
        String replPermission = null;
1110

    
1111
        // get the pid
1112
        Identifier identifier = new Identifier();
1113
        identifier.setValue(pid);
1114

    
1115
        // get the target node subject
1116
        try {
1117
            nodeSubject = params.get("targetNodeSubject")[0];
1118
            targetNodeSubject.setValue(nodeSubject);
1119

    
1120
        } catch (NullPointerException e) {
1121
            String msg = "The 'targetNodeSubject' must be provided as a parameter and was not.";
1122
            logMetacat.error(msg);
1123
            throw new InvalidRequest("4873", msg);
1124

    
1125
        }
1126

    
1127
        result = CNodeService.getInstance(request).isNodeAuthorized(session, targetNodeSubject, identifier);
1128

    
1129
        response.setStatus(200);
1130
        response.setContentType("text/xml");
1131
        return result;
1132

    
1133
    }
1134

    
1135
    /**
1136
     * Pass the request to set the replication policy to CNodeService
1137
     * 
1138
     * @param pid
1139
     *            the identifier of the object to set the replication policy on
1140
     * 
1141
     * @throws NotImplemented
1142
     * @throws NotFound
1143
     * @throws NotAuthorized
1144
     * @throws ServiceFailure
1145
     * @throws InvalidRequest
1146
     * @throws InvalidToken
1147
     * @throws IOException
1148
     * @throws InstantiationException
1149
     * @throws IllegalAccessException
1150
     * @throws JiBXException
1151
     * @throws VersionMismatch 
1152
     */
1153
    public boolean setReplicationPolicy(String pid) throws NotImplemented,
1154
            NotFound, NotAuthorized, ServiceFailure, InvalidRequest,
1155
            InvalidToken, IOException, InstantiationException,
1156
            IllegalAccessException, JiBXException, VersionMismatch {
1157

    
1158
        boolean result = false;
1159
        ReplicationPolicy policy = null;
1160
        long serialVersion = 0L;
1161
        String serialVersionStr = null;
1162

    
1163
        Identifier identifier = new Identifier();
1164
        identifier.setValue(pid);
1165

    
1166
        policy = collectReplicationPolicy();
1167

    
1168
        // get the serialVersion
1169
        try {
1170
            serialVersionStr = multipartparams.get("serialVersion").get(0);
1171
            serialVersion = new Long(serialVersionStr).longValue();
1172

    
1173
        } catch (NullPointerException e) {
1174
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
1175
            logMetacat.error(msg);
1176
            throw new InvalidRequest("4883", msg);
1177

    
1178
        }
1179
        result = CNodeService.getInstance(request).setReplicationPolicy(
1180
                session, identifier, policy, serialVersion);
1181
        response.setStatus(200);
1182
        response.setContentType("text/xml");
1183
        return result;
1184

    
1185
    }
1186
    
1187
    public boolean setObsoletedBy(String pid) throws NotImplemented,
1188
	    NotFound, NotAuthorized, ServiceFailure, InvalidRequest,
1189
	    InvalidToken, IOException, InstantiationException,
1190
	    IllegalAccessException, JiBXException, VersionMismatch {
1191
	
1192
	boolean result = false;
1193
	long serialVersion = 0L;
1194
	String serialVersionStr = null;
1195
	
1196
	Identifier identifier = new Identifier();
1197
	identifier.setValue(pid);
1198
	
1199
	Identifier obsoletedByPid = null;
1200
	try {
1201
		String obsoletedByPidString = params.get("obsoletedByPid")[0];
1202
		obsoletedByPid = new Identifier();
1203
		obsoletedByPid.setValue(obsoletedByPidString);
1204
	} catch (NullPointerException e) {
1205
	    String msg = "The 'obsoletedByPid' must be provided as a parameter and was not.";
1206
	    logMetacat.error(msg);
1207
	    throw new InvalidRequest("4883", msg);
1208
	}
1209
	
1210
	// get the serialVersion
1211
	try {
1212
	    serialVersionStr = params.get("serialVersion")[0];
1213
	    serialVersion = new Long(serialVersionStr).longValue();
1214
	
1215
	} catch (NullPointerException e) {
1216
	    String msg = "The 'serialVersion' must be provided as a parameter and was not.";
1217
	    logMetacat.error(msg);
1218
	    throw new InvalidRequest("4883", msg);
1219
	
1220
	}
1221
	result = CNodeService.getInstance(request).setObsoletedBy(
1222
	        session, identifier, obsoletedByPid, serialVersion);
1223
	response.setStatus(200);
1224
	response.setContentType("text/xml");
1225
	return result;
1226
	
1227
	}
1228

    
1229
    /**
1230
     * Pass the request to set the replication status to CNodeService
1231
     * 
1232
     * @param pid
1233
     *            the identifier of the object to set the replication status on
1234
     * 
1235
     * @throws ServiceFailure
1236
     * @throws NotImplemented
1237
     * @throws InvalidToken
1238
     * @throws NotAuthorized
1239
     * @throws InvalidRequest
1240
     * @throws NotFound
1241
     * @throws JiBXException 
1242
     * @throws IllegalAccessException 
1243
     * @throws InstantiationException 
1244
     * @throws IOException 
1245
     */
1246
    public boolean setReplicationStatus(String pid) throws ServiceFailure,
1247
            NotImplemented, InvalidToken, NotAuthorized, InvalidRequest,
1248
            NotFound {
1249
        
1250
        boolean result = false;
1251
        Identifier identifier = new Identifier();
1252
        identifier.setValue(pid);
1253
        BaseException failure = null;
1254
        ReplicationStatus status = null;
1255
        String replicationStatus = null;
1256
        NodeReference targetNodeRef = null;
1257
        String targetNode = null;
1258

    
1259
        // Parse the params out of the multipart form data
1260
        // Read the incoming data from its Mime Multipart encoding
1261
        logMetacat.debug("Parsing ReplicaStatus from the mime multipart entity");
1262

    
1263
        try {
1264
            failure = collectReplicationStatus();
1265
            
1266
        } catch (IOException e2) {
1267
            throw new ServiceFailure("4700", "Couldn't resolve the multipart request: " +
1268
                e2.getMessage());
1269
            
1270
        } catch (InstantiationException e2) {
1271
            throw new ServiceFailure("4700", "Couldn't resolve the multipart request: " +
1272
                e2.getMessage());
1273
            
1274
        } catch (IllegalAccessException e2) {
1275
            throw new ServiceFailure("4700", "Couldn't resolve the multipart request: " +
1276
                    e2.getMessage());
1277
            
1278
        } catch (JiBXException e2) {
1279
            throw new ServiceFailure("4700", "Couldn't resolve the multipart request: " +
1280
                    e2.getMessage());
1281
            
1282
        }
1283
        
1284
        // get the replication status param
1285
        try {
1286
            replicationStatus = multipartparams.get("status").get(0);
1287
            status = ReplicationStatus.convert(replicationStatus);
1288

    
1289
        } catch (NullPointerException npe) {
1290

    
1291
            logMetacat.debug("The 'status' parameter was not found in the "
1292
                    + "multipartparams map.  Trying the params map.");
1293

    
1294
            try {
1295
                replicationStatus = params.get("status")[0];
1296
                status = ReplicationStatus.convert(replicationStatus
1297
                        .toLowerCase());
1298

    
1299
            } catch (Exception e) {
1300
                String msg = "The 'status' must be provided as a parameter and was not.";
1301
                logMetacat.error(msg);
1302
                throw new InvalidRequest("4730", msg);
1303

    
1304
            }
1305

    
1306
        }
1307

    
1308
        // get the target node reference param
1309
        try {
1310
            targetNode = multipartparams.get("nodeRef").get(0);
1311
            targetNodeRef = new NodeReference();
1312
            targetNodeRef.setValue(targetNode);
1313

    
1314
        } catch (NullPointerException e) {
1315
            logMetacat.debug("The 'nodeRef' parameter was not found in the "
1316
                    + "multipartparams map.  Trying the params map.");
1317

    
1318
            try {
1319
                targetNode = params.get("nodeRef")[0];
1320
                targetNodeRef = new NodeReference();
1321
                targetNodeRef.setValue(targetNode);
1322

    
1323
            } catch (Exception e1) {
1324
                String msg = "The 'nodeRef' must be provided as a parameter and was not.";
1325
                logMetacat.error(msg);
1326
                throw new InvalidRequest("4730", msg);
1327

    
1328
            }
1329

    
1330
        }
1331

    
1332
        result = CNodeService.getInstance(request).setReplicationStatus(
1333
                session, identifier, targetNodeRef, status, failure);
1334
        response.setStatus(200);
1335
        response.setContentType("text/xml");
1336
        return result;
1337

    
1338
    }
1339

    
1340
    /**
1341
     * Pass the request to update the replication metadata to CNodeService
1342
     * 
1343
     * @param pid
1344
     *            the identifier of the object to update the replication
1345
     *            metadata on
1346
     * 
1347
     * @throws ServiceFailure
1348
     * @throws NotImplemented
1349
     * @throws InvalidToken
1350
     * @throws NotAuthorized
1351
     * @throws InvalidRequest
1352
     * @throws NotFound
1353
     * @throws VersionMismatch 
1354
     */
1355
    public boolean updateReplicationMetadata(String pid) throws ServiceFailure,
1356
            NotImplemented, InvalidToken, NotAuthorized, InvalidRequest,
1357
            NotFound, VersionMismatch {
1358

    
1359
        boolean result = false;
1360
        long serialVersion = 0L;
1361
        String serialVersionStr = null;
1362
        Replica replica = null;
1363
        Identifier identifier = new Identifier();
1364
        identifier.setValue(pid);
1365

    
1366
        replica = collectReplicaMetadata();
1367

    
1368
        // get the serialVersion
1369
        try {
1370
            serialVersionStr = multipartparams.get("serialVersion").get(0);
1371
            serialVersion = new Long(serialVersionStr).longValue();
1372

    
1373
        } catch (NullPointerException e) {
1374
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
1375
            logMetacat.error(msg);
1376
            throw new InvalidRequest("4853", msg);
1377

    
1378
        }
1379

    
1380
        result = CNodeService.getInstance(request).updateReplicationMetadata(
1381
                session, identifier, replica, serialVersion);
1382
        response.setStatus(200);
1383
        response.setContentType("text/xml");
1384
        return result;
1385

    
1386
    }
1387

    
1388
}
(1-1/9)