Project

General

Profile

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

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

    
26
import java.io.ByteArrayOutputStream;
27
import java.io.File;
28
import java.io.FileNotFoundException;
29
import java.io.FileOutputStream;
30
import java.io.IOException;
31
import java.io.InputStream;
32
import java.io.OutputStream;
33
import java.io.OutputStreamWriter;
34
import java.io.Writer;
35
import java.math.BigInteger;
36
import java.sql.PreparedStatement;
37
import java.sql.ResultSet;
38
import java.sql.SQLException;
39
import java.util.ArrayList;
40
import java.util.Calendar;
41
import java.util.Date;
42
import java.util.Hashtable;
43
import java.util.List;
44
import java.util.Set;
45
import java.util.Timer;
46
import java.util.Vector;
47
import java.util.concurrent.locks.Lock;
48

    
49
import javax.servlet.http.HttpServletRequest;
50

    
51
import org.apache.commons.io.IOUtils;
52
import org.apache.log4j.Logger;
53
import org.dataone.client.v2.CNode;
54
import org.dataone.client.v2.itk.D1Client;
55
import org.dataone.client.v2.formats.ObjectFormatCache;
56
import org.dataone.configuration.Settings;
57
import org.dataone.service.exceptions.BaseException;
58
import org.dataone.service.exceptions.IdentifierNotUnique;
59
import org.dataone.service.exceptions.InsufficientResources;
60
import org.dataone.service.exceptions.InvalidRequest;
61
import org.dataone.service.exceptions.InvalidSystemMetadata;
62
import org.dataone.service.exceptions.InvalidToken;
63
import org.dataone.service.exceptions.NotAuthorized;
64
import org.dataone.service.exceptions.NotFound;
65
import org.dataone.service.exceptions.NotImplemented;
66
import org.dataone.service.exceptions.ServiceFailure;
67
import org.dataone.service.exceptions.UnsupportedType;
68
import org.dataone.service.types.v1.AccessRule;
69
import org.dataone.service.types.v1.DescribeResponse;
70
import org.dataone.service.types.v1.Group;
71
import org.dataone.service.types.v1.Identifier;
72
import org.dataone.service.types.v1.ObjectFormatIdentifier;
73
import org.dataone.service.types.v1.ObjectList;
74
import org.dataone.service.types.v2.Log;
75
import org.dataone.service.types.v2.Node;
76
import org.dataone.service.types.v2.OptionList;
77
import org.dataone.service.types.v1.Event;
78
import org.dataone.service.types.v1.NodeReference;
79
import org.dataone.service.types.v1.NodeType;
80
import org.dataone.service.types.v2.ObjectFormat;
81
import org.dataone.service.types.v1.Permission;
82
import org.dataone.service.types.v1.Replica;
83
import org.dataone.service.types.v1.Session;
84
import org.dataone.service.types.v1.Subject;
85
import org.dataone.service.types.v2.SystemMetadata;
86
import org.dataone.service.types.v1.util.AuthUtils;
87
import org.dataone.service.types.v1.util.ChecksumUtil;
88
import org.dataone.service.util.Constants;
89

    
90
import edu.ucsb.nceas.metacat.AccessionNumber;
91
import edu.ucsb.nceas.metacat.AccessionNumberException;
92
import edu.ucsb.nceas.metacat.DBTransform;
93
import edu.ucsb.nceas.metacat.DocumentImpl;
94
import edu.ucsb.nceas.metacat.EventLog;
95
import edu.ucsb.nceas.metacat.IdentifierManager;
96
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
97
import edu.ucsb.nceas.metacat.MetacatHandler;
98
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
99
import edu.ucsb.nceas.metacat.common.query.stream.ContentTypeByteArrayInputStream;
100
import edu.ucsb.nceas.metacat.database.DBConnection;
101
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
102
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService;
103
import edu.ucsb.nceas.metacat.index.MetacatSolrIndex;
104
import edu.ucsb.nceas.metacat.properties.PropertyService;
105
import edu.ucsb.nceas.metacat.replication.ForceReplicationHandler;
106
import edu.ucsb.nceas.metacat.util.SkinUtil;
107
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
108

    
109
public abstract class D1NodeService {
110
    
111
  public static final String DELETEDMESSAGE = "The object with the PID has been deleted from the node.";
112
  
113
  private static Logger logMetacat = Logger.getLogger(D1NodeService.class);
114

    
115
  /** For logging the operations */
116
  protected HttpServletRequest request;
117
  
118
  /* reference to the metacat handler */
119
  protected MetacatHandler handler;
120
  
121
  /* parameters set in the incoming request */
122
  private Hashtable<String, String[]> params;
123
  
124
  /**
125
   * limit paged results sets to a configured maximum
126
   */
127
  protected static int MAXIMUM_DB_RECORD_COUNT = 7000;
128
  
129
  static {
130
		try {
131
			MAXIMUM_DB_RECORD_COUNT = Integer.valueOf(PropertyService.getProperty("database.webResultsetSize"));
132
		} catch (Exception e) {
133
			logMetacat.warn("Could not set MAXIMUM_DB_RECORD_COUNT", e);
134
		}
135
	}
136
  
137
  /**
138
   * out-of-band session object to be used when not passed in as a method parameter
139
   */
140
  protected Session session;
141

    
142
  /**
143
   * Constructor - used to set the metacatUrl from a subclass extending D1NodeService
144
   * 
145
   * @param metacatUrl - the URL of the metacat service, including the ending /d1
146
   */
147
  public D1NodeService(HttpServletRequest request) {
148
		this.request = request;
149
	}
150

    
151
  /**
152
   * retrieve the out-of-band session
153
   * @return
154
   */
155
  	public Session getSession() {
156
		return session;
157
	}
158
  	
159
  	/**
160
  	 * Set the out-of-band session
161
  	 * @param session
162
  	 */
163
	public void setSession(Session session) {
164
		this.session = session;
165
	}
166

    
167
  /**
168
   * This method provides a lighter weight mechanism than 
169
   * getSystemMetadata() for a client to determine basic 
170
   * properties of the referenced object.
171
   * 
172
   * @param session - the Session object containing the credentials for the Subject
173
   * @param pid - the identifier of the object to be described
174
   * 
175
   * @return describeResponse - A set of values providing a basic description 
176
   *                            of the object.
177
   * 
178
   * @throws InvalidToken
179
   * @throws ServiceFailure
180
   * @throws NotAuthorized
181
   * @throws NotFound
182
   * @throws NotImplemented
183
   * @throws InvalidRequest
184
   */
185
  public DescribeResponse describe(Session session, Identifier pid) 
186
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
187
      
188
      String serviceFailureCode = "4931";
189
      Identifier sid = getPIDForSID(pid, serviceFailureCode);
190
      if(sid != null) {
191
          pid = sid;
192
      }
193

    
194
    // get system metadata and construct the describe response
195
      SystemMetadata sysmeta = getSystemMetadata(session, pid);
196
      DescribeResponse describeResponse = 
197
      	new DescribeResponse(sysmeta.getFormatId(), sysmeta.getSize(), 
198
      			sysmeta.getDateSysMetadataModified(),
199
      			sysmeta.getChecksum(), sysmeta.getSerialVersion());
200

    
201
      return describeResponse;
202

    
203
  }
204
  
205
  /**
206
   * Deletes an object from the Member Node, where the object is either a 
207
   * data object or a science metadata object.
208
   * 
209
   * @param session - the Session object containing the credentials for the Subject
210
   * @param pid - The object identifier to be deleted
211
   * 
212
   * @return pid - the identifier of the object used for the deletion
213
   * 
214
   * @throws InvalidToken
215
   * @throws ServiceFailure
216
   * @throws NotAuthorized
217
   * @throws NotFound
218
   * @throws NotImplemented
219
   * @throws InvalidRequest
220
   */
221
  public Identifier delete(Session session, Identifier pid) 
222
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
223
      
224
      String localId = null;
225
      if (session == null) {
226
      	throw new InvalidToken("1330", "No session has been provided");
227
      }
228
      // just for logging purposes
229
      String username = session.getSubject().getValue();
230

    
231
      // do we have a valid pid?
232
      if (pid == null || pid.getValue().trim().equals("")) {
233
          throw new ServiceFailure("1350", "The provided identifier was invalid.");
234
      }
235

    
236
      // check for the existing identifier
237
      try {
238
          localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
239
      } catch (McdbDocNotFoundException e) {
240
          throw new NotFound("1340", "The object with the provided " + "identifier was not found.");
241
      } catch (SQLException e) {
242
          throw new ServiceFailure("1350", "The object with the provided " + "identifier "+pid.getValue()+" couldn't be identified since "+e.getMessage());
243
      }
244
      
245
      try {
246
          // delete the document, as admin
247
          DocumentImpl.delete(localId, null, null, null, true);
248
          EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, Event.DELETE.xmlValue());
249

    
250
          // archive it
251
          // DocumentImpl.delete() now sets this
252
          // see https://redmine.dataone.org/issues/3406
253
//          SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
254
//          sysMeta.setArchived(true);
255
//          sysMeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
256
//          HazelcastService.getInstance().getSystemMetadataMap().put(pid, sysMeta);
257
          
258
      } catch (McdbDocNotFoundException e) {
259
          throw new NotFound("1340", "The provided identifier was invalid.");
260

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

    
264
      } catch (InsufficientKarmaException e) {
265
          if ( logMetacat.isDebugEnabled() ) {
266
              e.printStackTrace();
267
          }
268
          throw new NotAuthorized("1320", "The provided identity does not have " + "permission to DELETE objects on the Member Node.");
269
      
270
      } catch (Exception e) { // for some reason DocumentImpl throws a general Exception
271
          throw new ServiceFailure("1350", "There was a problem deleting the object." + "The error message was: " + e.getMessage());
272
      }
273

    
274
      return pid;
275
  }
276
  
277
  /**
278
   * Low level, "are you alive" operation. A valid ping response is 
279
   * indicated by a HTTP status of 200.
280
   * 
281
   * @return true if the service is alive
282
   * 
283
   * @throws NotImplemented
284
   * @throws ServiceFailure
285
   * @throws InsufficientResources
286
   */
287
  public Date ping() 
288
      throws NotImplemented, ServiceFailure, InsufficientResources {
289

    
290
      // test if we can get a database connection
291
      int serialNumber = -1;
292
      DBConnection dbConn = null;
293
      try {
294
          dbConn = DBConnectionPool.getDBConnection("MNodeService.ping");
295
          serialNumber = dbConn.getCheckOutSerialNumber();
296
      } catch (SQLException e) {
297
      	ServiceFailure sf = new ServiceFailure("", e.getMessage());
298
      	sf.initCause(e);
299
          throw sf;
300
      } finally {
301
          // Return the database connection
302
          DBConnectionPool.returnDBConnection(dbConn, serialNumber);
303
      }
304

    
305
      return Calendar.getInstance().getTime();
306
  }
307
  
308
  /**
309
   * Adds a new object to the Node, where the object is either a data 
310
   * object or a science metadata object. This method is called by clients 
311
   * to create new data objects on Member Nodes or internally for Coordinating
312
   * Nodes
313
   * 
314
   * @param session - the Session object containing the credentials for the Subject
315
   * @param pid - The object identifier to be created
316
   * @param object - the object bytes
317
   * @param sysmeta - the system metadata that describes the object  
318
   * 
319
   * @return pid - the object identifier created
320
   * 
321
   * @throws InvalidToken
322
   * @throws ServiceFailure
323
   * @throws NotAuthorized
324
   * @throws IdentifierNotUnique
325
   * @throws UnsupportedType
326
   * @throws InsufficientResources
327
   * @throws InvalidSystemMetadata
328
   * @throws NotImplemented
329
   * @throws InvalidRequest
330
   */
331
  public Identifier create(Session session, Identifier pid, InputStream object,
332
    SystemMetadata sysmeta) 
333
    throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, 
334
    UnsupportedType, InsufficientResources, InvalidSystemMetadata, 
335
    NotImplemented, InvalidRequest {
336

    
337
    Identifier resultPid = null;
338
    String localId = null;
339
    boolean allowed = false;
340
    
341
    // check for null session
342
    if (session == null) {
343
    	throw new InvalidToken("4894", "Session is required to WRITE to the Node.");
344
    }
345
    Subject subject = session.getSubject();
346

    
347
    Subject publicSubject = new Subject();
348
    publicSubject.setValue(Constants.SUBJECT_PUBLIC);
349
	// be sure the user is authenticated for create()
350
    if (subject == null || subject.getValue() == null || 
351
        subject.equals(publicSubject) ) {
352
      throw new NotAuthorized("1100", "The provided identity does not have " +
353
        "permission to WRITE to the Node.");
354
      
355
    }
356
        
357
    // verify that pid == SystemMetadata.getIdentifier()
358
    logMetacat.debug("Comparing pid|sysmeta_pid: " + 
359
      pid.getValue() + "|" + sysmeta.getIdentifier().getValue());
360
    if (!pid.getValue().equals(sysmeta.getIdentifier().getValue())) {
361
        throw new InvalidSystemMetadata("1180", 
362
            "The supplied system metadata is invalid. " +
363
            "The identifier " + pid.getValue() + " does not match identifier" +
364
            "in the system metadata identified by " +
365
            sysmeta.getIdentifier().getValue() + ".");
366
        
367
    }
368
    
369

    
370
    logMetacat.debug("Checking if identifier exists: " + pid.getValue());
371
    // Check that the identifier does not already exist
372
    boolean idExists = false;
373
    try {
374
        idExists = IdentifierManager.getInstance().identifierExists(pid.getValue());
375
    } catch (SQLException e) {
376
        throw new ServiceFailure("1190", 
377
                                "The requested identifier " + pid.getValue() +
378
                                " couldn't be determined if it is unique since : "+e.getMessage());
379
    }
380
    if (idExists) {
381
	    	throw new IdentifierNotUnique("1120", 
382
			          "The requested identifier " + pid.getValue() +
383
			          " is already used by another object and" +
384
			          "therefore can not be used for this object. Clients should choose" +
385
			          "a new identifier that is unique and retry the operation or " +
386
			          "use CN.reserveIdentifier() to reserve one.");
387
    	
388
    }
389
    
390
    
391
    // TODO: this probably needs to be refined more
392
    try {
393
      allowed = isAuthorized(session, pid, Permission.WRITE);
394
            
395
    } catch (NotFound e) {
396
      // The identifier doesn't exist, writing should be fine.
397
      allowed = true;
398
    }
399
    
400
    if(!allowed) {
401
        throw new NotAuthorized("1100", "Provited Identity doesn't have the WRITE permission on the pid "+pid.getValue());
402
    }
403
    // verify checksum, only if we can reset the inputstream
404
    if (object.markSupported()) {
405
        logMetacat.debug("Checking checksum for: " + pid.getValue());
406
	    String checksumAlgorithm = sysmeta.getChecksum().getAlgorithm();
407
	    String checksumValue = sysmeta.getChecksum().getValue();
408
	    try {
409
			String computedChecksumValue = ChecksumUtil.checksum(object, checksumAlgorithm).getValue();
410
			// it's very important that we don't consume the stream
411
			object.reset();
412
			if (!computedChecksumValue.equals(checksumValue)) {
413
			    logMetacat.error("Checksum for " + pid.getValue() + " does not match system metadata, computed = " + computedChecksumValue );
414
				throw new InvalidSystemMetadata("4896", "Checksum given does not match that of the object");
415
			}
416
		} catch (Exception e) {
417
			String msg = "Error verifying checksum values";
418
	      	logMetacat.error(msg, e);
419
	        throw new ServiceFailure("1190", msg + ": " + e.getMessage());
420
		}
421
    } else {
422
    	logMetacat.warn("mark is not supported on the object's input stream - cannot verify checksum without consuming stream");
423
    }
424
    	
425
    // we have the go ahead
426
    //if ( allowed ) {
427
         
428
        // save the sysmeta
429
        try {
430
            // lock and unlock of the pid happens in the subclass
431
            HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
432
           
433
        
434
        } catch (Exception e) {
435
            logMetacat.error("Problem creating system metadata: " + pid.getValue(), e);
436
            throw new ServiceFailure("1190", e.getMessage());
437
        }
438
      
439
        logMetacat.debug("Allowed to insert: " + pid.getValue());
440

    
441
      // Science metadata (XML) or science data object?
442
      // TODO: there are cases where certain object formats are science metadata
443
      // but are not XML (netCDF ...).  Handle this.
444
      if ( isScienceMetadata(sysmeta) ) {
445
        
446
        // CASE METADATA:
447
      	//String objectAsXML = "";
448
        try {
449
	        //objectAsXML = IOUtils.toString(object, "UTF-8");
450
	        localId = insertOrUpdateDocument(object,"UTF-8", pid, session, "insert");
451
	        //localId = im.getLocalId(pid.getValue());
452

    
453
        } catch (IOException e) {
454
            removeSystemMeta(pid);
455
        	String msg = "The Node is unable to create the object. " +
456
          "There was a problem converting the object to XML";
457
        	logMetacat.info(msg);
458
          throw new ServiceFailure("1190", msg + ": " + e.getMessage());
459

    
460
        } catch (ServiceFailure e) {
461
            removeSystemMeta(pid);
462
            throw e;
463
        } catch (Exception e) {
464
            removeSystemMeta(pid);
465
            throw new ServiceFailure("1190", "The node is unable to create the object: " + e.getMessage());
466
        }
467
                    
468
      } else {
469
	        
470
	      // DEFAULT CASE: DATA (needs to be checked and completed)
471
          try {
472
              localId = insertDataObject(object, pid, session);
473
          } catch (ServiceFailure e) {
474
              removeSystemMeta(pid);
475
              throw e;
476
          } catch (Exception e) {
477
              removeSystemMeta(pid);
478
              throw new ServiceFailure("1190", "The node is unable to create the object: " + e.getMessage());
479
          }
480
	      
481
      }   
482
    
483
    //}
484

    
485
    logMetacat.debug("Done inserting new object: " + pid.getValue());
486
    
487
    // setting the resulting identifier failed
488
    if (localId == null ) {
489
        removeSystemMeta(pid);
490
      throw new ServiceFailure("1190", "The Node is unable to create the object. ");
491
    }
492
    
493
    try {
494
        // submit for indexing
495
        MetacatSolrIndex.getInstance().submit(sysmeta.getIdentifier(), sysmeta, null, true);
496
    } catch (Exception e) {
497
        logMetacat.warn("Couldn't create solr index for object "+pid.getValue());
498
    }
499

    
500
    resultPid = pid;
501
    
502
    logMetacat.debug("create() complete for object: " + pid.getValue());
503

    
504
    return resultPid;
505
  }
506
  
507
  /*
508
   * Roll-back method when inserting data object fails.
509
   */
510
  protected void removeSystemMeta(Identifier id){
511
      HazelcastService.getInstance().getSystemMetadataMap().remove(id);
512
  }
513
  
514
  /*
515
   * Roll-back method when inserting data object fails.
516
   */
517
  protected void removeSolrIndex(SystemMetadata sysMeta) {
518
      sysMeta.setSerialVersion(sysMeta.getSerialVersion().add(BigInteger.ONE));
519
      sysMeta.setArchived(true);
520
      sysMeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
521
      try {
522
          MetacatSolrIndex.getInstance().submit(sysMeta.getIdentifier(), sysMeta, null, false);
523
      } catch (Exception e) {
524
          logMetacat.warn("Can't remove the solr index for pid "+sysMeta.getIdentifier().getValue());
525
      }
526
      
527
  }
528

    
529
  /**
530
   * Return the log records associated with a given event between the start and 
531
   * end dates listed given a particular Subject listed in the Session
532
   * 
533
   * @param session - the Session object containing the credentials for the Subject
534
   * @param fromDate - the start date of the desired log records
535
   * @param toDate - the end date of the desired log records
536
   * @param event - restrict log records of a specific event type
537
   * @param start - zero based offset from the first record in the 
538
   *                set of matching log records. Used to assist with 
539
   *                paging the response.
540
   * @param count - maximum number of log records to return in the response. 
541
   *                Used to assist with paging the response.
542
   * 
543
   * @return the desired log records
544
   * 
545
   * @throws InvalidToken
546
   * @throws ServiceFailure
547
   * @throws NotAuthorized
548
   * @throws InvalidRequest
549
   * @throws NotImplemented
550
   */
551
  public Log getLogRecords(Session session, Date fromDate, Date toDate, 
552
      String event, String pidFilter, Integer start, Integer count) throws InvalidToken, ServiceFailure,
553
      NotAuthorized, InvalidRequest, NotImplemented {
554

    
555
	  // only admin access to this method
556
	  // see https://redmine.dataone.org/issues/2855
557
	  if (!isAdminAuthorized(session)) {
558
		  throw new NotAuthorized("1460", "Only the CN or admin is allowed to harvest logs from this node");
559
	  }
560
    Log log = new Log();
561
    IdentifierManager im = IdentifierManager.getInstance();
562
    EventLog el = EventLog.getInstance();
563
    if ( fromDate == null ) {
564
      logMetacat.debug("setting fromdate from null");
565
      fromDate = new Date(1);
566
    }
567
    if ( toDate == null ) {
568
      logMetacat.debug("setting todate from null");
569
      toDate = new Date();
570
    }
571

    
572
    if ( start == null ) {
573
    	start = 0;	
574
    }
575
    
576
    if ( count == null ) {
577
    	count = 1000;
578
    }
579
    
580
    // safeguard against large requests
581
    if (count > MAXIMUM_DB_RECORD_COUNT) {
582
    	count = MAXIMUM_DB_RECORD_COUNT;
583
    }
584

    
585
    String[] filterDocid = null;
586
    if (pidFilter != null && !pidFilter.trim().equals("")) {
587
        //check if the given identifier is a sid. If it is sid, choose the current pid of the sid.
588
        Identifier pid = new Identifier();
589
        pid.setValue(pidFilter);
590
        String serviceFailureCode = "1490";
591
        Identifier sid = getPIDForSID(pid, serviceFailureCode);
592
        if(sid != null) {
593
            pid = sid;
594
        }
595
        pidFilter = pid.getValue();
596
		try {
597
	      String localId = im.getLocalId(pidFilter);
598
	      filterDocid = new String[] {localId};
599
	    } catch (Exception ex) { 
600
	    	String msg = "Could not find localId for given pidFilter '" + pidFilter + "'";
601
	        logMetacat.warn(msg, ex);
602
	        //throw new InvalidRequest("1480", msg);
603
	        return log; //return 0 record
604
	    }
605
    }
606
    
607
    logMetacat.debug("fromDate: " + fromDate);
608
    logMetacat.debug("toDate: " + toDate);
609

    
610
    log = el.getD1Report(null, null, filterDocid, event,
611
        new java.sql.Timestamp(fromDate.getTime()),
612
        new java.sql.Timestamp(toDate.getTime()), false, start, count);
613
    
614
    logMetacat.info("getLogRecords");
615
    return log;
616
  }
617
    
618
  /**
619
   * Return the object identified by the given object identifier
620
   * 
621
   * @param session - the Session object containing the credentials for the Subject
622
   * @param pid - the object identifier for the given object
623
   * 
624
   * TODO: The D1 Authorization API doesn't provide information on which 
625
   * authentication system the Subject belongs to, and so it's not possible to
626
   * discern which Person or Group is a valid KNB LDAP DN.  Fix this.
627
   * 
628
   * @return inputStream - the input stream of the given object
629
   * 
630
   * @throws InvalidToken
631
   * @throws ServiceFailure
632
   * @throws NotAuthorized
633
   * @throws InvalidRequest
634
   * @throws NotImplemented
635
   */
636
  public InputStream get(Session session, Identifier pid) 
637
    throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
638
    NotImplemented {
639
    
640
    String serviceFailureCode = "1030";
641
    Identifier sid = getPIDForSID(pid, serviceFailureCode);
642
    if(sid != null) {
643
        pid = sid;
644
    }
645
    
646
    InputStream inputStream = null; // bytes to be returned
647
    handler = new MetacatHandler(new Timer());
648
    boolean allowed = false;
649
    String localId; // the metacat docid for the pid
650
    
651
    // get the local docid from Metacat
652
    try {
653
      localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
654
    
655
    } catch (McdbDocNotFoundException e) {
656
      throw new NotFound("1020", "The object specified by " + 
657
                         pid.getValue() +
658
                         " does not exist at this node.");
659
    } catch (SQLException e) {
660
        throw new ServiceFailure("1030", "The object specified by "+ pid.getValue()+
661
                                  " couldn't be identified at this node since "+e.getMessage());
662
    }
663
    
664
    // check for authorization
665
    try {
666
		allowed = isAuthorized(session, pid, Permission.READ);
667
	} catch (InvalidRequest e) {
668
		throw new ServiceFailure("1030", e.getDescription());
669
	}
670
    
671
    // if the person is authorized, perform the read
672
    if (allowed) {
673
      try {
674
        inputStream = handler.read(localId);
675
      } catch (McdbDocNotFoundException de) {
676
          String error ="";
677
          if(EventLog.getInstance().isDeleted(localId)) {
678
                error=DELETEDMESSAGE;
679
          }
680
          throw new NotFound("1020", "The object specified by " + 
681
                           pid.getValue() +
682
                           " does not exist at this node. "+error);
683
      } catch (Exception e) {
684
        throw new ServiceFailure("1030", "The object specified by " + 
685
            pid.getValue() +
686
            " could not be returned due to error: " +
687
            e.getMessage()+". ");
688
      }
689
    }
690

    
691
    // if we fail to set the input stream
692
    if ( inputStream == null ) {
693
        String error ="";
694
        if(EventLog.getInstance().isDeleted(localId)) {
695
              error=DELETEDMESSAGE;
696
        }
697
        throw new NotFound("1020", "The object specified by " + 
698
                         pid.getValue() +
699
                         " does not exist at this node. "+error);
700
    }
701
    
702
	// log the read event
703
    String principal = Constants.SUBJECT_PUBLIC;
704
    if (session != null && session.getSubject() != null) {
705
    	principal = session.getSubject().getValue();
706
    }
707
    EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), principal, localId, "read");
708
    
709
    return inputStream;
710
  }
711

    
712
  /**
713
   * Return the system metadata for a given object
714
   * 
715
   * @param session - the Session object containing the credentials for the Subject
716
   * @param pid - the object identifier for the given object
717
   * 
718
   * @return inputStream - the input stream of the given system metadata object
719
   * 
720
   * @throws InvalidToken
721
   * @throws ServiceFailure
722
   * @throws NotAuthorized
723
   * @throws NotFound
724
   * @throws InvalidRequest
725
   * @throws NotImplemented
726
   */
727
    public SystemMetadata getSystemMetadata(Session session, Identifier pid)
728
        throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
729
        NotImplemented {
730

    
731
        String serviceFailureCode = "1090";
732
        Identifier sid = getPIDForSID(pid, serviceFailureCode);
733
        if(sid != null) {
734
            pid = sid;
735
        }
736
        boolean isAuthorized = false;
737
        SystemMetadata systemMetadata = null;
738
        List<Replica> replicaList = null;
739
        NodeReference replicaNodeRef = null;
740
        List<Node> nodeListBySubject = null;
741
        Subject subject = null;
742
        
743
        if (session != null ) {
744
            subject = session.getSubject();
745
        }
746
        
747
        // check normal authorization
748
        BaseException originalAuthorizationException = null;
749
        if (!isAuthorized) {
750
            try {
751
                isAuthorized = isAuthorized(session, pid, Permission.READ);
752

    
753
            } catch (InvalidRequest e) {
754
                throw new ServiceFailure("1090", e.getDescription());
755
            } catch (NotAuthorized nae) {
756
            	// catch this for later
757
            	originalAuthorizationException = nae;
758
			}
759
        }
760
        
761
        // get the system metadata first because we need the replica list for auth
762
        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
763
        
764
        // check the replica information to expand access to MNs that might need it
765
        if (!isAuthorized) {
766
        	
767
	        try {
768
	        	
769
	            // if MNs are listed as replicas, allow access
770
	            if ( systemMetadata != null ) {
771
	                replicaList = systemMetadata.getReplicaList();
772
	                // only check if there are in fact replicas listed
773
	                if ( replicaList != null ) {
774
	                    
775
	                    if ( subject != null ) {
776
	                        // get the list of nodes with a matching node subject
777
	                        try {
778
	                            nodeListBySubject = listNodesBySubject(session.getSubject());
779
	
780
	                        } catch (BaseException e) {
781
	                            // Unexpected error contacting the CN via D1Client
782
	                            String msg = "Caught an unexpected error while trying "
783
	                                    + "to potentially authorize system metadata access "
784
	                                    + "based on the session subject. The error was "
785
	                                    + e.getMessage();
786
	                            logMetacat.error(msg);
787
	                            if (logMetacat.isDebugEnabled()) {
788
	                                e.printStackTrace();
789
	
790
	                            }
791
	                            // isAuthorized is still false 
792
	                        }
793
	
794
	                    }
795
	                    if (nodeListBySubject != null) {
796
	                        // compare node ids to replica node ids
797
	                        outer: for (Replica replica : replicaList) {
798
	                            replicaNodeRef = replica.getReplicaMemberNode();
799
	
800
	                            for (Node node : nodeListBySubject) {
801
	                                if (node.getIdentifier().equals(replicaNodeRef)) {
802
	                                    // node id via session subject matches a replica node
803
	                                    isAuthorized = true;
804
	                                    break outer;
805
	                                }
806
	                            }
807
	                        }
808
	                    }
809
	                }
810
	            }
811
	            
812
	            // if we still aren't authorized, then we are done
813
	            if (!isAuthorized) {
814
	                throw new NotAuthorized("1400", Permission.READ
815
	                        + " not allowed on " + pid.getValue());
816
	            }
817

    
818
	        } catch (RuntimeException e) {
819
	        	e.printStackTrace();
820
	            // convert hazelcast RuntimeException to ServiceFailure
821
	            throw new ServiceFailure("1090", "Unexpected error getting system metadata for: " + 
822
	                pid.getValue());	
823
	        }
824
	        
825
        }
826
        
827
        // It wasn't in the map
828
        if ( systemMetadata == null ) {
829
            String error ="";
830
            String localId = null;
831
            try {
832
                localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
833
              
834
             } catch (Exception e) {
835
                logMetacat.warn("Couldn't find the local id for the pid "+pid.getValue());
836
            }
837
            
838
            if(localId != null && EventLog.getInstance().isDeleted(localId)) {
839
                error = DELETEDMESSAGE;
840
            } else if (localId == null && EventLog.getInstance().isDeleted(pid.getValue())) {
841
                error = DELETEDMESSAGE;
842
            }
843
            throw new NotFound("1420", "No record found for: " + pid.getValue()+". "+error);
844
        }
845
        
846
        return systemMetadata;
847
    }
848
     
849
    
850
    /**
851
     * Test if the specified session represents the authoritative member node for the
852
     * given object specified by the identifier. According the the DataONE documentation, 
853
     * the authoritative member node has all the rights of the *rightsHolder*.
854
     * @param session - the Session object containing the credentials for the Subject
855
     * @param pid - the Identifier of the data object
856
     * @return true if the session represents the authoritative mn.
857
     * @throws ServiceFailure 
858
     * @throws NotImplemented 
859
     */
860
    public boolean isAuthoritativeMNodeAdmin(Session session, Identifier pid) {
861
        boolean allowed = false;
862
        //check the parameters
863
        if(session == null) {
864
            logMetacat.debug("D1NodeService.isAuthoritativeMNodeAdmin - the session object is null and return false.");
865
            return allowed;
866
        } else if (pid == null || pid.getValue() == null || pid.getValue().trim().equals("")) {
867
            logMetacat.debug("D1NodeService.isAuthoritativeMNodeAdmin - the Identifier object is null (not being specified) and return false.");
868
            return allowed;
869
        }
870
        
871
        //Get the subject from the session
872
        Subject subject = session.getSubject();
873
        if(subject != null) {
874
            //Get the authoritative member node info from the system metadata
875
            SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
876
            if(sysMeta != null) {
877
                NodeReference authoritativeMNode = sysMeta.getAuthoritativeMemberNode();
878
                if(authoritativeMNode != null) {
879
                        CNode cn = null;
880
                        try {
881
                            cn = D1Client.getCN();
882
                        } catch (BaseException e) {
883
                            logMetacat.error("D1NodeService.isAuthoritativeMNodeAdmin - couldn't connect to the CN since "+
884
                                            e.getDescription()+ ". The false value will be returned for the AuthoritativeMNodeAdmin.");
885
                            return allowed;
886
                        }
887
                        
888
                        if(cn != null) {
889
                            List<Node> nodes = null;
890
                            try {
891
                                nodes = cn.listNodes().getNodeList();
892
                            } catch (NotImplemented e) {
893
                                logMetacat.error("D1NodeService.isAuthoritativeMNodeAdmin - couldn't get the member nodes list from the CN since "+e.getDescription()+ 
894
                                                ". The false value will be returned for the AuthoritativeMNodeAdmin.");
895
                                return allowed;
896
                            } catch (ServiceFailure ee) {
897
                                logMetacat.error("D1NodeService.isAuthoritativeMNodeAdmin - couldn't get the member nodes list from the CN since "+ee.getDescription()+ 
898
                                                ". The false value will be returned for the AuthoritativeMNodeAdmin.");
899
                                return allowed;
900
                            }
901
                            if(nodes != null) {
902
                                for(Node node : nodes) {
903
                                    //find the authoritative node and get its subjects
904
                                    if (node.getType() == NodeType.MN && node.getIdentifier() != null && node.getIdentifier().equals(authoritativeMNode)) {
905
                                        List<Subject> nodeSubjects = node.getSubjectList();
906
                                        if(nodeSubjects != null) {
907
                                            // check if the session subject is in the node subject list
908
                                            for (Subject nodeSubject : nodeSubjects) {
909
                                                logMetacat.debug("D1NodeService.isAuthoritativeMNodeAdmin(), comparing subjects: " +
910
                                                    nodeSubject.getValue() + " and " + subject.getValue());
911
                                                if ( nodeSubject != null && nodeSubject.equals(subject) ) {
912
                                                    allowed = true; // subject of session == target node subject
913
                                                    break;
914
                                                }
915
                                            }              
916
                                        }
917
                                      
918
                                    }
919
                                }
920
                            }
921
                        }
922
                }
923
            }
924
        }
925
        return allowed;
926
    }
927
    
928
    
929
  /**
930
   * Test if the user identified by the provided token has administrative authorization 
931
   * 
932
   * @param session - the Session object containing the credentials for the Subject
933
   * 
934
   * @return true if the user is admin (mn itself or a cn )
935
   * 
936
   * @throws ServiceFailure
937
   * @throws InvalidToken
938
   * @throws NotFound
939
   * @throws NotAuthorized
940
   * @throws NotImplemented
941
   */
942
  public boolean isAdminAuthorized(Session session) 
943
      throws ServiceFailure, InvalidToken, NotAuthorized,
944
      NotImplemented {
945

    
946
      boolean allowed = false;
947
      
948
      // must have a session in order to check admin 
949
      if (session == null) {
950
         logMetacat.debug("In isAdminAuthorized(), session is null ");
951
         return false;
952
      }
953
      
954
      logMetacat.debug("In isAdminAuthorized(), checking CN or MN authorization for " +
955
           session.getSubject().getValue());
956
      
957
      // check if this is the node calling itself (MN)
958
      try {
959
          allowed = isNodeAdmin(session);
960
      } catch (Exception e) {
961
          logMetacat.warn("We can't determine if the session is a node subject. But we will contiune to check if it is a cn subject.");
962
      }
963
      
964
      
965
      // check the CN list
966
      if (!allowed) {
967
	      allowed = isCNAdmin(session);
968
      }
969
      
970
      return allowed;
971
  }
972
  
973
  /*
974
   * Determine if the specified session is a CN or not. Return true if it is; otherwise false.
975
   */
976
  protected boolean isCNAdmin (Session session) {
977
      boolean allowed = false;
978
      List<Node> nodes = null;
979

    
980
      try {
981
          // are we allowed to do this? only CNs are allowed
982
          CNode cn = D1Client.getCN();
983
          nodes = cn.listNodes().getNodeList();
984
      }
985
      catch (Throwable e) {
986
          logMetacat.warn("Couldn't get the node list from the cn since "+e.getMessage()+". So we can't determine if the subject is a CN.");
987
          return false;  
988
      }
989
          
990
      if ( nodes == null ) {
991
          return false;
992
          //throw new ServiceFailure("4852", "Couldn't get node list.");
993
      }
994
      
995
      // find the node in the node list
996
      for ( Node node : nodes ) {
997
          
998
          NodeReference nodeReference = node.getIdentifier();
999
          logMetacat.debug("In isAdminAuthorized(), Node reference is: " + nodeReference.getValue());
1000
          
1001
          Subject subject = session.getSubject();
1002
          
1003
          if (node.getType() == NodeType.CN) {
1004
              List<Subject> nodeSubjects = node.getSubjectList();
1005
              
1006
              // check if the session subject is in the node subject list
1007
              for (Subject nodeSubject : nodeSubjects) {
1008
                  logMetacat.debug("In isAdminAuthorized(), comparing subjects: " +
1009
                      nodeSubject.getValue() + " and " + subject.getValue());
1010
                  if ( nodeSubject.equals(subject) ) {
1011
                      allowed = true; // subject of session == target node subject
1012
                      break;
1013
                      
1014
                  }
1015
              }              
1016
          }
1017
      }
1018
      return allowed;
1019
  }
1020
  
1021
  /**
1022
   * Test if the user identified by the provided token has administrative authorization 
1023
   * on this node because they are calling themselves
1024
   * 
1025
   * @param session - the Session object containing the credentials for the Subject
1026
   * 
1027
   * @return true if the user is this node
1028
   * @throws ServiceFailure 
1029
   * @throws NotImplemented 
1030
   */
1031
  public boolean isNodeAdmin(Session session) throws NotImplemented, ServiceFailure {
1032

    
1033
      boolean allowed = false;
1034
      
1035
      // must have a session in order to check admin 
1036
      if (session == null) {
1037
         logMetacat.debug("In isNodeAdmin(), session is null ");
1038
         return false;
1039
      }
1040
      
1041
      logMetacat.debug("In isNodeAdmin(), MN authorization for " +
1042
           session.getSubject().getValue());
1043
      
1044
      Node node = MNodeService.getInstance(request).getCapabilities();
1045
      NodeReference nodeReference = node.getIdentifier();
1046
      logMetacat.debug("In isNodeAdmin(), Node reference is: " + nodeReference.getValue());
1047
      
1048
      Subject subject = session.getSubject();
1049
      
1050
      if (node.getType() == NodeType.MN) {
1051
          List<Subject> nodeSubjects = node.getSubjectList();
1052
          
1053
          // check if the session subject is in the node subject list
1054
          for (Subject nodeSubject : nodeSubjects) {
1055
              logMetacat.debug("In isNodeAdmin(), comparing subjects: " +
1056
                  nodeSubject.getValue() + " and " + subject.getValue());
1057
              if ( nodeSubject.equals(subject) ) {
1058
                  allowed = true; // subject of session == this node's subect
1059
                  break;
1060
              }
1061
          }              
1062
      }
1063
      
1064
      return allowed;
1065
  }
1066
  
1067
  /**
1068
   * Test if the user identified by the provided token has authorization 
1069
   * for the operation on the specified object.
1070
   * Allowed subjects include:
1071
   * 1. CNs
1072
   * 2. Authoritative node
1073
   * 3. Owner of the object
1074
   * 4. Users with the specified permission in the access rules.
1075
   * 
1076
   * @param session - the Session object containing the credentials for the Subject
1077
   * @param pid - The identifer of the resource for which access is being checked
1078
   * @param operation - The type of operation which is being requested for the given pid
1079
   *
1080
   * @return true if the operation is allowed
1081
   * 
1082
   * @throws ServiceFailure
1083
   * @throws InvalidToken
1084
   * @throws NotFound
1085
   * @throws NotAuthorized
1086
   * @throws NotImplemented
1087
   * @throws InvalidRequest
1088
   */
1089
  public boolean isAuthorized(Session session, Identifier pid, Permission permission)
1090
    throws ServiceFailure, InvalidToken, NotFound, NotAuthorized,
1091
    NotImplemented, InvalidRequest {
1092

    
1093
    boolean allowed = false;
1094
    
1095
    if (permission == null) {
1096
    	throw new InvalidRequest("1761", "Permission was not provided or is invalid");
1097
    }
1098
    
1099
    // always allow CN access
1100
    if ( isAdminAuthorized(session) ) {
1101
        allowed = true;
1102
        return allowed;
1103
        
1104
    }
1105
    
1106
    String serviceFailureCode = "1760";
1107
    Identifier sid = getPIDForSID(pid, serviceFailureCode);
1108
    if(sid != null) {
1109
        pid = sid;
1110
    }
1111
    
1112
    // the authoritative member node of the pid always has the access as well.
1113
    if (isAuthoritativeMNodeAdmin(session, pid)) {
1114
        allowed = true;
1115
        return allowed;
1116
    }
1117
    
1118
    //is it the owner of the object or the access rules allow the user?
1119
    allowed = userHasPermission(session,  pid, permission );
1120
    
1121
    // throw or return?
1122
    if (!allowed) {
1123
     // track the identities we have checked against
1124
      StringBuffer includedSubjects = new StringBuffer();
1125
      Set<Subject> subjects = AuthUtils.authorizedClientSubjects(session);
1126
      for (Subject s: subjects) {
1127
             includedSubjects.append(s.getValue() + "; ");
1128
        }    
1129
      throw new NotAuthorized("1820", permission + " not allowed on " + pid.getValue() + " for subject[s]: " + includedSubjects.toString() );
1130
    }
1131
    
1132
    return allowed;
1133
    
1134
  }
1135
  
1136
  
1137
  /*
1138
   * Determine if a user has the permission to perform the specified permission.
1139
   * 1. Owner can have any permission.
1140
   * 2. Access table allow the user has the permission
1141
   */
1142
  protected boolean userHasPermission(Session userSession, Identifier pid, Permission permission ) throws NotFound{
1143
      boolean allowed = false;
1144
      // permissions are hierarchical
1145
      List<Permission> expandedPermissions = null;
1146
      // get the subject[s] from the session
1147
      //defer to the shared util for recursively compiling the subjects   
1148
      Set<Subject> subjects = AuthUtils.authorizedClientSubjects(userSession);
1149
          
1150
      // get the system metadata
1151
      String pidStr = pid.getValue();
1152
      SystemMetadata systemMetadata = null;
1153
      try {
1154
          systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1155

    
1156
      } catch (Exception e) {
1157
          // convert Hazelcast RuntimeException to NotFound
1158
          logMetacat.error("An error occurred while getting system metadata for identifier " +
1159
              pid.getValue() + ". The error message was: " + e.getMessage());
1160
          throw new NotFound("1800", "No record found for " + pidStr);
1161
          
1162
      } 
1163
      
1164
      // throw not found if it was not found
1165
      if (systemMetadata == null) {
1166
          String localId = null;
1167
          String error = "No system metadata could be found for given PID: " + pidStr;
1168
          try {
1169
              localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1170
            
1171
           } catch (Exception e) {
1172
              logMetacat.warn("Couldn't find the local id for the pid "+pidStr);
1173
          }
1174
          
1175
          if(localId != null && EventLog.getInstance().isDeleted(localId)) {
1176
              error = error + ". "+DELETEDMESSAGE;
1177
          } else if (localId == null && EventLog.getInstance().isDeleted(pid.getValue())) {
1178
              error = error + ". "+DELETEDMESSAGE;
1179
          }
1180
          throw new NotFound("1800", error);
1181
      }
1182
          
1183
      // do we own it?
1184
      for (Subject s: subjects) {
1185
        logMetacat.debug("Comparing \t" + 
1186
                         systemMetadata.getRightsHolder().getValue() +
1187
                         " \tagainst \t" + s.getValue());
1188
          //includedSubjects.append(s.getValue() + "; ");
1189
          allowed = systemMetadata.getRightsHolder().equals(s);
1190
          if (allowed) {
1191
              return allowed;
1192
          }
1193
      }    
1194
      
1195
      // otherwise check the access rules
1196
      try {
1197
          List<AccessRule> allows = systemMetadata.getAccessPolicy().getAllowList();
1198
          search: // label break
1199
          for (AccessRule accessRule: allows) {
1200
            for (Subject s: subjects) {
1201
              logMetacat.debug("Checking allow access rule for subject: " + s.getValue());
1202
              if (accessRule.getSubjectList().contains(s)) {
1203
                  logMetacat.debug("Access rule contains subject: " + s.getValue());
1204
                  for (Permission p: accessRule.getPermissionList()) {
1205
                      logMetacat.debug("Checking permission: " + p.xmlValue());
1206
                      expandedPermissions = expandPermissions(p);
1207
                      allowed = expandedPermissions.contains(permission);
1208
                      if (allowed) {
1209
                          logMetacat.info("Permission granted: " + p.xmlValue() + " to " + s.getValue());
1210
                          break search; //label break
1211
                      }
1212
                  }
1213
                  
1214
              }
1215
            }
1216
          }
1217
      } catch (Exception e) {
1218
          // catch all for errors - safe side should be to deny the access
1219
          logMetacat.error("Problem checking authorization - defaulting to deny", e);
1220
          allowed = false;
1221
        
1222
      }
1223
      return allowed;
1224
  }
1225
  /*
1226
   * parse a logEntry and get the relevant field from it
1227
   * 
1228
   * @param fieldname
1229
   * @param entry
1230
   * @return
1231
   */
1232
  private String getLogEntryField(String fieldname, String entry) {
1233
    String begin = "<" + fieldname + ">";
1234
    String end = "</" + fieldname + ">";
1235
    // logMetacat.debug("looking for " + begin + " and " + end +
1236
    // " in entry " + entry);
1237
    String s = entry.substring(entry.indexOf(begin) + begin.length(), entry
1238
        .indexOf(end));
1239
    logMetacat.debug("entry " + fieldname + " : " + s);
1240
    return s;
1241
  }
1242

    
1243
  /** 
1244
   * Determine if a given object should be treated as an XML science metadata
1245
   * object. 
1246
   * 
1247
   * @param sysmeta - the SystemMetadata describing the object
1248
   * @return true if the object should be treated as science metadata
1249
   */
1250
  public static boolean isScienceMetadata(SystemMetadata sysmeta) {
1251
    
1252
    ObjectFormat objectFormat = null;
1253
    boolean isScienceMetadata = false;
1254
    
1255
    try {
1256
      objectFormat = ObjectFormatCache.getInstance().getFormat(sysmeta.getFormatId());
1257
      if ( objectFormat.getFormatType().equals("METADATA") ) {
1258
      	isScienceMetadata = true;
1259
      	
1260
      }
1261
      
1262
       
1263
    } catch (ServiceFailure e) {
1264
      logMetacat.debug("There was a problem determining if the object identified by" + 
1265
          sysmeta.getIdentifier().getValue() + 
1266
          " is science metadata: " + e.getMessage());
1267
    
1268
    } catch (NotFound e) {
1269
      logMetacat.debug("There was a problem determining if the object identified by" + 
1270
          sysmeta.getIdentifier().getValue() + 
1271
          " is science metadata: " + e.getMessage());
1272
    
1273
    }
1274
    
1275
    return isScienceMetadata;
1276

    
1277
  }
1278
  
1279
  /**
1280
   * Check fro whitespace in the given pid.
1281
   * null pids are also invalid by default
1282
   * @param pid
1283
   * @return
1284
   */
1285
  public static boolean isValidIdentifier(Identifier pid) {
1286
	  if (pid != null && pid.getValue() != null && pid.getValue().length() > 0) {
1287
		  return !pid.getValue().matches(".*\\s+.*");
1288
	  } 
1289
	  return false;
1290
  }
1291
  
1292
  
1293
  /**
1294
   * Insert or update an XML document into Metacat
1295
   * 
1296
   * @param xml - the XML document to insert or update
1297
   * @param pid - the identifier to be used for the resulting object
1298
   * 
1299
   * @return localId - the resulting docid of the document created or updated
1300
   * 
1301
   */
1302
  public String insertOrUpdateDocument(InputStream xml, String encoding,  Identifier pid, 
1303
    Session session, String insertOrUpdate) 
1304
    throws ServiceFailure, IOException {
1305
    
1306
  	logMetacat.debug("Starting to insert xml document...");
1307
    IdentifierManager im = IdentifierManager.getInstance();
1308

    
1309
    // generate pid/localId pair for sysmeta
1310
    String localId = null;
1311
    byte[] xmlBytes  = IOUtils.toByteArray(xml);
1312
    String xmlStr = new String(xmlBytes, encoding);
1313
    if(insertOrUpdate.equals("insert")) {
1314
      localId = im.generateLocalId(pid.getValue(), 1);
1315
      
1316
    } else {
1317
      //localid should already exist in the identifier table, so just find it
1318
      try {
1319
        logMetacat.debug("Updating pid " + pid.getValue());
1320
        logMetacat.debug("looking in identifier table for pid " + pid.getValue());
1321
        
1322
        localId = im.getLocalId(pid.getValue());
1323
        
1324
        logMetacat.debug("localId: " + localId);
1325
        //increment the revision
1326
        String docid = localId.substring(0, localId.lastIndexOf("."));
1327
        String revS = localId.substring(localId.lastIndexOf(".") + 1, localId.length());
1328
        int rev = new Integer(revS).intValue();
1329
        rev++;
1330
        docid = docid + "." + rev;
1331
        localId = docid;
1332
        logMetacat.debug("incremented localId: " + localId);
1333
      
1334
      } catch(McdbDocNotFoundException e) {
1335
        throw new ServiceFailure("1030", "D1NodeService.insertOrUpdateDocument(): " +
1336
            "pid " + pid.getValue() + 
1337
            " should have been in the identifier table, but it wasn't: " + 
1338
            e.getMessage());
1339
      
1340
      } catch (SQLException e) {
1341
          throw new ServiceFailure("1030", "D1NodeService.insertOrUpdateDocument() -"+
1342
                     " couldn't identify if the pid "+pid.getValue()+" is in the identifier table since "+e.getMessage());
1343
      }
1344
      
1345
    }
1346

    
1347
    params = new Hashtable<String, String[]>();
1348
    String[] action = new String[1];
1349
    action[0] = insertOrUpdate;
1350
    params.put("action", action);
1351
    String[] docid = new String[1];
1352
    docid[0] = localId;
1353
    params.put("docid", docid);
1354
    String[] doctext = new String[1];
1355
    doctext[0] = xmlStr;
1356
    params.put("doctext", doctext);
1357
    
1358
    String username = Constants.SUBJECT_PUBLIC;
1359
    String[] groupnames = null;
1360
    if (session != null ) {
1361
    	username = session.getSubject().getValue();
1362
    	if (session.getSubjectInfo() != null) {
1363
    		List<Group> groupList = session.getSubjectInfo().getGroupList();
1364
    		if (groupList != null) {
1365
    			groupnames = new String[groupList.size()];
1366
    			for (int i = 0; i < groupList.size(); i++ ) {
1367
    				groupnames[i] = groupList.get(i).getGroupName();
1368
    			}
1369
    		}
1370
    	}
1371
    }
1372
    
1373
    // do the insert or update action
1374
    handler = new MetacatHandler(new Timer());
1375
    String result = handler.handleInsertOrUpdateAction(request.getRemoteAddr(), request.getHeader("User-Agent"), null, 
1376
                        null, params, username, groupnames, false, false, xmlBytes);
1377
    
1378
    if(result.indexOf("<error>") != -1) {
1379
    	String detailCode = "";
1380
    	if ( insertOrUpdate.equals("insert") ) {
1381
    		// make sure to remove the mapping so that subsequent attempts do not fail with IdentifierNotUnique
1382
    		im.removeMapping(pid.getValue(), localId);
1383
    		detailCode = "1190";
1384
    		
1385
    	} else if ( insertOrUpdate.equals("update") ) {
1386
    		detailCode = "1310";
1387
    		
1388
    	}
1389
        throw new ServiceFailure(detailCode, 
1390
          "Error inserting or updating document: " + result);
1391
    }
1392
    logMetacat.debug("Finsished inserting xml document with id " + localId);
1393
    
1394
    return localId;
1395
  }
1396
  
1397
  /**
1398
   * Insert a data document
1399
   * 
1400
   * @param object
1401
   * @param pid
1402
   * @param sessionData
1403
   * @throws ServiceFailure
1404
   * @returns localId of the data object inserted
1405
   */
1406
  public String insertDataObject(InputStream object, Identifier pid, 
1407
          Session session) throws ServiceFailure {
1408
      
1409
    String username = Constants.SUBJECT_PUBLIC;
1410
    String[] groupnames = null;
1411
    if (session != null ) {
1412
    	username = session.getSubject().getValue();
1413
    	if (session.getSubjectInfo() != null) {
1414
    		List<Group> groupList = session.getSubjectInfo().getGroupList();
1415
    		if (groupList != null) {
1416
    			groupnames = new String[groupList.size()];
1417
    			for (int i = 0; i < groupList.size(); i++ ) {
1418
    				groupnames[i] = groupList.get(i).getGroupName();
1419
    			}
1420
    		}
1421
    	}
1422
    }
1423
  
1424
    // generate pid/localId pair for object
1425
    logMetacat.debug("Generating a pid/localId mapping");
1426
    IdentifierManager im = IdentifierManager.getInstance();
1427
    String localId = im.generateLocalId(pid.getValue(), 1);
1428
  
1429
    // Save the data file to disk using "localId" as the name
1430
    String datafilepath = null;
1431
	try {
1432
		datafilepath = PropertyService.getProperty("application.datafilepath");
1433
	} catch (PropertyNotFoundException e) {
1434
		ServiceFailure sf = new ServiceFailure("1190", "Lookup data file path" + e.getMessage());
1435
		sf.initCause(e);
1436
		throw sf;
1437
	}
1438
    boolean locked = false;
1439
	try {
1440
		locked = DocumentImpl.getDataFileLockGrant(localId);
1441
	} catch (Exception e) {
1442
		ServiceFailure sf = new ServiceFailure("1190", "Could not lock file for writing:" + e.getMessage());
1443
		sf.initCause(e);
1444
		throw sf;
1445
	}
1446

    
1447
    logMetacat.debug("Case DATA: starting to write to disk.");
1448
	if (locked) {
1449

    
1450
          File dataDirectory = new File(datafilepath);
1451
          dataDirectory.mkdirs();
1452
  
1453
          File newFile = writeStreamToFile(dataDirectory, localId, object);
1454
  
1455
          // TODO: Check that the file size matches SystemMetadata
1456
          // long size = newFile.length();
1457
          // if (size == 0) {
1458
          //     throw new IOException("Uploaded file is 0 bytes!");
1459
          // }
1460
  
1461
          // Register the file in the database (which generates an exception
1462
          // if the localId is not acceptable or other untoward things happen
1463
          try {
1464
            logMetacat.debug("Registering document...");
1465
            DocumentImpl.registerDocument(localId, "BIN", localId,
1466
                    username, groupnames);
1467
            logMetacat.debug("Registration step completed.");
1468
            
1469
          } catch (SQLException e) {
1470
            //newFile.delete();
1471
            logMetacat.debug("SQLE: " + e.getMessage());
1472
            e.printStackTrace(System.out);
1473
            throw new ServiceFailure("1190", "Registration failed: " + 
1474
            		e.getMessage());
1475
            
1476
          } catch (AccessionNumberException e) {
1477
            //newFile.delete();
1478
            logMetacat.debug("ANE: " + e.getMessage());
1479
            e.printStackTrace(System.out);
1480
            throw new ServiceFailure("1190", "Registration failed: " + 
1481
            	e.getMessage());
1482
            
1483
          } catch (Exception e) {
1484
            //newFile.delete();
1485
            logMetacat.debug("Exception: " + e.getMessage());
1486
            e.printStackTrace(System.out);
1487
            throw new ServiceFailure("1190", "Registration failed: " + 
1488
            	e.getMessage());
1489
          }
1490
  
1491
          logMetacat.debug("Logging the creation event.");
1492
          EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, "create");
1493
  
1494
          // Schedule replication for this data file, the "insert" action is important here!
1495
          logMetacat.debug("Scheduling replication.");
1496
          ForceReplicationHandler frh = new ForceReplicationHandler(localId, "insert", false, null);
1497
      }
1498
      
1499
      return localId;
1500
    
1501
  }
1502

    
1503
  /**
1504
   * Insert a systemMetadata document and return its localId
1505
   */
1506
  public void insertSystemMetadata(SystemMetadata sysmeta) 
1507
      throws ServiceFailure {
1508
      
1509
  	  logMetacat.debug("Starting to insert SystemMetadata...");
1510
      sysmeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1511
      logMetacat.debug("Inserting new system metadata with modified date " + 
1512
          sysmeta.getDateSysMetadataModified());
1513
      
1514
      //insert the system metadata
1515
      try {
1516
        // note: the calling subclass handles the map hazelcast lock/unlock
1517
      	HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
1518
      	// submit for indexing
1519
        MetacatSolrIndex.getInstance().submit(sysmeta.getIdentifier(), sysmeta, null, true);
1520
      } catch (Exception e) {
1521
          throw new ServiceFailure("1190", e.getMessage());
1522
          
1523
	    }  
1524
  }
1525
  
1526
  /**
1527
   * Retrieve the list of objects present on the MN that match the calling parameters
1528
   * 
1529
   * @param session - the Session object containing the credentials for the Subject
1530
   * @param startTime - Specifies the beginning of the time range from which 
1531
   *                    to return object (>=)
1532
   * @param endTime - Specifies the beginning of the time range from which 
1533
   *                  to return object (>=)
1534
   * @param objectFormat - Restrict results to the specified object format
1535
   * @param replicaStatus - Indicates if replicated objects should be returned in the list
1536
   * @param start - The zero-based index of the first value, relative to the 
1537
   *                first record of the resultset that matches the parameters.
1538
   * @param count - The maximum number of entries that should be returned in 
1539
   *                the response. The Member Node may return less entries 
1540
   *                than specified in this value.
1541
   * 
1542
   * @return objectList - the list of objects matching the criteria
1543
   * 
1544
   * @throws InvalidToken
1545
   * @throws ServiceFailure
1546
   * @throws NotAuthorized
1547
   * @throws InvalidRequest
1548
   * @throws NotImplemented
1549
   */
1550
  public ObjectList listObjects(Session session, Date startTime, Date endTime, ObjectFormatIdentifier objectFormatId, Identifier identifier, NodeReference nodeId, Integer start,
1551
          Integer count) throws NotAuthorized, InvalidRequest, NotImplemented, ServiceFailure, InvalidToken {
1552

    
1553
      ObjectList objectList = null;
1554

    
1555
      try {
1556
          // safeguard against large requests
1557
          if (count == null || count > MAXIMUM_DB_RECORD_COUNT) {
1558
              count = MAXIMUM_DB_RECORD_COUNT;
1559
          }
1560
          boolean isSid = false;
1561
          if(identifier != null) {
1562
              isSid = IdentifierManager.getInstance().systemMetadataSIDExists(identifier);
1563
          }
1564
          objectList = IdentifierManager.getInstance().querySystemMetadata(startTime, endTime, objectFormatId, nodeId, start, count, identifier, isSid);
1565
      } catch (Exception e) {
1566
          throw new ServiceFailure("1580", "Error querying system metadata: " + e.getMessage());
1567
      }
1568

    
1569
      return objectList;
1570
  }
1571

    
1572
  /**
1573
   * Update a systemMetadata document
1574
   * 
1575
   * @param sysMeta - the system metadata object in the system to update
1576
   */
1577
    protected void updateSystemMetadata(SystemMetadata sysMeta)
1578
        throws ServiceFailure {
1579

    
1580
        logMetacat.debug("D1NodeService.updateSystemMetadata() called.");
1581
        sysMeta.setDateSysMetadataModified(new Date());
1582
        try {
1583
            HazelcastService.getInstance().getSystemMetadataMap().lock(sysMeta.getIdentifier());
1584
            HazelcastService.getInstance().getSystemMetadataMap().put(sysMeta.getIdentifier(), sysMeta);
1585
            // submit for indexing
1586
            MetacatSolrIndex.getInstance().submit(sysMeta.getIdentifier(), sysMeta, null, true);
1587
        } catch (Exception e) {
1588
            throw new ServiceFailure("4862", e.getMessage());
1589

    
1590
        } finally {
1591
            HazelcastService.getInstance().getSystemMetadataMap().unlock(sysMeta.getIdentifier());
1592

    
1593
        }
1594

    
1595
    }
1596
    
1597
    /**
1598
     * Update the system metadata of the specified pid
1599
     * @param session - the identity of the client which calls the method
1600
     * @param pid - the identifier of the object which will be updated
1601
     * @param sysmeta - the new system metadata  
1602
     * @return
1603
     * @throws NotImplemented
1604
     * @throws NotAuthorized
1605
     * @throws ServiceFailure
1606
     * @throws InvalidRequest
1607
     * @throws InvalidSystemMetadata
1608
     * @throws InvalidToken
1609
     */
1610
	public boolean updateSystemMetadata(Session session, Identifier pid,
1611
			SystemMetadata sysmeta) throws NotImplemented, NotAuthorized,
1612
			ServiceFailure, InvalidRequest, InvalidSystemMetadata, InvalidToken {
1613
		
1614
		// The lock to be used for this identifier
1615
      Lock lock = null;
1616
     
1617
      // verify that guid == SystemMetadata.getIdentifier()
1618
      logMetacat.debug("Comparing guid|sysmeta_guid: " + pid.getValue() + 
1619
          "|" + sysmeta.getIdentifier().getValue());
1620
      
1621
      if (!pid.getValue().equals(sysmeta.getIdentifier().getValue())) {
1622
          throw new InvalidRequest("4863", 
1623
              "The identifier in method call (" + pid.getValue() + 
1624
              ") does not match identifier in system metadata (" +
1625
              sysmeta.getIdentifier().getValue() + ").");
1626
      }
1627
      //compare serial version.
1628
      
1629
      //check the sid
1630
      SystemMetadata currentSysmeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1631
      logMetacat.debug("The current dateUploaded is ============"+currentSysmeta.getDateUploaded());
1632
      logMetacat.debug("the dateUploaded in the new system metadata is "+sysmeta.getDateUploaded());
1633
      logMetacat.debug("The current dateUploaded is (by time) ============"+currentSysmeta.getDateUploaded().getTime());
1634
      logMetacat.debug("the dateUploaded in the new system metadata is (by time) "+sysmeta.getDateUploaded().getTime());
1635
      if(currentSysmeta == null ) {
1636
          //do we need throw an exception?
1637
          
1638
      } else {
1639
          
1640
          BigInteger newVersion = sysmeta.getSerialVersion();
1641
          if(newVersion == null) {
1642
              throw new InvalidRequest("4869", "The serial version can't be null in the new system metadata");
1643
          }
1644
          BigInteger currentVersion = currentSysmeta.getSerialVersion();
1645
          if(currentVersion != null && newVersion.compareTo(currentVersion) <= 0) {
1646
              throw new InvalidRequest("4869", "The serial version in the new system metadata is "+newVersion.toString()+
1647
                      " which is less than or equals the previous version "+currentVersion.toString()+". This is illegal in the updateSystemMetadata method.");
1648
          }
1649
          Identifier currentSid = currentSysmeta.getSeriesId();
1650
          if(currentSid != null) {
1651
              //new sid must match the current sid
1652
              Identifier newSid = sysmeta.getSeriesId();
1653
              if (!isValidIdentifier(newSid)) {
1654
                  throw new InvalidRequest("4869", "The series id in the system metadata is invalid in the request.");
1655
              } else {
1656
                  if(!newSid.getValue().equals(currentSid.getValue())) {
1657
                      throw new InvalidRequest("4869", "The series id "+newSid.getValue() +" in the system metadata doesn't match the current sid "+currentSid.getValue());
1658
                  }
1659
              }
1660
          } else {
1661
              //current system metadata doesn't have a sid. So we can have those scenarios
1662
              //1. The new sid may be null as well
1663
              //2. If the new sid does exist, it may be an identifier which hasn't bee used.
1664
              //3. If the new sid does exist, it may be an sid which equals the SID it obsoletes
1665
              //4. If the new sid does exist, it may be an sid which equauls the SID it was obsoleted by
1666
              Identifier newSid = sysmeta.getSeriesId();
1667
              if(newSid != null) {
1668
                  //It matches the rules of the checkSidInModifyingSystemMetadata
1669
                  checkSidInModifyingSystemMetadata(sysmeta, "4956", "4868");
1670
              }
1671
          }
1672
          checkModifiedImmutableFields(currentSysmeta, sysmeta);
1673
          checkOneTimeSettableSysmMetaFields(currentSysmeta, sysmeta);
1674
          if(currentSysmeta.getObsoletes() == null && sysmeta.getObsoletes() != null) {
1675
              //we are setting a value to the obsoletes field, so we should make sure if there is not object obsoletes the value
1676
              String obsoletes = existsInObsoletes(sysmeta.getObsoletes());
1677
              if( obsoletes != null) {
1678
                  throw new InvalidSystemMetadata("4956", "There is an object with id "+obsoletes +
1679
                          " already obsoletes the pid "+sysmeta.getObsoletes().getValue() +". You can't set the object "+pid.getValue()+" to obsolete the pid "+sysmeta.getObsoletes().getValue()+" again.");
1680
              }
1681
          }
1682
          
1683
          if(currentSysmeta.getObsoletedBy() == null && sysmeta.getObsoletedBy() != null) {
1684
              //we are setting a value to the obsoletedBy field, so we should make sure that the no another object obsoletes the pid we are updating. 
1685
              String obsoletedBy = existsInObsoletedBy(sysmeta.getObsoletedBy());
1686
              if( obsoletedBy != null) {
1687
                  throw new InvalidSystemMetadata("4956", "There is an object with id "+obsoletedBy +
1688
                          " already is obsoleted by the pid "+sysmeta.getObsoletedBy().getValue() +". You can't set the pid "+pid.getValue()+" to be obsoleted by the pid "+sysmeta.getObsoletedBy().getValue()+" again.");
1689
              }
1690
          }
1691
      }
1692
      
1693
      // do the actual update
1694
      this.updateSystemMetadata(sysmeta);
1695
      
1696
      try {
1697
    	  String localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1698
    	  EventLog.getInstance().log(request.getRemoteAddr(), 
1699
    	          request.getHeader("User-Agent"), session.getSubject().getValue(), 
1700
    	          localId, "updateSystemMetadata");
1701
      } catch (McdbDocNotFoundException e) {
1702
    	  // do nothing, no localId to log with
1703
    	  logMetacat.warn("Could not log 'updateSystemMetadata' event because no localId was found for pid: " + pid.getValue());
1704
      } catch (SQLException e) {
1705
          logMetacat.warn("Could not log 'updateSystemMetadata' event because the localId couldn't be identified for the pid: " + pid.getValue());
1706
      }
1707
      return true;
1708
	}
1709
	
1710
	
1711
	/*
1712
	 * Check if the newMeta modifies an immutable field. 
1713
	 */
1714
	private void checkModifiedImmutableFields(SystemMetadata orgMeta, SystemMetadata newMeta) throws InvalidRequest{
1715
	    if(orgMeta != null && newMeta != null) {
1716
	        if(newMeta.getIdentifier() == null) {
1717
	            throw new InvalidRequest("4869", "The new version of the system metadata is invalid since the identifier is null");
1718
	        }
1719
	        if(!orgMeta.getIdentifier().equals(newMeta.getIdentifier())) {
1720
	            throw new InvalidRequest("4869","The request is trying to modify an immutable field in the SystemMeta: the new system meta's identifier "+newMeta.getIdentifier().getValue()+" is "+
1721
	                  "different to the orginal one "+orgMeta.getIdentifier().getValue());
1722
	        }
1723
	        if(newMeta.getSize() == null) {
1724
	            throw new InvalidRequest("4869", "The new version of the system metadata is invalid since the size is null");
1725
	        }
1726
	        if(!orgMeta.getSize().equals(newMeta.getSize())) {
1727
	            throw new InvalidRequest("4869", "The request is trying to modify an immutable field in the SystemMeta: the new system meta's size "+newMeta.getSize().longValue()+" is "+
1728
	                      "different to the orginal one "+orgMeta.getSize().longValue());
1729
	        }
1730
	        if(newMeta.getChecksum()!= null && orgMeta.getChecksum() != null && !orgMeta.getChecksum().getValue().equals(newMeta.getChecksum().getValue())) {
1731
	            logMetacat.error("The request is trying to modify an immutable field in the SystemMeta: the new system meta's checksum "+newMeta.getChecksum().getValue()+" is "+
1732
                        "different to the orginal one "+orgMeta.getChecksum().getValue());
1733
	            throw new InvalidRequest("4869", "The request is trying to modify an immutable field in the SystemMeta: the new system meta's checksum "+newMeta.getChecksum().getValue()+" is "+
1734
                        "different to the orginal one "+orgMeta.getChecksum().getValue());
1735
	        }
1736
	        if(orgMeta.getSubmitter() != null && newMeta.getSubmitter() != null && !orgMeta.getSubmitter().equals(newMeta.getSubmitter())) {
1737
	            throw new InvalidRequest("4869", "The request is trying to modify an immutable field in the SystemMeta: the new system meta's submitter "+newMeta.getSubmitter().getValue()+" is "+
1738
                        "different to the orginal one "+orgMeta.getSubmitter().getValue());
1739
	        }
1740
	        
1741
	        if(orgMeta.getDateUploaded() != null && newMeta.getDateUploaded() != null && orgMeta.getDateUploaded().getTime() != newMeta.getDateUploaded().getTime()) {
1742
	            throw new InvalidRequest("4869", "The request is trying to modify an immutable field in the SystemMeta: the new system meta's date of uploaded "+newMeta.getDateUploaded()+" is "+
1743
                        "different to the orginal one "+orgMeta.getDateUploaded());
1744
	        }
1745
	        
1746
	        if(orgMeta.getOriginMemberNode() != null && newMeta.getOriginMemberNode() != null && !orgMeta.getOriginMemberNode().equals(newMeta.getOriginMemberNode())) {
1747
	            throw new InvalidRequest("4869", "The request is trying to modify an immutable field in the SystemMeta: the new system meta's orginal member node  "+newMeta.getOriginMemberNode().getValue()+" is "+
1748
                        "different to the orginal one "+orgMeta.getOriginMemberNode().getValue());
1749
	        }
1750
	        
1751
	        if (orgMeta.getOriginMemberNode() != null && newMeta.getOriginMemberNode() == null ) {
1752
	            throw new InvalidRequest("4869", "The request is trying to modify an immutable field in the SystemMeta: the new system meta's orginal member node is null and it "+" is "+
1753
                        "different to the orginal one "+orgMeta.getOriginMemberNode().getValue());
1754
	        }
1755
	        
1756
	        if(orgMeta.getSeriesId() != null && newMeta.getSeriesId() != null && !orgMeta.getSeriesId().equals(newMeta.getSeriesId())) {
1757
                throw new InvalidRequest("4869", "The request is trying to modify an immutable field in the SystemMeta: the new system meta's series id  "+newMeta.getSeriesId().getValue()+" is "+
1758
                        "different to the orginal one "+orgMeta.getSeriesId().getValue());
1759
            }
1760
	        
1761
	    }
1762
	}
1763
	
1764
	/*
1765
	 * Some fields in the system metadata, such as obsoletes or obsoletedBy can be set only once. 
1766
	 * After set, they are not allowed to be changed.
1767
	 */
1768
	private void checkOneTimeSettableSysmMetaFields(SystemMetadata orgMeta, SystemMetadata newMeta) throws InvalidRequest {
1769
	    if(orgMeta.getObsoletedBy() != null ) {
1770
	        if(newMeta.getObsoletedBy() == null || !orgMeta.getObsoletedBy().equals(newMeta.getObsoletedBy())) {
1771
	            throw new InvalidRequest("4869", "The request is trying to reset the obsoletedBy field in the system metadata of the object "
1772
	                    + orgMeta.getIdentifier().getValue() +". This is illegal since the obsoletedBy filed is set, you can't change it again.");
1773
	        }
1774
        }
1775
	    if(orgMeta.getObsoletes() != null) {
1776
	        if(newMeta.getObsoletes() == null || !orgMeta.getObsoletes().equals(newMeta.getObsoletes())) {
1777
	            throw new InvalidRequest("4869", "The request is trying to reset the obsoletes field in the system metadata of the object"+
1778
	               orgMeta.getIdentifier().getValue()+". This is illegal since the obsoletes filed is set, you can't change it again.");
1779
	        }
1780
	    }
1781
	}
1782
  
1783
  /**
1784
   * Given a Permission, returns a list of all permissions that it encompasses
1785
   * Permissions are hierarchical so that WRITE also allows READ.
1786
   * @param permission
1787
   * @return list of included Permissions for the given permission
1788
   */
1789
  protected List<Permission> expandPermissions(Permission permission) {
1790
	  	List<Permission> expandedPermissions = new ArrayList<Permission>();
1791
	    if (permission.equals(Permission.READ)) {
1792
	    	expandedPermissions.add(Permission.READ);
1793
	    }
1794
	    if (permission.equals(Permission.WRITE)) {
1795
	    	expandedPermissions.add(Permission.READ);
1796
	    	expandedPermissions.add(Permission.WRITE);
1797
	    }
1798
	    if (permission.equals(Permission.CHANGE_PERMISSION)) {
1799
	    	expandedPermissions.add(Permission.READ);
1800
	    	expandedPermissions.add(Permission.WRITE);
1801
	    	expandedPermissions.add(Permission.CHANGE_PERMISSION);
1802
	    }
1803
	    return expandedPermissions;
1804
  }
1805

    
1806
  /*
1807
   * Write a stream to a file
1808
   * 
1809
   * @param dir - the directory to write to
1810
   * @param fileName - the file name to write to
1811
   * @param data - the object bytes as an input stream
1812
   * 
1813
   * @return newFile - the new file created
1814
   * 
1815
   * @throws ServiceFailure
1816
   */
1817
  private File writeStreamToFile(File dir, String fileName, InputStream data) 
1818
    throws ServiceFailure {
1819
    
1820
    File newFile = new File(dir, fileName);
1821
    logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
1822

    
1823
    try {
1824
        if (newFile.createNewFile()) {
1825
          // write data stream to desired file
1826
          OutputStream os = new FileOutputStream(newFile);
1827
          long length = IOUtils.copyLarge(data, os);
1828
          os.flush();
1829
          os.close();
1830
        } else {
1831
          logMetacat.debug("File creation failed, or file already exists.");
1832
          throw new ServiceFailure("1190", "File already exists: " + fileName);
1833
        }
1834
    } catch (FileNotFoundException e) {
1835
      logMetacat.debug("FNF: " + e.getMessage());
1836
      throw new ServiceFailure("1190", "File not found: " + fileName + " " 
1837
                + e.getMessage());
1838
    } catch (IOException e) {
1839
      logMetacat.debug("IOE: " + e.getMessage());
1840
      throw new ServiceFailure("1190", "File was not written: " + fileName 
1841
                + " " + e.getMessage());
1842
    }
1843

    
1844
    return newFile;
1845
  }
1846

    
1847
  /*
1848
   * Returns a list of nodes that have been registered with the DataONE infrastructure
1849
   * that match the given session subject
1850
   * @return nodes - List of nodes from the registry with a matching session subject
1851
   * 
1852
   * @throws ServiceFailure
1853
   * @throws NotImplemented
1854
   */
1855
  protected List<Node> listNodesBySubject(Subject subject) 
1856
      throws ServiceFailure, NotImplemented {
1857
      List<Node> nodeList = new ArrayList<Node>();
1858
      
1859
      CNode cn = D1Client.getCN();
1860
      List<Node> nodes = cn.listNodes().getNodeList();
1861
      
1862
      // find the node in the node list
1863
      for ( Node node : nodes ) {
1864
          
1865
          List<Subject> nodeSubjects = node.getSubjectList();
1866
          if (nodeSubjects != null) {    
1867
	          // check if the session subject is in the node subject list
1868
	          for (Subject nodeSubject : nodeSubjects) {
1869
	              if ( nodeSubject.equals(subject) ) { // subject of session == node subject
1870
	                  nodeList.add(node);  
1871
	              }                              
1872
	          }
1873
          }
1874
      }
1875
      
1876
      return nodeList;
1877
      
1878
  }
1879

    
1880
  /**
1881
   * Archives an object, where the object is either a 
1882
   * data object or a science metadata object.
1883
   * 
1884
   * @param session - the Session object containing the credentials for the Subject
1885
   * @param pid - The object identifier to be archived
1886
   * 
1887
   * @return pid - the identifier of the object used for the archiving
1888
   * 
1889
   * @throws InvalidToken
1890
   * @throws ServiceFailure
1891
   * @throws NotAuthorized
1892
   * @throws NotFound
1893
   * @throws NotImplemented
1894
   * @throws InvalidRequest
1895
   */
1896
  public Identifier archive(Session session, Identifier pid) 
1897
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
1898

    
1899
      String localId = null;
1900
      boolean allowed = false;
1901
      String username = Constants.SUBJECT_PUBLIC;
1902
      String[] groupnames = null;
1903
      if (session == null) {
1904
      	throw new InvalidToken("1330", "No session has been provided");
1905
      } else {
1906
          username = session.getSubject().getValue();
1907
          if (session.getSubjectInfo() != null) {
1908
              List<Group> groupList = session.getSubjectInfo().getGroupList();
1909
              if (groupList != null) {
1910
                  groupnames = new String[groupList.size()];
1911
                  for (int i = 0; i < groupList.size(); i++) {
1912
                      groupnames[i] = groupList.get(i).getGroupName();
1913
                  }
1914
              }
1915
          }
1916
      }
1917

    
1918
      // do we have a valid pid?
1919
      if (pid == null || pid.getValue().trim().equals("")) {
1920
          throw new ServiceFailure("1350", "The provided identifier was invalid.");
1921
      }
1922
      
1923
      String serviceFailureCode = "1350";
1924
      Identifier sid = getPIDForSID(pid, serviceFailureCode);
1925
      if(sid != null) {
1926
          pid = sid;
1927
      }
1928

    
1929
      // check for the existing identifier
1930
      try {
1931
          localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1932
      } catch (McdbDocNotFoundException e) {
1933
          throw new NotFound("1340", "The object with the provided " + "identifier was not found.");
1934
      } catch (SQLException e) {
1935
          throw new ServiceFailure("1350", "The object with the provided identifier "+pid.getValue()+" couldn't be identified since "+e.getMessage());
1936
      }
1937

    
1938
      // does the subject have archive (a D1 CHANGE_PERMISSION level) privileges on the pid?
1939
      try {
1940
			allowed = isAuthorized(session, pid, Permission.CHANGE_PERMISSION);
1941
		} catch (InvalidRequest e) {
1942
          throw new ServiceFailure("1350", e.getDescription());
1943
		}
1944
          
1945

    
1946
      if (allowed) {
1947
          try {
1948
              // archive the document
1949
              DocumentImpl.delete(localId, null, null, null, false);
1950
              EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, Event.DELETE.xmlValue());
1951

    
1952
              // archive it
1953
              SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1954
              sysMeta.setArchived(true);
1955
              sysMeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1956
              HazelcastService.getInstance().getSystemMetadataMap().put(pid, sysMeta);
1957
              // submit for indexing
1958
              // DocumentImpl call above should do this.
1959
              // see: https://projects.ecoinformatics.org/ecoinfo/issues/6030
1960
              //HazelcastService.getInstance().getIndexQueue().add(sysMeta);
1961
              
1962
          } catch (McdbDocNotFoundException e) {
1963
              throw new NotFound("1340", "The provided identifier was invalid.");
1964

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

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

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

    
1975
      } else {
1976
          throw new NotAuthorized("1320", "The provided identity does not have " + "permission to archive the object on the Node.");
1977
      }
1978

    
1979
      return pid;
1980
  }
1981
  
1982
  
1983
  /**
1984
   * A utility method for v1 api to check the specified identifier exists as a pid
1985
   * @param identifier  the specified identifier
1986
   * @param serviceFailureCode  the detail error code for the service failure exception
1987
   * @param noFoundCode  the detail error code for the not found exception
1988
   * @throws ServiceFailure
1989
   * @throws NotFound
1990
   */
1991
  public void checkV1SystemMetaPidExist(Identifier identifier, String serviceFailureCode, String serviceFailureMessage,  
1992
          String noFoundCode, String notFoundMessage) throws ServiceFailure, NotFound {
1993
      boolean exists = false;
1994
      try {
1995
          exists = IdentifierManager.getInstance().systemMetadataPIDExists(identifier);
1996
      } catch (SQLException e) {
1997
          throw new ServiceFailure(serviceFailureCode, serviceFailureMessage+" since "+e.getMessage());
1998
      }
1999
      if(!exists) {
2000
         //the v1 method only handles a pid. so it should throw a not-found exception.
2001
          // check if the pid was deleted.
2002
          try {
2003
              String localId = IdentifierManager.getInstance().getLocalId(identifier.getValue());
2004
              if(EventLog.getInstance().isDeleted(localId)) {
2005
                  notFoundMessage=notFoundMessage+" "+DELETEDMESSAGE;
2006
              } 
2007
            } catch (Exception e) {
2008
              logMetacat.info("Couldn't determine if the not-found identifier "+identifier.getValue()+" was deleted since "+e.getMessage());
2009
            }
2010
            throw new NotFound(noFoundCode, notFoundMessage);
2011
      }
2012
  }
2013
  
2014
  /**
2015
   * Utility method to get the PID for an SID. If the specified identifier is not an SID
2016
   * , null will be returned.
2017
   * @param sid  the specified sid
2018
   * @param serviceFailureCode  the detail error code for the service failure exception
2019
   * @return the pid for the sid. If the specified identifier is not an SID, null will be returned.
2020
   * @throws ServiceFailure
2021
   */
2022
  protected Identifier getPIDForSID(Identifier sid, String serviceFailureCode) throws ServiceFailure {
2023
      Identifier id = null;
2024
      String serviceFailureMessage = "The PID "+" couldn't be identified for the sid " + sid.getValue();
2025
      try {
2026
          //determine if the given pid is a sid or not.
2027
          if(IdentifierManager.getInstance().systemMetadataSIDExists(sid)) {
2028
              try {
2029
                  //set the header pid for the sid if the identifier is a sid.
2030
                  id = IdentifierManager.getInstance().getHeadPID(sid);
2031
              } catch (SQLException sqle) {
2032
                  throw new ServiceFailure(serviceFailureCode, serviceFailureMessage+" since "+sqle.getMessage());
2033
              }
2034
              
2035
          }
2036
      } catch (SQLException e) {
2037
          throw new ServiceFailure(serviceFailureCode, serviceFailureMessage + " since "+e.getMessage());
2038
      }
2039
      return id;
2040
  }
2041

    
2042
  /*
2043
   * Determine if the sid is legitimate in CN.create and CN.registerSystemMetadata methods. It also is used as a part of rules of the updateSystemMetadata method. Here are the rules:
2044
   * A. If the sysmeta doesn't have an SID, nothing needs to be checked for the SID.
2045
   * B. If the sysmeta does have an SID, it may be an identifier which doesn't exist in the system.
2046
   * C. If the sysmeta does have an SID and it exists as an SID in the system, those scenarios are acceptable:
2047
   *    i. The sysmeta has an obsoletes field, the SID has the same value as the SID of the system metadata of the obsoleting pid.
2048
   *    ii. The sysmeta has an obsoletedBy field, the SID has the same value as the SID of the system metadata of the obsoletedBy pid. 
2049
   */
2050
  protected boolean checkSidInModifyingSystemMetadata(SystemMetadata sysmeta, String invalidSystemMetadataCode, String serviceFailureCode) throws InvalidSystemMetadata, ServiceFailure{
2051
      boolean pass = false;
2052
      if(sysmeta == null) {
2053
          throw new InvalidSystemMetadata(invalidSystemMetadataCode, "The system metadata is null in the request.");
2054
      }
2055
      Identifier sid = sysmeta.getSeriesId();
2056
      if(sid != null) {
2057
          // the series id exists
2058
          if (!isValidIdentifier(sid)) {
2059
              throw new InvalidSystemMetadata(invalidSystemMetadataCode, "The series id in the system metadata is invalid in the request.");
2060
          }
2061
          Identifier pid = sysmeta.getIdentifier();
2062
          if (!isValidIdentifier(pid)) {
2063
              throw new InvalidSystemMetadata(invalidSystemMetadataCode, "The pid in the system metadata is invalid in the request.");
2064
          }
2065
          //the series id equals the pid (new pid hasn't been registered in the system, so IdentifierManager.getInstance().identifierExists method can't exclude this scenario )
2066
          if(sid.getValue().equals(pid.getValue())) {
2067
              throw new InvalidSystemMetadata(invalidSystemMetadataCode, "The series id "+sid.getValue()+" in the system metadata shouldn't have the same value of the pid.");
2068
          }
2069
          try {
2070
              if (IdentifierManager.getInstance().identifierExists(sid.getValue())) {
2071
                  //the sid exists in system
2072
                  if(sysmeta.getObsoletes() != null) {
2073
                      SystemMetadata obsoletesSysmeta = HazelcastService.getInstance().getSystemMetadataMap().get(sysmeta.getObsoletes());
2074
                      if(obsoletesSysmeta != null) {
2075
                          Identifier obsoletesSid = obsoletesSysmeta.getSeriesId();
2076
                          if(obsoletesSid != null && obsoletesSid.getValue() != null && !obsoletesSid.getValue().trim().equals("")) {
2077
                              if(sid.getValue().equals(obsoletesSid.getValue())) {
2078
                                  pass = true;// the i of rule C
2079
                              }
2080
                          }
2081
                      } else {
2082
                           logMetacat.warn("D1NodeService.checkSidInModifyingSystemMetacat - Can't find the system metadata for the pid "+sysmeta.getObsoletes().getValue()+
2083
                                                                         " which is the value of the obsoletes. So we can't check if the sid " +sid.getValue()+" is legitimate ");
2084
                      }
2085
                  }
2086
                  if(!pass) {
2087
                      // the sid doesn't match the sid of the obsoleting identifier. So we check the obsoletedBy
2088
                      if(sysmeta.getObsoletedBy() != null) {
2089
                          SystemMetadata obsoletedBySysmeta = HazelcastService.getInstance().getSystemMetadataMap().get(sysmeta.getObsoletedBy());
2090
                          if(obsoletedBySysmeta != null) {
2091
                              Identifier obsoletedBySid = obsoletedBySysmeta.getSeriesId();
2092
                              if(obsoletedBySid != null && obsoletedBySid.getValue() != null && !obsoletedBySid.getValue().trim().equals("")) {
2093
                                  if(sid.getValue().equals(obsoletedBySid.getValue())) {
2094
                                      pass = true;// the ii of the rule C
2095
                                  }
2096
                              }
2097
                          } else {
2098
                              logMetacat.warn("D1NodeService.checkSidInModifyingSystemMetacat - Can't find the system metadata for the pid "+sysmeta.getObsoletes().getValue() 
2099
                                                                            +" which is the value of the obsoletedBy. So we can't check if the sid "+sid.getValue()+" is legitimate.");
2100
                          }
2101
                      }
2102
                  }
2103
                  if(!pass) {
2104
                      throw new InvalidSystemMetadata(invalidSystemMetadataCode, "The series id "+sid.getValue()+
2105
                              " in the system metadata exists in the system. And it doesn't match either previous object's sid or the next object's sid.");
2106
                  }
2107
              } else {
2108
                  pass = true; //Rule B
2109
              }
2110
          } catch (SQLException e) {
2111
              throw new ServiceFailure(serviceFailureCode, "Can't determine if the sid in the system metadata is unique or not since "+e.getMessage());
2112
          }
2113
          
2114
      } else {
2115
          //no sid. Rule A.
2116
          pass = true;
2117
      }
2118
      return pass;
2119
      
2120
  }
2121
  
2122
  //@Override
2123
  public OptionList listViews(Session arg0) throws InvalidToken,
2124
          ServiceFailure, NotAuthorized, InvalidRequest, NotImplemented {
2125
      OptionList views = new OptionList();
2126
      views.setKey("views");
2127
      views.setDescription("List of views for objects on the node");
2128
      Vector<String> skinNames = null;
2129
      try {
2130
          skinNames = SkinUtil.getSkinNames();
2131
      } catch (PropertyNotFoundException e) {
2132
          throw new ServiceFailure("2841", e.getMessage());
2133
      }
2134
      for (String skinName: skinNames) {
2135
          views.addOption(skinName);
2136
      }
2137
      return views;
2138
  }
2139
  
2140
  public OptionList listViews() throws InvalidToken,
2141
  ServiceFailure, NotAuthorized, InvalidRequest, NotImplemented { 
2142
      return listViews(null);
2143
  }
2144

    
2145
  //@Override
2146
  public InputStream view(Session session, String format, Identifier pid)
2147
          throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest,
2148
          NotImplemented, NotFound {
2149
      InputStream resultInputStream = null;
2150
      
2151
      String serviceFailureCode = "2831";
2152
      Identifier sid = getPIDForSID(pid, serviceFailureCode);
2153
      if(sid != null) {
2154
          pid = sid;
2155
      }
2156
      
2157
      SystemMetadata sysMeta = this.getSystemMetadata(session, pid);
2158
      InputStream object = this.get(session, pid);
2159

    
2160
      try {
2161
          // can only transform metadata, really
2162
          ObjectFormat objectFormat = ObjectFormatCache.getInstance().getFormat(sysMeta.getFormatId());
2163
          if (objectFormat.getFormatType().equals("METADATA")) {
2164
              // transform
2165
              DBTransform transformer = new DBTransform();
2166
              String documentContent = IOUtils.toString(object, "UTF-8");
2167
              String sourceType = objectFormat.getFormatId().getValue();
2168
              String targetType = "-//W3C//HTML//EN";
2169
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
2170
              Writer writer = new OutputStreamWriter(baos , "UTF-8");
2171
              // TODO: include more params?
2172
              Hashtable<String, String[]> params = new Hashtable<String, String[]>();
2173
              String localId = null;
2174
              try {
2175
                  localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
2176
              } catch (McdbDocNotFoundException e) {
2177
                  throw new NotFound("1020", e.getMessage());
2178
              }
2179
              params.put("qformat", new String[] {format});               
2180
              params.put("docid", new String[] {localId});
2181
              params.put("pid", new String[] {pid.getValue()});
2182
              transformer.transformXMLDocument(
2183
                      documentContent , 
2184
                      sourceType, 
2185
                      targetType , 
2186
                      format, 
2187
                      writer, 
2188
                      params, 
2189
                      null //sessionid
2190
                      );
2191
              
2192
              // finally, get the HTML back
2193
              resultInputStream = new ContentTypeByteArrayInputStream(baos.toByteArray());
2194
              ((ContentTypeByteArrayInputStream) resultInputStream).setContentType("text/html");
2195
  
2196
          } else {
2197
              // just return the raw bytes
2198
              resultInputStream = object;
2199
          }
2200
      } catch (IOException e) {
2201
          // report as service failure
2202
          ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2203
          sf.initCause(e);
2204
          throw sf;
2205
      } catch (PropertyNotFoundException e) {
2206
          // report as service failure
2207
          ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2208
          sf.initCause(e);
2209
          throw sf;
2210
      } catch (SQLException e) {
2211
          // report as service failure
2212
          ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2213
          sf.initCause(e);
2214
          throw sf;
2215
      } catch (ClassNotFoundException e) {
2216
          // report as service failure
2217
          ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2218
          sf.initCause(e);
2219
          throw sf;
2220
      }
2221
      
2222
      return resultInputStream;
2223
  } 
2224
  
2225
  /*
2226
   * Determine if the given identifier exists in the obsoletes field in the system metadata table.
2227
   * If the return value is not null, the given identifier exists in the given cloumn. The return value is 
2228
   * the guid of the first row.
2229
   */
2230
  protected String existsInObsoletes(Identifier id) throws InvalidRequest, ServiceFailure{
2231
      String guid = existsInFields("obsoletes", id);
2232
      return guid;
2233
  }
2234
  
2235
  /*
2236
   * Determine if the given identifier exists in the obsoletes field in the system metadata table.
2237
   * If the return value is not null, the given identifier exists in the given cloumn. The return value is 
2238
   * the guid of the first row.
2239
   */
2240
  protected String existsInObsoletedBy(Identifier id) throws InvalidRequest, ServiceFailure{
2241
      String guid = existsInFields("obsoleted_by", id);
2242
      return guid;
2243
  }
2244

    
2245
  /*
2246
   * Determine if the given identifier exists in the given column in the system metadata table. 
2247
   * If the return value is not null, the given identifier exists in the given cloumn. The return value is 
2248
   * the guid of the first row.
2249
   */
2250
  private String existsInFields(String column, Identifier id) throws InvalidRequest, ServiceFailure {
2251
      String guid = null;
2252
      if(id == null ) {
2253
          throw new InvalidRequest("4863", "The given identifier is null and we can't determine if the guid exists in the field "+column+" in the systemmetadata table");
2254
      }
2255
      String sql = "SELECT guid FROM systemmetadata WHERE "+column+" = ?";
2256
      int serialNumber = -1;
2257
      DBConnection dbConn = null;
2258
      PreparedStatement stmt = null;
2259
      ResultSet result = null;
2260
      try {
2261
          dbConn = 
2262
                  DBConnectionPool.getDBConnection("D1NodeService.existsInFields");
2263
          serialNumber = dbConn.getCheckOutSerialNumber();
2264
          stmt = dbConn.prepareStatement(sql);
2265
          stmt.setString(1, id.getValue());
2266
          result = stmt.executeQuery();
2267
          if(result.next()) {
2268
              guid = result.getString(1);
2269
          }
2270
          stmt.close();
2271
      } catch (SQLException e) {
2272
          e.printStackTrace();
2273
          throw new ServiceFailure("4862","We can't determine if the id "+id.getValue()+" exists in field "+column+" in the systemmetadata table since "+e.getMessage());
2274
      } finally {
2275
          // Return database connection to the pool
2276
          DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2277
          if(stmt != null) {
2278
              try {
2279
                  stmt.close();
2280
              } catch (SQLException e) {
2281
                  logMetacat.warn("We can close the PreparedStatment in D1NodeService.existsInFields since "+e.getMessage());
2282
              }
2283
          }
2284
          
2285
      }
2286
      return guid;
2287
      
2288
  }
2289
}
(2-2/8)