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-11-29 16:52:29 -0800 (Thu, 29 Nov 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.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
    	
430
    } catch (Exception e) {
431
    	logMetacat.error("Problem creating system metadata: " + pid.getValue(), e);
432
        throw new ServiceFailure("1190", e.getMessage());
433
	}
434
    
435
    // setting the resulting identifier failed
436
    if (localId == null ) {
437
      throw new ServiceFailure("1190", "The Node is unable to create the object. ");
438
    }
439

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

    
444
    return resultPid;
445
  }
446

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1250
        logMetacat.debug("D1NodeService.updateSystemMetadata() called.");
1251
        sysMeta.setDateSysMetadataModified(new Date());
1252
        try {
1253
            HazelcastService.getInstance().getSystemMetadataMap().lock(sysMeta.getIdentifier());
1254
            HazelcastService.getInstance().getSystemMetadataMap().put(sysMeta.getIdentifier(), sysMeta);
1255

    
1256
        } catch (Exception e) {
1257
            throw new ServiceFailure("4862", e.getMessage());
1258

    
1259
        } finally {
1260
            HazelcastService.getInstance().getSystemMetadataMap().unlock(sysMeta.getIdentifier());
1261

    
1262
        }
1263

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

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

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

    
1327
    return newFile;
1328
  }
1329

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

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

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

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

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

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

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

    
1427
              // archive it
1428
              SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1429
              sysMeta.setArchived(true);
1430
              sysMeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1431
              HazelcastService.getInstance().getSystemMetadataMap().put(pid, sysMeta);
1432
              
1433
          } catch (McdbDocNotFoundException e) {
1434
              throw new NotFound("1340", "The provided identifier was invalid.");
1435

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

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

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

    
1446
      } else {
1447
          throw new NotAuthorized("1320", "The provided identity does not have " + "permission to archive the object on the Node.");
1448
      }
1449

    
1450
      return pid;
1451
  }
1452
  
1453
  public Identifier archive(Identifier pid) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
1454
	  return archive(null, pid);
1455
  }
1456

    
1457

    
1458
}
(2-2/6)