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-07-17 11:46:58 -0700 (Tue, 17 Jul 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.File;
26
import java.io.FileInputStream;
27
import java.io.IOException;
28
import java.io.InputStream;
29
import java.io.OutputStream;
30
import java.util.Date;
31
import java.util.Enumeration;
32
import java.util.Map;
33

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

    
39
import org.apache.commons.fileupload.FileUploadException;
40
import org.apache.commons.io.IOUtils;
41
import org.apache.log4j.Logger;
42
import org.dataone.client.ObjectFormatCache;
43
import org.dataone.service.exceptions.BaseException;
44
import org.dataone.service.exceptions.IdentifierNotUnique;
45
import org.dataone.service.exceptions.InsufficientResources;
46
import org.dataone.service.exceptions.InvalidRequest;
47
import org.dataone.service.exceptions.InvalidSystemMetadata;
48
import org.dataone.service.exceptions.InvalidToken;
49
import org.dataone.service.exceptions.NotAuthorized;
50
import org.dataone.service.exceptions.NotFound;
51
import org.dataone.service.exceptions.NotImplemented;
52
import org.dataone.service.exceptions.ServiceFailure;
53
import org.dataone.service.exceptions.UnsupportedType;
54
import org.dataone.service.exceptions.VersionMismatch;
55
import org.dataone.service.types.v1.AccessPolicy;
56
import org.dataone.service.types.v1.Checksum;
57
import org.dataone.service.types.v1.ChecksumAlgorithmList;
58
import org.dataone.service.types.v1.DescribeResponse;
59
import org.dataone.service.types.v1.Event;
60
import org.dataone.service.types.v1.Identifier;
61
import org.dataone.service.types.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.Constants;
75
import org.dataone.service.util.DateTimeMarshaller;
76
import org.dataone.service.util.EncodingUtilities;
77
import org.dataone.service.util.TypeMarshaller;
78
import org.jibx.runtime.JiBXException;
79
import org.xml.sax.SAXException;
80

    
81
import edu.ucsb.nceas.metacat.dataone.CNodeService;
82
import edu.ucsb.nceas.metacat.properties.PropertyService;
83
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
84

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

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

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

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

    
143
        try {
144

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

    
156
            // for the rest of the resouce
157
            String extra = null;
158

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

    
163
            if (resource != null) {
164

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

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

    
177
                    // after the command
178
                    extra = parseTrailing(resource, RESOURCE_META);
179

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

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

    
199
                    // after the command
200
                    extra = parseTrailing(resource, RESOURCE_RESOLVE);
201

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

    
209
                    // after the command
210
                    extra = parseTrailing(resource, RESOURCE_OWNER);
211

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

    
219
                    // after the command
220
                    extra = parseTrailing(resource, RESOURCE_IS_AUTHORIZED);
221

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

    
232
                    // after the command
233
                    extra = parseTrailing(resource, RESOURCE_OBJECTS);
234

    
235
                    logMetacat.debug("objectId: " + extra);
236
                    logMetacat.debug("verb:" + httpVerb);
237

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

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

    
259
                    // after the command
260
                    extra = parseTrailing(resource, RESOURCE_FORMATS);
261

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

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

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

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

    
296
                    // handle checksum requests
297
                    if (httpVerb == GET) {
298

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

    
307
                    }
308

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

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

    
319
                } else if (resource.startsWith(RESOURCE_REPLICATION_META)
320
                        && httpVerb == PUT) {
321

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

    
329
                } else if (resource.startsWith(RESOURCE_REPLICATION_NOTIFY)
330
                        && httpVerb == PUT) {
331

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

    
339
                } else if (resource.startsWith(RESOURCE_REPLICATION_AUTHORIZED)
340
                        && httpVerb == GET) {
341

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

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

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

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

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

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

    
440
    }
441

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

    
459
        Date fromDate = null;
460
        Date toDate = null;
461
        Event event = null;
462
        Integer start = null;
463
        Integer count = null;
464
        String pidFilter = null;
465

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

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

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

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

    
513
        TypeMarshaller.marshalTypeToOutputStream(log, out);
514

    
515
    }
516

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

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

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

    
540
        // set the content type, default to octet-stream
541
        response.setContentType("application/octet-stream");
542
        response.setHeader("Content-Disposition",
543
                "inline; filename=" + id.getValue());
544
        // TODO: use objectFormat MIME type when we add that information
545
        if (sm.getFormatId().getValue().startsWith("text")) {
546
            response.setContentType(sm.getFormatId().getValue());
547
        } else if (sm.getFormatId().getValue().startsWith("image")) {
548
        	response.setContentType(sm.getFormatId().getValue());
549
        } else if (sm.getFormatId().getValue().startsWith("application")) {
550
        	response.setContentType(sm.getFormatId().getValue());
551
        } else if (ObjectFormatCache.getInstance().getFormat(sm.getFormatId()).getFormatType().equals("METADATA")) {
552
        	response.setContentType("text/xml");
553
        }
554

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

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

    
561
    }
562

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

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

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

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

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

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

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

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

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

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

    
654
            TypeMarshaller.marshalTypeToOutputStream(rId, out);
655

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

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

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

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

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

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

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

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

    
708
		TypeMarshaller.marshalTypeToOutputStream(result, out);
709

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

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

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

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

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

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

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

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

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

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

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

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

    
835
        TypeMarshaller.marshalTypeToOutputStream(objectFormat, out);
836

    
837
    }
838

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

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

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

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

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

    
942
    }
943

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

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

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

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

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

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

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

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

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

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

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

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

    
1122
    }
1123

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

    
1146
        long serialVersion = 0L;
1147
        String serialVersionStr = null;
1148
        AccessPolicy accessPolicy = collectAccessPolicy();
1149

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

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

    
1165
        }
1166

    
1167
        Identifier id = new Identifier();
1168
        id.setValue(pid);
1169

    
1170
        CNodeService.getInstance(request).setAccessPolicy(session, id,
1171
                accessPolicy, serialVersion);
1172

    
1173
    }
1174

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

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

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

    
1242
        // get the list
1243
        ObjectList ol = CNodeService.getInstance(request).listObjects(session,
1244
                startTime, endTime, formatId, replicaStatus, start, count);
1245

    
1246
        // send it
1247
        OutputStream out = response.getOutputStream();
1248
        response.setStatus(200);
1249
        response.setContentType("text/xml");
1250

    
1251
        // style the object with a processing directive
1252
        String stylesheet = null;
1253
        try {
1254
            stylesheet = PropertyService.getProperty("dataone.types.xsl");
1255
        } catch (PropertyNotFoundException e) {
1256
            logMetacat.warn("Could not locate DataONE types XSLT: "
1257
                    + e.getMessage());
1258
        }
1259

    
1260
        // Serialize and write it to the output stream
1261
        TypeMarshaller.marshalTypeToOutputStream(ol, out, stylesheet);
1262
    }
1263

    
1264
    /**
1265
     * Pass the request to get node replication authorization to CNodeService
1266
     * 
1267
     * @param pid
1268
     *            the identifier of the object to get authorization to replicate
1269
     * 
1270
     * @throws NotImplemented
1271
     * @throws NotAuthorized
1272
     * @throws InvalidToken
1273
     * @throws ServiceFailure
1274
     * @throws NotFound
1275
     * @throws InvalidRequest
1276
     */
1277
    public boolean isNodeAuthorized(String pid) throws NotImplemented,
1278
            NotAuthorized, InvalidToken, ServiceFailure, NotFound,
1279
            InvalidRequest {
1280

    
1281
        boolean result = false;
1282
        Subject targetNodeSubject = new Subject();
1283
        String nodeSubject = null;
1284
        String replPermission = null;
1285

    
1286
        // get the pid
1287
        Identifier identifier = new Identifier();
1288
        identifier.setValue(pid);
1289

    
1290
        // get the target node subject
1291
        try {
1292
            nodeSubject = params.get("targetNodeSubject")[0];
1293
            targetNodeSubject.setValue(nodeSubject);
1294

    
1295
        } catch (NullPointerException e) {
1296
            String msg = "The 'targetNodeSubject' must be provided as a parameter and was not.";
1297
            logMetacat.error(msg);
1298
            throw new InvalidRequest("4873", msg);
1299

    
1300
        }
1301

    
1302
        result = CNodeService.getInstance(request).isNodeAuthorized(session, targetNodeSubject, identifier);
1303

    
1304
        response.setStatus(200);
1305
        response.setContentType("text/xml");
1306
        return result;
1307

    
1308
    }
1309

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

    
1333
        boolean result = false;
1334
        ReplicationPolicy policy = null;
1335
        long serialVersion = 0L;
1336
        String serialVersionStr = null;
1337

    
1338
        Identifier identifier = new Identifier();
1339
        identifier.setValue(pid);
1340

    
1341
        policy = collectReplicationPolicy();
1342

    
1343
        // get the serialVersion
1344
        try {
1345
            serialVersionStr = multipartparams.get("serialVersion").get(0);
1346
            serialVersion = new Long(serialVersionStr).longValue();
1347

    
1348
        } catch (NullPointerException e) {
1349
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
1350
            logMetacat.error(msg);
1351
            throw new InvalidRequest("4883", msg);
1352

    
1353
        }
1354
        result = CNodeService.getInstance(request).setReplicationPolicy(
1355
                session, identifier, policy, serialVersion);
1356
        response.setStatus(200);
1357
        response.setContentType("text/xml");
1358
        return result;
1359

    
1360
    }
1361
    
1362
    /**
1363
     * Update the system metadata for a given pid, setting it to be obsoleted
1364
     * by the obsoletedByPid
1365
     *  
1366
     * @param pid
1367
     * @return
1368
     * @throws NotImplemented
1369
     * @throws NotFound
1370
     * @throws NotAuthorized
1371
     * @throws ServiceFailure
1372
     * @throws InvalidRequest
1373
     * @throws InvalidToken
1374
     * @throws InstantiationException
1375
     * @throws IllegalAccessException
1376
     * @throws VersionMismatch
1377
     */
1378
    public boolean setObsoletedBy(String pid) 
1379
        throws NotImplemented, NotFound, NotAuthorized, ServiceFailure, 
1380
        InvalidRequest, InvalidToken, InstantiationException, 
1381
        IllegalAccessException, VersionMismatch {
1382

    
1383
        boolean result = false;
1384
        long serialVersion = 0L;
1385
        String serialVersionStr = null;
1386

    
1387
        Identifier identifier = new Identifier();
1388
        identifier.setValue(pid);
1389
        String obsoletedByPidString = null;
1390
        Identifier obsoletedByPid = null;
1391

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

    
1404
        } catch (IOException e1) {
1405
            String msg = "IOException: Couldn't parse the mime multipart information: " +
1406
            e1.getMessage();
1407
            logMetacat.debug(msg);
1408
            throw new ServiceFailure("4941", msg);
1409
        
1410
        } catch (Exception e1) {
1411
            String msg = "Exception: Couldn't parse the mime multipart information: " +
1412
            e1.getMessage();
1413
            logMetacat.debug(msg);
1414
            throw new ServiceFailure("4941", msg);
1415

    
1416
        }
1417

    
1418
        // get the obsoletedByPid
1419
        try {
1420
            obsoletedByPidString = multipartparams.get("obsoletedByPid").get(0);
1421
            obsoletedByPid = new Identifier();
1422
            obsoletedByPid.setValue(obsoletedByPidString);
1423
        } catch (NullPointerException e) {
1424
            String msg = "The 'obsoletedByPid' must be provided as a parameter and was not.";
1425
            logMetacat.error(msg);
1426
            throw new InvalidRequest("4883", msg);
1427
        }
1428

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

    
1451
    }
1452
    
1453
    /**
1454
     * Delete the replica entry with the given nodeId for the given pid
1455
     * 
1456
     * @param pid
1457
     * @return
1458
     * @throws NotImplemented
1459
     * @throws NotFound
1460
     * @throws NotAuthorized
1461
     * @throws ServiceFailure
1462
     * @throws InvalidRequest
1463
     * @throws InvalidToken
1464
     * @throws InstantiationException
1465
     * @throws IllegalAccessException
1466
     * @throws VersionMismatch
1467
     */
1468
    public boolean deleteReplica(String pid) 
1469
        throws NotImplemented, NotFound, NotAuthorized, ServiceFailure, 
1470
        InvalidRequest, InvalidToken, InstantiationException, 
1471
        IllegalAccessException, VersionMismatch {
1472

    
1473
        boolean result = false;
1474
        long serialVersion = 0L;
1475
        String serialVersionStr = null;
1476

    
1477
        Identifier identifier = new Identifier();
1478
        identifier.setValue(pid);
1479

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

    
1494
        } catch (IOException e1) {
1495
            String msg = "IOException: Couldn't parse the mime multipart information: " +
1496
            e1.getMessage();
1497
            logMetacat.debug(msg);
1498
            throw new ServiceFailure("4951", msg);
1499
        
1500
        } catch (Exception e1) {
1501
            String msg = "Exception: Couldn't parse the mime multipart information: " +
1502
            e1.getMessage();
1503
            logMetacat.debug(msg);
1504
            throw new ServiceFailure("4951", msg);
1505

    
1506
        }
1507
        
1508
        // get the nodeId param
1509
        try {
1510
            String nodeIdString = multipartparams.get("nodeId").get(0);
1511
            nodeId = new NodeReference();
1512
            nodeId.setValue(nodeIdString);
1513
            
1514
        } catch (NullPointerException e) {
1515
            String msg = "The 'nodeId' must be provided as a parameter and was not.";
1516
            logMetacat.error(msg);
1517
            throw new InvalidRequest("4952", msg);
1518
        }
1519

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

    
1542
    }
1543

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

    
1574
        // Parse the params out of the multipart form data
1575
        // Read the incoming data from its Mime Multipart encoding
1576
        logMetacat.debug("Parsing ReplicaStatus from the mime multipart entity");
1577

    
1578
        try {
1579
            failure = collectReplicationStatus();
1580
            
1581
        } catch (IOException e2) {
1582
            throw new ServiceFailure("4700", "Couldn't resolve the multipart request: " +
1583
                e2.getMessage());
1584
            
1585
        } catch (InstantiationException e2) {
1586
            throw new ServiceFailure("4700", "Couldn't resolve the multipart request: " +
1587
                e2.getMessage());
1588
            
1589
        } catch (IllegalAccessException e2) {
1590
            throw new ServiceFailure("4700", "Couldn't resolve the multipart request: " +
1591
                    e2.getMessage());
1592
            
1593
        } catch (JiBXException e2) {
1594
            throw new ServiceFailure("4700", "Couldn't resolve the multipart request: " +
1595
                    e2.getMessage());
1596
            
1597
        }
1598
        
1599
        // get the replication status param
1600
        try {
1601
            replicationStatus = multipartparams.get("status").get(0);
1602
            status = ReplicationStatus.convert(replicationStatus);
1603

    
1604
        } catch (NullPointerException npe) {
1605

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

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

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

    
1619
            }
1620

    
1621
        }
1622

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

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

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

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

    
1643
            }
1644

    
1645
        }
1646

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

    
1653
    }
1654

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

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

    
1681
        replica = collectReplicaMetadata();
1682

    
1683
        // get the serialVersion
1684
        try {
1685
            serialVersionStr = multipartparams.get("serialVersion").get(0);
1686
            serialVersion = new Long(serialVersionStr).longValue();
1687

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

    
1693
        }
1694

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

    
1701
    }
1702

    
1703
}
(1-1/9)