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