Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000-2011 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author: leinfelder $'
7
 *     '$Date: 2013-06-21 11:11:20 -0700 (Fri, 21 Jun 2013) $'
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
 */
23

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

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

    
41
import javax.servlet.http.HttpServletRequest;
42

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

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

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

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

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

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

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

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

    
176
      return describeResponse;
177

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
445
    return resultPid;
446
  }
447

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

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

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

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

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

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

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

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

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

    
701
	        } catch (RuntimeException e) {
702
	        	e.printStackTrace();
703
	            // convert hazelcast RuntimeException to ServiceFailure
704
	            throw new ServiceFailure("1090", "Unexpected error getting system metadata for: " + 
705
	                pid.getValue());	
706
	        }
707
	        
708
        }
709
        
710
        // It wasn't in the map
711
        if ( systemMetadata == null ) {
712
            throw new NotFound("1420", "No record found for: " + pid.getValue());
713
        }
714
        
715
        return systemMetadata;
716
    }
717
     
718
  /**
719
   * Test if the user identified by the provided token has administrative authorization 
720
   * 
721
   * @param session - the Session object containing the credentials for the Subject
722
   * 
723
   * @return true if the user is admin
724
   * 
725
   * @throws ServiceFailure
726
   * @throws InvalidToken
727
   * @throws NotFound
728
   * @throws NotAuthorized
729
   * @throws NotImplemented
730
   */
731
  public boolean isAdminAuthorized(Session session) 
732
      throws ServiceFailure, InvalidToken, NotAuthorized,
733
      NotImplemented {
734

    
735
      boolean allowed = false;
736
      
737
      // must have a session in order to check admin 
738
      if (session == null) {
739
         logMetacat.debug("In isAdminAuthorized(), session is null ");
740
         return false;
741
      }
742
      
743
      logMetacat.debug("In isAdminAuthorized(), checking CN or MN authorization for " +
744
           session.getSubject().getValue());
745
      
746
      // check if this is the node calling itself (MN)
747
      allowed = isNodeAdmin(session);
748
      
749
      // check the CN list
750
      if (!allowed) {
751
	      // are we allowed to do this? only CNs are allowed
752
	      CNode cn = D1Client.getCN();
753
	      List<Node> nodes = cn.listNodes().getNodeList();
754
	      
755
	      if ( nodes == null ) {
756
	          throw new ServiceFailure("4852", "Couldn't get node list.");
757
	  
758
	      }
759
	      
760
	      // find the node in the node list
761
	      for ( Node node : nodes ) {
762
	          
763
	          NodeReference nodeReference = node.getIdentifier();
764
	          logMetacat.debug("In isAdminAuthorized(), Node reference is: " + nodeReference.getValue());
765
	          
766
	          Subject subject = session.getSubject();
767
	          
768
	          if (node.getType() == NodeType.CN) {
769
	              List<Subject> nodeSubjects = node.getSubjectList();
770
	              
771
	              // check if the session subject is in the node subject list
772
	              for (Subject nodeSubject : nodeSubjects) {
773
	                  logMetacat.debug("In isAdminAuthorized(), comparing subjects: " +
774
	                      nodeSubject.getValue() + " and " + subject.getValue());
775
	                  if ( nodeSubject.equals(subject) ) {
776
	                      allowed = true; // subject of session == target node subject
777
	                      break;
778
	                      
779
	                  }
780
	              }              
781
	          }
782
	      }
783
      }
784
      
785
      return allowed;
786
  }
787
  
788
  /**
789
   * Test if the user identified by the provided token has administrative authorization 
790
   * on this node because they are calling themselves
791
   * 
792
   * @param session - the Session object containing the credentials for the Subject
793
   * 
794
   * @return true if the user is this node
795
   * @throws ServiceFailure 
796
   * @throws NotImplemented 
797
   */
798
  public boolean isNodeAdmin(Session session) throws NotImplemented, ServiceFailure {
799

    
800
      boolean allowed = false;
801
      
802
      // must have a session in order to check admin 
803
      if (session == null) {
804
         logMetacat.debug("In isNodeAdmin(), session is null ");
805
         return false;
806
      }
807
      
808
      logMetacat.debug("In isNodeAdmin(), MN authorization for " +
809
           session.getSubject().getValue());
810
      
811
      Node node = MNodeService.getInstance(request).getCapabilities();
812
      NodeReference nodeReference = node.getIdentifier();
813
      logMetacat.debug("In isNodeAdmin(), Node reference is: " + nodeReference.getValue());
814
      
815
      Subject subject = session.getSubject();
816
      
817
      if (node.getType() == NodeType.MN) {
818
          List<Subject> nodeSubjects = node.getSubjectList();
819
          
820
          // check if the session subject is in the node subject list
821
          for (Subject nodeSubject : nodeSubjects) {
822
              logMetacat.debug("In isNodeAdmin(), comparing subjects: " +
823
                  nodeSubject.getValue() + " and " + subject.getValue());
824
              if ( nodeSubject.equals(subject) ) {
825
                  allowed = true; // subject of session == this node's subect
826
                  break;
827
              }
828
          }              
829
      }
830
      
831
      return allowed;
832
  }
833
  
834
  /**
835
   * Test if the user identified by the provided token has authorization 
836
   * for the operation on the specified object.
837
   * 
838
   * @param session - the Session object containing the credentials for the Subject
839
   * @param pid - The identifer of the resource for which access is being checked
840
   * @param operation - The type of operation which is being requested for the given pid
841
   *
842
   * @return true if the operation is allowed
843
   * 
844
   * @throws ServiceFailure
845
   * @throws InvalidToken
846
   * @throws NotFound
847
   * @throws NotAuthorized
848
   * @throws NotImplemented
849
   * @throws InvalidRequest
850
   */
851
  public boolean isAuthorized(Session session, Identifier pid, Permission permission)
852
    throws ServiceFailure, InvalidToken, NotFound, NotAuthorized,
853
    NotImplemented, InvalidRequest {
854

    
855
    boolean allowed = false;
856
    
857
    if (permission == null) {
858
    	throw new InvalidRequest("1761", "Permission was not provided or is invalid");
859
    }
860
    
861
    // permissions are hierarchical
862
    List<Permission> expandedPermissions = null;
863
    
864
    // always allow CN access
865
    if ( isAdminAuthorized(session) ) {
866
        allowed = true;
867
        return allowed;
868
        
869
    }
870
    
871
    // get the subject[s] from the session
872
	//defer to the shared util for recursively compiling the subjects	
873
	Set<Subject> subjects = AuthUtils.authorizedClientSubjects(session);
874
    
875
	// track the identities we have checked against
876
	StringBuffer includedSubjects = new StringBuffer();
877
    	
878
    // get the system metadata
879
    String pidStr = pid.getValue();
880
    SystemMetadata systemMetadata = null;
881
    try {
882
        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
883

    
884
    } catch (Exception e) {
885
        // convert Hazelcast RuntimeException to NotFound
886
        logMetacat.error("An error occurred while getting system metadata for identifier " +
887
            pid.getValue() + ". The error message was: " + e.getMessage());
888
        throw new NotFound("1800", "No record found for " + pidStr);
889
        
890
    } 
891
    
892
    // throw not found if it was not found
893
    if (systemMetadata == null) {
894
    	throw new NotFound("1800", "No system metadata could be found for given PID: " + pidStr);
895
    }
896
	    
897
    // do we own it?
898
    for (Subject s: subjects) {
899
      logMetacat.debug("Comparing \t" + 
900
                       systemMetadata.getRightsHolder().getValue() +
901
                       " \tagainst \t" + s.getValue());
902
      	includedSubjects.append(s.getValue() + "; ");
903
    	allowed = systemMetadata.getRightsHolder().equals(s);
904
    	if (allowed) {
905
    		return allowed;
906
    	}
907
    }    
908
    
909
    // otherwise check the access rules
910
    try {
911
	    List<AccessRule> allows = systemMetadata.getAccessPolicy().getAllowList();
912
	    search: // label break
913
	    for (AccessRule accessRule: allows) {
914
	      for (Subject s: subjects) {
915
	        logMetacat.debug("Checking allow access rule for subject: " + s.getValue());
916
	        if (accessRule.getSubjectList().contains(s)) {
917
	        	logMetacat.debug("Access rule contains subject: " + s.getValue());
918
	        	for (Permission p: accessRule.getPermissionList()) {
919
		        	logMetacat.debug("Checking permission: " + p.xmlValue());
920
	        		expandedPermissions = expandPermissions(p);
921
	        		allowed = expandedPermissions.contains(permission);
922
	        		if (allowed) {
923
			        	logMetacat.info("Permission granted: " + p.xmlValue() + " to " + s.getValue());
924
	        			break search; //label break
925
	        		}
926
	        	}
927
        		
928
	        }
929
	      }
930
	    }
931
    } catch (Exception e) {
932
    	// catch all for errors - safe side should be to deny the access
933
    	logMetacat.error("Problem checking authorization - defaulting to deny", e);
934
		allowed = false;
935
	  
936
    }
937
    
938
    // throw or return?
939
    if (!allowed) {
940
      throw new NotAuthorized("1820", permission + " not allowed on " + pidStr + " for subject[s]: " + includedSubjects.toString() );
941
    }
942
    
943
    return allowed;
944
    
945
  }
946
  
947
  /*
948
   * parse a logEntry and get the relevant field from it
949
   * 
950
   * @param fieldname
951
   * @param entry
952
   * @return
953
   */
954
  private String getLogEntryField(String fieldname, String entry) {
955
    String begin = "<" + fieldname + ">";
956
    String end = "</" + fieldname + ">";
957
    // logMetacat.debug("looking for " + begin + " and " + end +
958
    // " in entry " + entry);
959
    String s = entry.substring(entry.indexOf(begin) + begin.length(), entry
960
        .indexOf(end));
961
    logMetacat.debug("entry " + fieldname + " : " + s);
962
    return s;
963
  }
964

    
965
  /** 
966
   * Determine if a given object should be treated as an XML science metadata
967
   * object. 
968
   * 
969
   * @param sysmeta - the SystemMetadata describing the object
970
   * @return true if the object should be treated as science metadata
971
   */
972
  public static boolean isScienceMetadata(SystemMetadata sysmeta) {
973
    
974
    ObjectFormat objectFormat = null;
975
    boolean isScienceMetadata = false;
976
    
977
    try {
978
      objectFormat = ObjectFormatCache.getInstance().getFormat(sysmeta.getFormatId());
979
      if ( objectFormat.getFormatType().equals("METADATA") ) {
980
      	isScienceMetadata = true;
981
      	
982
      }
983
      
984
       
985
    } catch (ServiceFailure e) {
986
      logMetacat.debug("There was a problem determining if the object identified by" + 
987
          sysmeta.getIdentifier().getValue() + 
988
          " is science metadata: " + e.getMessage());
989
    
990
    } catch (NotFound e) {
991
      logMetacat.debug("There was a problem determining if the object identified by" + 
992
          sysmeta.getIdentifier().getValue() + 
993
          " is science metadata: " + e.getMessage());
994
    
995
    }
996
    
997
    return isScienceMetadata;
998

    
999
  }
1000
  
1001
  /**
1002
   * Check fro whitespace in the given pid.
1003
   * null pids are also invalid by default
1004
   * @param pid
1005
   * @return
1006
   */
1007
  public static boolean isValidIdentifier(Identifier pid) {
1008
	  if (pid != null && pid.getValue() != null && pid.getValue().length() > 0) {
1009
		  return !pid.getValue().matches(".*\\s+.*");
1010
	  } 
1011
	  return false;
1012
  }
1013
  
1014
  
1015
  /**
1016
   * Insert or update an XML document into Metacat
1017
   * 
1018
   * @param xml - the XML document to insert or update
1019
   * @param pid - the identifier to be used for the resulting object
1020
   * 
1021
   * @return localId - the resulting docid of the document created or updated
1022
   * 
1023
   */
1024
  public String insertOrUpdateDocument(String xml, Identifier pid, 
1025
    Session session, String insertOrUpdate) 
1026
    throws ServiceFailure {
1027
    
1028
  	logMetacat.debug("Starting to insert xml document...");
1029
    IdentifierManager im = IdentifierManager.getInstance();
1030

    
1031
    // generate pid/localId pair for sysmeta
1032
    String localId = null;
1033
    
1034
    if(insertOrUpdate.equals("insert")) {
1035
      localId = im.generateLocalId(pid.getValue(), 1);
1036
      
1037
    } else {
1038
      //localid should already exist in the identifier table, so just find it
1039
      try {
1040
        logMetacat.debug("Updating pid " + pid.getValue());
1041
        logMetacat.debug("looking in identifier table for pid " + pid.getValue());
1042
        
1043
        localId = im.getLocalId(pid.getValue());
1044
        
1045
        logMetacat.debug("localId: " + localId);
1046
        //increment the revision
1047
        String docid = localId.substring(0, localId.lastIndexOf("."));
1048
        String revS = localId.substring(localId.lastIndexOf(".") + 1, localId.length());
1049
        int rev = new Integer(revS).intValue();
1050
        rev++;
1051
        docid = docid + "." + rev;
1052
        localId = docid;
1053
        logMetacat.debug("incremented localId: " + localId);
1054
      
1055
      } catch(McdbDocNotFoundException e) {
1056
        throw new ServiceFailure("1030", "D1NodeService.insertOrUpdateDocument(): " +
1057
            "pid " + pid.getValue() + 
1058
            " should have been in the identifier table, but it wasn't: " + 
1059
            e.getMessage());
1060
      
1061
      }
1062
      
1063
    }
1064

    
1065
    params = new Hashtable<String, String[]>();
1066
    String[] action = new String[1];
1067
    action[0] = insertOrUpdate;
1068
    params.put("action", action);
1069
    String[] docid = new String[1];
1070
    docid[0] = localId;
1071
    params.put("docid", docid);
1072
    String[] doctext = new String[1];
1073
    doctext[0] = xml;
1074
    params.put("doctext", doctext);
1075
    
1076
    String username = Constants.SUBJECT_PUBLIC;
1077
    String[] groupnames = null;
1078
    if (session != null ) {
1079
    	username = session.getSubject().getValue();
1080
    	if (session.getSubjectInfo() != null) {
1081
    		List<Group> groupList = session.getSubjectInfo().getGroupList();
1082
    		if (groupList != null) {
1083
    			groupnames = new String[groupList.size()];
1084
    			for (int i = 0; i > groupList.size(); i++ ) {
1085
    				groupnames[i] = groupList.get(i).getGroupName();
1086
    			}
1087
    		}
1088
    	}
1089
    }
1090
    
1091
    // do the insert or update action
1092
    handler = new MetacatHandler(new Timer());
1093
    String result = handler.handleInsertOrUpdateAction(request.getRemoteAddr(), request.getHeader("User-Agent"), null, 
1094
                        null, params, username, groupnames, false, false);
1095
    
1096
    if(result.indexOf("<error>") != -1) {
1097
    	String detailCode = "";
1098
    	if ( insertOrUpdate.equals("insert") ) {
1099
    		// make sure to remove the mapping so that subsequent attempts do not fail with IdentifierNotUnique
1100
    		im.removeMapping(pid.getValue(), localId);
1101
    		detailCode = "1190";
1102
    		
1103
    	} else if ( insertOrUpdate.equals("update") ) {
1104
    		detailCode = "1310";
1105
    		
1106
    	}
1107
        throw new ServiceFailure(detailCode, 
1108
          "Error inserting or updating document: " + result);
1109
    }
1110
    logMetacat.debug("Finsished inserting xml document with id " + localId);
1111
    
1112
    return localId;
1113
  }
1114
  
1115
  /**
1116
   * Insert a data document
1117
   * 
1118
   * @param object
1119
   * @param pid
1120
   * @param sessionData
1121
   * @throws ServiceFailure
1122
   * @returns localId of the data object inserted
1123
   */
1124
  public String insertDataObject(InputStream object, Identifier pid, 
1125
          Session session) throws ServiceFailure {
1126
      
1127
    String username = Constants.SUBJECT_PUBLIC;
1128
    String[] groupnames = null;
1129
    if (session != null ) {
1130
    	username = session.getSubject().getValue();
1131
    	if (session.getSubjectInfo() != null) {
1132
    		List<Group> groupList = session.getSubjectInfo().getGroupList();
1133
    		if (groupList != null) {
1134
    			groupnames = new String[groupList.size()];
1135
    			for (int i = 0; i > groupList.size(); i++ ) {
1136
    				groupnames[i] = groupList.get(i).getGroupName();
1137
    			}
1138
    		}
1139
    	}
1140
    }
1141
  
1142
    // generate pid/localId pair for object
1143
    logMetacat.debug("Generating a pid/localId mapping");
1144
    IdentifierManager im = IdentifierManager.getInstance();
1145
    String localId = im.generateLocalId(pid.getValue(), 1);
1146
  
1147
    // Save the data file to disk using "localId" as the name
1148
    String datafilepath = null;
1149
	try {
1150
		datafilepath = PropertyService.getProperty("application.datafilepath");
1151
	} catch (PropertyNotFoundException e) {
1152
		ServiceFailure sf = new ServiceFailure("1190", "Lookup data file path" + e.getMessage());
1153
		sf.initCause(e);
1154
		throw sf;
1155
	}
1156
    boolean locked = false;
1157
	try {
1158
		locked = DocumentImpl.getDataFileLockGrant(localId);
1159
	} catch (Exception e) {
1160
		ServiceFailure sf = new ServiceFailure("1190", "Could not lock file for writing:" + e.getMessage());
1161
		sf.initCause(e);
1162
		throw sf;
1163
	}
1164

    
1165
    logMetacat.debug("Case DATA: starting to write to disk.");
1166
	if (locked) {
1167

    
1168
          File dataDirectory = new File(datafilepath);
1169
          dataDirectory.mkdirs();
1170
  
1171
          File newFile = writeStreamToFile(dataDirectory, localId, object);
1172
  
1173
          // TODO: Check that the file size matches SystemMetadata
1174
          // long size = newFile.length();
1175
          // if (size == 0) {
1176
          //     throw new IOException("Uploaded file is 0 bytes!");
1177
          // }
1178
  
1179
          // Register the file in the database (which generates an exception
1180
          // if the localId is not acceptable or other untoward things happen
1181
          try {
1182
            logMetacat.debug("Registering document...");
1183
            DocumentImpl.registerDocument(localId, "BIN", localId,
1184
                    username, groupnames);
1185
            logMetacat.debug("Registration step completed.");
1186
            
1187
          } catch (SQLException e) {
1188
            //newFile.delete();
1189
            logMetacat.debug("SQLE: " + e.getMessage());
1190
            e.printStackTrace(System.out);
1191
            throw new ServiceFailure("1190", "Registration failed: " + 
1192
            		e.getMessage());
1193
            
1194
          } catch (AccessionNumberException e) {
1195
            //newFile.delete();
1196
            logMetacat.debug("ANE: " + e.getMessage());
1197
            e.printStackTrace(System.out);
1198
            throw new ServiceFailure("1190", "Registration failed: " + 
1199
            	e.getMessage());
1200
            
1201
          } catch (Exception e) {
1202
            //newFile.delete();
1203
            logMetacat.debug("Exception: " + e.getMessage());
1204
            e.printStackTrace(System.out);
1205
            throw new ServiceFailure("1190", "Registration failed: " + 
1206
            	e.getMessage());
1207
          }
1208
  
1209
          logMetacat.debug("Logging the creation event.");
1210
          EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, "create");
1211
  
1212
          // Schedule replication for this data file, the "insert" action is important here!
1213
          logMetacat.debug("Scheduling replication.");
1214
          ForceReplicationHandler frh = new ForceReplicationHandler(localId, "insert", false, null);
1215
      }
1216
      
1217
      return localId;
1218
    
1219
  }
1220

    
1221
  /**
1222
   * Insert a systemMetadata document and return its localId
1223
   */
1224
  public void insertSystemMetadata(SystemMetadata sysmeta) 
1225
      throws ServiceFailure {
1226
      
1227
  	  logMetacat.debug("Starting to insert SystemMetadata...");
1228
      sysmeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1229
      logMetacat.debug("Inserting new system metadata with modified date " + 
1230
          sysmeta.getDateSysMetadataModified());
1231
      
1232
      //insert the system metadata
1233
      try {
1234
        // note: the calling subclass handles the map hazelcast lock/unlock
1235
      	HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
1236
      	// submit for indexing
1237
        HazelcastService.getInstance().getIndexQueue().add(sysmeta);
1238
      } catch (Exception e) {
1239
          throw new ServiceFailure("1190", e.getMessage());
1240
          
1241
	    }  
1242
  }
1243

    
1244
  /**
1245
   * Update a systemMetadata document
1246
   * 
1247
   * @param sysMeta - the system metadata object in the system to update
1248
   */
1249
    protected void updateSystemMetadata(SystemMetadata sysMeta)
1250
        throws ServiceFailure {
1251

    
1252
        logMetacat.debug("D1NodeService.updateSystemMetadata() called.");
1253
        sysMeta.setDateSysMetadataModified(new Date());
1254
        try {
1255
            HazelcastService.getInstance().getSystemMetadataMap().lock(sysMeta.getIdentifier());
1256
            HazelcastService.getInstance().getSystemMetadataMap().put(sysMeta.getIdentifier(), sysMeta);
1257
            // submit for indexing
1258
            HazelcastService.getInstance().getIndexQueue().add(sysMeta);
1259
        } catch (Exception e) {
1260
            throw new ServiceFailure("4862", e.getMessage());
1261

    
1262
        } finally {
1263
            HazelcastService.getInstance().getSystemMetadataMap().unlock(sysMeta.getIdentifier());
1264

    
1265
        }
1266

    
1267
    }
1268
  
1269
  /**
1270
   * Given a Permission, returns a list of all permissions that it encompasses
1271
   * Permissions are hierarchical so that WRITE also allows READ.
1272
   * @param permission
1273
   * @return list of included Permissions for the given permission
1274
   */
1275
  protected List<Permission> expandPermissions(Permission permission) {
1276
	  	List<Permission> expandedPermissions = new ArrayList<Permission>();
1277
	    if (permission.equals(Permission.READ)) {
1278
	    	expandedPermissions.add(Permission.READ);
1279
	    }
1280
	    if (permission.equals(Permission.WRITE)) {
1281
	    	expandedPermissions.add(Permission.READ);
1282
	    	expandedPermissions.add(Permission.WRITE);
1283
	    }
1284
	    if (permission.equals(Permission.CHANGE_PERMISSION)) {
1285
	    	expandedPermissions.add(Permission.READ);
1286
	    	expandedPermissions.add(Permission.WRITE);
1287
	    	expandedPermissions.add(Permission.CHANGE_PERMISSION);
1288
	    }
1289
	    return expandedPermissions;
1290
  }
1291

    
1292
  /*
1293
   * Write a stream to a file
1294
   * 
1295
   * @param dir - the directory to write to
1296
   * @param fileName - the file name to write to
1297
   * @param data - the object bytes as an input stream
1298
   * 
1299
   * @return newFile - the new file created
1300
   * 
1301
   * @throws ServiceFailure
1302
   */
1303
  private File writeStreamToFile(File dir, String fileName, InputStream data) 
1304
    throws ServiceFailure {
1305
    
1306
    File newFile = new File(dir, fileName);
1307
    logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
1308

    
1309
    try {
1310
        if (newFile.createNewFile()) {
1311
          // write data stream to desired file
1312
          OutputStream os = new FileOutputStream(newFile);
1313
          long length = IOUtils.copyLarge(data, os);
1314
          os.flush();
1315
          os.close();
1316
        } else {
1317
          logMetacat.debug("File creation failed, or file already exists.");
1318
          throw new ServiceFailure("1190", "File already exists: " + fileName);
1319
        }
1320
    } catch (FileNotFoundException e) {
1321
      logMetacat.debug("FNF: " + e.getMessage());
1322
      throw new ServiceFailure("1190", "File not found: " + fileName + " " 
1323
                + e.getMessage());
1324
    } catch (IOException e) {
1325
      logMetacat.debug("IOE: " + e.getMessage());
1326
      throw new ServiceFailure("1190", "File was not written: " + fileName 
1327
                + " " + e.getMessage());
1328
    }
1329

    
1330
    return newFile;
1331
  }
1332

    
1333
  /*
1334
   * Returns a list of nodes that have been registered with the DataONE infrastructure
1335
   * that match the given session subject
1336
   * @return nodes - List of nodes from the registry with a matching session subject
1337
   * 
1338
   * @throws ServiceFailure
1339
   * @throws NotImplemented
1340
   */
1341
  protected List<Node> listNodesBySubject(Subject subject) 
1342
      throws ServiceFailure, NotImplemented {
1343
      List<Node> nodeList = new ArrayList<Node>();
1344
      
1345
      CNode cn = D1Client.getCN();
1346
      List<Node> nodes = cn.listNodes().getNodeList();
1347
      
1348
      // find the node in the node list
1349
      for ( Node node : nodes ) {
1350
          
1351
          List<Subject> nodeSubjects = node.getSubjectList();
1352
          if (nodeSubjects != null) {    
1353
	          // check if the session subject is in the node subject list
1354
	          for (Subject nodeSubject : nodeSubjects) {
1355
	              if ( nodeSubject.equals(subject) ) { // subject of session == node subject
1356
	                  nodeList.add(node);  
1357
	              }                              
1358
	          }
1359
          }
1360
      }
1361
      
1362
      return nodeList;
1363
      
1364
  }
1365

    
1366
  /**
1367
   * Archives an object, where the object is either a 
1368
   * data object or a science metadata object.
1369
   * 
1370
   * @param session - the Session object containing the credentials for the Subject
1371
   * @param pid - The object identifier to be archived
1372
   * 
1373
   * @return pid - the identifier of the object used for the archiving
1374
   * 
1375
   * @throws InvalidToken
1376
   * @throws ServiceFailure
1377
   * @throws NotAuthorized
1378
   * @throws NotFound
1379
   * @throws NotImplemented
1380
   * @throws InvalidRequest
1381
   */
1382
  public Identifier archive(Session session, Identifier pid) 
1383
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
1384

    
1385
      String localId = null;
1386
      boolean allowed = false;
1387
      String username = Constants.SUBJECT_PUBLIC;
1388
      String[] groupnames = null;
1389
      if (session == null) {
1390
      	throw new InvalidToken("1330", "No session has been provided");
1391
      } else {
1392
          username = session.getSubject().getValue();
1393
          if (session.getSubjectInfo() != null) {
1394
              List<Group> groupList = session.getSubjectInfo().getGroupList();
1395
              if (groupList != null) {
1396
                  groupnames = new String[groupList.size()];
1397
                  for (int i = 0; i > groupList.size(); i++) {
1398
                      groupnames[i] = groupList.get(i).getGroupName();
1399
                  }
1400
              }
1401
          }
1402
      }
1403

    
1404
      // do we have a valid pid?
1405
      if (pid == null || pid.getValue().trim().equals("")) {
1406
          throw new ServiceFailure("1350", "The provided identifier was invalid.");
1407
      }
1408

    
1409
      // check for the existing identifier
1410
      try {
1411
          localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1412
      } catch (McdbDocNotFoundException e) {
1413
          throw new NotFound("1340", "The object with the provided " + "identifier was not found.");
1414
      }
1415

    
1416
      // does the subject have archive (a D1 CHANGE_PERMISSION level) privileges on the pid?
1417
      try {
1418
			allowed = isAuthorized(session, pid, Permission.CHANGE_PERMISSION);
1419
		} catch (InvalidRequest e) {
1420
          throw new ServiceFailure("1350", e.getDescription());
1421
		}
1422
          
1423

    
1424
      if (allowed) {
1425
          try {
1426
              // archive the document
1427
              DocumentImpl.delete(localId, null, null, null, false);
1428
              EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, Event.DELETE.xmlValue());
1429

    
1430
              // archive it
1431
              SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1432
              sysMeta.setArchived(true);
1433
              sysMeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1434
              HazelcastService.getInstance().getSystemMetadataMap().put(pid, sysMeta);
1435
              // submit for indexing
1436
              HazelcastService.getInstance().getIndexQueue().add(sysMeta);
1437
              
1438
          } catch (McdbDocNotFoundException e) {
1439
              throw new NotFound("1340", "The provided identifier was invalid.");
1440

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

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

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

    
1451
      } else {
1452
          throw new NotAuthorized("1320", "The provided identity does not have " + "permission to archive the object on the Node.");
1453
      }
1454

    
1455
      return pid;
1456
  }
1457
  
1458
  public Identifier archive(Identifier pid) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
1459
	  return archive(null, pid);
1460
  }
1461

    
1462

    
1463
}
(2-2/6)