Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000-2011 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author:  $'
7
 *     '$Date:  $'
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

    
24
package edu.ucsb.nceas.metacat.dataone;
25

    
26
import java.io.InputStream;
27
import java.math.BigInteger;
28
import java.util.ArrayList;
29
import java.util.Calendar;
30
import java.util.Date;
31
import java.util.List;
32
import java.util.concurrent.locks.Lock;
33

    
34
import javax.servlet.http.HttpServletRequest;
35

    
36
import org.apache.log4j.Logger;
37
import org.dataone.client.CNode;
38
import org.dataone.client.D1Client;
39
import org.dataone.client.MNode;
40
import org.dataone.service.cn.v1.CNAuthorization;
41
import org.dataone.service.cn.v1.CNCore;
42
import org.dataone.service.cn.v1.CNRead;
43
import org.dataone.service.cn.v1.CNReplication;
44
import org.dataone.service.exceptions.BaseException;
45
import org.dataone.service.exceptions.IdentifierNotUnique;
46
import org.dataone.service.exceptions.InsufficientResources;
47
import org.dataone.service.exceptions.InvalidRequest;
48
import org.dataone.service.exceptions.InvalidSystemMetadata;
49
import org.dataone.service.exceptions.InvalidToken;
50
import org.dataone.service.exceptions.NotAuthorized;
51
import org.dataone.service.exceptions.NotFound;
52
import org.dataone.service.exceptions.NotImplemented;
53
import org.dataone.service.exceptions.ServiceFailure;
54
import org.dataone.service.exceptions.UnsupportedType;
55
import org.dataone.service.exceptions.VersionMismatch;
56
import org.dataone.service.types.v1.AccessPolicy;
57
import org.dataone.service.types.v1.Checksum;
58
import org.dataone.service.types.v1.ChecksumAlgorithmList;
59
import org.dataone.service.types.v1.Identifier;
60
import org.dataone.service.types.v1.Node;
61
import org.dataone.service.types.v1.NodeList;
62
import org.dataone.service.types.v1.NodeReference;
63
import org.dataone.service.types.v1.NodeType;
64
import org.dataone.service.types.v1.ObjectFormat;
65
import org.dataone.service.types.v1.ObjectFormatIdentifier;
66
import org.dataone.service.types.v1.ObjectFormatList;
67
import org.dataone.service.types.v1.ObjectList;
68
import org.dataone.service.types.v1.ObjectLocationList;
69
import org.dataone.service.types.v1.Permission;
70
import org.dataone.service.types.v1.Replica;
71
import org.dataone.service.types.v1.ReplicationPolicy;
72
import org.dataone.service.types.v1.ReplicationStatus;
73
import org.dataone.service.types.v1.Session;
74
import org.dataone.service.types.v1.Subject;
75
import org.dataone.service.types.v1.SystemMetadata;
76
import org.dataone.service.types.v1.util.ServiceMethodRestrictionUtil;
77

    
78
import edu.ucsb.nceas.metacat.EventLog;
79
import edu.ucsb.nceas.metacat.IdentifierManager;
80
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService;
81

    
82
/**
83
 * Represents Metacat's implementation of the DataONE Coordinating Node 
84
 * service API. Methods implement the various CN* interfaces, and methods common
85
 * to both Member Node and Coordinating Node interfaces are found in the
86
 * D1NodeService super class.
87
 *
88
 */
89
public class CNodeService extends D1NodeService implements CNAuthorization,
90
    CNCore, CNRead, CNReplication {
91

    
92
  /* the logger instance */
93
  private Logger logMetacat = null;
94

    
95
  /**
96
   * singleton accessor
97
   */
98
  public static CNodeService getInstance(HttpServletRequest request) { 
99
    return new CNodeService(request);
100
  }
101
  
102
  /**
103
   * Constructor, private for singleton access
104
   */
105
  private CNodeService(HttpServletRequest request) {
106
    super(request);
107
    logMetacat = Logger.getLogger(CNodeService.class);
108
        
109
  }
110
    
111
  /**
112
   * Set the replication policy for an object given the object identifier
113
   * 
114
   * @param session - the Session object containing the credentials for the Subject
115
   * @param pid - the object identifier for the given object
116
   * @param policy - the replication policy to be applied
117
   * 
118
   * @return true or false
119
   * 
120
   * @throws NotImplemented
121
   * @throws NotAuthorized
122
   * @throws ServiceFailure
123
   * @throws InvalidRequest
124
   * @throws VersionMismatch
125
   * 
126
   */
127
  @Override
128
  public boolean setReplicationPolicy(Session session, Identifier pid,
129
      ReplicationPolicy policy, long serialVersion) 
130
      throws NotImplemented, NotFound, NotAuthorized, ServiceFailure, 
131
      InvalidRequest, InvalidToken, VersionMismatch {
132
      
133
      // The lock to be used for this identifier
134
      Lock lock = null;
135
      
136
      // get the subject
137
      Subject subject = session.getSubject();
138
      
139
      // are we allowed to do this?
140
      if (!isAuthorized(session, pid, Permission.CHANGE_PERMISSION)) {
141
          throw new NotAuthorized("4881", Permission.CHANGE_PERMISSION
142
                  + " not allowed by " + subject.getValue() + " on "
143
                  + pid.getValue());
144
          
145
      }
146
      
147
      SystemMetadata systemMetadata = null;
148
      try {
149
          lock = HazelcastService.getInstance().getLock(pid.getValue());
150
          lock.lock();
151
          logMetacat.debug("Locked identifier " + pid.getValue());
152

    
153
          try {
154
              if ( HazelcastService.getInstance().getSystemMetadataMap().containsKey(pid) ) {
155
                  systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
156
                  
157
              }
158
              
159
              // did we get it correctly?
160
              if ( systemMetadata == null ) {
161
                  throw new NotFound("4884", "Couldn't find an object identified by " + pid.getValue());
162
                  
163
              }
164

    
165
              // does the request have the most current system metadata?
166
              if ( systemMetadata.getSerialVersion().longValue() != serialVersion ) {
167
                 String msg = "The requested system metadata version number " + 
168
                     serialVersion + " differs from the current version at " +
169
                     systemMetadata.getSerialVersion().longValue() +
170
                     ". Please get the latest copy in order to modify it.";
171
                 throw new VersionMismatch("4886", msg);
172
                 
173
              }
174
              
175
          } catch (RuntimeException e) { // Catch is generic since HZ throws RuntimeException
176
              throw new NotFound("4884", "No record found for: " + pid.getValue());
177
            
178
          }
179
          
180
          // set the new policy
181
          systemMetadata.setReplicationPolicy(policy);
182
          
183
          // update the metadata
184
          try {
185
              systemMetadata.setSerialVersion(systemMetadata.getSerialVersion().add(BigInteger.ONE));
186
              systemMetadata.setDateSysMetadataModified(Calendar.getInstance().getTime());
187
              HazelcastService.getInstance().getSystemMetadataMap().put(systemMetadata.getIdentifier(), systemMetadata);
188
              notifyReplicaNodes(systemMetadata);
189
              
190
          } catch (RuntimeException e) {
191
              throw new ServiceFailure("4882", e.getMessage());
192
          
193
          }
194
          
195
      } catch (RuntimeException e) {
196
          throw new ServiceFailure("4882", e.getMessage());
197
          
198
      } finally {
199
          lock.unlock();
200
          logMetacat.debug("Unlocked identifier " + pid.getValue());
201
          
202
      }
203
    
204
      return true;
205
  }
206

    
207
  /**
208
   * Deletes the replica from the given Member Node
209
   * NOTE: MN.delete() may be an "archive" operation. TBD.
210
   * @param session
211
   * @param pid
212
   * @param nodeId
213
   * @param serialVersion
214
   * @return
215
   * @throws InvalidToken
216
   * @throws ServiceFailure
217
   * @throws NotAuthorized
218
   * @throws NotFound
219
   * @throws NotImplemented
220
   * @throws VersionMismatch
221
   */
222
  @Override
223
  public boolean deleteReplicationMetadata(Session session, Identifier pid, NodeReference nodeId, long serialVersion) 
224
  	throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented, VersionMismatch {
225
	  
226
	  	// The lock to be used for this identifier
227
		Lock lock = null;
228

    
229
		// get the subject
230
		Subject subject = session.getSubject();
231

    
232
		// are we allowed to do this?
233
		boolean isAuthorized = false;
234
		try {
235
			isAuthorized = isAuthorized(session, pid, Permission.WRITE);
236
		} catch (InvalidRequest e) {
237
			throw new ServiceFailure("4882", e.getDescription());
238
		}
239
		if (!isAuthorized) {
240
			throw new NotAuthorized("4881", Permission.WRITE
241
					+ " not allowed by " + subject.getValue() + " on "
242
					+ pid.getValue());
243

    
244
		}
245

    
246
		SystemMetadata systemMetadata = null;
247
		try {
248
			lock = HazelcastService.getInstance().getLock(pid.getValue());
249
			lock.lock();
250
			logMetacat.debug("Locked identifier " + pid.getValue());
251

    
252
			try {
253
				if (HazelcastService.getInstance().getSystemMetadataMap().containsKey(pid)) {
254
					systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
255
				}
256

    
257
				// did we get it correctly?
258
				if (systemMetadata == null) {
259
					throw new NotFound("4884", "Couldn't find an object identified by " + pid.getValue());
260
				}
261

    
262
				// does the request have the most current system metadata?
263
				if (systemMetadata.getSerialVersion().longValue() != serialVersion) {
264
					String msg = "The requested system metadata version number "
265
							+ serialVersion
266
							+ " differs from the current version at "
267
							+ systemMetadata.getSerialVersion().longValue()
268
							+ ". Please get the latest copy in order to modify it.";
269
					throw new VersionMismatch("4886", msg);
270

    
271
				}
272

    
273
			} catch (RuntimeException e) { // Catch is generic since HZ throws RuntimeException
274
				throw new NotFound("4884", "No record found for: " + pid.getValue());
275

    
276
			}
277
			  
278
			// check permissions
279
			// TODO: is this necessary?
280
			List<Node> nodeList = D1Client.getCN().listNodes().getNodeList();
281
			boolean isAllowed = ServiceMethodRestrictionUtil.isMethodAllowed(session.getSubject(), nodeList, "CNReplication", "deleteReplicationMetadata");
282
			if (isAllowed) {
283
				throw new NotAuthorized("4881", "Caller is not authorized to deleteReplicationMetadata");
284
			}
285
			  
286
			// delete the replica from the given node
287
			D1Client.getMN(nodeId).delete(session, pid);
288
			  
289
			// reflect that change in the system metadata
290
			List<Replica> updatedReplicas = new ArrayList<Replica>(systemMetadata.getReplicaList());
291
			for (Replica r: systemMetadata.getReplicaList()) {
292
				  if (r.getReplicaMemberNode().equals(nodeId)) {
293
					  updatedReplicas.remove(r);
294
					  break;
295
				  }
296
			}
297
			systemMetadata.setReplicaList(updatedReplicas);
298

    
299
			// update the metadata
300
			try {
301
				systemMetadata.setSerialVersion(systemMetadata.getSerialVersion().add(BigInteger.ONE));
302
				systemMetadata.setDateSysMetadataModified(Calendar.getInstance().getTime());
303
				HazelcastService.getInstance().getSystemMetadataMap().put(systemMetadata.getIdentifier(), systemMetadata);
304
			} catch (RuntimeException e) {
305
				throw new ServiceFailure("4882", e.getMessage());
306
			}
307

    
308
		} catch (RuntimeException e) {
309
			throw new ServiceFailure("4882", e.getMessage());
310
		} finally {
311
			lock.unlock();
312
			logMetacat.debug("Unlocked identifier " + pid.getValue());
313
		}
314

    
315
		return true;	  
316
	  
317
  }
318
  
319
  /**
320
   * Deletes an object from the Coordinating Node, where the object is a 
321
   * a science metadata object.
322
   * 
323
   * @param session - the Session object containing the credentials for the Subject
324
   * @param pid - The object identifier to be deleted
325
   * 
326
   * @return pid - the identifier of the object used for the deletion
327
   * 
328
   * @throws InvalidToken
329
   * @throws ServiceFailure
330
   * @throws NotAuthorized
331
   * @throws NotFound
332
   * @throws NotImplemented
333
   * @throws InvalidRequest
334
   */
335
  @Override
336
  public Identifier delete(Session session, Identifier pid) 
337
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
338

    
339
	  // check that it is CN/admin
340
	  boolean allowed = isAdminAuthorized(session, pid, Permission.CHANGE_PERMISSION);
341
	  
342
	  if (!allowed) {
343
		  String msg = "The subject is not allowed to call delete() on a Coordinating Node.";
344
		  logMetacat.info(msg);
345
		  throw new NotAuthorized("1320", msg);
346
	  }
347
	  
348
	  // defer to superclass implementation
349
      return super.delete(session, pid);
350
  }
351
  
352
  /**
353
   * Set the obsoletedBy attribute in System Metadata
354
   * @param session
355
   * @param pid
356
   * @param obsoletedByPid
357
   * @param serialVersion
358
   * @return
359
   * @throws NotImplemented
360
   * @throws NotFound
361
   * @throws NotAuthorized
362
   * @throws ServiceFailure
363
   * @throws InvalidRequest
364
   * @throws InvalidToken
365
   * @throws VersionMismatch
366
   */
367
  @Override
368
  public boolean setObsoletedBy(Session session, Identifier pid,
369
			Identifier obsoletedByPid, long serialVersion)
370
			throws NotImplemented, NotFound, NotAuthorized, ServiceFailure,
371
			InvalidRequest, InvalidToken, VersionMismatch {
372

    
373
		// The lock to be used for this identifier
374
		Lock lock = null;
375

    
376
		// get the subject
377
		Subject subject = session.getSubject();
378

    
379
		// are we allowed to do this?
380
		if (!isAuthorized(session, pid, Permission.WRITE)) {
381
			throw new NotAuthorized("4881", Permission.WRITE
382
					+ " not allowed by " + subject.getValue() + " on "
383
					+ pid.getValue());
384

    
385
		}
386

    
387

    
388
		SystemMetadata systemMetadata = null;
389
		try {
390
			lock = HazelcastService.getInstance().getLock(pid.getValue());
391
			lock.lock();
392
			logMetacat.debug("Locked identifier " + pid.getValue());
393

    
394
			try {
395
				if (HazelcastService.getInstance().getSystemMetadataMap().containsKey(pid)) {
396
					systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
397
				}
398

    
399
				// did we get it correctly?
400
				if (systemMetadata == null) {
401
					throw new NotFound("4884", "Couldn't find an object identified by " + pid.getValue());
402
				}
403

    
404
				// does the request have the most current system metadata?
405
				if (systemMetadata.getSerialVersion().longValue() != serialVersion) {
406
					String msg = "The requested system metadata version number "
407
							+ serialVersion
408
							+ " differs from the current version at "
409
							+ systemMetadata.getSerialVersion().longValue()
410
							+ ". Please get the latest copy in order to modify it.";
411
					throw new VersionMismatch("4886", msg);
412

    
413
				}
414

    
415
			} catch (RuntimeException e) { // Catch is generic since HZ throws RuntimeException
416
				throw new NotFound("4884", "No record found for: " + pid.getValue());
417

    
418
			}
419

    
420
			// set the new policy
421
			systemMetadata.setObsoletedBy(obsoletedByPid);
422

    
423
			// update the metadata
424
			try {
425
				systemMetadata.setSerialVersion(systemMetadata.getSerialVersion().add(BigInteger.ONE));
426
				systemMetadata.setDateSysMetadataModified(Calendar.getInstance().getTime());
427
				HazelcastService.getInstance().getSystemMetadataMap().put(systemMetadata.getIdentifier(), systemMetadata);
428
			} catch (RuntimeException e) {
429
				throw new ServiceFailure("4882", e.getMessage());
430
			}
431

    
432
		} catch (RuntimeException e) {
433
			throw new ServiceFailure("4882", e.getMessage());
434
		} finally {
435
			lock.unlock();
436
			logMetacat.debug("Unlocked identifier " + pid.getValue());
437
		}
438

    
439
		return true;
440
	}
441
  
442
  
443
  /**
444
   * Set the replication status for an object given the object identifier
445
   * 
446
   * @param session - the Session object containing the credentials for the Subject
447
   * @param pid - the object identifier for the given object
448
   * @param status - the replication status to be applied
449
   * 
450
   * @return true or false
451
   * 
452
   * @throws NotImplemented
453
   * @throws NotAuthorized
454
   * @throws ServiceFailure
455
   * @throws InvalidRequest
456
   * @throws InvalidToken
457
   * @throws NotFound
458
   * 
459
   */
460
  @Override
461
  public boolean setReplicationStatus(Session session, Identifier pid,
462
      NodeReference targetNode, ReplicationStatus status, BaseException failure) 
463
      throws ServiceFailure, NotImplemented, InvalidToken, NotAuthorized, 
464
      InvalidRequest, NotFound {
465
	  
466
	  // cannot be called by public
467
	  if (session == null) {
468
		  throw new NotAuthorized("4720", "Session cannot be null");
469
	  }
470
      
471
      // The lock to be used for this identifier
472
      Lock lock = null;
473
      
474
      boolean allowed = false;
475
      int replicaEntryIndex = -1;
476
      List<Replica> replicas = null;
477
      // get the subject
478
      Subject subject = session.getSubject();
479
      logMetacat.debug("ReplicationStatus for identifier " + pid.getValue() +
480
          " is " + status.toString());
481
      
482
      SystemMetadata systemMetadata = null;
483

    
484
      try {
485
          lock = HazelcastService.getInstance().getLock(pid.getValue());
486
          lock.lock();
487
          logMetacat.debug("Locked identifier " + pid.getValue());
488

    
489
          try {      
490
              systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
491

    
492
              // did we get it correctly?
493
              if ( systemMetadata == null ) {
494
                  logMetacat.debug("systemMetadata is null for " + pid.getValue());
495
                  throw new NotFound("4740", "Couldn't find an object identified by " + pid.getValue());
496
                  
497
              }
498
              replicas = systemMetadata.getReplicaList();
499
              int count = 0;
500
              
501
              // was there a failure? log it
502
              if ( failure != null && status == ReplicationStatus.FAILED ) {
503
                 String msg = "The replication request of the object identified by " + 
504
                     pid.getValue() + " failed.  The error message was " +
505
                     failure.getMessage() + ".";
506
              }
507
              
508
              if (replicas.size() > 0 && replicas != null) {
509
                  // find the target replica index in the replica list
510
                  for (Replica replica : replicas) {
511
                      String replicaNodeStr = replica.getReplicaMemberNode()
512
                              .getValue();
513
                      String targetNodeStr = targetNode.getValue();
514
                      logMetacat.debug("Comparing " + replicaNodeStr + " to "
515
                              + targetNodeStr);
516
                  
517
                      if (replicaNodeStr.equals(targetNodeStr)) {
518
                          replicaEntryIndex = count;
519
                          logMetacat.debug("replica entry index is: "
520
                                  + replicaEntryIndex);
521
                          break;
522
                      }
523
                      count++;
524
                  
525
                  }
526
              }
527
              // are we allowed to do this? only CNs and target MNs are allowed
528
              CNode cn = D1Client.getCN();
529
              List<Node> nodes = cn.listNodes().getNodeList();
530
              
531
              // find the node in the node list
532
              for ( Node node : nodes ) {
533
                  
534
                  NodeReference nodeReference = node.getIdentifier();
535
                  logMetacat.debug("In setReplicationStatus(), Node reference is: " + nodeReference.getValue());
536
                  
537
                  // allow target MN certs
538
                  if (targetNode.getValue().equals(nodeReference.getValue()) &&
539
                      node.getType() == NodeType.MN) {
540
                      List<Subject> nodeSubjects = node.getSubjectList();
541
                      
542
                      // check if the session subject is in the node subject list
543
                      for (Subject nodeSubject : nodeSubjects) {
544
                          logMetacat.debug("In setReplicationStatus(), comparing subjects: " +
545
                                  nodeSubject.getValue() + " and " + subject.getValue());
546
                          if ( nodeSubject.equals(subject) ) { // subject of session == target node subject
547
                              
548
                              // lastly limit to COMPLETED status updates from MNs only
549
                              if ( status == ReplicationStatus.COMPLETED ) {
550
                                  allowed = true;
551
                                  break;
552
                                  
553
                              }                              
554
                          }
555
                      }                 
556
                  }
557
              }
558

    
559
              if ( !allowed ) {
560
                  //check for CN admin access
561
                  allowed = isAuthorized(session, pid, Permission.WRITE);
562
                  
563
              }              
564
              
565
              if ( !allowed ) {
566
                  String msg = "The subject identified by "
567
                          + subject.getValue()
568
                          + " does not have permission to set the replication status for "
569
                          + "the replica identified by "
570
                          + targetNode.getValue() + ".";
571
                  logMetacat.info(msg);
572
                  throw new NotAuthorized("4720", msg);
573
                  
574
              }
575

    
576
          } catch (RuntimeException e) { // Catch is generic since HZ throws RuntimeException
577
            throw new NotFound("4740", "No record found for: " + pid.getValue() +
578
                " : " + e.getMessage());
579
            
580
          }
581
          
582
          Replica targetReplica = new Replica();
583
          // set the status for the replica
584
          if ( replicaEntryIndex != -1 ) {
585
              targetReplica = replicas.get(replicaEntryIndex);
586
              targetReplica.setReplicationStatus(status);
587
              logMetacat.debug("Set the replication status for " + 
588
                  targetReplica.getReplicaMemberNode().getValue() + " to " +
589
                  targetReplica.getReplicationStatus());
590
              
591
          } else {
592
              // this is a new entry, create it
593
              targetReplica.setReplicaMemberNode(targetNode);
594
              targetReplica.setReplicationStatus(status);
595
              targetReplica.setReplicaVerified(Calendar.getInstance().getTime());
596
              replicas.add(targetReplica);
597
              
598
          }
599
          
600
          systemMetadata.setReplicaList(replicas);
601
                
602
          // update the metadata
603
          try {
604
              systemMetadata.setSerialVersion(systemMetadata.getSerialVersion().add(BigInteger.ONE));
605
              systemMetadata.setDateSysMetadataModified(Calendar.getInstance().getTime());
606
              HazelcastService.getInstance().getSystemMetadataMap().put(systemMetadata.getIdentifier(), systemMetadata);
607
              
608
              if ( status == ReplicationStatus.FAILED && failure != null ) {
609
                  logMetacat.warn("Replication failed for identifier " + pid.getValue() +
610
                      " on target node " + targetNode + ". The exception was: " +
611
                      failure.getMessage());
612
              }
613
          } catch (RuntimeException e) {
614
              throw new ServiceFailure("4700", e.getMessage());
615
          
616
          }
617
          
618
    } catch (RuntimeException e) {
619
        String msg = "There was a RuntimeException getting the lock for " +
620
            pid.getValue();
621
        logMetacat.info(msg);
622
        
623
    } finally {
624
        lock.unlock();
625
        logMetacat.debug("Unlocked identifier " + pid.getValue());
626
        
627
    }
628
      
629
      return true;
630
  }
631
  
632
  /**
633
   * Return the checksum of the object given the identifier 
634
   * 
635
   * @param session - the Session object containing the credentials for the Subject
636
   * @param pid - the object identifier for the given object
637
   * 
638
   * @return checksum - the checksum of the object
639
   * 
640
   * @throws InvalidToken
641
   * @throws ServiceFailure
642
   * @throws NotAuthorized
643
   * @throws NotFound
644
   * @throws NotImplemented
645
   */
646
  @Override
647
  public Checksum getChecksum(Session session, Identifier pid)
648
    throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
649
    NotImplemented {
650
    
651
	boolean isAuthorized = false;
652
	try {
653
		isAuthorized = isAuthorized(session, pid, Permission.READ);
654
	} catch (InvalidRequest e) {
655
		throw new ServiceFailure("1410", e.getDescription());
656
	}  
657
    if (!isAuthorized) {
658
        throw new NotAuthorized("1400", Permission.READ + " not allowed on " + pid.getValue());  
659
    }
660
    
661
    SystemMetadata systemMetadata = null;
662
    Checksum checksum = null;
663
    
664
    try {
665
        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);        
666

    
667
        if (systemMetadata == null ) {
668
            throw new NotFound("1420", "Couldn't find an object identified by " + pid.getValue());
669
        }
670
        checksum = systemMetadata.getChecksum();
671
        
672
    } catch (RuntimeException e) {
673
        throw new ServiceFailure("1410", "An error occurred getting the checksum for " + 
674
            pid.getValue() + ". The error message was: " + e.getMessage());
675
      
676
    }
677
    
678
    return checksum;
679
  }
680

    
681
  /**
682
   * Resolve the location of a given object
683
   * 
684
   * @param session - the Session object containing the credentials for the Subject
685
   * @param pid - the object identifier for the given object
686
   * 
687
   * @return objectLocationList - the list of nodes known to contain the object
688
   * 
689
   * @throws InvalidToken
690
   * @throws ServiceFailure
691
   * @throws NotAuthorized
692
   * @throws NotFound
693
   * @throws NotImplemented
694
   */
695
  @Override
696
  public ObjectLocationList resolve(Session session, Identifier pid)
697
    throws InvalidToken, ServiceFailure, NotAuthorized,
698
    NotFound, NotImplemented {
699

    
700
    throw new NotImplemented("4131", "resolve not implemented");
701

    
702
  }
703

    
704
  /**
705
   * Search the metadata catalog for identifiers that match the criteria
706
   * 
707
   * @param session - the Session object containing the credentials for the Subject
708
   * @param queryType - An identifier for the type of query expression 
709
   *                    provided in the query
710
   * @param query -  The criteria for matching the characteristics of the 
711
   *                 metadata objects of interest
712
   * 
713
   * @return objectList - the list of objects matching the criteria
714
   * 
715
   * @throws InvalidToken
716
   * @throws ServiceFailure
717
   * @throws NotAuthorized
718
   * @throws InvalidRequest
719
   * @throws NotImplemented
720
   */
721
  @Override
722
  public ObjectList search(Session session, String queryType, String query)
723
    throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest,
724
    NotImplemented {
725

    
726
    ObjectList objectList = null;
727
    try {
728
        objectList = 
729
          IdentifierManager.getInstance().querySystemMetadata(
730
              null, //startTime, 
731
              null, //endTime,
732
              null, //objectFormat, 
733
              false, //replicaStatus, 
734
              0, //start, 
735
              -1 //count
736
              );
737
        
738
    } catch (Exception e) {
739
      throw new ServiceFailure("4310", "Error querying system metadata: " + e.getMessage());
740
    }
741

    
742
      return objectList;
743
      
744
    //throw new NotImplemented("4281", "search not implemented");
745
    
746
    // the code block below is from an older implementation
747
    
748
    /*  This block commented out because of the EcoGrid circular dependency.
749
         *  For now, query will not be supported until the circularity can be
750
         *  resolved, probably by moving the ecogrid query syntax transformers
751
         *  directly into the Metacat codebase.  MBJ 2010-02-03
752
         
753
        try {
754
            EcogridQueryParser parser = new EcogridQueryParser(request
755
                    .getReader());
756
            parser.parseXML();
757
            QueryType queryType = parser.getEcogridQuery();
758
            EcogridJavaToMetacatJavaQueryTransformer queryTransformer = 
759
                new EcogridJavaToMetacatJavaQueryTransformer();
760
            QuerySpecification metacatQuery = queryTransformer
761
                    .transform(queryType);
762

    
763
            DBQuery metacat = new DBQuery();
764

    
765
            boolean useXMLIndex = (new Boolean(PropertyService
766
                    .getProperty("database.usexmlindex"))).booleanValue();
767
            String xmlquery = "query"; // we don't care the query in resultset,
768
            // the query can be anything
769
            PrintWriter out = null; // we don't want metacat result, so set out null
770

    
771
            // parameter: queryspecification, user, group, usingIndexOrNot
772
            StringBuffer result = metacat.createResultDocument(xmlquery,
773
                    metacatQuery, out, username, groupNames, useXMLIndex);
774

    
775
            // create result set transfer       
776
            String saxparser = PropertyService.getProperty("xml.saxparser");
777
            MetacatResultsetParser metacatResultsetParser = new MetacatResultsetParser(
778
                    new StringReader(result.toString()), saxparser, queryType
779
                            .getNamespace().get_value());
780
            ResultsetType records = metacatResultsetParser.getEcogridResult();
781

    
782
            System.out
783
                    .println(EcogridResultsetTransformer.toXMLString(records));
784
            response.setContentType("text/xml");
785
            out = response.getWriter();
786
            out.print(EcogridResultsetTransformer.toXMLString(records));
787

    
788
        } catch (Exception e) {
789
            e.printStackTrace();
790
        }*/
791
    
792

    
793
  }
794
  
795
  /**
796
   * Returns the object format registered in the DataONE Object Format 
797
   * Vocabulary for the given format identifier
798
   * 
799
   * @param fmtid - the identifier of the format requested
800
   * 
801
   * @return objectFormat - the object format requested
802
   * 
803
   * @throws ServiceFailure
804
   * @throws NotFound
805
   * @throws InsufficientResources
806
   * @throws NotImplemented
807
   */
808
  @Override
809
  public ObjectFormat getFormat(ObjectFormatIdentifier fmtid)
810
    throws ServiceFailure, NotFound, NotImplemented {
811
     
812
      return ObjectFormatService.getInstance().getFormat(fmtid);
813
      
814
  }
815

    
816
  /**
817
   * Returns a list of all object formats registered in the DataONE Object 
818
   * Format Vocabulary
819
    * 
820
   * @return objectFormatList - The list of object formats registered in 
821
   *                            the DataONE Object Format Vocabulary
822
   * 
823
   * @throws ServiceFailure
824
   * @throws NotImplemented
825
   * @throws InsufficientResources
826
   */
827
  @Override
828
  public ObjectFormatList listFormats() 
829
    throws ServiceFailure, NotImplemented {
830

    
831
    return ObjectFormatService.getInstance().listFormats();
832
  }
833

    
834
  /**
835
   * Returns a list of nodes that have been registered with the DataONE infrastructure
836
    * 
837
   * @return nodeList - List of nodes from the registry
838
   * 
839
   * @throws ServiceFailure
840
   * @throws NotImplemented
841
   */
842
  @Override
843
  public NodeList listNodes() 
844
    throws NotImplemented, ServiceFailure {
845

    
846
    throw new NotImplemented("4800", "listNodes not implemented");
847
  }
848

    
849
  /**
850
   * Provides a mechanism for adding system metadata independently of its 
851
   * associated object, such as when adding system metadata for data objects.
852
    * 
853
   * @param session - the Session object containing the credentials for the Subject
854
   * @param pid - The identifier of the object to register the system metadata against
855
   * @param sysmeta - The system metadata to be registered
856
   * 
857
   * @return true if the registration succeeds
858
   * 
859
   * @throws NotImplemented
860
   * @throws NotAuthorized
861
   * @throws ServiceFailure
862
   * @throws InvalidRequest
863
   * @throws InvalidSystemMetadata
864
   */
865
  @Override
866
  public Identifier registerSystemMetadata(Session session, Identifier pid,
867
      SystemMetadata sysmeta) 
868
      throws NotImplemented, NotAuthorized, ServiceFailure, InvalidRequest, 
869
      InvalidSystemMetadata {
870

    
871
      // The lock to be used for this identifier
872
      Lock lock = null;
873

    
874
      // TODO: control who can call this?
875
      if (session == null) {
876
          //TODO: many of the thrown exceptions do not use the correct error codes
877
          //check these against the docs and correct them
878
          throw new NotAuthorized("4861", "No Session - could not authorize for registration." +
879
                  "  If you are not logged in, please do so and retry the request.");
880
      }
881
      
882
      // verify that guid == SystemMetadata.getIdentifier()
883
      logMetacat.debug("Comparing guid|sysmeta_guid: " + pid.getValue() + 
884
          "|" + sysmeta.getIdentifier().getValue());
885
      if (!pid.getValue().equals(sysmeta.getIdentifier().getValue())) {
886
          throw new InvalidRequest("4863", 
887
              "The identifier in method call (" + pid.getValue() + 
888
              ") does not match identifier in system metadata (" +
889
              sysmeta.getIdentifier().getValue() + ").");
890
      }
891

    
892
      try {
893
          lock = HazelcastService.getInstance().getLock(sysmeta.getIdentifier().getValue());
894
          lock.lock();
895
          logMetacat.debug("Locked identifier " + pid.getValue());
896
          logMetacat.debug("Checking if identifier exists...");
897
          // Check that the identifier does not already exist
898
          if (HazelcastService.getInstance().getSystemMetadataMap().containsKey(pid)) {
899
              throw new InvalidRequest("4863", 
900
                  "The identifier is already in use by an existing object.");
901
          
902
          }
903
          
904
          // insert the system metadata into the object store
905
          logMetacat.debug("Starting to insert SystemMetadata...");
906
          try {
907
              sysmeta.setSerialVersion(BigInteger.ONE);
908
              sysmeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
909
              HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
910
              
911
          } catch (RuntimeException e) {
912
            logMetacat.error("Problem registering system metadata: " + pid.getValue(), e);
913
              throw new ServiceFailure("4862", "Error inserting system metadata: " + 
914
                  e.getClass() + ": " + e.getMessage());
915
              
916
          }
917
          
918
      } catch (RuntimeException e) {
919
          throw new ServiceFailure("4862", "Error inserting system metadata: " + 
920
                  e.getClass() + ": " + e.getMessage());
921
          
922
      }  finally {
923
          lock.unlock();
924
          logMetacat.debug("Unlocked identifier " + pid.getValue());
925
          
926
      }
927

    
928
      
929
      logMetacat.debug("Returning from registerSystemMetadata");
930
      EventLog.getInstance().log(request.getRemoteAddr(), 
931
          request.getHeader("User-Agent"), session.getSubject().getValue(), 
932
          pid.getValue(), "registerSystemMetadata");
933
      return pid;
934
  }
935
  
936
  /**
937
   * Given an optional scope and format, reserves and returns an identifier 
938
   * within that scope and format that is unique and will not be 
939
   * used by any other sessions. 
940
    * 
941
   * @param session - the Session object containing the credentials for the Subject
942
   * @param pid - The identifier of the object to register the system metadata against
943
   * @param scope - An optional string to be used to qualify the scope of 
944
   *                the identifier namespace, which is applied differently 
945
   *                depending on the format requested. If scope is not 
946
   *                supplied, a default scope will be used.
947
   * @param format - The optional name of the identifier format to be used, 
948
   *                  drawn from a DataONE-specific vocabulary of identifier 
949
   *                 format names, including several common syntaxes such 
950
   *                 as DOI, LSID, UUID, and LSRN, among others. If the 
951
   *                 format is not supplied by the caller, the CN service 
952
   *                 will use a default identifier format, which may change 
953
   *                 over time.
954
   * 
955
   * @return true if the registration succeeds
956
   * 
957
   * @throws InvalidToken
958
   * @throws ServiceFailure
959
   * @throws NotAuthorized
960
   * @throws IdentifierNotUnique
961
   * @throws NotImplemented
962
   */
963
  @Override
964
  public Identifier reserveIdentifier(Session session, Identifier pid)
965
  throws InvalidToken, ServiceFailure,
966
        NotAuthorized, IdentifierNotUnique, NotImplemented, InvalidRequest {
967

    
968
    throw new NotImplemented("4191", "reserveIdentifier not implemented on this node");
969
  }
970
  
971
  @Override
972
  public Identifier generateIdentifier(Session session, String scheme, String fragment)
973
  throws InvalidToken, ServiceFailure,
974
        NotAuthorized, NotImplemented, InvalidRequest {
975
    throw new NotImplemented("4191", "generateIdentifier not implemented on this node");
976
  }
977
  
978
  /**
979
    * Checks whether the pid is reserved by the subject in the session param
980
    * If the reservation is held on the pid by the subject, we return true.
981
    * 
982
   * @param session - the Session object containing the Subject
983
   * @param pid - The identifier to check
984
   * 
985
   * @return true if the reservation exists for the subject/pid
986
   * 
987
   * @throws InvalidToken
988
   * @throws ServiceFailure
989
   * @throws NotFound - when the pid is not found (in use or in reservation)
990
   * @throws NotAuthorized - when the subject does not hold a reservation on the pid
991
   * @throws IdentifierNotUnique - when the pid is in use
992
   * @throws NotImplemented
993
   */
994

    
995
  @Override
996
  public boolean hasReservation(Session session, Subject subject, Identifier pid) 
997
      throws InvalidToken, ServiceFailure, NotFound, NotAuthorized, IdentifierNotUnique, 
998
      NotImplemented, InvalidRequest {
999
  
1000
      throw new NotImplemented("4191", "hasReservation not implemented on this node");
1001
  }
1002

    
1003
  /**
1004
   * Changes ownership (RightsHolder) of the specified object to the 
1005
   * subject specified by userId
1006
    * 
1007
   * @param session - the Session object containing the credentials for the Subject
1008
   * @param pid - Identifier of the object to be modified
1009
   * @param userId - The subject that will be taking ownership of the specified object.
1010
   *
1011
   * @return pid - the identifier of the modified object
1012
   * 
1013
   * @throws ServiceFailure
1014
   * @throws InvalidToken
1015
   * @throws NotFound
1016
   * @throws NotAuthorized
1017
   * @throws NotImplemented
1018
   * @throws InvalidRequest
1019
   */  
1020
  @Override
1021
  public Identifier setRightsHolder(Session session, Identifier pid, Subject userId,
1022
      long serialVersion)
1023
      throws InvalidToken, ServiceFailure, NotFound, NotAuthorized,
1024
      NotImplemented, InvalidRequest, VersionMismatch {
1025
      
1026
      // The lock to be used for this identifier
1027
      Lock lock = null;
1028

    
1029
      // get the subject
1030
      Subject subject = session.getSubject();
1031
      
1032
      // are we allowed to do this?
1033
      if (!isAuthorized(session, pid, Permission.CHANGE_PERMISSION)) {
1034
          throw new NotAuthorized("4440", "not allowed by "
1035
                  + subject.getValue() + " on " + pid.getValue());
1036
          
1037
      }
1038
      
1039
      SystemMetadata systemMetadata = null;
1040
      try {
1041
          lock = HazelcastService.getInstance().getLock(pid.getValue());
1042
          logMetacat.debug("Locked identifier " + pid.getValue());
1043

    
1044
          try {
1045
              systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1046
              
1047
              // does the request have the most current system metadata?
1048
              if ( systemMetadata.getSerialVersion().longValue() != serialVersion ) {
1049
                 String msg = "The requested system metadata version number " + 
1050
                     serialVersion + " differs from the current version at " +
1051
                     systemMetadata.getSerialVersion().longValue() +
1052
                     ". Please get the latest copy in order to modify it.";
1053
                 throw new VersionMismatch("4443", msg);
1054
              }
1055
              
1056
          } catch (RuntimeException e) { // Catch is generic since HZ throws RuntimeException
1057
              throw new NotFound("4460", "No record found for: " + pid.getValue());
1058
              
1059
          }
1060
              
1061
          // set the new rights holder
1062
          systemMetadata.setRightsHolder(userId);
1063
          
1064
          // update the metadata
1065
          try {
1066
              systemMetadata.setSerialVersion(systemMetadata.getSerialVersion().add(BigInteger.ONE));
1067
              systemMetadata.setDateSysMetadataModified(Calendar.getInstance().getTime());
1068
              HazelcastService.getInstance().getSystemMetadataMap().put(pid, systemMetadata);
1069
              notifyReplicaNodes(systemMetadata);
1070
              
1071
          } catch (RuntimeException e) {
1072
              throw new ServiceFailure("4490", e.getMessage());
1073
          
1074
          }
1075
          
1076
      } catch (RuntimeException e) {
1077
          throw new ServiceFailure("4490", e.getMessage());
1078
          
1079
      } finally {
1080
          lock.unlock();
1081
          logMetacat.debug("Unlocked identifier " + pid.getValue());
1082
      
1083
      }
1084
      
1085
      return pid;
1086
  }
1087

    
1088
  /**
1089
   * Verify that a replication task is authorized by comparing the target node's
1090
   * Subject (from the X.509 certificate-derived Session) with the list of 
1091
   * subjects in the known, pending replication tasks map.
1092
   * 
1093
   * @param originatingNodeSession - Session information that contains the 
1094
   *                                 identity of the calling user
1095
   * @param targetNodeSubject - Subject identifying the target node
1096
   * @param pid - the identifier of the object to be replicated
1097
   * @param replicatePermission - the execute permission to be granted
1098
   * 
1099
   * @throws ServiceFailure
1100
   * @throws NotImplemented
1101
   * @throws InvalidToken
1102
   * @throws NotAuthorized
1103
   * @throws InvalidRequest
1104
   * @throws NotFound
1105
   */
1106
  @Override
1107
  public boolean isNodeAuthorized(Session originatingNodeSession, 
1108
    Subject targetNodeSubject, Identifier pid) 
1109
    throws NotImplemented, NotAuthorized, InvalidToken, ServiceFailure, 
1110
    NotFound, InvalidRequest {
1111
    
1112
    boolean isAllowed = false;
1113
    SystemMetadata sysmeta = null;
1114
    NodeReference targetNode = null;
1115
    
1116
    try {
1117
      // get the target node reference from the nodes list
1118
      CNode cn = D1Client.getCN();
1119
      List<Node> nodes = cn.listNodes().getNodeList();
1120
      
1121
      if ( nodes != null ) {
1122
        for (Node node : nodes) {
1123
            
1124
            for (Subject nodeSubject : node.getSubjectList()) {
1125
                
1126
                if ( nodeSubject.equals(targetNodeSubject) ) {
1127
                    targetNode = node.getIdentifier();
1128
                    logMetacat.debug("targetNode is : " + targetNode.getValue());
1129
                    break;
1130
                }
1131
            }
1132
            
1133
            if ( targetNode != null) { break; }
1134
        }
1135
        
1136
      } else {
1137
          String msg = "Couldn't get the node list from the CN";
1138
          logMetacat.debug(msg);
1139
          throw new ServiceFailure("4872", msg);
1140
          
1141
      }
1142
      
1143
      // can't find a node listed with the given subject
1144
      if ( targetNode == null ) {
1145
          String msg = "There is no Member Node registered with a node subject " +
1146
              "matching " + targetNodeSubject.getValue();
1147
          logMetacat.info(msg);
1148
          throw new NotAuthorized("4871", msg);
1149
          
1150
      }
1151
      
1152
      logMetacat.debug("Getting system metadata for identifier " + pid.getValue());
1153
      
1154
      sysmeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1155

    
1156
      if ( sysmeta != null ) {
1157
          
1158
          List<Replica> replicaList = sysmeta.getReplicaList();
1159
          
1160
          if ( replicaList != null ) {
1161
              
1162
              // find the replica with the status set to 'requested'
1163
              for (Replica replica : replicaList) {
1164
                  ReplicationStatus status = replica.getReplicationStatus();
1165
                  NodeReference listedNode = replica.getReplicaMemberNode();
1166
                  if ( listedNode != null && targetNode != null ) {
1167
                      logMetacat.debug("Comparing " + listedNode.getValue()
1168
                              + " to " + targetNode.getValue());
1169
                      
1170
                      if (listedNode.getValue().equals(targetNode.getValue())
1171
                              && status.equals(ReplicationStatus.REQUESTED)) {
1172
                          isAllowed = true;
1173
                          break;
1174

    
1175
                      }
1176
                  }
1177
              }
1178
          }
1179
          logMetacat.debug("The " + targetNode.getValue() + " is allowed " +
1180
              "to replicate: " + isAllowed + " for " + pid.getValue());
1181

    
1182
          
1183
      } else {
1184
          logMetacat.debug("System metadata for identifier " + pid.getValue() +
1185
          " is null.");          
1186
          throw new NotFound("4874", "Couldn't find an object identified by " + pid.getValue());
1187
          
1188
      }
1189

    
1190
    } catch (RuntimeException e) {
1191
    	  ServiceFailure sf = new ServiceFailure("4872", 
1192
                "Runtime Exception: Couldn't determine if node is allowed: " + 
1193
                e.getCause().getMessage());
1194
    	  sf.initCause(e);
1195
        throw sf;
1196
        
1197
    }
1198
      
1199
    return isAllowed;
1200
    
1201
  }
1202

    
1203
  /**
1204
   * Adds a new object to the Node, where the object is a science metadata object.
1205
   * 
1206
   * @param session - the Session object containing the credentials for the Subject
1207
   * @param pid - The object identifier to be created
1208
   * @param object - the object bytes
1209
   * @param sysmeta - the system metadata that describes the object  
1210
   * 
1211
   * @return pid - the object identifier created
1212
   * 
1213
   * @throws InvalidToken
1214
   * @throws ServiceFailure
1215
   * @throws NotAuthorized
1216
   * @throws IdentifierNotUnique
1217
   * @throws UnsupportedType
1218
   * @throws InsufficientResources
1219
   * @throws InvalidSystemMetadata
1220
   * @throws NotImplemented
1221
   * @throws InvalidRequest
1222
   */
1223
  public Identifier create(Session session, Identifier pid, InputStream object,
1224
    SystemMetadata sysmeta) 
1225
    throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, 
1226
    UnsupportedType, InsufficientResources, InvalidSystemMetadata, 
1227
    NotImplemented, InvalidRequest {
1228
                  
1229
      // The lock to be used for this identifier
1230
      Lock lock = null;
1231

    
1232
      try {
1233
          lock = HazelcastService.getInstance().getLock(pid.getValue());
1234
          // are we allowed?
1235
          boolean isAllowed = false;
1236
          isAllowed = isAdminAuthorized(session, pid, Permission.WRITE);
1237

    
1238
          // proceed if we're called by a CN
1239
          if ( isAllowed ) {
1240
              // create the coordinating node version of the document      
1241
              lock.lock();
1242
              logMetacat.debug("Locked identifier " + pid.getValue());
1243
              sysmeta.setSerialVersion(BigInteger.ONE);
1244
              sysmeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1245
              sysmeta.setArchived(false); // this is a create op, not update
1246
              
1247
              // the CN should have set the origin and authoritative member node fields
1248
              try {
1249
                  sysmeta.getOriginMemberNode().getValue();
1250
                  sysmeta.getAuthoritativeMemberNode().getValue();
1251
                  
1252
              } catch (NullPointerException npe) {
1253
                  throw new InvalidSystemMetadata("4896", 
1254
                      "Both the origin and authoritative member node identifiers need to be set.");
1255
                  
1256
              }
1257
              pid = super.create(session, pid, object, sysmeta);
1258

    
1259
          } else {
1260
              String msg = "The subject listed as " + session.getSubject().getValue() + 
1261
                  " isn't allowed to call create() on a Coordinating Node.";
1262
              logMetacat.info(msg);
1263
              throw new NotAuthorized("1100", msg);
1264
          }
1265
          
1266
      } catch (RuntimeException e) {
1267
          // Convert Hazelcast runtime exceptions to service failures
1268
          String msg = "There was a problem creating the object identified by " +
1269
              pid.getValue() + ". There error message was: " + e.getMessage();
1270
          throw new ServiceFailure("4893", msg);
1271
          
1272
      } finally {
1273
    	  if (lock != null) {
1274
	          lock.unlock();
1275
	          logMetacat.debug("Unlocked identifier " + pid.getValue());
1276
    	  }
1277
      }
1278
      
1279
      return pid;
1280

    
1281
  }
1282

    
1283
  /**
1284
   * Set access for a given object using the object identifier and a Subject
1285
   * under a given Session.
1286
   * 
1287
   * @param session - the Session object containing the credentials for the Subject
1288
   * @param pid - the object identifier for the given object to apply the policy
1289
   * @param policy - the access policy to be applied
1290
   * 
1291
   * @return true if the application of the policy succeeds
1292
   * @throws InvalidToken
1293
   * @throws ServiceFailure
1294
   * @throws NotFound
1295
   * @throws NotAuthorized
1296
   * @throws NotImplemented
1297
   * @throws InvalidRequest
1298
   */
1299
  public boolean setAccessPolicy(Session session, Identifier pid, 
1300
      AccessPolicy accessPolicy, long serialVersion) 
1301
      throws InvalidToken, ServiceFailure, NotFound, NotAuthorized, 
1302
      NotImplemented, InvalidRequest, VersionMismatch {
1303
      
1304
      // The lock to be used for this identifier
1305
      Lock lock = null;
1306
      SystemMetadata systemMetadata = null;
1307
      
1308
      boolean success = false;
1309
      
1310
      // get the subject
1311
      Subject subject = session.getSubject();
1312
      
1313
      // are we allowed to do this?
1314
      if (!isAuthorized(session, pid, Permission.CHANGE_PERMISSION)) {
1315
          throw new NotAuthorized("4420", "not allowed by "
1316
                  + subject.getValue() + " on " + pid.getValue());
1317
      }
1318
      
1319
      try {
1320
          lock = HazelcastService.getInstance().getLock(pid.getValue());
1321
          lock.lock();
1322
          logMetacat.debug("Locked identifier " + pid.getValue());
1323

    
1324
          try {
1325
              systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1326

    
1327
              if ( systemMetadata == null ) {
1328
                  throw new NotFound("4400", "Couldn't find an object identified by " + pid.getValue());
1329
                  
1330
              }
1331
              // does the request have the most current system metadata?
1332
              if ( systemMetadata.getSerialVersion().longValue() != serialVersion ) {
1333
                 String msg = "The requested system metadata version number " + 
1334
                     serialVersion + " differs from the current version at " +
1335
                     systemMetadata.getSerialVersion().longValue() +
1336
                     ". Please get the latest copy in order to modify it.";
1337
                 throw new VersionMismatch("4402", msg);
1338
                 
1339
              }
1340
              
1341
          } catch (RuntimeException e) {
1342
              // convert Hazelcast RuntimeException to NotFound
1343
              throw new NotFound("4400", "No record found for: " + pid);
1344
            
1345
          }
1346
              
1347
          // set the access policy
1348
          systemMetadata.setAccessPolicy(accessPolicy);
1349
          
1350
          // update the system metadata
1351
          try {
1352
              systemMetadata.setSerialVersion(systemMetadata.getSerialVersion().add(BigInteger.ONE));
1353
              systemMetadata.setDateSysMetadataModified(Calendar.getInstance().getTime());
1354
              HazelcastService.getInstance().getSystemMetadataMap().put(systemMetadata.getIdentifier(), systemMetadata);
1355
              notifyReplicaNodes(systemMetadata);
1356
              
1357
          } catch (RuntimeException e) {
1358
              // convert Hazelcast RuntimeException to ServiceFailure
1359
              throw new ServiceFailure("4430", e.getMessage());
1360
            
1361
          }
1362
          
1363
      } catch (RuntimeException e) {
1364
          throw new ServiceFailure("4430", e.getMessage());
1365
          
1366
      } finally {
1367
          lock.unlock();
1368
          logMetacat.debug("Unlocked identifier " + pid.getValue());
1369
        
1370
      }
1371

    
1372
    
1373
    // TODO: how do we know if the map was persisted?
1374
    success = true;
1375
    
1376
    return success;
1377
  }
1378

    
1379
  /**
1380
   * Full replacement of replication metadata in the system metadata for the 
1381
   * specified object, changes date system metadata modified
1382
   * 
1383
   * @param session - the Session object containing the credentials for the Subject
1384
   * @param pid - the object identifier for the given object to apply the policy
1385
   * @param replica - the replica to be updated
1386
   * @return
1387
   * @throws NotImplemented
1388
   * @throws NotAuthorized
1389
   * @throws ServiceFailure
1390
   * @throws InvalidRequest
1391
   * @throws NotFound
1392
   * @throws VersionMismatch
1393
   */
1394
  @Override
1395
  public boolean updateReplicationMetadata(Session session, Identifier pid,
1396
      Replica replica, long serialVersion) 
1397
      throws NotImplemented, NotAuthorized, ServiceFailure, InvalidRequest,
1398
      NotFound, VersionMismatch {
1399
      
1400
      // The lock to be used for this identifier
1401
      Lock lock = null;
1402
      
1403
      // get the subject
1404
      Subject subject = session.getSubject();
1405
      
1406
      // are we allowed to do this?
1407
      try {
1408

    
1409
          // what is the controlling permission?
1410
          if (!isAuthorized(session, pid, Permission.WRITE)) {
1411
              throw new NotAuthorized("4851", "not allowed by "
1412
                      + subject.getValue() + " on " + pid.getValue());
1413
          }
1414

    
1415
        
1416
      } catch (InvalidToken e) {
1417
          throw new NotAuthorized("4851", "not allowed by " + subject.getValue() + 
1418
                  " on " + pid.getValue());  
1419
          
1420
      }
1421

    
1422
      SystemMetadata systemMetadata = null;
1423
      try {
1424
          lock = HazelcastService.getInstance().getLock(pid.getValue());
1425
          lock.lock();
1426
          logMetacat.debug("Locked identifier " + pid.getValue());
1427

    
1428
          try {      
1429
              systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1430

    
1431
              // does the request have the most current system metadata?
1432
              if ( systemMetadata.getSerialVersion().longValue() != serialVersion ) {
1433
                 String msg = "The requested system metadata version number " + 
1434
                     serialVersion + " differs from the current version at " +
1435
                     systemMetadata.getSerialVersion().longValue() +
1436
                     ". Please get the latest copy in order to modify it.";
1437
                 throw new VersionMismatch("4855", msg);
1438
              }
1439
              
1440
          } catch (RuntimeException e) { // Catch is generic since HZ throws RuntimeException
1441
              throw new NotFound("4854", "No record found for: " + pid.getValue() +
1442
                  " : " + e.getMessage());
1443
            
1444
          }
1445
              
1446
          // set the status for the replica
1447
          List<Replica> replicas = systemMetadata.getReplicaList();
1448
          NodeReference replicaNode = replica.getReplicaMemberNode();
1449
          int index = 0;
1450
          for (Replica listedReplica: replicas) {
1451
              
1452
              // remove the replica that we are replacing
1453
              if ( replicaNode.getValue().equals(listedReplica.getReplicaMemberNode().getValue())) {
1454
                  replicas.remove(index);
1455
                  break;
1456
                  
1457
              }
1458
              index++;
1459
          }
1460
          
1461
          // add the new replica item
1462
          replicas.add(replica);
1463
          systemMetadata.setReplicaList(replicas);
1464
          
1465
          // update the metadata
1466
          try {
1467
              systemMetadata.setSerialVersion(systemMetadata.getSerialVersion().add(BigInteger.ONE));
1468
              systemMetadata.setDateSysMetadataModified(Calendar.getInstance().getTime());
1469
              HazelcastService.getInstance().getSystemMetadataMap().put(systemMetadata.getIdentifier(), systemMetadata);
1470
            
1471
          } catch (RuntimeException e) {
1472
              logMetacat.info("Unknown RuntimeException thrown: " + e.getCause().getMessage());
1473
              throw new ServiceFailure("4852", e.getMessage());
1474
          
1475
          }
1476
          
1477
      } catch (RuntimeException e) {
1478
          logMetacat.info("Unknown RuntimeException thrown: " + e.getCause().getMessage());
1479
          throw new ServiceFailure("4852", e.getMessage());
1480
      
1481
      } finally {
1482
          lock.unlock();
1483
          logMetacat.debug("Unlocked identifier " + pid.getValue());
1484
          
1485
      }
1486
    
1487
      return true;
1488
      
1489
  }
1490
  
1491
  /**
1492
   * 
1493
   */
1494
  @Override
1495
  public ObjectList listObjects(Session session, Date startTime, 
1496
      Date endTime, ObjectFormatIdentifier formatid, Boolean replicaStatus,
1497
      Integer start, Integer count)
1498
      throws InvalidRequest, InvalidToken, NotAuthorized, NotImplemented,
1499
      ServiceFailure {
1500
      
1501
      ObjectList objectList = null;
1502
        try {
1503
            objectList = IdentifierManager.getInstance().querySystemMetadata(startTime, endTime, formatid, replicaStatus, start, count);
1504
        } catch (Exception e) {
1505
            throw new ServiceFailure("1580", "Error querying system metadata: " + e.getMessage());
1506
        }
1507

    
1508
        return objectList;
1509
  }
1510

    
1511
  
1512
 	/**
1513
 	 * Returns a list of checksum algorithms that are supported by DataONE.
1514
 	 * @return cal  the list of checksum algorithms
1515
 	 * 
1516
 	 * @throws ServiceFailure
1517
 	 * @throws NotImplemented
1518
 	 */
1519
  @Override
1520
  public ChecksumAlgorithmList listChecksumAlgorithms()
1521
			throws ServiceFailure, NotImplemented {
1522
		ChecksumAlgorithmList cal = new ChecksumAlgorithmList();
1523
		cal.addAlgorithm("MD5");
1524
		cal.addAlgorithm("SHA-1");
1525
		return cal;
1526
		
1527
	}
1528

    
1529
  /**
1530
   * Notify replica Member Nodes of system metadata changes for a given pid
1531
   * 
1532
   * @param currentSystemMetadata - the up to date system metadata
1533
   */
1534
  public void notifyReplicaNodes(SystemMetadata currentSystemMetadata) {
1535
      
1536
      Session session = null;
1537
      List<Replica> replicaList = currentSystemMetadata.getReplicaList();
1538
      MNode mn = null;
1539
      NodeReference replicaNodeRef = null;
1540
      CNode cn = null;
1541
      NodeType nodeType = null;
1542
      List<Node> nodeList = null;
1543
      
1544
      try {
1545
          cn = D1Client.getCN();
1546
          nodeList = cn.listNodes().getNodeList();
1547
          
1548
      } catch (Exception e) { // handle BaseException and other I/O issues
1549
          
1550
          // swallow errors since the call is not critical
1551
          logMetacat.error("Can't inform MNs of system metadata changes due " +
1552
              "to communication issues with the CN: " + e.getMessage());
1553
          
1554
      }
1555
      
1556
      if ( replicaList != null ) {
1557
          
1558
          // iterate through the replicas and inform  MN replica nodes
1559
          for (Replica replica : replicaList) {
1560
              
1561
              replicaNodeRef = replica.getReplicaMemberNode();
1562
              try {
1563
                  if (nodeList != null) {
1564
                      // find the node type
1565
                      for (Node node : nodeList) {
1566
                          if (node.getIdentifier().getValue() == 
1567
                              replicaNodeRef.getValue()) {
1568
                              nodeType = node.getType();
1569
                              break;
1570
              
1571
                          }
1572
                      }
1573
                  }
1574
              
1575
                  // notify only MNs
1576
                  if (nodeType != null && nodeType == NodeType.MN) {
1577
                      mn = D1Client.getMN(replicaNodeRef);
1578
                      mn.systemMetadataChanged(session, 
1579
                          currentSystemMetadata.getIdentifier(), 
1580
                          currentSystemMetadata.getSerialVersion().longValue(),
1581
                          currentSystemMetadata.getDateSysMetadataModified());
1582
                  }
1583
              
1584
              } catch (Exception e) { // handle BaseException and other I/O issues
1585
              
1586
                  // swallow errors since the call is not critical
1587
                  logMetacat.error("Can't inform "
1588
                          + replicaNodeRef.getValue()
1589
                          + " of system metadata changes due "
1590
                          + "to communication issues with the CN: "
1591
                          + e.getMessage());
1592
              
1593
              }
1594
          }
1595
      }
1596
  }
1597
}
(1-1/5)