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: cjones $'
7
 *     '$Date: 2011-12-16 07:36:39 -0800 (Fri, 16 Dec 2011) $'
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.types.v1.AccessPolicy;
58
import org.dataone.service.types.v1.Checksum;
59
import org.dataone.service.types.v1.Event;
60
import org.dataone.service.types.v1.Identifier;
61
import org.dataone.service.types.v1.Log;
62
import org.dataone.service.types.v1.NodeReference;
63
import org.dataone.service.types.v1.ObjectFormat;
64
import org.dataone.service.types.v1.ObjectFormatIdentifier;
65
import org.dataone.service.types.v1.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.v1.SystemMetadata;
74
import org.dataone.service.util.DateTimeMarshaller;
75
import org.dataone.service.util.TypeMarshaller;
76
import org.jibx.runtime.JiBXException;
77
import org.xml.sax.SAXException;
78

    
79
import edu.ucsb.nceas.metacat.dataone.CNodeService;
80
import edu.ucsb.nceas.metacat.properties.PropertyService;
81
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
82

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

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

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

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

    
142
        try {
143

    
144
            // get the resource
145
            String resource = request.getPathInfo();
146
            resource = resource.substring(resource.indexOf("/") + 1);
147

    
148
            // for the rest of the resouce
149
            String extra = null;
150

    
151
            logMetacat.debug("handling verb " + httpVerb
152
                    + " request with resource '" + resource + "'");
153
            boolean status = false;
154

    
155
            if (resource != null) {
156

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

    
166
                } else if (resource.startsWith(RESOURCE_META)) {
167
                    logMetacat.debug("Using resource: " + RESOURCE_META);
168

    
169
                    // after the command
170
                    extra = parseTrailing(resource, RESOURCE_META);
171

    
172
                    // get
173
                    if (httpVerb == GET) {
174
                        getSystemMetadataObject(extra);
175
                        status = true;
176
                    }
177
                    // post to register system metadata
178
                    if (httpVerb == POST) {
179
                        registerSystemMetadata(extra);
180
                        status = true;
181
                    }
182

    
183
                } else if (resource.startsWith(RESOURCE_RESERVE)) {
184
                    // reserve the ID (in params)
185
                    if (httpVerb == POST) {
186
                        reserve();
187
                        status = true;
188
                    }
189
                } else if (resource.startsWith(RESOURCE_ASSERT_RELATION)) {
190

    
191
                    // after the command
192
                    extra = parseTrailing(resource, RESOURCE_ASSERT_RELATION);
193

    
194
                    // reserve the ID (in params)
195
                    if (httpVerb == GET) {
196
                        assertRelation(extra);
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(extra, FUNCTION_NAME_INSERT);
249
                        status = true;
250
                    }
251

    
252
                } else if (resource.startsWith(RESOURCE_FORMATS)) {
253
                    logMetacat.debug("Using resource: " + RESOURCE_FORMATS);
254

    
255
                    // after the command
256
                    extra = parseTrailing(resource, RESOURCE_FORMATS);
257

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

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

    
278
                } else if (resource.startsWith(RESOURCE_CHECKSUM)) {
279
                    logMetacat.debug("Using resource: " + RESOURCE_CHECKSUM);
280

    
281
                    // after the command
282
                    extra = parseTrailing(resource, RESOURCE_CHECKSUM);
283

    
284
                    // handle checksum requests
285
                    if (httpVerb == GET) {
286

    
287
                        checksum(extra);
288
                        status = true;
289

    
290
                    }
291

    
292
                } else if (resource.startsWith(RESOURCE_REPLICATION_POLICY)
293
                        && httpVerb == PUT) {
294

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

    
302
                } else if (resource.startsWith(RESOURCE_REPLICATION_META)
303
                        && httpVerb == PUT) {
304

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

    
312
                } else if (resource.startsWith(RESOURCE_REPLICATION_NOTIFY)
313
                        && httpVerb == PUT) {
314

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

    
322
                } else if (resource.startsWith(RESOURCE_REPLICATION_AUTHORIZED)
323
                        && httpVerb == GET) {
324

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

    
333
                }
334

    
335
                if (!status) {
336
                    throw new ServiceFailure("0000", "Unknown error, status = "
337
                            + status);
338
                }
339
            } else {
340
                throw new InvalidRequest("0000", "No resource matched for "
341
                        + resource);
342
            }
343
        } catch (BaseException be) {
344
            // report Exceptions as clearly and generically as possible
345
            OutputStream out = null;
346
            try {
347
                out = response.getOutputStream();
348
            } catch (IOException ioe) {
349
                logMetacat.error("Could not get output stream from response",
350
                        ioe);
351
            }
352
            serializeException(be, out);
353
        } catch (Exception e) {
354
            // report Exceptions as clearly and generically as possible
355
            logMetacat.error(e.getClass() + ": " + e.getMessage(), e);
356
            OutputStream out = null;
357
            try {
358
                out = response.getOutputStream();
359
            } catch (IOException ioe) {
360
                logMetacat.error("Could not get output stream from response",
361
                        ioe);
362
            }
363
            ServiceFailure se = new ServiceFailure("0000", e.getMessage());
364
            serializeException(se, out);
365
        }
366
    }
367

    
368
    /**
369
     * Get the checksum for the given guid
370
     * 
371
     * @param guid
372
     * @throws NotImplemented
373
     * @throws InvalidRequest
374
     * @throws NotFound
375
     * @throws NotAuthorized
376
     * @throws ServiceFailure
377
     * @throws InvalidToken
378
     * @throws IOException
379
     * @throws JiBXException
380
     */
381
    private void checksum(String guid) throws InvalidToken, ServiceFailure,
382
            NotAuthorized, NotFound, InvalidRequest, NotImplemented,
383
            JiBXException, IOException {
384
        Identifier guidid = new Identifier();
385
        guidid.setValue(guid);
386
        logMetacat.debug("getting checksum for object " + guid);
387
        Checksum c = CNodeService.getInstance(request).getChecksum(session,
388
                guidid);
389
        logMetacat.debug("got checksum " + c.getValue());
390
        response.setStatus(200);
391
        logMetacat.debug("serializing response");
392
        TypeMarshaller.marshalTypeToOutputStream(c, response.getOutputStream());
393
        logMetacat.debug("done serializing response.");
394

    
395
    }
396

    
397
    /**
398
     * get the logs based on passed params. Available params are token,
399
     * fromDate, toDate, event. See
400
     * http://mule1.dataone.org/ArchitectureDocs/mn_api_crud
401
     * .html#MN_crud.getLogRecords for more info
402
     * 
403
     * @throws NotImplemented
404
     * @throws InvalidRequest
405
     * @throws NotAuthorized
406
     * @throws ServiceFailure
407
     * @throws InvalidToken
408
     * @throws IOException
409
     * @throws JiBXException
410
     */
411
    private void getLog() throws InvalidToken, ServiceFailure, NotAuthorized,
412
            InvalidRequest, NotImplemented, IOException, JiBXException {
413

    
414
        Date fromDate = null;
415
        Date toDate = null;
416
        Event event = null;
417
        Integer start = null;
418
        Integer count = null;
419

    
420
        try {
421
            String fromDateS = params.get("fromDate")[0];
422
            logMetacat.debug("param fromDateS: " + fromDateS);
423
            fromDate = DateTimeMarshaller.deserializeDateToUTC(fromDateS);
424
        } catch (Exception e) {
425
            logMetacat.warn("Could not parse fromDate: " + e.getMessage());
426
        }
427
        try {
428
            String toDateS = params.get("toDate")[0];
429
            logMetacat.debug("param toDateS: " + toDateS);
430
            toDate = DateTimeMarshaller.deserializeDateToUTC(toDateS);
431
        } catch (Exception e) {
432
            logMetacat.warn("Could not parse toDate: " + e.getMessage());
433
        }
434
        try {
435
            String eventS = params.get("event")[0];
436
            event = Event.convert(eventS);
437
        } catch (Exception e) {
438
            logMetacat.warn("Could not parse event: " + e.getMessage());
439
        }
440
        logMetacat.debug("fromDate: " + fromDate + " toDate: " + toDate);
441

    
442
        try {
443
            start = Integer.parseInt(params.get("start")[0]);
444
        } catch (Exception e) {
445
            logMetacat.warn("Could not parse start: " + e.getMessage());
446
        }
447
        try {
448
            count = Integer.parseInt(params.get("count")[0]);
449
        } catch (Exception e) {
450
            logMetacat.warn("Could not parse count: " + e.getMessage());
451
        }
452

    
453
        logMetacat.debug("calling getLogRecords");
454
        Log log = CNodeService.getInstance(request).getLogRecords(session,
455
                fromDate, toDate, event, start, count);
456

    
457
        OutputStream out = response.getOutputStream();
458
        response.setStatus(200);
459
        response.setContentType("text/xml");
460

    
461
        TypeMarshaller.marshalTypeToOutputStream(log, out);
462

    
463
    }
464

    
465
    /**
466
     * Implements REST version of DataONE CRUD API --> get
467
     * 
468
     * @param guid
469
     *            ID of data object to be read
470
     * @throws NotImplemented
471
     * @throws InvalidRequest
472
     * @throws NotFound
473
     * @throws NotAuthorized
474
     * @throws ServiceFailure
475
     * @throws InvalidToken
476
     * @throws IOException
477
     */
478
    protected void getObject(String guid) throws InvalidToken, ServiceFailure,
479
            NotAuthorized, NotFound, InvalidRequest, NotImplemented,
480
            IOException {
481

    
482
        Identifier id = new Identifier();
483
        id.setValue(guid);
484

    
485
        SystemMetadata sm = CNodeService.getInstance(request)
486
                .getSystemMetadata(session, id);
487

    
488
        // set the content type
489
        if (sm.getFormatId()
490
                .getValue()
491
                .trim()
492
                .equals(ObjectFormatCache.getInstance().getFormat("text/csv")
493
                        .getFormatId().getValue())) {
494
            response.setContentType("text/csv");
495
            response.setHeader("Content-Disposition",
496
                    "inline; filename=" + id.getValue() + ".csv");
497
        } else if (sm
498
                .getFormatId()
499
                .getValue()
500
                .trim()
501
                .equals(ObjectFormatCache.getInstance().getFormat("text/plain")
502
                        .getFormatId().getValue())) {
503
            response.setContentType("text/plain");
504
            response.setHeader("Content-Disposition",
505
                    "inline; filename=" + id.getValue() + ".txt");
506
        } else if (sm
507
                .getFormatId()
508
                .getValue()
509
                .trim()
510
                .equals(ObjectFormatCache.getInstance()
511
                        .getFormat("application/octet-stream").getFormatId()
512
                        .getValue())) {
513
            response.setContentType("application/octet-stream");
514
        } else {
515
            response.setContentType("text/xml");
516
            response.setHeader("Content-Disposition",
517
                    "inline; filename=" + id.getValue() + ".xml");
518
        }
519

    
520
        InputStream data = CNodeService.getInstance(request).get(session, id);
521

    
522
        OutputStream out = response.getOutputStream();
523
        response.setStatus(200);
524
        IOUtils.copyLarge(data, out);
525

    
526
    }
527

    
528
    /**
529
     * Implements REST version of DataONE CRUD API --> getSystemMetadata
530
     * 
531
     * @param guid
532
     *            ID of data object to be read
533
     * @throws NotImplemented
534
     * @throws InvalidRequest
535
     * @throws NotFound
536
     * @throws NotAuthorized
537
     * @throws ServiceFailure
538
     * @throws InvalidToken
539
     * @throws IOException
540
     * @throws JiBXException
541
     */
542
    protected void getSystemMetadataObject(String guid) throws InvalidToken,
543
            ServiceFailure, NotAuthorized, NotFound, InvalidRequest,
544
            NotImplemented, IOException, JiBXException {
545

    
546
        Identifier id = new Identifier();
547
        id.setValue(guid);
548
        SystemMetadata sysmeta = CNodeService.getInstance(request)
549
                .getSystemMetadata(session, id);
550

    
551
        response.setContentType("text/xml");
552
        response.setStatus(200);
553
        OutputStream out = response.getOutputStream();
554

    
555
        // Serialize and write it to the output stream
556
        TypeMarshaller.marshalTypeToOutputStream(sysmeta, out);
557
    }
558

    
559
    /**
560
     * Earthgrid API > Put Service >Put Function : calls MetacatHandler >
561
     * handleInsertOrUpdateAction
562
     * 
563
     * @param guid
564
     *            - ID of data object to be inserted or updated. If action is
565
     *            update, the pid is the existing pid. If insert, the pid is the
566
     *            new one
567
     * @throws InvalidRequest
568
     * @throws ServiceFailure
569
     * @throws IdentifierNotUnique
570
     * @throws JiBXException
571
     * @throws NotImplemented
572
     * @throws InvalidSystemMetadata
573
     * @throws InsufficientResources
574
     * @throws UnsupportedType
575
     * @throws NotAuthorized
576
     * @throws InvalidToken
577
     * @throws IOException
578
     * @throws IllegalAccessException
579
     * @throws InstantiationException
580
     */
581
    protected void putObject(String pid, String action) throws ServiceFailure,
582
            InvalidRequest, IdentifierNotUnique, JiBXException, InvalidToken,
583
            NotAuthorized, UnsupportedType, InsufficientResources,
584
            InvalidSystemMetadata, NotImplemented, IOException,
585
            InstantiationException, IllegalAccessException {
586
        logMetacat.debug("Entering putObject: " + pid + "/" + action);
587

    
588
        // Read the incoming data from its Mime Multipart encoding
589
        Map<String, File> files = collectMultipartFiles();
590
        InputStream object = null;
591
        InputStream sysmeta = null;
592

    
593
        File smFile = files.get("sysmeta");
594
        sysmeta = new FileInputStream(smFile);
595
        File objFile = files.get("object");
596
        object = new FileInputStream(objFile);
597

    
598
        if (action.equals(FUNCTION_NAME_INSERT)) { // handle inserts
599

    
600
            logMetacat.debug("Commence creation...");
601
            SystemMetadata smd = TypeMarshaller.unmarshalTypeFromStream(
602
                    SystemMetadata.class, sysmeta);
603

    
604
            Identifier id = new Identifier();
605
            id.setValue(pid);
606
            logMetacat.debug("creating object with pid " + id.getValue());
607
            Identifier rId = CNodeService.getInstance(request).create(session,
608
                    id, object, smd);
609

    
610
            OutputStream out = response.getOutputStream();
611
            response.setStatus(200);
612
            response.setContentType("text/xml");
613

    
614
            TypeMarshaller.marshalTypeToOutputStream(rId, out);
615

    
616
        } else {
617
            throw new InvalidRequest("1000", "Operation must be create.");
618
        }
619
    }
620

    
621
    /**
622
     * List the object formats registered with the system
623
     * 
624
     * @throws NotImplemented
625
     * @throws InsufficientResources
626
     * @throws NotFound
627
     * @throws ServiceFailure
628
     * @throws InvalidRequest
629
     * @throws IOException
630
     * @throws JiBXException
631
     */
632
    private void listFormats() throws InvalidRequest, ServiceFailure, NotFound,
633
            InsufficientResources, NotImplemented, IOException, JiBXException {
634
        logMetacat.debug("Entering listFormats()");
635

    
636
        ObjectFormatList objectFormatList = CNodeService.getInstance(request)
637
                .listFormats();
638
        // get the response output stream
639
        OutputStream out = response.getOutputStream();
640
        response.setStatus(200);
641
        response.setContentType("text/xml");
642

    
643
        // style the object with a processing directive
644
        String stylesheet = null;
645
        try {
646
            stylesheet = PropertyService.getProperty("dataone.types.xsl");
647
        } catch (PropertyNotFoundException e) {
648
            logMetacat.warn("Could not locate DataONE types XSLT: "
649
                    + e.getMessage());
650
        }
651

    
652
        TypeMarshaller.marshalTypeToOutputStream(objectFormatList, out,
653
                stylesheet);
654

    
655
    }
656

    
657
    /**
658
     * Return the requested object format
659
     * 
660
     * @param fmtidStr
661
     *            the requested format identifier as a string
662
     * @throws NotImplemented
663
     * @throws InsufficientResources
664
     * @throws NotFound
665
     * @throws ServiceFailure
666
     * @throws InvalidRequest
667
     * @throws IOException
668
     * @throws JiBXException
669
     */
670
    private void getFormat(String fmtidStr) throws InvalidRequest,
671
            ServiceFailure, NotFound, InsufficientResources, NotImplemented,
672
            IOException, JiBXException {
673
        logMetacat.debug("Entering listFormats()");
674

    
675
        ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
676
        fmtid.setValue(fmtidStr);
677

    
678
        // get the specified object format
679
        ObjectFormat objectFormat = CNodeService.getInstance(request)
680
                .getFormat(fmtid);
681

    
682
        OutputStream out = response.getOutputStream();
683
        response.setStatus(200);
684
        response.setContentType("text/xml");
685

    
686
        TypeMarshaller.marshalTypeToOutputStream(objectFormat, out);
687

    
688
    }
689

    
690
    /**
691
     * Reserve the given Identifier
692
     * 
693
     * @throws InvalidToken
694
     * @throws ServiceFailure
695
     * @throws NotAuthorized
696
     * @throws IdentifierNotUnique
697
     * @throws NotImplemented
698
     * @throws InvalidRequest
699
     * @throws IOException
700
     * @throws JiBXException
701
     */
702
    private void reserve() throws InvalidToken, ServiceFailure, NotAuthorized,
703
            IdentifierNotUnique, NotImplemented, InvalidRequest, IOException,
704
            JiBXException {
705
        Identifier pid = null;
706
        String scope = null;
707
        String format = null;
708
        // gather the params
709
        try {
710
            String id = params.get("pid")[0];
711
            pid = new Identifier();
712
            pid.setValue(id);
713
        } catch (Exception e) {
714
            logMetacat.warn("pid not specified");
715
        }
716
        try {
717
            scope = params.get("scope")[0];
718
        } catch (Exception e) {
719
            logMetacat.warn("pid not specified");
720
        }
721
        try {
722
            format = params.get("format")[0];
723
        } catch (Exception e) {
724
            logMetacat.warn("pid not specified");
725
        }
726
        // call the implementation
727
        Identifier resultPid = CNodeService.getInstance(request)
728
                .reserveIdentifier(session, pid);
729
        OutputStream out = response.getOutputStream();
730
        response.setStatus(200);
731
        response.setContentType("text/xml");
732
        // send back the reserved pid
733
        TypeMarshaller.marshalTypeToOutputStream(resultPid, out);
734
    }
735

    
736
    /**
737
     * 
738
     * @param id
739
     * @throws InvalidRequest
740
     * @throws InvalidToken
741
     * @throws ServiceFailure
742
     * @throws NotAuthorized
743
     * @throws NotFound
744
     * @throws NotImplemented
745
     * @throws IOException
746
     * @throws JiBXException
747
     */
748
    private void resolve(String id) throws InvalidRequest, InvalidToken,
749
            ServiceFailure, NotAuthorized, NotFound, NotImplemented,
750
            IOException, JiBXException {
751
        Identifier pid = new Identifier();
752
        pid.setValue(id);
753
        ObjectLocationList locationList = CNodeService.getInstance(request)
754
                .resolve(session, pid);
755
        OutputStream out = response.getOutputStream();
756
        response.setStatus(200);
757
        response.setContentType("text/xml");
758
        TypeMarshaller.marshalTypeToOutputStream(locationList, out);
759

    
760
    }
761

    
762
    /**
763
     * Assert that a relationship exists between two resources
764
     * 
765
     * @param id
766
     * @return
767
     * @throws InvalidToken
768
     * @throws ServiceFailure
769
     * @throws NotAuthorized
770
     * @throws NotFound
771
     * @throws InvalidRequest
772
     * @throws NotImplemented
773
     */
774
    private boolean assertRelation(String id) throws InvalidToken,
775
            ServiceFailure, NotAuthorized, NotFound, InvalidRequest,
776
            NotImplemented {
777
        Identifier pidOfSubject = new Identifier();
778
        pidOfSubject.setValue(id);
779
        String relationship = null;
780
        try {
781
            relationship = params.get("relationship")[0];
782
        } catch (Exception e) {
783
            logMetacat.warn("relationship not specified");
784
        }
785
        Identifier pidOfObject = new Identifier();
786
        try {
787
            String objPid = params.get("pidOfObject")[0];
788
            pidOfObject.setValue(objPid);
789
        } catch (Exception e) {
790
            logMetacat.warn("pidOfObject not specified");
791
        }
792
        boolean result = CNodeService.getInstance(request).assertRelation(
793
                session, pidOfSubject, relationship, pidOfObject);
794
        response.setStatus(200);
795
        response.setContentType("text/xml");
796
        return result;
797
    }
798

    
799
    /**
800
     * Set the owner of a resource
801
     * 
802
     * @param id
803
     * @throws JiBXException
804
     * @throws InvalidToken
805
     * @throws ServiceFailure
806
     * @throws NotFound
807
     * @throws NotAuthorized
808
     * @throws NotImplemented
809
     * @throws InvalidRequest
810
     * @throws IOException
811
     * @throws IllegalAccessException
812
     * @throws InstantiationException
813
     */
814
    private void owner(String id) throws JiBXException, InvalidToken,
815
            ServiceFailure, NotFound, NotAuthorized, NotImplemented,
816
            InvalidRequest, IOException, InstantiationException,
817
            IllegalAccessException {
818

    
819
        Identifier pid = new Identifier();
820
        pid.setValue(id);
821

    
822
        long serialVersion = 0L;
823
        String serialVersionStr = null;
824

    
825
        // get the serialVersion
826
        try {
827
            serialVersionStr = params.get("serialVersion")[0];
828
            serialVersion = new Long(serialVersionStr).longValue();
829

    
830
        } catch (NullPointerException e) {
831
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
832
            logMetacat.error(msg);
833
            throw new InvalidRequest("4442", msg);
834

    
835
        }
836

    
837
        // get the subject
838
        String subjectStr = params.get("subject")[0];
839
        Subject subject = TypeMarshaller.unmarshalTypeFromStream(Subject.class,
840
                new ByteArrayInputStream(subjectStr.getBytes("UTF-8")));
841

    
842
        Identifier retPid = CNodeService.getInstance(request).setOwner(session,
843
                pid, subject, serialVersion);
844
        OutputStream out = response.getOutputStream();
845
        response.setStatus(200);
846
        response.setContentType("text/xml");
847
        TypeMarshaller.marshalTypeToOutputStream(retPid, out);
848
    }
849

    
850
    /**
851
     * Processes the authorization check for given id
852
     * 
853
     * @param id
854
     * @return
855
     * @throws ServiceFailure
856
     * @throws InvalidToken
857
     * @throws NotFound
858
     * @throws NotAuthorized
859
     * @throws NotImplemented
860
     * @throws InvalidRequest
861
     */
862
    private boolean isAuthorized(String id) throws ServiceFailure,
863
            InvalidToken, NotFound, NotAuthorized, NotImplemented,
864
            InvalidRequest {
865
        Identifier pid = new Identifier();
866
        pid.setValue(id);
867
        String permission = params.get("action")[0];
868
        boolean result = CNodeService.getInstance(request).isAuthorized(
869
                session, pid, Permission.convert(permission));
870
        response.setStatus(200);
871
        response.setContentType("text/xml");
872
        return result;
873
    }
874

    
875
    /**
876
     * Register System Metadata without data or metadata object
877
     * 
878
     * @param pid
879
     *            identifier for System Metadata entry
880
     * @throws JiBXException
881
     * @throws FileUploadException
882
     * @throws IOException
883
     * @throws InvalidRequest
884
     * @throws ServiceFailure
885
     * @throws InvalidSystemMetadata
886
     * @throws NotAuthorized
887
     * @throws NotImplemented
888
     * @throws IllegalAccessException
889
     * @throws InstantiationException
890
     */
891
    protected Identifier registerSystemMetadata(String pid)
892
            throws ServiceFailure, InvalidRequest, IOException,
893
            FileUploadException, JiBXException, NotImplemented, NotAuthorized,
894
            InvalidSystemMetadata, InstantiationException,
895
            IllegalAccessException {
896
        logMetacat.debug("Entering registerSystemMetadata: " + pid);
897

    
898
        // get the system metadata from the request
899
        SystemMetadata systemMetadata = collectSystemMetadata();
900

    
901
        Identifier guid = new Identifier();
902
        guid.setValue(pid);
903
        logMetacat.debug("registering system metadata with pid "
904
                + guid.getValue());
905
        Identifier retGuid = CNodeService.getInstance(request)
906
                .registerSystemMetadata(session, guid, systemMetadata);
907

    
908
        response.setStatus(200);
909
        response.setContentType("text/xml");
910
        return retGuid;
911

    
912
    }
913

    
914
    /**
915
     * set the access perms on a document
916
     * 
917
     * @throws JiBXException
918
     * @throws InvalidRequest
919
     * @throws NotImplemented
920
     * @throws NotAuthorized
921
     * @throws NotFound
922
     * @throws ServiceFailure
923
     * @throws InvalidToken
924
     * @throws IllegalAccessException
925
     * @throws InstantiationException
926
     * @throws IOException
927
     * @throws SAXException
928
     * @throws ParserConfigurationException
929
     */
930
    protected void setAccess(String pid) throws JiBXException, InvalidToken,
931
            ServiceFailure, NotFound, NotAuthorized, NotImplemented,
932
            InvalidRequest, IOException, InstantiationException,
933
            IllegalAccessException, ParserConfigurationException, SAXException {
934

    
935
        long serialVersion = 0L;
936
        String serialVersionStr = null;
937

    
938
        // get the serialVersion
939
        try {
940
            serialVersionStr = params.get("serialVersion")[0];
941
            serialVersion = new Long(serialVersionStr).longValue();
942

    
943
        } catch (NullPointerException e) {
944
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
945
            logMetacat.error(msg);
946
            throw new InvalidRequest("4402", msg);
947

    
948
        }
949

    
950
        Identifier id = new Identifier();
951
        id.setValue(pid);
952

    
953
        AccessPolicy accessPolicy = collectAccessPolicy();
954
        CNodeService.getInstance(request).setAccessPolicy(session, id,
955
                accessPolicy, serialVersion);
956

    
957
    }
958

    
959
    /**
960
     * List the objects
961
     * 
962
     * @throws NotImplemented
963
     * @throws InvalidRequest
964
     * @throws NotAuthorized
965
     * @throws ServiceFailure
966
     * @throws InvalidToken
967
     * @throws NotFound
968
     * @throws IOException
969
     * @throws JiBXException
970
     * @throws Exception
971
     */
972
    private void listObjects() throws InvalidToken, ServiceFailure,
973
            NotAuthorized, InvalidRequest, NotImplemented, NotFound,
974
            IOException, JiBXException {
975

    
976
        Date startTime = null;
977
        Date endTime = null;
978
        ObjectFormat objectFormat = null;
979
        boolean replicaStatus = false;
980
        int start = 0;
981
        int count = -1;
982
        Enumeration<String> paramlist = request.getParameterNames();
983
        while (paramlist.hasMoreElements()) {
984
            // parse the params and make the call
985
            String name = paramlist.nextElement();
986
            String[] value = request.getParameterValues(name);
987

    
988
            if (name.equals("startTime") && value != null) {
989
                try {
990
                    startTime = DateTimeMarshaller
991
                            .deserializeDateToUTC(value[0]);
992
                } catch (Exception e) {
993
                    // if we can't parse it, just don't use the startTime param
994
                    logMetacat.warn("Could not parse startTime: " + value[0]);
995
                    startTime = null;
996
                }
997
            } else if (name.equals("endTime") && value != null) {
998
                try {
999
                    endTime = DateTimeMarshaller.deserializeDateToUTC(value[0]);
1000
                } catch (Exception e) {
1001
                    // if we can't parse it, just don't use the endTime param
1002
                    logMetacat.warn("Could not parse endTime: " + value[0]);
1003
                    endTime = null;
1004
                }
1005
            } else if (name.equals("objectFormat") && value != null) {
1006
                objectFormat = ObjectFormatCache.getInstance().getFormat(
1007
                        value[0]);
1008
            } else if (name.equals("replicaStatus") && value != null) {
1009
                replicaStatus = Boolean.parseBoolean(value[0]);
1010
            } else if (name.equals("start") && value != null) {
1011
                start = Integer.valueOf(value[0]);
1012
            } else if (name.equals("count") && value != null) {
1013
                count = Integer.valueOf(value[0]);
1014
            }
1015
        }
1016
        // make the call
1017
        logMetacat.debug("session: " + session + " startTime: " + startTime
1018
                + " endtime: " + endTime + " objectFormat: " + objectFormat
1019
                + " replicaStatus: " + replicaStatus + " start: " + start
1020
                + " count: " + count);
1021

    
1022
        ObjectFormatIdentifier fmtid = null;
1023
        if (objectFormat != null) {
1024
            fmtid = objectFormat.getFormatId();
1025
        }
1026

    
1027
        // get the list
1028
        ObjectList ol = CNodeService.getInstance(request).listObjects(session,
1029
                startTime, endTime, fmtid, replicaStatus, start, count);
1030

    
1031
        // send it
1032
        OutputStream out = response.getOutputStream();
1033
        response.setStatus(200);
1034
        response.setContentType("text/xml");
1035

    
1036
        // style the object with a processing directive
1037
        String stylesheet = null;
1038
        try {
1039
            stylesheet = PropertyService.getProperty("dataone.types.xsl");
1040
        } catch (PropertyNotFoundException e) {
1041
            logMetacat.warn("Could not locate DataONE types XSLT: "
1042
                    + e.getMessage());
1043
        }
1044

    
1045
        // Serialize and write it to the output stream
1046
        TypeMarshaller.marshalTypeToOutputStream(ol, out, stylesheet);
1047
    }
1048

    
1049
    /**
1050
     * Pass the request to get node replication authorization to CNodeService
1051
     * 
1052
     * @param pid
1053
     *            the identifier of the object to get authorization to replicate
1054
     * 
1055
     * @throws NotImplemented
1056
     * @throws NotAuthorized
1057
     * @throws InvalidToken
1058
     * @throws ServiceFailure
1059
     * @throws NotFound
1060
     * @throws InvalidRequest
1061
     */
1062
    public boolean isNodeAuthorized(String pid) throws NotImplemented,
1063
            NotAuthorized, InvalidToken, ServiceFailure, NotFound,
1064
            InvalidRequest {
1065

    
1066
        boolean result = false;
1067
        Subject targetNodeSubject = new Subject();
1068
        String nodeSubject = null;
1069
        String replPermission = null;
1070

    
1071
        // get the pid
1072
        Identifier identifier = new Identifier();
1073
        identifier.setValue(pid);
1074

    
1075
        // get the target node subject
1076
        try {
1077
            nodeSubject = params.get("targetNodeSubject")[0];
1078
            targetNodeSubject.setValue(nodeSubject);
1079

    
1080
        } catch (NullPointerException e) {
1081
            String msg = "The 'targetNodeSubject' must be provided as a parameter and was not.";
1082
            logMetacat.error(msg);
1083
            throw new InvalidRequest("4873", msg);
1084

    
1085
        }
1086

    
1087
        result = CNodeService.getInstance(request).isNodeAuthorized(session, targetNodeSubject, identifier);
1088

    
1089
        response.setStatus(200);
1090
        response.setContentType("text/xml");
1091
        return result;
1092

    
1093
    }
1094

    
1095
    /**
1096
     * Pass the request to set the replication policy to CNodeService
1097
     * 
1098
     * @param pid
1099
     *            the identifier of the object to set the replication policy on
1100
     * 
1101
     * @throws NotImplemented
1102
     * @throws NotFound
1103
     * @throws NotAuthorized
1104
     * @throws ServiceFailure
1105
     * @throws InvalidRequest
1106
     * @throws InvalidToken
1107
     * @throws IOException
1108
     * @throws InstantiationException
1109
     * @throws IllegalAccessException
1110
     * @throws JiBXException
1111
     */
1112
    public boolean setReplicationPolicy(String pid) throws NotImplemented,
1113
            NotFound, NotAuthorized, ServiceFailure, InvalidRequest,
1114
            InvalidToken, IOException, InstantiationException,
1115
            IllegalAccessException, JiBXException {
1116

    
1117
        boolean result = false;
1118
        ReplicationPolicy policy = null;
1119
        long serialVersion = 0L;
1120
        String serialVersionStr = null;
1121

    
1122
        Identifier identifier = new Identifier();
1123
        identifier.setValue(pid);
1124

    
1125
        policy = collectReplicationPolicy();
1126

    
1127
        // get the serialVersion
1128
        try {
1129
            serialVersionStr = multipartparams.get("serialVersion").get(0);
1130
            serialVersion = new Long(serialVersionStr).longValue();
1131

    
1132
        } catch (NullPointerException e) {
1133
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
1134
            logMetacat.error(msg);
1135
            throw new InvalidRequest("4883", msg);
1136

    
1137
        }
1138
        result = CNodeService.getInstance(request).setReplicationPolicy(
1139
                session, identifier, policy, serialVersion);
1140
        response.setStatus(200);
1141
        response.setContentType("text/xml");
1142
        return result;
1143

    
1144
    }
1145

    
1146
    /**
1147
     * Pass the request to set the replication status to CNodeService
1148
     * 
1149
     * @param pid
1150
     *            the identifier of the object to set the replication status on
1151
     * 
1152
     * @throws ServiceFailure
1153
     * @throws NotImplemented
1154
     * @throws InvalidToken
1155
     * @throws NotAuthorized
1156
     * @throws InvalidRequest
1157
     * @throws NotFound
1158
     * @throws JiBXException 
1159
     * @throws IllegalAccessException 
1160
     * @throws InstantiationException 
1161
     * @throws IOException 
1162
     */
1163
    public boolean setReplicationStatus(String pid) throws ServiceFailure,
1164
            NotImplemented, InvalidToken, NotAuthorized, InvalidRequest,
1165
            NotFound {
1166
        
1167
        boolean result = false;
1168
        Identifier identifier = new Identifier();
1169
        identifier.setValue(pid);
1170
        BaseException failure = null;
1171
        ReplicationStatus status = null;
1172
        String replicationStatus = null;
1173
        NodeReference targetNodeRef = null;
1174
        String targetNode = null;
1175

    
1176
        // Parse the params out of the multipart form data
1177
        // Read the incoming data from its Mime Multipart encoding
1178
        logMetacat.debug("Parsing ReplicaStatus from the mime multipart entity");
1179

    
1180
        try {
1181
            failure = collectReplicationStatus();
1182
            
1183
        } catch (IOException e2) {
1184
            throw new ServiceFailure("4700", "Couldn't resolve the multipart request: " +
1185
                e2.getMessage());
1186
            
1187
        } catch (InstantiationException e2) {
1188
            throw new ServiceFailure("4700", "Couldn't resolve the multipart request: " +
1189
                e2.getMessage());
1190
            
1191
        } catch (IllegalAccessException e2) {
1192
            throw new ServiceFailure("4700", "Couldn't resolve the multipart request: " +
1193
                    e2.getMessage());
1194
            
1195
        } catch (JiBXException e2) {
1196
            throw new ServiceFailure("4700", "Couldn't resolve the multipart request: " +
1197
                    e2.getMessage());
1198
            
1199
        }
1200
        
1201
        // get the replication status param
1202
        try {
1203
            replicationStatus = multipartparams.get("status").get(0);
1204
            status = ReplicationStatus.convert(replicationStatus);
1205

    
1206
        } catch (NullPointerException npe) {
1207

    
1208
            logMetacat.debug("The 'status' parameter was not found in the "
1209
                    + "multipartparams map.  Trying the params map.");
1210

    
1211
            try {
1212
                replicationStatus = params.get("status")[0];
1213
                status = ReplicationStatus.convert(replicationStatus
1214
                        .toLowerCase());
1215

    
1216
            } catch (Exception e) {
1217
                String msg = "The 'status' must be provided as a parameter and was not.";
1218
                logMetacat.error(msg);
1219
                throw new InvalidRequest("4730", msg);
1220

    
1221
            }
1222

    
1223
        }
1224

    
1225
        // get the target node reference param
1226
        try {
1227
            targetNode = multipartparams.get("nodeRef").get(0);
1228
            targetNodeRef = new NodeReference();
1229
            targetNodeRef.setValue(targetNode);
1230

    
1231
        } catch (NullPointerException e) {
1232
            logMetacat.debug("The 'nodeRef' parameter was not found in the "
1233
                    + "multipartparams map.  Trying the params map.");
1234

    
1235
            try {
1236
                targetNode = params.get("nodeRef")[0];
1237
                targetNodeRef = new NodeReference();
1238
                targetNodeRef.setValue(targetNode);
1239

    
1240
            } catch (Exception e1) {
1241
                String msg = "The 'nodeRef' must be provided as a parameter and was not.";
1242
                logMetacat.error(msg);
1243
                throw new InvalidRequest("4730", msg);
1244

    
1245
            }
1246

    
1247
        }
1248

    
1249
        result = CNodeService.getInstance(request).setReplicationStatus(
1250
                session, identifier, targetNodeRef, status, failure);
1251
        response.setStatus(200);
1252
        response.setContentType("text/xml");
1253
        return result;
1254

    
1255
    }
1256

    
1257
    /**
1258
     * Pass the request to update the replication metadata to CNodeService
1259
     * 
1260
     * @param pid
1261
     *            the identifier of the object to update the replication
1262
     *            metadata on
1263
     * 
1264
     * @throws ServiceFailure
1265
     * @throws NotImplemented
1266
     * @throws InvalidToken
1267
     * @throws NotAuthorized
1268
     * @throws InvalidRequest
1269
     * @throws NotFound
1270
     */
1271
    public boolean updateReplicationMetadata(String pid) throws ServiceFailure,
1272
            NotImplemented, InvalidToken, NotAuthorized, InvalidRequest,
1273
            NotFound {
1274

    
1275
        boolean result = false;
1276
        long serialVersion = 0L;
1277
        String serialVersionStr = null;
1278
        Replica replica = null;
1279
        Identifier identifier = new Identifier();
1280
        identifier.setValue(pid);
1281

    
1282
        replica = collectReplicaMetadata();
1283

    
1284
        // get the serialVersion
1285
        try {
1286
            serialVersionStr = multipartparams.get("serialVersion").get(0);
1287
            serialVersion = new Long(serialVersionStr).longValue();
1288

    
1289
        } catch (NullPointerException e) {
1290
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
1291
            logMetacat.error(msg);
1292
            throw new InvalidRequest("4853", msg);
1293

    
1294
        }
1295

    
1296
        result = CNodeService.getInstance(request).updateReplicationMetadata(
1297
                session, identifier, replica, serialVersion);
1298
        response.setStatus(200);
1299
        response.setContentType("text/xml");
1300
        return result;
1301

    
1302
    }
1303

    
1304
}
(1-1/9)