Project

General

Profile

1 6174 cjones
/**
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 6226 cjones
import java.io.File;
27
import java.io.FileNotFoundException;
28
import java.io.FileOutputStream;
29
import java.io.IOException;
30 6174 cjones
import java.io.InputStream;
31 6241 cjones
import java.io.OutputStream;
32 6226 cjones
import java.sql.SQLException;
33 6225 leinfelder
import java.util.ArrayList;
34 6186 leinfelder
import java.util.Calendar;
35 6174 cjones
import java.util.Date;
36 6226 cjones
import java.util.Hashtable;
37 6194 leinfelder
import java.util.List;
38 7149 leinfelder
import java.util.Set;
39 6226 cjones
import java.util.Timer;
40 8810 leinfelder
import java.util.concurrent.locks.Lock;
41 6174 cjones
42 6542 leinfelder
import javax.servlet.http.HttpServletRequest;
43 6226 cjones
44 6241 cjones
import org.apache.commons.io.IOUtils;
45 6186 leinfelder
import org.apache.log4j.Logger;
46 8810 leinfelder
import org.dataone.client.v2.CNode;
47
import org.dataone.client.v2.itk.D1Client;
48
import org.dataone.client.v2.formats.ObjectFormatCache;
49 7132 cjones
import org.dataone.service.exceptions.BaseException;
50 6241 cjones
import org.dataone.service.exceptions.IdentifierNotUnique;
51
import org.dataone.service.exceptions.InsufficientResources;
52 6174 cjones
import org.dataone.service.exceptions.InvalidRequest;
53 6241 cjones
import org.dataone.service.exceptions.InvalidSystemMetadata;
54 6174 cjones
import org.dataone.service.exceptions.InvalidToken;
55
import org.dataone.service.exceptions.NotAuthorized;
56
import org.dataone.service.exceptions.NotFound;
57
import org.dataone.service.exceptions.NotImplemented;
58
import org.dataone.service.exceptions.ServiceFailure;
59 6241 cjones
import org.dataone.service.exceptions.UnsupportedType;
60 6366 leinfelder
import org.dataone.service.types.v1.AccessRule;
61 6803 leinfelder
import org.dataone.service.types.v1.DescribeResponse;
62 7439 leinfelder
import org.dataone.service.types.v1.Group;
63 6366 leinfelder
import org.dataone.service.types.v1.Identifier;
64 9071 tao
import org.dataone.service.types.v1.ObjectFormatIdentifier;
65
import org.dataone.service.types.v1.ObjectList;
66 8810 leinfelder
import org.dataone.service.types.v2.Log;
67
import org.dataone.service.types.v2.Node;
68
import org.dataone.service.types.v1.Event;
69 6366 leinfelder
import org.dataone.service.types.v1.NodeReference;
70 6865 cjones
import org.dataone.service.types.v1.NodeType;
71 8810 leinfelder
import org.dataone.service.types.v2.ObjectFormat;
72 6366 leinfelder
import org.dataone.service.types.v1.Permission;
73 7132 cjones
import org.dataone.service.types.v1.Replica;
74 6366 leinfelder
import org.dataone.service.types.v1.Session;
75
import org.dataone.service.types.v1.Subject;
76 8810 leinfelder
import org.dataone.service.types.v2.SystemMetadata;
77 7146 leinfelder
import org.dataone.service.types.v1.util.AuthUtils;
78 6445 leinfelder
import org.dataone.service.types.v1.util.ChecksumUtil;
79 7439 leinfelder
import org.dataone.service.util.Constants;
80 6174 cjones
81 6241 cjones
import edu.ucsb.nceas.metacat.AccessionNumberException;
82
import edu.ucsb.nceas.metacat.DocumentImpl;
83 6186 leinfelder
import edu.ucsb.nceas.metacat.EventLog;
84
import edu.ucsb.nceas.metacat.IdentifierManager;
85 6194 leinfelder
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
86 6226 cjones
import edu.ucsb.nceas.metacat.MetacatHandler;
87 7077 leinfelder
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
88 6803 leinfelder
import edu.ucsb.nceas.metacat.database.DBConnection;
89
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
90 6447 leinfelder
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService;
91 8464 leinfelder
import edu.ucsb.nceas.metacat.index.MetacatSolrIndex;
92 6226 cjones
import edu.ucsb.nceas.metacat.properties.PropertyService;
93 6241 cjones
import edu.ucsb.nceas.metacat.replication.ForceReplicationHandler;
94 6226 cjones
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
95 6186 leinfelder
96 6174 cjones
public abstract class D1NodeService {
97 8903 tao
98
  public static final String DELETEDMESSAGE = "The object with the PID has been deleted from the node.";
99 6227 cjones
100 6226 cjones
  private static Logger logMetacat = Logger.getLogger(D1NodeService.class);
101 6186 leinfelder
102 6542 leinfelder
  /** For logging the operations */
103
  protected HttpServletRequest request;
104 6241 cjones
105 6226 cjones
  /* reference to the metacat handler */
106 6389 leinfelder
  protected MetacatHandler handler;
107 6174 cjones
108 6226 cjones
  /* parameters set in the incoming request */
109
  private Hashtable<String, String[]> params;
110 7417 leinfelder
111
  /**
112 7439 leinfelder
   * limit paged results sets to a configured maximum
113
   */
114
  protected static int MAXIMUM_DB_RECORD_COUNT = 7000;
115
116
  static {
117
		try {
118
			MAXIMUM_DB_RECORD_COUNT = Integer.valueOf(PropertyService.getProperty("database.webResultsetSize"));
119
		} catch (Exception e) {
120
			logMetacat.warn("Could not set MAXIMUM_DB_RECORD_COUNT", e);
121
		}
122
	}
123
124
  /**
125 7417 leinfelder
   * out-of-band session object to be used when not passed in as a method parameter
126
   */
127
  protected Session session;
128 6226 cjones
129 6241 cjones
  /**
130
   * Constructor - used to set the metacatUrl from a subclass extending D1NodeService
131
   *
132
   * @param metacatUrl - the URL of the metacat service, including the ending /d1
133
   */
134 6542 leinfelder
  public D1NodeService(HttpServletRequest request) {
135
		this.request = request;
136
	}
137 7417 leinfelder
138 6241 cjones
  /**
139 7417 leinfelder
   * retrieve the out-of-band session
140
   * @return
141
   */
142
  	public Session getSession() {
143
		return session;
144
	}
145
146
  	/**
147
  	 * Set the out-of-band session
148
  	 * @param session
149
  	 */
150
	public void setSession(Session session) {
151
		this.session = session;
152
	}
153
154
  /**
155 6803 leinfelder
   * This method provides a lighter weight mechanism than
156
   * getSystemMetadata() for a client to determine basic
157
   * properties of the referenced object.
158
   *
159
   * @param session - the Session object containing the credentials for the Subject
160
   * @param pid - the identifier of the object to be described
161
   *
162
   * @return describeResponse - A set of values providing a basic description
163
   *                            of the object.
164
   *
165
   * @throws InvalidToken
166
   * @throws ServiceFailure
167
   * @throws NotAuthorized
168
   * @throws NotFound
169
   * @throws NotImplemented
170
   * @throws InvalidRequest
171
   */
172
  public DescribeResponse describe(Session session, Identifier pid)
173
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
174 9036 tao
175
      String serviceFailureCode = "4931";
176
      Identifier sid = getPIDForSID(pid, serviceFailureCode);
177
      if(sid != null) {
178
          pid = sid;
179
      }
180 6803 leinfelder
181
    // get system metadata and construct the describe response
182
      SystemMetadata sysmeta = getSystemMetadata(session, pid);
183
      DescribeResponse describeResponse =
184
      	new DescribeResponse(sysmeta.getFormatId(), sysmeta.getSize(),
185
      			sysmeta.getDateSysMetadataModified(),
186
      			sysmeta.getChecksum(), sysmeta.getSerialVersion());
187
188
      return describeResponse;
189
190
  }
191
192
  /**
193 7077 leinfelder
   * Deletes an object from the Member Node, where the object is either a
194
   * data object or a science metadata object.
195
   *
196
   * @param session - the Session object containing the credentials for the Subject
197
   * @param pid - The object identifier to be deleted
198
   *
199
   * @return pid - the identifier of the object used for the deletion
200
   *
201
   * @throws InvalidToken
202
   * @throws ServiceFailure
203
   * @throws NotAuthorized
204
   * @throws NotFound
205
   * @throws NotImplemented
206
   * @throws InvalidRequest
207
   */
208
  public Identifier delete(Session session, Identifier pid)
209
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
210 9050 tao
211 7077 leinfelder
      String localId = null;
212
      if (session == null) {
213
      	throw new InvalidToken("1330", "No session has been provided");
214
      }
215 7157 leinfelder
      // just for logging purposes
216
      String username = session.getSubject().getValue();
217 7077 leinfelder
218
      // do we have a valid pid?
219
      if (pid == null || pid.getValue().trim().equals("")) {
220
          throw new ServiceFailure("1350", "The provided identifier was invalid.");
221
      }
222
223
      // check for the existing identifier
224
      try {
225
          localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
226
      } catch (McdbDocNotFoundException e) {
227
          throw new NotFound("1340", "The object with the provided " + "identifier was not found.");
228 9024 tao
      } catch (SQLException e) {
229
          throw new ServiceFailure("1350", "The object with the provided " + "identifier "+pid.getValue()+" couldn't be identified since "+e.getMessage());
230 7077 leinfelder
      }
231 7157 leinfelder
232 7077 leinfelder
      try {
233 7157 leinfelder
          // delete the document, as admin
234
          DocumentImpl.delete(localId, null, null, null, true);
235
          EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, Event.DELETE.xmlValue());
236
237
          // archive it
238 7430 leinfelder
          // DocumentImpl.delete() now sets this
239
          // see https://redmine.dataone.org/issues/3406
240
//          SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
241
//          sysMeta.setArchived(true);
242
//          sysMeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
243
//          HazelcastService.getInstance().getSystemMetadataMap().put(pid, sysMeta);
244 7077 leinfelder
245 7157 leinfelder
      } catch (McdbDocNotFoundException e) {
246
          throw new NotFound("1340", "The provided identifier was invalid.");
247 7077 leinfelder
248 7157 leinfelder
      } catch (SQLException e) {
249
          throw new ServiceFailure("1350", "There was a problem deleting the object." + "The error message was: " + e.getMessage());
250 7077 leinfelder
251 7157 leinfelder
      } catch (InsufficientKarmaException e) {
252 7244 cjones
          if ( logMetacat.isDebugEnabled() ) {
253
              e.printStackTrace();
254
          }
255 7157 leinfelder
          throw new NotAuthorized("1320", "The provided identity does not have " + "permission to DELETE objects on the Member Node.");
256 7244 cjones
257 7157 leinfelder
      } catch (Exception e) { // for some reason DocumentImpl throws a general Exception
258
          throw new ServiceFailure("1350", "There was a problem deleting the object." + "The error message was: " + e.getMessage());
259 7077 leinfelder
      }
260
261
      return pid;
262
  }
263
264
  /**
265 6803 leinfelder
   * Low level, "are you alive" operation. A valid ping response is
266
   * indicated by a HTTP status of 200.
267
   *
268
   * @return true if the service is alive
269
   *
270
   * @throws NotImplemented
271
   * @throws ServiceFailure
272
   * @throws InsufficientResources
273
   */
274
  public Date ping()
275
      throws NotImplemented, ServiceFailure, InsufficientResources {
276
277
      // test if we can get a database connection
278
      int serialNumber = -1;
279
      DBConnection dbConn = null;
280
      try {
281
          dbConn = DBConnectionPool.getDBConnection("MNodeService.ping");
282
          serialNumber = dbConn.getCheckOutSerialNumber();
283
      } catch (SQLException e) {
284
      	ServiceFailure sf = new ServiceFailure("", e.getMessage());
285
      	sf.initCause(e);
286
          throw sf;
287
      } finally {
288
          // Return the database connection
289
          DBConnectionPool.returnDBConnection(dbConn, serialNumber);
290
      }
291
292
      return Calendar.getInstance().getTime();
293
  }
294
295
  /**
296 6241 cjones
   * Adds a new object to the Node, where the object is either a data
297
   * object or a science metadata object. This method is called by clients
298
   * to create new data objects on Member Nodes or internally for Coordinating
299
   * Nodes
300
   *
301
   * @param session - the Session object containing the credentials for the Subject
302
   * @param pid - The object identifier to be created
303
   * @param object - the object bytes
304
   * @param sysmeta - the system metadata that describes the object
305
   *
306
   * @return pid - the object identifier created
307
   *
308
   * @throws InvalidToken
309
   * @throws ServiceFailure
310
   * @throws NotAuthorized
311
   * @throws IdentifierNotUnique
312
   * @throws UnsupportedType
313
   * @throws InsufficientResources
314
   * @throws InvalidSystemMetadata
315
   * @throws NotImplemented
316
   * @throws InvalidRequest
317
   */
318
  public Identifier create(Session session, Identifier pid, InputStream object,
319
    SystemMetadata sysmeta)
320
    throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique,
321
    UnsupportedType, InsufficientResources, InvalidSystemMetadata,
322
    NotImplemented, InvalidRequest {
323 6174 cjones
324 6241 cjones
    Identifier resultPid = null;
325
    String localId = null;
326
    boolean allowed = false;
327
328 6530 leinfelder
    // check for null session
329
    if (session == null) {
330
    	throw new InvalidToken("4894", "Session is required to WRITE to the Node.");
331
    }
332
    Subject subject = session.getSubject();
333
334 6688 leinfelder
    Subject publicSubject = new Subject();
335
    publicSubject.setValue(Constants.SUBJECT_PUBLIC);
336
	// be sure the user is authenticated for create()
337 6518 leinfelder
    if (subject == null || subject.getValue() == null ||
338 6688 leinfelder
        subject.equals(publicSubject) ) {
339 6241 cjones
      throw new NotAuthorized("1100", "The provided identity does not have " +
340 6518 leinfelder
        "permission to WRITE to the Node.");
341 6241 cjones
342
    }
343 9068 tao
344 6241 cjones
    // verify that pid == SystemMetadata.getIdentifier()
345
    logMetacat.debug("Comparing pid|sysmeta_pid: " +
346
      pid.getValue() + "|" + sysmeta.getIdentifier().getValue());
347
    if (!pid.getValue().equals(sysmeta.getIdentifier().getValue())) {
348
        throw new InvalidSystemMetadata("1180",
349
            "The supplied system metadata is invalid. " +
350
            "The identifier " + pid.getValue() + " does not match identifier" +
351
            "in the system metadata identified by " +
352
            sysmeta.getIdentifier().getValue() + ".");
353
354
    }
355 9045 tao
356 6241 cjones
357 7122 leinfelder
    logMetacat.debug("Checking if identifier exists: " + pid.getValue());
358 6241 cjones
    // Check that the identifier does not already exist
359 9017 tao
    boolean idExists = false;
360
    try {
361
        idExists = IdentifierManager.getInstance().identifierExists(pid.getValue());
362
    } catch (SQLException e) {
363
        throw new ServiceFailure("1190",
364
                                "The requested identifier " + pid.getValue() +
365
                                " couldn't be determined if it is unique since : "+e.getMessage());
366
    }
367
    if (idExists) {
368 6278 leinfelder
	    	throw new IdentifierNotUnique("1120",
369
			          "The requested identifier " + pid.getValue() +
370
			          " is already used by another object and" +
371
			          "therefore can not be used for this object. Clients should choose" +
372
			          "a new identifier that is unique and retry the operation or " +
373 6518 leinfelder
			          "use CN.reserveIdentifier() to reserve one.");
374 6337 leinfelder
375 6241 cjones
    }
376 6518 leinfelder
377 9045 tao
378 6596 leinfelder
    // TODO: this probably needs to be refined more
379 6241 cjones
    try {
380
      allowed = isAuthorized(session, pid, Permission.WRITE);
381
382
    } catch (NotFound e) {
383
      // The identifier doesn't exist, writing should be fine.
384
      allowed = true;
385
    }
386
387 6445 leinfelder
    // verify checksum, only if we can reset the inputstream
388
    if (object.markSupported()) {
389 7122 leinfelder
        logMetacat.debug("Checking checksum for: " + pid.getValue());
390 6445 leinfelder
	    String checksumAlgorithm = sysmeta.getChecksum().getAlgorithm();
391
	    String checksumValue = sysmeta.getChecksum().getValue();
392
	    try {
393
			String computedChecksumValue = ChecksumUtil.checksum(object, checksumAlgorithm).getValue();
394
			// it's very important that we don't consume the stream
395
			object.reset();
396
			if (!computedChecksumValue.equals(checksumValue)) {
397 7122 leinfelder
			    logMetacat.error("Checksum for " + pid.getValue() + " does not match system metadata, computed = " + computedChecksumValue );
398 6445 leinfelder
				throw new InvalidSystemMetadata("4896", "Checksum given does not match that of the object");
399
			}
400
		} catch (Exception e) {
401
			String msg = "Error verifying checksum values";
402
	      	logMetacat.error(msg, e);
403
	        throw new ServiceFailure("1190", msg + ": " + e.getMessage());
404
		}
405
    } else {
406
    	logMetacat.warn("mark is not supported on the object's input stream - cannot verify checksum without consuming stream");
407
    }
408
409 6241 cjones
    // we have the go ahead
410
    if ( allowed ) {
411
412 7122 leinfelder
        logMetacat.debug("Allowed to insert: " + pid.getValue());
413
414 6241 cjones
      // Science metadata (XML) or science data object?
415
      // TODO: there are cases where certain object formats are science metadata
416
      // but are not XML (netCDF ...).  Handle this.
417
      if ( isScienceMetadata(sysmeta) ) {
418
419
        // CASE METADATA:
420 8948 tao
      	//String objectAsXML = "";
421 6241 cjones
        try {
422 8948 tao
	        //objectAsXML = IOUtils.toString(object, "UTF-8");
423
	        localId = insertOrUpdateDocument(object,"UTF-8", pid, session, "insert");
424 6241 cjones
	        //localId = im.getLocalId(pid.getValue());
425
426
        } catch (IOException e) {
427
        	String msg = "The Node is unable to create the object. " +
428
          "There was a problem converting the object to XML";
429
        	logMetacat.info(msg);
430
          throw new ServiceFailure("1190", msg + ": " + e.getMessage());
431
432
        }
433
434 6302 leinfelder
      } else {
435
436
	      // DEFAULT CASE: DATA (needs to be checked and completed)
437
	      localId = insertDataObject(object, pid, session);
438
      }
439
440 6241 cjones
    }
441 6313 leinfelder
442 7122 leinfelder
    logMetacat.debug("Done inserting new object: " + pid.getValue());
443
444 6313 leinfelder
    // save the sysmeta
445 6468 leinfelder
    try {
446 7812 leinfelder
    	// lock and unlock of the pid happens in the subclass
447 6468 leinfelder
    	HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
448 7812 leinfelder
    	// submit for indexing
449 8647 leinfelder
        MetacatSolrIndex.getInstance().submit(sysmeta.getIdentifier(), sysmeta, null, true);
450 8464 leinfelder
451 6468 leinfelder
    } catch (Exception e) {
452 6495 leinfelder
    	logMetacat.error("Problem creating system metadata: " + pid.getValue(), e);
453 6468 leinfelder
        throw new ServiceFailure("1190", e.getMessage());
454
	}
455 6302 leinfelder
456 6241 cjones
    // setting the resulting identifier failed
457 6302 leinfelder
    if (localId == null ) {
458
      throw new ServiceFailure("1190", "The Node is unable to create the object. ");
459 6241 cjones
    }
460 6313 leinfelder
461 6302 leinfelder
    resultPid = pid;
462
463 7122 leinfelder
    logMetacat.debug("create() complete for object: " + pid.getValue());
464
465 6241 cjones
    return resultPid;
466
  }
467
468 6227 cjones
  /**
469
   * Return the log records associated with a given event between the start and
470
   * end dates listed given a particular Subject listed in the Session
471
   *
472
   * @param session - the Session object containing the credentials for the Subject
473
   * @param fromDate - the start date of the desired log records
474
   * @param toDate - the end date of the desired log records
475
   * @param event - restrict log records of a specific event type
476
   * @param start - zero based offset from the first record in the
477
   *                set of matching log records. Used to assist with
478
   *                paging the response.
479
   * @param count - maximum number of log records to return in the response.
480
   *                Used to assist with paging the response.
481
   *
482
   * @return the desired log records
483
   *
484
   * @throws InvalidToken
485
   * @throws ServiceFailure
486
   * @throws NotAuthorized
487
   * @throws InvalidRequest
488
   * @throws NotImplemented
489
   */
490
  public Log getLogRecords(Session session, Date fromDate, Date toDate,
491 8810 leinfelder
      String event, String pidFilter, Integer start, Integer count) throws InvalidToken, ServiceFailure,
492 6227 cjones
      NotAuthorized, InvalidRequest, NotImplemented {
493 6174 cjones
494 7234 leinfelder
	  // only admin access to this method
495
	  // see https://redmine.dataone.org/issues/2855
496
	  if (!isAdminAuthorized(session)) {
497
		  throw new NotAuthorized("1460", "Only the CN or admin is allowed to harvest logs from this node");
498
	  }
499 9063 tao
    Log log = new Log();
500 6227 cjones
    IdentifierManager im = IdentifierManager.getInstance();
501
    EventLog el = EventLog.getInstance();
502 6353 cjones
    if ( fromDate == null ) {
503 6227 cjones
      logMetacat.debug("setting fromdate from null");
504
      fromDate = new Date(1);
505
    }
506 6353 cjones
    if ( toDate == null ) {
507 6227 cjones
      logMetacat.debug("setting todate from null");
508
      toDate = new Date();
509
    }
510 6186 leinfelder
511 6353 cjones
    if ( start == null ) {
512 7285 leinfelder
    	start = 0;
513 6353 cjones
    }
514
515
    if ( count == null ) {
516
    	count = 1000;
517
    }
518 7439 leinfelder
519
    // safeguard against large requests
520
    if (count > MAXIMUM_DB_RECORD_COUNT) {
521
    	count = MAXIMUM_DB_RECORD_COUNT;
522
    }
523 6353 cjones
524 7184 leinfelder
    String[] filterDocid = null;
525 9063 tao
    if (pidFilter != null && !pidFilter.trim().equals("")) {
526
        //check if the given identifier is a sid. If it is sid, choose the current pid of the sid.
527
        Identifier pid = new Identifier();
528
        pid.setValue(pidFilter);
529
        String serviceFailureCode = "1490";
530
        Identifier sid = getPIDForSID(pid, serviceFailureCode);
531
        if(sid != null) {
532
            pid = sid;
533
        }
534
        pidFilter = pid.getValue();
535 7184 leinfelder
		try {
536
	      String localId = im.getLocalId(pidFilter);
537
	      filterDocid = new String[] {localId};
538
	    } catch (Exception ex) {
539
	    	String msg = "Could not find localId for given pidFilter '" + pidFilter + "'";
540
	        logMetacat.warn(msg, ex);
541
	        //throw new InvalidRequest("1480", msg);
542 9063 tao
	        return log; //return 0 record
543 7184 leinfelder
	    }
544
    }
545
546 6227 cjones
    logMetacat.debug("fromDate: " + fromDate);
547
    logMetacat.debug("toDate: " + toDate);
548 6186 leinfelder
549 9063 tao
    log = el.getD1Report(null, null, filterDocid, event,
550 6227 cjones
        new java.sql.Timestamp(fromDate.getTime()),
551 7285 leinfelder
        new java.sql.Timestamp(toDate.getTime()), false, start, count);
552 7234 leinfelder
553 6227 cjones
    logMetacat.info("getLogRecords");
554
    return log;
555
  }
556 6241 cjones
557 6227 cjones
  /**
558
   * Return the object identified by the given object identifier
559
   *
560
   * @param session - the Session object containing the credentials for the Subject
561
   * @param pid - the object identifier for the given object
562
   *
563
   * TODO: The D1 Authorization API doesn't provide information on which
564
   * authentication system the Subject belongs to, and so it's not possible to
565
   * discern which Person or Group is a valid KNB LDAP DN.  Fix this.
566
   *
567
   * @return inputStream - the input stream of the given object
568
   *
569
   * @throws InvalidToken
570
   * @throws ServiceFailure
571
   * @throws NotAuthorized
572
   * @throws InvalidRequest
573
   * @throws NotImplemented
574
   */
575
  public InputStream get(Session session, Identifier pid)
576
    throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
577 6609 cjones
    NotImplemented {
578 6226 cjones
579 9033 tao
    String serviceFailureCode = "1030";
580
    Identifier sid = getPIDForSID(pid, serviceFailureCode);
581
    if(sid != null) {
582
        pid = sid;
583 9032 tao
    }
584
585 6227 cjones
    InputStream inputStream = null; // bytes to be returned
586 6226 cjones
    handler = new MetacatHandler(new Timer());
587 6227 cjones
    boolean allowed = false;
588
    String localId; // the metacat docid for the pid
589 6226 cjones
590 6227 cjones
    // get the local docid from Metacat
591
    try {
592
      localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
593
594 6226 cjones
    } catch (McdbDocNotFoundException e) {
595
      throw new NotFound("1020", "The object specified by " +
596 6227 cjones
                         pid.getValue() +
597 6283 leinfelder
                         " does not exist at this node.");
598 9024 tao
    } catch (SQLException e) {
599
        throw new ServiceFailure("1030", "The object specified by "+ pid.getValue()+
600
                                  " couldn't be identified at this node since "+e.getMessage());
601 6226 cjones
    }
602
603
    // check for authorization
604 7029 leinfelder
    try {
605
		allowed = isAuthorized(session, pid, Permission.READ);
606
	} catch (InvalidRequest e) {
607
		throw new ServiceFailure("1030", e.getDescription());
608
	}
609 6227 cjones
610
    // if the person is authorized, perform the read
611 6323 leinfelder
    if (allowed) {
612 6226 cjones
      try {
613 6323 leinfelder
        inputStream = handler.read(localId);
614 9023 tao
      } catch (McdbDocNotFoundException de) {
615
          String error ="";
616
          if(EventLog.getInstance().isDeleted(localId)) {
617
                error=DELETEDMESSAGE;
618
          }
619
          throw new NotFound("1020", "The object specified by " +
620
                           pid.getValue() +
621
                           " does not exist at this node. "+error);
622 6323 leinfelder
      } catch (Exception e) {
623 9023 tao
        throw new ServiceFailure("1030", "The object specified by " +
624 6226 cjones
            pid.getValue() +
625 9023 tao
            " could not be returned due to error: " +
626
            e.getMessage()+". ");
627 6226 cjones
      }
628 6227 cjones
    }
629 6226 cjones
630 6227 cjones
    // if we fail to set the input stream
631
    if ( inputStream == null ) {
632 9023 tao
        String error ="";
633
        if(EventLog.getInstance().isDeleted(localId)) {
634
              error=DELETEDMESSAGE;
635
        }
636
        throw new NotFound("1020", "The object specified by " +
637 6226 cjones
                         pid.getValue() +
638 9023 tao
                         " does not exist at this node. "+error);
639 6227 cjones
    }
640
641 6389 leinfelder
	// log the read event
642 6532 leinfelder
    String principal = Constants.SUBJECT_PUBLIC;
643 6421 leinfelder
    if (session != null && session.getSubject() != null) {
644 6389 leinfelder
    	principal = session.getSubject().getValue();
645
    }
646 6542 leinfelder
    EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), principal, localId, "read");
647 6389 leinfelder
648 6227 cjones
    return inputStream;
649
  }
650 6174 cjones
651 6227 cjones
  /**
652
   * Return the system metadata for a given object
653
   *
654
   * @param session - the Session object containing the credentials for the Subject
655
   * @param pid - the object identifier for the given object
656
   *
657
   * @return inputStream - the input stream of the given system metadata object
658
   *
659
   * @throws InvalidToken
660
   * @throws ServiceFailure
661
   * @throws NotAuthorized
662
   * @throws NotFound
663
   * @throws InvalidRequest
664
   * @throws NotImplemented
665
   */
666 7132 cjones
    public SystemMetadata getSystemMetadata(Session session, Identifier pid)
667
        throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
668
        NotImplemented {
669
670 9037 tao
        String serviceFailureCode = "1090";
671
        Identifier sid = getPIDForSID(pid, serviceFailureCode);
672
        if(sid != null) {
673
            pid = sid;
674
        }
675 7132 cjones
        boolean isAuthorized = false;
676
        SystemMetadata systemMetadata = null;
677
        List<Replica> replicaList = null;
678
        NodeReference replicaNodeRef = null;
679
        List<Node> nodeListBySubject = null;
680
        Subject subject = null;
681 6572 cjones
682 7132 cjones
        if (session != null ) {
683
            subject = session.getSubject();
684
        }
685 6572 cjones
686 7145 leinfelder
        // check normal authorization
687
        BaseException originalAuthorizationException = null;
688
        if (!isAuthorized) {
689
            try {
690
                isAuthorized = isAuthorized(session, pid, Permission.READ);
691 7132 cjones
692 7145 leinfelder
            } catch (InvalidRequest e) {
693
                throw new ServiceFailure("1090", e.getDescription());
694
            } catch (NotAuthorized nae) {
695
            	// catch this for later
696
            	originalAuthorizationException = nae;
697
			}
698
        }
699 7132 cjones
700 7145 leinfelder
        // get the system metadata first because we need the replica list for auth
701
        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
702
703
        // check the replica information to expand access to MNs that might need it
704
        if (!isAuthorized) {
705
706
	        try {
707
708
	            // if MNs are listed as replicas, allow access
709
	            if ( systemMetadata != null ) {
710
	                replicaList = systemMetadata.getReplicaList();
711
	                // only check if there are in fact replicas listed
712
	                if ( replicaList != null ) {
713
714
	                    if ( subject != null ) {
715
	                        // get the list of nodes with a matching node subject
716
	                        try {
717
	                            nodeListBySubject = listNodesBySubject(session.getSubject());
718
719
	                        } catch (BaseException e) {
720
	                            // Unexpected error contacting the CN via D1Client
721
	                            String msg = "Caught an unexpected error while trying "
722
	                                    + "to potentially authorize system metadata access "
723
	                                    + "based on the session subject. The error was "
724
	                                    + e.getMessage();
725
	                            logMetacat.error(msg);
726
	                            if (logMetacat.isDebugEnabled()) {
727
	                                e.printStackTrace();
728
729
	                            }
730
	                            // isAuthorized is still false
731
	                        }
732
733
	                    }
734
	                    if (nodeListBySubject != null) {
735
	                        // compare node ids to replica node ids
736
	                        outer: for (Replica replica : replicaList) {
737
	                            replicaNodeRef = replica.getReplicaMemberNode();
738
739
	                            for (Node node : nodeListBySubject) {
740
	                                if (node.getIdentifier().equals(replicaNodeRef)) {
741
	                                    // node id via session subject matches a replica node
742
	                                    isAuthorized = true;
743
	                                    break outer;
744
	                                }
745
	                            }
746
	                        }
747
	                    }
748
	                }
749
	            }
750
751
	            // if we still aren't authorized, then we are done
752
	            if (!isAuthorized) {
753
	                throw new NotAuthorized("1400", Permission.READ
754
	                        + " not allowed on " + pid.getValue());
755
	            }
756 7132 cjones
757 7145 leinfelder
	        } catch (RuntimeException e) {
758
	        	e.printStackTrace();
759
	            // convert hazelcast RuntimeException to ServiceFailure
760
	            throw new ServiceFailure("1090", "Unexpected error getting system metadata for: " +
761
	                pid.getValue());
762
	        }
763
764 7132 cjones
        }
765 7145 leinfelder
766 7132 cjones
        // It wasn't in the map
767
        if ( systemMetadata == null ) {
768 8903 tao
            String error ="";
769
            String localId = null;
770
            try {
771
                localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
772
773
             } catch (Exception e) {
774
                logMetacat.warn("Couldn't find the local id for the pid "+pid.getValue());
775
            }
776
777
            if(localId != null && EventLog.getInstance().isDeleted(localId)) {
778
                error = DELETEDMESSAGE;
779 8971 tao
            } else if (localId == null && EventLog.getInstance().isDeleted(pid.getValue())) {
780
                error = DELETEDMESSAGE;
781 8903 tao
            }
782
            throw new NotFound("1420", "No record found for: " + pid.getValue()+". "+error);
783 7132 cjones
        }
784
785
        return systemMetadata;
786
    }
787 6572 cjones
788 8360 tao
789
    /**
790
     * Test if the specified session represents the authoritative member node for the
791
     * given object specified by the identifier. According the the DataONE documentation,
792
     * the authoritative member node has all the rights of the *rightsHolder*.
793
     * @param session - the Session object containing the credentials for the Subject
794
     * @param pid - the Identifier of the data object
795
     * @return true if the session represents the authoritative mn.
796
     * @throws ServiceFailure
797
     * @throws NotImplemented
798
     */
799
    public boolean isAuthoritativeMNodeAdmin(Session session, Identifier pid) {
800
        boolean allowed = false;
801
        //check the parameters
802
        if(session == null) {
803
            logMetacat.debug("D1NodeService.isAuthoritativeMNodeAdmin - the session object is null and return false.");
804
            return allowed;
805
        } else if (pid == null || pid.getValue() == null || pid.getValue().trim().equals("")) {
806
            logMetacat.debug("D1NodeService.isAuthoritativeMNodeAdmin - the Identifier object is null (not being specified) and return false.");
807
            return allowed;
808
        }
809
810
        //Get the subject from the session
811
        Subject subject = session.getSubject();
812
        if(subject != null) {
813
            //Get the authoritative member node info from the system metadata
814
            SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
815
            if(sysMeta != null) {
816
                NodeReference authoritativeMNode = sysMeta.getAuthoritativeMemberNode();
817
                if(authoritativeMNode != null) {
818
                        CNode cn = null;
819
                        try {
820
                            cn = D1Client.getCN();
821 8810 leinfelder
                        } catch (BaseException e) {
822 8360 tao
                            logMetacat.error("D1NodeService.isAuthoritativeMNodeAdmin - couldn't connect to the CN since "+
823
                                            e.getDescription()+ ". The false value will be returned for the AuthoritativeMNodeAdmin.");
824
                            return allowed;
825
                        }
826
827
                        if(cn != null) {
828
                            List<Node> nodes = null;
829
                            try {
830
                                nodes = cn.listNodes().getNodeList();
831
                            } catch (NotImplemented e) {
832
                                logMetacat.error("D1NodeService.isAuthoritativeMNodeAdmin - couldn't get the member nodes list from the CN since "+e.getDescription()+
833
                                                ". The false value will be returned for the AuthoritativeMNodeAdmin.");
834
                                return allowed;
835
                            } catch (ServiceFailure ee) {
836
                                logMetacat.error("D1NodeService.isAuthoritativeMNodeAdmin - couldn't get the member nodes list from the CN since "+ee.getDescription()+
837
                                                ". The false value will be returned for the AuthoritativeMNodeAdmin.");
838
                                return allowed;
839
                            }
840
                            if(nodes != null) {
841
                                for(Node node : nodes) {
842
                                    //find the authoritative node and get its subjects
843
                                    if (node.getType() == NodeType.MN && node.getIdentifier() != null && node.getIdentifier().equals(authoritativeMNode)) {
844
                                        List<Subject> nodeSubjects = node.getSubjectList();
845
                                        if(nodeSubjects != null) {
846
                                            // check if the session subject is in the node subject list
847
                                            for (Subject nodeSubject : nodeSubjects) {
848
                                                logMetacat.debug("D1NodeService.isAuthoritativeMNodeAdmin(), comparing subjects: " +
849
                                                    nodeSubject.getValue() + " and " + subject.getValue());
850
                                                if ( nodeSubject != null && nodeSubject.equals(subject) ) {
851
                                                    allowed = true; // subject of session == target node subject
852
                                                    break;
853
                                                }
854
                                            }
855
                                        }
856
857
                                    }
858
                                }
859
                            }
860
                        }
861
                }
862
            }
863
        }
864
        return allowed;
865
    }
866
867
868 6227 cjones
  /**
869 6865 cjones
   * Test if the user identified by the provided token has administrative authorization
870
   *
871
   * @param session - the Session object containing the credentials for the Subject
872
   *
873 7142 leinfelder
   * @return true if the user is admin
874 6865 cjones
   *
875
   * @throws ServiceFailure
876
   * @throws InvalidToken
877
   * @throws NotFound
878
   * @throws NotAuthorized
879
   * @throws NotImplemented
880
   */
881 7142 leinfelder
  public boolean isAdminAuthorized(Session session)
882 7079 leinfelder
      throws ServiceFailure, InvalidToken, NotAuthorized,
883 6865 cjones
      NotImplemented {
884
885
      boolean allowed = false;
886 7069 leinfelder
887
      // must have a session in order to check admin
888
      if (session == null) {
889 7142 leinfelder
         logMetacat.debug("In isAdminAuthorized(), session is null ");
890
         return false;
891 7069 leinfelder
      }
892
893 7072 cjones
      logMetacat.debug("In isAdminAuthorized(), checking CN or MN authorization for " +
894 7142 leinfelder
           session.getSubject().getValue());
895 7072 cjones
896 7330 leinfelder
      // check if this is the node calling itself (MN)
897
      allowed = isNodeAdmin(session);
898 6865 cjones
899 7330 leinfelder
      // check the CN list
900
      if (!allowed) {
901 8826 leinfelder
	      List<Node> nodes = null;
902
903
    	  try {
904
		      // are we allowed to do this? only CNs are allowed
905
		      CNode cn = D1Client.getCN();
906
		      nodes = cn.listNodes().getNodeList();
907
    	  }
908
	      catch (Throwable e) {
909
	    	  logMetacat.warn(e.getMessage());
910
	    	  return false;
911
	      }
912
913 7330 leinfelder
	      if ( nodes == null ) {
914 8826 leinfelder
	    	  return false;
915
	          //throw new ServiceFailure("4852", "Couldn't get node list.");
916 7330 leinfelder
	      }
917
918
	      // find the node in the node list
919
	      for ( Node node : nodes ) {
920
921
	          NodeReference nodeReference = node.getIdentifier();
922
	          logMetacat.debug("In isAdminAuthorized(), Node reference is: " + nodeReference.getValue());
923
924
	          Subject subject = session.getSubject();
925
926
	          if (node.getType() == NodeType.CN) {
927
	              List<Subject> nodeSubjects = node.getSubjectList();
928
929
	              // check if the session subject is in the node subject list
930
	              for (Subject nodeSubject : nodeSubjects) {
931
	                  logMetacat.debug("In isAdminAuthorized(), comparing subjects: " +
932
	                      nodeSubject.getValue() + " and " + subject.getValue());
933
	                  if ( nodeSubject.equals(subject) ) {
934
	                      allowed = true; // subject of session == target node subject
935
	                      break;
936
937
	                  }
938
	              }
939
	          }
940
	      }
941 6865 cjones
      }
942
943
      return allowed;
944
  }
945
946
  /**
947 7162 leinfelder
   * Test if the user identified by the provided token has administrative authorization
948
   * on this node because they are calling themselves
949
   *
950
   * @param session - the Session object containing the credentials for the Subject
951
   *
952
   * @return true if the user is this node
953
   * @throws ServiceFailure
954
   * @throws NotImplemented
955
   */
956
  public boolean isNodeAdmin(Session session) throws NotImplemented, ServiceFailure {
957
958
      boolean allowed = false;
959
960
      // must have a session in order to check admin
961
      if (session == null) {
962
         logMetacat.debug("In isNodeAdmin(), session is null ");
963
         return false;
964
      }
965
966
      logMetacat.debug("In isNodeAdmin(), MN authorization for " +
967
           session.getSubject().getValue());
968
969
      Node node = MNodeService.getInstance(request).getCapabilities();
970
      NodeReference nodeReference = node.getIdentifier();
971
      logMetacat.debug("In isNodeAdmin(), Node reference is: " + nodeReference.getValue());
972
973
      Subject subject = session.getSubject();
974
975
      if (node.getType() == NodeType.MN) {
976
          List<Subject> nodeSubjects = node.getSubjectList();
977
978
          // check if the session subject is in the node subject list
979
          for (Subject nodeSubject : nodeSubjects) {
980
              logMetacat.debug("In isNodeAdmin(), comparing subjects: " +
981
                  nodeSubject.getValue() + " and " + subject.getValue());
982
              if ( nodeSubject.equals(subject) ) {
983
                  allowed = true; // subject of session == this node's subect
984
                  break;
985
              }
986
          }
987
      }
988
989
      return allowed;
990
  }
991
992
  /**
993 6227 cjones
   * Test if the user identified by the provided token has authorization
994 6865 cjones
   * for the operation on the specified object.
995 6227 cjones
   *
996
   * @param session - the Session object containing the credentials for the Subject
997
   * @param pid - The identifer of the resource for which access is being checked
998
   * @param operation - The type of operation which is being requested for the given pid
999
   *
1000
   * @return true if the operation is allowed
1001
   *
1002
   * @throws ServiceFailure
1003
   * @throws InvalidToken
1004
   * @throws NotFound
1005
   * @throws NotAuthorized
1006
   * @throws NotImplemented
1007
   * @throws InvalidRequest
1008
   */
1009
  public boolean isAuthorized(Session session, Identifier pid, Permission permission)
1010
    throws ServiceFailure, InvalidToken, NotFound, NotAuthorized,
1011 7029 leinfelder
    NotImplemented, InvalidRequest {
1012 6174 cjones
1013 6227 cjones
    boolean allowed = false;
1014
1015 7029 leinfelder
    if (permission == null) {
1016
    	throw new InvalidRequest("1761", "Permission was not provided or is invalid");
1017
    }
1018
1019 6816 leinfelder
    // permissions are hierarchical
1020 6830 leinfelder
    List<Permission> expandedPermissions = null;
1021 6816 leinfelder
1022 7067 cjones
    // always allow CN access
1023 7142 leinfelder
    if ( isAdminAuthorized(session) ) {
1024 7067 cjones
        allowed = true;
1025
        return allowed;
1026
1027
    }
1028
1029 9051 tao
    String serviceFailureCode = "1760";
1030
    Identifier sid = getPIDForSID(pid, serviceFailureCode);
1031
    if(sid != null) {
1032
        pid = sid;
1033
    }
1034
1035 8360 tao
    // the authoritative member node of the pid always has the access as well.
1036
    if (isAuthoritativeMNodeAdmin(session, pid)) {
1037
        allowed = true;
1038
        return allowed;
1039
    }
1040
1041 7000 leinfelder
    // get the subject[s] from the session
1042 7146 leinfelder
	//defer to the shared util for recursively compiling the subjects
1043 7149 leinfelder
	Set<Subject> subjects = AuthUtils.authorizedClientSubjects(session);
1044 6293 leinfelder
1045 7398 leinfelder
	// track the identities we have checked against
1046
	StringBuffer includedSubjects = new StringBuffer();
1047
1048 6227 cjones
    // get the system metadata
1049 6241 cjones
    String pidStr = pid.getValue();
1050 6227 cjones
    SystemMetadata systemMetadata = null;
1051
    try {
1052 6574 cjones
        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1053
1054
    } catch (Exception e) {
1055
        // convert Hazelcast RuntimeException to NotFound
1056
        logMetacat.error("An error occurred while getting system metadata for identifier " +
1057
            pid.getValue() + ". The error message was: " + e.getMessage());
1058
        throw new NotFound("1800", "No record found for " + pidStr);
1059
1060 6858 cjones
    }
1061 6596 leinfelder
1062
    // throw not found if it was not found
1063
    if (systemMetadata == null) {
1064 8901 tao
        String localId = null;
1065
        String error = "No system metadata could be found for given PID: " + pidStr;
1066
        try {
1067
            localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1068
1069
         } catch (Exception e) {
1070
            logMetacat.warn("Couldn't find the local id for the pid "+pidStr);
1071
        }
1072
1073
        if(localId != null && EventLog.getInstance().isDeleted(localId)) {
1074 8903 tao
            error = error + ". "+DELETEDMESSAGE;
1075 8971 tao
        } else if (localId == null && EventLog.getInstance().isDeleted(pid.getValue())) {
1076
            error = error + ". "+DELETEDMESSAGE;
1077 8901 tao
        }
1078
        throw new NotFound("1800", error);
1079 6596 leinfelder
    }
1080 6245 leinfelder
1081 6293 leinfelder
    // do we own it?
1082
    for (Subject s: subjects) {
1083 7039 cjones
      logMetacat.debug("Comparing \t" +
1084
                       systemMetadata.getRightsHolder().getValue() +
1085
                       " \tagainst \t" + s.getValue());
1086 7398 leinfelder
      	includedSubjects.append(s.getValue() + "; ");
1087 6688 leinfelder
    	allowed = systemMetadata.getRightsHolder().equals(s);
1088 6293 leinfelder
    	if (allowed) {
1089
    		return allowed;
1090
    	}
1091
    }
1092
1093
    // otherwise check the access rules
1094 6245 leinfelder
    try {
1095
	    List<AccessRule> allows = systemMetadata.getAccessPolicy().getAllowList();
1096 6293 leinfelder
	    search: // label break
1097 6245 leinfelder
	    for (AccessRule accessRule: allows) {
1098
	      for (Subject s: subjects) {
1099 7059 leinfelder
	        logMetacat.debug("Checking allow access rule for subject: " + s.getValue());
1100 6816 leinfelder
	        if (accessRule.getSubjectList().contains(s)) {
1101 7059 leinfelder
	        	logMetacat.debug("Access rule contains subject: " + s.getValue());
1102 6830 leinfelder
	        	for (Permission p: accessRule.getPermissionList()) {
1103 7059 leinfelder
		        	logMetacat.debug("Checking permission: " + p.xmlValue());
1104 6830 leinfelder
	        		expandedPermissions = expandPermissions(p);
1105
	        		allowed = expandedPermissions.contains(permission);
1106
	        		if (allowed) {
1107 7059 leinfelder
			        	logMetacat.info("Permission granted: " + p.xmlValue() + " to " + s.getValue());
1108 6830 leinfelder
	        			break search; //label break
1109
	        		}
1110 6816 leinfelder
	        	}
1111
1112 6245 leinfelder
	        }
1113
	      }
1114
	    }
1115
    } catch (Exception e) {
1116
    	// catch all for errors - safe side should be to deny the access
1117
    	logMetacat.error("Problem checking authorization - defaulting to deny", e);
1118
		allowed = false;
1119 7039 cjones
1120
    }
1121 6227 cjones
1122 6245 leinfelder
    // throw or return?
1123 6227 cjones
    if (!allowed) {
1124 7398 leinfelder
      throw new NotAuthorized("1820", permission + " not allowed on " + pidStr + " for subject[s]: " + includedSubjects.toString() );
1125 6227 cjones
    }
1126 6245 leinfelder
1127 6227 cjones
    return allowed;
1128 6241 cjones
1129 6227 cjones
  }
1130
1131 6256 cjones
  /*
1132 6227 cjones
   * parse a logEntry and get the relevant field from it
1133
   *
1134
   * @param fieldname
1135
   * @param entry
1136
   * @return
1137
   */
1138
  private String getLogEntryField(String fieldname, String entry) {
1139
    String begin = "<" + fieldname + ">";
1140
    String end = "</" + fieldname + ">";
1141
    // logMetacat.debug("looking for " + begin + " and " + end +
1142
    // " in entry " + entry);
1143
    String s = entry.substring(entry.indexOf(begin) + begin.length(), entry
1144
        .indexOf(end));
1145
    logMetacat.debug("entry " + fieldname + " : " + s);
1146
    return s;
1147
  }
1148 6174 cjones
1149 6257 cjones
  /**
1150 6241 cjones
   * Determine if a given object should be treated as an XML science metadata
1151
   * object.
1152
   *
1153
   * @param sysmeta - the SystemMetadata describing the object
1154
   * @return true if the object should be treated as science metadata
1155
   */
1156 6433 leinfelder
  public static boolean isScienceMetadata(SystemMetadata sysmeta) {
1157 6241 cjones
1158
    ObjectFormat objectFormat = null;
1159
    boolean isScienceMetadata = false;
1160
1161
    try {
1162 6561 leinfelder
      objectFormat = ObjectFormatCache.getInstance().getFormat(sysmeta.getFormatId());
1163 6433 leinfelder
      if ( objectFormat.getFormatType().equals("METADATA") ) {
1164 6402 cjones
      	isScienceMetadata = true;
1165
1166
      }
1167 6241 cjones
1168
1169
    } catch (ServiceFailure e) {
1170
      logMetacat.debug("There was a problem determining if the object identified by" +
1171
          sysmeta.getIdentifier().getValue() +
1172
          " is science metadata: " + e.getMessage());
1173
1174
    } catch (NotFound e) {
1175
      logMetacat.debug("There was a problem determining if the object identified by" +
1176
          sysmeta.getIdentifier().getValue() +
1177
          " is science metadata: " + e.getMessage());
1178
1179
    }
1180
1181
    return isScienceMetadata;
1182 6226 cjones
1183 6241 cjones
  }
1184
1185
  /**
1186 7315 leinfelder
   * Check fro whitespace in the given pid.
1187
   * null pids are also invalid by default
1188
   * @param pid
1189
   * @return
1190
   */
1191
  public static boolean isValidIdentifier(Identifier pid) {
1192
	  if (pid != null && pid.getValue() != null && pid.getValue().length() > 0) {
1193 7816 leinfelder
		  return !pid.getValue().matches(".*\\s+.*");
1194 7315 leinfelder
	  }
1195
	  return false;
1196
  }
1197
1198
1199
  /**
1200 6241 cjones
   * Insert or update an XML document into Metacat
1201
   *
1202
   * @param xml - the XML document to insert or update
1203
   * @param pid - the identifier to be used for the resulting object
1204
   *
1205
   * @return localId - the resulting docid of the document created or updated
1206
   *
1207
   */
1208 8948 tao
  public String insertOrUpdateDocument(InputStream xml, String encoding,  Identifier pid,
1209 6241 cjones
    Session session, String insertOrUpdate)
1210 8948 tao
    throws ServiceFailure, IOException {
1211 6241 cjones
1212
  	logMetacat.debug("Starting to insert xml document...");
1213
    IdentifierManager im = IdentifierManager.getInstance();
1214
1215
    // generate pid/localId pair for sysmeta
1216
    String localId = null;
1217 8948 tao
    byte[] xmlBytes  = IOUtils.toByteArray(xml);
1218
    String xmlStr = new String(xmlBytes, encoding);
1219 6241 cjones
    if(insertOrUpdate.equals("insert")) {
1220
      localId = im.generateLocalId(pid.getValue(), 1);
1221
1222
    } else {
1223
      //localid should already exist in the identifier table, so just find it
1224
      try {
1225
        logMetacat.debug("Updating pid " + pid.getValue());
1226
        logMetacat.debug("looking in identifier table for pid " + pid.getValue());
1227
1228
        localId = im.getLocalId(pid.getValue());
1229
1230
        logMetacat.debug("localId: " + localId);
1231
        //increment the revision
1232
        String docid = localId.substring(0, localId.lastIndexOf("."));
1233
        String revS = localId.substring(localId.lastIndexOf(".") + 1, localId.length());
1234
        int rev = new Integer(revS).intValue();
1235
        rev++;
1236
        docid = docid + "." + rev;
1237
        localId = docid;
1238
        logMetacat.debug("incremented localId: " + localId);
1239
1240
      } catch(McdbDocNotFoundException e) {
1241
        throw new ServiceFailure("1030", "D1NodeService.insertOrUpdateDocument(): " +
1242
            "pid " + pid.getValue() +
1243
            " should have been in the identifier table, but it wasn't: " +
1244
            e.getMessage());
1245
1246 9024 tao
      } catch (SQLException e) {
1247
          throw new ServiceFailure("1030", "D1NodeService.insertOrUpdateDocument() -"+
1248
                     " couldn't identify if the pid "+pid.getValue()+" is in the identifier table since "+e.getMessage());
1249 6241 cjones
      }
1250
1251
    }
1252
1253 6443 leinfelder
    params = new Hashtable<String, String[]>();
1254 6241 cjones
    String[] action = new String[1];
1255
    action[0] = insertOrUpdate;
1256
    params.put("action", action);
1257
    String[] docid = new String[1];
1258
    docid[0] = localId;
1259
    params.put("docid", docid);
1260
    String[] doctext = new String[1];
1261 8948 tao
    doctext[0] = xmlStr;
1262 6241 cjones
    params.put("doctext", doctext);
1263
1264 6532 leinfelder
    String username = Constants.SUBJECT_PUBLIC;
1265 6241 cjones
    String[] groupnames = null;
1266 6302 leinfelder
    if (session != null ) {
1267
    	username = session.getSubject().getValue();
1268 6532 leinfelder
    	if (session.getSubjectInfo() != null) {
1269
    		List<Group> groupList = session.getSubjectInfo().getGroupList();
1270 6302 leinfelder
    		if (groupList != null) {
1271
    			groupnames = new String[groupList.size()];
1272 8314 tao
    			for (int i = 0; i < groupList.size(); i++ ) {
1273 6302 leinfelder
    				groupnames[i] = groupList.get(i).getGroupName();
1274
    			}
1275
    		}
1276
    	}
1277 6241 cjones
    }
1278
1279
    // do the insert or update action
1280 6443 leinfelder
    handler = new MetacatHandler(new Timer());
1281 6542 leinfelder
    String result = handler.handleInsertOrUpdateAction(request.getRemoteAddr(), request.getHeader("User-Agent"), null,
1282 8948 tao
                        null, params, username, groupnames, false, false, xmlBytes);
1283 6241 cjones
1284
    if(result.indexOf("<error>") != -1) {
1285
    	String detailCode = "";
1286
    	if ( insertOrUpdate.equals("insert") ) {
1287 7017 leinfelder
    		// make sure to remove the mapping so that subsequent attempts do not fail with IdentifierNotUnique
1288
    		im.removeMapping(pid.getValue(), localId);
1289 6241 cjones
    		detailCode = "1190";
1290
1291
    	} else if ( insertOrUpdate.equals("update") ) {
1292
    		detailCode = "1310";
1293
1294
    	}
1295
        throw new ServiceFailure(detailCode,
1296
          "Error inserting or updating document: " + result);
1297
    }
1298
    logMetacat.debug("Finsished inserting xml document with id " + localId);
1299
1300
    return localId;
1301
  }
1302
1303
  /**
1304
   * Insert a data document
1305
   *
1306
   * @param object
1307
   * @param pid
1308
   * @param sessionData
1309
   * @throws ServiceFailure
1310
   * @returns localId of the data object inserted
1311
   */
1312 6807 leinfelder
  public String insertDataObject(InputStream object, Identifier pid,
1313 6241 cjones
          Session session) throws ServiceFailure {
1314
1315 6532 leinfelder
    String username = Constants.SUBJECT_PUBLIC;
1316 6241 cjones
    String[] groupnames = null;
1317 6302 leinfelder
    if (session != null ) {
1318
    	username = session.getSubject().getValue();
1319 6532 leinfelder
    	if (session.getSubjectInfo() != null) {
1320
    		List<Group> groupList = session.getSubjectInfo().getGroupList();
1321 6302 leinfelder
    		if (groupList != null) {
1322
    			groupnames = new String[groupList.size()];
1323 8314 tao
    			for (int i = 0; i < groupList.size(); i++ ) {
1324 6302 leinfelder
    				groupnames[i] = groupList.get(i).getGroupName();
1325
    			}
1326
    		}
1327
    	}
1328 6241 cjones
    }
1329
1330
    // generate pid/localId pair for object
1331
    logMetacat.debug("Generating a pid/localId mapping");
1332
    IdentifierManager im = IdentifierManager.getInstance();
1333
    String localId = im.generateLocalId(pid.getValue(), 1);
1334 6719 leinfelder
1335
    // Save the data file to disk using "localId" as the name
1336
    String datafilepath = null;
1337
	try {
1338
		datafilepath = PropertyService.getProperty("application.datafilepath");
1339
	} catch (PropertyNotFoundException e) {
1340
		ServiceFailure sf = new ServiceFailure("1190", "Lookup data file path" + e.getMessage());
1341
		sf.initCause(e);
1342
		throw sf;
1343
	}
1344
    boolean locked = false;
1345
	try {
1346
		locked = DocumentImpl.getDataFileLockGrant(localId);
1347
	} catch (Exception e) {
1348
		ServiceFailure sf = new ServiceFailure("1190", "Could not lock file for writing:" + e.getMessage());
1349
		sf.initCause(e);
1350
		throw sf;
1351
	}
1352
1353
    logMetacat.debug("Case DATA: starting to write to disk.");
1354
	if (locked) {
1355
1356 6241 cjones
          File dataDirectory = new File(datafilepath);
1357
          dataDirectory.mkdirs();
1358
1359
          File newFile = writeStreamToFile(dataDirectory, localId, object);
1360
1361
          // TODO: Check that the file size matches SystemMetadata
1362
          // long size = newFile.length();
1363
          // if (size == 0) {
1364
          //     throw new IOException("Uploaded file is 0 bytes!");
1365
          // }
1366
1367
          // Register the file in the database (which generates an exception
1368
          // if the localId is not acceptable or other untoward things happen
1369
          try {
1370
            logMetacat.debug("Registering document...");
1371
            DocumentImpl.registerDocument(localId, "BIN", localId,
1372
                    username, groupnames);
1373
            logMetacat.debug("Registration step completed.");
1374
1375
          } catch (SQLException e) {
1376
            //newFile.delete();
1377
            logMetacat.debug("SQLE: " + e.getMessage());
1378
            e.printStackTrace(System.out);
1379
            throw new ServiceFailure("1190", "Registration failed: " +
1380
            		e.getMessage());
1381
1382
          } catch (AccessionNumberException e) {
1383
            //newFile.delete();
1384
            logMetacat.debug("ANE: " + e.getMessage());
1385
            e.printStackTrace(System.out);
1386
            throw new ServiceFailure("1190", "Registration failed: " +
1387
            	e.getMessage());
1388
1389
          } catch (Exception e) {
1390
            //newFile.delete();
1391
            logMetacat.debug("Exception: " + e.getMessage());
1392
            e.printStackTrace(System.out);
1393
            throw new ServiceFailure("1190", "Registration failed: " +
1394
            	e.getMessage());
1395
          }
1396
1397
          logMetacat.debug("Logging the creation event.");
1398 6542 leinfelder
          EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, "create");
1399 6241 cjones
1400 7355 leinfelder
          // Schedule replication for this data file, the "insert" action is important here!
1401 6241 cjones
          logMetacat.debug("Scheduling replication.");
1402 7355 leinfelder
          ForceReplicationHandler frh = new ForceReplicationHandler(localId, "insert", false, null);
1403 6719 leinfelder
      }
1404
1405
      return localId;
1406 6241 cjones
1407
  }
1408 6255 cjones
1409 6256 cjones
  /**
1410
   * Insert a systemMetadata document and return its localId
1411
   */
1412 6807 leinfelder
  public void insertSystemMetadata(SystemMetadata sysmeta)
1413 6572 cjones
      throws ServiceFailure {
1414
1415
  	  logMetacat.debug("Starting to insert SystemMetadata...");
1416
      sysmeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1417
      logMetacat.debug("Inserting new system metadata with modified date " +
1418
          sysmeta.getDateSysMetadataModified());
1419
1420
      //insert the system metadata
1421
      try {
1422
        // note: the calling subclass handles the map hazelcast lock/unlock
1423
      	HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
1424 7812 leinfelder
      	// submit for indexing
1425 8647 leinfelder
        MetacatSolrIndex.getInstance().submit(sysmeta.getIdentifier(), sysmeta, null, true);
1426 6572 cjones
      } catch (Exception e) {
1427
          throw new ServiceFailure("1190", e.getMessage());
1428
1429
	    }
1430 6256 cjones
  }
1431 9071 tao
1432
  /**
1433
   * Retrieve the list of objects present on the MN that match the calling parameters
1434
   *
1435
   * @param session - the Session object containing the credentials for the Subject
1436
   * @param startTime - Specifies the beginning of the time range from which
1437
   *                    to return object (>=)
1438
   * @param endTime - Specifies the beginning of the time range from which
1439
   *                  to return object (>=)
1440
   * @param objectFormat - Restrict results to the specified object format
1441
   * @param replicaStatus - Indicates if replicated objects should be returned in the list
1442
   * @param start - The zero-based index of the first value, relative to the
1443
   *                first record of the resultset that matches the parameters.
1444
   * @param count - The maximum number of entries that should be returned in
1445
   *                the response. The Member Node may return less entries
1446
   *                than specified in this value.
1447
   *
1448
   * @return objectList - the list of objects matching the criteria
1449
   *
1450
   * @throws InvalidToken
1451
   * @throws ServiceFailure
1452
   * @throws NotAuthorized
1453
   * @throws InvalidRequest
1454
   * @throws NotImplemented
1455
   */
1456
  public ObjectList listObjects(Session session, Date startTime, Date endTime, ObjectFormatIdentifier objectFormatId, Identifier identifier, Boolean replicaStatus, Integer start,
1457
          Integer count) throws NotAuthorized, InvalidRequest, NotImplemented, ServiceFailure, InvalidToken {
1458 6256 cjones
1459 9071 tao
      ObjectList objectList = null;
1460
1461
      try {
1462
          // safeguard against large requests
1463
          if (count == null || count > MAXIMUM_DB_RECORD_COUNT) {
1464
              count = MAXIMUM_DB_RECORD_COUNT;
1465
          }
1466 9072 tao
          boolean isSid = false;
1467
          if(identifier != null) {
1468
              isSid = IdentifierManager.getInstance().systemMetadataSIDExists(identifier);
1469
          }
1470
          objectList = IdentifierManager.getInstance().querySystemMetadata(startTime, endTime, objectFormatId, replicaStatus, start, count, identifier, isSid);
1471 9071 tao
      } catch (Exception e) {
1472
          throw new ServiceFailure("1580", "Error querying system metadata: " + e.getMessage());
1473
      }
1474
1475
      return objectList;
1476
  }
1477
1478 6256 cjones
  /**
1479 6255 cjones
   * Update a systemMetadata document
1480
   *
1481
   * @param sysMeta - the system metadata object in the system to update
1482
   */
1483 6858 cjones
    protected void updateSystemMetadata(SystemMetadata sysMeta)
1484
        throws ServiceFailure {
1485
1486
        logMetacat.debug("D1NodeService.updateSystemMetadata() called.");
1487
        sysMeta.setDateSysMetadataModified(new Date());
1488
        try {
1489
            HazelcastService.getInstance().getSystemMetadataMap().lock(sysMeta.getIdentifier());
1490
            HazelcastService.getInstance().getSystemMetadataMap().put(sysMeta.getIdentifier(), sysMeta);
1491 7812 leinfelder
            // submit for indexing
1492 8647 leinfelder
            MetacatSolrIndex.getInstance().submit(sysMeta.getIdentifier(), sysMeta, null, true);
1493 6858 cjones
        } catch (Exception e) {
1494
            throw new ServiceFailure("4862", e.getMessage());
1495
1496
        } finally {
1497
            HazelcastService.getInstance().getSystemMetadataMap().unlock(sysMeta.getIdentifier());
1498
1499
        }
1500
1501
    }
1502 8810 leinfelder
1503
	public boolean updateSystemMetadata(Session session, Identifier pid,
1504
			SystemMetadata sysmeta) throws NotImplemented, NotAuthorized,
1505
			ServiceFailure, InvalidRequest, InvalidSystemMetadata, InvalidToken {
1506
1507
		// The lock to be used for this identifier
1508
      Lock lock = null;
1509
1510
      // TODO: control who can call this?
1511
      if (session == null) {
1512
          //TODO: many of the thrown exceptions do not use the correct error codes
1513
          //check these against the docs and correct them
1514
          throw new NotAuthorized("4861", "No Session - could not authorize for registration." +
1515
                  "  If you are not logged in, please do so and retry the request.");
1516
      }
1517
1518
      // verify that guid == SystemMetadata.getIdentifier()
1519
      logMetacat.debug("Comparing guid|sysmeta_guid: " + pid.getValue() +
1520
          "|" + sysmeta.getIdentifier().getValue());
1521
1522
      if (!pid.getValue().equals(sysmeta.getIdentifier().getValue())) {
1523
          throw new InvalidRequest("4863",
1524
              "The identifier in method call (" + pid.getValue() +
1525
              ") does not match identifier in system metadata (" +
1526
              sysmeta.getIdentifier().getValue() + ").");
1527
      }
1528
1529
      // do the actual update
1530
      this.updateSystemMetadata(sysmeta);
1531
1532
      try {
1533
    	  String localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1534
    	  EventLog.getInstance().log(request.getRemoteAddr(),
1535
    	          request.getHeader("User-Agent"), session.getSubject().getValue(),
1536
    	          localId, "updateSystemMetadata");
1537
      } catch (McdbDocNotFoundException e) {
1538
    	  // do nothing, no localId to log with
1539
    	  logMetacat.warn("Could not log 'updateSystemMetadata' event because no localId was found for pid: " + pid.getValue());
1540 9024 tao
      } catch (SQLException e) {
1541
          logMetacat.warn("Could not log 'updateSystemMetadata' event because the localId couldn't be identified for the pid: " + pid.getValue());
1542 8810 leinfelder
      }
1543
1544
      return true;
1545
	}
1546 6816 leinfelder
1547
  /**
1548
   * Given a Permission, returns a list of all permissions that it encompasses
1549
   * Permissions are hierarchical so that WRITE also allows READ.
1550
   * @param permission
1551
   * @return list of included Permissions for the given permission
1552
   */
1553
  protected List<Permission> expandPermissions(Permission permission) {
1554
	  	List<Permission> expandedPermissions = new ArrayList<Permission>();
1555
	    if (permission.equals(Permission.READ)) {
1556
	    	expandedPermissions.add(Permission.READ);
1557
	    }
1558
	    if (permission.equals(Permission.WRITE)) {
1559
	    	expandedPermissions.add(Permission.READ);
1560
	    	expandedPermissions.add(Permission.WRITE);
1561
	    }
1562
	    if (permission.equals(Permission.CHANGE_PERMISSION)) {
1563
	    	expandedPermissions.add(Permission.READ);
1564
	    	expandedPermissions.add(Permission.WRITE);
1565
	    	expandedPermissions.add(Permission.CHANGE_PERMISSION);
1566
	    }
1567
	    return expandedPermissions;
1568
  }
1569 6255 cjones
1570
  /*
1571 6241 cjones
   * Write a stream to a file
1572
   *
1573
   * @param dir - the directory to write to
1574
   * @param fileName - the file name to write to
1575
   * @param data - the object bytes as an input stream
1576
   *
1577
   * @return newFile - the new file created
1578
   *
1579
   * @throws ServiceFailure
1580
   */
1581
  private File writeStreamToFile(File dir, String fileName, InputStream data)
1582
    throws ServiceFailure {
1583
1584
    File newFile = new File(dir, fileName);
1585
    logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
1586
1587
    try {
1588
        if (newFile.createNewFile()) {
1589
          // write data stream to desired file
1590
          OutputStream os = new FileOutputStream(newFile);
1591
          long length = IOUtils.copyLarge(data, os);
1592
          os.flush();
1593
          os.close();
1594
        } else {
1595
          logMetacat.debug("File creation failed, or file already exists.");
1596
          throw new ServiceFailure("1190", "File already exists: " + fileName);
1597
        }
1598
    } catch (FileNotFoundException e) {
1599
      logMetacat.debug("FNF: " + e.getMessage());
1600
      throw new ServiceFailure("1190", "File not found: " + fileName + " "
1601
                + e.getMessage());
1602
    } catch (IOException e) {
1603
      logMetacat.debug("IOE: " + e.getMessage());
1604
      throw new ServiceFailure("1190", "File was not written: " + fileName
1605
                + " " + e.getMessage());
1606
    }
1607
1608
    return newFile;
1609
  }
1610 7132 cjones
1611
  /*
1612
   * Returns a list of nodes that have been registered with the DataONE infrastructure
1613
   * that match the given session subject
1614
   * @return nodes - List of nodes from the registry with a matching session subject
1615
   *
1616
   * @throws ServiceFailure
1617
   * @throws NotImplemented
1618
   */
1619
  protected List<Node> listNodesBySubject(Subject subject)
1620
      throws ServiceFailure, NotImplemented {
1621 7136 leinfelder
      List<Node> nodeList = new ArrayList<Node>();
1622 7132 cjones
1623
      CNode cn = D1Client.getCN();
1624
      List<Node> nodes = cn.listNodes().getNodeList();
1625
1626
      // find the node in the node list
1627
      for ( Node node : nodes ) {
1628
1629
          List<Subject> nodeSubjects = node.getSubjectList();
1630 7139 leinfelder
          if (nodeSubjects != null) {
1631
	          // check if the session subject is in the node subject list
1632
	          for (Subject nodeSubject : nodeSubjects) {
1633
	              if ( nodeSubject.equals(subject) ) { // subject of session == node subject
1634
	                  nodeList.add(node);
1635
	              }
1636
	          }
1637
          }
1638 7132 cjones
      }
1639
1640
      return nodeList;
1641
1642
  }
1643
1644 7159 leinfelder
  /**
1645 7148 leinfelder
   * Archives an object, where the object is either a
1646
   * data object or a science metadata object.
1647
   *
1648
   * @param session - the Session object containing the credentials for the Subject
1649
   * @param pid - The object identifier to be archived
1650
   *
1651
   * @return pid - the identifier of the object used for the archiving
1652
   *
1653
   * @throws InvalidToken
1654
   * @throws ServiceFailure
1655
   * @throws NotAuthorized
1656
   * @throws NotFound
1657
   * @throws NotImplemented
1658
   * @throws InvalidRequest
1659
   */
1660
  public Identifier archive(Session session, Identifier pid)
1661
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
1662 7132 cjones
1663 7148 leinfelder
      String localId = null;
1664
      boolean allowed = false;
1665
      String username = Constants.SUBJECT_PUBLIC;
1666
      String[] groupnames = null;
1667
      if (session == null) {
1668
      	throw new InvalidToken("1330", "No session has been provided");
1669
      } else {
1670
          username = session.getSubject().getValue();
1671
          if (session.getSubjectInfo() != null) {
1672
              List<Group> groupList = session.getSubjectInfo().getGroupList();
1673
              if (groupList != null) {
1674
                  groupnames = new String[groupList.size()];
1675 8314 tao
                  for (int i = 0; i < groupList.size(); i++) {
1676 7148 leinfelder
                      groupnames[i] = groupList.get(i).getGroupName();
1677
                  }
1678
              }
1679
          }
1680
      }
1681
1682
      // do we have a valid pid?
1683
      if (pid == null || pid.getValue().trim().equals("")) {
1684
          throw new ServiceFailure("1350", "The provided identifier was invalid.");
1685
      }
1686 9050 tao
1687
      String serviceFailureCode = "1350";
1688
      Identifier sid = getPIDForSID(pid, serviceFailureCode);
1689
      if(sid != null) {
1690
          pid = sid;
1691
      }
1692 7148 leinfelder
1693
      // check for the existing identifier
1694
      try {
1695
          localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1696
      } catch (McdbDocNotFoundException e) {
1697
          throw new NotFound("1340", "The object with the provided " + "identifier was not found.");
1698 9024 tao
      } catch (SQLException e) {
1699
          throw new ServiceFailure("1350", "The object with the provided identifier "+pid.getValue()+" couldn't be identified since "+e.getMessage());
1700 7148 leinfelder
      }
1701
1702
      // does the subject have archive (a D1 CHANGE_PERMISSION level) privileges on the pid?
1703
      try {
1704
			allowed = isAuthorized(session, pid, Permission.CHANGE_PERMISSION);
1705
		} catch (InvalidRequest e) {
1706
          throw new ServiceFailure("1350", e.getDescription());
1707
		}
1708
1709
1710
      if (allowed) {
1711
          try {
1712
              // archive the document
1713 7236 cjones
              DocumentImpl.delete(localId, null, null, null, false);
1714 7148 leinfelder
              EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, Event.DELETE.xmlValue());
1715
1716
              // archive it
1717
              SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1718
              sysMeta.setArchived(true);
1719 7156 leinfelder
              sysMeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1720 7148 leinfelder
              HazelcastService.getInstance().getSystemMetadataMap().put(pid, sysMeta);
1721 7812 leinfelder
              // submit for indexing
1722 7862 leinfelder
              // DocumentImpl call above should do this.
1723
              // see: https://projects.ecoinformatics.org/ecoinfo/issues/6030
1724
              //HazelcastService.getInstance().getIndexQueue().add(sysMeta);
1725 7148 leinfelder
1726
          } catch (McdbDocNotFoundException e) {
1727
              throw new NotFound("1340", "The provided identifier was invalid.");
1728
1729
          } catch (SQLException e) {
1730
              throw new ServiceFailure("1350", "There was a problem archiving the object." + "The error message was: " + e.getMessage());
1731
1732
          } catch (InsufficientKarmaException e) {
1733
              throw new NotAuthorized("1320", "The provided identity does not have " + "permission to archive this object.");
1734
1735
          } catch (Exception e) { // for some reason DocumentImpl throws a general Exception
1736
              throw new ServiceFailure("1350", "There was a problem archiving the object." + "The error message was: " + e.getMessage());
1737
          }
1738
1739
      } else {
1740
          throw new NotAuthorized("1320", "The provided identity does not have " + "permission to archive the object on the Node.");
1741
      }
1742
1743
      return pid;
1744
  }
1745 9033 tao
1746
1747
  /**
1748
   * A utility method for v1 api to check the specified identifier exists as a pid
1749
   * @param identifier  the specified identifier
1750
   * @param serviceFailureCode  the detail error code for the service failure exception
1751
   * @param noFoundCode  the detail error code for the not found exception
1752
   * @throws ServiceFailure
1753
   * @throws NotFound
1754
   */
1755 9036 tao
  public void checkV1SystemMetaPidExist(Identifier identifier, String serviceFailureCode, String serviceFailureMessage,
1756
          String noFoundCode, String notFoundMessage) throws ServiceFailure, NotFound {
1757 9033 tao
      boolean exists = false;
1758
      try {
1759
          exists = IdentifierManager.getInstance().systemMetadataPIDExists(identifier);
1760
      } catch (SQLException e) {
1761 9036 tao
          throw new ServiceFailure(serviceFailureCode, serviceFailureMessage+" since "+e.getMessage());
1762 9033 tao
      }
1763
      if(!exists) {
1764 9041 tao
         //the v1 method only handles a pid. so it should throw a not-found exception.
1765
          // check if the pid was deleted.
1766
          try {
1767
              String localId = IdentifierManager.getInstance().getLocalId(identifier.getValue());
1768
              if(EventLog.getInstance().isDeleted(localId)) {
1769
                  notFoundMessage=notFoundMessage+" "+DELETEDMESSAGE;
1770
              }
1771
            } catch (Exception e) {
1772
              logMetacat.info("Couldn't determine if the not-found identifier "+identifier.getValue()+" was deleted since "+e.getMessage());
1773
            }
1774
            throw new NotFound(noFoundCode, notFoundMessage);
1775 9033 tao
      }
1776
  }
1777
1778
  /**
1779
   * Utility method to get the PID for an SID. If the specified identifier is not an SID
1780
   * , null will be returned.
1781
   * @param sid  the specified sid
1782
   * @param serviceFailureCode  the detail error code for the service failure exception
1783
   * @return the pid for the sid. If the specified identifier is not an SID, null will be returned.
1784
   * @throws ServiceFailure
1785
   */
1786
  protected Identifier getPIDForSID(Identifier sid, String serviceFailureCode) throws ServiceFailure {
1787
      Identifier id = null;
1788 9036 tao
      String serviceFailureMessage = "The PID "+" couldn't be identified for the sid " + sid.getValue();
1789 9033 tao
      try {
1790
          //determine if the given pid is a sid or not.
1791
          if(IdentifierManager.getInstance().systemMetadataSIDExists(sid)) {
1792
              try {
1793
                  //set the header pid for the sid if the identifier is a sid.
1794
                  id = IdentifierManager.getInstance().getHeadPID(sid);
1795
              } catch (SQLException sqle) {
1796 9036 tao
                  throw new ServiceFailure(serviceFailureCode, serviceFailureMessage+" since "+sqle.getMessage());
1797 9033 tao
              }
1798
1799
          }
1800
      } catch (SQLException e) {
1801 9036 tao
          throw new ServiceFailure(serviceFailureCode, serviceFailureMessage + " since "+e.getMessage());
1802 9033 tao
      }
1803
      return id;
1804
  }
1805 7148 leinfelder
1806
1807 6241 cjones
}