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