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: 2015-01-16 17:59:32 -0800 (Fri, 16 Jan 2015) $'
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.v1.ObjectFormatIdentifier;
65
import org.dataone.service.types.v1.ObjectList;
66
import org.dataone.service.types.v2.Log;
67
import org.dataone.service.types.v2.Node;
68
import org.dataone.service.types.v1.Event;
69
import org.dataone.service.types.v1.NodeReference;
70
import org.dataone.service.types.v1.NodeType;
71
import org.dataone.service.types.v2.ObjectFormat;
72
import org.dataone.service.types.v1.Permission;
73
import org.dataone.service.types.v1.Replica;
74
import org.dataone.service.types.v1.Session;
75
import org.dataone.service.types.v1.Subject;
76
import org.dataone.service.types.v2.SystemMetadata;
77
import org.dataone.service.types.v1.util.AuthUtils;
78
import org.dataone.service.types.v1.util.ChecksumUtil;
79
import org.dataone.service.util.Constants;
80

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

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

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

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

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

    
154
  /**
155
   * This method provides a lighter weight mechanism than 
156
   * getSystemMetadata() for a client to determine basic 
157
   * properties of the referenced object.
158
   * 
159
   * @param session - the Session object containing the credentials for the Subject
160
   * @param pid - the identifier of the object to be described
161
   * 
162
   * @return describeResponse - A set of values providing a basic description 
163
   *                            of the object.
164
   * 
165
   * @throws InvalidToken
166
   * @throws ServiceFailure
167
   * @throws NotAuthorized
168
   * @throws NotFound
169
   * @throws NotImplemented
170
   * @throws InvalidRequest
171
   */
172
  public DescribeResponse describe(Session session, Identifier pid) 
173
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
174
      
175
      String serviceFailureCode = "4931";
176
      Identifier sid = getPIDForSID(pid, serviceFailureCode);
177
      if(sid != null) {
178
          pid = sid;
179
      }
180

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

    
188
      return describeResponse;
189

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
465
    return resultPid;
466
  }
467

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

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

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

    
524
    String[] filterDocid = null;
525
    if (pidFilter != null && !pidFilter.trim().equals("")) {
526
        //check if the given identifier is a sid. If it is sid, choose the current pid of the sid.
527
        Identifier pid = new Identifier();
528
        pid.setValue(pidFilter);
529
        String serviceFailureCode = "1490";
530
        Identifier sid = getPIDForSID(pid, serviceFailureCode);
531
        if(sid != null) {
532
            pid = sid;
533
        }
534
        pidFilter = pid.getValue();
535
		try {
536
	      String localId = im.getLocalId(pidFilter);
537
	      filterDocid = new String[] {localId};
538
	    } catch (Exception ex) { 
539
	    	String msg = "Could not find localId for given pidFilter '" + pidFilter + "'";
540
	        logMetacat.warn(msg, ex);
541
	        //throw new InvalidRequest("1480", msg);
542
	        return log; //return 0 record
543
	    }
544
    }
545
    
546
    logMetacat.debug("fromDate: " + fromDate);
547
    logMetacat.debug("toDate: " + toDate);
548

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

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

    
651
  /**
652
   * Return the system metadata for a given object
653
   * 
654
   * @param session - the Session object containing the credentials for the Subject
655
   * @param pid - the object identifier for the given object
656
   * 
657
   * @return inputStream - the input stream of the given system metadata object
658
   * 
659
   * @throws InvalidToken
660
   * @throws ServiceFailure
661
   * @throws NotAuthorized
662
   * @throws NotFound
663
   * @throws InvalidRequest
664
   * @throws NotImplemented
665
   */
666
    public SystemMetadata getSystemMetadata(Session session, Identifier pid)
667
        throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
668
        NotImplemented {
669

    
670
        String serviceFailureCode = "1090";
671
        Identifier sid = getPIDForSID(pid, serviceFailureCode);
672
        if(sid != null) {
673
            pid = sid;
674
        }
675
        boolean isAuthorized = false;
676
        SystemMetadata systemMetadata = null;
677
        List<Replica> replicaList = null;
678
        NodeReference replicaNodeRef = null;
679
        List<Node> nodeListBySubject = null;
680
        Subject subject = null;
681
        
682
        if (session != null ) {
683
            subject = session.getSubject();
684
        }
685
        
686
        // check normal authorization
687
        BaseException originalAuthorizationException = null;
688
        if (!isAuthorized) {
689
            try {
690
                isAuthorized = isAuthorized(session, pid, Permission.READ);
691

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

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

    
885
      boolean allowed = false;
886
      
887
      // must have a session in order to check admin 
888
      if (session == null) {
889
         logMetacat.debug("In isAdminAuthorized(), session is null ");
890
         return false;
891
      }
892
      
893
      logMetacat.debug("In isAdminAuthorized(), checking CN or MN authorization for " +
894
           session.getSubject().getValue());
895
      
896
      // check if this is the node calling itself (MN)
897
      allowed = isNodeAdmin(session);
898
      
899
      // check the CN list
900
      if (!allowed) {
901
	      List<Node> nodes = null;
902

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

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

    
1013
    boolean allowed = false;
1014
    
1015
    if (permission == null) {
1016
    	throw new InvalidRequest("1761", "Permission was not provided or is invalid");
1017
    }
1018
    
1019
    // permissions are hierarchical
1020
    List<Permission> expandedPermissions = null;
1021
    
1022
    // always allow CN access
1023
    if ( isAdminAuthorized(session) ) {
1024
        allowed = true;
1025
        return allowed;
1026
        
1027
    }
1028
    
1029
    String serviceFailureCode = "1760";
1030
    Identifier sid = getPIDForSID(pid, serviceFailureCode);
1031
    if(sid != null) {
1032
        pid = sid;
1033
    }
1034
    
1035
    // the authoritative member node of the pid always has the access as well.
1036
    if (isAuthoritativeMNodeAdmin(session, pid)) {
1037
        allowed = true;
1038
        return allowed;
1039
    }
1040
    
1041
    // get the subject[s] from the session
1042
	//defer to the shared util for recursively compiling the subjects	
1043
	Set<Subject> subjects = AuthUtils.authorizedClientSubjects(session);
1044
    
1045
	// track the identities we have checked against
1046
	StringBuffer includedSubjects = new StringBuffer();
1047
    	
1048
    // get the system metadata
1049
    String pidStr = pid.getValue();
1050
    SystemMetadata systemMetadata = null;
1051
    try {
1052
        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1053

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

    
1149
  /** 
1150
   * Determine if a given object should be treated as an XML science metadata
1151
   * object. 
1152
   * 
1153
   * @param sysmeta - the SystemMetadata describing the object
1154
   * @return true if the object should be treated as science metadata
1155
   */
1156
  public static boolean isScienceMetadata(SystemMetadata sysmeta) {
1157
    
1158
    ObjectFormat objectFormat = null;
1159
    boolean isScienceMetadata = false;
1160
    
1161
    try {
1162
      objectFormat = ObjectFormatCache.getInstance().getFormat(sysmeta.getFormatId());
1163
      if ( objectFormat.getFormatType().equals("METADATA") ) {
1164
      	isScienceMetadata = true;
1165
      	
1166
      }
1167
      
1168
       
1169
    } catch (ServiceFailure e) {
1170
      logMetacat.debug("There was a problem determining if the object identified by" + 
1171
          sysmeta.getIdentifier().getValue() + 
1172
          " is science metadata: " + e.getMessage());
1173
    
1174
    } catch (NotFound e) {
1175
      logMetacat.debug("There was a problem determining if the object identified by" + 
1176
          sysmeta.getIdentifier().getValue() + 
1177
          " is science metadata: " + e.getMessage());
1178
    
1179
    }
1180
    
1181
    return isScienceMetadata;
1182

    
1183
  }
1184
  
1185
  /**
1186
   * Check fro whitespace in the given pid.
1187
   * null pids are also invalid by default
1188
   * @param pid
1189
   * @return
1190
   */
1191
  public static boolean isValidIdentifier(Identifier pid) {
1192
	  if (pid != null && pid.getValue() != null && pid.getValue().length() > 0) {
1193
		  return !pid.getValue().matches(".*\\s+.*");
1194
	  } 
1195
	  return false;
1196
  }
1197
  
1198
  
1199
  /**
1200
   * Insert or update an XML document into Metacat
1201
   * 
1202
   * @param xml - the XML document to insert or update
1203
   * @param pid - the identifier to be used for the resulting object
1204
   * 
1205
   * @return localId - the resulting docid of the document created or updated
1206
   * 
1207
   */
1208
  public String insertOrUpdateDocument(InputStream xml, String encoding,  Identifier pid, 
1209
    Session session, String insertOrUpdate) 
1210
    throws ServiceFailure, IOException {
1211
    
1212
  	logMetacat.debug("Starting to insert xml document...");
1213
    IdentifierManager im = IdentifierManager.getInstance();
1214

    
1215
    // generate pid/localId pair for sysmeta
1216
    String localId = null;
1217
    byte[] xmlBytes  = IOUtils.toByteArray(xml);
1218
    String xmlStr = new String(xmlBytes, encoding);
1219
    if(insertOrUpdate.equals("insert")) {
1220
      localId = im.generateLocalId(pid.getValue(), 1);
1221
      
1222
    } else {
1223
      //localid should already exist in the identifier table, so just find it
1224
      try {
1225
        logMetacat.debug("Updating pid " + pid.getValue());
1226
        logMetacat.debug("looking in identifier table for pid " + pid.getValue());
1227
        
1228
        localId = im.getLocalId(pid.getValue());
1229
        
1230
        logMetacat.debug("localId: " + localId);
1231
        //increment the revision
1232
        String docid = localId.substring(0, localId.lastIndexOf("."));
1233
        String revS = localId.substring(localId.lastIndexOf(".") + 1, localId.length());
1234
        int rev = new Integer(revS).intValue();
1235
        rev++;
1236
        docid = docid + "." + rev;
1237
        localId = docid;
1238
        logMetacat.debug("incremented localId: " + localId);
1239
      
1240
      } catch(McdbDocNotFoundException e) {
1241
        throw new ServiceFailure("1030", "D1NodeService.insertOrUpdateDocument(): " +
1242
            "pid " + pid.getValue() + 
1243
            " should have been in the identifier table, but it wasn't: " + 
1244
            e.getMessage());
1245
      
1246
      } catch (SQLException e) {
1247
          throw new ServiceFailure("1030", "D1NodeService.insertOrUpdateDocument() -"+
1248
                     " couldn't identify if the pid "+pid.getValue()+" is in the identifier table since "+e.getMessage());
1249
      }
1250
      
1251
    }
1252

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

    
1353
    logMetacat.debug("Case DATA: starting to write to disk.");
1354
	if (locked) {
1355

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

    
1409
  /**
1410
   * Insert a systemMetadata document and return its localId
1411
   */
1412
  public void insertSystemMetadata(SystemMetadata sysmeta) 
1413
      throws ServiceFailure {
1414
      
1415
  	  logMetacat.debug("Starting to insert SystemMetadata...");
1416
      sysmeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1417
      logMetacat.debug("Inserting new system metadata with modified date " + 
1418
          sysmeta.getDateSysMetadataModified());
1419
      
1420
      //insert the system metadata
1421
      try {
1422
        // note: the calling subclass handles the map hazelcast lock/unlock
1423
      	HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
1424
      	// submit for indexing
1425
        MetacatSolrIndex.getInstance().submit(sysmeta.getIdentifier(), sysmeta, null, true);
1426
      } catch (Exception e) {
1427
          throw new ServiceFailure("1190", e.getMessage());
1428
          
1429
	    }  
1430
  }
1431
  
1432
  /**
1433
   * Retrieve the list of objects present on the MN that match the calling parameters
1434
   * 
1435
   * @param session - the Session object containing the credentials for the Subject
1436
   * @param startTime - Specifies the beginning of the time range from which 
1437
   *                    to return object (>=)
1438
   * @param endTime - Specifies the beginning of the time range from which 
1439
   *                  to return object (>=)
1440
   * @param objectFormat - Restrict results to the specified object format
1441
   * @param replicaStatus - Indicates if replicated objects should be returned in the list
1442
   * @param start - The zero-based index of the first value, relative to the 
1443
   *                first record of the resultset that matches the parameters.
1444
   * @param count - The maximum number of entries that should be returned in 
1445
   *                the response. The Member Node may return less entries 
1446
   *                than specified in this value.
1447
   * 
1448
   * @return objectList - the list of objects matching the criteria
1449
   * 
1450
   * @throws InvalidToken
1451
   * @throws ServiceFailure
1452
   * @throws NotAuthorized
1453
   * @throws InvalidRequest
1454
   * @throws NotImplemented
1455
   */
1456
  public ObjectList listObjects(Session session, Date startTime, Date endTime, ObjectFormatIdentifier objectFormatId, Identifier identifier, Boolean replicaStatus, Integer start,
1457
          Integer count) throws NotAuthorized, InvalidRequest, NotImplemented, ServiceFailure, InvalidToken {
1458

    
1459
      ObjectList objectList = null;
1460

    
1461
      try {
1462
          // safeguard against large requests
1463
          if (count == null || count > MAXIMUM_DB_RECORD_COUNT) {
1464
              count = MAXIMUM_DB_RECORD_COUNT;
1465
          }
1466
          boolean isSid = false;
1467
          if(identifier != null) {
1468
              isSid = IdentifierManager.getInstance().systemMetadataSIDExists(identifier);
1469
          }
1470
          objectList = IdentifierManager.getInstance().querySystemMetadata(startTime, endTime, objectFormatId, replicaStatus, start, count, identifier, isSid);
1471
      } catch (Exception e) {
1472
          throw new ServiceFailure("1580", "Error querying system metadata: " + e.getMessage());
1473
      }
1474

    
1475
      return objectList;
1476
  }
1477

    
1478
  /**
1479
   * Update a systemMetadata document
1480
   * 
1481
   * @param sysMeta - the system metadata object in the system to update
1482
   */
1483
    protected void updateSystemMetadata(SystemMetadata sysMeta)
1484
        throws ServiceFailure {
1485

    
1486
        logMetacat.debug("D1NodeService.updateSystemMetadata() called.");
1487
        sysMeta.setDateSysMetadataModified(new Date());
1488
        try {
1489
            HazelcastService.getInstance().getSystemMetadataMap().lock(sysMeta.getIdentifier());
1490
            HazelcastService.getInstance().getSystemMetadataMap().put(sysMeta.getIdentifier(), sysMeta);
1491
            // submit for indexing
1492
            MetacatSolrIndex.getInstance().submit(sysMeta.getIdentifier(), sysMeta, null, true);
1493
        } catch (Exception e) {
1494
            throw new ServiceFailure("4862", e.getMessage());
1495

    
1496
        } finally {
1497
            HazelcastService.getInstance().getSystemMetadataMap().unlock(sysMeta.getIdentifier());
1498

    
1499
        }
1500

    
1501
    }
1502
    
1503
	public boolean updateSystemMetadata(Session session, Identifier pid,
1504
			SystemMetadata sysmeta) throws NotImplemented, NotAuthorized,
1505
			ServiceFailure, InvalidRequest, InvalidSystemMetadata, InvalidToken {
1506
		
1507
		// The lock to be used for this identifier
1508
      Lock lock = null;
1509

    
1510
      // TODO: control who can call this?
1511
      if (session == null) {
1512
          //TODO: many of the thrown exceptions do not use the correct error codes
1513
          //check these against the docs and correct them
1514
          throw new NotAuthorized("4861", "No Session - could not authorize for registration." +
1515
                  "  If you are not logged in, please do so and retry the request.");
1516
      }
1517
      
1518
      // verify that guid == SystemMetadata.getIdentifier()
1519
      logMetacat.debug("Comparing guid|sysmeta_guid: " + pid.getValue() + 
1520
          "|" + sysmeta.getIdentifier().getValue());
1521
      
1522
      if (!pid.getValue().equals(sysmeta.getIdentifier().getValue())) {
1523
          throw new InvalidRequest("4863", 
1524
              "The identifier in method call (" + pid.getValue() + 
1525
              ") does not match identifier in system metadata (" +
1526
              sysmeta.getIdentifier().getValue() + ").");
1527
      }
1528

    
1529
      // do the actual update
1530
      this.updateSystemMetadata(sysmeta);
1531
      
1532
      try {
1533
    	  String localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1534
    	  EventLog.getInstance().log(request.getRemoteAddr(), 
1535
    	          request.getHeader("User-Agent"), session.getSubject().getValue(), 
1536
    	          localId, "updateSystemMetadata");
1537
      } catch (McdbDocNotFoundException e) {
1538
    	  // do nothing, no localId to log with
1539
    	  logMetacat.warn("Could not log 'updateSystemMetadata' event because no localId was found for pid: " + pid.getValue());
1540
      } catch (SQLException e) {
1541
          logMetacat.warn("Could not log 'updateSystemMetadata' event because the localId couldn't be identified for the pid: " + pid.getValue());
1542
      }
1543
      
1544
      return true;
1545
	}
1546
  
1547
  /**
1548
   * Given a Permission, returns a list of all permissions that it encompasses
1549
   * Permissions are hierarchical so that WRITE also allows READ.
1550
   * @param permission
1551
   * @return list of included Permissions for the given permission
1552
   */
1553
  protected List<Permission> expandPermissions(Permission permission) {
1554
	  	List<Permission> expandedPermissions = new ArrayList<Permission>();
1555
	    if (permission.equals(Permission.READ)) {
1556
	    	expandedPermissions.add(Permission.READ);
1557
	    }
1558
	    if (permission.equals(Permission.WRITE)) {
1559
	    	expandedPermissions.add(Permission.READ);
1560
	    	expandedPermissions.add(Permission.WRITE);
1561
	    }
1562
	    if (permission.equals(Permission.CHANGE_PERMISSION)) {
1563
	    	expandedPermissions.add(Permission.READ);
1564
	    	expandedPermissions.add(Permission.WRITE);
1565
	    	expandedPermissions.add(Permission.CHANGE_PERMISSION);
1566
	    }
1567
	    return expandedPermissions;
1568
  }
1569

    
1570
  /*
1571
   * Write a stream to a file
1572
   * 
1573
   * @param dir - the directory to write to
1574
   * @param fileName - the file name to write to
1575
   * @param data - the object bytes as an input stream
1576
   * 
1577
   * @return newFile - the new file created
1578
   * 
1579
   * @throws ServiceFailure
1580
   */
1581
  private File writeStreamToFile(File dir, String fileName, InputStream data) 
1582
    throws ServiceFailure {
1583
    
1584
    File newFile = new File(dir, fileName);
1585
    logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
1586

    
1587
    try {
1588
        if (newFile.createNewFile()) {
1589
          // write data stream to desired file
1590
          OutputStream os = new FileOutputStream(newFile);
1591
          long length = IOUtils.copyLarge(data, os);
1592
          os.flush();
1593
          os.close();
1594
        } else {
1595
          logMetacat.debug("File creation failed, or file already exists.");
1596
          throw new ServiceFailure("1190", "File already exists: " + fileName);
1597
        }
1598
    } catch (FileNotFoundException e) {
1599
      logMetacat.debug("FNF: " + e.getMessage());
1600
      throw new ServiceFailure("1190", "File not found: " + fileName + " " 
1601
                + e.getMessage());
1602
    } catch (IOException e) {
1603
      logMetacat.debug("IOE: " + e.getMessage());
1604
      throw new ServiceFailure("1190", "File was not written: " + fileName 
1605
                + " " + e.getMessage());
1606
    }
1607

    
1608
    return newFile;
1609
  }
1610

    
1611
  /*
1612
   * Returns a list of nodes that have been registered with the DataONE infrastructure
1613
   * that match the given session subject
1614
   * @return nodes - List of nodes from the registry with a matching session subject
1615
   * 
1616
   * @throws ServiceFailure
1617
   * @throws NotImplemented
1618
   */
1619
  protected List<Node> listNodesBySubject(Subject subject) 
1620
      throws ServiceFailure, NotImplemented {
1621
      List<Node> nodeList = new ArrayList<Node>();
1622
      
1623
      CNode cn = D1Client.getCN();
1624
      List<Node> nodes = cn.listNodes().getNodeList();
1625
      
1626
      // find the node in the node list
1627
      for ( Node node : nodes ) {
1628
          
1629
          List<Subject> nodeSubjects = node.getSubjectList();
1630
          if (nodeSubjects != null) {    
1631
	          // check if the session subject is in the node subject list
1632
	          for (Subject nodeSubject : nodeSubjects) {
1633
	              if ( nodeSubject.equals(subject) ) { // subject of session == node subject
1634
	                  nodeList.add(node);  
1635
	              }                              
1636
	          }
1637
          }
1638
      }
1639
      
1640
      return nodeList;
1641
      
1642
  }
1643

    
1644
  /**
1645
   * Archives an object, where the object is either a 
1646
   * data object or a science metadata object.
1647
   * 
1648
   * @param session - the Session object containing the credentials for the Subject
1649
   * @param pid - The object identifier to be archived
1650
   * 
1651
   * @return pid - the identifier of the object used for the archiving
1652
   * 
1653
   * @throws InvalidToken
1654
   * @throws ServiceFailure
1655
   * @throws NotAuthorized
1656
   * @throws NotFound
1657
   * @throws NotImplemented
1658
   * @throws InvalidRequest
1659
   */
1660
  public Identifier archive(Session session, Identifier pid) 
1661
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
1662

    
1663
      String localId = null;
1664
      boolean allowed = false;
1665
      String username = Constants.SUBJECT_PUBLIC;
1666
      String[] groupnames = null;
1667
      if (session == null) {
1668
      	throw new InvalidToken("1330", "No session has been provided");
1669
      } else {
1670
          username = session.getSubject().getValue();
1671
          if (session.getSubjectInfo() != null) {
1672
              List<Group> groupList = session.getSubjectInfo().getGroupList();
1673
              if (groupList != null) {
1674
                  groupnames = new String[groupList.size()];
1675
                  for (int i = 0; i < groupList.size(); i++) {
1676
                      groupnames[i] = groupList.get(i).getGroupName();
1677
                  }
1678
              }
1679
          }
1680
      }
1681

    
1682
      // do we have a valid pid?
1683
      if (pid == null || pid.getValue().trim().equals("")) {
1684
          throw new ServiceFailure("1350", "The provided identifier was invalid.");
1685
      }
1686
      
1687
      String serviceFailureCode = "1350";
1688
      Identifier sid = getPIDForSID(pid, serviceFailureCode);
1689
      if(sid != null) {
1690
          pid = sid;
1691
      }
1692

    
1693
      // check for the existing identifier
1694
      try {
1695
          localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1696
      } catch (McdbDocNotFoundException e) {
1697
          throw new NotFound("1340", "The object with the provided " + "identifier was not found.");
1698
      } catch (SQLException e) {
1699
          throw new ServiceFailure("1350", "The object with the provided identifier "+pid.getValue()+" couldn't be identified since "+e.getMessage());
1700
      }
1701

    
1702
      // does the subject have archive (a D1 CHANGE_PERMISSION level) privileges on the pid?
1703
      try {
1704
			allowed = isAuthorized(session, pid, Permission.CHANGE_PERMISSION);
1705
		} catch (InvalidRequest e) {
1706
          throw new ServiceFailure("1350", e.getDescription());
1707
		}
1708
          
1709

    
1710
      if (allowed) {
1711
          try {
1712
              // archive the document
1713
              DocumentImpl.delete(localId, null, null, null, false);
1714
              EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, Event.DELETE.xmlValue());
1715

    
1716
              // archive it
1717
              SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1718
              sysMeta.setArchived(true);
1719
              sysMeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1720
              HazelcastService.getInstance().getSystemMetadataMap().put(pid, sysMeta);
1721
              // submit for indexing
1722
              // DocumentImpl call above should do this.
1723
              // see: https://projects.ecoinformatics.org/ecoinfo/issues/6030
1724
              //HazelcastService.getInstance().getIndexQueue().add(sysMeta);
1725
              
1726
          } catch (McdbDocNotFoundException e) {
1727
              throw new NotFound("1340", "The provided identifier was invalid.");
1728

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

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

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

    
1739
      } else {
1740
          throw new NotAuthorized("1320", "The provided identity does not have " + "permission to archive the object on the Node.");
1741
      }
1742

    
1743
      return pid;
1744
  }
1745
  
1746
  
1747
  /**
1748
   * A utility method for v1 api to check the specified identifier exists as a pid
1749
   * @param identifier  the specified identifier
1750
   * @param serviceFailureCode  the detail error code for the service failure exception
1751
   * @param noFoundCode  the detail error code for the not found exception
1752
   * @throws ServiceFailure
1753
   * @throws NotFound
1754
   */
1755
  public void checkV1SystemMetaPidExist(Identifier identifier, String serviceFailureCode, String serviceFailureMessage,  
1756
          String noFoundCode, String notFoundMessage) throws ServiceFailure, NotFound {
1757
      boolean exists = false;
1758
      try {
1759
          exists = IdentifierManager.getInstance().systemMetadataPIDExists(identifier);
1760
      } catch (SQLException e) {
1761
          throw new ServiceFailure(serviceFailureCode, serviceFailureMessage+" since "+e.getMessage());
1762
      }
1763
      if(!exists) {
1764
         //the v1 method only handles a pid. so it should throw a not-found exception.
1765
          // check if the pid was deleted.
1766
          try {
1767
              String localId = IdentifierManager.getInstance().getLocalId(identifier.getValue());
1768
              if(EventLog.getInstance().isDeleted(localId)) {
1769
                  notFoundMessage=notFoundMessage+" "+DELETEDMESSAGE;
1770
              } 
1771
            } catch (Exception e) {
1772
              logMetacat.info("Couldn't determine if the not-found identifier "+identifier.getValue()+" was deleted since "+e.getMessage());
1773
            }
1774
            throw new NotFound(noFoundCode, notFoundMessage);
1775
      }
1776
  }
1777
  
1778
  /**
1779
   * Utility method to get the PID for an SID. If the specified identifier is not an SID
1780
   * , null will be returned.
1781
   * @param sid  the specified sid
1782
   * @param serviceFailureCode  the detail error code for the service failure exception
1783
   * @return the pid for the sid. If the specified identifier is not an SID, null will be returned.
1784
   * @throws ServiceFailure
1785
   */
1786
  protected Identifier getPIDForSID(Identifier sid, String serviceFailureCode) throws ServiceFailure {
1787
      Identifier id = null;
1788
      String serviceFailureMessage = "The PID "+" couldn't be identified for the sid " + sid.getValue();
1789
      try {
1790
          //determine if the given pid is a sid or not.
1791
          if(IdentifierManager.getInstance().systemMetadataSIDExists(sid)) {
1792
              try {
1793
                  //set the header pid for the sid if the identifier is a sid.
1794
                  id = IdentifierManager.getInstance().getHeadPID(sid);
1795
              } catch (SQLException sqle) {
1796
                  throw new ServiceFailure(serviceFailureCode, serviceFailureMessage+" since "+sqle.getMessage());
1797
              }
1798
              
1799
          }
1800
      } catch (SQLException e) {
1801
          throw new ServiceFailure(serviceFailureCode, serviceFailureMessage + " since "+e.getMessage());
1802
      }
1803
      return id;
1804
  }
1805

    
1806

    
1807
}
(2-2/7)