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