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

    
415
      // Science metadata (XML) or science data object?
416
      // TODO: there are cases where certain object formats are science metadata
417
      // but are not XML (netCDF ...).  Handle this.
418
      if ( isScienceMetadata(sysmeta) ) {
419
        
420
        // CASE METADATA:
421
      	//String objectAsXML = "";
422
        try {
423
	        //objectAsXML = IOUtils.toString(object, "UTF-8");
424
	        localId = insertOrUpdateDocument(object,"UTF-8", pid, session, "insert");
425
	        //localId = im.getLocalId(pid.getValue());
426

    
427
        } catch (IOException e) {
428
        	String msg = "The Node is unable to create the object. " +
429
          "There was a problem converting the object to XML";
430
        	logMetacat.info(msg);
431
          throw new ServiceFailure("1190", msg + ": " + e.getMessage());
432

    
433
        }
434
                    
435
      } else {
436
	        
437
	      // DEFAULT CASE: DATA (needs to be checked and completed)
438
	      localId = insertDataObject(object, pid, session);
439
      }   
440
    
441
    }
442

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

    
462
    resultPid = pid;
463
    
464
    logMetacat.debug("create() complete for object: " + pid.getValue());
465

    
466
    return resultPid;
467
  }
468

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

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

    
512
    if ( start == null ) {
513
    	start = 0;	
514
    }
515
    
516
    if ( count == null ) {
517
    	count = 1000;
518
    }
519
    
520
    // safeguard against large requests
521
    if (count > MAXIMUM_DB_RECORD_COUNT) {
522
    	count = MAXIMUM_DB_RECORD_COUNT;
523
    }
524

    
525
    String[] filterDocid = null;
526
    if (pidFilter != null) {
527
		try {
528
	      String localId = im.getLocalId(pidFilter);
529
	      filterDocid = new String[] {localId};
530
	    } catch (Exception ex) { 
531
	    	String msg = "Could not find localId for given pidFilter '" + pidFilter + "'";
532
	        logMetacat.warn(msg, ex);
533
	        //throw new InvalidRequest("1480", msg);
534
	    }
535
    }
536
    
537
    logMetacat.debug("fromDate: " + fromDate);
538
    logMetacat.debug("toDate: " + toDate);
539

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

    
621
    // if we fail to set the input stream
622
    if ( inputStream == null ) {
623
        String error ="";
624
        if(EventLog.getInstance().isDeleted(localId)) {
625
              error=DELETEDMESSAGE;
626
        }
627
        throw new NotFound("1020", "The object specified by " + 
628
                         pid.getValue() +
629
                         " does not exist at this node. "+error);
630
    }
631
    
632
	// log the read event
633
    String principal = Constants.SUBJECT_PUBLIC;
634
    if (session != null && session.getSubject() != null) {
635
    	principal = session.getSubject().getValue();
636
    }
637
    EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), principal, localId, "read");
638
    
639
    return inputStream;
640
  }
641

    
642
  /**
643
   * Return the system metadata for a given object
644
   * 
645
   * @param session - the Session object containing the credentials for the Subject
646
   * @param pid - the object identifier for the given object
647
   * 
648
   * @return inputStream - the input stream of the given system metadata object
649
   * 
650
   * @throws InvalidToken
651
   * @throws ServiceFailure
652
   * @throws NotAuthorized
653
   * @throws NotFound
654
   * @throws InvalidRequest
655
   * @throws NotImplemented
656
   */
657
    public SystemMetadata getSystemMetadata(Session session, Identifier pid)
658
        throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
659
        NotImplemented {
660

    
661
        String serviceFailureCode = "1090";
662
        Identifier sid = getPIDForSID(pid, serviceFailureCode);
663
        if(sid != null) {
664
            pid = sid;
665
        }
666
        boolean isAuthorized = false;
667
        SystemMetadata systemMetadata = null;
668
        List<Replica> replicaList = null;
669
        NodeReference replicaNodeRef = null;
670
        List<Node> nodeListBySubject = null;
671
        Subject subject = null;
672
        
673
        if (session != null ) {
674
            subject = session.getSubject();
675
        }
676
        
677
        // check normal authorization
678
        BaseException originalAuthorizationException = null;
679
        if (!isAuthorized) {
680
            try {
681
                isAuthorized = isAuthorized(session, pid, Permission.READ);
682

    
683
            } catch (InvalidRequest e) {
684
                throw new ServiceFailure("1090", e.getDescription());
685
            } catch (NotAuthorized nae) {
686
            	// catch this for later
687
            	originalAuthorizationException = nae;
688
			}
689
        }
690
        
691
        // get the system metadata first because we need the replica list for auth
692
        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
693
        
694
        // check the replica information to expand access to MNs that might need it
695
        if (!isAuthorized) {
696
        	
697
	        try {
698
	        	
699
	            // if MNs are listed as replicas, allow access
700
	            if ( systemMetadata != null ) {
701
	                replicaList = systemMetadata.getReplicaList();
702
	                // only check if there are in fact replicas listed
703
	                if ( replicaList != null ) {
704
	                    
705
	                    if ( subject != null ) {
706
	                        // get the list of nodes with a matching node subject
707
	                        try {
708
	                            nodeListBySubject = listNodesBySubject(session.getSubject());
709
	
710
	                        } catch (BaseException e) {
711
	                            // Unexpected error contacting the CN via D1Client
712
	                            String msg = "Caught an unexpected error while trying "
713
	                                    + "to potentially authorize system metadata access "
714
	                                    + "based on the session subject. The error was "
715
	                                    + e.getMessage();
716
	                            logMetacat.error(msg);
717
	                            if (logMetacat.isDebugEnabled()) {
718
	                                e.printStackTrace();
719
	
720
	                            }
721
	                            // isAuthorized is still false 
722
	                        }
723
	
724
	                    }
725
	                    if (nodeListBySubject != null) {
726
	                        // compare node ids to replica node ids
727
	                        outer: for (Replica replica : replicaList) {
728
	                            replicaNodeRef = replica.getReplicaMemberNode();
729
	
730
	                            for (Node node : nodeListBySubject) {
731
	                                if (node.getIdentifier().equals(replicaNodeRef)) {
732
	                                    // node id via session subject matches a replica node
733
	                                    isAuthorized = true;
734
	                                    break outer;
735
	                                }
736
	                            }
737
	                        }
738
	                    }
739
	                }
740
	            }
741
	            
742
	            // if we still aren't authorized, then we are done
743
	            if (!isAuthorized) {
744
	                throw new NotAuthorized("1400", Permission.READ
745
	                        + " not allowed on " + pid.getValue());
746
	            }
747

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

    
876
      boolean allowed = false;
877
      
878
      // must have a session in order to check admin 
879
      if (session == null) {
880
         logMetacat.debug("In isAdminAuthorized(), session is null ");
881
         return false;
882
      }
883
      
884
      logMetacat.debug("In isAdminAuthorized(), checking CN or MN authorization for " +
885
           session.getSubject().getValue());
886
      
887
      // check if this is the node calling itself (MN)
888
      allowed = isNodeAdmin(session);
889
      
890
      // check the CN list
891
      if (!allowed) {
892
	      List<Node> nodes = null;
893

    
894
    	  try {
895
		      // are we allowed to do this? only CNs are allowed
896
		      CNode cn = D1Client.getCN();
897
		      nodes = cn.listNodes().getNodeList();
898
    	  }
899
	      catch (Throwable e) {
900
	    	  logMetacat.warn(e.getMessage());
901
	    	  return false;  
902
	      }
903
		      
904
	      if ( nodes == null ) {
905
	    	  return false;
906
	          //throw new ServiceFailure("4852", "Couldn't get node list.");
907
	      }
908
	      
909
	      // find the node in the node list
910
	      for ( Node node : nodes ) {
911
	          
912
	          NodeReference nodeReference = node.getIdentifier();
913
	          logMetacat.debug("In isAdminAuthorized(), Node reference is: " + nodeReference.getValue());
914
	          
915
	          Subject subject = session.getSubject();
916
	          
917
	          if (node.getType() == NodeType.CN) {
918
	              List<Subject> nodeSubjects = node.getSubjectList();
919
	              
920
	              // check if the session subject is in the node subject list
921
	              for (Subject nodeSubject : nodeSubjects) {
922
	                  logMetacat.debug("In isAdminAuthorized(), comparing subjects: " +
923
	                      nodeSubject.getValue() + " and " + subject.getValue());
924
	                  if ( nodeSubject.equals(subject) ) {
925
	                      allowed = true; // subject of session == target node subject
926
	                      break;
927
	                      
928
	                  }
929
	              }              
930
	          }
931
	      }
932
      }
933
      
934
      return allowed;
935
  }
936
  
937
  /**
938
   * Test if the user identified by the provided token has administrative authorization 
939
   * on this node because they are calling themselves
940
   * 
941
   * @param session - the Session object containing the credentials for the Subject
942
   * 
943
   * @return true if the user is this node
944
   * @throws ServiceFailure 
945
   * @throws NotImplemented 
946
   */
947
  public boolean isNodeAdmin(Session session) throws NotImplemented, ServiceFailure {
948

    
949
      boolean allowed = false;
950
      
951
      // must have a session in order to check admin 
952
      if (session == null) {
953
         logMetacat.debug("In isNodeAdmin(), session is null ");
954
         return false;
955
      }
956
      
957
      logMetacat.debug("In isNodeAdmin(), MN authorization for " +
958
           session.getSubject().getValue());
959
      
960
      Node node = MNodeService.getInstance(request).getCapabilities();
961
      NodeReference nodeReference = node.getIdentifier();
962
      logMetacat.debug("In isNodeAdmin(), Node reference is: " + nodeReference.getValue());
963
      
964
      Subject subject = session.getSubject();
965
      
966
      if (node.getType() == NodeType.MN) {
967
          List<Subject> nodeSubjects = node.getSubjectList();
968
          
969
          // check if the session subject is in the node subject list
970
          for (Subject nodeSubject : nodeSubjects) {
971
              logMetacat.debug("In isNodeAdmin(), comparing subjects: " +
972
                  nodeSubject.getValue() + " and " + subject.getValue());
973
              if ( nodeSubject.equals(subject) ) {
974
                  allowed = true; // subject of session == this node's subect
975
                  break;
976
              }
977
          }              
978
      }
979
      
980
      return allowed;
981
  }
982
  
983
  /**
984
   * Test if the user identified by the provided token has authorization 
985
   * for the operation on the specified object.
986
   * 
987
   * @param session - the Session object containing the credentials for the Subject
988
   * @param pid - The identifer of the resource for which access is being checked
989
   * @param operation - The type of operation which is being requested for the given pid
990
   *
991
   * @return true if the operation is allowed
992
   * 
993
   * @throws ServiceFailure
994
   * @throws InvalidToken
995
   * @throws NotFound
996
   * @throws NotAuthorized
997
   * @throws NotImplemented
998
   * @throws InvalidRequest
999
   */
1000
  public boolean isAuthorized(Session session, Identifier pid, Permission permission)
1001
    throws ServiceFailure, InvalidToken, NotFound, NotAuthorized,
1002
    NotImplemented, InvalidRequest {
1003

    
1004
    boolean allowed = false;
1005
    
1006
    if (permission == null) {
1007
    	throw new InvalidRequest("1761", "Permission was not provided or is invalid");
1008
    }
1009
    
1010
    // permissions are hierarchical
1011
    List<Permission> expandedPermissions = null;
1012
    
1013
    // always allow CN access
1014
    if ( isAdminAuthorized(session) ) {
1015
        allowed = true;
1016
        return allowed;
1017
        
1018
    }
1019
    
1020
    // the authoritative member node of the pid always has the access as well.
1021
    if (isAuthoritativeMNodeAdmin(session, pid)) {
1022
        allowed = true;
1023
        return allowed;
1024
    }
1025
    
1026
    // get the subject[s] from the session
1027
	//defer to the shared util for recursively compiling the subjects	
1028
	Set<Subject> subjects = AuthUtils.authorizedClientSubjects(session);
1029
    
1030
	// track the identities we have checked against
1031
	StringBuffer includedSubjects = new StringBuffer();
1032
    	
1033
    // get the system metadata
1034
    String pidStr = pid.getValue();
1035
    SystemMetadata systemMetadata = null;
1036
    try {
1037
        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1038

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

    
1134
  /** 
1135
   * Determine if a given object should be treated as an XML science metadata
1136
   * object. 
1137
   * 
1138
   * @param sysmeta - the SystemMetadata describing the object
1139
   * @return true if the object should be treated as science metadata
1140
   */
1141
  public static boolean isScienceMetadata(SystemMetadata sysmeta) {
1142
    
1143
    ObjectFormat objectFormat = null;
1144
    boolean isScienceMetadata = false;
1145
    
1146
    try {
1147
      objectFormat = ObjectFormatCache.getInstance().getFormat(sysmeta.getFormatId());
1148
      if ( objectFormat.getFormatType().equals("METADATA") ) {
1149
      	isScienceMetadata = true;
1150
      	
1151
      }
1152
      
1153
       
1154
    } catch (ServiceFailure e) {
1155
      logMetacat.debug("There was a problem determining if the object identified by" + 
1156
          sysmeta.getIdentifier().getValue() + 
1157
          " is science metadata: " + e.getMessage());
1158
    
1159
    } catch (NotFound e) {
1160
      logMetacat.debug("There was a problem determining if the object identified by" + 
1161
          sysmeta.getIdentifier().getValue() + 
1162
          " is science metadata: " + e.getMessage());
1163
    
1164
    }
1165
    
1166
    return isScienceMetadata;
1167

    
1168
  }
1169
  
1170
  /**
1171
   * Check fro whitespace in the given pid.
1172
   * null pids are also invalid by default
1173
   * @param pid
1174
   * @return
1175
   */
1176
  public static boolean isValidIdentifier(Identifier pid) {
1177
	  if (pid != null && pid.getValue() != null && pid.getValue().length() > 0) {
1178
		  return !pid.getValue().matches(".*\\s+.*");
1179
	  } 
1180
	  return false;
1181
  }
1182
  
1183
  
1184
  /**
1185
   * Insert or update an XML document into Metacat
1186
   * 
1187
   * @param xml - the XML document to insert or update
1188
   * @param pid - the identifier to be used for the resulting object
1189
   * 
1190
   * @return localId - the resulting docid of the document created or updated
1191
   * 
1192
   */
1193
  public String insertOrUpdateDocument(InputStream xml, String encoding,  Identifier pid, 
1194
    Session session, String insertOrUpdate) 
1195
    throws ServiceFailure, IOException {
1196
    
1197
  	logMetacat.debug("Starting to insert xml document...");
1198
    IdentifierManager im = IdentifierManager.getInstance();
1199

    
1200
    // generate pid/localId pair for sysmeta
1201
    String localId = null;
1202
    byte[] xmlBytes  = IOUtils.toByteArray(xml);
1203
    String xmlStr = new String(xmlBytes, encoding);
1204
    if(insertOrUpdate.equals("insert")) {
1205
      localId = im.generateLocalId(pid.getValue(), 1);
1206
      
1207
    } else {
1208
      //localid should already exist in the identifier table, so just find it
1209
      try {
1210
        logMetacat.debug("Updating pid " + pid.getValue());
1211
        logMetacat.debug("looking in identifier table for pid " + pid.getValue());
1212
        
1213
        localId = im.getLocalId(pid.getValue());
1214
        
1215
        logMetacat.debug("localId: " + localId);
1216
        //increment the revision
1217
        String docid = localId.substring(0, localId.lastIndexOf("."));
1218
        String revS = localId.substring(localId.lastIndexOf(".") + 1, localId.length());
1219
        int rev = new Integer(revS).intValue();
1220
        rev++;
1221
        docid = docid + "." + rev;
1222
        localId = docid;
1223
        logMetacat.debug("incremented localId: " + localId);
1224
      
1225
      } catch(McdbDocNotFoundException e) {
1226
        throw new ServiceFailure("1030", "D1NodeService.insertOrUpdateDocument(): " +
1227
            "pid " + pid.getValue() + 
1228
            " should have been in the identifier table, but it wasn't: " + 
1229
            e.getMessage());
1230
      
1231
      } catch (SQLException e) {
1232
          throw new ServiceFailure("1030", "D1NodeService.insertOrUpdateDocument() -"+
1233
                     " couldn't identify if the pid "+pid.getValue()+" is in the identifier table since "+e.getMessage());
1234
      }
1235
      
1236
    }
1237

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

    
1338
    logMetacat.debug("Case DATA: starting to write to disk.");
1339
	if (locked) {
1340

    
1341
          File dataDirectory = new File(datafilepath);
1342
          dataDirectory.mkdirs();
1343
  
1344
          File newFile = writeStreamToFile(dataDirectory, localId, object);
1345
  
1346
          // TODO: Check that the file size matches SystemMetadata
1347
          // long size = newFile.length();
1348
          // if (size == 0) {
1349
          //     throw new IOException("Uploaded file is 0 bytes!");
1350
          // }
1351
  
1352
          // Register the file in the database (which generates an exception
1353
          // if the localId is not acceptable or other untoward things happen
1354
          try {
1355
            logMetacat.debug("Registering document...");
1356
            DocumentImpl.registerDocument(localId, "BIN", localId,
1357
                    username, groupnames);
1358
            logMetacat.debug("Registration step completed.");
1359
            
1360
          } catch (SQLException e) {
1361
            //newFile.delete();
1362
            logMetacat.debug("SQLE: " + e.getMessage());
1363
            e.printStackTrace(System.out);
1364
            throw new ServiceFailure("1190", "Registration failed: " + 
1365
            		e.getMessage());
1366
            
1367
          } catch (AccessionNumberException e) {
1368
            //newFile.delete();
1369
            logMetacat.debug("ANE: " + e.getMessage());
1370
            e.printStackTrace(System.out);
1371
            throw new ServiceFailure("1190", "Registration failed: " + 
1372
            	e.getMessage());
1373
            
1374
          } catch (Exception e) {
1375
            //newFile.delete();
1376
            logMetacat.debug("Exception: " + e.getMessage());
1377
            e.printStackTrace(System.out);
1378
            throw new ServiceFailure("1190", "Registration failed: " + 
1379
            	e.getMessage());
1380
          }
1381
  
1382
          logMetacat.debug("Logging the creation event.");
1383
          EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, "create");
1384
  
1385
          // Schedule replication for this data file, the "insert" action is important here!
1386
          logMetacat.debug("Scheduling replication.");
1387
          ForceReplicationHandler frh = new ForceReplicationHandler(localId, "insert", false, null);
1388
      }
1389
      
1390
      return localId;
1391
    
1392
  }
1393

    
1394
  /**
1395
   * Insert a systemMetadata document and return its localId
1396
   */
1397
  public void insertSystemMetadata(SystemMetadata sysmeta) 
1398
      throws ServiceFailure {
1399
      
1400
  	  logMetacat.debug("Starting to insert SystemMetadata...");
1401
      sysmeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1402
      logMetacat.debug("Inserting new system metadata with modified date " + 
1403
          sysmeta.getDateSysMetadataModified());
1404
      
1405
      //insert the system metadata
1406
      try {
1407
        // note: the calling subclass handles the map hazelcast lock/unlock
1408
      	HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
1409
      	// submit for indexing
1410
        MetacatSolrIndex.getInstance().submit(sysmeta.getIdentifier(), sysmeta, null, true);
1411
      } catch (Exception e) {
1412
          throw new ServiceFailure("1190", e.getMessage());
1413
          
1414
	    }  
1415
  }
1416

    
1417
  /**
1418
   * Update a systemMetadata document
1419
   * 
1420
   * @param sysMeta - the system metadata object in the system to update
1421
   */
1422
    protected void updateSystemMetadata(SystemMetadata sysMeta)
1423
        throws ServiceFailure {
1424

    
1425
        logMetacat.debug("D1NodeService.updateSystemMetadata() called.");
1426
        sysMeta.setDateSysMetadataModified(new Date());
1427
        try {
1428
            HazelcastService.getInstance().getSystemMetadataMap().lock(sysMeta.getIdentifier());
1429
            HazelcastService.getInstance().getSystemMetadataMap().put(sysMeta.getIdentifier(), sysMeta);
1430
            // submit for indexing
1431
            MetacatSolrIndex.getInstance().submit(sysMeta.getIdentifier(), sysMeta, null, true);
1432
        } catch (Exception e) {
1433
            throw new ServiceFailure("4862", e.getMessage());
1434

    
1435
        } finally {
1436
            HazelcastService.getInstance().getSystemMetadataMap().unlock(sysMeta.getIdentifier());
1437

    
1438
        }
1439

    
1440
    }
1441
    
1442
	public boolean updateSystemMetadata(Session session, Identifier pid,
1443
			SystemMetadata sysmeta) throws NotImplemented, NotAuthorized,
1444
			ServiceFailure, InvalidRequest, InvalidSystemMetadata, InvalidToken {
1445
		
1446
		// The lock to be used for this identifier
1447
      Lock lock = null;
1448

    
1449
      // TODO: control who can call this?
1450
      if (session == null) {
1451
          //TODO: many of the thrown exceptions do not use the correct error codes
1452
          //check these against the docs and correct them
1453
          throw new NotAuthorized("4861", "No Session - could not authorize for registration." +
1454
                  "  If you are not logged in, please do so and retry the request.");
1455
      }
1456
      
1457
      // verify that guid == SystemMetadata.getIdentifier()
1458
      logMetacat.debug("Comparing guid|sysmeta_guid: " + pid.getValue() + 
1459
          "|" + sysmeta.getIdentifier().getValue());
1460
      
1461
      if (!pid.getValue().equals(sysmeta.getIdentifier().getValue())) {
1462
          throw new InvalidRequest("4863", 
1463
              "The identifier in method call (" + pid.getValue() + 
1464
              ") does not match identifier in system metadata (" +
1465
              sysmeta.getIdentifier().getValue() + ").");
1466
      }
1467

    
1468
      // do the actual update
1469
      this.updateSystemMetadata(sysmeta);
1470
      
1471
      try {
1472
    	  String localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1473
    	  EventLog.getInstance().log(request.getRemoteAddr(), 
1474
    	          request.getHeader("User-Agent"), session.getSubject().getValue(), 
1475
    	          localId, "updateSystemMetadata");
1476
      } catch (McdbDocNotFoundException e) {
1477
    	  // do nothing, no localId to log with
1478
    	  logMetacat.warn("Could not log 'updateSystemMetadata' event because no localId was found for pid: " + pid.getValue());
1479
      } catch (SQLException e) {
1480
          logMetacat.warn("Could not log 'updateSystemMetadata' event because the localId couldn't be identified for the pid: " + pid.getValue());
1481
      }
1482
      
1483
      return true;
1484
	}
1485
  
1486
  /**
1487
   * Given a Permission, returns a list of all permissions that it encompasses
1488
   * Permissions are hierarchical so that WRITE also allows READ.
1489
   * @param permission
1490
   * @return list of included Permissions for the given permission
1491
   */
1492
  protected List<Permission> expandPermissions(Permission permission) {
1493
	  	List<Permission> expandedPermissions = new ArrayList<Permission>();
1494
	    if (permission.equals(Permission.READ)) {
1495
	    	expandedPermissions.add(Permission.READ);
1496
	    }
1497
	    if (permission.equals(Permission.WRITE)) {
1498
	    	expandedPermissions.add(Permission.READ);
1499
	    	expandedPermissions.add(Permission.WRITE);
1500
	    }
1501
	    if (permission.equals(Permission.CHANGE_PERMISSION)) {
1502
	    	expandedPermissions.add(Permission.READ);
1503
	    	expandedPermissions.add(Permission.WRITE);
1504
	    	expandedPermissions.add(Permission.CHANGE_PERMISSION);
1505
	    }
1506
	    return expandedPermissions;
1507
  }
1508

    
1509
  /*
1510
   * Write a stream to a file
1511
   * 
1512
   * @param dir - the directory to write to
1513
   * @param fileName - the file name to write to
1514
   * @param data - the object bytes as an input stream
1515
   * 
1516
   * @return newFile - the new file created
1517
   * 
1518
   * @throws ServiceFailure
1519
   */
1520
  private File writeStreamToFile(File dir, String fileName, InputStream data) 
1521
    throws ServiceFailure {
1522
    
1523
    File newFile = new File(dir, fileName);
1524
    logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
1525

    
1526
    try {
1527
        if (newFile.createNewFile()) {
1528
          // write data stream to desired file
1529
          OutputStream os = new FileOutputStream(newFile);
1530
          long length = IOUtils.copyLarge(data, os);
1531
          os.flush();
1532
          os.close();
1533
        } else {
1534
          logMetacat.debug("File creation failed, or file already exists.");
1535
          throw new ServiceFailure("1190", "File already exists: " + fileName);
1536
        }
1537
    } catch (FileNotFoundException e) {
1538
      logMetacat.debug("FNF: " + e.getMessage());
1539
      throw new ServiceFailure("1190", "File not found: " + fileName + " " 
1540
                + e.getMessage());
1541
    } catch (IOException e) {
1542
      logMetacat.debug("IOE: " + e.getMessage());
1543
      throw new ServiceFailure("1190", "File was not written: " + fileName 
1544
                + " " + e.getMessage());
1545
    }
1546

    
1547
    return newFile;
1548
  }
1549

    
1550
  /*
1551
   * Returns a list of nodes that have been registered with the DataONE infrastructure
1552
   * that match the given session subject
1553
   * @return nodes - List of nodes from the registry with a matching session subject
1554
   * 
1555
   * @throws ServiceFailure
1556
   * @throws NotImplemented
1557
   */
1558
  protected List<Node> listNodesBySubject(Subject subject) 
1559
      throws ServiceFailure, NotImplemented {
1560
      List<Node> nodeList = new ArrayList<Node>();
1561
      
1562
      CNode cn = D1Client.getCN();
1563
      List<Node> nodes = cn.listNodes().getNodeList();
1564
      
1565
      // find the node in the node list
1566
      for ( Node node : nodes ) {
1567
          
1568
          List<Subject> nodeSubjects = node.getSubjectList();
1569
          if (nodeSubjects != null) {    
1570
	          // check if the session subject is in the node subject list
1571
	          for (Subject nodeSubject : nodeSubjects) {
1572
	              if ( nodeSubject.equals(subject) ) { // subject of session == node subject
1573
	                  nodeList.add(node);  
1574
	              }                              
1575
	          }
1576
          }
1577
      }
1578
      
1579
      return nodeList;
1580
      
1581
  }
1582

    
1583
  /**
1584
   * Archives an object, where the object is either a 
1585
   * data object or a science metadata object.
1586
   * 
1587
   * @param session - the Session object containing the credentials for the Subject
1588
   * @param pid - The object identifier to be archived
1589
   * 
1590
   * @return pid - the identifier of the object used for the archiving
1591
   * 
1592
   * @throws InvalidToken
1593
   * @throws ServiceFailure
1594
   * @throws NotAuthorized
1595
   * @throws NotFound
1596
   * @throws NotImplemented
1597
   * @throws InvalidRequest
1598
   */
1599
  public Identifier archive(Session session, Identifier pid) 
1600
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
1601

    
1602
      String localId = null;
1603
      boolean allowed = false;
1604
      String username = Constants.SUBJECT_PUBLIC;
1605
      String[] groupnames = null;
1606
      if (session == null) {
1607
      	throw new InvalidToken("1330", "No session has been provided");
1608
      } else {
1609
          username = session.getSubject().getValue();
1610
          if (session.getSubjectInfo() != null) {
1611
              List<Group> groupList = session.getSubjectInfo().getGroupList();
1612
              if (groupList != null) {
1613
                  groupnames = new String[groupList.size()];
1614
                  for (int i = 0; i < groupList.size(); i++) {
1615
                      groupnames[i] = groupList.get(i).getGroupName();
1616
                  }
1617
              }
1618
          }
1619
      }
1620

    
1621
      // do we have a valid pid?
1622
      if (pid == null || pid.getValue().trim().equals("")) {
1623
          throw new ServiceFailure("1350", "The provided identifier was invalid.");
1624
      }
1625

    
1626
      // check for the existing identifier
1627
      try {
1628
          localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1629
      } catch (McdbDocNotFoundException e) {
1630
          throw new NotFound("1340", "The object with the provided " + "identifier was not found.");
1631
      } catch (SQLException e) {
1632
          throw new ServiceFailure("1350", "The object with the provided identifier "+pid.getValue()+" couldn't be identified since "+e.getMessage());
1633
      }
1634

    
1635
      // does the subject have archive (a D1 CHANGE_PERMISSION level) privileges on the pid?
1636
      try {
1637
			allowed = isAuthorized(session, pid, Permission.CHANGE_PERMISSION);
1638
		} catch (InvalidRequest e) {
1639
          throw new ServiceFailure("1350", e.getDescription());
1640
		}
1641
          
1642

    
1643
      if (allowed) {
1644
          try {
1645
              // archive the document
1646
              DocumentImpl.delete(localId, null, null, null, false);
1647
              EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, Event.DELETE.xmlValue());
1648

    
1649
              // archive it
1650
              SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1651
              sysMeta.setArchived(true);
1652
              sysMeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1653
              HazelcastService.getInstance().getSystemMetadataMap().put(pid, sysMeta);
1654
              // submit for indexing
1655
              // DocumentImpl call above should do this.
1656
              // see: https://projects.ecoinformatics.org/ecoinfo/issues/6030
1657
              //HazelcastService.getInstance().getIndexQueue().add(sysMeta);
1658
              
1659
          } catch (McdbDocNotFoundException e) {
1660
              throw new NotFound("1340", "The provided identifier was invalid.");
1661

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

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

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

    
1672
      } else {
1673
          throw new NotAuthorized("1320", "The provided identity does not have " + "permission to archive the object on the Node.");
1674
      }
1675

    
1676
      return pid;
1677
  }
1678
  
1679
  
1680
  /**
1681
   * A utility method for v1 api to check the specified identifier exists as a pid
1682
   * @param identifier  the specified identifier
1683
   * @param serviceFailureCode  the detail error code for the service failure exception
1684
   * @param noFoundCode  the detail error code for the not found exception
1685
   * @throws ServiceFailure
1686
   * @throws NotFound
1687
   */
1688
  public void checkV1SystemMetaPidExist(Identifier identifier, String serviceFailureCode, String serviceFailureMessage,  
1689
          String noFoundCode, String notFoundMessage) throws ServiceFailure, NotFound {
1690
      boolean exists = false;
1691
      try {
1692
          exists = IdentifierManager.getInstance().systemMetadataPIDExists(identifier);
1693
      } catch (SQLException e) {
1694
          throw new ServiceFailure(serviceFailureCode, serviceFailureMessage+" since "+e.getMessage());
1695
      }
1696
      if(!exists) {
1697
         //the v1 method only handles a pid. so it should throw a not-found exception.
1698
          // check if the pid was deleted.
1699
          try {
1700
              String localId = IdentifierManager.getInstance().getLocalId(identifier.getValue());
1701
              if(EventLog.getInstance().isDeleted(localId)) {
1702
                  notFoundMessage=notFoundMessage+" "+DELETEDMESSAGE;
1703
              } 
1704
            } catch (Exception e) {
1705
              logMetacat.info("Couldn't determine if the not-found identifier "+identifier.getValue()+" was deleted since "+e.getMessage());
1706
            }
1707
            throw new NotFound(noFoundCode, notFoundMessage);
1708
      }
1709
  }
1710
  
1711
  /**
1712
   * Utility method to get the PID for an SID. If the specified identifier is not an SID
1713
   * , null will be returned.
1714
   * @param sid  the specified sid
1715
   * @param serviceFailureCode  the detail error code for the service failure exception
1716
   * @return the pid for the sid. If the specified identifier is not an SID, null will be returned.
1717
   * @throws ServiceFailure
1718
   */
1719
  protected Identifier getPIDForSID(Identifier sid, String serviceFailureCode) throws ServiceFailure {
1720
      Identifier id = null;
1721
      String serviceFailureMessage = "The PID "+" couldn't be identified for the sid " + sid.getValue();
1722
      try {
1723
          //determine if the given pid is a sid or not.
1724
          if(IdentifierManager.getInstance().systemMetadataSIDExists(sid)) {
1725
              try {
1726
                  //set the header pid for the sid if the identifier is a sid.
1727
                  id = IdentifierManager.getInstance().getHeadPID(sid);
1728
              } catch (SQLException sqle) {
1729
                  throw new ServiceFailure(serviceFailureCode, serviceFailureMessage+" since "+sqle.getMessage());
1730
              }
1731
              
1732
          }
1733
      } catch (SQLException e) {
1734
          throw new ServiceFailure(serviceFailureCode, serviceFailureMessage + " since "+e.getMessage());
1735
      }
1736
      return id;
1737
  }
1738

    
1739

    
1740
}
(2-2/7)