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: leinfelder $'
7
 *     '$Date: 2014-02-25 16:14:20 -0800 (Tue, 25 Feb 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

    
41
import javax.servlet.http.HttpServletRequest;
42

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

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

    
93
public abstract class D1NodeService {
94
  
95
  private static Logger logMetacat = Logger.getLogger(D1NodeService.class);
96

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

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

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

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

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

    
177
      return describeResponse;
178

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

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

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

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

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

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

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

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

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

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

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

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

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

    
396
      // Science metadata (XML) or science data object?
397
      // TODO: there are cases where certain object formats are science metadata
398
      // but are not XML (netCDF ...).  Handle this.
399
      if ( isScienceMetadata(sysmeta) ) {
400
        
401
        // CASE METADATA:
402
      	String objectAsXML = "";
403
        try {
404
	        objectAsXML = IOUtils.toString(object, "UTF-8");
405
	        localId = insertOrUpdateDocument(objectAsXML, pid, session, "insert");
406
	        //localId = im.getLocalId(pid.getValue());
407

    
408
        } catch (IOException e) {
409
        	String msg = "The Node is unable to create the object. " +
410
          "There was a problem converting the object to XML";
411
        	logMetacat.info(msg);
412
          throw new ServiceFailure("1190", msg + ": " + e.getMessage());
413

    
414
        }
415
                    
416
      } else {
417
	        
418
	      // DEFAULT CASE: DATA (needs to be checked and completed)
419
	      localId = insertDataObject(object, pid, session);
420
      }   
421
    
422
    }
423

    
424
    logMetacat.debug("Done inserting new object: " + pid.getValue());
425
    
426
    // save the sysmeta
427
    try {
428
    	// lock and unlock of the pid happens in the subclass
429
    	HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
430
    	// submit for indexing
431
        MetacatSolrIndex.getInstance().submit(sysmeta.getIdentifier(), sysmeta, null, true);
432
        
433
    } catch (Exception e) {
434
    	logMetacat.error("Problem creating system metadata: " + pid.getValue(), e);
435
        throw new ServiceFailure("1190", e.getMessage());
436
	}
437
    
438
    // setting the resulting identifier failed
439
    if (localId == null ) {
440
      throw new ServiceFailure("1190", "The Node is unable to create the object. ");
441
    }
442

    
443
    resultPid = pid;
444
    
445
    logMetacat.debug("create() complete for object: " + pid.getValue());
446

    
447
    return resultPid;
448
  }
449

    
450
  /**
451
   * Return the log records associated with a given event between the start and 
452
   * end dates listed given a particular Subject listed in the Session
453
   * 
454
   * @param session - the Session object containing the credentials for the Subject
455
   * @param fromDate - the start date of the desired log records
456
   * @param toDate - the end date of the desired log records
457
   * @param event - restrict log records of a specific event type
458
   * @param start - zero based offset from the first record in the 
459
   *                set of matching log records. Used to assist with 
460
   *                paging the response.
461
   * @param count - maximum number of log records to return in the response. 
462
   *                Used to assist with paging the response.
463
   * 
464
   * @return the desired log records
465
   * 
466
   * @throws InvalidToken
467
   * @throws ServiceFailure
468
   * @throws NotAuthorized
469
   * @throws InvalidRequest
470
   * @throws NotImplemented
471
   */
472
  public Log getLogRecords(Session session, Date fromDate, Date toDate, 
473
      Event event, String pidFilter, Integer start, Integer count) throws InvalidToken, ServiceFailure,
474
      NotAuthorized, InvalidRequest, NotImplemented {
475

    
476
	  // only admin access to this method
477
	  // see https://redmine.dataone.org/issues/2855
478
	  if (!isAdminAuthorized(session)) {
479
		  throw new NotAuthorized("1460", "Only the CN or admin is allowed to harvest logs from this node");
480
	  }
481
	  
482
    IdentifierManager im = IdentifierManager.getInstance();
483
    EventLog el = EventLog.getInstance();
484
    if ( fromDate == null ) {
485
      logMetacat.debug("setting fromdate from null");
486
      fromDate = new Date(1);
487
    }
488
    if ( toDate == null ) {
489
      logMetacat.debug("setting todate from null");
490
      toDate = new Date();
491
    }
492

    
493
    if ( start == null ) {
494
    	start = 0;	
495
    }
496
    
497
    if ( count == null ) {
498
    	count = 1000;
499
    }
500
    
501
    // safeguard against large requests
502
    if (count > MAXIMUM_DB_RECORD_COUNT) {
503
    	count = MAXIMUM_DB_RECORD_COUNT;
504
    }
505

    
506
    String[] filterDocid = null;
507
    if (pidFilter != null) {
508
		try {
509
	      String localId = im.getLocalId(pidFilter);
510
	      filterDocid = new String[] {localId};
511
	    } catch (Exception ex) { 
512
	    	String msg = "Could not find localId for given pidFilter '" + pidFilter + "'";
513
	        logMetacat.warn(msg, ex);
514
	        //throw new InvalidRequest("1480", msg);
515
	    }
516
    }
517
    
518
    logMetacat.debug("fromDate: " + fromDate);
519
    logMetacat.debug("toDate: " + toDate);
520

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

    
585
    // if we fail to set the input stream
586
    if ( inputStream == null ) {
587
      throw new NotFound("1020", "The object specified by " + 
588
                         pid.getValue() +
589
                         "does not exist at this node.");
590
    }
591
    
592
	// log the read event
593
    String principal = Constants.SUBJECT_PUBLIC;
594
    if (session != null && session.getSubject() != null) {
595
    	principal = session.getSubject().getValue();
596
    }
597
    EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), principal, localId, "read");
598
    
599
    return inputStream;
600
  }
601

    
602
  /**
603
   * Return the system metadata for a given object
604
   * 
605
   * @param session - the Session object containing the credentials for the Subject
606
   * @param pid - the object identifier for the given object
607
   * 
608
   * @return inputStream - the input stream of the given system metadata object
609
   * 
610
   * @throws InvalidToken
611
   * @throws ServiceFailure
612
   * @throws NotAuthorized
613
   * @throws NotFound
614
   * @throws InvalidRequest
615
   * @throws NotImplemented
616
   */
617
    public SystemMetadata getSystemMetadata(Session session, Identifier pid)
618
        throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
619
        NotImplemented {
620

    
621
        boolean isAuthorized = false;
622
        SystemMetadata systemMetadata = null;
623
        List<Replica> replicaList = null;
624
        NodeReference replicaNodeRef = null;
625
        List<Node> nodeListBySubject = null;
626
        Subject subject = null;
627
        
628
        if (session != null ) {
629
            subject = session.getSubject();
630
        }
631
        
632
        // check normal authorization
633
        BaseException originalAuthorizationException = null;
634
        if (!isAuthorized) {
635
            try {
636
                isAuthorized = isAuthorized(session, pid, Permission.READ);
637

    
638
            } catch (InvalidRequest e) {
639
                throw new ServiceFailure("1090", e.getDescription());
640
            } catch (NotAuthorized nae) {
641
            	// catch this for later
642
            	originalAuthorizationException = nae;
643
			}
644
        }
645
        
646
        // get the system metadata first because we need the replica list for auth
647
        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
648
        
649
        // check the replica information to expand access to MNs that might need it
650
        if (!isAuthorized) {
651
        	
652
	        try {
653
	        	
654
	            // if MNs are listed as replicas, allow access
655
	            if ( systemMetadata != null ) {
656
	                replicaList = systemMetadata.getReplicaList();
657
	                // only check if there are in fact replicas listed
658
	                if ( replicaList != null ) {
659
	                    
660
	                    if ( subject != null ) {
661
	                        // get the list of nodes with a matching node subject
662
	                        try {
663
	                            nodeListBySubject = listNodesBySubject(session.getSubject());
664
	
665
	                        } catch (BaseException e) {
666
	                            // Unexpected error contacting the CN via D1Client
667
	                            String msg = "Caught an unexpected error while trying "
668
	                                    + "to potentially authorize system metadata access "
669
	                                    + "based on the session subject. The error was "
670
	                                    + e.getMessage();
671
	                            logMetacat.error(msg);
672
	                            if (logMetacat.isDebugEnabled()) {
673
	                                e.printStackTrace();
674
	
675
	                            }
676
	                            // isAuthorized is still false 
677
	                        }
678
	
679
	                    }
680
	                    if (nodeListBySubject != null) {
681
	                        // compare node ids to replica node ids
682
	                        outer: for (Replica replica : replicaList) {
683
	                            replicaNodeRef = replica.getReplicaMemberNode();
684
	
685
	                            for (Node node : nodeListBySubject) {
686
	                                if (node.getIdentifier().equals(replicaNodeRef)) {
687
	                                    // node id via session subject matches a replica node
688
	                                    isAuthorized = true;
689
	                                    break outer;
690
	                                }
691
	                            }
692
	                        }
693
	                    }
694
	                }
695
	            }
696
	            
697
	            // if we still aren't authorized, then we are done
698
	            if (!isAuthorized) {
699
	                throw new NotAuthorized("1400", Permission.READ
700
	                        + " not allowed on " + pid.getValue());
701
	            }
702

    
703
	        } catch (RuntimeException e) {
704
	        	e.printStackTrace();
705
	            // convert hazelcast RuntimeException to ServiceFailure
706
	            throw new ServiceFailure("1090", "Unexpected error getting system metadata for: " + 
707
	                pid.getValue());	
708
	        }
709
	        
710
        }
711
        
712
        // It wasn't in the map
713
        if ( systemMetadata == null ) {
714
            throw new NotFound("1420", "No record found for: " + pid.getValue());
715
        }
716
        
717
        return systemMetadata;
718
    }
719
     
720
    
721
    /**
722
     * Test if the specified session represents the authoritative member node for the
723
     * given object specified by the identifier. According the the DataONE documentation, 
724
     * the authoritative member node has all the rights of the *rightsHolder*.
725
     * @param session - the Session object containing the credentials for the Subject
726
     * @param pid - the Identifier of the data object
727
     * @return true if the session represents the authoritative mn.
728
     * @throws ServiceFailure 
729
     * @throws NotImplemented 
730
     */
731
    public boolean isAuthoritativeMNodeAdmin(Session session, Identifier pid) {
732
        boolean allowed = false;
733
        //check the parameters
734
        if(session == null) {
735
            logMetacat.debug("D1NodeService.isAuthoritativeMNodeAdmin - the session object is null and return false.");
736
            return allowed;
737
        } else if (pid == null || pid.getValue() == null || pid.getValue().trim().equals("")) {
738
            logMetacat.debug("D1NodeService.isAuthoritativeMNodeAdmin - the Identifier object is null (not being specified) and return false.");
739
            return allowed;
740
        }
741
        
742
        //Get the subject from the session
743
        Subject subject = session.getSubject();
744
        if(subject != null) {
745
            //Get the authoritative member node info from the system metadata
746
            SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
747
            if(sysMeta != null) {
748
                NodeReference authoritativeMNode = sysMeta.getAuthoritativeMemberNode();
749
                if(authoritativeMNode != null) {
750
                        CNode cn = null;
751
                        try {
752
                            cn = D1Client.getCN();
753
                        } catch (ServiceFailure e) {
754
                            logMetacat.error("D1NodeService.isAuthoritativeMNodeAdmin - couldn't connect to the CN since "+
755
                                            e.getDescription()+ ". The false value will be returned for the AuthoritativeMNodeAdmin.");
756
                            return allowed;
757
                        }
758
                        
759
                        if(cn != null) {
760
                            List<Node> nodes = null;
761
                            try {
762
                                nodes = cn.listNodes().getNodeList();
763
                            } catch (NotImplemented e) {
764
                                logMetacat.error("D1NodeService.isAuthoritativeMNodeAdmin - couldn't get the member nodes list from the CN since "+e.getDescription()+ 
765
                                                ". The false value will be returned for the AuthoritativeMNodeAdmin.");
766
                                return allowed;
767
                            } catch (ServiceFailure ee) {
768
                                logMetacat.error("D1NodeService.isAuthoritativeMNodeAdmin - couldn't get the member nodes list from the CN since "+ee.getDescription()+ 
769
                                                ". The false value will be returned for the AuthoritativeMNodeAdmin.");
770
                                return allowed;
771
                            }
772
                            if(nodes != null) {
773
                                for(Node node : nodes) {
774
                                    //find the authoritative node and get its subjects
775
                                    if (node.getType() == NodeType.MN && node.getIdentifier() != null && node.getIdentifier().equals(authoritativeMNode)) {
776
                                        List<Subject> nodeSubjects = node.getSubjectList();
777
                                        if(nodeSubjects != null) {
778
                                            // check if the session subject is in the node subject list
779
                                            for (Subject nodeSubject : nodeSubjects) {
780
                                                logMetacat.debug("D1NodeService.isAuthoritativeMNodeAdmin(), comparing subjects: " +
781
                                                    nodeSubject.getValue() + " and " + subject.getValue());
782
                                                if ( nodeSubject != null && nodeSubject.equals(subject) ) {
783
                                                    allowed = true; // subject of session == target node subject
784
                                                    break;
785
                                                }
786
                                            }              
787
                                        }
788
                                      
789
                                    }
790
                                }
791
                            }
792
                        }
793
                }
794
            }
795
        }
796
        return allowed;
797
    }
798
    
799
    
800
  /**
801
   * Test if the user identified by the provided token has administrative authorization 
802
   * 
803
   * @param session - the Session object containing the credentials for the Subject
804
   * 
805
   * @return true if the user is admin
806
   * 
807
   * @throws ServiceFailure
808
   * @throws InvalidToken
809
   * @throws NotFound
810
   * @throws NotAuthorized
811
   * @throws NotImplemented
812
   */
813
  public boolean isAdminAuthorized(Session session) 
814
      throws ServiceFailure, InvalidToken, NotAuthorized,
815
      NotImplemented {
816

    
817
      boolean allowed = false;
818
      
819
      // must have a session in order to check admin 
820
      if (session == null) {
821
         logMetacat.debug("In isAdminAuthorized(), session is null ");
822
         return false;
823
      }
824
      
825
      logMetacat.debug("In isAdminAuthorized(), checking CN or MN authorization for " +
826
           session.getSubject().getValue());
827
      
828
      // check if this is the node calling itself (MN)
829
      allowed = isNodeAdmin(session);
830
      
831
      // check the CN list
832
      if (!allowed) {
833
	      // are we allowed to do this? only CNs are allowed
834
	      CNode cn = D1Client.getCN();
835
	      List<Node> nodes = cn.listNodes().getNodeList();
836
	      
837
	      if ( nodes == null ) {
838
	          throw new ServiceFailure("4852", "Couldn't get node list.");
839
	  
840
	      }
841
	      
842
	      // find the node in the node list
843
	      for ( Node node : nodes ) {
844
	          
845
	          NodeReference nodeReference = node.getIdentifier();
846
	          logMetacat.debug("In isAdminAuthorized(), Node reference is: " + nodeReference.getValue());
847
	          
848
	          Subject subject = session.getSubject();
849
	          
850
	          if (node.getType() == NodeType.CN) {
851
	              List<Subject> nodeSubjects = node.getSubjectList();
852
	              
853
	              // check if the session subject is in the node subject list
854
	              for (Subject nodeSubject : nodeSubjects) {
855
	                  logMetacat.debug("In isAdminAuthorized(), comparing subjects: " +
856
	                      nodeSubject.getValue() + " and " + subject.getValue());
857
	                  if ( nodeSubject.equals(subject) ) {
858
	                      allowed = true; // subject of session == target node subject
859
	                      break;
860
	                      
861
	                  }
862
	              }              
863
	          }
864
	      }
865
      }
866
      
867
      return allowed;
868
  }
869
  
870
  /**
871
   * Test if the user identified by the provided token has administrative authorization 
872
   * on this node because they are calling themselves
873
   * 
874
   * @param session - the Session object containing the credentials for the Subject
875
   * 
876
   * @return true if the user is this node
877
   * @throws ServiceFailure 
878
   * @throws NotImplemented 
879
   */
880
  public boolean isNodeAdmin(Session session) throws NotImplemented, ServiceFailure {
881

    
882
      boolean allowed = false;
883
      
884
      // must have a session in order to check admin 
885
      if (session == null) {
886
         logMetacat.debug("In isNodeAdmin(), session is null ");
887
         return false;
888
      }
889
      
890
      logMetacat.debug("In isNodeAdmin(), MN authorization for " +
891
           session.getSubject().getValue());
892
      
893
      Node node = MNodeService.getInstance(request).getCapabilities();
894
      NodeReference nodeReference = node.getIdentifier();
895
      logMetacat.debug("In isNodeAdmin(), Node reference is: " + nodeReference.getValue());
896
      
897
      Subject subject = session.getSubject();
898
      
899
      if (node.getType() == NodeType.MN) {
900
          List<Subject> nodeSubjects = node.getSubjectList();
901
          
902
          // check if the session subject is in the node subject list
903
          for (Subject nodeSubject : nodeSubjects) {
904
              logMetacat.debug("In isNodeAdmin(), comparing subjects: " +
905
                  nodeSubject.getValue() + " and " + subject.getValue());
906
              if ( nodeSubject.equals(subject) ) {
907
                  allowed = true; // subject of session == this node's subect
908
                  break;
909
              }
910
          }              
911
      }
912
      
913
      return allowed;
914
  }
915
  
916
  /**
917
   * Test if the user identified by the provided token has authorization 
918
   * for the operation on the specified object.
919
   * 
920
   * @param session - the Session object containing the credentials for the Subject
921
   * @param pid - The identifer of the resource for which access is being checked
922
   * @param operation - The type of operation which is being requested for the given pid
923
   *
924
   * @return true if the operation is allowed
925
   * 
926
   * @throws ServiceFailure
927
   * @throws InvalidToken
928
   * @throws NotFound
929
   * @throws NotAuthorized
930
   * @throws NotImplemented
931
   * @throws InvalidRequest
932
   */
933
  public boolean isAuthorized(Session session, Identifier pid, Permission permission)
934
    throws ServiceFailure, InvalidToken, NotFound, NotAuthorized,
935
    NotImplemented, InvalidRequest {
936

    
937
    boolean allowed = false;
938
    
939
    if (permission == null) {
940
    	throw new InvalidRequest("1761", "Permission was not provided or is invalid");
941
    }
942
    
943
    // permissions are hierarchical
944
    List<Permission> expandedPermissions = null;
945
    
946
    // always allow CN access
947
    if ( isAdminAuthorized(session) ) {
948
        allowed = true;
949
        return allowed;
950
        
951
    }
952
    
953
    // the authoritative member node of the pid always has the access as well.
954
    if (isAuthoritativeMNodeAdmin(session, pid)) {
955
        allowed = true;
956
        return allowed;
957
    }
958
    
959
    // get the subject[s] from the session
960
	//defer to the shared util for recursively compiling the subjects	
961
	Set<Subject> subjects = AuthUtils.authorizedClientSubjects(session);
962
    
963
	// track the identities we have checked against
964
	StringBuffer includedSubjects = new StringBuffer();
965
    	
966
    // get the system metadata
967
    String pidStr = pid.getValue();
968
    SystemMetadata systemMetadata = null;
969
    try {
970
        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
971

    
972
    } catch (Exception e) {
973
        // convert Hazelcast RuntimeException to NotFound
974
        logMetacat.error("An error occurred while getting system metadata for identifier " +
975
            pid.getValue() + ". The error message was: " + e.getMessage());
976
        throw new NotFound("1800", "No record found for " + pidStr);
977
        
978
    } 
979
    
980
    // throw not found if it was not found
981
    if (systemMetadata == null) {
982
    	throw new NotFound("1800", "No system metadata could be found for given PID: " + pidStr);
983
    }
984
	    
985
    // do we own it?
986
    for (Subject s: subjects) {
987
      logMetacat.debug("Comparing \t" + 
988
                       systemMetadata.getRightsHolder().getValue() +
989
                       " \tagainst \t" + s.getValue());
990
      	includedSubjects.append(s.getValue() + "; ");
991
    	allowed = systemMetadata.getRightsHolder().equals(s);
992
    	if (allowed) {
993
    		return allowed;
994
    	}
995
    }    
996
    
997
    // otherwise check the access rules
998
    try {
999
	    List<AccessRule> allows = systemMetadata.getAccessPolicy().getAllowList();
1000
	    search: // label break
1001
	    for (AccessRule accessRule: allows) {
1002
	      for (Subject s: subjects) {
1003
	        logMetacat.debug("Checking allow access rule for subject: " + s.getValue());
1004
	        if (accessRule.getSubjectList().contains(s)) {
1005
	        	logMetacat.debug("Access rule contains subject: " + s.getValue());
1006
	        	for (Permission p: accessRule.getPermissionList()) {
1007
		        	logMetacat.debug("Checking permission: " + p.xmlValue());
1008
	        		expandedPermissions = expandPermissions(p);
1009
	        		allowed = expandedPermissions.contains(permission);
1010
	        		if (allowed) {
1011
			        	logMetacat.info("Permission granted: " + p.xmlValue() + " to " + s.getValue());
1012
	        			break search; //label break
1013
	        		}
1014
	        	}
1015
        		
1016
	        }
1017
	      }
1018
	    }
1019
    } catch (Exception e) {
1020
    	// catch all for errors - safe side should be to deny the access
1021
    	logMetacat.error("Problem checking authorization - defaulting to deny", e);
1022
		allowed = false;
1023
	  
1024
    }
1025
    
1026
    // throw or return?
1027
    if (!allowed) {
1028
      throw new NotAuthorized("1820", permission + " not allowed on " + pidStr + " for subject[s]: " + includedSubjects.toString() );
1029
    }
1030
    
1031
    return allowed;
1032
    
1033
  }
1034
  
1035
  /*
1036
   * parse a logEntry and get the relevant field from it
1037
   * 
1038
   * @param fieldname
1039
   * @param entry
1040
   * @return
1041
   */
1042
  private String getLogEntryField(String fieldname, String entry) {
1043
    String begin = "<" + fieldname + ">";
1044
    String end = "</" + fieldname + ">";
1045
    // logMetacat.debug("looking for " + begin + " and " + end +
1046
    // " in entry " + entry);
1047
    String s = entry.substring(entry.indexOf(begin) + begin.length(), entry
1048
        .indexOf(end));
1049
    logMetacat.debug("entry " + fieldname + " : " + s);
1050
    return s;
1051
  }
1052

    
1053
  /** 
1054
   * Determine if a given object should be treated as an XML science metadata
1055
   * object. 
1056
   * 
1057
   * @param sysmeta - the SystemMetadata describing the object
1058
   * @return true if the object should be treated as science metadata
1059
   */
1060
  public static boolean isScienceMetadata(SystemMetadata sysmeta) {
1061
    
1062
    ObjectFormat objectFormat = null;
1063
    boolean isScienceMetadata = false;
1064
    
1065
    try {
1066
      objectFormat = ObjectFormatCache.getInstance().getFormat(sysmeta.getFormatId());
1067
      if ( objectFormat.getFormatType().equals("METADATA") ) {
1068
      	isScienceMetadata = true;
1069
      	
1070
      }
1071
      
1072
       
1073
    } catch (ServiceFailure e) {
1074
      logMetacat.debug("There was a problem determining if the object identified by" + 
1075
          sysmeta.getIdentifier().getValue() + 
1076
          " is science metadata: " + e.getMessage());
1077
    
1078
    } catch (NotFound e) {
1079
      logMetacat.debug("There was a problem determining if the object identified by" + 
1080
          sysmeta.getIdentifier().getValue() + 
1081
          " is science metadata: " + e.getMessage());
1082
    
1083
    }
1084
    
1085
    return isScienceMetadata;
1086

    
1087
  }
1088
  
1089
  /**
1090
   * Check fro whitespace in the given pid.
1091
   * null pids are also invalid by default
1092
   * @param pid
1093
   * @return
1094
   */
1095
  public static boolean isValidIdentifier(Identifier pid) {
1096
	  if (pid != null && pid.getValue() != null && pid.getValue().length() > 0) {
1097
		  return !pid.getValue().matches(".*\\s+.*");
1098
	  } 
1099
	  return false;
1100
  }
1101
  
1102
  
1103
  /**
1104
   * Insert or update an XML document into Metacat
1105
   * 
1106
   * @param xml - the XML document to insert or update
1107
   * @param pid - the identifier to be used for the resulting object
1108
   * 
1109
   * @return localId - the resulting docid of the document created or updated
1110
   * 
1111
   */
1112
  public String insertOrUpdateDocument(String xml, Identifier pid, 
1113
    Session session, String insertOrUpdate) 
1114
    throws ServiceFailure {
1115
    
1116
  	logMetacat.debug("Starting to insert xml document...");
1117
    IdentifierManager im = IdentifierManager.getInstance();
1118

    
1119
    // generate pid/localId pair for sysmeta
1120
    String localId = null;
1121
    
1122
    if(insertOrUpdate.equals("insert")) {
1123
      localId = im.generateLocalId(pid.getValue(), 1);
1124
      
1125
    } else {
1126
      //localid should already exist in the identifier table, so just find it
1127
      try {
1128
        logMetacat.debug("Updating pid " + pid.getValue());
1129
        logMetacat.debug("looking in identifier table for pid " + pid.getValue());
1130
        
1131
        localId = im.getLocalId(pid.getValue());
1132
        
1133
        logMetacat.debug("localId: " + localId);
1134
        //increment the revision
1135
        String docid = localId.substring(0, localId.lastIndexOf("."));
1136
        String revS = localId.substring(localId.lastIndexOf(".") + 1, localId.length());
1137
        int rev = new Integer(revS).intValue();
1138
        rev++;
1139
        docid = docid + "." + rev;
1140
        localId = docid;
1141
        logMetacat.debug("incremented localId: " + localId);
1142
      
1143
      } catch(McdbDocNotFoundException e) {
1144
        throw new ServiceFailure("1030", "D1NodeService.insertOrUpdateDocument(): " +
1145
            "pid " + pid.getValue() + 
1146
            " should have been in the identifier table, but it wasn't: " + 
1147
            e.getMessage());
1148
      
1149
      }
1150
      
1151
    }
1152

    
1153
    params = new Hashtable<String, String[]>();
1154
    String[] action = new String[1];
1155
    action[0] = insertOrUpdate;
1156
    params.put("action", action);
1157
    String[] docid = new String[1];
1158
    docid[0] = localId;
1159
    params.put("docid", docid);
1160
    String[] doctext = new String[1];
1161
    doctext[0] = xml;
1162
    params.put("doctext", doctext);
1163
    
1164
    String username = Constants.SUBJECT_PUBLIC;
1165
    String[] groupnames = null;
1166
    if (session != null ) {
1167
    	username = session.getSubject().getValue();
1168
    	if (session.getSubjectInfo() != null) {
1169
    		List<Group> groupList = session.getSubjectInfo().getGroupList();
1170
    		if (groupList != null) {
1171
    			groupnames = new String[groupList.size()];
1172
    			for (int i = 0; i < groupList.size(); i++ ) {
1173
    				groupnames[i] = groupList.get(i).getGroupName();
1174
    			}
1175
    		}
1176
    	}
1177
    }
1178
    
1179
    // do the insert or update action
1180
    handler = new MetacatHandler(new Timer());
1181
    String result = handler.handleInsertOrUpdateAction(request.getRemoteAddr(), request.getHeader("User-Agent"), null, 
1182
                        null, params, username, groupnames, false, false);
1183
    
1184
    if(result.indexOf("<error>") != -1) {
1185
    	String detailCode = "";
1186
    	if ( insertOrUpdate.equals("insert") ) {
1187
    		// make sure to remove the mapping so that subsequent attempts do not fail with IdentifierNotUnique
1188
    		im.removeMapping(pid.getValue(), localId);
1189
    		detailCode = "1190";
1190
    		
1191
    	} else if ( insertOrUpdate.equals("update") ) {
1192
    		detailCode = "1310";
1193
    		
1194
    	}
1195
        throw new ServiceFailure(detailCode, 
1196
          "Error inserting or updating document: " + result);
1197
    }
1198
    logMetacat.debug("Finsished inserting xml document with id " + localId);
1199
    
1200
    return localId;
1201
  }
1202
  
1203
  /**
1204
   * Insert a data document
1205
   * 
1206
   * @param object
1207
   * @param pid
1208
   * @param sessionData
1209
   * @throws ServiceFailure
1210
   * @returns localId of the data object inserted
1211
   */
1212
  public String insertDataObject(InputStream object, Identifier pid, 
1213
          Session session) throws ServiceFailure {
1214
      
1215
    String username = Constants.SUBJECT_PUBLIC;
1216
    String[] groupnames = null;
1217
    if (session != null ) {
1218
    	username = session.getSubject().getValue();
1219
    	if (session.getSubjectInfo() != null) {
1220
    		List<Group> groupList = session.getSubjectInfo().getGroupList();
1221
    		if (groupList != null) {
1222
    			groupnames = new String[groupList.size()];
1223
    			for (int i = 0; i < groupList.size(); i++ ) {
1224
    				groupnames[i] = groupList.get(i).getGroupName();
1225
    			}
1226
    		}
1227
    	}
1228
    }
1229
  
1230
    // generate pid/localId pair for object
1231
    logMetacat.debug("Generating a pid/localId mapping");
1232
    IdentifierManager im = IdentifierManager.getInstance();
1233
    String localId = im.generateLocalId(pid.getValue(), 1);
1234
  
1235
    // Save the data file to disk using "localId" as the name
1236
    String datafilepath = null;
1237
	try {
1238
		datafilepath = PropertyService.getProperty("application.datafilepath");
1239
	} catch (PropertyNotFoundException e) {
1240
		ServiceFailure sf = new ServiceFailure("1190", "Lookup data file path" + e.getMessage());
1241
		sf.initCause(e);
1242
		throw sf;
1243
	}
1244
    boolean locked = false;
1245
	try {
1246
		locked = DocumentImpl.getDataFileLockGrant(localId);
1247
	} catch (Exception e) {
1248
		ServiceFailure sf = new ServiceFailure("1190", "Could not lock file for writing:" + e.getMessage());
1249
		sf.initCause(e);
1250
		throw sf;
1251
	}
1252

    
1253
    logMetacat.debug("Case DATA: starting to write to disk.");
1254
	if (locked) {
1255

    
1256
          File dataDirectory = new File(datafilepath);
1257
          dataDirectory.mkdirs();
1258
  
1259
          File newFile = writeStreamToFile(dataDirectory, localId, object);
1260
  
1261
          // TODO: Check that the file size matches SystemMetadata
1262
          // long size = newFile.length();
1263
          // if (size == 0) {
1264
          //     throw new IOException("Uploaded file is 0 bytes!");
1265
          // }
1266
  
1267
          // Register the file in the database (which generates an exception
1268
          // if the localId is not acceptable or other untoward things happen
1269
          try {
1270
            logMetacat.debug("Registering document...");
1271
            DocumentImpl.registerDocument(localId, "BIN", localId,
1272
                    username, groupnames);
1273
            logMetacat.debug("Registration step completed.");
1274
            
1275
          } catch (SQLException e) {
1276
            //newFile.delete();
1277
            logMetacat.debug("SQLE: " + e.getMessage());
1278
            e.printStackTrace(System.out);
1279
            throw new ServiceFailure("1190", "Registration failed: " + 
1280
            		e.getMessage());
1281
            
1282
          } catch (AccessionNumberException e) {
1283
            //newFile.delete();
1284
            logMetacat.debug("ANE: " + e.getMessage());
1285
            e.printStackTrace(System.out);
1286
            throw new ServiceFailure("1190", "Registration failed: " + 
1287
            	e.getMessage());
1288
            
1289
          } catch (Exception e) {
1290
            //newFile.delete();
1291
            logMetacat.debug("Exception: " + e.getMessage());
1292
            e.printStackTrace(System.out);
1293
            throw new ServiceFailure("1190", "Registration failed: " + 
1294
            	e.getMessage());
1295
          }
1296
  
1297
          logMetacat.debug("Logging the creation event.");
1298
          EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, "create");
1299
  
1300
          // Schedule replication for this data file, the "insert" action is important here!
1301
          logMetacat.debug("Scheduling replication.");
1302
          ForceReplicationHandler frh = new ForceReplicationHandler(localId, "insert", false, null);
1303
      }
1304
      
1305
      return localId;
1306
    
1307
  }
1308

    
1309
  /**
1310
   * Insert a systemMetadata document and return its localId
1311
   */
1312
  public void insertSystemMetadata(SystemMetadata sysmeta) 
1313
      throws ServiceFailure {
1314
      
1315
  	  logMetacat.debug("Starting to insert SystemMetadata...");
1316
      sysmeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1317
      logMetacat.debug("Inserting new system metadata with modified date " + 
1318
          sysmeta.getDateSysMetadataModified());
1319
      
1320
      //insert the system metadata
1321
      try {
1322
        // note: the calling subclass handles the map hazelcast lock/unlock
1323
      	HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
1324
      	// submit for indexing
1325
        MetacatSolrIndex.getInstance().submit(sysmeta.getIdentifier(), sysmeta, null, true);
1326
      } catch (Exception e) {
1327
          throw new ServiceFailure("1190", e.getMessage());
1328
          
1329
	    }  
1330
  }
1331

    
1332
  /**
1333
   * Update a systemMetadata document
1334
   * 
1335
   * @param sysMeta - the system metadata object in the system to update
1336
   */
1337
    protected void updateSystemMetadata(SystemMetadata sysMeta)
1338
        throws ServiceFailure {
1339

    
1340
        logMetacat.debug("D1NodeService.updateSystemMetadata() called.");
1341
        sysMeta.setDateSysMetadataModified(new Date());
1342
        try {
1343
            HazelcastService.getInstance().getSystemMetadataMap().lock(sysMeta.getIdentifier());
1344
            HazelcastService.getInstance().getSystemMetadataMap().put(sysMeta.getIdentifier(), sysMeta);
1345
            // submit for indexing
1346
            MetacatSolrIndex.getInstance().submit(sysMeta.getIdentifier(), sysMeta, null, true);
1347
        } catch (Exception e) {
1348
            throw new ServiceFailure("4862", e.getMessage());
1349

    
1350
        } finally {
1351
            HazelcastService.getInstance().getSystemMetadataMap().unlock(sysMeta.getIdentifier());
1352

    
1353
        }
1354

    
1355
    }
1356
  
1357
  /**
1358
   * Given a Permission, returns a list of all permissions that it encompasses
1359
   * Permissions are hierarchical so that WRITE also allows READ.
1360
   * @param permission
1361
   * @return list of included Permissions for the given permission
1362
   */
1363
  protected List<Permission> expandPermissions(Permission permission) {
1364
	  	List<Permission> expandedPermissions = new ArrayList<Permission>();
1365
	    if (permission.equals(Permission.READ)) {
1366
	    	expandedPermissions.add(Permission.READ);
1367
	    }
1368
	    if (permission.equals(Permission.WRITE)) {
1369
	    	expandedPermissions.add(Permission.READ);
1370
	    	expandedPermissions.add(Permission.WRITE);
1371
	    }
1372
	    if (permission.equals(Permission.CHANGE_PERMISSION)) {
1373
	    	expandedPermissions.add(Permission.READ);
1374
	    	expandedPermissions.add(Permission.WRITE);
1375
	    	expandedPermissions.add(Permission.CHANGE_PERMISSION);
1376
	    }
1377
	    return expandedPermissions;
1378
  }
1379

    
1380
  /*
1381
   * Write a stream to a file
1382
   * 
1383
   * @param dir - the directory to write to
1384
   * @param fileName - the file name to write to
1385
   * @param data - the object bytes as an input stream
1386
   * 
1387
   * @return newFile - the new file created
1388
   * 
1389
   * @throws ServiceFailure
1390
   */
1391
  private File writeStreamToFile(File dir, String fileName, InputStream data) 
1392
    throws ServiceFailure {
1393
    
1394
    File newFile = new File(dir, fileName);
1395
    logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
1396

    
1397
    try {
1398
        if (newFile.createNewFile()) {
1399
          // write data stream to desired file
1400
          OutputStream os = new FileOutputStream(newFile);
1401
          long length = IOUtils.copyLarge(data, os);
1402
          os.flush();
1403
          os.close();
1404
        } else {
1405
          logMetacat.debug("File creation failed, or file already exists.");
1406
          throw new ServiceFailure("1190", "File already exists: " + fileName);
1407
        }
1408
    } catch (FileNotFoundException e) {
1409
      logMetacat.debug("FNF: " + e.getMessage());
1410
      throw new ServiceFailure("1190", "File not found: " + fileName + " " 
1411
                + e.getMessage());
1412
    } catch (IOException e) {
1413
      logMetacat.debug("IOE: " + e.getMessage());
1414
      throw new ServiceFailure("1190", "File was not written: " + fileName 
1415
                + " " + e.getMessage());
1416
    }
1417

    
1418
    return newFile;
1419
  }
1420

    
1421
  /*
1422
   * Returns a list of nodes that have been registered with the DataONE infrastructure
1423
   * that match the given session subject
1424
   * @return nodes - List of nodes from the registry with a matching session subject
1425
   * 
1426
   * @throws ServiceFailure
1427
   * @throws NotImplemented
1428
   */
1429
  protected List<Node> listNodesBySubject(Subject subject) 
1430
      throws ServiceFailure, NotImplemented {
1431
      List<Node> nodeList = new ArrayList<Node>();
1432
      
1433
      CNode cn = D1Client.getCN();
1434
      List<Node> nodes = cn.listNodes().getNodeList();
1435
      
1436
      // find the node in the node list
1437
      for ( Node node : nodes ) {
1438
          
1439
          List<Subject> nodeSubjects = node.getSubjectList();
1440
          if (nodeSubjects != null) {    
1441
	          // check if the session subject is in the node subject list
1442
	          for (Subject nodeSubject : nodeSubjects) {
1443
	              if ( nodeSubject.equals(subject) ) { // subject of session == node subject
1444
	                  nodeList.add(node);  
1445
	              }                              
1446
	          }
1447
          }
1448
      }
1449
      
1450
      return nodeList;
1451
      
1452
  }
1453

    
1454
  /**
1455
   * Archives an object, where the object is either a 
1456
   * data object or a science metadata object.
1457
   * 
1458
   * @param session - the Session object containing the credentials for the Subject
1459
   * @param pid - The object identifier to be archived
1460
   * 
1461
   * @return pid - the identifier of the object used for the archiving
1462
   * 
1463
   * @throws InvalidToken
1464
   * @throws ServiceFailure
1465
   * @throws NotAuthorized
1466
   * @throws NotFound
1467
   * @throws NotImplemented
1468
   * @throws InvalidRequest
1469
   */
1470
  public Identifier archive(Session session, Identifier pid) 
1471
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
1472

    
1473
      String localId = null;
1474
      boolean allowed = false;
1475
      String username = Constants.SUBJECT_PUBLIC;
1476
      String[] groupnames = null;
1477
      if (session == null) {
1478
      	throw new InvalidToken("1330", "No session has been provided");
1479
      } else {
1480
          username = session.getSubject().getValue();
1481
          if (session.getSubjectInfo() != null) {
1482
              List<Group> groupList = session.getSubjectInfo().getGroupList();
1483
              if (groupList != null) {
1484
                  groupnames = new String[groupList.size()];
1485
                  for (int i = 0; i < groupList.size(); i++) {
1486
                      groupnames[i] = groupList.get(i).getGroupName();
1487
                  }
1488
              }
1489
          }
1490
      }
1491

    
1492
      // do we have a valid pid?
1493
      if (pid == null || pid.getValue().trim().equals("")) {
1494
          throw new ServiceFailure("1350", "The provided identifier was invalid.");
1495
      }
1496

    
1497
      // check for the existing identifier
1498
      try {
1499
          localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1500
      } catch (McdbDocNotFoundException e) {
1501
          throw new NotFound("1340", "The object with the provided " + "identifier was not found.");
1502
      }
1503

    
1504
      // does the subject have archive (a D1 CHANGE_PERMISSION level) privileges on the pid?
1505
      try {
1506
			allowed = isAuthorized(session, pid, Permission.CHANGE_PERMISSION);
1507
		} catch (InvalidRequest e) {
1508
          throw new ServiceFailure("1350", e.getDescription());
1509
		}
1510
          
1511

    
1512
      if (allowed) {
1513
          try {
1514
              // archive the document
1515
              DocumentImpl.delete(localId, null, null, null, false);
1516
              EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, Event.DELETE.xmlValue());
1517

    
1518
              // archive it
1519
              SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1520
              sysMeta.setArchived(true);
1521
              sysMeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1522
              HazelcastService.getInstance().getSystemMetadataMap().put(pid, sysMeta);
1523
              // submit for indexing
1524
              // DocumentImpl call above should do this.
1525
              // see: https://projects.ecoinformatics.org/ecoinfo/issues/6030
1526
              //HazelcastService.getInstance().getIndexQueue().add(sysMeta);
1527
              
1528
          } catch (McdbDocNotFoundException e) {
1529
              throw new NotFound("1340", "The provided identifier was invalid.");
1530

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

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

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

    
1541
      } else {
1542
          throw new NotAuthorized("1320", "The provided identity does not have " + "permission to archive the object on the Node.");
1543
      }
1544

    
1545
      return pid;
1546
  }
1547
  
1548
  public Identifier archive(Identifier pid) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
1549
	  return archive(null, pid);
1550
  }
1551

    
1552

    
1553
}
(2-2/7)