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-31 15:49:58 -0800 (Wed, 31 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
      String serviceFailureCode = "4931";
174
      Identifier sid = getPIDForSID(pid, serviceFailureCode);
175
      if(sid != null) {
176
          pid = sid;
177
      }
178

    
179
    // get system metadata and construct the describe response
180
      SystemMetadata sysmeta = getSystemMetadata(session, pid);
181
      DescribeResponse describeResponse = 
182
      	new DescribeResponse(sysmeta.getFormatId(), sysmeta.getSize(), 
183
      			sysmeta.getDateSysMetadataModified(),
184
      			sysmeta.getChecksum(), sysmeta.getSerialVersion());
185

    
186
      return describeResponse;
187

    
188
  }
189
  
190
  /**
191
   * Deletes an object from the Member Node, where the object is either a 
192
   * data object or a science metadata object.
193
   * 
194
   * @param session - the Session object containing the credentials for the Subject
195
   * @param pid - The object identifier to be deleted
196
   * 
197
   * @return pid - the identifier of the object used for the deletion
198
   * 
199
   * @throws InvalidToken
200
   * @throws ServiceFailure
201
   * @throws NotAuthorized
202
   * @throws NotFound
203
   * @throws NotImplemented
204
   * @throws InvalidRequest
205
   */
206
  public Identifier delete(Session session, Identifier pid) 
207
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
208
      
209
      String localId = null;
210
      if (session == null) {
211
      	throw new InvalidToken("1330", "No session has been provided");
212
      }
213
      // just for logging purposes
214
      String username = session.getSubject().getValue();
215

    
216
      // do we have a valid pid?
217
      if (pid == null || pid.getValue().trim().equals("")) {
218
          throw new ServiceFailure("1350", "The provided identifier was invalid.");
219
      }
220

    
221
      // check for the existing identifier
222
      try {
223
          localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
224
      } catch (McdbDocNotFoundException e) {
225
          throw new NotFound("1340", "The object with the provided " + "identifier was not found.");
226
      } catch (SQLException e) {
227
          throw new ServiceFailure("1350", "The object with the provided " + "identifier "+pid.getValue()+" couldn't be identified since "+e.getMessage());
228
      }
229
      
230
      try {
231
          // delete the document, as admin
232
          DocumentImpl.delete(localId, null, null, null, true);
233
          EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, Event.DELETE.xmlValue());
234

    
235
          // archive it
236
          // DocumentImpl.delete() now sets this
237
          // see https://redmine.dataone.org/issues/3406
238
//          SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
239
//          sysMeta.setArchived(true);
240
//          sysMeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
241
//          HazelcastService.getInstance().getSystemMetadataMap().put(pid, sysMeta);
242
          
243
      } catch (McdbDocNotFoundException e) {
244
          throw new NotFound("1340", "The provided identifier was invalid.");
245

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

    
249
      } catch (InsufficientKarmaException e) {
250
          if ( logMetacat.isDebugEnabled() ) {
251
              e.printStackTrace();
252
          }
253
          throw new NotAuthorized("1320", "The provided identity does not have " + "permission to DELETE objects on the Member Node.");
254
      
255
      } catch (Exception e) { // for some reason DocumentImpl throws a general Exception
256
          throw new ServiceFailure("1350", "There was a problem deleting the object." + "The error message was: " + e.getMessage());
257
      }
258

    
259
      return pid;
260
  }
261
  
262
  /**
263
   * Low level, "are you alive" operation. A valid ping response is 
264
   * indicated by a HTTP status of 200.
265
   * 
266
   * @return true if the service is alive
267
   * 
268
   * @throws NotImplemented
269
   * @throws ServiceFailure
270
   * @throws InsufficientResources
271
   */
272
  public Date ping() 
273
      throws NotImplemented, ServiceFailure, InsufficientResources {
274

    
275
      // test if we can get a database connection
276
      int serialNumber = -1;
277
      DBConnection dbConn = null;
278
      try {
279
          dbConn = DBConnectionPool.getDBConnection("MNodeService.ping");
280
          serialNumber = dbConn.getCheckOutSerialNumber();
281
      } catch (SQLException e) {
282
      	ServiceFailure sf = new ServiceFailure("", e.getMessage());
283
      	sf.initCause(e);
284
          throw sf;
285
      } finally {
286
          // Return the database connection
287
          DBConnectionPool.returnDBConnection(dbConn, serialNumber);
288
      }
289

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

    
322
    Identifier resultPid = null;
323
    String localId = null;
324
    boolean allowed = false;
325
    
326
    // check for null session
327
    if (session == null) {
328
    	throw new InvalidToken("4894", "Session is required to WRITE to the Node.");
329
    }
330
    Subject subject = session.getSubject();
331

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

    
360
    logMetacat.debug("Checking if identifier exists: " + pid.getValue());
361
    // Check that the identifier does not already exist
362
    boolean idExists = false;
363
    try {
364
        idExists = IdentifierManager.getInstance().identifierExists(pid.getValue());
365
    } catch (SQLException e) {
366
        throw new ServiceFailure("1190", 
367
                                "The requested identifier " + pid.getValue() +
368
                                " couldn't be determined if it is unique since : "+e.getMessage());
369
    }
370
    if (idExists) {
371
	    	throw new IdentifierNotUnique("1120", 
372
			          "The requested identifier " + pid.getValue() +
373
			          " is already used by another object and" +
374
			          "therefore can not be used for this object. Clients should choose" +
375
			          "a new identifier that is unique and retry the operation or " +
376
			          "use CN.reserveIdentifier() to reserve one.");
377
    	
378
    }
379
    
380
    // verify the sid in the system metadata
381
    Identifier sid = sysmeta.getSeriesId();
382
    if(sid != null) {
383
        if (!isValidIdentifier(sid)) {
384
            throw new InvalidSystemMetadata("1180", "The provided series id is invalid.");
385
        }
386
        try {
387
            idExists = IdentifierManager.getInstance().identifierExists(sid.getValue());
388
        } catch (SQLException e) {
389
            throw new ServiceFailure("1190", 
390
                                    "The series identifier " + sid.getValue() +
391
                                    " in the system metadata couldn't be determined if it is unique since : "+e.getMessage());
392
        }
393
        if (idExists) {
394
                throw new InvalidSystemMetadata("1180", 
395
                          "The series identifier " + sid.getValue() +
396
                          " is already used by another object and" +
397
                          "therefore can not be used for this object. Clients should choose" +
398
                          "a new identifier that is unique and retry the operation or " +
399
                          "use CN.reserveIdentifier() to reserve one.");
400
            
401
        }
402
        //the series id equals the pid (new pid hasn't been registered in the system, so IdentifierManager.getInstance().identifierExists method can't exclude this scenario )
403
        if(sid.getValue().equals(pid.getValue())) {
404
            throw new InvalidSystemMetadata("1180", "The series id "+sid.getValue()+" in the system metadata shouldn't have the same value of the pid.");
405
        }
406
    }
407
    
408
    // TODO: this probably needs to be refined more
409
    try {
410
      allowed = isAuthorized(session, pid, Permission.WRITE);
411
            
412
    } catch (NotFound e) {
413
      // The identifier doesn't exist, writing should be fine.
414
      allowed = true;
415
    }
416
    
417
    // verify checksum, only if we can reset the inputstream
418
    if (object.markSupported()) {
419
        logMetacat.debug("Checking checksum for: " + pid.getValue());
420
	    String checksumAlgorithm = sysmeta.getChecksum().getAlgorithm();
421
	    String checksumValue = sysmeta.getChecksum().getValue();
422
	    try {
423
			String computedChecksumValue = ChecksumUtil.checksum(object, checksumAlgorithm).getValue();
424
			// it's very important that we don't consume the stream
425
			object.reset();
426
			if (!computedChecksumValue.equals(checksumValue)) {
427
			    logMetacat.error("Checksum for " + pid.getValue() + " does not match system metadata, computed = " + computedChecksumValue );
428
				throw new InvalidSystemMetadata("4896", "Checksum given does not match that of the object");
429
			}
430
		} catch (Exception e) {
431
			String msg = "Error verifying checksum values";
432
	      	logMetacat.error(msg, e);
433
	        throw new ServiceFailure("1190", msg + ": " + e.getMessage());
434
		}
435
    } else {
436
    	logMetacat.warn("mark is not supported on the object's input stream - cannot verify checksum without consuming stream");
437
    }
438
    	
439
    // we have the go ahead
440
    if ( allowed ) {
441
      
442
        logMetacat.debug("Allowed to insert: " + pid.getValue());
443

    
444
      // Science metadata (XML) or science data object?
445
      // TODO: there are cases where certain object formats are science metadata
446
      // but are not XML (netCDF ...).  Handle this.
447
      if ( isScienceMetadata(sysmeta) ) {
448
        
449
        // CASE METADATA:
450
      	//String objectAsXML = "";
451
        try {
452
	        //objectAsXML = IOUtils.toString(object, "UTF-8");
453
	        localId = insertOrUpdateDocument(object,"UTF-8", pid, session, "insert");
454
	        //localId = im.getLocalId(pid.getValue());
455

    
456
        } catch (IOException e) {
457
        	String msg = "The Node is unable to create the object. " +
458
          "There was a problem converting the object to XML";
459
        	logMetacat.info(msg);
460
          throw new ServiceFailure("1190", msg + ": " + e.getMessage());
461

    
462
        }
463
                    
464
      } else {
465
	        
466
	      // DEFAULT CASE: DATA (needs to be checked and completed)
467
	      localId = insertDataObject(object, pid, session);
468
      }   
469
    
470
    }
471

    
472
    logMetacat.debug("Done inserting new object: " + pid.getValue());
473
    
474
    // save the sysmeta
475
    try {
476
    	// lock and unlock of the pid happens in the subclass
477
    	HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
478
    	// submit for indexing
479
        MetacatSolrIndex.getInstance().submit(sysmeta.getIdentifier(), sysmeta, null, true);
480
        
481
    } catch (Exception e) {
482
    	logMetacat.error("Problem creating system metadata: " + pid.getValue(), e);
483
        throw new ServiceFailure("1190", e.getMessage());
484
	}
485
    
486
    // setting the resulting identifier failed
487
    if (localId == null ) {
488
      throw new ServiceFailure("1190", "The Node is unable to create the object. ");
489
    }
490

    
491
    resultPid = pid;
492
    
493
    logMetacat.debug("create() complete for object: " + pid.getValue());
494

    
495
    return resultPid;
496
  }
497

    
498
  /**
499
   * Return the log records associated with a given event between the start and 
500
   * end dates listed given a particular Subject listed in the Session
501
   * 
502
   * @param session - the Session object containing the credentials for the Subject
503
   * @param fromDate - the start date of the desired log records
504
   * @param toDate - the end date of the desired log records
505
   * @param event - restrict log records of a specific event type
506
   * @param start - zero based offset from the first record in the 
507
   *                set of matching log records. Used to assist with 
508
   *                paging the response.
509
   * @param count - maximum number of log records to return in the response. 
510
   *                Used to assist with paging the response.
511
   * 
512
   * @return the desired log records
513
   * 
514
   * @throws InvalidToken
515
   * @throws ServiceFailure
516
   * @throws NotAuthorized
517
   * @throws InvalidRequest
518
   * @throws NotImplemented
519
   */
520
  public Log getLogRecords(Session session, Date fromDate, Date toDate, 
521
      String event, String pidFilter, Integer start, Integer count) throws InvalidToken, ServiceFailure,
522
      NotAuthorized, InvalidRequest, NotImplemented {
523

    
524
	  // only admin access to this method
525
	  // see https://redmine.dataone.org/issues/2855
526
	  if (!isAdminAuthorized(session)) {
527
		  throw new NotAuthorized("1460", "Only the CN or admin is allowed to harvest logs from this node");
528
	  }
529
	  
530
    IdentifierManager im = IdentifierManager.getInstance();
531
    EventLog el = EventLog.getInstance();
532
    if ( fromDate == null ) {
533
      logMetacat.debug("setting fromdate from null");
534
      fromDate = new Date(1);
535
    }
536
    if ( toDate == null ) {
537
      logMetacat.debug("setting todate from null");
538
      toDate = new Date();
539
    }
540

    
541
    if ( start == null ) {
542
    	start = 0;	
543
    }
544
    
545
    if ( count == null ) {
546
    	count = 1000;
547
    }
548
    
549
    // safeguard against large requests
550
    if (count > MAXIMUM_DB_RECORD_COUNT) {
551
    	count = MAXIMUM_DB_RECORD_COUNT;
552
    }
553

    
554
    String[] filterDocid = null;
555
    if (pidFilter != null) {
556
		try {
557
	      String localId = im.getLocalId(pidFilter);
558
	      filterDocid = new String[] {localId};
559
	    } catch (Exception ex) { 
560
	    	String msg = "Could not find localId for given pidFilter '" + pidFilter + "'";
561
	        logMetacat.warn(msg, ex);
562
	        //throw new InvalidRequest("1480", msg);
563
	    }
564
    }
565
    
566
    logMetacat.debug("fromDate: " + fromDate);
567
    logMetacat.debug("toDate: " + toDate);
568

    
569
    Log log = el.getD1Report(null, null, filterDocid, event,
570
        new java.sql.Timestamp(fromDate.getTime()),
571
        new java.sql.Timestamp(toDate.getTime()), false, start, count);
572
    
573
    logMetacat.info("getLogRecords");
574
    return log;
575
  }
576
    
577
  /**
578
   * Return the object identified by the given object identifier
579
   * 
580
   * @param session - the Session object containing the credentials for the Subject
581
   * @param pid - the object identifier for the given object
582
   * 
583
   * TODO: The D1 Authorization API doesn't provide information on which 
584
   * authentication system the Subject belongs to, and so it's not possible to
585
   * discern which Person or Group is a valid KNB LDAP DN.  Fix this.
586
   * 
587
   * @return inputStream - the input stream of the given object
588
   * 
589
   * @throws InvalidToken
590
   * @throws ServiceFailure
591
   * @throws NotAuthorized
592
   * @throws InvalidRequest
593
   * @throws NotImplemented
594
   */
595
  public InputStream get(Session session, Identifier pid) 
596
    throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
597
    NotImplemented {
598
    
599
    String serviceFailureCode = "1030";
600
    Identifier sid = getPIDForSID(pid, serviceFailureCode);
601
    if(sid != null) {
602
        pid = sid;
603
    }
604
    
605
    InputStream inputStream = null; // bytes to be returned
606
    handler = new MetacatHandler(new Timer());
607
    boolean allowed = false;
608
    String localId; // the metacat docid for the pid
609
    
610
    // get the local docid from Metacat
611
    try {
612
      localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
613
    
614
    } catch (McdbDocNotFoundException e) {
615
      throw new NotFound("1020", "The object specified by " + 
616
                         pid.getValue() +
617
                         " does not exist at this node.");
618
    } catch (SQLException e) {
619
        throw new ServiceFailure("1030", "The object specified by "+ pid.getValue()+
620
                                  " couldn't be identified at this node since "+e.getMessage());
621
    }
622
    
623
    // check for authorization
624
    try {
625
		allowed = isAuthorized(session, pid, Permission.READ);
626
	} catch (InvalidRequest e) {
627
		throw new ServiceFailure("1030", e.getDescription());
628
	}
629
    
630
    // if the person is authorized, perform the read
631
    if (allowed) {
632
      try {
633
        inputStream = handler.read(localId);
634
      } catch (McdbDocNotFoundException de) {
635
          String error ="";
636
          if(EventLog.getInstance().isDeleted(localId)) {
637
                error=DELETEDMESSAGE;
638
          }
639
          throw new NotFound("1020", "The object specified by " + 
640
                           pid.getValue() +
641
                           " does not exist at this node. "+error);
642
      } catch (Exception e) {
643
        throw new ServiceFailure("1030", "The object specified by " + 
644
            pid.getValue() +
645
            " could not be returned due to error: " +
646
            e.getMessage()+". ");
647
      }
648
    }
649

    
650
    // if we fail to set the input stream
651
    if ( inputStream == null ) {
652
        String error ="";
653
        if(EventLog.getInstance().isDeleted(localId)) {
654
              error=DELETEDMESSAGE;
655
        }
656
        throw new NotFound("1020", "The object specified by " + 
657
                         pid.getValue() +
658
                         " does not exist at this node. "+error);
659
    }
660
    
661
	// log the read event
662
    String principal = Constants.SUBJECT_PUBLIC;
663
    if (session != null && session.getSubject() != null) {
664
    	principal = session.getSubject().getValue();
665
    }
666
    EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), principal, localId, "read");
667
    
668
    return inputStream;
669
  }
670

    
671
  /**
672
   * Return the system metadata for a given object
673
   * 
674
   * @param session - the Session object containing the credentials for the Subject
675
   * @param pid - the object identifier for the given object
676
   * 
677
   * @return inputStream - the input stream of the given system metadata object
678
   * 
679
   * @throws InvalidToken
680
   * @throws ServiceFailure
681
   * @throws NotAuthorized
682
   * @throws NotFound
683
   * @throws InvalidRequest
684
   * @throws NotImplemented
685
   */
686
    public SystemMetadata getSystemMetadata(Session session, Identifier pid)
687
        throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
688
        NotImplemented {
689

    
690
        String serviceFailureCode = "1090";
691
        Identifier sid = getPIDForSID(pid, serviceFailureCode);
692
        if(sid != null) {
693
            pid = sid;
694
        }
695
        boolean isAuthorized = false;
696
        SystemMetadata systemMetadata = null;
697
        List<Replica> replicaList = null;
698
        NodeReference replicaNodeRef = null;
699
        List<Node> nodeListBySubject = null;
700
        Subject subject = null;
701
        
702
        if (session != null ) {
703
            subject = session.getSubject();
704
        }
705
        
706
        // check normal authorization
707
        BaseException originalAuthorizationException = null;
708
        if (!isAuthorized) {
709
            try {
710
                isAuthorized = isAuthorized(session, pid, Permission.READ);
711

    
712
            } catch (InvalidRequest e) {
713
                throw new ServiceFailure("1090", e.getDescription());
714
            } catch (NotAuthorized nae) {
715
            	// catch this for later
716
            	originalAuthorizationException = nae;
717
			}
718
        }
719
        
720
        // get the system metadata first because we need the replica list for auth
721
        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
722
        
723
        // check the replica information to expand access to MNs that might need it
724
        if (!isAuthorized) {
725
        	
726
	        try {
727
	        	
728
	            // if MNs are listed as replicas, allow access
729
	            if ( systemMetadata != null ) {
730
	                replicaList = systemMetadata.getReplicaList();
731
	                // only check if there are in fact replicas listed
732
	                if ( replicaList != null ) {
733
	                    
734
	                    if ( subject != null ) {
735
	                        // get the list of nodes with a matching node subject
736
	                        try {
737
	                            nodeListBySubject = listNodesBySubject(session.getSubject());
738
	
739
	                        } catch (BaseException e) {
740
	                            // Unexpected error contacting the CN via D1Client
741
	                            String msg = "Caught an unexpected error while trying "
742
	                                    + "to potentially authorize system metadata access "
743
	                                    + "based on the session subject. The error was "
744
	                                    + e.getMessage();
745
	                            logMetacat.error(msg);
746
	                            if (logMetacat.isDebugEnabled()) {
747
	                                e.printStackTrace();
748
	
749
	                            }
750
	                            // isAuthorized is still false 
751
	                        }
752
	
753
	                    }
754
	                    if (nodeListBySubject != null) {
755
	                        // compare node ids to replica node ids
756
	                        outer: for (Replica replica : replicaList) {
757
	                            replicaNodeRef = replica.getReplicaMemberNode();
758
	
759
	                            for (Node node : nodeListBySubject) {
760
	                                if (node.getIdentifier().equals(replicaNodeRef)) {
761
	                                    // node id via session subject matches a replica node
762
	                                    isAuthorized = true;
763
	                                    break outer;
764
	                                }
765
	                            }
766
	                        }
767
	                    }
768
	                }
769
	            }
770
	            
771
	            // if we still aren't authorized, then we are done
772
	            if (!isAuthorized) {
773
	                throw new NotAuthorized("1400", Permission.READ
774
	                        + " not allowed on " + pid.getValue());
775
	            }
776

    
777
	        } catch (RuntimeException e) {
778
	        	e.printStackTrace();
779
	            // convert hazelcast RuntimeException to ServiceFailure
780
	            throw new ServiceFailure("1090", "Unexpected error getting system metadata for: " + 
781
	                pid.getValue());	
782
	        }
783
	        
784
        }
785
        
786
        // It wasn't in the map
787
        if ( systemMetadata == null ) {
788
            String error ="";
789
            String localId = null;
790
            try {
791
                localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
792
              
793
             } catch (Exception e) {
794
                logMetacat.warn("Couldn't find the local id for the pid "+pid.getValue());
795
            }
796
            
797
            if(localId != null && EventLog.getInstance().isDeleted(localId)) {
798
                error = DELETEDMESSAGE;
799
            } else if (localId == null && EventLog.getInstance().isDeleted(pid.getValue())) {
800
                error = DELETEDMESSAGE;
801
            }
802
            throw new NotFound("1420", "No record found for: " + pid.getValue()+". "+error);
803
        }
804
        
805
        return systemMetadata;
806
    }
807
     
808
    
809
    /**
810
     * Test if the specified session represents the authoritative member node for the
811
     * given object specified by the identifier. According the the DataONE documentation, 
812
     * the authoritative member node has all the rights of the *rightsHolder*.
813
     * @param session - the Session object containing the credentials for the Subject
814
     * @param pid - the Identifier of the data object
815
     * @return true if the session represents the authoritative mn.
816
     * @throws ServiceFailure 
817
     * @throws NotImplemented 
818
     */
819
    public boolean isAuthoritativeMNodeAdmin(Session session, Identifier pid) {
820
        boolean allowed = false;
821
        //check the parameters
822
        if(session == null) {
823
            logMetacat.debug("D1NodeService.isAuthoritativeMNodeAdmin - the session object is null and return false.");
824
            return allowed;
825
        } else if (pid == null || pid.getValue() == null || pid.getValue().trim().equals("")) {
826
            logMetacat.debug("D1NodeService.isAuthoritativeMNodeAdmin - the Identifier object is null (not being specified) and return false.");
827
            return allowed;
828
        }
829
        
830
        //Get the subject from the session
831
        Subject subject = session.getSubject();
832
        if(subject != null) {
833
            //Get the authoritative member node info from the system metadata
834
            SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
835
            if(sysMeta != null) {
836
                NodeReference authoritativeMNode = sysMeta.getAuthoritativeMemberNode();
837
                if(authoritativeMNode != null) {
838
                        CNode cn = null;
839
                        try {
840
                            cn = D1Client.getCN();
841
                        } catch (BaseException e) {
842
                            logMetacat.error("D1NodeService.isAuthoritativeMNodeAdmin - couldn't connect to the CN since "+
843
                                            e.getDescription()+ ". The false value will be returned for the AuthoritativeMNodeAdmin.");
844
                            return allowed;
845
                        }
846
                        
847
                        if(cn != null) {
848
                            List<Node> nodes = null;
849
                            try {
850
                                nodes = cn.listNodes().getNodeList();
851
                            } catch (NotImplemented e) {
852
                                logMetacat.error("D1NodeService.isAuthoritativeMNodeAdmin - couldn't get the member nodes list from the CN since "+e.getDescription()+ 
853
                                                ". The false value will be returned for the AuthoritativeMNodeAdmin.");
854
                                return allowed;
855
                            } catch (ServiceFailure ee) {
856
                                logMetacat.error("D1NodeService.isAuthoritativeMNodeAdmin - couldn't get the member nodes list from the CN since "+ee.getDescription()+ 
857
                                                ". The false value will be returned for the AuthoritativeMNodeAdmin.");
858
                                return allowed;
859
                            }
860
                            if(nodes != null) {
861
                                for(Node node : nodes) {
862
                                    //find the authoritative node and get its subjects
863
                                    if (node.getType() == NodeType.MN && node.getIdentifier() != null && node.getIdentifier().equals(authoritativeMNode)) {
864
                                        List<Subject> nodeSubjects = node.getSubjectList();
865
                                        if(nodeSubjects != null) {
866
                                            // check if the session subject is in the node subject list
867
                                            for (Subject nodeSubject : nodeSubjects) {
868
                                                logMetacat.debug("D1NodeService.isAuthoritativeMNodeAdmin(), comparing subjects: " +
869
                                                    nodeSubject.getValue() + " and " + subject.getValue());
870
                                                if ( nodeSubject != null && nodeSubject.equals(subject) ) {
871
                                                    allowed = true; // subject of session == target node subject
872
                                                    break;
873
                                                }
874
                                            }              
875
                                        }
876
                                      
877
                                    }
878
                                }
879
                            }
880
                        }
881
                }
882
            }
883
        }
884
        return allowed;
885
    }
886
    
887
    
888
  /**
889
   * Test if the user identified by the provided token has administrative authorization 
890
   * 
891
   * @param session - the Session object containing the credentials for the Subject
892
   * 
893
   * @return true if the user is admin
894
   * 
895
   * @throws ServiceFailure
896
   * @throws InvalidToken
897
   * @throws NotFound
898
   * @throws NotAuthorized
899
   * @throws NotImplemented
900
   */
901
  public boolean isAdminAuthorized(Session session) 
902
      throws ServiceFailure, InvalidToken, NotAuthorized,
903
      NotImplemented {
904

    
905
      boolean allowed = false;
906
      
907
      // must have a session in order to check admin 
908
      if (session == null) {
909
         logMetacat.debug("In isAdminAuthorized(), session is null ");
910
         return false;
911
      }
912
      
913
      logMetacat.debug("In isAdminAuthorized(), checking CN or MN authorization for " +
914
           session.getSubject().getValue());
915
      
916
      // check if this is the node calling itself (MN)
917
      allowed = isNodeAdmin(session);
918
      
919
      // check the CN list
920
      if (!allowed) {
921
	      List<Node> nodes = null;
922

    
923
    	  try {
924
		      // are we allowed to do this? only CNs are allowed
925
		      CNode cn = D1Client.getCN();
926
		      nodes = cn.listNodes().getNodeList();
927
    	  }
928
	      catch (Throwable e) {
929
	    	  logMetacat.warn(e.getMessage());
930
	    	  return false;  
931
	      }
932
		      
933
	      if ( nodes == null ) {
934
	    	  return false;
935
	          //throw new ServiceFailure("4852", "Couldn't get node list.");
936
	      }
937
	      
938
	      // find the node in the node list
939
	      for ( Node node : nodes ) {
940
	          
941
	          NodeReference nodeReference = node.getIdentifier();
942
	          logMetacat.debug("In isAdminAuthorized(), Node reference is: " + nodeReference.getValue());
943
	          
944
	          Subject subject = session.getSubject();
945
	          
946
	          if (node.getType() == NodeType.CN) {
947
	              List<Subject> nodeSubjects = node.getSubjectList();
948
	              
949
	              // check if the session subject is in the node subject list
950
	              for (Subject nodeSubject : nodeSubjects) {
951
	                  logMetacat.debug("In isAdminAuthorized(), comparing subjects: " +
952
	                      nodeSubject.getValue() + " and " + subject.getValue());
953
	                  if ( nodeSubject.equals(subject) ) {
954
	                      allowed = true; // subject of session == target node subject
955
	                      break;
956
	                      
957
	                  }
958
	              }              
959
	          }
960
	      }
961
      }
962
      
963
      return allowed;
964
  }
965
  
966
  /**
967
   * Test if the user identified by the provided token has administrative authorization 
968
   * on this node because they are calling themselves
969
   * 
970
   * @param session - the Session object containing the credentials for the Subject
971
   * 
972
   * @return true if the user is this node
973
   * @throws ServiceFailure 
974
   * @throws NotImplemented 
975
   */
976
  public boolean isNodeAdmin(Session session) throws NotImplemented, ServiceFailure {
977

    
978
      boolean allowed = false;
979
      
980
      // must have a session in order to check admin 
981
      if (session == null) {
982
         logMetacat.debug("In isNodeAdmin(), session is null ");
983
         return false;
984
      }
985
      
986
      logMetacat.debug("In isNodeAdmin(), MN authorization for " +
987
           session.getSubject().getValue());
988
      
989
      Node node = MNodeService.getInstance(request).getCapabilities();
990
      NodeReference nodeReference = node.getIdentifier();
991
      logMetacat.debug("In isNodeAdmin(), Node reference is: " + nodeReference.getValue());
992
      
993
      Subject subject = session.getSubject();
994
      
995
      if (node.getType() == NodeType.MN) {
996
          List<Subject> nodeSubjects = node.getSubjectList();
997
          
998
          // check if the session subject is in the node subject list
999
          for (Subject nodeSubject : nodeSubjects) {
1000
              logMetacat.debug("In isNodeAdmin(), comparing subjects: " +
1001
                  nodeSubject.getValue() + " and " + subject.getValue());
1002
              if ( nodeSubject.equals(subject) ) {
1003
                  allowed = true; // subject of session == this node's subect
1004
                  break;
1005
              }
1006
          }              
1007
      }
1008
      
1009
      return allowed;
1010
  }
1011
  
1012
  /**
1013
   * Test if the user identified by the provided token has authorization 
1014
   * for the operation on the specified object.
1015
   * 
1016
   * @param session - the Session object containing the credentials for the Subject
1017
   * @param pid - The identifer of the resource for which access is being checked
1018
   * @param operation - The type of operation which is being requested for the given pid
1019
   *
1020
   * @return true if the operation is allowed
1021
   * 
1022
   * @throws ServiceFailure
1023
   * @throws InvalidToken
1024
   * @throws NotFound
1025
   * @throws NotAuthorized
1026
   * @throws NotImplemented
1027
   * @throws InvalidRequest
1028
   */
1029
  public boolean isAuthorized(Session session, Identifier pid, Permission permission)
1030
    throws ServiceFailure, InvalidToken, NotFound, NotAuthorized,
1031
    NotImplemented, InvalidRequest {
1032

    
1033
    boolean allowed = false;
1034
    
1035
    if (permission == null) {
1036
    	throw new InvalidRequest("1761", "Permission was not provided or is invalid");
1037
    }
1038
    
1039
    // permissions are hierarchical
1040
    List<Permission> expandedPermissions = null;
1041
    
1042
    // always allow CN access
1043
    if ( isAdminAuthorized(session) ) {
1044
        allowed = true;
1045
        return allowed;
1046
        
1047
    }
1048
    
1049
    String serviceFailureCode = "1760";
1050
    Identifier sid = getPIDForSID(pid, serviceFailureCode);
1051
    if(sid != null) {
1052
        pid = sid;
1053
    }
1054
    
1055
    // the authoritative member node of the pid always has the access as well.
1056
    if (isAuthoritativeMNodeAdmin(session, pid)) {
1057
        allowed = true;
1058
        return allowed;
1059
    }
1060
    
1061
    // get the subject[s] from the session
1062
	//defer to the shared util for recursively compiling the subjects	
1063
	Set<Subject> subjects = AuthUtils.authorizedClientSubjects(session);
1064
    
1065
	// track the identities we have checked against
1066
	StringBuffer includedSubjects = new StringBuffer();
1067
    	
1068
    // get the system metadata
1069
    String pidStr = pid.getValue();
1070
    SystemMetadata systemMetadata = null;
1071
    try {
1072
        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1073

    
1074
    } catch (Exception e) {
1075
        // convert Hazelcast RuntimeException to NotFound
1076
        logMetacat.error("An error occurred while getting system metadata for identifier " +
1077
            pid.getValue() + ". The error message was: " + e.getMessage());
1078
        throw new NotFound("1800", "No record found for " + pidStr);
1079
        
1080
    } 
1081
    
1082
    // throw not found if it was not found
1083
    if (systemMetadata == null) {
1084
        String localId = null;
1085
        String error = "No system metadata could be found for given PID: " + pidStr;
1086
        try {
1087
            localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1088
          
1089
         } catch (Exception e) {
1090
            logMetacat.warn("Couldn't find the local id for the pid "+pidStr);
1091
        }
1092
        
1093
        if(localId != null && EventLog.getInstance().isDeleted(localId)) {
1094
            error = error + ". "+DELETEDMESSAGE;
1095
        } else if (localId == null && EventLog.getInstance().isDeleted(pid.getValue())) {
1096
            error = error + ". "+DELETEDMESSAGE;
1097
        }
1098
        throw new NotFound("1800", error);
1099
    }
1100
	    
1101
    // do we own it?
1102
    for (Subject s: subjects) {
1103
      logMetacat.debug("Comparing \t" + 
1104
                       systemMetadata.getRightsHolder().getValue() +
1105
                       " \tagainst \t" + s.getValue());
1106
      	includedSubjects.append(s.getValue() + "; ");
1107
    	allowed = systemMetadata.getRightsHolder().equals(s);
1108
    	if (allowed) {
1109
    		return allowed;
1110
    	}
1111
    }    
1112
    
1113
    // otherwise check the access rules
1114
    try {
1115
	    List<AccessRule> allows = systemMetadata.getAccessPolicy().getAllowList();
1116
	    search: // label break
1117
	    for (AccessRule accessRule: allows) {
1118
	      for (Subject s: subjects) {
1119
	        logMetacat.debug("Checking allow access rule for subject: " + s.getValue());
1120
	        if (accessRule.getSubjectList().contains(s)) {
1121
	        	logMetacat.debug("Access rule contains subject: " + s.getValue());
1122
	        	for (Permission p: accessRule.getPermissionList()) {
1123
		        	logMetacat.debug("Checking permission: " + p.xmlValue());
1124
	        		expandedPermissions = expandPermissions(p);
1125
	        		allowed = expandedPermissions.contains(permission);
1126
	        		if (allowed) {
1127
			        	logMetacat.info("Permission granted: " + p.xmlValue() + " to " + s.getValue());
1128
	        			break search; //label break
1129
	        		}
1130
	        	}
1131
        		
1132
	        }
1133
	      }
1134
	    }
1135
    } catch (Exception e) {
1136
    	// catch all for errors - safe side should be to deny the access
1137
    	logMetacat.error("Problem checking authorization - defaulting to deny", e);
1138
		allowed = false;
1139
	  
1140
    }
1141
    
1142
    // throw or return?
1143
    if (!allowed) {
1144
      throw new NotAuthorized("1820", permission + " not allowed on " + pidStr + " for subject[s]: " + includedSubjects.toString() );
1145
    }
1146
    
1147
    return allowed;
1148
    
1149
  }
1150
  
1151
  /*
1152
   * parse a logEntry and get the relevant field from it
1153
   * 
1154
   * @param fieldname
1155
   * @param entry
1156
   * @return
1157
   */
1158
  private String getLogEntryField(String fieldname, String entry) {
1159
    String begin = "<" + fieldname + ">";
1160
    String end = "</" + fieldname + ">";
1161
    // logMetacat.debug("looking for " + begin + " and " + end +
1162
    // " in entry " + entry);
1163
    String s = entry.substring(entry.indexOf(begin) + begin.length(), entry
1164
        .indexOf(end));
1165
    logMetacat.debug("entry " + fieldname + " : " + s);
1166
    return s;
1167
  }
1168

    
1169
  /** 
1170
   * Determine if a given object should be treated as an XML science metadata
1171
   * object. 
1172
   * 
1173
   * @param sysmeta - the SystemMetadata describing the object
1174
   * @return true if the object should be treated as science metadata
1175
   */
1176
  public static boolean isScienceMetadata(SystemMetadata sysmeta) {
1177
    
1178
    ObjectFormat objectFormat = null;
1179
    boolean isScienceMetadata = false;
1180
    
1181
    try {
1182
      objectFormat = ObjectFormatCache.getInstance().getFormat(sysmeta.getFormatId());
1183
      if ( objectFormat.getFormatType().equals("METADATA") ) {
1184
      	isScienceMetadata = true;
1185
      	
1186
      }
1187
      
1188
       
1189
    } catch (ServiceFailure e) {
1190
      logMetacat.debug("There was a problem determining if the object identified by" + 
1191
          sysmeta.getIdentifier().getValue() + 
1192
          " is science metadata: " + e.getMessage());
1193
    
1194
    } catch (NotFound e) {
1195
      logMetacat.debug("There was a problem determining if the object identified by" + 
1196
          sysmeta.getIdentifier().getValue() + 
1197
          " is science metadata: " + e.getMessage());
1198
    
1199
    }
1200
    
1201
    return isScienceMetadata;
1202

    
1203
  }
1204
  
1205
  /**
1206
   * Check fro whitespace in the given pid.
1207
   * null pids are also invalid by default
1208
   * @param pid
1209
   * @return
1210
   */
1211
  public static boolean isValidIdentifier(Identifier pid) {
1212
	  if (pid != null && pid.getValue() != null && pid.getValue().length() > 0) {
1213
		  return !pid.getValue().matches(".*\\s+.*");
1214
	  } 
1215
	  return false;
1216
  }
1217
  
1218
  
1219
  /**
1220
   * Insert or update an XML document into Metacat
1221
   * 
1222
   * @param xml - the XML document to insert or update
1223
   * @param pid - the identifier to be used for the resulting object
1224
   * 
1225
   * @return localId - the resulting docid of the document created or updated
1226
   * 
1227
   */
1228
  public String insertOrUpdateDocument(InputStream xml, String encoding,  Identifier pid, 
1229
    Session session, String insertOrUpdate) 
1230
    throws ServiceFailure, IOException {
1231
    
1232
  	logMetacat.debug("Starting to insert xml document...");
1233
    IdentifierManager im = IdentifierManager.getInstance();
1234

    
1235
    // generate pid/localId pair for sysmeta
1236
    String localId = null;
1237
    byte[] xmlBytes  = IOUtils.toByteArray(xml);
1238
    String xmlStr = new String(xmlBytes, encoding);
1239
    if(insertOrUpdate.equals("insert")) {
1240
      localId = im.generateLocalId(pid.getValue(), 1);
1241
      
1242
    } else {
1243
      //localid should already exist in the identifier table, so just find it
1244
      try {
1245
        logMetacat.debug("Updating pid " + pid.getValue());
1246
        logMetacat.debug("looking in identifier table for pid " + pid.getValue());
1247
        
1248
        localId = im.getLocalId(pid.getValue());
1249
        
1250
        logMetacat.debug("localId: " + localId);
1251
        //increment the revision
1252
        String docid = localId.substring(0, localId.lastIndexOf("."));
1253
        String revS = localId.substring(localId.lastIndexOf(".") + 1, localId.length());
1254
        int rev = new Integer(revS).intValue();
1255
        rev++;
1256
        docid = docid + "." + rev;
1257
        localId = docid;
1258
        logMetacat.debug("incremented localId: " + localId);
1259
      
1260
      } catch(McdbDocNotFoundException e) {
1261
        throw new ServiceFailure("1030", "D1NodeService.insertOrUpdateDocument(): " +
1262
            "pid " + pid.getValue() + 
1263
            " should have been in the identifier table, but it wasn't: " + 
1264
            e.getMessage());
1265
      
1266
      } catch (SQLException e) {
1267
          throw new ServiceFailure("1030", "D1NodeService.insertOrUpdateDocument() -"+
1268
                     " couldn't identify if the pid "+pid.getValue()+" is in the identifier table since "+e.getMessage());
1269
      }
1270
      
1271
    }
1272

    
1273
    params = new Hashtable<String, String[]>();
1274
    String[] action = new String[1];
1275
    action[0] = insertOrUpdate;
1276
    params.put("action", action);
1277
    String[] docid = new String[1];
1278
    docid[0] = localId;
1279
    params.put("docid", docid);
1280
    String[] doctext = new String[1];
1281
    doctext[0] = xmlStr;
1282
    params.put("doctext", doctext);
1283
    
1284
    String username = Constants.SUBJECT_PUBLIC;
1285
    String[] groupnames = null;
1286
    if (session != null ) {
1287
    	username = session.getSubject().getValue();
1288
    	if (session.getSubjectInfo() != null) {
1289
    		List<Group> groupList = session.getSubjectInfo().getGroupList();
1290
    		if (groupList != null) {
1291
    			groupnames = new String[groupList.size()];
1292
    			for (int i = 0; i < groupList.size(); i++ ) {
1293
    				groupnames[i] = groupList.get(i).getGroupName();
1294
    			}
1295
    		}
1296
    	}
1297
    }
1298
    
1299
    // do the insert or update action
1300
    handler = new MetacatHandler(new Timer());
1301
    String result = handler.handleInsertOrUpdateAction(request.getRemoteAddr(), request.getHeader("User-Agent"), null, 
1302
                        null, params, username, groupnames, false, false, xmlBytes);
1303
    
1304
    if(result.indexOf("<error>") != -1) {
1305
    	String detailCode = "";
1306
    	if ( insertOrUpdate.equals("insert") ) {
1307
    		// make sure to remove the mapping so that subsequent attempts do not fail with IdentifierNotUnique
1308
    		im.removeMapping(pid.getValue(), localId);
1309
    		detailCode = "1190";
1310
    		
1311
    	} else if ( insertOrUpdate.equals("update") ) {
1312
    		detailCode = "1310";
1313
    		
1314
    	}
1315
        throw new ServiceFailure(detailCode, 
1316
          "Error inserting or updating document: " + result);
1317
    }
1318
    logMetacat.debug("Finsished inserting xml document with id " + localId);
1319
    
1320
    return localId;
1321
  }
1322
  
1323
  /**
1324
   * Insert a data document
1325
   * 
1326
   * @param object
1327
   * @param pid
1328
   * @param sessionData
1329
   * @throws ServiceFailure
1330
   * @returns localId of the data object inserted
1331
   */
1332
  public String insertDataObject(InputStream object, Identifier pid, 
1333
          Session session) throws ServiceFailure {
1334
      
1335
    String username = Constants.SUBJECT_PUBLIC;
1336
    String[] groupnames = null;
1337
    if (session != null ) {
1338
    	username = session.getSubject().getValue();
1339
    	if (session.getSubjectInfo() != null) {
1340
    		List<Group> groupList = session.getSubjectInfo().getGroupList();
1341
    		if (groupList != null) {
1342
    			groupnames = new String[groupList.size()];
1343
    			for (int i = 0; i < groupList.size(); i++ ) {
1344
    				groupnames[i] = groupList.get(i).getGroupName();
1345
    			}
1346
    		}
1347
    	}
1348
    }
1349
  
1350
    // generate pid/localId pair for object
1351
    logMetacat.debug("Generating a pid/localId mapping");
1352
    IdentifierManager im = IdentifierManager.getInstance();
1353
    String localId = im.generateLocalId(pid.getValue(), 1);
1354
  
1355
    // Save the data file to disk using "localId" as the name
1356
    String datafilepath = null;
1357
	try {
1358
		datafilepath = PropertyService.getProperty("application.datafilepath");
1359
	} catch (PropertyNotFoundException e) {
1360
		ServiceFailure sf = new ServiceFailure("1190", "Lookup data file path" + e.getMessage());
1361
		sf.initCause(e);
1362
		throw sf;
1363
	}
1364
    boolean locked = false;
1365
	try {
1366
		locked = DocumentImpl.getDataFileLockGrant(localId);
1367
	} catch (Exception e) {
1368
		ServiceFailure sf = new ServiceFailure("1190", "Could not lock file for writing:" + e.getMessage());
1369
		sf.initCause(e);
1370
		throw sf;
1371
	}
1372

    
1373
    logMetacat.debug("Case DATA: starting to write to disk.");
1374
	if (locked) {
1375

    
1376
          File dataDirectory = new File(datafilepath);
1377
          dataDirectory.mkdirs();
1378
  
1379
          File newFile = writeStreamToFile(dataDirectory, localId, object);
1380
  
1381
          // TODO: Check that the file size matches SystemMetadata
1382
          // long size = newFile.length();
1383
          // if (size == 0) {
1384
          //     throw new IOException("Uploaded file is 0 bytes!");
1385
          // }
1386
  
1387
          // Register the file in the database (which generates an exception
1388
          // if the localId is not acceptable or other untoward things happen
1389
          try {
1390
            logMetacat.debug("Registering document...");
1391
            DocumentImpl.registerDocument(localId, "BIN", localId,
1392
                    username, groupnames);
1393
            logMetacat.debug("Registration step completed.");
1394
            
1395
          } catch (SQLException e) {
1396
            //newFile.delete();
1397
            logMetacat.debug("SQLE: " + e.getMessage());
1398
            e.printStackTrace(System.out);
1399
            throw new ServiceFailure("1190", "Registration failed: " + 
1400
            		e.getMessage());
1401
            
1402
          } catch (AccessionNumberException e) {
1403
            //newFile.delete();
1404
            logMetacat.debug("ANE: " + e.getMessage());
1405
            e.printStackTrace(System.out);
1406
            throw new ServiceFailure("1190", "Registration failed: " + 
1407
            	e.getMessage());
1408
            
1409
          } catch (Exception e) {
1410
            //newFile.delete();
1411
            logMetacat.debug("Exception: " + e.getMessage());
1412
            e.printStackTrace(System.out);
1413
            throw new ServiceFailure("1190", "Registration failed: " + 
1414
            	e.getMessage());
1415
          }
1416
  
1417
          logMetacat.debug("Logging the creation event.");
1418
          EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, "create");
1419
  
1420
          // Schedule replication for this data file, the "insert" action is important here!
1421
          logMetacat.debug("Scheduling replication.");
1422
          ForceReplicationHandler frh = new ForceReplicationHandler(localId, "insert", false, null);
1423
      }
1424
      
1425
      return localId;
1426
    
1427
  }
1428

    
1429
  /**
1430
   * Insert a systemMetadata document and return its localId
1431
   */
1432
  public void insertSystemMetadata(SystemMetadata sysmeta) 
1433
      throws ServiceFailure {
1434
      
1435
  	  logMetacat.debug("Starting to insert SystemMetadata...");
1436
      sysmeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1437
      logMetacat.debug("Inserting new system metadata with modified date " + 
1438
          sysmeta.getDateSysMetadataModified());
1439
      
1440
      //insert the system metadata
1441
      try {
1442
        // note: the calling subclass handles the map hazelcast lock/unlock
1443
      	HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
1444
      	// submit for indexing
1445
        MetacatSolrIndex.getInstance().submit(sysmeta.getIdentifier(), sysmeta, null, true);
1446
      } catch (Exception e) {
1447
          throw new ServiceFailure("1190", e.getMessage());
1448
          
1449
	    }  
1450
  }
1451

    
1452
  /**
1453
   * Update a systemMetadata document
1454
   * 
1455
   * @param sysMeta - the system metadata object in the system to update
1456
   */
1457
    protected void updateSystemMetadata(SystemMetadata sysMeta)
1458
        throws ServiceFailure {
1459

    
1460
        logMetacat.debug("D1NodeService.updateSystemMetadata() called.");
1461
        sysMeta.setDateSysMetadataModified(new Date());
1462
        try {
1463
            HazelcastService.getInstance().getSystemMetadataMap().lock(sysMeta.getIdentifier());
1464
            HazelcastService.getInstance().getSystemMetadataMap().put(sysMeta.getIdentifier(), sysMeta);
1465
            // submit for indexing
1466
            MetacatSolrIndex.getInstance().submit(sysMeta.getIdentifier(), sysMeta, null, true);
1467
        } catch (Exception e) {
1468
            throw new ServiceFailure("4862", e.getMessage());
1469

    
1470
        } finally {
1471
            HazelcastService.getInstance().getSystemMetadataMap().unlock(sysMeta.getIdentifier());
1472

    
1473
        }
1474

    
1475
    }
1476
    
1477
	public boolean updateSystemMetadata(Session session, Identifier pid,
1478
			SystemMetadata sysmeta) throws NotImplemented, NotAuthorized,
1479
			ServiceFailure, InvalidRequest, InvalidSystemMetadata, InvalidToken {
1480
		
1481
		// The lock to be used for this identifier
1482
      Lock lock = null;
1483

    
1484
      // TODO: control who can call this?
1485
      if (session == null) {
1486
          //TODO: many of the thrown exceptions do not use the correct error codes
1487
          //check these against the docs and correct them
1488
          throw new NotAuthorized("4861", "No Session - could not authorize for registration." +
1489
                  "  If you are not logged in, please do so and retry the request.");
1490
      }
1491
      
1492
      // verify that guid == SystemMetadata.getIdentifier()
1493
      logMetacat.debug("Comparing guid|sysmeta_guid: " + pid.getValue() + 
1494
          "|" + sysmeta.getIdentifier().getValue());
1495
      
1496
      if (!pid.getValue().equals(sysmeta.getIdentifier().getValue())) {
1497
          throw new InvalidRequest("4863", 
1498
              "The identifier in method call (" + pid.getValue() + 
1499
              ") does not match identifier in system metadata (" +
1500
              sysmeta.getIdentifier().getValue() + ").");
1501
      }
1502

    
1503
      // do the actual update
1504
      this.updateSystemMetadata(sysmeta);
1505
      
1506
      try {
1507
    	  String localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1508
    	  EventLog.getInstance().log(request.getRemoteAddr(), 
1509
    	          request.getHeader("User-Agent"), session.getSubject().getValue(), 
1510
    	          localId, "updateSystemMetadata");
1511
      } catch (McdbDocNotFoundException e) {
1512
    	  // do nothing, no localId to log with
1513
    	  logMetacat.warn("Could not log 'updateSystemMetadata' event because no localId was found for pid: " + pid.getValue());
1514
      } catch (SQLException e) {
1515
          logMetacat.warn("Could not log 'updateSystemMetadata' event because the localId couldn't be identified for the pid: " + pid.getValue());
1516
      }
1517
      
1518
      return true;
1519
	}
1520
  
1521
  /**
1522
   * Given a Permission, returns a list of all permissions that it encompasses
1523
   * Permissions are hierarchical so that WRITE also allows READ.
1524
   * @param permission
1525
   * @return list of included Permissions for the given permission
1526
   */
1527
  protected List<Permission> expandPermissions(Permission permission) {
1528
	  	List<Permission> expandedPermissions = new ArrayList<Permission>();
1529
	    if (permission.equals(Permission.READ)) {
1530
	    	expandedPermissions.add(Permission.READ);
1531
	    }
1532
	    if (permission.equals(Permission.WRITE)) {
1533
	    	expandedPermissions.add(Permission.READ);
1534
	    	expandedPermissions.add(Permission.WRITE);
1535
	    }
1536
	    if (permission.equals(Permission.CHANGE_PERMISSION)) {
1537
	    	expandedPermissions.add(Permission.READ);
1538
	    	expandedPermissions.add(Permission.WRITE);
1539
	    	expandedPermissions.add(Permission.CHANGE_PERMISSION);
1540
	    }
1541
	    return expandedPermissions;
1542
  }
1543

    
1544
  /*
1545
   * Write a stream to a file
1546
   * 
1547
   * @param dir - the directory to write to
1548
   * @param fileName - the file name to write to
1549
   * @param data - the object bytes as an input stream
1550
   * 
1551
   * @return newFile - the new file created
1552
   * 
1553
   * @throws ServiceFailure
1554
   */
1555
  private File writeStreamToFile(File dir, String fileName, InputStream data) 
1556
    throws ServiceFailure {
1557
    
1558
    File newFile = new File(dir, fileName);
1559
    logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
1560

    
1561
    try {
1562
        if (newFile.createNewFile()) {
1563
          // write data stream to desired file
1564
          OutputStream os = new FileOutputStream(newFile);
1565
          long length = IOUtils.copyLarge(data, os);
1566
          os.flush();
1567
          os.close();
1568
        } else {
1569
          logMetacat.debug("File creation failed, or file already exists.");
1570
          throw new ServiceFailure("1190", "File already exists: " + fileName);
1571
        }
1572
    } catch (FileNotFoundException e) {
1573
      logMetacat.debug("FNF: " + e.getMessage());
1574
      throw new ServiceFailure("1190", "File not found: " + fileName + " " 
1575
                + e.getMessage());
1576
    } catch (IOException e) {
1577
      logMetacat.debug("IOE: " + e.getMessage());
1578
      throw new ServiceFailure("1190", "File was not written: " + fileName 
1579
                + " " + e.getMessage());
1580
    }
1581

    
1582
    return newFile;
1583
  }
1584

    
1585
  /*
1586
   * Returns a list of nodes that have been registered with the DataONE infrastructure
1587
   * that match the given session subject
1588
   * @return nodes - List of nodes from the registry with a matching session subject
1589
   * 
1590
   * @throws ServiceFailure
1591
   * @throws NotImplemented
1592
   */
1593
  protected List<Node> listNodesBySubject(Subject subject) 
1594
      throws ServiceFailure, NotImplemented {
1595
      List<Node> nodeList = new ArrayList<Node>();
1596
      
1597
      CNode cn = D1Client.getCN();
1598
      List<Node> nodes = cn.listNodes().getNodeList();
1599
      
1600
      // find the node in the node list
1601
      for ( Node node : nodes ) {
1602
          
1603
          List<Subject> nodeSubjects = node.getSubjectList();
1604
          if (nodeSubjects != null) {    
1605
	          // check if the session subject is in the node subject list
1606
	          for (Subject nodeSubject : nodeSubjects) {
1607
	              if ( nodeSubject.equals(subject) ) { // subject of session == node subject
1608
	                  nodeList.add(node);  
1609
	              }                              
1610
	          }
1611
          }
1612
      }
1613
      
1614
      return nodeList;
1615
      
1616
  }
1617

    
1618
  /**
1619
   * Archives an object, where the object is either a 
1620
   * data object or a science metadata object.
1621
   * 
1622
   * @param session - the Session object containing the credentials for the Subject
1623
   * @param pid - The object identifier to be archived
1624
   * 
1625
   * @return pid - the identifier of the object used for the archiving
1626
   * 
1627
   * @throws InvalidToken
1628
   * @throws ServiceFailure
1629
   * @throws NotAuthorized
1630
   * @throws NotFound
1631
   * @throws NotImplemented
1632
   * @throws InvalidRequest
1633
   */
1634
  public Identifier archive(Session session, Identifier pid) 
1635
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
1636

    
1637
      String localId = null;
1638
      boolean allowed = false;
1639
      String username = Constants.SUBJECT_PUBLIC;
1640
      String[] groupnames = null;
1641
      if (session == null) {
1642
      	throw new InvalidToken("1330", "No session has been provided");
1643
      } else {
1644
          username = session.getSubject().getValue();
1645
          if (session.getSubjectInfo() != null) {
1646
              List<Group> groupList = session.getSubjectInfo().getGroupList();
1647
              if (groupList != null) {
1648
                  groupnames = new String[groupList.size()];
1649
                  for (int i = 0; i < groupList.size(); i++) {
1650
                      groupnames[i] = groupList.get(i).getGroupName();
1651
                  }
1652
              }
1653
          }
1654
      }
1655

    
1656
      // do we have a valid pid?
1657
      if (pid == null || pid.getValue().trim().equals("")) {
1658
          throw new ServiceFailure("1350", "The provided identifier was invalid.");
1659
      }
1660
      
1661
      String serviceFailureCode = "1350";
1662
      Identifier sid = getPIDForSID(pid, serviceFailureCode);
1663
      if(sid != null) {
1664
          pid = sid;
1665
      }
1666

    
1667
      // check for the existing identifier
1668
      try {
1669
          localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1670
      } catch (McdbDocNotFoundException e) {
1671
          throw new NotFound("1340", "The object with the provided " + "identifier was not found.");
1672
      } catch (SQLException e) {
1673
          throw new ServiceFailure("1350", "The object with the provided identifier "+pid.getValue()+" couldn't be identified since "+e.getMessage());
1674
      }
1675

    
1676
      // does the subject have archive (a D1 CHANGE_PERMISSION level) privileges on the pid?
1677
      try {
1678
			allowed = isAuthorized(session, pid, Permission.CHANGE_PERMISSION);
1679
		} catch (InvalidRequest e) {
1680
          throw new ServiceFailure("1350", e.getDescription());
1681
		}
1682
          
1683

    
1684
      if (allowed) {
1685
          try {
1686
              // archive the document
1687
              DocumentImpl.delete(localId, null, null, null, false);
1688
              EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, Event.DELETE.xmlValue());
1689

    
1690
              // archive it
1691
              SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1692
              sysMeta.setArchived(true);
1693
              sysMeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1694
              HazelcastService.getInstance().getSystemMetadataMap().put(pid, sysMeta);
1695
              // submit for indexing
1696
              // DocumentImpl call above should do this.
1697
              // see: https://projects.ecoinformatics.org/ecoinfo/issues/6030
1698
              //HazelcastService.getInstance().getIndexQueue().add(sysMeta);
1699
              
1700
          } catch (McdbDocNotFoundException e) {
1701
              throw new NotFound("1340", "The provided identifier was invalid.");
1702

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

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

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

    
1713
      } else {
1714
          throw new NotAuthorized("1320", "The provided identity does not have " + "permission to archive the object on the Node.");
1715
      }
1716

    
1717
      return pid;
1718
  }
1719
  
1720
  
1721
  /**
1722
   * A utility method for v1 api to check the specified identifier exists as a pid
1723
   * @param identifier  the specified identifier
1724
   * @param serviceFailureCode  the detail error code for the service failure exception
1725
   * @param noFoundCode  the detail error code for the not found exception
1726
   * @throws ServiceFailure
1727
   * @throws NotFound
1728
   */
1729
  public void checkV1SystemMetaPidExist(Identifier identifier, String serviceFailureCode, String serviceFailureMessage,  
1730
          String noFoundCode, String notFoundMessage) throws ServiceFailure, NotFound {
1731
      boolean exists = false;
1732
      try {
1733
          exists = IdentifierManager.getInstance().systemMetadataPIDExists(identifier);
1734
      } catch (SQLException e) {
1735
          throw new ServiceFailure(serviceFailureCode, serviceFailureMessage+" since "+e.getMessage());
1736
      }
1737
      if(!exists) {
1738
         //the v1 method only handles a pid. so it should throw a not-found exception.
1739
          // check if the pid was deleted.
1740
          try {
1741
              String localId = IdentifierManager.getInstance().getLocalId(identifier.getValue());
1742
              if(EventLog.getInstance().isDeleted(localId)) {
1743
                  notFoundMessage=notFoundMessage+" "+DELETEDMESSAGE;
1744
              } 
1745
            } catch (Exception e) {
1746
              logMetacat.info("Couldn't determine if the not-found identifier "+identifier.getValue()+" was deleted since "+e.getMessage());
1747
            }
1748
            throw new NotFound(noFoundCode, notFoundMessage);
1749
      }
1750
  }
1751
  
1752
  /**
1753
   * Utility method to get the PID for an SID. If the specified identifier is not an SID
1754
   * , null will be returned.
1755
   * @param sid  the specified sid
1756
   * @param serviceFailureCode  the detail error code for the service failure exception
1757
   * @return the pid for the sid. If the specified identifier is not an SID, null will be returned.
1758
   * @throws ServiceFailure
1759
   */
1760
  protected Identifier getPIDForSID(Identifier sid, String serviceFailureCode) throws ServiceFailure {
1761
      Identifier id = null;
1762
      String serviceFailureMessage = "The PID "+" couldn't be identified for the sid " + sid.getValue();
1763
      try {
1764
          //determine if the given pid is a sid or not.
1765
          if(IdentifierManager.getInstance().systemMetadataSIDExists(sid)) {
1766
              try {
1767
                  //set the header pid for the sid if the identifier is a sid.
1768
                  id = IdentifierManager.getInstance().getHeadPID(sid);
1769
              } catch (SQLException sqle) {
1770
                  throw new ServiceFailure(serviceFailureCode, serviceFailureMessage+" since "+sqle.getMessage());
1771
              }
1772
              
1773
          }
1774
      } catch (SQLException e) {
1775
          throw new ServiceFailure(serviceFailureCode, serviceFailureMessage + " since "+e.getMessage());
1776
      }
1777
      return id;
1778
  }
1779

    
1780

    
1781
}
(2-2/7)