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.nio.charset.Charset;
40
import java.security.NoSuchAlgorithmException;
41
import java.sql.SQLException;
42
import java.util.ArrayList;
43
import java.util.Calendar;
44
import java.util.Date;
45
import java.util.HashMap;
46
import java.util.HashSet;
47
import java.util.Hashtable;
48
import java.util.List;
49
import java.util.Map;
50
import java.util.Set;
51
import java.util.Timer;
52
import java.util.UUID;
53
import java.util.Vector;
54

    
55
import javax.servlet.http.HttpServletRequest;
56

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

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

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

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

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

    
204

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

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

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

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

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

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

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

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

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

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

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

    
448
            isScienceMetadata = isScienceMetadata(sysmeta);
449

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

    
453
                // update the science metadata XML document
454
                // TODO: handle non-XML metadata/data documents (like netCDF)
455
                // TODO: don't put objects into memory using stream to string
456
                //String objectAsXML = "";
457
                try {
458
                    //objectAsXML = IOUtils.toString(object, "UTF-8");
459
                	
460
                	InputStream editedObject = editScienceMetadata(session, object, pid, newPid);
461
                	
462
                    localId = insertOrUpdateDocument(editedObject, "UTF-8", pid, session, "update");
463
                	
464
                    // register the newPid and the generated localId
465
                    if (newPid != null) {
466
                        IdentifierManager.getInstance().createMapping(newPid.getValue(), localId);
467
                    }
468

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

    
474
                }
475

    
476
            } else {
477

    
478
                // update the data object
479
                localId = insertDataObject(object, newPid, session);
480

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

    
494
            // prep the new system metadata, add pid to the affected lists
495
            sysmeta.setObsoletes(pid);
496
            //sysmeta.addDerivedFrom(pid);
497

    
498
            // and insert the new system metadata
499
            insertSystemMetadata(sysmeta);
500

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

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

    
516
        return newPid;
517
    }
518

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

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

    
537
        // set the dates
538
        Date now = Calendar.getInstance().getTime();
539
        sysmeta.setDateSysMetadataModified(now);
540
        sysmeta.setDateUploaded(now);
541
        
542
        // set the serial version
543
        sysmeta.setSerialVersion(BigInteger.ZERO);
544

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

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

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

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

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

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

    
669

    
670
        // get the local node id
671
        try {
672
            nodeIdStr = PropertyService.getProperty("dataone.nodeId");
673
            nodeId = new NodeReference();
674
            nodeId.setValue(nodeIdStr);
675

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

    
684
        }
685
        
686

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

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

    
744
            }
745

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

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

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

    
786
        // verify checksum on the object, if supported
787
        if (object.markSupported()) {
788
            Checksum givenChecksum = sysmeta.getChecksum();
789
            Checksum computedChecksum = null;
790
            try {
791
                computedChecksum = ChecksumUtil.checksum(object, givenChecksum.getAlgorithm());
792
                object.reset();
793

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

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

    
834
        // finish by setting the replication status
835
        setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.COMPLETED, null);
836
        return result;
837

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

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

    
894
        return super.get(session, pid);
895

    
896
    }
897

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

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

    
928
        try {
929
            checksum = ChecksumUtil.checksum(inputStream, algorithm);
930

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

    
939
        if (checksum == null) {
940
            throw new ServiceFailure("1410", "The checksum for the object specified by " + pid.getValue() + "could not be returned.");
941
        }
942

    
943
        return checksum;
944
    }
945

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

    
966
        return super.getSystemMetadata(session, pid);
967
    }
968

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

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

    
1024
        String nodeName = null;
1025
        String nodeId = null;
1026
        String subject = null;
1027
        String contactSubject = null;
1028
        String nodeDesc = null;
1029
        String nodeTypeString = null;
1030
        NodeType nodeType = null;
1031
        List<String> mnCoreServiceVersions = null;
1032
        List<String> mnReadServiceVersions = null;
1033
        List<String> mnAuthorizationServiceVersions = null;
1034
        List<String> mnStorageServiceVersions = null;
1035
        List<String> mnReplicationServiceVersions = null;
1036

    
1037
        boolean nodeSynchronize = false;
1038
        boolean nodeReplicate = false;
1039
        List<String> mnCoreServiceAvailables = null;
1040
        List<String> mnReadServiceAvailables = null;
1041
        List<String> mnAuthorizationServiceAvailables = null;
1042
        List<String> mnStorageServiceAvailables = null;
1043
        List<String> mnReplicationServiceAvailables = null;
1044

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

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

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

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

    
1093
            // services: MNAuthorization, MNCore, MNRead, MNReplication, MNStorage
1094
            Services services = new Services();
1095

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

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

    
1184
            node.setType(nodeType);
1185
            return node;
1186

    
1187
        } catch (PropertyNotFoundException pnfe) {
1188
            String msg = "MNodeService.getCapabilities(): " + "property not found: " + pnfe.getMessage();
1189
            logMetacat.error(msg);
1190
            throw new ServiceFailure("2162", msg);
1191
        }
1192
    }
1193

    
1194
    
1195

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

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

    
1230
            }
1231
            
1232
        } else {
1233
            throw new ServiceFailure("2161", "The identifier cannot be null.");
1234

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

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

    
1266
    }
1267

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

    
1275
        logMetacat.info("MNodeService.getReplica() called.");
1276

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

    
1286
        InputStream inputStream = null; // bytes to be returned
1287
        handler = new MetacatHandler(new Timer());
1288
        boolean allowed = false;
1289
        String localId; // the metacat docid for the pid
1290

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

    
1303
        Subject targetNodeSubject = session.getSubject();
1304

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

    
1316
        } catch (InvalidRequest e1) {
1317
            throw new ServiceFailure("2181", "Could not determine if node is authorized: " 
1318
                    + e1.getMessage());
1319

    
1320
        }
1321

    
1322
        logMetacat.info("Called D1Client.isNodeAuthorized(). Allowed = " + allowed +
1323
            " for identifier " + pid.getValue());
1324

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

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

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

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

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

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

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

    
1675
	
1676

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

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

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

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

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

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

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

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

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

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

    
2244
				if(fileNames.containsKey(entryPid)){
2245
					//Let's use the file name and extension from the metadata is we have it
2246
					fileName = entryPid.getValue().replaceAll("[^a-zA-Z0-9\\-\\.]", "_") + "-" + fileNames.get(entryPid).replaceAll("[^a-zA-Z0-9\\-\\.]", "_");
2247
				}
2248
				else{
2249
					//If we couldn't find a given file name, use the system metadata extension
2250
					String extension = ObjectFormatInfo.instance().getExtension(entrySysMeta.getFormatId().getValue());
2251
					fileName += extension;
2252
				}
2253
				
2254
		        //Create a new file for this item and add to the list
2255
				File tempFile = new File(tempDir, fileName);
2256
				tempFiles.add(tempFile);
2257
				
2258
				InputStream entryInputStream = this.get(session, entryPid);			
2259
				IOUtils.copy(entryInputStream, new FileOutputStream(tempFile));
2260
				bag.addFileToPayload(tempFile);
2261
				pidMapping.append(entryPid.getValue() + "\t" + "data/" + tempFile.getName() + "\n");
2262
			}
2263
			
2264
			//add the the pid to data file map
2265
			IOUtils.write(pidMapping.toString(), new FileOutputStream(pidMappingFile));
2266
			bag.addFileAsTag(pidMappingFile);
2267
			tempFiles.add(pidMappingFile);
2268
			
2269
			bag = bag.makeComplete();
2270
			
2271
			///Now create the zip file
2272
			//Use the pid as the file name prefix, replacing all non-word characters
2273
			String zipName = pid.getValue().replaceAll("\\W", "_");
2274
			
2275
			File bagFile = new File(tempDir, zipName+".zip");
2276
			
2277
			bag.setFile(bagFile);
2278
			ZipWriter zipWriter = new ZipWriter(bagFactory);
2279
			bag.write(zipWriter, bagFile);
2280
			bagFile = bag.getFile();
2281
			// use custom FIS that will delete the file when closed
2282
			bagInputStream = new DeleteOnCloseFileInputStream(bagFile);
2283
			// also mark for deletion on shutdown in case the stream is never closed
2284
			bagFile.deleteOnExit();
2285
			tempFiles.add(bagFile);
2286
			
2287
			// clean up other temp files
2288
			for (int i=tempFiles.size()-1; i>=0; i--){
2289
				tempFiles.get(i).delete();
2290
			}
2291
			
2292
		} catch (IOException e) {
2293
			// report as service failure
2294
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2295
			sf.initCause(e);
2296
			throw sf;
2297
		} catch (OREException e) {
2298
			// report as service failure
2299
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2300
			sf.initCause(e);
2301
			throw sf;
2302
		} catch (URISyntaxException e) {
2303
			// report as service failure
2304
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2305
			sf.initCause(e);
2306
			throw sf;
2307
		} catch (OREParserException e) {
2308
			// report as service failure
2309
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2310
			sf.initCause(e);
2311
			throw sf;
2312
		}
2313
		
2314
		return bagInputStream;
2315
	}
2316
	
2317
	 /**
2318
	   * Archives an object, where the object is either a 
2319
	   * data object or a science metadata object.
2320
	   * 
2321
	   * @param session - the Session object containing the credentials for the Subject
2322
	   * @param pid - The object identifier to be archived
2323
	   * 
2324
	   * @return pid - the identifier of the object used for the archiving
2325
	   * 
2326
	   * @throws InvalidToken
2327
	   * @throws ServiceFailure
2328
	   * @throws NotAuthorized
2329
	   * @throws NotFound
2330
	   * @throws NotImplemented
2331
	   * @throws InvalidRequest
2332
	   */
2333
	  public Identifier archive(Session session, Identifier pid) 
2334
	      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
2335
	      boolean allowed = false;
2336
	      // do we have a valid pid?
2337
	      if (pid == null || pid.getValue().trim().equals("")) {
2338
	          throw new ServiceFailure("1350", "The provided identifier was invalid.");
2339
	      }
2340
	      
2341
	      String serviceFailureCode = "1350";
2342
	      Identifier sid = getPIDForSID(pid, serviceFailureCode);
2343
	      if(sid != null) {
2344
	          pid = sid;
2345
	      }
2346
	      // does the subject have archive (a D1 CHANGE_PERMISSION level) privileges on the pid?
2347
	      try {
2348
	            allowed = isAuthorized(session, pid, Permission.CHANGE_PERMISSION);
2349
	        } catch (InvalidRequest e) {
2350
	          throw new ServiceFailure("1350", e.getDescription());
2351
	        } 
2352

    
2353
	      if (allowed) {
2354
	         try {
2355
	             HazelcastService.getInstance().getSystemMetadataMap().lock(pid);
2356
	             logMetacat.debug("MNodeService.archive - lock the identifier "+pid.getValue()+" in the system metadata map.");
2357
	             SystemMetadata sysmeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
2358
	             boolean needModifyDate = true;
2359
	             boolean logArchive = true;
2360
	             super.archiveObject(logArchive, session, pid, sysmeta, needModifyDate); 
2361
	         } finally {
2362
	             HazelcastService.getInstance().getSystemMetadataMap().unlock(pid);
2363
	             logMetacat.debug("MNodeService.archive - unlock the identifier "+pid.getValue()+" in the system metadata map.");
2364
	         }
2365
	        
2366

    
2367
	      } else {
2368
	          throw new NotAuthorized("1320", "The provided identity does not have " + "permission to archive the object on the Node.");
2369
	      }
2370

    
2371
	      return pid;
2372
	  }
2373
    
2374
	/**
2375
	 * Update the system metadata of the specified pid.
2376
	 */
2377
	@Override
2378
	public boolean updateSystemMetadata(Session session, Identifier pid,
2379
            SystemMetadata sysmeta) throws NotImplemented, NotAuthorized,
2380
            ServiceFailure, InvalidRequest, InvalidSystemMetadata, InvalidToken {
2381
	 if(sysmeta == null) {
2382
	     throw  new InvalidRequest("4863", "The system metadata object should NOT be null in the updateSystemMetadata request.");
2383
	 }
2384
	 if(pid == null || pid.getValue() == null) {
2385
         throw new InvalidRequest("4863", "Please specify the id in the updateSystemMetadata request ") ;
2386
     }
2387

    
2388
     if (session == null) {
2389
         //TODO: many of the thrown exceptions do not use the correct error codes
2390
         //check these against the docs and correct them
2391
         throw new NotAuthorized("4861", "No Session - could not authorize for updating system metadata." +
2392
                 "  If you are not logged in, please do so and retry the request.");
2393
     } else {
2394
         try {
2395
             //Following session can do the change:
2396
           //- Authoritative Member Node (we can use isNodeAdmin since we checked isAuthoritativeNode )
2397
             //- Owner of object (coved by the userHasPermission method)
2398
             //- user subjects with the change permission
2399
             //Note: Coordinating Node can not because MN is authoritative
2400
             /*if(!isAuthoritativeNode(pid)) {
2401
                throw  new InvalidRequest("4863", "Client can only call updateSystemMetadata request on the authoritative memember node.");
2402
             }
2403
             if(!isNodeAdmin(session) && !userHasPermission(session, pid, Permission.CHANGE_PERMISSION)) {
2404
                 throw new NotAuthorized("4861", "The client -"+ session.getSubject().getValue()+ "is not authorized for updating the system metadata of the object "+pid.getValue());
2405
             }*/
2406
             if(!allowUpdating(session, pid, Permission.CHANGE_PERMISSION)) {
2407
                 throw new NotAuthorized("4861", "The client -"+ session.getSubject().getValue()+ "is not authorized for updating the system metadata of the object "+pid.getValue());
2408
             }
2409
         } catch (NotFound e) {
2410
             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());
2411
         }
2412
         
2413
     }
2414
      //update the system metadata locally
2415
      boolean success = false;
2416
      try {
2417
          HazelcastService.getInstance().getSystemMetadataMap().lock(pid);
2418
          SystemMetadata currentSysmeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
2419
          if(currentSysmeta == null) {
2420
              throw  new InvalidRequest("4863", "We can't find the current system metadata on the member node for the id "+pid.getValue());
2421
          }
2422
          Date currentModiDate = currentSysmeta.getDateSysMetadataModified();
2423
          Date commingModiDate = sysmeta.getDateSysMetadataModified();
2424
          if(commingModiDate == null) {
2425
              throw  new InvalidRequest("4863", "The system metadata modification date can't be null.");
2426
          }
2427
          if(currentModiDate != null && commingModiDate.getTime() != currentModiDate.getTime()) {
2428
              throw new InvalidRequest("4863", "Your system metadata modification date is "+commingModiDate.toString()+
2429
                      ". It doesn't match our current system metadata modification date in the member node - "+currentModiDate.toString()+
2430
                      ". Please check if you have got the newest version of the system metadata before the modification.");
2431
          }
2432
          boolean needUpdateModificationDate = true;
2433
          boolean fromCN = false;
2434
          success = updateSystemMetadata(session, pid, sysmeta, needUpdateModificationDate, currentSysmeta, fromCN);
2435
      } finally {
2436
          HazelcastService.getInstance().getSystemMetadataMap().unlock(pid);
2437
      }
2438
      
2439
      if(success) {
2440
          //TODO
2441
          //notify the cns the synchornize the new system metadata.
2442
          this.cn = D1Client.getCN();
2443
          try {
2444
              if(this.cn == null)  {
2445
                  logMetacat.warn("updateSystemMetadata - can't get the instance of the CN. So the system metadata in CN can't be updated.");
2446
              } else {
2447
                  this.cn.synchronize(null, pid);
2448
              }
2449
          } catch (BaseException e) {
2450
              e.printStackTrace();
2451
              logMetacat.error("It is a DataONEBaseException and its detail code is "+e.getDetail_code() +" and its code is "+e.getCode());
2452
              logMetacat.error("Can't update the systemmetadata of pid "+pid.getValue()+" in CNs since "+e.getMessage());
2453
          } catch (Exception e) {
2454
              e.printStackTrace();
2455
              logMetacat.error("Can't update the systemmetadata of pid "+pid.getValue()+" in CNs since "+e.getMessage());
2456
          }
2457
          
2458
          // attempt to re-register the identifier (it checks if it is a doi)
2459
          try {
2460
        	  DOIService.getInstance().registerDOI(sysmeta);
2461
          } catch (Exception e) {
2462
  			logMetacat.warn("Could not [re]register DOI: " + e.getMessage(), e);
2463
          }
2464
      }
2465
      return success;
2466
    }
2467
	
2468
	/*
2469
     * Determine if the current node is the authoritative node for the given pid.
2470
     */
2471
    protected boolean isAuthoritativeNode(Identifier pid) {
2472
        boolean isAuthoritativeNode = false;
2473
        if(pid != null && pid.getValue() != null) {
2474
            SystemMetadata sys = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
2475
            if(sys != null) {
2476
                NodeReference node = sys.getAuthoritativeMemberNode();
2477
                if(node != null) {
2478
                    String nodeValue = node.getValue();
2479
                    logMetacat.debug("The authoritative node for id "+pid.getValue()+" is "+nodeValue);
2480
                    //System.out.println("The authoritative node for id "+pid.getValue()+" is "+nodeValue);
2481
                    String currentNodeId = Settings.getConfiguration().getString("dataone.nodeId");
2482
                    logMetacat.debug("The node id in metacat.properties is "+currentNodeId);
2483
                    //System.out.println("The node id in metacat.properties is "+currentNodeId);
2484
                    if(currentNodeId != null && !currentNodeId.trim().equals("") && currentNodeId.equals(nodeValue)) {
2485
                        logMetacat.debug("They are matching");
2486
                        //System.out.println("They are matching");
2487
                        isAuthoritativeNode = true;
2488
                    }
2489
                }
2490
            }
2491
        }
2492
        return isAuthoritativeNode;
2493
    }
2494
    
2495
    /*
2496
     * Rules are:
2497
     * 1. If the session has an cn object, it is allowed.
2498
     * 2. If it is not a cn object, the client should have approperate permission and it should also happen on the authorative node.
2499
     */
2500
    private boolean allowUpdating(Session session, Identifier pid, Permission permission) throws NotAuthorized, NotFound{
2501
        boolean allow = false;
2502
        if(isCNAdmin (session)) {
2503
            allow = true;
2504
        } else {
2505
            if(isAuthoritativeNode(pid)) {
2506
                if(userHasPermission(session, pid, permission)) {
2507
                    allow = true;
2508
                } else {
2509
                    allow = false;
2510
                }
2511
            } else {
2512
                throw new NotAuthorized("4861", "Client can only call the request on the authoritative memember node.");
2513
            }
2514
        }
2515
        return allow;
2516
        
2517
    }
2518
    
2519
}
(5-5/8)