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

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

    
26
import java.io.File;
27
import java.io.FileNotFoundException;
28
import java.io.FileOutputStream;
29
import java.io.IOException;
30
import java.io.InputStream;
31
import java.io.OutputStream;
32
import java.sql.SQLException;
33
import java.util.ArrayList;
34
import java.util.Calendar;
35
import java.util.Date;
36
import java.util.Hashtable;
37
import java.util.List;
38
import java.util.Set;
39
import java.util.Timer;
40
import java.util.concurrent.locks.Lock;
41

    
42
import javax.servlet.http.HttpServletRequest;
43

    
44
import org.apache.commons.io.IOUtils;
45
import org.apache.log4j.Logger;
46
import org.dataone.client.v2.CNode;
47
import org.dataone.client.v2.itk.D1Client;
48
import org.dataone.client.v2.formats.ObjectFormatCache;
49
import org.dataone.service.exceptions.BaseException;
50
import org.dataone.service.exceptions.IdentifierNotUnique;
51
import org.dataone.service.exceptions.InsufficientResources;
52
import org.dataone.service.exceptions.InvalidRequest;
53
import org.dataone.service.exceptions.InvalidSystemMetadata;
54
import org.dataone.service.exceptions.InvalidToken;
55
import org.dataone.service.exceptions.NotAuthorized;
56
import org.dataone.service.exceptions.NotFound;
57
import org.dataone.service.exceptions.NotImplemented;
58
import org.dataone.service.exceptions.ServiceFailure;
59
import org.dataone.service.exceptions.UnsupportedType;
60
import org.dataone.service.types.v1.AccessRule;
61
import org.dataone.service.types.v1.DescribeResponse;
62
import org.dataone.service.types.v1.Group;
63
import org.dataone.service.types.v1.Identifier;
64
import org.dataone.service.types.v2.Log;
65
import org.dataone.service.types.v2.Node;
66
import org.dataone.service.types.v1.Event;
67
import org.dataone.service.types.v1.NodeReference;
68
import org.dataone.service.types.v1.NodeType;
69
import org.dataone.service.types.v2.ObjectFormat;
70
import org.dataone.service.types.v1.Permission;
71
import org.dataone.service.types.v1.Replica;
72
import org.dataone.service.types.v1.Session;
73
import org.dataone.service.types.v1.Subject;
74
import org.dataone.service.types.v2.SystemMetadata;
75
import org.dataone.service.types.v1.util.AuthUtils;
76
import org.dataone.service.types.v1.util.ChecksumUtil;
77
import org.dataone.service.util.Constants;
78

    
79
import edu.ucsb.nceas.metacat.AccessionNumberException;
80
import edu.ucsb.nceas.metacat.DocumentImpl;
81
import edu.ucsb.nceas.metacat.EventLog;
82
import edu.ucsb.nceas.metacat.IdentifierManager;
83
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
84
import edu.ucsb.nceas.metacat.MetacatHandler;
85
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
86
import edu.ucsb.nceas.metacat.database.DBConnection;
87
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
88
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService;
89
import edu.ucsb.nceas.metacat.index.MetacatSolrIndex;
90
import edu.ucsb.nceas.metacat.properties.PropertyService;
91
import edu.ucsb.nceas.metacat.replication.ForceReplicationHandler;
92
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
93

    
94
public abstract class D1NodeService {
95
    
96
  public static final String DELETEDMESSAGE = "The object with the PID has been deleted from the node.";
97
  
98
  private static Logger logMetacat = Logger.getLogger(D1NodeService.class);
99

    
100
  /** For logging the operations */
101
  protected HttpServletRequest request;
102
  
103
  /* reference to the metacat handler */
104
  protected MetacatHandler handler;
105
  
106
  /* parameters set in the incoming request */
107
  private Hashtable<String, String[]> params;
108
  
109
  /**
110
   * limit paged results sets to a configured maximum
111
   */
112
  protected static int MAXIMUM_DB_RECORD_COUNT = 7000;
113
  
114
  static {
115
		try {
116
			MAXIMUM_DB_RECORD_COUNT = Integer.valueOf(PropertyService.getProperty("database.webResultsetSize"));
117
		} catch (Exception e) {
118
			logMetacat.warn("Could not set MAXIMUM_DB_RECORD_COUNT", e);
119
		}
120
	}
121
  
122
  /**
123
   * out-of-band session object to be used when not passed in as a method parameter
124
   */
125
  protected Session session;
126

    
127
  /**
128
   * Constructor - used to set the metacatUrl from a subclass extending D1NodeService
129
   * 
130
   * @param metacatUrl - the URL of the metacat service, including the ending /d1
131
   */
132
  public D1NodeService(HttpServletRequest request) {
133
		this.request = request;
134
	}
135

    
136
  /**
137
   * retrieve the out-of-band session
138
   * @return
139
   */
140
  	public Session getSession() {
141
		return session;
142
	}
143
  	
144
  	/**
145
  	 * Set the out-of-band session
146
  	 * @param session
147
  	 */
148
	public void setSession(Session session) {
149
		this.session = session;
150
	}
151

    
152
  /**
153
   * This method provides a lighter weight mechanism than 
154
   * getSystemMetadata() for a client to determine basic 
155
   * properties of the referenced object.
156
   * 
157
   * @param session - the Session object containing the credentials for the Subject
158
   * @param pid - the identifier of the object to be described
159
   * 
160
   * @return describeResponse - A set of values providing a basic description 
161
   *                            of the object.
162
   * 
163
   * @throws InvalidToken
164
   * @throws ServiceFailure
165
   * @throws NotAuthorized
166
   * @throws NotFound
167
   * @throws NotImplemented
168
   * @throws InvalidRequest
169
   */
170
  public DescribeResponse describe(Session session, Identifier pid) 
171
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
172

    
173
    // get system metadata and construct the describe response
174
      SystemMetadata sysmeta = getSystemMetadata(session, pid);
175
      DescribeResponse describeResponse = 
176
      	new DescribeResponse(sysmeta.getFormatId(), sysmeta.getSize(), 
177
      			sysmeta.getDateSysMetadataModified(),
178
      			sysmeta.getChecksum(), sysmeta.getSerialVersion());
179

    
180
      return describeResponse;
181

    
182
  }
183
  
184
  /**
185
   * Deletes an object from the Member Node, where the object is either a 
186
   * data object or a science metadata object.
187
   * 
188
   * @param session - the Session object containing the credentials for the Subject
189
   * @param pid - The object identifier to be deleted
190
   * 
191
   * @return pid - the identifier of the object used for the deletion
192
   * 
193
   * @throws InvalidToken
194
   * @throws ServiceFailure
195
   * @throws NotAuthorized
196
   * @throws NotFound
197
   * @throws NotImplemented
198
   * @throws InvalidRequest
199
   */
200
  public Identifier delete(Session session, Identifier pid) 
201
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
202

    
203
      String localId = null;
204
      if (session == null) {
205
      	throw new InvalidToken("1330", "No session has been provided");
206
      }
207
      // just for logging purposes
208
      String username = session.getSubject().getValue();
209

    
210
      // do we have a valid pid?
211
      if (pid == null || pid.getValue().trim().equals("")) {
212
          throw new ServiceFailure("1350", "The provided identifier was invalid.");
213
      }
214

    
215
      // check for the existing identifier
216
      try {
217
          localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
218
      } catch (McdbDocNotFoundException e) {
219
          throw new NotFound("1340", "The object with the provided " + "identifier was not found.");
220
      }
221
      
222
      try {
223
          // delete the document, as admin
224
          DocumentImpl.delete(localId, null, null, null, true);
225
          EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, Event.DELETE.xmlValue());
226

    
227
          // archive it
228
          // DocumentImpl.delete() now sets this
229
          // see https://redmine.dataone.org/issues/3406
230
//          SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
231
//          sysMeta.setArchived(true);
232
//          sysMeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
233
//          HazelcastService.getInstance().getSystemMetadataMap().put(pid, sysMeta);
234
          
235
      } catch (McdbDocNotFoundException e) {
236
          throw new NotFound("1340", "The provided identifier was invalid.");
237

    
238
      } catch (SQLException e) {
239
          throw new ServiceFailure("1350", "There was a problem deleting the object." + "The error message was: " + e.getMessage());
240

    
241
      } catch (InsufficientKarmaException e) {
242
          if ( logMetacat.isDebugEnabled() ) {
243
              e.printStackTrace();
244
          }
245
          throw new NotAuthorized("1320", "The provided identity does not have " + "permission to DELETE objects on the Member Node.");
246
      
247
      } catch (Exception e) { // for some reason DocumentImpl throws a general Exception
248
          throw new ServiceFailure("1350", "There was a problem deleting the object." + "The error message was: " + e.getMessage());
249
      }
250

    
251
      return pid;
252
  }
253
  
254
  /**
255
   * Low level, "are you alive" operation. A valid ping response is 
256
   * indicated by a HTTP status of 200.
257
   * 
258
   * @return true if the service is alive
259
   * 
260
   * @throws NotImplemented
261
   * @throws ServiceFailure
262
   * @throws InsufficientResources
263
   */
264
  public Date ping() 
265
      throws NotImplemented, ServiceFailure, InsufficientResources {
266

    
267
      // test if we can get a database connection
268
      int serialNumber = -1;
269
      DBConnection dbConn = null;
270
      try {
271
          dbConn = DBConnectionPool.getDBConnection("MNodeService.ping");
272
          serialNumber = dbConn.getCheckOutSerialNumber();
273
      } catch (SQLException e) {
274
      	ServiceFailure sf = new ServiceFailure("", e.getMessage());
275
      	sf.initCause(e);
276
          throw sf;
277
      } finally {
278
          // Return the database connection
279
          DBConnectionPool.returnDBConnection(dbConn, serialNumber);
280
      }
281

    
282
      return Calendar.getInstance().getTime();
283
  }
284
  
285
  /**
286
   * Adds a new object to the Node, where the object is either a data 
287
   * object or a science metadata object. This method is called by clients 
288
   * to create new data objects on Member Nodes or internally for Coordinating
289
   * Nodes
290
   * 
291
   * @param session - the Session object containing the credentials for the Subject
292
   * @param pid - The object identifier to be created
293
   * @param object - the object bytes
294
   * @param sysmeta - the system metadata that describes the object  
295
   * 
296
   * @return pid - the object identifier created
297
   * 
298
   * @throws InvalidToken
299
   * @throws ServiceFailure
300
   * @throws NotAuthorized
301
   * @throws IdentifierNotUnique
302
   * @throws UnsupportedType
303
   * @throws InsufficientResources
304
   * @throws InvalidSystemMetadata
305
   * @throws NotImplemented
306
   * @throws InvalidRequest
307
   */
308
  public Identifier create(Session session, Identifier pid, InputStream object,
309
    SystemMetadata sysmeta) 
310
    throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, 
311
    UnsupportedType, InsufficientResources, InvalidSystemMetadata, 
312
    NotImplemented, InvalidRequest {
313

    
314
    Identifier resultPid = null;
315
    String localId = null;
316
    boolean allowed = false;
317
    
318
    // check for null session
319
    if (session == null) {
320
    	throw new InvalidToken("4894", "Session is required to WRITE to the Node.");
321
    }
322
    Subject subject = session.getSubject();
323

    
324
    Subject publicSubject = new Subject();
325
    publicSubject.setValue(Constants.SUBJECT_PUBLIC);
326
	// be sure the user is authenticated for create()
327
    if (subject == null || subject.getValue() == null || 
328
        subject.equals(publicSubject) ) {
329
      throw new NotAuthorized("1100", "The provided identity does not have " +
330
        "permission to WRITE to the Node.");
331
      
332
    }
333
    
334
    // verify the pid is valid format
335
    if (!isValidIdentifier(pid)) {
336
    	throw new InvalidRequest("1202", "The provided identifier is invalid.");
337
    }
338
    
339
    // verify that pid == SystemMetadata.getIdentifier()
340
    logMetacat.debug("Comparing pid|sysmeta_pid: " + 
341
      pid.getValue() + "|" + sysmeta.getIdentifier().getValue());
342
    if (!pid.getValue().equals(sysmeta.getIdentifier().getValue())) {
343
        throw new InvalidSystemMetadata("1180", 
344
            "The supplied system metadata is invalid. " +
345
            "The identifier " + pid.getValue() + " does not match identifier" +
346
            "in the system metadata identified by " +
347
            sysmeta.getIdentifier().getValue() + ".");
348
        
349
    }
350

    
351
    logMetacat.debug("Checking if identifier exists: " + pid.getValue());
352
    // Check that the identifier does not already exist
353
    boolean idExists = false;
354
    try {
355
        idExists = IdentifierManager.getInstance().identifierExists(pid.getValue());
356
    } catch (SQLException e) {
357
        throw new ServiceFailure("1190", 
358
                                "The requested identifier " + pid.getValue() +
359
                                " couldn't be determined if it is unique since : "+e.getMessage());
360
    }
361
    if (idExists) {
362
	    	throw new IdentifierNotUnique("1120", 
363
			          "The requested identifier " + pid.getValue() +
364
			          " is already used by another object and" +
365
			          "therefore can not be used for this object. Clients should choose" +
366
			          "a new identifier that is unique and retry the operation or " +
367
			          "use CN.reserveIdentifier() to reserve one.");
368
    	
369
    }
370
    
371
    // TODO: this probably needs to be refined more
372
    try {
373
      allowed = isAuthorized(session, pid, Permission.WRITE);
374
            
375
    } catch (NotFound e) {
376
      // The identifier doesn't exist, writing should be fine.
377
      allowed = true;
378
    }
379
    
380
    // verify checksum, only if we can reset the inputstream
381
    if (object.markSupported()) {
382
        logMetacat.debug("Checking checksum for: " + pid.getValue());
383
	    String checksumAlgorithm = sysmeta.getChecksum().getAlgorithm();
384
	    String checksumValue = sysmeta.getChecksum().getValue();
385
	    try {
386
			String computedChecksumValue = ChecksumUtil.checksum(object, checksumAlgorithm).getValue();
387
			// it's very important that we don't consume the stream
388
			object.reset();
389
			if (!computedChecksumValue.equals(checksumValue)) {
390
			    logMetacat.error("Checksum for " + pid.getValue() + " does not match system metadata, computed = " + computedChecksumValue );
391
				throw new InvalidSystemMetadata("4896", "Checksum given does not match that of the object");
392
			}
393
		} catch (Exception e) {
394
			String msg = "Error verifying checksum values";
395
	      	logMetacat.error(msg, e);
396
	        throw new ServiceFailure("1190", msg + ": " + e.getMessage());
397
		}
398
    } else {
399
    	logMetacat.warn("mark is not supported on the object's input stream - cannot verify checksum without consuming stream");
400
    }
401
    	
402
    // we have the go ahead
403
    if ( allowed ) {
404
      
405
        logMetacat.debug("Allowed to insert: " + pid.getValue());
406

    
407
      // Science metadata (XML) or science data object?
408
      // TODO: there are cases where certain object formats are science metadata
409
      // but are not XML (netCDF ...).  Handle this.
410
      if ( isScienceMetadata(sysmeta) ) {
411
        
412
        // CASE METADATA:
413
      	//String objectAsXML = "";
414
        try {
415
	        //objectAsXML = IOUtils.toString(object, "UTF-8");
416
	        localId = insertOrUpdateDocument(object,"UTF-8", pid, session, "insert");
417
	        //localId = im.getLocalId(pid.getValue());
418

    
419
        } catch (IOException e) {
420
        	String msg = "The Node is unable to create the object. " +
421
          "There was a problem converting the object to XML";
422
        	logMetacat.info(msg);
423
          throw new ServiceFailure("1190", msg + ": " + e.getMessage());
424

    
425
        }
426
                    
427
      } else {
428
	        
429
	      // DEFAULT CASE: DATA (needs to be checked and completed)
430
	      localId = insertDataObject(object, pid, session);
431
      }   
432
    
433
    }
434

    
435
    logMetacat.debug("Done inserting new object: " + pid.getValue());
436
    
437
    // save the sysmeta
438
    try {
439
    	// lock and unlock of the pid happens in the subclass
440
    	HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
441
    	// submit for indexing
442
        MetacatSolrIndex.getInstance().submit(sysmeta.getIdentifier(), sysmeta, null, true);
443
        
444
    } catch (Exception e) {
445
    	logMetacat.error("Problem creating system metadata: " + pid.getValue(), e);
446
        throw new ServiceFailure("1190", e.getMessage());
447
	}
448
    
449
    // setting the resulting identifier failed
450
    if (localId == null ) {
451
      throw new ServiceFailure("1190", "The Node is unable to create the object. ");
452
    }
453

    
454
    resultPid = pid;
455
    
456
    logMetacat.debug("create() complete for object: " + pid.getValue());
457

    
458
    return resultPid;
459
  }
460

    
461
  /**
462
   * Return the log records associated with a given event between the start and 
463
   * end dates listed given a particular Subject listed in the Session
464
   * 
465
   * @param session - the Session object containing the credentials for the Subject
466
   * @param fromDate - the start date of the desired log records
467
   * @param toDate - the end date of the desired log records
468
   * @param event - restrict log records of a specific event type
469
   * @param start - zero based offset from the first record in the 
470
   *                set of matching log records. Used to assist with 
471
   *                paging the response.
472
   * @param count - maximum number of log records to return in the response. 
473
   *                Used to assist with paging the response.
474
   * 
475
   * @return the desired log records
476
   * 
477
   * @throws InvalidToken
478
   * @throws ServiceFailure
479
   * @throws NotAuthorized
480
   * @throws InvalidRequest
481
   * @throws NotImplemented
482
   */
483
  public Log getLogRecords(Session session, Date fromDate, Date toDate, 
484
      String event, String pidFilter, Integer start, Integer count) throws InvalidToken, ServiceFailure,
485
      NotAuthorized, InvalidRequest, NotImplemented {
486

    
487
	  // only admin access to this method
488
	  // see https://redmine.dataone.org/issues/2855
489
	  if (!isAdminAuthorized(session)) {
490
		  throw new NotAuthorized("1460", "Only the CN or admin is allowed to harvest logs from this node");
491
	  }
492
	  
493
    IdentifierManager im = IdentifierManager.getInstance();
494
    EventLog el = EventLog.getInstance();
495
    if ( fromDate == null ) {
496
      logMetacat.debug("setting fromdate from null");
497
      fromDate = new Date(1);
498
    }
499
    if ( toDate == null ) {
500
      logMetacat.debug("setting todate from null");
501
      toDate = new Date();
502
    }
503

    
504
    if ( start == null ) {
505
    	start = 0;	
506
    }
507
    
508
    if ( count == null ) {
509
    	count = 1000;
510
    }
511
    
512
    // safeguard against large requests
513
    if (count > MAXIMUM_DB_RECORD_COUNT) {
514
    	count = MAXIMUM_DB_RECORD_COUNT;
515
    }
516

    
517
    String[] filterDocid = null;
518
    if (pidFilter != null) {
519
		try {
520
	      String localId = im.getLocalId(pidFilter);
521
	      filterDocid = new String[] {localId};
522
	    } catch (Exception ex) { 
523
	    	String msg = "Could not find localId for given pidFilter '" + pidFilter + "'";
524
	        logMetacat.warn(msg, ex);
525
	        //throw new InvalidRequest("1480", msg);
526
	    }
527
    }
528
    
529
    logMetacat.debug("fromDate: " + fromDate);
530
    logMetacat.debug("toDate: " + toDate);
531

    
532
    Log log = el.getD1Report(null, null, filterDocid, event,
533
        new java.sql.Timestamp(fromDate.getTime()),
534
        new java.sql.Timestamp(toDate.getTime()), false, start, count);
535
    
536
    logMetacat.info("getLogRecords");
537
    return log;
538
  }
539
    
540
  /**
541
   * Return the object identified by the given object identifier
542
   * 
543
   * @param session - the Session object containing the credentials for the Subject
544
   * @param pid - the object identifier for the given object
545
   * 
546
   * TODO: The D1 Authorization API doesn't provide information on which 
547
   * authentication system the Subject belongs to, and so it's not possible to
548
   * discern which Person or Group is a valid KNB LDAP DN.  Fix this.
549
   * 
550
   * @return inputStream - the input stream of the given object
551
   * 
552
   * @throws InvalidToken
553
   * @throws ServiceFailure
554
   * @throws NotAuthorized
555
   * @throws InvalidRequest
556
   * @throws NotImplemented
557
   */
558
  public InputStream get(Session session, Identifier pid) 
559
    throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
560
    NotImplemented {
561
    
562
    InputStream inputStream = null; // bytes to be returned
563
    handler = new MetacatHandler(new Timer());
564
    boolean allowed = false;
565
    String localId; // the metacat docid for the pid
566
    
567
    // get the local docid from Metacat
568
    try {
569
      localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
570
    
571
    } catch (McdbDocNotFoundException e) {
572
      throw new NotFound("1020", "The object specified by " + 
573
                         pid.getValue() +
574
                         " does not exist at this node.");
575
    }
576
    
577
    // check for authorization
578
    try {
579
		allowed = isAuthorized(session, pid, Permission.READ);
580
	} catch (InvalidRequest e) {
581
		throw new ServiceFailure("1030", e.getDescription());
582
	}
583
    
584
    // if the person is authorized, perform the read
585
    if (allowed) {
586
      try {
587
        inputStream = handler.read(localId);
588
      } catch (McdbDocNotFoundException de) {
589
          String error ="";
590
          if(EventLog.getInstance().isDeleted(localId)) {
591
                error=DELETEDMESSAGE;
592
          }
593
          throw new NotFound("1020", "The object specified by " + 
594
                           pid.getValue() +
595
                           " does not exist at this node. "+error);
596
      } catch (Exception e) {
597
        throw new ServiceFailure("1030", "The object specified by " + 
598
            pid.getValue() +
599
            " could not be returned due to error: " +
600
            e.getMessage()+". ");
601
      }
602
    }
603

    
604
    // if we fail to set the input stream
605
    if ( inputStream == null ) {
606
        String error ="";
607
        if(EventLog.getInstance().isDeleted(localId)) {
608
              error=DELETEDMESSAGE;
609
        }
610
        throw new NotFound("1020", "The object specified by " + 
611
                         pid.getValue() +
612
                         " does not exist at this node. "+error);
613
    }
614
    
615
	// log the read event
616
    String principal = Constants.SUBJECT_PUBLIC;
617
    if (session != null && session.getSubject() != null) {
618
    	principal = session.getSubject().getValue();
619
    }
620
    EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), principal, localId, "read");
621
    
622
    return inputStream;
623
  }
624

    
625
  /**
626
   * Return the system metadata for a given object
627
   * 
628
   * @param session - the Session object containing the credentials for the Subject
629
   * @param pid - the object identifier for the given object
630
   * 
631
   * @return inputStream - the input stream of the given system metadata object
632
   * 
633
   * @throws InvalidToken
634
   * @throws ServiceFailure
635
   * @throws NotAuthorized
636
   * @throws NotFound
637
   * @throws InvalidRequest
638
   * @throws NotImplemented
639
   */
640
    public SystemMetadata getSystemMetadata(Session session, Identifier pid)
641
        throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
642
        NotImplemented {
643

    
644
        boolean isAuthorized = false;
645
        SystemMetadata systemMetadata = null;
646
        List<Replica> replicaList = null;
647
        NodeReference replicaNodeRef = null;
648
        List<Node> nodeListBySubject = null;
649
        Subject subject = null;
650
        
651
        if (session != null ) {
652
            subject = session.getSubject();
653
        }
654
        
655
        // check normal authorization
656
        BaseException originalAuthorizationException = null;
657
        if (!isAuthorized) {
658
            try {
659
                isAuthorized = isAuthorized(session, pid, Permission.READ);
660

    
661
            } catch (InvalidRequest e) {
662
                throw new ServiceFailure("1090", e.getDescription());
663
            } catch (NotAuthorized nae) {
664
            	// catch this for later
665
            	originalAuthorizationException = nae;
666
			}
667
        }
668
        
669
        // get the system metadata first because we need the replica list for auth
670
        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
671
        
672
        // check the replica information to expand access to MNs that might need it
673
        if (!isAuthorized) {
674
        	
675
	        try {
676
	        	
677
	            // if MNs are listed as replicas, allow access
678
	            if ( systemMetadata != null ) {
679
	                replicaList = systemMetadata.getReplicaList();
680
	                // only check if there are in fact replicas listed
681
	                if ( replicaList != null ) {
682
	                    
683
	                    if ( subject != null ) {
684
	                        // get the list of nodes with a matching node subject
685
	                        try {
686
	                            nodeListBySubject = listNodesBySubject(session.getSubject());
687
	
688
	                        } catch (BaseException e) {
689
	                            // Unexpected error contacting the CN via D1Client
690
	                            String msg = "Caught an unexpected error while trying "
691
	                                    + "to potentially authorize system metadata access "
692
	                                    + "based on the session subject. The error was "
693
	                                    + e.getMessage();
694
	                            logMetacat.error(msg);
695
	                            if (logMetacat.isDebugEnabled()) {
696
	                                e.printStackTrace();
697
	
698
	                            }
699
	                            // isAuthorized is still false 
700
	                        }
701
	
702
	                    }
703
	                    if (nodeListBySubject != null) {
704
	                        // compare node ids to replica node ids
705
	                        outer: for (Replica replica : replicaList) {
706
	                            replicaNodeRef = replica.getReplicaMemberNode();
707
	
708
	                            for (Node node : nodeListBySubject) {
709
	                                if (node.getIdentifier().equals(replicaNodeRef)) {
710
	                                    // node id via session subject matches a replica node
711
	                                    isAuthorized = true;
712
	                                    break outer;
713
	                                }
714
	                            }
715
	                        }
716
	                    }
717
	                }
718
	            }
719
	            
720
	            // if we still aren't authorized, then we are done
721
	            if (!isAuthorized) {
722
	                throw new NotAuthorized("1400", Permission.READ
723
	                        + " not allowed on " + pid.getValue());
724
	            }
725

    
726
	        } catch (RuntimeException e) {
727
	        	e.printStackTrace();
728
	            // convert hazelcast RuntimeException to ServiceFailure
729
	            throw new ServiceFailure("1090", "Unexpected error getting system metadata for: " + 
730
	                pid.getValue());	
731
	        }
732
	        
733
        }
734
        
735
        // It wasn't in the map
736
        if ( systemMetadata == null ) {
737
            String error ="";
738
            String localId = null;
739
            try {
740
                localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
741
              
742
             } catch (Exception e) {
743
                logMetacat.warn("Couldn't find the local id for the pid "+pid.getValue());
744
            }
745
            
746
            if(localId != null && EventLog.getInstance().isDeleted(localId)) {
747
                error = DELETEDMESSAGE;
748
            } else if (localId == null && EventLog.getInstance().isDeleted(pid.getValue())) {
749
                error = DELETEDMESSAGE;
750
            }
751
            throw new NotFound("1420", "No record found for: " + pid.getValue()+". "+error);
752
        }
753
        
754
        return systemMetadata;
755
    }
756
     
757
    
758
    /**
759
     * Test if the specified session represents the authoritative member node for the
760
     * given object specified by the identifier. According the the DataONE documentation, 
761
     * the authoritative member node has all the rights of the *rightsHolder*.
762
     * @param session - the Session object containing the credentials for the Subject
763
     * @param pid - the Identifier of the data object
764
     * @return true if the session represents the authoritative mn.
765
     * @throws ServiceFailure 
766
     * @throws NotImplemented 
767
     */
768
    public boolean isAuthoritativeMNodeAdmin(Session session, Identifier pid) {
769
        boolean allowed = false;
770
        //check the parameters
771
        if(session == null) {
772
            logMetacat.debug("D1NodeService.isAuthoritativeMNodeAdmin - the session object is null and return false.");
773
            return allowed;
774
        } else if (pid == null || pid.getValue() == null || pid.getValue().trim().equals("")) {
775
            logMetacat.debug("D1NodeService.isAuthoritativeMNodeAdmin - the Identifier object is null (not being specified) and return false.");
776
            return allowed;
777
        }
778
        
779
        //Get the subject from the session
780
        Subject subject = session.getSubject();
781
        if(subject != null) {
782
            //Get the authoritative member node info from the system metadata
783
            SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
784
            if(sysMeta != null) {
785
                NodeReference authoritativeMNode = sysMeta.getAuthoritativeMemberNode();
786
                if(authoritativeMNode != null) {
787
                        CNode cn = null;
788
                        try {
789
                            cn = D1Client.getCN();
790
                        } catch (BaseException e) {
791
                            logMetacat.error("D1NodeService.isAuthoritativeMNodeAdmin - couldn't connect to the CN since "+
792
                                            e.getDescription()+ ". The false value will be returned for the AuthoritativeMNodeAdmin.");
793
                            return allowed;
794
                        }
795
                        
796
                        if(cn != null) {
797
                            List<Node> nodes = null;
798
                            try {
799
                                nodes = cn.listNodes().getNodeList();
800
                            } catch (NotImplemented e) {
801
                                logMetacat.error("D1NodeService.isAuthoritativeMNodeAdmin - couldn't get the member nodes list from the CN since "+e.getDescription()+ 
802
                                                ". The false value will be returned for the AuthoritativeMNodeAdmin.");
803
                                return allowed;
804
                            } catch (ServiceFailure ee) {
805
                                logMetacat.error("D1NodeService.isAuthoritativeMNodeAdmin - couldn't get the member nodes list from the CN since "+ee.getDescription()+ 
806
                                                ". The false value will be returned for the AuthoritativeMNodeAdmin.");
807
                                return allowed;
808
                            }
809
                            if(nodes != null) {
810
                                for(Node node : nodes) {
811
                                    //find the authoritative node and get its subjects
812
                                    if (node.getType() == NodeType.MN && node.getIdentifier() != null && node.getIdentifier().equals(authoritativeMNode)) {
813
                                        List<Subject> nodeSubjects = node.getSubjectList();
814
                                        if(nodeSubjects != null) {
815
                                            // check if the session subject is in the node subject list
816
                                            for (Subject nodeSubject : nodeSubjects) {
817
                                                logMetacat.debug("D1NodeService.isAuthoritativeMNodeAdmin(), comparing subjects: " +
818
                                                    nodeSubject.getValue() + " and " + subject.getValue());
819
                                                if ( nodeSubject != null && nodeSubject.equals(subject) ) {
820
                                                    allowed = true; // subject of session == target node subject
821
                                                    break;
822
                                                }
823
                                            }              
824
                                        }
825
                                      
826
                                    }
827
                                }
828
                            }
829
                        }
830
                }
831
            }
832
        }
833
        return allowed;
834
    }
835
    
836
    
837
  /**
838
   * Test if the user identified by the provided token has administrative authorization 
839
   * 
840
   * @param session - the Session object containing the credentials for the Subject
841
   * 
842
   * @return true if the user is admin
843
   * 
844
   * @throws ServiceFailure
845
   * @throws InvalidToken
846
   * @throws NotFound
847
   * @throws NotAuthorized
848
   * @throws NotImplemented
849
   */
850
  public boolean isAdminAuthorized(Session session) 
851
      throws ServiceFailure, InvalidToken, NotAuthorized,
852
      NotImplemented {
853

    
854
      boolean allowed = false;
855
      
856
      // must have a session in order to check admin 
857
      if (session == null) {
858
         logMetacat.debug("In isAdminAuthorized(), session is null ");
859
         return false;
860
      }
861
      
862
      logMetacat.debug("In isAdminAuthorized(), checking CN or MN authorization for " +
863
           session.getSubject().getValue());
864
      
865
      // check if this is the node calling itself (MN)
866
      allowed = isNodeAdmin(session);
867
      
868
      // check the CN list
869
      if (!allowed) {
870
	      List<Node> nodes = null;
871

    
872
    	  try {
873
		      // are we allowed to do this? only CNs are allowed
874
		      CNode cn = D1Client.getCN();
875
		      nodes = cn.listNodes().getNodeList();
876
    	  }
877
	      catch (Throwable e) {
878
	    	  logMetacat.warn(e.getMessage());
879
	    	  return false;  
880
	      }
881
		      
882
	      if ( nodes == null ) {
883
	    	  return false;
884
	          //throw new ServiceFailure("4852", "Couldn't get node list.");
885
	      }
886
	      
887
	      // find the node in the node list
888
	      for ( Node node : nodes ) {
889
	          
890
	          NodeReference nodeReference = node.getIdentifier();
891
	          logMetacat.debug("In isAdminAuthorized(), Node reference is: " + nodeReference.getValue());
892
	          
893
	          Subject subject = session.getSubject();
894
	          
895
	          if (node.getType() == NodeType.CN) {
896
	              List<Subject> nodeSubjects = node.getSubjectList();
897
	              
898
	              // check if the session subject is in the node subject list
899
	              for (Subject nodeSubject : nodeSubjects) {
900
	                  logMetacat.debug("In isAdminAuthorized(), comparing subjects: " +
901
	                      nodeSubject.getValue() + " and " + subject.getValue());
902
	                  if ( nodeSubject.equals(subject) ) {
903
	                      allowed = true; // subject of session == target node subject
904
	                      break;
905
	                      
906
	                  }
907
	              }              
908
	          }
909
	      }
910
      }
911
      
912
      return allowed;
913
  }
914
  
915
  /**
916
   * Test if the user identified by the provided token has administrative authorization 
917
   * on this node because they are calling themselves
918
   * 
919
   * @param session - the Session object containing the credentials for the Subject
920
   * 
921
   * @return true if the user is this node
922
   * @throws ServiceFailure 
923
   * @throws NotImplemented 
924
   */
925
  public boolean isNodeAdmin(Session session) throws NotImplemented, ServiceFailure {
926

    
927
      boolean allowed = false;
928
      
929
      // must have a session in order to check admin 
930
      if (session == null) {
931
         logMetacat.debug("In isNodeAdmin(), session is null ");
932
         return false;
933
      }
934
      
935
      logMetacat.debug("In isNodeAdmin(), MN authorization for " +
936
           session.getSubject().getValue());
937
      
938
      Node node = MNodeService.getInstance(request).getCapabilities();
939
      NodeReference nodeReference = node.getIdentifier();
940
      logMetacat.debug("In isNodeAdmin(), Node reference is: " + nodeReference.getValue());
941
      
942
      Subject subject = session.getSubject();
943
      
944
      if (node.getType() == NodeType.MN) {
945
          List<Subject> nodeSubjects = node.getSubjectList();
946
          
947
          // check if the session subject is in the node subject list
948
          for (Subject nodeSubject : nodeSubjects) {
949
              logMetacat.debug("In isNodeAdmin(), comparing subjects: " +
950
                  nodeSubject.getValue() + " and " + subject.getValue());
951
              if ( nodeSubject.equals(subject) ) {
952
                  allowed = true; // subject of session == this node's subect
953
                  break;
954
              }
955
          }              
956
      }
957
      
958
      return allowed;
959
  }
960
  
961
  /**
962
   * Test if the user identified by the provided token has authorization 
963
   * for the operation on the specified object.
964
   * 
965
   * @param session - the Session object containing the credentials for the Subject
966
   * @param pid - The identifer of the resource for which access is being checked
967
   * @param operation - The type of operation which is being requested for the given pid
968
   *
969
   * @return true if the operation is allowed
970
   * 
971
   * @throws ServiceFailure
972
   * @throws InvalidToken
973
   * @throws NotFound
974
   * @throws NotAuthorized
975
   * @throws NotImplemented
976
   * @throws InvalidRequest
977
   */
978
  public boolean isAuthorized(Session session, Identifier pid, Permission permission)
979
    throws ServiceFailure, InvalidToken, NotFound, NotAuthorized,
980
    NotImplemented, InvalidRequest {
981

    
982
    boolean allowed = false;
983
    
984
    if (permission == null) {
985
    	throw new InvalidRequest("1761", "Permission was not provided or is invalid");
986
    }
987
    
988
    // permissions are hierarchical
989
    List<Permission> expandedPermissions = null;
990
    
991
    // always allow CN access
992
    if ( isAdminAuthorized(session) ) {
993
        allowed = true;
994
        return allowed;
995
        
996
    }
997
    
998
    // the authoritative member node of the pid always has the access as well.
999
    if (isAuthoritativeMNodeAdmin(session, pid)) {
1000
        allowed = true;
1001
        return allowed;
1002
    }
1003
    
1004
    // get the subject[s] from the session
1005
	//defer to the shared util for recursively compiling the subjects	
1006
	Set<Subject> subjects = AuthUtils.authorizedClientSubjects(session);
1007
    
1008
	// track the identities we have checked against
1009
	StringBuffer includedSubjects = new StringBuffer();
1010
    	
1011
    // get the system metadata
1012
    String pidStr = pid.getValue();
1013
    SystemMetadata systemMetadata = null;
1014
    try {
1015
        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1016

    
1017
    } catch (Exception e) {
1018
        // convert Hazelcast RuntimeException to NotFound
1019
        logMetacat.error("An error occurred while getting system metadata for identifier " +
1020
            pid.getValue() + ". The error message was: " + e.getMessage());
1021
        throw new NotFound("1800", "No record found for " + pidStr);
1022
        
1023
    } 
1024
    
1025
    // throw not found if it was not found
1026
    if (systemMetadata == null) {
1027
        String localId = null;
1028
        String error = "No system metadata could be found for given PID: " + pidStr;
1029
        try {
1030
            localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1031
          
1032
         } catch (Exception e) {
1033
            logMetacat.warn("Couldn't find the local id for the pid "+pidStr);
1034
        }
1035
        
1036
        if(localId != null && EventLog.getInstance().isDeleted(localId)) {
1037
            error = error + ". "+DELETEDMESSAGE;
1038
        } else if (localId == null && EventLog.getInstance().isDeleted(pid.getValue())) {
1039
            error = error + ". "+DELETEDMESSAGE;
1040
        }
1041
        throw new NotFound("1800", error);
1042
    }
1043
	    
1044
    // do we own it?
1045
    for (Subject s: subjects) {
1046
      logMetacat.debug("Comparing \t" + 
1047
                       systemMetadata.getRightsHolder().getValue() +
1048
                       " \tagainst \t" + s.getValue());
1049
      	includedSubjects.append(s.getValue() + "; ");
1050
    	allowed = systemMetadata.getRightsHolder().equals(s);
1051
    	if (allowed) {
1052
    		return allowed;
1053
    	}
1054
    }    
1055
    
1056
    // otherwise check the access rules
1057
    try {
1058
	    List<AccessRule> allows = systemMetadata.getAccessPolicy().getAllowList();
1059
	    search: // label break
1060
	    for (AccessRule accessRule: allows) {
1061
	      for (Subject s: subjects) {
1062
	        logMetacat.debug("Checking allow access rule for subject: " + s.getValue());
1063
	        if (accessRule.getSubjectList().contains(s)) {
1064
	        	logMetacat.debug("Access rule contains subject: " + s.getValue());
1065
	        	for (Permission p: accessRule.getPermissionList()) {
1066
		        	logMetacat.debug("Checking permission: " + p.xmlValue());
1067
	        		expandedPermissions = expandPermissions(p);
1068
	        		allowed = expandedPermissions.contains(permission);
1069
	        		if (allowed) {
1070
			        	logMetacat.info("Permission granted: " + p.xmlValue() + " to " + s.getValue());
1071
	        			break search; //label break
1072
	        		}
1073
	        	}
1074
        		
1075
	        }
1076
	      }
1077
	    }
1078
    } catch (Exception e) {
1079
    	// catch all for errors - safe side should be to deny the access
1080
    	logMetacat.error("Problem checking authorization - defaulting to deny", e);
1081
		allowed = false;
1082
	  
1083
    }
1084
    
1085
    // throw or return?
1086
    if (!allowed) {
1087
      throw new NotAuthorized("1820", permission + " not allowed on " + pidStr + " for subject[s]: " + includedSubjects.toString() );
1088
    }
1089
    
1090
    return allowed;
1091
    
1092
  }
1093
  
1094
  /*
1095
   * parse a logEntry and get the relevant field from it
1096
   * 
1097
   * @param fieldname
1098
   * @param entry
1099
   * @return
1100
   */
1101
  private String getLogEntryField(String fieldname, String entry) {
1102
    String begin = "<" + fieldname + ">";
1103
    String end = "</" + fieldname + ">";
1104
    // logMetacat.debug("looking for " + begin + " and " + end +
1105
    // " in entry " + entry);
1106
    String s = entry.substring(entry.indexOf(begin) + begin.length(), entry
1107
        .indexOf(end));
1108
    logMetacat.debug("entry " + fieldname + " : " + s);
1109
    return s;
1110
  }
1111

    
1112
  /** 
1113
   * Determine if a given object should be treated as an XML science metadata
1114
   * object. 
1115
   * 
1116
   * @param sysmeta - the SystemMetadata describing the object
1117
   * @return true if the object should be treated as science metadata
1118
   */
1119
  public static boolean isScienceMetadata(SystemMetadata sysmeta) {
1120
    
1121
    ObjectFormat objectFormat = null;
1122
    boolean isScienceMetadata = false;
1123
    
1124
    try {
1125
      objectFormat = ObjectFormatCache.getInstance().getFormat(sysmeta.getFormatId());
1126
      if ( objectFormat.getFormatType().equals("METADATA") ) {
1127
      	isScienceMetadata = true;
1128
      	
1129
      }
1130
      
1131
       
1132
    } catch (ServiceFailure e) {
1133
      logMetacat.debug("There was a problem determining if the object identified by" + 
1134
          sysmeta.getIdentifier().getValue() + 
1135
          " is science metadata: " + e.getMessage());
1136
    
1137
    } catch (NotFound e) {
1138
      logMetacat.debug("There was a problem determining if the object identified by" + 
1139
          sysmeta.getIdentifier().getValue() + 
1140
          " is science metadata: " + e.getMessage());
1141
    
1142
    }
1143
    
1144
    return isScienceMetadata;
1145

    
1146
  }
1147
  
1148
  /**
1149
   * Check fro whitespace in the given pid.
1150
   * null pids are also invalid by default
1151
   * @param pid
1152
   * @return
1153
   */
1154
  public static boolean isValidIdentifier(Identifier pid) {
1155
	  if (pid != null && pid.getValue() != null && pid.getValue().length() > 0) {
1156
		  return !pid.getValue().matches(".*\\s+.*");
1157
	  } 
1158
	  return false;
1159
  }
1160
  
1161
  
1162
  /**
1163
   * Insert or update an XML document into Metacat
1164
   * 
1165
   * @param xml - the XML document to insert or update
1166
   * @param pid - the identifier to be used for the resulting object
1167
   * 
1168
   * @return localId - the resulting docid of the document created or updated
1169
   * 
1170
   */
1171
  public String insertOrUpdateDocument(InputStream xml, String encoding,  Identifier pid, 
1172
    Session session, String insertOrUpdate) 
1173
    throws ServiceFailure, IOException {
1174
    
1175
  	logMetacat.debug("Starting to insert xml document...");
1176
    IdentifierManager im = IdentifierManager.getInstance();
1177

    
1178
    // generate pid/localId pair for sysmeta
1179
    String localId = null;
1180
    byte[] xmlBytes  = IOUtils.toByteArray(xml);
1181
    String xmlStr = new String(xmlBytes, encoding);
1182
    if(insertOrUpdate.equals("insert")) {
1183
      localId = im.generateLocalId(pid.getValue(), 1);
1184
      
1185
    } else {
1186
      //localid should already exist in the identifier table, so just find it
1187
      try {
1188
        logMetacat.debug("Updating pid " + pid.getValue());
1189
        logMetacat.debug("looking in identifier table for pid " + pid.getValue());
1190
        
1191
        localId = im.getLocalId(pid.getValue());
1192
        
1193
        logMetacat.debug("localId: " + localId);
1194
        //increment the revision
1195
        String docid = localId.substring(0, localId.lastIndexOf("."));
1196
        String revS = localId.substring(localId.lastIndexOf(".") + 1, localId.length());
1197
        int rev = new Integer(revS).intValue();
1198
        rev++;
1199
        docid = docid + "." + rev;
1200
        localId = docid;
1201
        logMetacat.debug("incremented localId: " + localId);
1202
      
1203
      } catch(McdbDocNotFoundException e) {
1204
        throw new ServiceFailure("1030", "D1NodeService.insertOrUpdateDocument(): " +
1205
            "pid " + pid.getValue() + 
1206
            " should have been in the identifier table, but it wasn't: " + 
1207
            e.getMessage());
1208
      
1209
      }
1210
      
1211
    }
1212

    
1213
    params = new Hashtable<String, String[]>();
1214
    String[] action = new String[1];
1215
    action[0] = insertOrUpdate;
1216
    params.put("action", action);
1217
    String[] docid = new String[1];
1218
    docid[0] = localId;
1219
    params.put("docid", docid);
1220
    String[] doctext = new String[1];
1221
    doctext[0] = xmlStr;
1222
    params.put("doctext", doctext);
1223
    
1224
    String username = Constants.SUBJECT_PUBLIC;
1225
    String[] groupnames = null;
1226
    if (session != null ) {
1227
    	username = session.getSubject().getValue();
1228
    	if (session.getSubjectInfo() != null) {
1229
    		List<Group> groupList = session.getSubjectInfo().getGroupList();
1230
    		if (groupList != null) {
1231
    			groupnames = new String[groupList.size()];
1232
    			for (int i = 0; i < groupList.size(); i++ ) {
1233
    				groupnames[i] = groupList.get(i).getGroupName();
1234
    			}
1235
    		}
1236
    	}
1237
    }
1238
    
1239
    // do the insert or update action
1240
    handler = new MetacatHandler(new Timer());
1241
    String result = handler.handleInsertOrUpdateAction(request.getRemoteAddr(), request.getHeader("User-Agent"), null, 
1242
                        null, params, username, groupnames, false, false, xmlBytes);
1243
    
1244
    if(result.indexOf("<error>") != -1) {
1245
    	String detailCode = "";
1246
    	if ( insertOrUpdate.equals("insert") ) {
1247
    		// make sure to remove the mapping so that subsequent attempts do not fail with IdentifierNotUnique
1248
    		im.removeMapping(pid.getValue(), localId);
1249
    		detailCode = "1190";
1250
    		
1251
    	} else if ( insertOrUpdate.equals("update") ) {
1252
    		detailCode = "1310";
1253
    		
1254
    	}
1255
        throw new ServiceFailure(detailCode, 
1256
          "Error inserting or updating document: " + result);
1257
    }
1258
    logMetacat.debug("Finsished inserting xml document with id " + localId);
1259
    
1260
    return localId;
1261
  }
1262
  
1263
  /**
1264
   * Insert a data document
1265
   * 
1266
   * @param object
1267
   * @param pid
1268
   * @param sessionData
1269
   * @throws ServiceFailure
1270
   * @returns localId of the data object inserted
1271
   */
1272
  public String insertDataObject(InputStream object, Identifier pid, 
1273
          Session session) throws ServiceFailure {
1274
      
1275
    String username = Constants.SUBJECT_PUBLIC;
1276
    String[] groupnames = null;
1277
    if (session != null ) {
1278
    	username = session.getSubject().getValue();
1279
    	if (session.getSubjectInfo() != null) {
1280
    		List<Group> groupList = session.getSubjectInfo().getGroupList();
1281
    		if (groupList != null) {
1282
    			groupnames = new String[groupList.size()];
1283
    			for (int i = 0; i < groupList.size(); i++ ) {
1284
    				groupnames[i] = groupList.get(i).getGroupName();
1285
    			}
1286
    		}
1287
    	}
1288
    }
1289
  
1290
    // generate pid/localId pair for object
1291
    logMetacat.debug("Generating a pid/localId mapping");
1292
    IdentifierManager im = IdentifierManager.getInstance();
1293
    String localId = im.generateLocalId(pid.getValue(), 1);
1294
  
1295
    // Save the data file to disk using "localId" as the name
1296
    String datafilepath = null;
1297
	try {
1298
		datafilepath = PropertyService.getProperty("application.datafilepath");
1299
	} catch (PropertyNotFoundException e) {
1300
		ServiceFailure sf = new ServiceFailure("1190", "Lookup data file path" + e.getMessage());
1301
		sf.initCause(e);
1302
		throw sf;
1303
	}
1304
    boolean locked = false;
1305
	try {
1306
		locked = DocumentImpl.getDataFileLockGrant(localId);
1307
	} catch (Exception e) {
1308
		ServiceFailure sf = new ServiceFailure("1190", "Could not lock file for writing:" + e.getMessage());
1309
		sf.initCause(e);
1310
		throw sf;
1311
	}
1312

    
1313
    logMetacat.debug("Case DATA: starting to write to disk.");
1314
	if (locked) {
1315

    
1316
          File dataDirectory = new File(datafilepath);
1317
          dataDirectory.mkdirs();
1318
  
1319
          File newFile = writeStreamToFile(dataDirectory, localId, object);
1320
  
1321
          // TODO: Check that the file size matches SystemMetadata
1322
          // long size = newFile.length();
1323
          // if (size == 0) {
1324
          //     throw new IOException("Uploaded file is 0 bytes!");
1325
          // }
1326
  
1327
          // Register the file in the database (which generates an exception
1328
          // if the localId is not acceptable or other untoward things happen
1329
          try {
1330
            logMetacat.debug("Registering document...");
1331
            DocumentImpl.registerDocument(localId, "BIN", localId,
1332
                    username, groupnames);
1333
            logMetacat.debug("Registration step completed.");
1334
            
1335
          } catch (SQLException e) {
1336
            //newFile.delete();
1337
            logMetacat.debug("SQLE: " + e.getMessage());
1338
            e.printStackTrace(System.out);
1339
            throw new ServiceFailure("1190", "Registration failed: " + 
1340
            		e.getMessage());
1341
            
1342
          } catch (AccessionNumberException e) {
1343
            //newFile.delete();
1344
            logMetacat.debug("ANE: " + e.getMessage());
1345
            e.printStackTrace(System.out);
1346
            throw new ServiceFailure("1190", "Registration failed: " + 
1347
            	e.getMessage());
1348
            
1349
          } catch (Exception e) {
1350
            //newFile.delete();
1351
            logMetacat.debug("Exception: " + e.getMessage());
1352
            e.printStackTrace(System.out);
1353
            throw new ServiceFailure("1190", "Registration failed: " + 
1354
            	e.getMessage());
1355
          }
1356
  
1357
          logMetacat.debug("Logging the creation event.");
1358
          EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, "create");
1359
  
1360
          // Schedule replication for this data file, the "insert" action is important here!
1361
          logMetacat.debug("Scheduling replication.");
1362
          ForceReplicationHandler frh = new ForceReplicationHandler(localId, "insert", false, null);
1363
      }
1364
      
1365
      return localId;
1366
    
1367
  }
1368

    
1369
  /**
1370
   * Insert a systemMetadata document and return its localId
1371
   */
1372
  public void insertSystemMetadata(SystemMetadata sysmeta) 
1373
      throws ServiceFailure {
1374
      
1375
  	  logMetacat.debug("Starting to insert SystemMetadata...");
1376
      sysmeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1377
      logMetacat.debug("Inserting new system metadata with modified date " + 
1378
          sysmeta.getDateSysMetadataModified());
1379
      
1380
      //insert the system metadata
1381
      try {
1382
        // note: the calling subclass handles the map hazelcast lock/unlock
1383
      	HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
1384
      	// submit for indexing
1385
        MetacatSolrIndex.getInstance().submit(sysmeta.getIdentifier(), sysmeta, null, true);
1386
      } catch (Exception e) {
1387
          throw new ServiceFailure("1190", e.getMessage());
1388
          
1389
	    }  
1390
  }
1391

    
1392
  /**
1393
   * Update a systemMetadata document
1394
   * 
1395
   * @param sysMeta - the system metadata object in the system to update
1396
   */
1397
    protected void updateSystemMetadata(SystemMetadata sysMeta)
1398
        throws ServiceFailure {
1399

    
1400
        logMetacat.debug("D1NodeService.updateSystemMetadata() called.");
1401
        sysMeta.setDateSysMetadataModified(new Date());
1402
        try {
1403
            HazelcastService.getInstance().getSystemMetadataMap().lock(sysMeta.getIdentifier());
1404
            HazelcastService.getInstance().getSystemMetadataMap().put(sysMeta.getIdentifier(), sysMeta);
1405
            // submit for indexing
1406
            MetacatSolrIndex.getInstance().submit(sysMeta.getIdentifier(), sysMeta, null, true);
1407
        } catch (Exception e) {
1408
            throw new ServiceFailure("4862", e.getMessage());
1409

    
1410
        } finally {
1411
            HazelcastService.getInstance().getSystemMetadataMap().unlock(sysMeta.getIdentifier());
1412

    
1413
        }
1414

    
1415
    }
1416
    
1417
	public boolean updateSystemMetadata(Session session, Identifier pid,
1418
			SystemMetadata sysmeta) throws NotImplemented, NotAuthorized,
1419
			ServiceFailure, InvalidRequest, InvalidSystemMetadata, InvalidToken {
1420
		
1421
		// The lock to be used for this identifier
1422
      Lock lock = null;
1423

    
1424
      // TODO: control who can call this?
1425
      if (session == null) {
1426
          //TODO: many of the thrown exceptions do not use the correct error codes
1427
          //check these against the docs and correct them
1428
          throw new NotAuthorized("4861", "No Session - could not authorize for registration." +
1429
                  "  If you are not logged in, please do so and retry the request.");
1430
      }
1431
      
1432
      // verify that guid == SystemMetadata.getIdentifier()
1433
      logMetacat.debug("Comparing guid|sysmeta_guid: " + pid.getValue() + 
1434
          "|" + sysmeta.getIdentifier().getValue());
1435
      
1436
      if (!pid.getValue().equals(sysmeta.getIdentifier().getValue())) {
1437
          throw new InvalidRequest("4863", 
1438
              "The identifier in method call (" + pid.getValue() + 
1439
              ") does not match identifier in system metadata (" +
1440
              sysmeta.getIdentifier().getValue() + ").");
1441
      }
1442

    
1443
      // do the actual update
1444
      this.updateSystemMetadata(sysmeta);
1445
      
1446
      try {
1447
    	  String localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1448
    	  EventLog.getInstance().log(request.getRemoteAddr(), 
1449
    	          request.getHeader("User-Agent"), session.getSubject().getValue(), 
1450
    	          localId, "updateSystemMetadata");
1451
      } catch (McdbDocNotFoundException e) {
1452
    	  // do nothing, no localId to log with
1453
    	  logMetacat.warn("Could not log 'updateSystemMetadata' event because no localId was found for pid: " + pid.getValue());
1454
      }
1455
      
1456
      return true;
1457
	}
1458
  
1459
  /**
1460
   * Given a Permission, returns a list of all permissions that it encompasses
1461
   * Permissions are hierarchical so that WRITE also allows READ.
1462
   * @param permission
1463
   * @return list of included Permissions for the given permission
1464
   */
1465
  protected List<Permission> expandPermissions(Permission permission) {
1466
	  	List<Permission> expandedPermissions = new ArrayList<Permission>();
1467
	    if (permission.equals(Permission.READ)) {
1468
	    	expandedPermissions.add(Permission.READ);
1469
	    }
1470
	    if (permission.equals(Permission.WRITE)) {
1471
	    	expandedPermissions.add(Permission.READ);
1472
	    	expandedPermissions.add(Permission.WRITE);
1473
	    }
1474
	    if (permission.equals(Permission.CHANGE_PERMISSION)) {
1475
	    	expandedPermissions.add(Permission.READ);
1476
	    	expandedPermissions.add(Permission.WRITE);
1477
	    	expandedPermissions.add(Permission.CHANGE_PERMISSION);
1478
	    }
1479
	    return expandedPermissions;
1480
  }
1481

    
1482
  /*
1483
   * Write a stream to a file
1484
   * 
1485
   * @param dir - the directory to write to
1486
   * @param fileName - the file name to write to
1487
   * @param data - the object bytes as an input stream
1488
   * 
1489
   * @return newFile - the new file created
1490
   * 
1491
   * @throws ServiceFailure
1492
   */
1493
  private File writeStreamToFile(File dir, String fileName, InputStream data) 
1494
    throws ServiceFailure {
1495
    
1496
    File newFile = new File(dir, fileName);
1497
    logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
1498

    
1499
    try {
1500
        if (newFile.createNewFile()) {
1501
          // write data stream to desired file
1502
          OutputStream os = new FileOutputStream(newFile);
1503
          long length = IOUtils.copyLarge(data, os);
1504
          os.flush();
1505
          os.close();
1506
        } else {
1507
          logMetacat.debug("File creation failed, or file already exists.");
1508
          throw new ServiceFailure("1190", "File already exists: " + fileName);
1509
        }
1510
    } catch (FileNotFoundException e) {
1511
      logMetacat.debug("FNF: " + e.getMessage());
1512
      throw new ServiceFailure("1190", "File not found: " + fileName + " " 
1513
                + e.getMessage());
1514
    } catch (IOException e) {
1515
      logMetacat.debug("IOE: " + e.getMessage());
1516
      throw new ServiceFailure("1190", "File was not written: " + fileName 
1517
                + " " + e.getMessage());
1518
    }
1519

    
1520
    return newFile;
1521
  }
1522

    
1523
  /*
1524
   * Returns a list of nodes that have been registered with the DataONE infrastructure
1525
   * that match the given session subject
1526
   * @return nodes - List of nodes from the registry with a matching session subject
1527
   * 
1528
   * @throws ServiceFailure
1529
   * @throws NotImplemented
1530
   */
1531
  protected List<Node> listNodesBySubject(Subject subject) 
1532
      throws ServiceFailure, NotImplemented {
1533
      List<Node> nodeList = new ArrayList<Node>();
1534
      
1535
      CNode cn = D1Client.getCN();
1536
      List<Node> nodes = cn.listNodes().getNodeList();
1537
      
1538
      // find the node in the node list
1539
      for ( Node node : nodes ) {
1540
          
1541
          List<Subject> nodeSubjects = node.getSubjectList();
1542
          if (nodeSubjects != null) {    
1543
	          // check if the session subject is in the node subject list
1544
	          for (Subject nodeSubject : nodeSubjects) {
1545
	              if ( nodeSubject.equals(subject) ) { // subject of session == node subject
1546
	                  nodeList.add(node);  
1547
	              }                              
1548
	          }
1549
          }
1550
      }
1551
      
1552
      return nodeList;
1553
      
1554
  }
1555

    
1556
  /**
1557
   * Archives an object, where the object is either a 
1558
   * data object or a science metadata object.
1559
   * 
1560
   * @param session - the Session object containing the credentials for the Subject
1561
   * @param pid - The object identifier to be archived
1562
   * 
1563
   * @return pid - the identifier of the object used for the archiving
1564
   * 
1565
   * @throws InvalidToken
1566
   * @throws ServiceFailure
1567
   * @throws NotAuthorized
1568
   * @throws NotFound
1569
   * @throws NotImplemented
1570
   * @throws InvalidRequest
1571
   */
1572
  public Identifier archive(Session session, Identifier pid) 
1573
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
1574

    
1575
      String localId = null;
1576
      boolean allowed = false;
1577
      String username = Constants.SUBJECT_PUBLIC;
1578
      String[] groupnames = null;
1579
      if (session == null) {
1580
      	throw new InvalidToken("1330", "No session has been provided");
1581
      } else {
1582
          username = session.getSubject().getValue();
1583
          if (session.getSubjectInfo() != null) {
1584
              List<Group> groupList = session.getSubjectInfo().getGroupList();
1585
              if (groupList != null) {
1586
                  groupnames = new String[groupList.size()];
1587
                  for (int i = 0; i < groupList.size(); i++) {
1588
                      groupnames[i] = groupList.get(i).getGroupName();
1589
                  }
1590
              }
1591
          }
1592
      }
1593

    
1594
      // do we have a valid pid?
1595
      if (pid == null || pid.getValue().trim().equals("")) {
1596
          throw new ServiceFailure("1350", "The provided identifier was invalid.");
1597
      }
1598

    
1599
      // check for the existing identifier
1600
      try {
1601
          localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1602
      } catch (McdbDocNotFoundException e) {
1603
          throw new NotFound("1340", "The object with the provided " + "identifier was not found.");
1604
      }
1605

    
1606
      // does the subject have archive (a D1 CHANGE_PERMISSION level) privileges on the pid?
1607
      try {
1608
			allowed = isAuthorized(session, pid, Permission.CHANGE_PERMISSION);
1609
		} catch (InvalidRequest e) {
1610
          throw new ServiceFailure("1350", e.getDescription());
1611
		}
1612
          
1613

    
1614
      if (allowed) {
1615
          try {
1616
              // archive the document
1617
              DocumentImpl.delete(localId, null, null, null, false);
1618
              EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, Event.DELETE.xmlValue());
1619

    
1620
              // archive it
1621
              SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1622
              sysMeta.setArchived(true);
1623
              sysMeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1624
              HazelcastService.getInstance().getSystemMetadataMap().put(pid, sysMeta);
1625
              // submit for indexing
1626
              // DocumentImpl call above should do this.
1627
              // see: https://projects.ecoinformatics.org/ecoinfo/issues/6030
1628
              //HazelcastService.getInstance().getIndexQueue().add(sysMeta);
1629
              
1630
          } catch (McdbDocNotFoundException e) {
1631
              throw new NotFound("1340", "The provided identifier was invalid.");
1632

    
1633
          } catch (SQLException e) {
1634
              throw new ServiceFailure("1350", "There was a problem archiving the object." + "The error message was: " + e.getMessage());
1635

    
1636
          } catch (InsufficientKarmaException e) {
1637
              throw new NotAuthorized("1320", "The provided identity does not have " + "permission to archive this object.");
1638

    
1639
          } catch (Exception e) { // for some reason DocumentImpl throws a general Exception
1640
              throw new ServiceFailure("1350", "There was a problem archiving the object." + "The error message was: " + e.getMessage());
1641
          }
1642

    
1643
      } else {
1644
          throw new NotAuthorized("1320", "The provided identity does not have " + "permission to archive the object on the Node.");
1645
      }
1646

    
1647
      return pid;
1648
  }
1649

    
1650

    
1651
}
(2-2/7)