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