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:  $'
7
 *     '$Date:  $'
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.ByteArrayInputStream;
27
import java.io.ByteArrayOutputStream;
28
import java.io.File;
29
import java.io.FileInputStream;
30
import java.io.FileOutputStream;
31
import java.io.IOException;
32
import java.io.InputStream;
33
import java.io.InputStreamReader;
34
import java.io.OutputStreamWriter;
35
import java.io.UnsupportedEncodingException;
36
import java.io.Writer;
37
import java.math.BigInteger;
38
import java.net.URISyntaxException;
39
import java.security.NoSuchAlgorithmException;
40
import java.sql.SQLException;
41
import java.util.ArrayList;
42
import java.util.Calendar;
43
import java.util.Date;
44
import java.util.HashMap;
45
import java.util.HashSet;
46
import java.util.Hashtable;
47
import java.util.List;
48
import java.util.Map;
49
import java.util.Set;
50
import java.util.Timer;
51
import java.util.UUID;
52
import java.util.Vector;
53

    
54
import javax.servlet.http.HttpServletRequest;
55

    
56
import org.apache.commons.io.IOUtils;
57
import org.apache.log4j.Logger;
58
import org.dataone.client.v2.CNode;
59
import org.dataone.client.v2.itk.D1Client;
60
import org.dataone.client.v2.MNode;
61
import org.dataone.client.v2.formats.ObjectFormatCache;
62
import org.dataone.client.auth.CertificateManager;
63
import org.dataone.client.v2.formats.ObjectFormatInfo;
64
import org.dataone.configuration.Settings;
65
import org.dataone.ore.ResourceMapFactory;
66
import org.dataone.service.exceptions.BaseException;
67
import org.dataone.service.exceptions.IdentifierNotUnique;
68
import org.dataone.service.exceptions.InsufficientResources;
69
import org.dataone.service.exceptions.InvalidRequest;
70
import org.dataone.service.exceptions.InvalidSystemMetadata;
71
import org.dataone.service.exceptions.InvalidToken;
72
import org.dataone.service.exceptions.NotAuthorized;
73
import org.dataone.service.exceptions.NotFound;
74
import org.dataone.service.exceptions.NotImplemented;
75
import org.dataone.service.exceptions.ServiceFailure;
76
import org.dataone.service.exceptions.SynchronizationFailed;
77
import org.dataone.service.exceptions.UnsupportedType;
78
import org.dataone.service.mn.tier1.v2.MNCore;
79
import org.dataone.service.mn.tier1.v2.MNRead;
80
import org.dataone.service.mn.tier2.v2.MNAuthorization;
81
import org.dataone.service.mn.tier3.v2.MNStorage;
82
import org.dataone.service.mn.tier4.v2.MNReplication;
83
import org.dataone.service.mn.v2.MNPackage;
84
import org.dataone.service.mn.v2.MNQuery;
85
import org.dataone.service.mn.v2.MNView;
86
import org.dataone.service.types.v1.AccessRule;
87
import org.dataone.service.types.v1.Checksum;
88
import org.dataone.service.types.v1.DescribeResponse;
89
import org.dataone.service.types.v1.Event;
90
import org.dataone.service.types.v1.Identifier;
91
import org.dataone.service.types.v2.Log;
92
import org.dataone.service.types.v2.LogEntry;
93
import org.dataone.service.types.v2.OptionList;
94
import org.dataone.service.types.v1.MonitorInfo;
95
import org.dataone.service.types.v1.MonitorList;
96
import org.dataone.service.types.v2.Node;
97
import org.dataone.service.types.v2.NodeList;
98
import org.dataone.service.types.v1.NodeReference;
99
import org.dataone.service.types.v1.NodeState;
100
import org.dataone.service.types.v1.NodeType;
101
import org.dataone.service.types.v2.ObjectFormat;
102
import org.dataone.service.types.v1.ObjectFormatIdentifier;
103
import org.dataone.service.types.v1.ObjectList;
104
import org.dataone.service.types.v1.Permission;
105
import org.dataone.service.types.v1.Ping;
106
import org.dataone.service.types.v1.Replica;
107
import org.dataone.service.types.v1.ReplicationStatus;
108
import org.dataone.service.types.v1.Schedule;
109
import org.dataone.service.types.v1.Service;
110
import org.dataone.service.types.v1.Services;
111
import org.dataone.service.types.v1.Session;
112
import org.dataone.service.types.v1.Subject;
113
import org.dataone.service.types.v1.Synchronization;
114
import org.dataone.service.types.v2.SystemMetadata;
115
import org.dataone.service.types.v1.util.AuthUtils;
116
import org.dataone.service.types.v1.util.ChecksumUtil;
117
import org.dataone.service.types.v1_1.QueryEngineDescription;
118
import org.dataone.service.types.v1_1.QueryEngineList;
119
import org.dataone.service.types.v1_1.QueryField;
120
import org.dataone.service.util.Constants;
121
import org.dataone.service.util.TypeMarshaller;
122
import org.dspace.foresite.OREException;
123
import org.dspace.foresite.OREParserException;
124
import org.dspace.foresite.ORESerialiserException;
125
import org.dspace.foresite.ResourceMap;
126
import org.ecoinformatics.datamanager.parser.DataPackage;
127
import org.ecoinformatics.datamanager.parser.Entity;
128
import org.ecoinformatics.datamanager.parser.generic.DataPackageParserInterface;
129
import org.ecoinformatics.datamanager.parser.generic.Eml200DataPackageParser;
130

    
131
import edu.ucsb.nceas.ezid.EZIDException;
132
import edu.ucsb.nceas.metacat.DBQuery;
133
import edu.ucsb.nceas.metacat.DBTransform;
134
import edu.ucsb.nceas.metacat.EventLog;
135
import edu.ucsb.nceas.metacat.IdentifierManager;
136
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
137
import edu.ucsb.nceas.metacat.MetaCatServlet;
138
import edu.ucsb.nceas.metacat.MetacatHandler;
139
import edu.ucsb.nceas.metacat.common.query.EnabledQueryEngines;
140
import edu.ucsb.nceas.metacat.common.query.stream.ContentTypeByteArrayInputStream;
141
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService;
142
import edu.ucsb.nceas.metacat.index.MetacatSolrEngineDescriptionHandler;
143
import edu.ucsb.nceas.metacat.index.MetacatSolrIndex;
144
import edu.ucsb.nceas.metacat.properties.PropertyService;
145
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
146
import edu.ucsb.nceas.metacat.util.DeleteOnCloseFileInputStream;
147
import edu.ucsb.nceas.metacat.util.DocumentUtil;
148
import edu.ucsb.nceas.metacat.util.SkinUtil;
149
import edu.ucsb.nceas.metacat.util.SystemUtil;
150
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
151
import edu.ucsb.nceas.utilities.XMLUtilities;
152
import edu.ucsb.nceas.utilities.export.HtmlToPdf;
153
import gov.loc.repository.bagit.Bag;
154
import gov.loc.repository.bagit.BagFactory;
155
import gov.loc.repository.bagit.writer.impl.ZipWriter;
156

    
157
/**
158
 * Represents Metacat's implementation of the DataONE Member Node 
159
 * service API. Methods implement the various MN* interfaces, and methods common
160
 * to both Member Node and Coordinating Node interfaces are found in the
161
 * D1NodeService base class.
162
 * 
163
 * Implements:
164
 * MNCore.ping()
165
 * MNCore.getLogRecords()
166
 * MNCore.getObjectStatistics()
167
 * MNCore.getOperationStatistics()
168
 * MNCore.getStatus()
169
 * MNCore.getCapabilities()
170
 * MNRead.get()
171
 * MNRead.getSystemMetadata()
172
 * MNRead.describe()
173
 * MNRead.getChecksum()
174
 * MNRead.listObjects()
175
 * MNRead.synchronizationFailed()
176
 * MNAuthorization.isAuthorized()
177
 * MNAuthorization.setAccessPolicy()
178
 * MNStorage.create()
179
 * MNStorage.update()
180
 * MNStorage.delete()
181
 * MNStorage.updateSystemMetadata()
182
 * MNReplication.replicate()
183
 * 
184
 */
185
public class MNodeService extends D1NodeService 
186
    implements MNAuthorization, MNCore, MNRead, MNReplication, MNStorage, MNQuery, MNView, MNPackage {
187

    
188
    //private static final String PATHQUERY = "pathquery";
189
	public static final String UUID_SCHEME = "UUID";
190
	public static final String DOI_SCHEME = "DOI";
191
	private static final String UUID_PREFIX = "urn:uuid:";
192

    
193
	/* the logger instance */
194
    private Logger logMetacat = null;
195
    
196
    /* A reference to a remote Memeber Node */
197
    //private MNode mn;
198
    
199
    /* A reference to a Coordinating Node */
200
    private CNode cn;
201

    
202

    
203
    /**
204
     * Singleton accessor to get an instance of MNodeService.
205
     * 
206
     * @return instance - the instance of MNodeService
207
     */
208
    public static MNodeService getInstance(HttpServletRequest request) {
209
        return new MNodeService(request);
210
    }
211

    
212
    /**
213
     * Constructor, private for singleton access
214
     */
215
    private MNodeService(HttpServletRequest request) {
216
        super(request);
217
        logMetacat = Logger.getLogger(MNodeService.class);
218
        
219
        // set the Member Node certificate file location
220
        CertificateManager.getInstance().setCertificateLocation(Settings.getConfiguration().getString("D1Client.certificate.file"));
221
    }
222

    
223
    /**
224
     * Deletes an object from the Member Node, where the object is either a 
225
     * data object or a science metadata object.
226
     * 
227
     * @param session - the Session object containing the credentials for the Subject
228
     * @param pid - The object identifier to be deleted
229
     * 
230
     * @return pid - the identifier of the object used for the deletion
231
     * 
232
     * @throws InvalidToken
233
     * @throws ServiceFailure
234
     * @throws NotAuthorized
235
     * @throws NotFound
236
     * @throws NotImplemented
237
     * @throws InvalidRequest
238
     */
239
    @Override
240
    public Identifier delete(Session session, Identifier pid) 
241
        throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
242

    
243
    	// only admin of  the MN or the CN is allowed a full delete
244
        boolean allowed = false;
245
        allowed = isAdminAuthorized(session);
246
        
247
        String serviceFailureCode = "2902";
248
        Identifier sid = getPIDForSID(pid, serviceFailureCode);
249
        if(sid != null) {
250
            pid = sid;
251
        }
252
        
253
        //check if it is the authoritative member node
254
        if(!allowed) {
255
            allowed = isAuthoritativeMNodeAdmin(session, pid);
256
        }
257
        
258
        if (!allowed) { 
259
            throw new NotAuthorized("1320", "The provided identity does not have " + "permission to delete objects on the Node.");
260
        }
261
    	
262
    	// defer to superclass implementation
263
        return super.delete(session, pid);
264
    }
265

    
266
    /**
267
     * Updates an existing object by creating a new object identified by 
268
     * newPid on the Member Node which explicitly obsoletes the object 
269
     * identified by pid through appropriate changes to the SystemMetadata 
270
     * of pid and newPid
271
     * 
272
     * @param session - the Session object containing the credentials for the Subject
273
     * @param pid - The identifier of the object to be updated
274
     * @param object - the new object bytes
275
     * @param sysmeta - the new system metadata describing the object
276
     * 
277
     * @return newPid - the identifier of the new object
278
     * 
279
     * @throws InvalidToken
280
     * @throws ServiceFailure
281
     * @throws NotAuthorized
282
     * @throws NotFound
283
     * @throws NotImplemented
284
     * @throws IdentifierNotUnique
285
     * @throws UnsupportedType
286
     * @throws InsufficientResources
287
     * @throws InvalidSystemMetadata
288
     * @throws InvalidRequest
289
     */
290
    @Override
291
    public Identifier update(Session session, Identifier pid, InputStream object, 
292
        Identifier newPid, SystemMetadata sysmeta) 
293
        throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, 
294
        UnsupportedType, InsufficientResources, NotFound, 
295
        InvalidSystemMetadata, NotImplemented, InvalidRequest {
296

    
297
        //transform a sid to a pid if it is applicable
298
        String serviceFailureCode = "1310";
299
        Identifier sid = getPIDForSID(pid, serviceFailureCode);
300
        if(sid != null) {
301
            pid = sid;
302
        }
303
        
304
        String localId = null;
305
        boolean allowed = false;
306
        boolean isScienceMetadata = false;
307
        
308
        if (session == null) {
309
        	throw new InvalidToken("1210", "No session has been provided");
310
        }
311
        Subject subject = session.getSubject();
312

    
313
        // verify the pid is valid format
314
        if (!isValidIdentifier(pid)) {
315
        	throw new InvalidRequest("1202", "The provided identifier is invalid.");
316
        }
317
        
318
        // verify the new pid is valid format
319
        if (!isValidIdentifier(newPid)) {
320
            throw new InvalidRequest("1202", "The provided identifier is invalid.");
321
        }
322
        
323
        // make sure that the newPid doesn't exists
324
        boolean idExists = true;
325
        try {
326
            idExists = IdentifierManager.getInstance().identifierExists(newPid.getValue());
327
        } catch (SQLException e) {
328
            throw new ServiceFailure("1310", 
329
                                    "The requested identifier " + newPid.getValue() +
330
                                    " couldn't be determined if it is unique since : "+e.getMessage());
331
        }
332
        if (idExists) {
333
                throw new IdentifierNotUnique("1220", 
334
                          "The requested identifier " + newPid.getValue() +
335
                          " is already used by another object and" +
336
                          "therefore can not be used for this object. Clients should choose" +
337
                          "a new identifier that is unique and retry the operation or " +
338
                          "use CN.reserveIdentifier() to reserve one.");
339
            
340
        }
341
        
342
       
343

    
344
        // check for the existing identifier
345
        try {
346
            localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
347
            
348
        } catch (McdbDocNotFoundException e) {
349
            throw new InvalidRequest("1202", "The object with the provided " + 
350
                "identifier was not found.");
351
            
352
        } catch (SQLException ee) {
353
            throw new ServiceFailure("1310", "The object with the provided " + 
354
                    "identifier "+pid.getValue()+" can't be identified since - "+ee.getMessage());
355
        }
356
        
357
        // set the originating node
358
        NodeReference originMemberNode = this.getCapabilities().getIdentifier();
359
        sysmeta.setOriginMemberNode(originMemberNode);
360
        
361
        // set the submitter to match the certificate
362
        sysmeta.setSubmitter(subject);
363
        // set the dates
364
        Date now = Calendar.getInstance().getTime();
365
        sysmeta.setDateSysMetadataModified(now);
366
        sysmeta.setDateUploaded(now);
367
        
368
        // make sure serial version is set to something
369
        BigInteger serialVersion = sysmeta.getSerialVersion();
370
        if (serialVersion == null) {
371
        	sysmeta.setSerialVersion(BigInteger.ZERO);
372
        }
373

    
374
        // does the subject have WRITE ( == update) priveleges on the pid?
375
        //allowed = isAuthorized(session, pid, Permission.WRITE);
376
        //CN having the permission is allowed; user with the write permission and calling on the authoritative node is allowed.
377
        allowed = allowUpdating(session, pid, Permission.WRITE);
378
        if (allowed) {
379
        	
380
        	// check quality of SM
381
        	if (sysmeta.getObsoletedBy() != null) {
382
        		throw new InvalidSystemMetadata("1300", "Cannot include obsoletedBy when updating object");
383
        	}
384
        	if (sysmeta.getObsoletes() != null && !sysmeta.getObsoletes().getValue().equals(pid.getValue())) {
385
        		throw new InvalidSystemMetadata("1300", "The identifier provided in obsoletes does not match old Identifier");
386
        	}
387

    
388
            // get the existing system metadata for the object
389
            SystemMetadata existingSysMeta = getSystemMetadata(session, pid);
390
            //System.out.println("the archive is "+existingSysMeta.getArchived());
391
            //Base on documentation, we can't update an archived object:
392
            //The update operation MUST fail with Exceptions.InvalidRequest on objects that have the Types.SystemMetadata.archived property set to true.
393
            if(existingSysMeta.getArchived() != null && existingSysMeta.getArchived()) {
394
                throw new InvalidRequest("1202","An archived object"+pid.getValue()+" can't be updated");
395
            }
396

    
397
            // check for previous update
398
            // see: https://redmine.dataone.org/issues/3336
399
            Identifier existingObsoletedBy = existingSysMeta.getObsoletedBy();
400
            if (existingObsoletedBy != null) {
401
            	throw new InvalidRequest("1202", 
402
            			"The previous identifier has already been made obsolete by: " + existingObsoletedBy.getValue());
403
            }
404
            //check the sid in the system metadata. If it exists, it should be non-exist or match the old sid in the previous system metadata.
405
            Identifier sidInSys = sysmeta.getSeriesId();
406
            if(sidInSys != null) {
407
                if (!isValidIdentifier(sidInSys)) {
408
                    throw new InvalidSystemMetadata("1300", "The provided series id in the system metadata is invalid.");
409
                }
410
                Identifier previousSid = existingSysMeta.getSeriesId();
411
                if(previousSid != null) {
412
                    // there is a previous sid, if the new sid doesn't match it, the new sid should be non-existing.
413
                    if(!sidInSys.getValue().equals(previousSid.getValue())) {
414
                        try {
415
                            idExists = IdentifierManager.getInstance().identifierExists(sidInSys.getValue());
416
                        } catch (SQLException e) {
417
                            throw new ServiceFailure("1310", 
418
                                                    "The requested identifier " + sidInSys.getValue() +
419
                                                    " couldn't be determined if it is unique since : "+e.getMessage());
420
                        }
421
                        if(idExists) {
422
                            throw new InvalidSystemMetadata("1300", "The series id "+sidInSys.getValue()+" in the system metadata doesn't match the previous series id "
423
                                                            +previousSid.getValue()+", so it should NOT exist. However, it was used by another object.");
424
                        }
425
                    }
426
                } else {
427
                    // there is no previous sid, the new sid should be non-existing.
428
                    try {
429
                        idExists = IdentifierManager.getInstance().identifierExists(sidInSys.getValue());
430
                    } catch (SQLException e) {
431
                        throw new ServiceFailure("1310", 
432
                                                "The requested identifier " + sidInSys.getValue() +
433
                                                " couldn't be determined if it is unique since : "+e.getMessage());
434
                    }
435
                    if(idExists) {
436
                        throw new InvalidSystemMetadata("1300", "The series id "+sidInSys.getValue()+" in the system metadata should NOT exist since the previous series id is null."
437
                                                        +"However, it was used by another object.");
438
                    }
439
                }
440
                //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)
441
                if(sidInSys.getValue().equals(newPid.getValue())) {
442
                    throw new InvalidSystemMetadata("1300", "The series id "+sidInSys.getValue()+" in the system metadata shouldn't have the same value of the pid.");
443
                }
444
            }
445

    
446
            isScienceMetadata = isScienceMetadata(sysmeta);
447

    
448
            // do we have XML metadata or a data object?
449
            if (isScienceMetadata) {
450

    
451
                // update the science metadata XML document
452
                // TODO: handle non-XML metadata/data documents (like netCDF)
453
                // TODO: don't put objects into memory using stream to string
454
                //String objectAsXML = "";
455
                try {
456
                    //objectAsXML = IOUtils.toString(object, "UTF-8");
457
                    // give the old pid so we can calculate the new local id 
458
                    localId = insertOrUpdateDocument(object, "UTF-8", pid, session, "update");
459
                    // register the newPid and the generated localId
460
                    if (newPid != null) {
461
                        IdentifierManager.getInstance().createMapping(newPid.getValue(), localId);
462

    
463
                    }
464

    
465
                } catch (IOException e) {
466
                    String msg = "The Node is unable to create the object. " + "There was a problem converting the object to XML";
467
                    logMetacat.info(msg);
468
                    throw new ServiceFailure("1310", msg + ": " + e.getMessage());
469

    
470
                }
471

    
472
            } else {
473

    
474
                // update the data object
475
                localId = insertDataObject(object, newPid, session);
476

    
477
            }
478
            
479
            // add the newPid to the obsoletedBy list for the existing sysmeta
480
            existingSysMeta.setObsoletedBy(newPid);
481
            //increase version
482
            BigInteger current = existingSysMeta.getSerialVersion();
483
            //System.out.println("the current version is "+current);
484
            current = current.add(BigInteger.ONE);
485
            //System.out.println("the new current version is "+current);
486
            existingSysMeta.setSerialVersion(current);
487
            // then update the existing system metadata
488
            updateSystemMetadata(existingSysMeta);
489

    
490
            // prep the new system metadata, add pid to the affected lists
491
            sysmeta.setObsoletes(pid);
492
            //sysmeta.addDerivedFrom(pid);
493

    
494
            // and insert the new system metadata
495
            insertSystemMetadata(sysmeta);
496

    
497
            // log the update event
498
            EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), subject.getValue(), localId, Event.UPDATE.toString());
499
            
500
            // attempt to register the identifier - it checks if it is a doi
501
            try {
502
    			DOIService.getInstance().registerDOI(sysmeta);
503
    		} catch (Exception e) {
504
                throw new ServiceFailure("1190", "Could not register DOI: " + e.getMessage());
505
    		}
506

    
507
        } else {
508
            throw new NotAuthorized("1200", "The provided identity does not have " + "permission to UPDATE the object identified by " + pid.getValue()
509
                    + " on the Member Node.");
510
        }
511

    
512
        return newPid;
513
    }
514

    
515
    public Identifier create(Session session, Identifier pid, InputStream object, SystemMetadata sysmeta) throws InvalidToken, ServiceFailure, NotAuthorized,
516
            IdentifierNotUnique, UnsupportedType, InsufficientResources, InvalidSystemMetadata, NotImplemented, InvalidRequest {
517

    
518
        // check for null session
519
        if (session == null) {
520
          throw new InvalidToken("1110", "Session is required to WRITE to the Node.");
521
        }
522
        // verify the pid is valid format
523
        if (!isValidIdentifier(pid)) {
524
            throw new InvalidRequest("1102", "The provided identifier is invalid.");
525
        }
526
        // set the submitter to match the certificate
527
        sysmeta.setSubmitter(session.getSubject());
528
        // set the originating node
529
        NodeReference originMemberNode = this.getCapabilities().getIdentifier();
530
        sysmeta.setOriginMemberNode(originMemberNode);
531
        sysmeta.setArchived(false);
532

    
533
        // set the dates
534
        Date now = Calendar.getInstance().getTime();
535
        sysmeta.setDateSysMetadataModified(now);
536
        sysmeta.setDateUploaded(now);
537
        
538
        // set the serial version
539
        sysmeta.setSerialVersion(BigInteger.ZERO);
540

    
541
        // check that we are not attempting to subvert versioning
542
        if (sysmeta.getObsoletes() != null && sysmeta.getObsoletes().getValue() != null) {
543
            throw new InvalidSystemMetadata("1180", 
544
              "The supplied system metadata is invalid. " +
545
              "The obsoletes field cannot have a value when creating entries.");
546
        }
547
        
548
        if (sysmeta.getObsoletedBy() != null && sysmeta.getObsoletedBy().getValue() != null) {
549
            throw new InvalidSystemMetadata("1180", 
550
              "The supplied system metadata is invalid. " +
551
              "The obsoletedBy field cannot have a value when creating entries.");
552
        }
553
        
554
        // verify the sid in the system metadata
555
        Identifier sid = sysmeta.getSeriesId();
556
        boolean idExists = false;
557
        if(sid != null) {
558
            if (!isValidIdentifier(sid)) {
559
                throw new InvalidSystemMetadata("1180", "The provided series id is invalid.");
560
            }
561
            try {
562
                idExists = IdentifierManager.getInstance().identifierExists(sid.getValue());
563
            } catch (SQLException e) {
564
                throw new ServiceFailure("1190", 
565
                                        "The series identifier " + sid.getValue() +
566
                                        " in the system metadata couldn't be determined if it is unique since : "+e.getMessage());
567
            }
568
            if (idExists) {
569
                    throw new InvalidSystemMetadata("1180", 
570
                              "The series identifier " + sid.getValue() +
571
                              " is already used by another object and" +
572
                              "therefore can not be used for this object. Clients should choose" +
573
                              "a new identifier that is unique and retry the operation or " +
574
                              "use CN.reserveIdentifier() to reserve one.");
575
                
576
            }
577
            //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 )
578
            if(sid.getValue().equals(pid.getValue())) {
579
                throw new InvalidSystemMetadata("1180", "The series id "+sid.getValue()+" in the system metadata shouldn't have the same value of the pid.");
580
            }
581
        }
582

    
583
        // call the shared impl
584
        Identifier resultPid = super.create(session, pid, object, sysmeta);
585
        
586
        // attempt to register the identifier - it checks if it is a doi
587
        try {
588
			DOIService.getInstance().registerDOI(sysmeta);
589
		} catch (Exception e) {
590
			ServiceFailure sf = new ServiceFailure("1190", "Could not register DOI: " + e.getMessage());
591
			sf.initCause(e);
592
            throw sf;
593
		}
594
        
595
        // return 
596
		return resultPid ;
597
    }
598

    
599
    /**
600
     * Called by a Coordinating Node to request that the Member Node create a 
601
     * copy of the specified object by retrieving it from another Member 
602
     * Node and storing it locally so that it can be made accessible to 
603
     * the DataONE system.
604
     * 
605
     * @param session - the Session object containing the credentials for the Subject
606
     * @param sysmeta - Copy of the CN held system metadata for the object
607
     * @param sourceNode - A reference to node from which the content should be 
608
     *                     retrieved. The reference should be resolved by 
609
     *                     checking the CN node registry.
610
     * 
611
     * @return true if the replication succeeds
612
     * 
613
     * @throws ServiceFailure
614
     * @throws NotAuthorized
615
     * @throws NotImplemented
616
     * @throws UnsupportedType
617
     * @throws InsufficientResources
618
     * @throws InvalidRequest
619
     */
620
    @Override
621
    public boolean replicate(Session session, SystemMetadata sysmeta,
622
            NodeReference sourceNode) throws NotImplemented, ServiceFailure,
623
            NotAuthorized, InvalidRequest, InsufficientResources,
624
            UnsupportedType {
625

    
626
        if (session != null && sysmeta != null && sourceNode != null) {
627
            logMetacat.info("MNodeService.replicate() called with parameters: \n" +
628
                            "\tSession.Subject      = "                           +
629
                            session.getSubject().getValue() + "\n"                +
630
                            "\tidentifier           = "                           + 
631
                            sysmeta.getIdentifier().getValue()                    +
632
                            "\n" + "\tSource NodeReference ="                     +
633
                            sourceNode.getValue());
634
        }
635
        boolean result = false;
636
        String nodeIdStr = null;
637
        NodeReference nodeId = null;
638

    
639
        // get the referenced object
640
        Identifier pid = sysmeta.getIdentifier();
641
        // verify the pid is valid format
642
        if (!isValidIdentifier(pid)) {
643
            throw new InvalidRequest("2153", "The provided identifier in the system metadata is invalid.");
644
        }
645

    
646
        // get from the membernode
647
        // TODO: switch credentials for the server retrieval?
648
        this.cn = D1Client.getCN();
649
        InputStream object = null;
650
        Session thisNodeSession = null;
651
        SystemMetadata localSystemMetadata = null;
652
        BaseException failure = null;
653
        String localId = null;
654
        
655
        // TODO: check credentials
656
        // cannot be called by public
657
        if (session == null || session.getSubject() == null) {
658
            String msg = "No session was provided to replicate identifier " +
659
            sysmeta.getIdentifier().getValue();
660
            logMetacat.error(msg);
661
            throw new NotAuthorized("2152", msg);
662
            
663
        }
664

    
665

    
666
        // get the local node id
667
        try {
668
            nodeIdStr = PropertyService.getProperty("dataone.nodeId");
669
            nodeId = new NodeReference();
670
            nodeId.setValue(nodeIdStr);
671

    
672
        } catch (PropertyNotFoundException e1) {
673
            String msg = "Couldn't get dataone.nodeId property: " + e1.getMessage();
674
            failure = new ServiceFailure("2151", msg);
675
            //setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, failure);
676
            logMetacat.error(msg);
677
            //return true;
678
            throw new ServiceFailure("2151", msg);
679

    
680
        }
681
        
682

    
683
        try {
684
            // do we already have a replica?
685
            try {
686
                localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
687
                // if we have a local id, get the local object
688
                try {
689
                    object = MetacatHandler.read(localId);
690
                } catch (Exception e) {
691
                	// NOTE: we may already know about this ID because it could be a data file described by a metadata file
692
                	// https://redmine.dataone.org/issues/2572
693
                	// TODO: fix this so that we don't prevent ourselves from getting replicas
694
                	
695
                    // let the CN know that the replication failed
696
                	logMetacat.warn("Object content not found on this node despite having localId: " + localId);
697
                	String msg = "Can't read the object bytes properly, replica is invalid.";
698
                    ServiceFailure serviceFailure = new ServiceFailure("2151", msg);
699
                    setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, serviceFailure);
700
                    logMetacat.warn(msg);
701
                    throw serviceFailure;
702
                    
703
                }
704

    
705
            } catch (McdbDocNotFoundException e) {
706
                logMetacat.info("No replica found. Continuing.");
707
                
708
            } catch (SQLException ee) {
709
                throw new ServiceFailure("2151", "Couldn't identify the local id of the object with the specified identifier "
710
                                        +pid.getValue()+" since - "+ee.getMessage());
711
            }
712
            
713
            // no local replica, get a replica
714
            if ( object == null ) {
715
                /*boolean success = true;
716
                try {
717
                    //use the v2 ping api to connect the source node
718
                    mn.ping();
719
                } catch (Exception e) {
720
                    success = false;
721
                }*/
722
                D1NodeVersionChecker checker = new D1NodeVersionChecker(sourceNode);
723
                String nodeVersion = checker.getVersion("MNRead");
724
                if(nodeVersion != null && nodeVersion.equals(D1NodeVersionChecker.V1)) {
725
                    //The source node is a v1 node, we use the v1 api
726
                    org.dataone.client.v1.MNode mNodeV1 =  org.dataone.client.v1.itk.D1Client.getMN(sourceNode);
727
                    object = mNodeV1.getReplica(thisNodeSession, pid);
728
                } else if (nodeVersion != null && nodeVersion.equals(D1NodeVersionChecker.V2)){
729
                 // session should be null to use the default certificate
730
                    // location set in the Certificate manager
731
                    MNode mn = D1Client.getMN(sourceNode);
732
                    object = mn.getReplica(thisNodeSession, pid);
733
                } else {
734
                    throw new ServiceFailure("2151", "The version of MNRead service is "+nodeVersion+" in the source node "+sourceNode.getValue()+" and it is supported. Please check the information in the cn");
735
                }
736
                
737
                logMetacat.info("MNodeService.getReplica() called for identifier "
738
                                + pid.getValue());
739

    
740
            }
741

    
742
        } catch (InvalidToken e) {            
743
            String msg = "Could not retrieve object to replicate (InvalidToken): "+ e.getMessage();
744
            failure = new ServiceFailure("2151", msg);
745
            setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, failure);
746
            logMetacat.error(msg);
747
            throw new ServiceFailure("2151", msg);
748

    
749
        } catch (NotFound e) {
750
            String msg = "Could not retrieve object to replicate (NotFound): "+ e.getMessage();
751
            failure = new ServiceFailure("2151", msg);
752
            setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, failure);
753
            logMetacat.error(msg);
754
            throw new ServiceFailure("2151", msg);
755

    
756
        } catch (NotAuthorized e) {
757
            String msg = "Could not retrieve object to replicate (NotAuthorized): "+ e.getMessage();
758
            failure = new ServiceFailure("2151", msg);
759
            setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, failure);
760
            logMetacat.error(msg);
761
            throw new ServiceFailure("2151", msg);
762
        } catch (NotImplemented e) {
763
            String msg = "Could not retrieve object to replicate (mn.getReplica NotImplemented): "+ e.getMessage();
764
            failure = new ServiceFailure("2151", msg);
765
            setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, failure);
766
            logMetacat.error(msg);
767
            throw new ServiceFailure("2151", msg);
768
        } catch (ServiceFailure e) {
769
            String msg = "Could not retrieve object to replicate (ServiceFailure): "+ e.getMessage();
770
            failure = new ServiceFailure("2151", msg);
771
            setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, failure);
772
            logMetacat.error(msg);
773
            throw new ServiceFailure("2151", msg);
774
        } catch (InsufficientResources e) {
775
            String msg = "Could not retrieve object to replicate (InsufficientResources): "+ e.getMessage();
776
            failure = new ServiceFailure("2151", msg);
777
            setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, failure);
778
            logMetacat.error(msg);
779
            throw new ServiceFailure("2151", msg);
780
        }
781

    
782
        // verify checksum on the object, if supported
783
        if (object.markSupported()) {
784
            Checksum givenChecksum = sysmeta.getChecksum();
785
            Checksum computedChecksum = null;
786
            try {
787
                computedChecksum = ChecksumUtil.checksum(object, givenChecksum.getAlgorithm());
788
                object.reset();
789

    
790
            } catch (Exception e) {
791
                String msg = "Error computing checksum on replica: " + e.getMessage();
792
                logMetacat.error(msg);
793
                ServiceFailure sf = new ServiceFailure("2151", msg);
794
                sf.initCause(e);
795
                setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, sf);
796
                throw sf;
797
            }
798
            if (!givenChecksum.getValue().equals(computedChecksum.getValue())) {
799
                logMetacat.error("Given    checksum for " + pid.getValue() + 
800
                    "is " + givenChecksum.getValue());
801
                logMetacat.error("Computed checksum for " + pid.getValue() + 
802
                    "is " + computedChecksum.getValue());
803
                String msg = "Computed checksum does not match declared checksum";
804
                failure = new ServiceFailure("2151", msg);
805
                setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, failure);
806
                throw new ServiceFailure("2151", msg);
807
            }
808
        }
809

    
810
        // add it to local store
811
        Identifier retPid;
812
        try {
813
            // skip the MN.create -- this mutates the system metadata and we don't want it to
814
            if ( localId == null ) {
815
                // TODO: this will fail if we already "know" about the identifier
816
            	// FIXME: see https://redmine.dataone.org/issues/2572
817
                retPid = super.create(session, pid, object, sysmeta);
818
                result = (retPid.getValue().equals(pid.getValue()));
819
            }
820
            
821
        } catch (Exception e) {
822
            String msg = "Could not save object to local store (" + e.getClass().getName() + "): " + e.getMessage();
823
            failure = new ServiceFailure("2151", msg);
824
            setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, failure);
825
            logMetacat.error(msg);
826
            throw new ServiceFailure("2151", msg);
827
            
828
        }
829

    
830
        // finish by setting the replication status
831
        setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.COMPLETED, null);
832
        return result;
833

    
834
    }
835
    
836
    /*
837
     * If the given node supports v2 replication.
838
     */
839
    private boolean supportV2Replication(Node node) throws InvalidRequest {
840
        return supportVersionReplication(node, "v2");
841
    }
842
    
843
    /*
844
     * If the given node support the the given version replication. Return true if it does.
845
     */
846
    private boolean supportVersionReplication(Node node, String version) throws InvalidRequest{
847
        boolean support = false;
848
        if(node == null) {
849
            throw new InvalidRequest("2153", "There is no capacity information about the node in the replicate.");
850
        } else {
851
            Services services = node.getServices();
852
            if(services == null) {
853
                throw new InvalidRequest("2153", "Can't get replica from a node which doesn't have the replicate service.");
854
            } else {
855
               List<Service> list = services.getServiceList();
856
               if(list == null) {
857
                   throw new InvalidRequest("2153", "Can't get replica from a node which doesn't have the replicate service.");
858
               } else {
859
                   for(Service service : list) {
860
                       if(service != null && service.getName() != null && service.getName().equals("MNReplication") && 
861
                               service.getVersion() != null && service.getVersion().equalsIgnoreCase(version) && service.getAvailable() == true ) {
862
                           support = true;
863
                           
864
                       }
865
                   }
866
               }
867
            }
868
        }
869
        return support;
870
    }
871

    
872
    /**
873
     * Return the object identified by the given object identifier
874
     * 
875
     * @param session - the Session object containing the credentials for the Subject
876
     * @param pid - the object identifier for the given object
877
     * 
878
     * @return inputStream - the input stream of the given object
879
     * 
880
     * @throws InvalidToken
881
     * @throws ServiceFailure
882
     * @throws NotAuthorized
883
     * @throws InvalidRequest
884
     * @throws NotImplemented
885
     */
886
    @Override
887
    public InputStream get(Session session, Identifier pid) 
888
    throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
889

    
890
        return super.get(session, pid);
891

    
892
    }
893

    
894
    /**
895
     * Returns a Checksum for the specified object using an accepted hashing algorithm
896
     * 
897
     * @param session - the Session object containing the credentials for the Subject
898
     * @param pid - the object identifier for the given object
899
     * @param algorithm -  the name of an algorithm that will be used to compute 
900
     *                     a checksum of the bytes of the object
901
     * 
902
     * @return checksum - the checksum of the given object
903
     * 
904
     * @throws InvalidToken
905
     * @throws ServiceFailure
906
     * @throws NotAuthorized
907
     * @throws NotFound
908
     * @throws InvalidRequest
909
     * @throws NotImplemented
910
     */
911
    @Override
912
    public Checksum getChecksum(Session session, Identifier pid, String algorithm) 
913
        throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
914
        InvalidRequest, NotImplemented {
915

    
916
        Checksum checksum = null;
917
        String serviceFailure = "1410";
918
        String notFound = "1420";
919
        //Checkum only handles the pid, not sid
920
        checkV1SystemMetaPidExist(pid, serviceFailure, "The checksum for the object specified by "+pid.getValue()+" couldn't be returned ",  notFound, 
921
                "The object specified by "+pid.getValue()+" does not exist at this node.");
922
        InputStream inputStream = get(session, pid);
923

    
924
        try {
925
            checksum = ChecksumUtil.checksum(inputStream, algorithm);
926

    
927
        } catch (NoSuchAlgorithmException e) {
928
            throw new ServiceFailure("1410", "The checksum for the object specified by " + pid.getValue() + "could not be returned due to an internal error: "
929
                    + e.getMessage());
930
        } catch (IOException e) {
931
            throw new ServiceFailure("1410", "The checksum for the object specified by " + pid.getValue() + "could not be returned due to an internal error: "
932
                    + e.getMessage());
933
        }
934

    
935
        if (checksum == null) {
936
            throw new ServiceFailure("1410", "The checksum for the object specified by " + pid.getValue() + "could not be returned.");
937
        }
938

    
939
        return checksum;
940
    }
941

    
942
    /**
943
     * Return the system metadata for a given object
944
     * 
945
     * @param session - the Session object containing the credentials for the Subject
946
     * @param pid - the object identifier for the given object
947
     * 
948
     * @return inputStream - the input stream of the given system metadata object
949
     * 
950
     * @throws InvalidToken
951
     * @throws ServiceFailure
952
     * @throws NotAuthorized
953
     * @throws NotFound
954
     * @throws InvalidRequest
955
     * @throws NotImplemented
956
     */
957
    @Override
958
    public SystemMetadata getSystemMetadata(Session session, Identifier pid) 
959
        throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
960
        NotImplemented {
961

    
962
        return super.getSystemMetadata(session, pid);
963
    }
964

    
965
    /**
966
     * Retrieve the list of objects present on the MN that match the calling parameters
967
     * 
968
     * @param session - the Session object containing the credentials for the Subject
969
     * @param startTime - Specifies the beginning of the time range from which 
970
     *                    to return object (>=)
971
     * @param endTime - Specifies the beginning of the time range from which 
972
     *                  to return object (>=)
973
     * @param objectFormat - Restrict results to the specified object format
974
     * @param replicaStatus - Indicates if replicated objects should be returned in the list
975
     * @param start - The zero-based index of the first value, relative to the 
976
     *                first record of the resultset that matches the parameters.
977
     * @param count - The maximum number of entries that should be returned in 
978
     *                the response. The Member Node may return less entries 
979
     *                than specified in this value.
980
     * 
981
     * @return objectList - the list of objects matching the criteria
982
     * 
983
     * @throws InvalidToken
984
     * @throws ServiceFailure
985
     * @throws NotAuthorized
986
     * @throws InvalidRequest
987
     * @throws NotImplemented
988
     */
989
    @Override
990
    public ObjectList listObjects(Session session, Date startTime, Date endTime, ObjectFormatIdentifier objectFormatId, Identifier identifier, Boolean replicaStatus, Integer start,
991
            Integer count) throws NotAuthorized, InvalidRequest, NotImplemented, ServiceFailure, InvalidToken {
992
        NodeReference nodeId = null;
993
        if(!replicaStatus) {
994
            //not include those objects whose authoritative node is not this mn
995
            nodeId = new NodeReference();
996
            try {
997
                String currentNodeId = PropertyService.getInstance().getProperty("dataone.nodeId"); // return only pids for which this mn
998
                nodeId.setValue(currentNodeId);
999
            } catch(Exception e) {
1000
                throw new ServiceFailure("1580", e.getMessage());
1001
            }
1002
        }
1003
        return super.listObjects(session, startTime, endTime, objectFormatId, identifier, nodeId, start, count);
1004
    }
1005

    
1006
    /**
1007
     * Return a description of the node's capabilities and services.
1008
     * 
1009
     * @return node - the technical capabilities of the Member Node
1010
     * 
1011
     * @throws ServiceFailure
1012
     * @throws NotAuthorized
1013
     * @throws InvalidRequest
1014
     * @throws NotImplemented
1015
     */
1016
    @Override
1017
    public Node getCapabilities() 
1018
        throws NotImplemented, ServiceFailure {
1019

    
1020
        String nodeName = null;
1021
        String nodeId = null;
1022
        String subject = null;
1023
        String contactSubject = null;
1024
        String nodeDesc = null;
1025
        String nodeTypeString = null;
1026
        NodeType nodeType = null;
1027
        List<String> mnCoreServiceVersions = null;
1028
        List<String> mnReadServiceVersions = null;
1029
        List<String> mnAuthorizationServiceVersions = null;
1030
        List<String> mnStorageServiceVersions = null;
1031
        List<String> mnReplicationServiceVersions = null;
1032

    
1033
        boolean nodeSynchronize = false;
1034
        boolean nodeReplicate = false;
1035
        List<String> mnCoreServiceAvailables = null;
1036
        List<String> mnReadServiceAvailables = null;
1037
        List<String> mnAuthorizationServiceAvailables = null;
1038
        List<String> mnStorageServiceAvailables = null;
1039
        List<String> mnReplicationServiceAvailables = null;
1040

    
1041
        try {
1042
            // get the properties of the node based on configuration information
1043
            nodeName = Settings.getConfiguration().getString("dataone.nodeName");
1044
            nodeId = Settings.getConfiguration().getString("dataone.nodeId");
1045
            subject = Settings.getConfiguration().getString("dataone.subject");
1046
            contactSubject = Settings.getConfiguration().getString("dataone.contactSubject");
1047
            nodeDesc = Settings.getConfiguration().getString("dataone.nodeDescription");
1048
            nodeTypeString = Settings.getConfiguration().getString("dataone.nodeType");
1049
            nodeType = NodeType.convert(nodeTypeString);
1050
            nodeSynchronize = new Boolean(Settings.getConfiguration().getString("dataone.nodeSynchronize")).booleanValue();
1051
            nodeReplicate = new Boolean(Settings.getConfiguration().getString("dataone.nodeReplicate")).booleanValue();
1052

    
1053
            // Set the properties of the node based on configuration information and
1054
            // calls to current status methods
1055
            String serviceName = SystemUtil.getSecureContextURL() + "/" + PropertyService.getProperty("dataone.serviceName");
1056
            Node node = new Node();
1057
            node.setBaseURL(serviceName + "/" + nodeTypeString);
1058
            node.setDescription(nodeDesc);
1059

    
1060
            // set the node's health information
1061
            node.setState(NodeState.UP);
1062
            
1063
            // set the ping response to the current value
1064
            Ping canPing = new Ping();
1065
            canPing.setSuccess(false);
1066
            try {
1067
            	Date pingDate = ping();
1068
                canPing.setSuccess(pingDate != null);
1069
            } catch (BaseException e) {
1070
                e.printStackTrace();
1071
                // guess it can't be pinged
1072
            }
1073
            
1074
            node.setPing(canPing);
1075

    
1076
            NodeReference identifier = new NodeReference();
1077
            identifier.setValue(nodeId);
1078
            node.setIdentifier(identifier);
1079
            Subject s = new Subject();
1080
            s.setValue(subject);
1081
            node.addSubject(s);
1082
            Subject contact = new Subject();
1083
            contact.setValue(contactSubject);
1084
            node.addContactSubject(contact);
1085
            node.setName(nodeName);
1086
            node.setReplicate(nodeReplicate);
1087
            node.setSynchronize(nodeSynchronize);
1088

    
1089
            // services: MNAuthorization, MNCore, MNRead, MNReplication, MNStorage
1090
            Services services = new Services();
1091

    
1092
            mnCoreServiceVersions = Settings.getConfiguration().getList("dataone.mnCore.serviceVersion");
1093
            mnCoreServiceAvailables = Settings.getConfiguration().getList("dataone.mnCore.serviceAvailable");
1094
            if(mnCoreServiceVersions != null && mnCoreServiceAvailables != null && mnCoreServiceVersions.size() == mnCoreServiceAvailables.size()) {
1095
                for(int i=0; i<mnCoreServiceVersions.size(); i++) {
1096
                    String version = mnCoreServiceVersions.get(i);
1097
                    boolean available = new Boolean(mnCoreServiceAvailables.get(i)).booleanValue();
1098
                    Service sMNCore = new Service();
1099
                    sMNCore.setName("MNCore");
1100
                    sMNCore.setVersion(version);
1101
                    sMNCore.setAvailable(available);
1102
                    services.addService(sMNCore);
1103
                }
1104
            }
1105
            
1106
            mnReadServiceVersions = Settings.getConfiguration().getList("dataone.mnRead.serviceVersion");
1107
            mnReadServiceAvailables = Settings.getConfiguration().getList("dataone.mnRead.serviceAvailable");
1108
            if(mnReadServiceVersions != null && mnReadServiceAvailables != null && mnReadServiceVersions.size()==mnReadServiceAvailables.size()) {
1109
                for(int i=0; i<mnReadServiceVersions.size(); i++) {
1110
                    String version = mnReadServiceVersions.get(i);
1111
                    boolean available = new Boolean(mnReadServiceAvailables.get(i)).booleanValue();
1112
                    Service sMNRead = new Service();
1113
                    sMNRead.setName("MNRead");
1114
                    sMNRead.setVersion(version);
1115
                    sMNRead.setAvailable(available);
1116
                    services.addService(sMNRead);
1117
                }
1118
            }
1119
           
1120
            mnAuthorizationServiceVersions = Settings.getConfiguration().getList("dataone.mnAuthorization.serviceVersion");
1121
            mnAuthorizationServiceAvailables = Settings.getConfiguration().getList("dataone.mnAuthorization.serviceAvailable");
1122
            if(mnAuthorizationServiceVersions != null && mnAuthorizationServiceAvailables != null && mnAuthorizationServiceVersions.size()==mnAuthorizationServiceAvailables.size()) {
1123
                for(int i=0; i<mnAuthorizationServiceVersions.size(); i++) {
1124
                    String version = mnAuthorizationServiceVersions.get(i);
1125
                    boolean available = new Boolean(mnAuthorizationServiceAvailables.get(i)).booleanValue();
1126
                    Service sMNAuthorization = new Service();
1127
                    sMNAuthorization.setName("MNAuthorization");
1128
                    sMNAuthorization.setVersion(version);
1129
                    sMNAuthorization.setAvailable(available);
1130
                    services.addService(sMNAuthorization);
1131
                }
1132
            }
1133
           
1134
            mnStorageServiceVersions = Settings.getConfiguration().getList("dataone.mnStorage.serviceVersion");
1135
            mnStorageServiceAvailables = Settings.getConfiguration().getList("dataone.mnStorage.serviceAvailable");
1136
            if(mnStorageServiceVersions != null && mnStorageServiceAvailables != null && mnStorageServiceVersions.size() == mnStorageServiceAvailables.size()) {
1137
                for(int i=0; i<mnStorageServiceVersions.size(); i++) {
1138
                    String version = mnStorageServiceVersions.get(i);
1139
                    boolean available = new Boolean(mnStorageServiceAvailables.get(i)).booleanValue();
1140
                    Service sMNStorage = new Service();
1141
                    sMNStorage.setName("MNStorage");
1142
                    sMNStorage.setVersion(version);
1143
                    sMNStorage.setAvailable(available);
1144
                    services.addService(sMNStorage);
1145
                }
1146
            }
1147
            
1148
            mnReplicationServiceVersions = Settings.getConfiguration().getList("dataone.mnReplication.serviceVersion");
1149
            mnReplicationServiceAvailables = Settings.getConfiguration().getList("dataone.mnReplication.serviceAvailable");
1150
            if(mnReplicationServiceVersions != null && mnReplicationServiceAvailables != null && mnReplicationServiceVersions.size() == mnReplicationServiceAvailables.size()) {
1151
                for (int i=0; i<mnReplicationServiceVersions.size(); i++) {
1152
                    String version = mnReplicationServiceVersions.get(i);
1153
                    boolean available = new Boolean(mnReplicationServiceAvailables.get(i)).booleanValue();
1154
                    Service sMNReplication = new Service();
1155
                    sMNReplication.setName("MNReplication");
1156
                    sMNReplication.setVersion(version);
1157
                    sMNReplication.setAvailable(available);
1158
                    services.addService(sMNReplication);
1159
                }
1160
            }
1161
            
1162
            node.setServices(services);
1163

    
1164
            // Set the schedule for synchronization
1165
            Synchronization synchronization = new Synchronization();
1166
            Schedule schedule = new Schedule();
1167
            Date now = new Date();
1168
            schedule.setYear(PropertyService.getProperty("dataone.nodeSynchronization.schedule.year"));
1169
            schedule.setMon(PropertyService.getProperty("dataone.nodeSynchronization.schedule.mon"));
1170
            schedule.setMday(PropertyService.getProperty("dataone.nodeSynchronization.schedule.mday"));
1171
            schedule.setWday(PropertyService.getProperty("dataone.nodeSynchronization.schedule.wday"));
1172
            schedule.setHour(PropertyService.getProperty("dataone.nodeSynchronization.schedule.hour"));
1173
            schedule.setMin(PropertyService.getProperty("dataone.nodeSynchronization.schedule.min"));
1174
            schedule.setSec(PropertyService.getProperty("dataone.nodeSynchronization.schedule.sec"));
1175
            synchronization.setSchedule(schedule);
1176
            synchronization.setLastHarvested(now);
1177
            synchronization.setLastCompleteHarvest(now);
1178
            node.setSynchronization(synchronization);
1179

    
1180
            node.setType(nodeType);
1181
            return node;
1182

    
1183
        } catch (PropertyNotFoundException pnfe) {
1184
            String msg = "MNodeService.getCapabilities(): " + "property not found: " + pnfe.getMessage();
1185
            logMetacat.error(msg);
1186
            throw new ServiceFailure("2162", msg);
1187
        }
1188
    }
1189

    
1190
    
1191

    
1192
    /**
1193
     * A callback method used by a CN to indicate to a MN that it cannot 
1194
     * complete synchronization of the science metadata identified by pid.  Log
1195
     * the event in the metacat event log.
1196
     * 
1197
     * @param session
1198
     * @param syncFailed
1199
     * 
1200
     * @throws ServiceFailure
1201
     * @throws NotAuthorized
1202
     * @throws NotImplemented
1203
     */
1204
    @Override
1205
    public boolean synchronizationFailed(Session session, SynchronizationFailed syncFailed) 
1206
        throws NotImplemented, ServiceFailure, NotAuthorized {
1207

    
1208
        String localId;
1209
        Identifier pid;
1210
        if ( syncFailed.getPid() != null ) {
1211
            pid = new Identifier();
1212
            pid.setValue(syncFailed.getPid());
1213
            boolean allowed;
1214
            
1215
            //are we allowed? only CNs
1216
            try {
1217
                allowed = isAdminAuthorized(session);
1218
                if ( !allowed ){
1219
                    throw new NotAuthorized("2162", 
1220
                            "Not allowed to call synchronizationFailed() on this node.");
1221
                }
1222
            } catch (InvalidToken e) {
1223
                throw new NotAuthorized("2162", 
1224
                        "Not allowed to call synchronizationFailed() on this node.");
1225

    
1226
            }
1227
            
1228
        } else {
1229
            throw new ServiceFailure("2161", "The identifier cannot be null.");
1230

    
1231
        }
1232
        
1233
        try {
1234
            localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1235
        } catch (McdbDocNotFoundException e) {
1236
            throw new ServiceFailure("2161", "The identifier specified by " + 
1237
                    syncFailed.getPid() + " was not found on this node.");
1238

    
1239
        } catch (SQLException e) {
1240
            throw new ServiceFailure("2161", "Couldn't identify the local id of the identifier specified by " + 
1241
                    syncFailed.getPid() + " since "+e.getMessage());
1242
        }
1243
        // TODO: update the CN URL below when the CNRead.SynchronizationFailed
1244
        // method is changed to include the URL as a parameter
1245
        logMetacat.debug("Synchronization for the object identified by " + 
1246
                pid.getValue() + " failed from " + syncFailed.getNodeId() + 
1247
                " Logging the event to the Metacat EventLog as a 'syncFailed' event.");
1248
        // TODO: use the event type enum when the SYNCHRONIZATION_FAILED event is added
1249
        String principal = Constants.SUBJECT_PUBLIC;
1250
        if (session != null && session.getSubject() != null) {
1251
          principal = session.getSubject().getValue();
1252
        }
1253
        try {
1254
          EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), principal, localId, "synchronization_failed");
1255
        } catch (Exception e) {
1256
            throw new ServiceFailure("2161", "Could not log the error for: " + pid.getValue());
1257
        }
1258
        //EventLog.getInstance().log("CN URL WILL GO HERE", 
1259
        //  session.getSubject().getValue(), localId, Event.SYNCHRONIZATION_FAILED);
1260
        return true;
1261

    
1262
    }
1263

    
1264
    /**
1265
     * Essentially a get() but with different logging behavior
1266
     */
1267
    @Override
1268
    public InputStream getReplica(Session session, Identifier pid) 
1269
        throws NotAuthorized, NotImplemented, ServiceFailure, InvalidToken, NotFound {
1270

    
1271
        logMetacat.info("MNodeService.getReplica() called.");
1272

    
1273
        // cannot be called by public
1274
        if (session == null) {
1275
        	throw new InvalidToken("2183", "No session was provided.");
1276
        }
1277
        
1278
        logMetacat.info("MNodeService.getReplica() called with parameters: \n" +
1279
             "\tSession.Subject      = " + session.getSubject().getValue() + "\n" +
1280
             "\tIdentifier           = " + pid.getValue());
1281

    
1282
        InputStream inputStream = null; // bytes to be returned
1283
        handler = new MetacatHandler(new Timer());
1284
        boolean allowed = false;
1285
        String localId; // the metacat docid for the pid
1286

    
1287
        // get the local docid from Metacat
1288
        try {
1289
            localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1290
        } catch (McdbDocNotFoundException e) {
1291
            throw new NotFound("2185", "The object specified by " + 
1292
                    pid.getValue() + " does not exist at this node.");
1293
            
1294
        } catch (SQLException e) {
1295
            throw new ServiceFailure("2181", "The local id of the object specified by " + 
1296
                    pid.getValue() + " couldn't be identified since "+e.getMessage());
1297
        }
1298

    
1299
        Subject targetNodeSubject = session.getSubject();
1300

    
1301
        // check for authorization to replicate, null session to act as this source MN
1302
        try {
1303
            allowed = D1Client.getCN().isNodeAuthorized(null, targetNodeSubject, pid);
1304
        } catch (InvalidToken e1) {
1305
            throw new ServiceFailure("2181", "Could not determine if node is authorized: " 
1306
                + e1.getMessage());
1307
            
1308
        } catch (NotFound e1) {
1309
            throw new NotFound("2185", "Could not find the object "+pid.getValue()+" in this node - " 
1310
                    + e1.getMessage());
1311

    
1312
        } catch (InvalidRequest e1) {
1313
            throw new ServiceFailure("2181", "Could not determine if node is authorized: " 
1314
                    + e1.getMessage());
1315

    
1316
        }
1317

    
1318
        logMetacat.info("Called D1Client.isNodeAuthorized(). Allowed = " + allowed +
1319
            " for identifier " + pid.getValue());
1320

    
1321
        // if the person is authorized, perform the read
1322
        if (allowed) {
1323
            try {
1324
                inputStream = MetacatHandler.read(localId);
1325
            } catch (Exception e) {
1326
                throw new ServiceFailure("2181", "The object specified by " + 
1327
                    pid.getValue() + "could not be returned due to error: " + e.getMessage());
1328
            }
1329
        } else {
1330
            throw new NotAuthorized("2182", "The pid "+pid.getValue()+" is not authorized to be read by the client.");
1331
        }
1332

    
1333
        // if we fail to set the input stream
1334
        if (inputStream == null) {
1335
            throw new ServiceFailure("2181", "The object specified by " + 
1336
                pid.getValue() + " can't be returned from the node.");
1337
        }
1338

    
1339
        // log the replica event
1340
        String principal = null;
1341
        if (session.getSubject() != null) {
1342
            principal = session.getSubject().getValue();
1343
        }
1344
        EventLog.getInstance().log(request.getRemoteAddr(), 
1345
            request.getHeader("User-Agent"), principal, localId, "replicate");
1346

    
1347
        return inputStream;
1348
    }
1349
    
1350
    /**
1351
     * A method to notify the Member Node that the authoritative copy of 
1352
     * system metadata on the Coordinating Nodes has changed.
1353
     *
1354
     * @param session   Session information that contains the identity of the 
1355
     *                  calling user as retrieved from the X.509 certificate 
1356
     *                  which must be traceable to the CILogon service.
1357
     * @param serialVersion   The serialVersion of the system metadata
1358
     * @param dateSysMetaLastModified  The time stamp for when the system metadata was changed
1359
     * @throws NotImplemented
1360
     * @throws ServiceFailure
1361
     * @throws NotAuthorized
1362
     * @throws InvalidRequest
1363
     * @throws InvalidToken
1364
     */
1365
    public boolean systemMetadataChanged(Session session, Identifier pid,
1366
        long serialVersion, Date dateSysMetaLastModified) 
1367
        throws NotImplemented, ServiceFailure, NotAuthorized, InvalidRequest,
1368
        InvalidToken {
1369
        boolean needCheckAuthoriativeNode = true; 
1370
        return systemMetadataChanged(needCheckAuthoriativeNode, session, pid,serialVersion, dateSysMetaLastModified);
1371
    }
1372

    
1373
    /**
1374
     * A method to notify the Member Node that the authoritative copy of 
1375
     * system metadata on the Coordinating Nodes has changed.
1376
     * @param needCheckAuthoriativeNode  this is for the dataone version 2. In the
1377
     * version 2, there are two scenarios:
1378
     * 1. If the node is the authoritative node, it only accepts serial version and replica list.
1379
     * 2. If the node is a replica, it accepts everything.
1380
     * For the v1, api, the parameter should be false. 
1381
     * @param session   Session information that contains the identity of the 
1382
     *                  calling user as retrieved from the X.509 certificate 
1383
     *                  which must be traceable to the CILogon service.
1384
     * @param serialVersion   The serialVersion of the system metadata
1385
     * @param dateSysMetaLastModified  The time stamp for when the system metadata was changed
1386
     * @throws NotImplemented
1387
     * @throws ServiceFailure
1388
     * @throws NotAuthorized
1389
     * @throws InvalidRequest
1390
     * @throws InvalidToken
1391
     */
1392
    public boolean systemMetadataChanged(boolean needCheckAuthoriativeNode, Session session, Identifier pid,
1393
        long serialVersion, Date dateSysMetaLastModified) 
1394
        throws NotImplemented, ServiceFailure, NotAuthorized, InvalidRequest,
1395
        InvalidToken {
1396
        
1397
        // cannot be called by public
1398
        if (session == null) {
1399
        	throw new InvalidToken("1332", "No session was provided.");
1400
        }
1401

    
1402
        String serviceFailureCode = "1333";
1403
        Identifier sid = getPIDForSID(pid, serviceFailureCode);
1404
        if(sid != null) {
1405
            pid = sid;
1406
        }
1407
        
1408
        SystemMetadata currentLocalSysMeta = null;
1409
        SystemMetadata newSysMeta = null;
1410
        CNode cn = D1Client.getCN();
1411
        NodeList nodeList = null;
1412
        Subject callingSubject = null;
1413
        boolean allowed = false;
1414
        
1415
        // are we allowed to call this?
1416
        callingSubject = session.getSubject();
1417
        nodeList = cn.listNodes();
1418
        
1419
        for(Node node : nodeList.getNodeList()) {
1420
            // must be a CN
1421
            if ( node.getType().equals(NodeType.CN)) {
1422
               List<Subject> subjectList = node.getSubjectList();
1423
               // the calling subject must be in the subject list
1424
               if ( subjectList.contains(callingSubject)) {
1425
                   allowed = true;
1426
                   
1427
               }
1428
               
1429
            }
1430
        }
1431
        
1432
        if (!allowed ) {
1433
            String msg = "The subject identified by " + callingSubject.getValue() +
1434
              " is not authorized to call this service.";
1435
            throw new NotAuthorized("1331", msg);
1436
            
1437
        }
1438
        try {
1439
            HazelcastService.getInstance().getSystemMetadataMap().lock(pid);
1440
        
1441
            // compare what we have locally to what is sent in the change notification
1442
            try {
1443
                currentLocalSysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1444
                 
1445
            } catch (RuntimeException e) {
1446
                String msg = "SystemMetadata for pid " + pid.getValue() +
1447
                  " couldn't be updated because it couldn't be found locally: " +
1448
                  e.getMessage();
1449
                logMetacat.error(msg);
1450
                ServiceFailure sf = new ServiceFailure("1333", msg);
1451
                sf.initCause(e);
1452
                throw sf; 
1453
            }
1454
            
1455
            if(currentLocalSysMeta == null) {
1456
                throw new InvalidRequest("1334", "We can't find the system metadata in the node for the id "+pid.getValue());
1457
            }
1458
            if (currentLocalSysMeta.getSerialVersion().longValue() < serialVersion ) {
1459
                try {
1460
                    newSysMeta = cn.getSystemMetadata(null, pid);
1461
                } catch (NotFound e) {
1462
                    // huh? you just said you had it
1463
                	String msg = "On updating the local copy of system metadata " + 
1464
                    "for pid " + pid.getValue() +", the CN reports it is not found." +
1465
                    " The error message was: " + e.getMessage();
1466
                    logMetacat.error(msg);
1467
                    //ServiceFailure sf = new ServiceFailure("1333", msg);
1468
                    InvalidRequest sf = new InvalidRequest("1334", msg);
1469
                    sf.initCause(e);
1470
                    throw sf;
1471
                }
1472
                
1473
                //check about the sid in the system metadata
1474
                Identifier newSID = newSysMeta.getSeriesId();
1475
                if(newSID != null) {
1476
                    if (!isValidIdentifier(newSID)) {
1477
                        throw new InvalidRequest("1334", "The series identifier in the new system metadata is invalid.");
1478
                    }
1479
                    Identifier currentSID = currentLocalSysMeta.getSeriesId();
1480
                    if( currentSID != null && currentSID.getValue() != null) {
1481
                        if(!newSID.getValue().equals(currentSID.getValue())) {
1482
                            //newSID doesn't match the currentSID. The newSID shouldn't be used.
1483
                            try {
1484
                                if(IdentifierManager.getInstance().identifierExists(newSID.getValue())) {
1485
                                    throw new InvalidRequest("1334", "The series identifier "+newSID.getValue()+" in the new system metadata has been used by another object.");
1486
                                }
1487
                            } catch (SQLException sql) {
1488
                                throw new ServiceFailure("1333", "Couldn't determine if the SID "+newSID.getValue()+" in the system metadata exists in the node since "+sql.getMessage());
1489
                            }
1490
                            
1491
                        }
1492
                    } else {
1493
                        //newSID shouldn't be used
1494
                        try {
1495
                            if(IdentifierManager.getInstance().identifierExists(newSID.getValue())) {
1496
                                throw new InvalidRequest("1334", "The series identifier "+newSID.getValue()+" in the new system metadata has been used by another object.");
1497
                            }
1498
                        } catch (SQLException sql) {
1499
                            throw new ServiceFailure("1333", "Couldn't determine if the SID "+newSID.getValue()+" in the system metadata exists in the node since "+sql.getMessage());
1500
                        }
1501
                    }
1502
                }
1503
                // update the local copy of system metadata for the pid
1504
                try {
1505
                    if(needCheckAuthoriativeNode) {
1506
                        //this is for the v2 api.
1507
                        if(isAuthoritativeNode(pid)) {
1508
                            //this is the authoritative node, so we only accept replica and serial version
1509
                            List<Replica> replicas = newSysMeta.getReplicaList();
1510
                            newSysMeta = currentLocalSysMeta;
1511
                            newSysMeta.setSerialVersion(new BigInteger((new Long(serialVersion)).toString()));
1512
                            newSysMeta.setReplicaList(replicas);
1513
                        }
1514
                    }
1515
                    HazelcastService.getInstance().getSystemMetadataMap().put(newSysMeta.getIdentifier(), newSysMeta);
1516
                    logMetacat.info("Updated local copy of system metadata for pid " +
1517
                        pid.getValue() + " after change notification from the CN.");
1518
                    
1519
                    // TODO: consider inspecting the change for archive
1520
                    // see: https://projects.ecoinformatics.org/ecoinfo/issues/6417
1521
    //                if (newSysMeta.getArchived() != null && newSysMeta.getArchived().booleanValue()) {
1522
    //                	try {
1523
    //						this.archive(session, newSysMeta.getIdentifier());
1524
    //					} catch (NotFound e) {
1525
    //						// do we care? nothing to do about it now
1526
    //						logMetacat.error(e.getMessage(), e);
1527
    //					}
1528
    //                }
1529
                    
1530
                } catch (RuntimeException e) {
1531
                    String msg = "SystemMetadata for pid " + pid.getValue() +
1532
                      " couldn't be updated: " +
1533
                      e.getMessage();
1534
                    logMetacat.error(msg);
1535
                    ServiceFailure sf = new ServiceFailure("1333", msg);
1536
                    sf.initCause(e);
1537
                    throw sf;
1538
                }
1539
                
1540
               
1541
            }
1542
        } finally {
1543
            HazelcastService.getInstance().getSystemMetadataMap().unlock(pid);
1544
        }
1545
        
1546
        if (currentLocalSysMeta.getSerialVersion().longValue() < serialVersion ) {
1547
            // attempt to re-register the identifier (it checks if it is a doi)
1548
            try {
1549
                DOIService.getInstance().registerDOI(newSysMeta);
1550
            } catch (Exception e) {
1551
                logMetacat.warn("Could not [re]register DOI: " + e.getMessage(), e);
1552
            }
1553
            
1554
            // submit for indexing
1555
            try {
1556
                MetacatSolrIndex.getInstance().submit(newSysMeta.getIdentifier(), newSysMeta, null, true);
1557
            } catch (Exception e) {
1558
                logMetacat.error("Could not submit changed systemMetadata for indexing, pid: " + newSysMeta.getIdentifier().getValue(), e);
1559
            }
1560
        }
1561
        
1562
        return true;
1563
        
1564
    }
1565
    
1566
    /*
1567
     * Set the replication status for the object on the Coordinating Node
1568
     * 
1569
     * @param session - the session for the this target node
1570
     * @param pid - the identifier of the object being updated
1571
     * @param nodeId - the identifier of this target node
1572
     * @param status - the replication status to set
1573
     * @param failure - the exception to include, if any
1574
     */
1575
    private void setReplicationStatus(Session session, Identifier pid, 
1576
        NodeReference nodeId, ReplicationStatus status, BaseException failure) 
1577
        throws ServiceFailure, NotImplemented, NotAuthorized, 
1578
        InvalidRequest {
1579
        
1580
        // call the CN as the MN to set the replication status
1581
        try {
1582
            this.cn = D1Client.getCN();
1583
            this.cn.setReplicationStatus(session, pid, nodeId,
1584
                    status, failure);
1585
            
1586
        } catch (InvalidToken e) {
1587
        	String msg = "Could not set the replication status for " + pid.getValue() + " on the CN (InvalidToken): " + e.getMessage();
1588
            logMetacat.error(msg);
1589
        	throw new ServiceFailure("2151",
1590
                    msg);
1591
            
1592
        } catch (NotFound e) {
1593
        	String msg = "Could not set the replication status for " + pid.getValue() + " on the CN (NotFound): " + e.getMessage();
1594
            logMetacat.error(msg);
1595
        	throw new ServiceFailure("2151",
1596
                    msg);
1597
            
1598
        }
1599
    }
1600
    
1601
    private SystemMetadata makePublicIfNot(SystemMetadata sysmeta, Identifier pid) throws ServiceFailure, InvalidToken, NotFound, NotImplemented, InvalidRequest {
1602
    	// check if it is publicly readable
1603
		boolean isPublic = false;
1604
		Subject publicSubject = new Subject();
1605
		publicSubject.setValue(Constants.SUBJECT_PUBLIC);
1606
		Session publicSession = new Session();
1607
		publicSession.setSubject(publicSubject);
1608
		AccessRule publicRule = new AccessRule();
1609
		publicRule.addPermission(Permission.READ);
1610
		publicRule.addSubject(publicSubject);
1611
		
1612
		// see if we need to add the rule
1613
		try {
1614
			isPublic = this.isAuthorized(publicSession, pid, Permission.READ);
1615
		} catch (NotAuthorized na) {
1616
			// well, certainly not authorized for public read!
1617
		}
1618
		if (!isPublic) {
1619
			sysmeta.getAccessPolicy().addAllow(publicRule);
1620
		}
1621
		
1622
		return sysmeta;
1623
    }
1624

    
1625
	@Override
1626
	public Identifier generateIdentifier(Session session, String scheme, String fragment)
1627
			throws InvalidToken, ServiceFailure, NotAuthorized, NotImplemented,
1628
			InvalidRequest {
1629
		
1630
		// check for null session
1631
        if (session == null) {
1632
          throw new InvalidToken("2190", "Session is required to generate an Identifier at this Node.");
1633
        }
1634
		
1635
		Identifier identifier = new Identifier();
1636
		
1637
		// handle different schemes
1638
		if (scheme.equalsIgnoreCase(UUID_SCHEME)) {
1639
			// UUID
1640
			UUID uuid = UUID.randomUUID();
1641
            identifier.setValue(UUID_PREFIX + uuid.toString());
1642
		} else if (scheme.equalsIgnoreCase(DOI_SCHEME)) {
1643
			// generate a DOI
1644
			try {
1645
				identifier = DOIService.getInstance().generateDOI();
1646
			} catch (EZIDException e) {
1647
				ServiceFailure sf = new ServiceFailure("2191", "Could not generate DOI: " + e.getMessage());
1648
				sf.initCause(e);
1649
				throw sf;
1650
			}
1651
		} else {
1652
			// default if we don't know the scheme
1653
			if (fragment != null) {
1654
				// for now, just autogen with fragment
1655
				String autogenId = DocumentUtil.generateDocumentId(fragment, 0);
1656
				identifier.setValue(autogenId);			
1657
			} else {
1658
				// autogen with no fragment
1659
				String autogenId = DocumentUtil.generateDocumentId(0);
1660
				identifier.setValue(autogenId);
1661
			}
1662
		}
1663
		
1664
		// TODO: reserve the identifier with the CN. We can only do this when
1665
		// 1) the MN is part of a CN cluster
1666
		// 2) the request is from an authenticated user
1667
		
1668
		return identifier;
1669
	}
1670

    
1671
	
1672

    
1673
	@Override
1674
	public QueryEngineDescription getQueryEngineDescription(Session session, String engine)
1675
			throws InvalidToken, ServiceFailure, NotAuthorized, NotImplemented,
1676
			NotFound {
1677
	    if(engine != null && engine.equals(EnabledQueryEngines.PATHQUERYENGINE)) {
1678
	        if(!EnabledQueryEngines.getInstance().isEnabled(EnabledQueryEngines.PATHQUERYENGINE)) {
1679
                throw new NotImplemented("0000", "MNodeService.query - the query engine "+engine +" hasn't been implemented or has been disabled.");
1680
            }
1681
	        QueryEngineDescription qed = new QueryEngineDescription();
1682
	        qed.setName(EnabledQueryEngines.PATHQUERYENGINE);
1683
	        qed.setQueryEngineVersion("1.0");
1684
	        qed.addAdditionalInfo("This is the traditional structured query for Metacat");
1685
	        Vector<String> pathsForIndexing = null;
1686
	        try {
1687
	            pathsForIndexing = SystemUtil.getPathsForIndexing();
1688
	        } catch (MetacatUtilException e) {
1689
	            logMetacat.warn("Could not get index paths", e);
1690
	        }
1691
	        for (String fieldName: pathsForIndexing) {
1692
	            QueryField field = new QueryField();
1693
	            field.addDescription("Indexed field for path '" + fieldName + "'");
1694
	            field.setName(fieldName);
1695
	            field.setReturnable(true);
1696
	            field.setSearchable(true);
1697
	            field.setSortable(false);
1698
	            // TODO: determine type and multivaluedness
1699
	            field.setType(String.class.getName());
1700
	            //field.setMultivalued(true);
1701
	            qed.addQueryField(field);
1702
	        }
1703
	        return qed;
1704
	    } else if (engine != null && engine.equals(EnabledQueryEngines.SOLRENGINE)) {
1705
	        if(!EnabledQueryEngines.getInstance().isEnabled(EnabledQueryEngines.SOLRENGINE)) {
1706
                throw new NotImplemented("0000", "MNodeService.getQueryEngineDescription - the query engine "+engine +" hasn't been implemented or has been disabled.");
1707
            }
1708
	        try {
1709
	            QueryEngineDescription qed = MetacatSolrEngineDescriptionHandler.getInstance().getQueryEngineDescritpion();
1710
	            return qed;
1711
	        } catch (Exception e) {
1712
	            e.printStackTrace();
1713
	            throw new ServiceFailure("Solr server error", e.getMessage());
1714
	        }
1715
	    } else {
1716
	        throw new NotFound("404", "The Metacat member node can't find the query engine - "+engine);
1717
	    }
1718
		
1719
	}
1720

    
1721
	@Override
1722
	public QueryEngineList listQueryEngines(Session session) throws InvalidToken,
1723
			ServiceFailure, NotAuthorized, NotImplemented {
1724
		QueryEngineList qel = new QueryEngineList();
1725
		//qel.addQueryEngine(EnabledQueryEngines.PATHQUERYENGINE);
1726
		//qel.addQueryEngine(EnabledQueryEngines.SOLRENGINE);
1727
		List<String> enables = EnabledQueryEngines.getInstance().getEnabled();
1728
		for(String name : enables) {
1729
		    qel.addQueryEngine(name);
1730
		}
1731
		return qel;
1732
	}
1733

    
1734
	@Override
1735
	public InputStream query(Session session, String engine, String query) throws InvalidToken,
1736
			ServiceFailure, NotAuthorized, InvalidRequest, NotImplemented,
1737
			NotFound {
1738
	    String user = Constants.SUBJECT_PUBLIC;
1739
        String[] groups= null;
1740
        Set<Subject> subjects = null;
1741
        if (session != null) {
1742
            user = session.getSubject().getValue();
1743
            subjects = AuthUtils.authorizedClientSubjects(session);
1744
            if (subjects != null) {
1745
                List<String> groupList = new ArrayList<String>();
1746
                for (Subject subject: subjects) {
1747
                    groupList.add(subject.getValue());
1748
                }
1749
                groups = groupList.toArray(new String[0]);
1750
            }
1751
        } else {
1752
            //add the public user subject to the set 
1753
            Subject subject = new Subject();
1754
            subject.setValue(Constants.SUBJECT_PUBLIC);
1755
            subjects = new HashSet<Subject>();
1756
            subjects.add(subject);
1757
        }
1758
        //System.out.println("====== user is "+user);
1759
        //System.out.println("====== groups are "+groups);
1760
		if (engine != null && engine.equals(EnabledQueryEngines.PATHQUERYENGINE)) {
1761
		    if(!EnabledQueryEngines.getInstance().isEnabled(EnabledQueryEngines.PATHQUERYENGINE)) {
1762
                throw new NotImplemented("0000", "MNodeService.query - the query engine "+engine +" hasn't been implemented or has been disabled.");
1763
            }
1764
			try {
1765
				DBQuery queryobj = new DBQuery();
1766
				
1767
				String results = queryobj.performPathquery(query, user, groups);
1768
				ContentTypeByteArrayInputStream ctbais = new ContentTypeByteArrayInputStream(results.getBytes(MetaCatServlet.DEFAULT_ENCODING));
1769
				ctbais.setContentType("text/xml");
1770
				return ctbais;
1771

    
1772
			} catch (Exception e) {
1773
				throw new ServiceFailure("Pathquery error", e.getMessage());
1774
			}
1775
			
1776
		} else if (engine != null && engine.equals(EnabledQueryEngines.SOLRENGINE)) {
1777
		    if(!EnabledQueryEngines.getInstance().isEnabled(EnabledQueryEngines.SOLRENGINE)) {
1778
		        throw new NotImplemented("0000", "MNodeService.query - the query engine "+engine +" hasn't been implemented or has been disabled.");
1779
		    }
1780
		    logMetacat.info("The query is ==================================== \n"+query);
1781
		    try {
1782
		        
1783
                return MetacatSolrIndex.getInstance().query(query, subjects);
1784
            } catch (Exception e) {
1785
                // TODO Auto-generated catch block
1786
                throw new ServiceFailure("Solr server error", e.getMessage());
1787
            } 
1788
		}
1789
		return null;
1790
	}
1791
	
1792
	/**
1793
	 * Given an existing Science Metadata PID, this method mints a DOI
1794
	 * and updates the original object "publishing" the update with the DOI.
1795
	 * This includes updating the ORE map that describes the Science Metadata+data.
1796
	 * TODO: ensure all referenced objects allow public read
1797
	 * 
1798
	 * @see https://projects.ecoinformatics.org/ecoinfo/issues/6014
1799
	 * 
1800
	 * @param originalIdentifier
1801
	 * @param request
1802
	 * @throws InvalidRequest 
1803
	 * @throws NotImplemented 
1804
	 * @throws NotAuthorized 
1805
	 * @throws ServiceFailure 
1806
	 * @throws InvalidToken 
1807
	 * @throws NotFound
1808
	 * @throws InvalidSystemMetadata 
1809
	 * @throws InsufficientResources 
1810
	 * @throws UnsupportedType 
1811
	 * @throws IdentifierNotUnique 
1812
	 */
1813
	public Identifier publish(Session session, Identifier originalIdentifier) throws InvalidToken, ServiceFailure, NotAuthorized, NotImplemented, InvalidRequest, NotFound, IdentifierNotUnique, UnsupportedType, InsufficientResources, InvalidSystemMetadata {
1814
		
1815
	    String serviceFailureCode = "1030";
1816
	    Identifier sid = getPIDForSID(originalIdentifier, serviceFailureCode);
1817
	    if(sid != null) {
1818
	        originalIdentifier = sid;
1819
	    }
1820
		// get the original SM
1821
		SystemMetadata originalSystemMetadata = this.getSystemMetadata(session, originalIdentifier);
1822

    
1823
		// make copy of it using the marshaller to ensure DEEP copy
1824
		SystemMetadata sysmeta = null;
1825
		try {
1826
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
1827
			TypeMarshaller.marshalTypeToOutputStream(originalSystemMetadata, baos);
1828
			sysmeta = TypeMarshaller.unmarshalTypeFromStream(SystemMetadata.class, new ByteArrayInputStream(baos.toByteArray()));
1829
		} catch (Exception e) {
1830
			// report as service failure
1831
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
1832
			sf.initCause(e);
1833
			throw sf;
1834
		}
1835

    
1836
		// mint a DOI for the new revision
1837
		Identifier newIdentifier = this.generateIdentifier(session, MNodeService.DOI_SCHEME, null);
1838
				
1839
		// set new metadata values
1840
		sysmeta.setIdentifier(newIdentifier);
1841
		sysmeta.setObsoletes(originalIdentifier);
1842
		sysmeta.setObsoletedBy(null);
1843
		
1844
		// ensure it is publicly readable
1845
		sysmeta = makePublicIfNot(sysmeta, originalIdentifier);
1846
		
1847
		// get the bytes
1848
		InputStream inputStream = this.get(session, originalIdentifier);
1849
		
1850
		// update the object
1851
		this.update(session, originalIdentifier, inputStream, newIdentifier, sysmeta);
1852
		
1853
		// update ORE that references the scimeta
1854
		// first try the naive method, then check the SOLR index
1855
		try {
1856
			String localId = IdentifierManager.getInstance().getLocalId(originalIdentifier.getValue());
1857
			
1858
			Identifier potentialOreIdentifier = new Identifier();
1859
			potentialOreIdentifier.setValue(SystemMetadataFactory.RESOURCE_MAP_PREFIX + localId);
1860
			
1861
			InputStream oreInputStream = null;
1862
			try {
1863
				oreInputStream = this.get(session, potentialOreIdentifier);
1864
			} catch (NotFound nf) {
1865
				// this is probably okay for many sci meta data docs
1866
				logMetacat.warn("No potential ORE map found for: " + potentialOreIdentifier.getValue());
1867
				// try the SOLR index
1868
				List<Identifier> potentialOreIdentifiers = this.lookupOreFor(originalIdentifier, false);
1869
				if (potentialOreIdentifiers != null) {
1870
					potentialOreIdentifier = potentialOreIdentifiers.get(0);
1871
					try {
1872
						oreInputStream = this.get(session, potentialOreIdentifier);
1873
					} catch (NotFound nf2) {
1874
						// this is probably okay for many sci meta data docs
1875
						logMetacat.warn("No potential ORE map found for: " + potentialOreIdentifier.getValue());
1876
					}
1877
				}
1878
			}
1879
			if (oreInputStream != null) {
1880
				Identifier newOreIdentifier = MNodeService.getInstance(request).generateIdentifier(session, MNodeService.UUID_SCHEME, null);
1881
	
1882
				Map<Identifier, Map<Identifier, List<Identifier>>> resourceMapStructure = ResourceMapFactory.getInstance().parseResourceMap(oreInputStream);
1883
				Map<Identifier, List<Identifier>> sciMetaMap = resourceMapStructure.get(potentialOreIdentifier);
1884
				List<Identifier> dataIdentifiers = sciMetaMap.get(originalIdentifier);
1885
					
1886
				// reconstruct the ORE with the new identifiers
1887
				sciMetaMap.remove(originalIdentifier);
1888
				sciMetaMap.put(newIdentifier, dataIdentifiers);
1889
				
1890
				ResourceMap resourceMap = ResourceMapFactory.getInstance().createResourceMap(newOreIdentifier, sciMetaMap);
1891
				String resourceMapString = ResourceMapFactory.getInstance().serializeResourceMap(resourceMap);
1892
				
1893
				// get the original ORE SM and update the values
1894
				SystemMetadata originalOreSysMeta = this.getSystemMetadata(session, potentialOreIdentifier);
1895
				SystemMetadata oreSysMeta = new SystemMetadata();
1896
				try {
1897
					ByteArrayOutputStream baos = new ByteArrayOutputStream();
1898
					TypeMarshaller.marshalTypeToOutputStream(originalOreSysMeta, baos);
1899
					oreSysMeta = TypeMarshaller.unmarshalTypeFromStream(SystemMetadata.class, new ByteArrayInputStream(baos.toByteArray()));
1900
				} catch (Exception e) {
1901
					// report as service failure
1902
					ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
1903
					sf.initCause(e);
1904
					throw sf;
1905
				}
1906

    
1907
				oreSysMeta.setIdentifier(newOreIdentifier);
1908
				oreSysMeta.setObsoletes(potentialOreIdentifier);
1909
				oreSysMeta.setObsoletedBy(null);
1910
				oreSysMeta.setSize(BigInteger.valueOf(resourceMapString.getBytes("UTF-8").length));
1911
				oreSysMeta.setChecksum(ChecksumUtil.checksum(resourceMapString.getBytes("UTF-8"), oreSysMeta.getChecksum().getAlgorithm()));
1912
				
1913
				// ensure ORE is publicly readable
1914
				oreSysMeta = makePublicIfNot(oreSysMeta, potentialOreIdentifier);
1915
				
1916
				// ensure all data objects allow public read
1917
				List<String> pidsToSync = new ArrayList<String>();
1918
				for (Identifier dataId: dataIdentifiers) {
1919
					SystemMetadata dataSysMeta = this.getSystemMetadata(session, dataId);
1920
					dataSysMeta = makePublicIfNot(dataSysMeta, dataId);
1921
					this.updateSystemMetadata(dataSysMeta);
1922
					pidsToSync.add(dataId.getValue());
1923
				}
1924
				SyncAccessPolicy sap = new SyncAccessPolicy();
1925
				try {
1926
					sap.sync(pidsToSync);
1927
				} catch (Exception e) {
1928
					// ignore
1929
					logMetacat.warn("Error attempting to sync access for data objects when publishing package");
1930
				}
1931
				
1932
				// save the updated ORE
1933
				this.update(
1934
						session, 
1935
						potentialOreIdentifier, 
1936
						new ByteArrayInputStream(resourceMapString.getBytes("UTF-8")), 
1937
						newOreIdentifier, 
1938
						oreSysMeta);
1939
				
1940
			} else {
1941
				// create a new ORE for them
1942
				// https://projects.ecoinformatics.org/ecoinfo/issues/6194
1943
				try {
1944
					// find the local id for the NEW package.
1945
					String newLocalId = IdentifierManager.getInstance().getLocalId(newIdentifier.getValue());
1946
	
1947
					@SuppressWarnings("unused")
1948
					SystemMetadata extraSysMeta = SystemMetadataFactory.createSystemMetadata(newLocalId, true, false);
1949
					// should be done generating the ORE here, and the same permissions were used from the metadata object
1950
					
1951
				} catch (Exception e) {
1952
					// oops, guess there was a problem - no package for you
1953
					logMetacat.error("Could not generate new ORE for published object: " + newIdentifier.getValue(), e);
1954
				}
1955
			}
1956
		} catch (McdbDocNotFoundException e) {
1957
			// report as service failure
1958
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
1959
			sf.initCause(e);
1960
			throw sf;
1961
		} catch (UnsupportedEncodingException e) {
1962
			// report as service failure
1963
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
1964
			sf.initCause(e);
1965
			throw sf;
1966
		} catch (OREException e) {
1967
			// report as service failure
1968
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
1969
			sf.initCause(e);
1970
			throw sf;
1971
		} catch (URISyntaxException e) {
1972
			// report as service failure
1973
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
1974
			sf.initCause(e);
1975
			throw sf;
1976
		} catch (OREParserException e) {
1977
			// report as service failure
1978
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
1979
			sf.initCause(e);
1980
			throw sf;
1981
		} catch (ORESerialiserException e) {
1982
			// report as service failure
1983
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
1984
			sf.initCause(e);
1985
			throw sf;
1986
		} catch (NoSuchAlgorithmException e) {
1987
			// report as service failure
1988
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
1989
			sf.initCause(e);
1990
			throw sf;
1991
		} catch (SQLException e) {
1992
            // report as service failure
1993
            ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
1994
            sf.initCause(e);
1995
            throw sf;
1996
        }
1997
		
1998
		return newIdentifier;
1999
	}
2000
	
2001
	/**
2002
	 * Determines if we already have registered an ORE map for this package
2003
	 * NOTE: uses a solr query to locate OREs for the object
2004
	 * @param guid of the EML/packaging object
2005
	 * @return list of resource map identifiers for the given pid
2006
	 */
2007
	public List<Identifier> lookupOreFor(Identifier guid, boolean includeObsolete) {
2008
		// Search for the ORE if we can find it
2009
		String pid = guid.getValue();
2010
		List<Identifier> retList = null;
2011
		try {
2012
			String query = "fl=id,resourceMap&wt=xml&q=-obsoletedBy:[* TO *]+resourceMap:[* TO *]+id:\"" + pid + "\"";
2013
			if (includeObsolete) {
2014
				query = "fl=id,resourceMap&wt=xml&q=resourceMap:[* TO *]+id:\"" + pid + "\"";
2015
			}
2016
			
2017
			InputStream results = this.query(null, "solr", query);
2018
			org.w3c.dom.Node rootNode = XMLUtilities.getXMLReaderAsDOMTreeRootNode(new InputStreamReader(results, "UTF-8"));
2019
			//String resultString = XMLUtilities.getDOMTreeAsString(rootNode);
2020
			org.w3c.dom.NodeList nodeList = XMLUtilities.getNodeListWithXPath(rootNode, "//arr[@name=\"resourceMap\"]/str");
2021
			if (nodeList != null && nodeList.getLength() > 0) {
2022
				retList = new ArrayList<Identifier>();
2023
				for (int i = 0; i < nodeList.getLength(); i++) {
2024
					String found = nodeList.item(i).getFirstChild().getNodeValue();
2025
					Identifier oreId = new Identifier();
2026
					oreId.setValue(found);
2027
					retList.add(oreId);
2028
				}
2029
			}
2030
		} catch (Exception e) {
2031
			logMetacat.error("Error checking for resourceMap[s] on pid " + pid + ". " + e.getMessage(), e);
2032
		}
2033
		
2034
		return retList;
2035
	}
2036
	
2037

    
2038
	@Override
2039
	public InputStream getPackage(Session session, ObjectFormatIdentifier formatId,
2040
			Identifier pid) throws InvalidToken, ServiceFailure,
2041
			NotAuthorized, InvalidRequest, NotImplemented, NotFound {
2042
	    if(formatId == null) {
2043
	        throw new InvalidRequest("2873", "The format type can't be null in the getpackage method.");
2044
	    } else if(!formatId.getValue().equals("application/bagit-097")) {
2045
	        throw new NotImplemented("", "The format "+formatId.getValue()+" is not supported in the getpackage method");
2046
	    }
2047
	    String serviceFailureCode = "2871";
2048
	    Identifier sid = getPIDForSID(pid, serviceFailureCode);
2049
	    if(sid != null) {
2050
	        pid = sid;
2051
	    }
2052
		InputStream bagInputStream = null;
2053
		BagFactory bagFactory = new BagFactory();
2054
		Bag bag = bagFactory.createBag();
2055
		
2056
		// track the temp files we use so we can delete them when finished
2057
		List<File> tempFiles = new ArrayList<File>();
2058
		
2059
		// the pids to include in the package
2060
		List<Identifier> packagePids = new ArrayList<Identifier>();
2061
		
2062
		// catch non-D1 service errors and throw as ServiceFailures
2063
		try {
2064
			//Create a map of dataone ids and file names
2065
			Map<Identifier, String> fileNames = new HashMap<Identifier, String>();
2066
			
2067
			// track the pid-to-file mapping
2068
			StringBuffer pidMapping = new StringBuffer();
2069
			
2070
			// find the package contents
2071
			SystemMetadata sysMeta = this.getSystemMetadata(session, pid);
2072
			if (ObjectFormatCache.getInstance().getFormat(sysMeta.getFormatId()).getFormatType().equals("RESOURCE")) {
2073
				//Get the resource map as a map of Identifiers
2074
				InputStream oreInputStream = this.get(session, pid);
2075
				Map<Identifier, Map<Identifier, List<Identifier>>> resourceMapStructure = ResourceMapFactory.getInstance().parseResourceMap(oreInputStream);
2076
				packagePids.addAll(resourceMapStructure.keySet());
2077
				//Loop through each object in this resource map
2078
				for (Map<Identifier, List<Identifier>> entries: resourceMapStructure.values()) {
2079
					//Loop through each metadata object in this entry
2080
					Set<Identifier> metadataIdentifiers = entries.keySet();
2081
					for(Identifier metadataID: metadataIdentifiers){
2082
						try{
2083
							//Get the system metadata for this metadata object
2084
							SystemMetadata metadataSysMeta = this.getSystemMetadata(session, metadataID);
2085
							
2086
							// include user-friendly metadata
2087
							if (ObjectFormatCache.getInstance().getFormat(metadataSysMeta.getFormatId()).getFormatType().equals("METADATA")) {
2088
								InputStream metadataStream = this.get(session, metadataID);
2089
							
2090
								try {
2091
									// transform
2092
						            String format = "default";
2093

    
2094
									DBTransform transformer = new DBTransform();
2095
						            String documentContent = IOUtils.toString(metadataStream, "UTF-8");
2096
						            String sourceType = metadataSysMeta.getFormatId().getValue();
2097
						            String targetType = "-//W3C//HTML//EN";
2098
						            ByteArrayOutputStream baos = new ByteArrayOutputStream();
2099
						            Writer writer = new OutputStreamWriter(baos , "UTF-8");
2100
						            // TODO: include more params?
2101
						            Hashtable<String, String[]> params = new Hashtable<String, String[]>();
2102
						            String localId = null;
2103
									try {
2104
										localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
2105
									} catch (McdbDocNotFoundException e) {
2106
										throw new NotFound("1020", e.getMessage());
2107
									}
2108
									params.put("qformat", new String[] {format});	            
2109
						            params.put("docid", new String[] {localId});
2110
						            params.put("pid", new String[] {pid.getValue()});
2111
						            params.put("displaymodule", new String[] {"printall"});
2112
						            
2113
						            transformer.transformXMLDocument(
2114
						                    documentContent , 
2115
						                    sourceType, 
2116
						                    targetType , 
2117
						                    format, 
2118
						                    writer, 
2119
						                    params, 
2120
						                    null //sessionid
2121
						                    );
2122
						            
2123
						            // finally, get the HTML back
2124
						            ContentTypeByteArrayInputStream resultInputStream = new ContentTypeByteArrayInputStream(baos.toByteArray());
2125
						            
2126
						            // write to temp file with correct css path
2127
						            File tmpDir = File.createTempFile("package_", "_dir");
2128
						            tmpDir.delete();
2129
						            tmpDir.mkdir();
2130
						            File htmlFile = File.createTempFile("metadata", ".html", tmpDir);
2131
						            File cssDir = new File(tmpDir, format);
2132
						            cssDir.mkdir();
2133
						            File cssFile = new File(tmpDir, format + "/" + format + ".css");
2134
						            String pdfFileName = metadataID.getValue().replaceAll("[^a-zA-Z0-9\\-\\.]", "_") + "-METADATA.pdf";
2135
						            File pdfFile = new File(tmpDir, pdfFileName);
2136
						            //File pdfFile = File.createTempFile("metadata", ".pdf", tmpDir);
2137
						            
2138
						            // put the CSS file in place for the html to find it
2139
						            String originalCssPath = SystemUtil.getContextDir() + "/style/skins/" + format + "/" + format + ".css";
2140
						            IOUtils.copy(new FileInputStream(originalCssPath), new FileOutputStream(cssFile));
2141
						            
2142
						            // write the HTML file
2143
						            IOUtils.copy(resultInputStream, new FileOutputStream(htmlFile));
2144
						            
2145
						            // convert to PDF
2146
						            HtmlToPdf.export(htmlFile.getAbsolutePath(), pdfFile.getAbsolutePath());
2147
						            
2148
						            //add to the package
2149
						            bag.addFileToPayload(pdfFile);
2150
									pidMapping.append(metadataID.getValue() + " (pdf)" +  "\t" + "data/" + pdfFile.getName() + "\n");
2151
						            
2152
						            // mark for clean up after we are done
2153
									htmlFile.delete();
2154
									cssFile.delete();
2155
									cssDir.delete();
2156
						            tempFiles.add(tmpDir);
2157
									tempFiles.add(pdfFile); // delete this first later on
2158
						            
2159
								} catch (Exception e) {
2160
									logMetacat.warn("Could not transform metadata", e);
2161
								}
2162
							}
2163

    
2164
							
2165
							//If this is in eml format, extract the filename and GUID from each entity in its package
2166
							if (metadataSysMeta.getFormatId().getValue().startsWith("eml://")) {
2167
								//Get the package
2168
								DataPackageParserInterface parser = new Eml200DataPackageParser();
2169
								InputStream emlStream = this.get(session, metadataID);
2170
								parser.parse(emlStream);
2171
								DataPackage dataPackage = parser.getDataPackage();
2172
								
2173
								//Get all the entities in this package and loop through each to extract its ID and file name
2174
								Entity[] entities = dataPackage.getEntityList();
2175
								for(Entity entity: entities){
2176
									try{
2177
										//Get the file name from the metadata
2178
										String fileNameFromMetadata = entity.getName();
2179
										
2180
										//Get the ecogrid URL from the metadata
2181
										String ecogridIdentifier = entity.getEntityIdentifier();
2182
										//Parse the ecogrid URL to get the local id
2183
										String idFromMetadata = DocumentUtil.getAccessionNumberFromEcogridIdentifier(ecogridIdentifier);
2184
										
2185
										//Get the docid and rev pair
2186
										String docid = DocumentUtil.getDocIdFromString(idFromMetadata);
2187
										String rev = DocumentUtil.getRevisionStringFromString(idFromMetadata);
2188
										
2189
										//Get the GUID
2190
										String guid = IdentifierManager.getInstance().getGUID(docid, Integer.valueOf(rev));
2191
										Identifier dataIdentifier = new Identifier();
2192
										dataIdentifier.setValue(guid);
2193
										
2194
										//Add the GUID to our GUID & file name map
2195
										fileNames.put(dataIdentifier, fileNameFromMetadata);
2196
									}
2197
									catch(Exception e){
2198
										//Prevent just one entity error
2199
										e.printStackTrace();
2200
										logMetacat.debug(e.getMessage(), e);
2201
									}
2202
								}
2203
							}
2204
						}
2205
						catch(Exception e){
2206
							//Catch errors that would prevent package download
2207
							logMetacat.debug(e.toString());
2208
						}
2209
					}
2210
					packagePids.addAll(entries.keySet());
2211
					for (List<Identifier> dataPids: entries.values()) {
2212
						packagePids.addAll(dataPids);
2213
					}
2214
				}
2215
			} else {
2216
				// just the lone pid in this package
2217
				packagePids.add(pid);
2218
			}
2219
			
2220
			//Create a temp file, then delete it and make a directory with that name
2221
			File tempDir = File.createTempFile("temp", Long.toString(System.nanoTime()));
2222
			tempDir.delete();
2223
			tempDir = new File(tempDir.getPath() + "_dir");
2224
			tempDir.mkdir();			
2225
			tempFiles.add(tempDir);
2226
			File pidMappingFile = new File(tempDir, "pid-mapping.txt");
2227
			
2228
			// loop through the package contents
2229
			for (Identifier entryPid: packagePids) {
2230
				//Get the system metadata for each item
2231
				SystemMetadata entrySysMeta = this.getSystemMetadata(session, entryPid);					
2232
				
2233
				String objectFormatType = ObjectFormatCache.getInstance().getFormat(entrySysMeta.getFormatId()).getFormatType();
2234
				String fileName = null;
2235
				
2236
				//TODO: Be more specific of what characters to replace. Make sure periods arent replaced for the filename from metadata
2237
				//Our default file name is just the ID + format type (e.g. walker.1.1-DATA)
2238
				fileName = entryPid.getValue().replaceAll("[^a-zA-Z0-9\\-\\.]", "_") + "-" + objectFormatType;
2239

    
2240
				if(fileNames.containsKey(entryPid)){
2241
					//Let's use the file name and extension from the metadata is we have it
2242
					fileName = entryPid.getValue().replaceAll("[^a-zA-Z0-9\\-\\.]", "_") + "-" + fileNames.get(entryPid).replaceAll("[^a-zA-Z0-9\\-\\.]", "_");
2243
				}
2244
				else{
2245
					//If we couldn't find a given file name, use the system metadata extension
2246
					String extension = ObjectFormatInfo.instance().getExtension(entrySysMeta.getFormatId().getValue());
2247
					fileName += extension;
2248
				}
2249
				
2250
		        //Create a new file for this item and add to the list
2251
				File tempFile = new File(tempDir, fileName);
2252
				tempFiles.add(tempFile);
2253
				
2254
				InputStream entryInputStream = this.get(session, entryPid);			
2255
				IOUtils.copy(entryInputStream, new FileOutputStream(tempFile));
2256
				bag.addFileToPayload(tempFile);
2257
				pidMapping.append(entryPid.getValue() + "\t" + "data/" + tempFile.getName() + "\n");
2258
			}
2259
			
2260
			//add the the pid to data file map
2261
			IOUtils.write(pidMapping.toString(), new FileOutputStream(pidMappingFile));
2262
			bag.addFileAsTag(pidMappingFile);
2263
			tempFiles.add(pidMappingFile);
2264
			
2265
			bag = bag.makeComplete();
2266
			
2267
			///Now create the zip file
2268
			//Use the pid as the file name prefix, replacing all non-word characters
2269
			String zipName = pid.getValue().replaceAll("\\W", "_");
2270
			
2271
			File bagFile = new File(tempDir, zipName+".zip");
2272
			
2273
			bag.setFile(bagFile);
2274
			ZipWriter zipWriter = new ZipWriter(bagFactory);
2275
			bag.write(zipWriter, bagFile);
2276
			bagFile = bag.getFile();
2277
			// use custom FIS that will delete the file when closed
2278
			bagInputStream = new DeleteOnCloseFileInputStream(bagFile);
2279
			// also mark for deletion on shutdown in case the stream is never closed
2280
			bagFile.deleteOnExit();
2281
			tempFiles.add(bagFile);
2282
			
2283
			// clean up other temp files
2284
			for (int i=tempFiles.size()-1; i>=0; i--){
2285
				tempFiles.get(i).delete();
2286
			}
2287
			
2288
		} catch (IOException e) {
2289
			// report as service failure
2290
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2291
			sf.initCause(e);
2292
			throw sf;
2293
		} catch (OREException e) {
2294
			// report as service failure
2295
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2296
			sf.initCause(e);
2297
			throw sf;
2298
		} catch (URISyntaxException e) {
2299
			// report as service failure
2300
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2301
			sf.initCause(e);
2302
			throw sf;
2303
		} catch (OREParserException e) {
2304
			// report as service failure
2305
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2306
			sf.initCause(e);
2307
			throw sf;
2308
		}
2309
		
2310
		return bagInputStream;
2311
	}
2312
    
2313
	/**
2314
	 * Update the system metadata of the specified pid.
2315
	 */
2316
	@Override
2317
	public boolean updateSystemMetadata(Session session, Identifier pid,
2318
            SystemMetadata sysmeta) throws NotImplemented, NotAuthorized,
2319
            ServiceFailure, InvalidRequest, InvalidSystemMetadata, InvalidToken {
2320
	 if(sysmeta == null) {
2321
	     throw  new InvalidRequest("4863", "The system metadata object should NOT be null in the updateSystemMetadata request.");
2322
	 }
2323
	 if(pid == null || pid.getValue() == null) {
2324
         throw new InvalidRequest("4863", "Please specify the id in the updateSystemMetadata request ") ;
2325
     }
2326

    
2327
     if (session == null) {
2328
         //TODO: many of the thrown exceptions do not use the correct error codes
2329
         //check these against the docs and correct them
2330
         throw new NotAuthorized("4861", "No Session - could not authorize for updating system metadata." +
2331
                 "  If you are not logged in, please do so and retry the request.");
2332
     } else {
2333
         try {
2334
             //Following session can do the change:
2335
           //- Authoritative Member Node (we can use isNodeAdmin since we checked isAuthoritativeNode )
2336
             //- Owner of object (coved by the userHasPermission method)
2337
             //- user subjects with the change permission
2338
             //Note: Coordinating Node can not because MN is authoritative
2339
             /*if(!isAuthoritativeNode(pid)) {
2340
                throw  new InvalidRequest("4863", "Client can only call updateSystemMetadata request on the authoritative memember node.");
2341
             }
2342
             if(!isNodeAdmin(session) && !userHasPermission(session, pid, Permission.CHANGE_PERMISSION)) {
2343
                 throw new NotAuthorized("4861", "The client -"+ session.getSubject().getValue()+ "is not authorized for updating the system metadata of the object "+pid.getValue());
2344
             }*/
2345
             if(!allowUpdating(session, pid, Permission.CHANGE_PERMISSION)) {
2346
                 throw new NotAuthorized("4861", "The client -"+ session.getSubject().getValue()+ "is not authorized for updating the system metadata of the object "+pid.getValue());
2347
             }
2348
         } catch (NotFound e) {
2349
             throw new InvalidRequest("4863", "Can't determine if the client has the permission to update the system metacat of the object with id "+pid.getValue()+" since "+e.getDescription());
2350
         }
2351
         
2352
     }
2353
      //update the system metadata locally
2354
      boolean success = false;
2355
      try {
2356
          HazelcastService.getInstance().getSystemMetadataMap().lock(pid);
2357
          SystemMetadata currentSysmeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
2358
          if(currentSysmeta == null) {
2359
              throw  new InvalidRequest("4863", "We can't find the current system metadata on the member node for the id "+pid.getValue());
2360
          }
2361
          Date currentModiDate = currentSysmeta.getDateSysMetadataModified();
2362
          Date commingModiDate = sysmeta.getDateSysMetadataModified();
2363
          if(commingModiDate == null) {
2364
              throw  new InvalidRequest("4863", "The system metadata modification date can't be null.");
2365
          }
2366
          if(currentModiDate != null && commingModiDate.getTime() != currentModiDate.getTime()) {
2367
              throw new InvalidRequest("4863", "Your system metadata modification date is "+commingModiDate.toString()+
2368
                      ". It doesn't match our current system metadata modification date in the member node - "+currentModiDate.toString()+
2369
                      ". Please check if you have got the newest version of the system metadata before the modification.");
2370
          }
2371
          boolean needUpdateModificationDate = true;
2372
          success = updateSystemMetadata(session, pid, sysmeta, needUpdateModificationDate, currentSysmeta);
2373
      } finally {
2374
          HazelcastService.getInstance().getSystemMetadataMap().unlock(pid);
2375
      }
2376
      
2377
      if(success) {
2378
          //TODO
2379
          //notify the cns the synchornize the new system metadata.
2380
          this.cn = D1Client.getCN();
2381
          try {
2382
              if(this.cn == null)  {
2383
                  logMetacat.warn("updateSystemMetadata - can't get the instance of the CN. So the system metadata in CN can't be updated.");
2384
              } else {
2385
                  this.cn.synchronize(null, pid);
2386
              }
2387
          } catch (BaseException e) {
2388
              e.printStackTrace();
2389
              logMetacat.error("It is a DataONEBaseException and its detail code is "+e.getDetail_code() +" and its code is "+e.getCode());
2390
              logMetacat.error("Can't update the systemmetadata of pid "+pid.getValue()+" in CNs since "+e.getMessage());
2391
          } catch (Exception e) {
2392
              e.printStackTrace();
2393
              logMetacat.error("Can't update the systemmetadata of pid "+pid.getValue()+" in CNs since "+e.getMessage());
2394
          }
2395
          
2396
          // attempt to re-register the identifier (it checks if it is a doi)
2397
          try {
2398
        	  DOIService.getInstance().registerDOI(sysmeta);
2399
          } catch (Exception e) {
2400
  			logMetacat.warn("Could not [re]register DOI: " + e.getMessage(), e);
2401
          }
2402
      }
2403
      return success;
2404
    }
2405
	
2406
	/*
2407
     * Determine if the current node is the authoritative node for the given pid.
2408
     */
2409
    protected boolean isAuthoritativeNode(Identifier pid) {
2410
        boolean isAuthoritativeNode = false;
2411
        if(pid != null && pid.getValue() != null) {
2412
            SystemMetadata sys = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
2413
            if(sys != null) {
2414
                NodeReference node = sys.getAuthoritativeMemberNode();
2415
                if(node != null) {
2416
                    String nodeValue = node.getValue();
2417
                    logMetacat.debug("The authoritative node for id "+pid.getValue()+" is "+nodeValue);
2418
                    //System.out.println("The authoritative node for id "+pid.getValue()+" is "+nodeValue);
2419
                    String currentNodeId = Settings.getConfiguration().getString("dataone.nodeId");
2420
                    logMetacat.debug("The node id in metacat.properties is "+currentNodeId);
2421
                    //System.out.println("The node id in metacat.properties is "+currentNodeId);
2422
                    if(currentNodeId != null && !currentNodeId.trim().equals("") && currentNodeId.equals(nodeValue)) {
2423
                        logMetacat.debug("They are matching");
2424
                        //System.out.println("They are matching");
2425
                        isAuthoritativeNode = true;
2426
                    }
2427
                }
2428
            }
2429
        }
2430
        return isAuthoritativeNode;
2431
    }
2432
    
2433
    /*
2434
     * Rules are:
2435
     * 1. If the session has an cn object, it is allowed.
2436
     * 2. If it is not a cn object, the client should have approperate permission and it should also happen on the authorative node.
2437
     */
2438
    private boolean allowUpdating(Session session, Identifier pid, Permission permission) throws NotAuthorized, NotFound{
2439
        boolean allow = false;
2440
        if(isCNAdmin (session)) {
2441
            allow = true;
2442
        } else {
2443
            if(isAuthoritativeNode(pid)) {
2444
                if(userHasPermission(session, pid, permission)) {
2445
                    allow = true;
2446
                } else {
2447
                    allow = false;
2448
                }
2449
            } else {
2450
                throw new NotAuthorized("4861", "Client can only call the request on the authoritative memember node.");
2451
            }
2452
        }
2453
        return allow;
2454
        
2455
    }
2456
    
2457
}
(5-5/8)