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: 2012-04-30 15:05:02 -0700 (Mon, 30 Apr 2012) $'
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.Arrays;
35
import java.util.Calendar;
36
import java.util.Date;
37
import java.util.Hashtable;
38
import java.util.List;
39
import java.util.Timer;
40
import java.util.Vector;
41

    
42
import javax.servlet.http.HttpServletRequest;
43

    
44

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

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

    
97
public abstract class D1NodeService {
98
  
99
  private static Logger logMetacat = Logger.getLogger(D1NodeService.class);
100

    
101
  /** For logging the operations */
102
  protected HttpServletRequest request;
103
  
104
  /* reference to the metacat handler */
105
  protected MetacatHandler handler;
106
  
107
  /* parameters set in the incoming request */
108
  private Hashtable<String, String[]> params;
109

    
110
  /**
111
   * Constructor - used to set the metacatUrl from a subclass extending D1NodeService
112
   * 
113
   * @param metacatUrl - the URL of the metacat service, including the ending /d1
114
   */
115
  public D1NodeService(HttpServletRequest request) {
116
		this.request = request;
117
	}
118
  
119
  /**
120
   * This method provides a lighter weight mechanism than 
121
   * getSystemMetadata() for a client to determine basic 
122
   * properties of the referenced object.
123
   * 
124
   * @param session - the Session object containing the credentials for the Subject
125
   * @param pid - the identifier of the object to be described
126
   * 
127
   * @return describeResponse - A set of values providing a basic description 
128
   *                            of the object.
129
   * 
130
   * @throws InvalidToken
131
   * @throws ServiceFailure
132
   * @throws NotAuthorized
133
   * @throws NotFound
134
   * @throws NotImplemented
135
   * @throws InvalidRequest
136
   */
137
  public DescribeResponse describe(Session session, Identifier pid) 
138
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
139

    
140
    // get system metadata and construct the describe response
141
      SystemMetadata sysmeta = getSystemMetadata(session, pid);
142
      DescribeResponse describeResponse = 
143
      	new DescribeResponse(sysmeta.getFormatId(), sysmeta.getSize(), 
144
      			sysmeta.getDateSysMetadataModified(),
145
      			sysmeta.getChecksum(), sysmeta.getSerialVersion());
146

    
147
      return describeResponse;
148

    
149
  }
150
  
151
  /**
152
   * Deletes an object from the Member Node, where the object is either a 
153
   * data object or a science metadata object.
154
   * 
155
   * @param session - the Session object containing the credentials for the Subject
156
   * @param pid - The object identifier to be deleted
157
   * 
158
   * @return pid - the identifier of the object used for the deletion
159
   * 
160
   * @throws InvalidToken
161
   * @throws ServiceFailure
162
   * @throws NotAuthorized
163
   * @throws NotFound
164
   * @throws NotImplemented
165
   * @throws InvalidRequest
166
   */
167
  public Identifier delete(Session session, Identifier pid) 
168
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
169

    
170
      String localId = null;
171
      boolean allowed = false;
172
      String username = Constants.SUBJECT_PUBLIC;
173
      String[] groupnames = null;
174
      if (session == null) {
175
      	throw new InvalidToken("1330", "No session has been provided");
176
      } else {
177
          username = session.getSubject().getValue();
178
          if (session.getSubjectInfo() != null) {
179
              List<Group> groupList = session.getSubjectInfo().getGroupList();
180
              if (groupList != null) {
181
                  groupnames = new String[groupList.size()];
182
                  for (int i = 0; i > groupList.size(); i++) {
183
                      groupnames[i] = groupList.get(i).getGroupName();
184
                  }
185
              }
186
          }
187
      }
188

    
189
      // do we have a valid pid?
190
      if (pid == null || pid.getValue().trim().equals("")) {
191
          throw new ServiceFailure("1350", "The provided identifier was invalid.");
192
      }
193

    
194
      // check for the existing identifier
195
      try {
196
          localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
197
      } catch (McdbDocNotFoundException e) {
198
          throw new NotFound("1340", "The object with the provided " + "identifier was not found.");
199
      }
200

    
201
      // does the subject have DELETE (a D1 CHANGE_PERMISSION level) priveleges on the pid?
202
      try {
203
			allowed = isAuthorized(session, pid, Permission.CHANGE_PERMISSION);
204
		} catch (InvalidRequest e) {
205
          throw new ServiceFailure("1350", e.getDescription());
206
		}
207
          
208

    
209
      if (allowed) {
210
          try {
211
              // delete the document
212
        	  //  TODO: the method - or one like it - actually needs to remove content not just archive it
213
              DocumentImpl.delete(localId, username, groupnames, null);
214
              EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, Event.DELETE.xmlValue());
215

    
216
              // archive it
217
              SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
218
              sysMeta.setArchived(true);
219
              HazelcastService.getInstance().getSystemMetadataMap().put(pid, sysMeta);
220
              
221
          } catch (McdbDocNotFoundException e) {
222
              throw new NotFound("1340", "The provided identifier was invalid.");
223

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

    
227
          } catch (InsufficientKarmaException e) {
228
              throw new NotAuthorized("1320", "The provided identity does not have " + "permission to DELETE objects on the Member Node.");
229

    
230
          } catch (Exception e) { // for some reason DocumentImpl throws a general Exception
231
              throw new ServiceFailure("1350", "There was a problem deleting the object." + "The error message was: " + e.getMessage());
232
          }
233

    
234
      } else {
235
          throw new NotAuthorized("1320", "The provided identity does not have " + "permission to DELETE objects on the Member Node.");
236
      }
237

    
238
      return pid;
239
  }
240
  
241
  /**
242
   * Low level, "are you alive" operation. A valid ping response is 
243
   * indicated by a HTTP status of 200.
244
   * 
245
   * @return true if the service is alive
246
   * 
247
   * @throws NotImplemented
248
   * @throws ServiceFailure
249
   * @throws InsufficientResources
250
   */
251
  public Date ping() 
252
      throws NotImplemented, ServiceFailure, InsufficientResources {
253

    
254
      // test if we can get a database connection
255
      int serialNumber = -1;
256
      DBConnection dbConn = null;
257
      try {
258
          dbConn = DBConnectionPool.getDBConnection("MNodeService.ping");
259
          serialNumber = dbConn.getCheckOutSerialNumber();
260
      } catch (SQLException e) {
261
      	ServiceFailure sf = new ServiceFailure("", e.getMessage());
262
      	sf.initCause(e);
263
          throw sf;
264
      } finally {
265
          // Return the database connection
266
          DBConnectionPool.returnDBConnection(dbConn, serialNumber);
267
      }
268

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

    
301
    Identifier resultPid = null;
302
    String localId = null;
303
    boolean allowed = false;
304
    
305
    // check for null session
306
    if (session == null) {
307
    	throw new InvalidToken("4894", "Session is required to WRITE to the Node.");
308
    }
309
    Subject subject = session.getSubject();
310

    
311
    Subject publicSubject = new Subject();
312
    publicSubject.setValue(Constants.SUBJECT_PUBLIC);
313
	// be sure the user is authenticated for create()
314
    if (subject == null || subject.getValue() == null || 
315
        subject.equals(publicSubject) ) {
316
      throw new NotAuthorized("1100", "The provided identity does not have " +
317
        "permission to WRITE to the Node.");
318
      
319
    }
320
    
321
    // verify that pid == SystemMetadata.getIdentifier()
322
    logMetacat.debug("Comparing pid|sysmeta_pid: " + 
323
      pid.getValue() + "|" + sysmeta.getIdentifier().getValue());
324
    if (!pid.getValue().equals(sysmeta.getIdentifier().getValue())) {
325
        throw new InvalidSystemMetadata("1180", 
326
            "The supplied system metadata is invalid. " +
327
            "The identifier " + pid.getValue() + " does not match identifier" +
328
            "in the system metadata identified by " +
329
            sysmeta.getIdentifier().getValue() + ".");
330
        
331
    }
332

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

    
381
      // Science metadata (XML) or science data object?
382
      // TODO: there are cases where certain object formats are science metadata
383
      // but are not XML (netCDF ...).  Handle this.
384
      if ( isScienceMetadata(sysmeta) ) {
385
        
386
        // CASE METADATA:
387
      	String objectAsXML = "";
388
        try {
389
	        objectAsXML = IOUtils.toString(object, "UTF-8");
390
	        localId = insertOrUpdateDocument(objectAsXML, pid, session, "insert");
391
	        //localId = im.getLocalId(pid.getValue());
392

    
393
        } catch (IOException e) {
394
        	String msg = "The Node is unable to create the object. " +
395
          "There was a problem converting the object to XML";
396
        	logMetacat.info(msg);
397
          throw new ServiceFailure("1190", msg + ": " + e.getMessage());
398

    
399
        }
400
                    
401
      } else {
402
	        
403
	      // DEFAULT CASE: DATA (needs to be checked and completed)
404
	      localId = insertDataObject(object, pid, session);
405
      }   
406
    
407
    }
408

    
409
    logMetacat.debug("Done inserting new object: " + pid.getValue());
410
    
411
    // save the sysmeta
412
    try {
413
      // lock and unlock of the pid happens in the subclass
414
    	HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
415
    	
416
    } catch (Exception e) {
417
    	logMetacat.error("Problem creating system metadata: " + pid.getValue(), e);
418
        throw new ServiceFailure("1190", e.getMessage());
419
	}
420
    
421
    // setting the resulting identifier failed
422
    if (localId == null ) {
423
      throw new ServiceFailure("1190", "The Node is unable to create the object. ");
424
    }
425

    
426
    resultPid = pid;
427
    
428
    logMetacat.debug("create() complete for object: " + pid.getValue());
429

    
430
    return resultPid;
431
  }
432

    
433
  /**
434
   * Return the log records associated with a given event between the start and 
435
   * end dates listed given a particular Subject listed in the Session
436
   * 
437
   * @param session - the Session object containing the credentials for the Subject
438
   * @param fromDate - the start date of the desired log records
439
   * @param toDate - the end date of the desired log records
440
   * @param event - restrict log records of a specific event type
441
   * @param start - zero based offset from the first record in the 
442
   *                set of matching log records. Used to assist with 
443
   *                paging the response.
444
   * @param count - maximum number of log records to return in the response. 
445
   *                Used to assist with paging the response.
446
   * 
447
   * @return the desired log records
448
   * 
449
   * @throws InvalidToken
450
   * @throws ServiceFailure
451
   * @throws NotAuthorized
452
   * @throws InvalidRequest
453
   * @throws NotImplemented
454
   */
455
  public Log getLogRecords(Session session, Date fromDate, Date toDate, 
456
      Event event, String pidFilter, Integer start, Integer count) throws InvalidToken, ServiceFailure,
457
      NotAuthorized, InvalidRequest, NotImplemented {
458

    
459

    
460
    Log log = new Log();
461
    List<LogEntry> logs = new Vector<LogEntry>();
462
    IdentifierManager im = IdentifierManager.getInstance();
463
    EventLog el = EventLog.getInstance();
464
    if ( fromDate == null ) {
465
      logMetacat.debug("setting fromdate from null");
466
      fromDate = new Date(1);
467
    }
468
    if ( toDate == null ) {
469
      logMetacat.debug("setting todate from null");
470
      toDate = new Date();
471
    }
472

    
473
    if ( start == null ) {
474
    	start = 0;
475
    	
476
    }
477
    
478
    if ( count == null ) {
479
    	count = 1000;
480
    	
481
    }
482

    
483
    logMetacat.debug("fromDate: " + fromDate);
484
    logMetacat.debug("toDate: " + toDate);
485

    
486
    String report = el.getReport(null, null, null, null,
487
        new java.sql.Timestamp(fromDate.getTime()),
488
        new java.sql.Timestamp(toDate.getTime()), false);
489

    
490
    logMetacat.debug("report: " + report);
491

    
492
    String logEntry = "<logEntry>";
493
    String endLogEntry = "</logEntry>";
494
    int startIndex = 0;
495
    int foundIndex = report.indexOf(logEntry, startIndex);
496
    while (foundIndex != -1) {
497
      // parse out each entry
498
      int endEntryIndex = report.indexOf(endLogEntry, foundIndex);
499
      String entry = report.substring(foundIndex, endEntryIndex);
500
      logMetacat.debug("entry: " + entry);
501
      startIndex = endEntryIndex + endLogEntry.length();
502
      foundIndex = report.indexOf(logEntry, startIndex);
503

    
504
      String entryId = getLogEntryField("entryid", entry);
505
      String ipAddress = getLogEntryField("ipAddress", entry);
506
      String principal = getLogEntryField("principal", entry);
507
      String userAgent = getLogEntryField("userAgent", entry);
508
      String docid = getLogEntryField("docid", entry);
509
      String eventS = getLogEntryField("event", entry);
510
      String dateLogged = getLogEntryField("dateLogged", entry);
511

    
512
      LogEntry le = new LogEntry();
513

    
514
      // handle Metacat -> DataONE event conversion
515
      if (eventS.equalsIgnoreCase("insert")) {
516
    	  eventS = Event.CREATE.xmlValue();
517
      }
518
      Event e = Event.convert(eventS);
519
      if (e == null) { // skip any events that are not Dataone Crud events
520
          logMetacat.warn("skipping unknown event type: '" + eventS + "'");
521
          continue;
522
      }
523
      le.setEvent(e);
524
      le.setEntryId(entryId);
525
      Identifier identifier = new Identifier();
526
      try {
527
        logMetacat.debug("converting docid '" + docid + "' to a pid.");
528
        if (docid == null || docid.trim().equals("") || docid.trim().equals("null")) {
529
          continue;
530
        }
531
        String docidNoRev = DocumentUtil.getSmartDocId(docid);
532
        //docid = docid.substring(0, docid.lastIndexOf("."));
533
        int rev = DocumentUtil.getRevisionFromAccessionNumber(docid);
534
        //int rev = im.getLatestRevForLocalId(docid);
535
        String guid = im.getGUID(docidNoRev, rev);
536
        identifier.setValue(guid);
537
      } catch (Exception ex) { 
538
        // try to get the pid, if that doesn't
539
        // work, just use the local id
540
        // throw new ServiceFailure("1030",
541
        // "Error getting pid for localId " +
542
        // docid + ": " + ex.getMessage());\
543

    
544
          logMetacat.warn("could not find pid for docid '" + docid + "'");
545

    
546
        // skip it if the pid can't be found
547
        continue;
548
      }
549

    
550
      // skip if we are not allowed to read the document in question
551
      // https://redmine.dataone.org/issues/2444
552
      boolean allowed = false;
553
      try {
554
    	  allowed = isAuthorized(session, identifier, Permission.READ);
555
      } catch (NotAuthorized ignore) {}
556
      catch (NotFound nf) {
557
    	  logMetacat.warn("Could not check authorization for pid: " + identifier.getValue(), nf);
558
      }
559
      if (!allowed) {
560
    	  logMetacat.debug(Permission.READ + " not allowed on document: " + identifier.getValue());
561
    	  continue;
562
      }
563
      
564
      le.setIdentifier(identifier);
565
      le.setIpAddress(ipAddress);
566
      Date logDate = DateTimeMarshaller.deserializeDateToUTC(dateLogged);
567
      le.setDateLogged(logDate);
568
      NodeReference memberNode = new NodeReference();
569
      String nodeId = "localhost";
570
      try {
571
          nodeId = PropertyService.getProperty("dataone.nodeId");
572
      } catch (PropertyNotFoundException e1) {
573
          // TODO Auto-generated catch block
574
          e1.printStackTrace();
575
      }
576
      memberNode.setValue(nodeId);
577
      le.setNodeIdentifier(memberNode);
578
      Subject princ = new Subject();
579
      princ.setValue(principal);
580
      le.setSubject(princ);
581
      le.setUserAgent(userAgent);
582

    
583
      // event filtering?
584
      if (event == null) {
585
    	  logs.add(le);
586
      } else if (le.getEvent().equals(event)) {
587
    	  logs.add(le);
588
      }
589
    }
590
    
591
    // d1 paging
592
    int total = logs.size();
593
    if (start != null && count != null) {
594
    	int toIndex = start + count;
595
    	// do not exceed total
596
    	toIndex = Math.min(toIndex, total);
597
    	// do not start greater than total
598
    	start = Math.min(start, total);
599
    	// sub set of the list
600
    	logs = new ArrayList<LogEntry>(logs.subList(start, toIndex));
601
    }
602

    
603
    log.setLogEntryList(logs);
604
    log.setStart(start);
605
    log.setCount(logs.size());
606
    log.setTotal(total);
607
    logMetacat.info("getLogRecords");
608
    return log;
609
  }
610
    
611
  /**
612
   * Return the object identified by the given object identifier
613
   * 
614
   * @param session - the Session object containing the credentials for the Subject
615
   * @param pid - the object identifier for the given object
616
   * 
617
   * TODO: The D1 Authorization API doesn't provide information on which 
618
   * authentication system the Subject belongs to, and so it's not possible to
619
   * discern which Person or Group is a valid KNB LDAP DN.  Fix this.
620
   * 
621
   * @return inputStream - the input stream of the given object
622
   * 
623
   * @throws InvalidToken
624
   * @throws ServiceFailure
625
   * @throws NotAuthorized
626
   * @throws InvalidRequest
627
   * @throws NotImplemented
628
   */
629
  public InputStream get(Session session, Identifier pid) 
630
    throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
631
    NotImplemented {
632
    
633
    InputStream inputStream = null; // bytes to be returned
634
    handler = new MetacatHandler(new Timer());
635
    boolean allowed = false;
636
    String localId; // the metacat docid for the pid
637
    
638
    // get the local docid from Metacat
639
    try {
640
      localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
641
    
642
    } catch (McdbDocNotFoundException e) {
643
      throw new NotFound("1020", "The object specified by " + 
644
                         pid.getValue() +
645
                         " does not exist at this node.");
646
    }
647
    
648
    // check for authorization
649
    try {
650
		allowed = isAuthorized(session, pid, Permission.READ);
651
	} catch (InvalidRequest e) {
652
		throw new ServiceFailure("1030", e.getDescription());
653
	}
654
    
655
    // if the person is authorized, perform the read
656
    if (allowed) {
657
      try {
658
        inputStream = handler.read(localId);
659
      } catch (Exception e) {
660
        throw new ServiceFailure("1020", "The object specified by " + 
661
            pid.getValue() +
662
            "could not be returned due to error: " +
663
            e.getMessage());
664
      }
665
    }
666

    
667
    // if we fail to set the input stream
668
    if ( inputStream == null ) {
669
      throw new NotFound("1020", "The object specified by " + 
670
                         pid.getValue() +
671
                         "does not exist at this node.");
672
    }
673
    
674
	// log the read event
675
    String principal = Constants.SUBJECT_PUBLIC;
676
    if (session != null && session.getSubject() != null) {
677
    	principal = session.getSubject().getValue();
678
    }
679
    EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), principal, localId, "read");
680
    
681
    return inputStream;
682
  }
683

    
684
  /**
685
   * Return the system metadata for a given object
686
   * 
687
   * @param session - the Session object containing the credentials for the Subject
688
   * @param pid - the object identifier for the given object
689
   * 
690
   * @return inputStream - the input stream of the given system metadata object
691
   * 
692
   * @throws InvalidToken
693
   * @throws ServiceFailure
694
   * @throws NotAuthorized
695
   * @throws NotFound
696
   * @throws InvalidRequest
697
   * @throws NotImplemented
698
   */
699
    public SystemMetadata getSystemMetadata(Session session, Identifier pid)
700
        throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
701
        NotImplemented {
702

    
703
        boolean isAuthorized = false;
704
        SystemMetadata systemMetadata = null;
705
        List<Replica> replicaList = null;
706
        NodeReference replicaNodeRef = null;
707
        List<Node> nodeListBySubject = null;
708
        Subject subject = null;
709
        
710
        if (session != null ) {
711
            subject = session.getSubject();
712
        }
713
        
714
        // check normal authorization
715
        BaseException originalAuthorizationException = null;
716
        if (!isAuthorized) {
717
            try {
718
                isAuthorized = isAuthorized(session, pid, Permission.READ);
719

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

    
785
	        } catch (RuntimeException e) {
786
	        	e.printStackTrace();
787
	            // convert hazelcast RuntimeException to ServiceFailure
788
	            throw new ServiceFailure("1090", "Unexpected error getting system metadata for: " + 
789
	                pid.getValue());	
790
	        }
791
	        
792
        }
793
        
794
        // It wasn't in the map
795
        if ( systemMetadata == null ) {
796
            throw new NotFound("1420", "No record found for: " + pid.getValue());
797
        }
798
        
799
        return systemMetadata;
800
    }
801
     
802
  /**
803
   * Test if the user identified by the provided token has administrative authorization 
804
   * 
805
   * @param session - the Session object containing the credentials for the Subject
806
   * 
807
   * @return true if the user is admin
808
   * 
809
   * @throws ServiceFailure
810
   * @throws InvalidToken
811
   * @throws NotFound
812
   * @throws NotAuthorized
813
   * @throws NotImplemented
814
   */
815
  public boolean isAdminAuthorized(Session session) 
816
      throws ServiceFailure, InvalidToken, NotAuthorized,
817
      NotImplemented {
818

    
819
      boolean allowed = false;
820
      
821
      // must have a session in order to check admin 
822
      if (session == null) {
823
         logMetacat.debug("In isAdminAuthorized(), session is null ");
824
         return false;
825
      }
826
      
827
      logMetacat.debug("In isAdminAuthorized(), checking CN or MN authorization for " +
828
           session.getSubject().getValue());
829
      
830
      // are we allowed to do this? only CNs are allowed
831
      CNode cn = D1Client.getCN();
832
      List<Node> nodes = cn.listNodes().getNodeList();
833
      
834
      if ( nodes == null ) {
835
          throw new ServiceFailure("4852", "Couldn't get node list.");
836
  
837
      }
838
      
839
      // find the node in the node list
840
      for ( Node node : nodes ) {
841
          
842
          NodeReference nodeReference = node.getIdentifier();
843
          logMetacat.debug("In isAdminAuthorized(), Node reference is: " + nodeReference.getValue());
844
          
845
          Subject subject = session.getSubject();
846
          
847
          if (node.getType() == NodeType.CN) {
848
              List<Subject> nodeSubjects = node.getSubjectList();
849
              
850
              // check if the session subject is in the node subject list
851
              for (Subject nodeSubject : nodeSubjects) {
852
                  logMetacat.debug("In isAdminAuthorized(), comparing subjects: " +
853
                      nodeSubject.getValue() + " and " + subject.getValue());
854
                  if ( nodeSubject.equals(subject) ) {
855
                      allowed = true; // subject of session == target node subject
856
                      break;
857
                      
858
                  }
859
              }              
860
          }
861
      }
862

    
863
      
864
      return allowed;
865
  }
866
  
867
  /**
868
   * Test if the user identified by the provided token has authorization 
869
   * for the operation on the specified object.
870
   * 
871
   * @param session - the Session object containing the credentials for the Subject
872
   * @param pid - The identifer of the resource for which access is being checked
873
   * @param operation - The type of operation which is being requested for the given pid
874
   *
875
   * @return true if the operation is allowed
876
   * 
877
   * @throws ServiceFailure
878
   * @throws InvalidToken
879
   * @throws NotFound
880
   * @throws NotAuthorized
881
   * @throws NotImplemented
882
   * @throws InvalidRequest
883
   */
884
  public boolean isAuthorized(Session session, Identifier pid, Permission permission)
885
    throws ServiceFailure, InvalidToken, NotFound, NotAuthorized,
886
    NotImplemented, InvalidRequest {
887

    
888
    boolean allowed = false;
889
    
890
    if (permission == null) {
891
    	throw new InvalidRequest("1761", "Permission was not provided or is invalid");
892
    }
893
    
894
    // permissions are hierarchical
895
    List<Permission> expandedPermissions = null;
896
    
897
    // always allow CN access
898
    if ( isAdminAuthorized(session) ) {
899
        allowed = true;
900
        return allowed;
901
        
902
    }
903
    
904
    // get the subject[s] from the session
905
	//defer to the shared util for recursively compiling the subjects	
906
	List<Subject> subjects = Arrays.asList(AuthUtils.authorizedClientSubjects(session));
907
    
908
    // get the system metadata
909
    String pidStr = pid.getValue();
910
    SystemMetadata systemMetadata = null;
911
    try {
912
        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
913

    
914
    } catch (Exception e) {
915
        // convert Hazelcast RuntimeException to NotFound
916
        logMetacat.error("An error occurred while getting system metadata for identifier " +
917
            pid.getValue() + ". The error message was: " + e.getMessage());
918
        throw new NotFound("1800", "No record found for " + pidStr);
919
        
920
    } 
921
    
922
    // throw not found if it was not found
923
    if (systemMetadata == null) {
924
    	throw new NotFound("1800", "No system metadata could be found for given PID: " + pidStr);
925
    }
926
	    
927
    // do we own it?
928
    for (Subject s: subjects) {
929
      logMetacat.debug("Comparing \t" + 
930
                       systemMetadata.getRightsHolder().getValue() +
931
                       " \tagainst \t" + s.getValue());
932
    	allowed = systemMetadata.getRightsHolder().equals(s);
933
    	if (allowed) {
934
    		return allowed;
935
    	}
936
    }    
937
    
938
    // otherwise check the access rules
939
    try {
940
	    List<AccessRule> allows = systemMetadata.getAccessPolicy().getAllowList();
941
	    search: // label break
942
	    for (AccessRule accessRule: allows) {
943
	      for (Subject s: subjects) {
944
	        logMetacat.debug("Checking allow access rule for subject: " + s.getValue());
945
	        if (accessRule.getSubjectList().contains(s)) {
946
	        	logMetacat.debug("Access rule contains subject: " + s.getValue());
947
	        	for (Permission p: accessRule.getPermissionList()) {
948
		        	logMetacat.debug("Checking permission: " + p.xmlValue());
949
	        		expandedPermissions = expandPermissions(p);
950
	        		allowed = expandedPermissions.contains(permission);
951
	        		if (allowed) {
952
			        	logMetacat.info("Permission granted: " + p.xmlValue() + " to " + s.getValue());
953
	        			break search; //label break
954
	        		}
955
	        	}
956
        		
957
	        }
958
	      }
959
	    }
960
    } catch (Exception e) {
961
    	// catch all for errors - safe side should be to deny the access
962
    	logMetacat.error("Problem checking authorization - defaulting to deny", e);
963
		allowed = false;
964
	  
965
    }
966
    
967
    // throw or return?
968
    if (!allowed) {
969
      throw new NotAuthorized("1820", permission + " not allowed on " + pidStr);
970
    }
971
    
972
    return allowed;
973
    
974
  }
975
  
976
  /*
977
   * parse a logEntry and get the relevant field from it
978
   * 
979
   * @param fieldname
980
   * @param entry
981
   * @return
982
   */
983
  private String getLogEntryField(String fieldname, String entry) {
984
    String begin = "<" + fieldname + ">";
985
    String end = "</" + fieldname + ">";
986
    // logMetacat.debug("looking for " + begin + " and " + end +
987
    // " in entry " + entry);
988
    String s = entry.substring(entry.indexOf(begin) + begin.length(), entry
989
        .indexOf(end));
990
    logMetacat.debug("entry " + fieldname + " : " + s);
991
    return s;
992
  }
993

    
994
  /** 
995
   * Determine if a given object should be treated as an XML science metadata
996
   * object. 
997
   * 
998
   * @param sysmeta - the SystemMetadata describing the object
999
   * @return true if the object should be treated as science metadata
1000
   */
1001
  public static boolean isScienceMetadata(SystemMetadata sysmeta) {
1002
    
1003
    ObjectFormat objectFormat = null;
1004
    boolean isScienceMetadata = false;
1005
    
1006
    try {
1007
      objectFormat = ObjectFormatCache.getInstance().getFormat(sysmeta.getFormatId());
1008
      if ( objectFormat.getFormatType().equals("METADATA") ) {
1009
      	isScienceMetadata = true;
1010
      	
1011
      }
1012
      
1013
       
1014
    } catch (ServiceFailure e) {
1015
      logMetacat.debug("There was a problem determining if the object identified by" + 
1016
          sysmeta.getIdentifier().getValue() + 
1017
          " is science metadata: " + e.getMessage());
1018
    
1019
    } catch (NotFound e) {
1020
      logMetacat.debug("There was a problem determining if the object identified by" + 
1021
          sysmeta.getIdentifier().getValue() + 
1022
          " is science metadata: " + e.getMessage());
1023
    
1024
    }
1025
    
1026
    return isScienceMetadata;
1027

    
1028
  }
1029
  
1030
  /**
1031
   * Insert or update an XML document into Metacat
1032
   * 
1033
   * @param xml - the XML document to insert or update
1034
   * @param pid - the identifier to be used for the resulting object
1035
   * 
1036
   * @return localId - the resulting docid of the document created or updated
1037
   * 
1038
   */
1039
  public String insertOrUpdateDocument(String xml, Identifier pid, 
1040
    Session session, String insertOrUpdate) 
1041
    throws ServiceFailure {
1042
    
1043
  	logMetacat.debug("Starting to insert xml document...");
1044
    IdentifierManager im = IdentifierManager.getInstance();
1045

    
1046
    // generate pid/localId pair for sysmeta
1047
    String localId = null;
1048
    
1049
    if(insertOrUpdate.equals("insert")) {
1050
      localId = im.generateLocalId(pid.getValue(), 1);
1051
      
1052
    } else {
1053
      //localid should already exist in the identifier table, so just find it
1054
      try {
1055
        logMetacat.debug("Updating pid " + pid.getValue());
1056
        logMetacat.debug("looking in identifier table for pid " + pid.getValue());
1057
        
1058
        localId = im.getLocalId(pid.getValue());
1059
        
1060
        logMetacat.debug("localId: " + localId);
1061
        //increment the revision
1062
        String docid = localId.substring(0, localId.lastIndexOf("."));
1063
        String revS = localId.substring(localId.lastIndexOf(".") + 1, localId.length());
1064
        int rev = new Integer(revS).intValue();
1065
        rev++;
1066
        docid = docid + "." + rev;
1067
        localId = docid;
1068
        logMetacat.debug("incremented localId: " + localId);
1069
      
1070
      } catch(McdbDocNotFoundException e) {
1071
        throw new ServiceFailure("1030", "D1NodeService.insertOrUpdateDocument(): " +
1072
            "pid " + pid.getValue() + 
1073
            " should have been in the identifier table, but it wasn't: " + 
1074
            e.getMessage());
1075
      
1076
      }
1077
      
1078
    }
1079

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

    
1180
    logMetacat.debug("Case DATA: starting to write to disk.");
1181
	if (locked) {
1182

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

    
1237
  /**
1238
   * Insert a systemMetadata document and return its localId
1239
   */
1240
  public void insertSystemMetadata(SystemMetadata sysmeta) 
1241
      throws ServiceFailure {
1242
      
1243
  	  logMetacat.debug("Starting to insert SystemMetadata...");
1244
      sysmeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1245
      logMetacat.debug("Inserting new system metadata with modified date " + 
1246
          sysmeta.getDateSysMetadataModified());
1247
      
1248
      //insert the system metadata
1249
      try {
1250
        // note: the calling subclass handles the map hazelcast lock/unlock
1251
      	HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
1252
      	
1253
      } catch (Exception e) {
1254
          throw new ServiceFailure("1190", e.getMessage());
1255
          
1256
	    }  
1257
  }
1258

    
1259
  /**
1260
   * Update a systemMetadata document
1261
   * 
1262
   * @param sysMeta - the system metadata object in the system to update
1263
   */
1264
    protected void updateSystemMetadata(SystemMetadata sysMeta)
1265
        throws ServiceFailure {
1266

    
1267
        logMetacat.debug("D1NodeService.updateSystemMetadata() called.");
1268
        sysMeta.setDateSysMetadataModified(new Date());
1269
        try {
1270
            HazelcastService.getInstance().getSystemMetadataMap().lock(sysMeta.getIdentifier());
1271
            HazelcastService.getInstance().getSystemMetadataMap().put(sysMeta.getIdentifier(), sysMeta);
1272

    
1273
        } catch (Exception e) {
1274
            throw new ServiceFailure("4862", e.getMessage());
1275

    
1276
        } finally {
1277
            HazelcastService.getInstance().getSystemMetadataMap().unlock(sysMeta.getIdentifier());
1278

    
1279
        }
1280

    
1281
    }
1282
  
1283
  /**
1284
   * Given a Permission, returns a list of all permissions that it encompasses
1285
   * Permissions are hierarchical so that WRITE also allows READ.
1286
   * @param permission
1287
   * @return list of included Permissions for the given permission
1288
   */
1289
  protected List<Permission> expandPermissions(Permission permission) {
1290
	  	List<Permission> expandedPermissions = new ArrayList<Permission>();
1291
	    if (permission.equals(Permission.READ)) {
1292
	    	expandedPermissions.add(Permission.READ);
1293
	    }
1294
	    if (permission.equals(Permission.WRITE)) {
1295
	    	expandedPermissions.add(Permission.READ);
1296
	    	expandedPermissions.add(Permission.WRITE);
1297
	    }
1298
	    if (permission.equals(Permission.CHANGE_PERMISSION)) {
1299
	    	expandedPermissions.add(Permission.READ);
1300
	    	expandedPermissions.add(Permission.WRITE);
1301
	    	expandedPermissions.add(Permission.CHANGE_PERMISSION);
1302
	    }
1303
	    return expandedPermissions;
1304
  }
1305

    
1306
  /*
1307
   * Write a stream to a file
1308
   * 
1309
   * @param dir - the directory to write to
1310
   * @param fileName - the file name to write to
1311
   * @param data - the object bytes as an input stream
1312
   * 
1313
   * @return newFile - the new file created
1314
   * 
1315
   * @throws ServiceFailure
1316
   */
1317
  private File writeStreamToFile(File dir, String fileName, InputStream data) 
1318
    throws ServiceFailure {
1319
    
1320
    File newFile = new File(dir, fileName);
1321
    logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
1322

    
1323
    try {
1324
        if (newFile.createNewFile()) {
1325
          // write data stream to desired file
1326
          OutputStream os = new FileOutputStream(newFile);
1327
          long length = IOUtils.copyLarge(data, os);
1328
          os.flush();
1329
          os.close();
1330
        } else {
1331
          logMetacat.debug("File creation failed, or file already exists.");
1332
          throw new ServiceFailure("1190", "File already exists: " + fileName);
1333
        }
1334
    } catch (FileNotFoundException e) {
1335
      logMetacat.debug("FNF: " + e.getMessage());
1336
      throw new ServiceFailure("1190", "File not found: " + fileName + " " 
1337
                + e.getMessage());
1338
    } catch (IOException e) {
1339
      logMetacat.debug("IOE: " + e.getMessage());
1340
      throw new ServiceFailure("1190", "File was not written: " + fileName 
1341
                + " " + e.getMessage());
1342
    }
1343

    
1344
    return newFile;
1345
  }
1346

    
1347
  /*
1348
   * Returns a list of nodes that have been registered with the DataONE infrastructure
1349
   * that match the given session subject
1350
   * @return nodes - List of nodes from the registry with a matching session subject
1351
   * 
1352
   * @throws ServiceFailure
1353
   * @throws NotImplemented
1354
   */
1355
  protected List<Node> listNodesBySubject(Subject subject) 
1356
      throws ServiceFailure, NotImplemented {
1357
      List<Node> nodeList = new ArrayList<Node>();
1358
      
1359
      CNode cn = D1Client.getCN();
1360
      List<Node> nodes = cn.listNodes().getNodeList();
1361
      
1362
      // find the node in the node list
1363
      for ( Node node : nodes ) {
1364
          
1365
          List<Subject> nodeSubjects = node.getSubjectList();
1366
          if (nodeSubjects != null) {    
1367
	          // check if the session subject is in the node subject list
1368
	          for (Subject nodeSubject : nodeSubjects) {
1369
	              if ( nodeSubject.equals(subject) ) { // subject of session == node subject
1370
	                  nodeList.add(node);  
1371
	              }                              
1372
	          }
1373
          }
1374
      }
1375
      
1376
      return nodeList;
1377
      
1378
  }
1379

    
1380

    
1381
}
(2-2/5)