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 6572 cjones
      // lock and unlock of the pid happens in the subclass
428 6468 leinfelder
    	HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
429 6572 cjones
430 6468 leinfelder
    } catch (Exception e) {
431 6495 leinfelder
    	logMetacat.error("Problem creating system metadata: " + pid.getValue(), e);
432 6468 leinfelder
        throw new ServiceFailure("1190", e.getMessage());
433
	}
434 6302 leinfelder
435 6241 cjones
    // setting the resulting identifier failed
436 6302 leinfelder
    if (localId == null ) {
437
      throw new ServiceFailure("1190", "The Node is unable to create the object. ");
438 6241 cjones
    }
439 6313 leinfelder
440 6302 leinfelder
    resultPid = pid;
441
442 7122 leinfelder
    logMetacat.debug("create() complete for object: " + pid.getValue());
443
444 6241 cjones
    return resultPid;
445
  }
446
447 6227 cjones
  /**
448
   * Return the log records associated with a given event between the start and
449
   * end dates listed given a particular Subject listed in the Session
450
   *
451
   * @param session - the Session object containing the credentials for the Subject
452
   * @param fromDate - the start date of the desired log records
453
   * @param toDate - the end date of the desired log records
454
   * @param event - restrict log records of a specific event type
455
   * @param start - zero based offset from the first record in the
456
   *                set of matching log records. Used to assist with
457
   *                paging the response.
458
   * @param count - maximum number of log records to return in the response.
459
   *                Used to assist with paging the response.
460
   *
461
   * @return the desired log records
462
   *
463
   * @throws InvalidToken
464
   * @throws ServiceFailure
465
   * @throws NotAuthorized
466
   * @throws InvalidRequest
467
   * @throws NotImplemented
468
   */
469
  public Log getLogRecords(Session session, Date fromDate, Date toDate,
470 7101 leinfelder
      Event event, String pidFilter, Integer start, Integer count) throws InvalidToken, ServiceFailure,
471 6227 cjones
      NotAuthorized, InvalidRequest, NotImplemented {
472 6174 cjones
473 7234 leinfelder
	  // only admin access to this method
474
	  // see https://redmine.dataone.org/issues/2855
475
	  if (!isAdminAuthorized(session)) {
476
		  throw new NotAuthorized("1460", "Only the CN or admin is allowed to harvest logs from this node");
477
	  }
478
479 6227 cjones
    IdentifierManager im = IdentifierManager.getInstance();
480
    EventLog el = EventLog.getInstance();
481 6353 cjones
    if ( fromDate == null ) {
482 6227 cjones
      logMetacat.debug("setting fromdate from null");
483
      fromDate = new Date(1);
484
    }
485 6353 cjones
    if ( toDate == null ) {
486 6227 cjones
      logMetacat.debug("setting todate from null");
487
      toDate = new Date();
488
    }
489 6186 leinfelder
490 6353 cjones
    if ( start == null ) {
491 7285 leinfelder
    	start = 0;
492 6353 cjones
    }
493
494
    if ( count == null ) {
495
    	count = 1000;
496
    }
497 7439 leinfelder
498
    // safeguard against large requests
499
    if (count > MAXIMUM_DB_RECORD_COUNT) {
500
    	count = MAXIMUM_DB_RECORD_COUNT;
501
    }
502 6353 cjones
503 7184 leinfelder
    String[] filterDocid = null;
504
    if (pidFilter != null) {
505
		try {
506
	      String localId = im.getLocalId(pidFilter);
507
	      filterDocid = new String[] {localId};
508
	    } catch (Exception ex) {
509
	    	String msg = "Could not find localId for given pidFilter '" + pidFilter + "'";
510
	        logMetacat.warn(msg, ex);
511
	        //throw new InvalidRequest("1480", msg);
512
	    }
513
    }
514
515 6227 cjones
    logMetacat.debug("fromDate: " + fromDate);
516
    logMetacat.debug("toDate: " + toDate);
517 6186 leinfelder
518 7285 leinfelder
    Log log = el.getD1Report(null, null, filterDocid, event,
519 6227 cjones
        new java.sql.Timestamp(fromDate.getTime()),
520 7285 leinfelder
        new java.sql.Timestamp(toDate.getTime()), false, start, count);
521 7234 leinfelder
522 6227 cjones
    logMetacat.info("getLogRecords");
523
    return log;
524
  }
525 6241 cjones
526 6227 cjones
  /**
527
   * Return the object identified by the given object identifier
528
   *
529
   * @param session - the Session object containing the credentials for the Subject
530
   * @param pid - the object identifier for the given object
531
   *
532
   * TODO: The D1 Authorization API doesn't provide information on which
533
   * authentication system the Subject belongs to, and so it's not possible to
534
   * discern which Person or Group is a valid KNB LDAP DN.  Fix this.
535
   *
536
   * @return inputStream - the input stream of the given object
537
   *
538
   * @throws InvalidToken
539
   * @throws ServiceFailure
540
   * @throws NotAuthorized
541
   * @throws InvalidRequest
542
   * @throws NotImplemented
543
   */
544
  public InputStream get(Session session, Identifier pid)
545
    throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
546 6609 cjones
    NotImplemented {
547 6226 cjones
548 6227 cjones
    InputStream inputStream = null; // bytes to be returned
549 6226 cjones
    handler = new MetacatHandler(new Timer());
550 6227 cjones
    boolean allowed = false;
551
    String localId; // the metacat docid for the pid
552 6226 cjones
553 6227 cjones
    // get the local docid from Metacat
554
    try {
555
      localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
556
557 6226 cjones
    } catch (McdbDocNotFoundException e) {
558
      throw new NotFound("1020", "The object specified by " +
559 6227 cjones
                         pid.getValue() +
560 6283 leinfelder
                         " does not exist at this node.");
561 6226 cjones
    }
562
563
    // check for authorization
564 7029 leinfelder
    try {
565
		allowed = isAuthorized(session, pid, Permission.READ);
566
	} catch (InvalidRequest e) {
567
		throw new ServiceFailure("1030", e.getDescription());
568
	}
569 6227 cjones
570
    // if the person is authorized, perform the read
571 6323 leinfelder
    if (allowed) {
572 6226 cjones
      try {
573 6323 leinfelder
        inputStream = handler.read(localId);
574
      } catch (Exception e) {
575 7162 leinfelder
        throw new NotFound("1020", "The object specified by " +
576 6226 cjones
            pid.getValue() +
577 6323 leinfelder
            "could not be returned due to error: " +
578 6226 cjones
            e.getMessage());
579
      }
580 6227 cjones
    }
581 6226 cjones
582 6227 cjones
    // if we fail to set the input stream
583
    if ( inputStream == null ) {
584 6226 cjones
      throw new NotFound("1020", "The object specified by " +
585
                         pid.getValue() +
586
                         "does not exist at this node.");
587 6227 cjones
    }
588
589 6389 leinfelder
	// log the read event
590 6532 leinfelder
    String principal = Constants.SUBJECT_PUBLIC;
591 6421 leinfelder
    if (session != null && session.getSubject() != null) {
592 6389 leinfelder
    	principal = session.getSubject().getValue();
593
    }
594 6542 leinfelder
    EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), principal, localId, "read");
595 6389 leinfelder
596 6227 cjones
    return inputStream;
597
  }
598 6174 cjones
599 6227 cjones
  /**
600
   * Return the system metadata for a given object
601
   *
602
   * @param session - the Session object containing the credentials for the Subject
603
   * @param pid - the object identifier for the given object
604
   *
605
   * @return inputStream - the input stream of the given system metadata object
606
   *
607
   * @throws InvalidToken
608
   * @throws ServiceFailure
609
   * @throws NotAuthorized
610
   * @throws NotFound
611
   * @throws InvalidRequest
612
   * @throws NotImplemented
613
   */
614 7132 cjones
    public SystemMetadata getSystemMetadata(Session session, Identifier pid)
615
        throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
616
        NotImplemented {
617
618
        boolean isAuthorized = false;
619
        SystemMetadata systemMetadata = null;
620
        List<Replica> replicaList = null;
621
        NodeReference replicaNodeRef = null;
622
        List<Node> nodeListBySubject = null;
623
        Subject subject = null;
624 6572 cjones
625 7132 cjones
        if (session != null ) {
626
            subject = session.getSubject();
627
        }
628 6572 cjones
629 7145 leinfelder
        // check normal authorization
630
        BaseException originalAuthorizationException = null;
631
        if (!isAuthorized) {
632
            try {
633
                isAuthorized = isAuthorized(session, pid, Permission.READ);
634 7132 cjones
635 7145 leinfelder
            } catch (InvalidRequest e) {
636
                throw new ServiceFailure("1090", e.getDescription());
637
            } catch (NotAuthorized nae) {
638
            	// catch this for later
639
            	originalAuthorizationException = nae;
640
			}
641
        }
642 7132 cjones
643 7145 leinfelder
        // get the system metadata first because we need the replica list for auth
644
        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
645
646
        // check the replica information to expand access to MNs that might need it
647
        if (!isAuthorized) {
648
649
	        try {
650
651
	            // if MNs are listed as replicas, allow access
652
	            if ( systemMetadata != null ) {
653
	                replicaList = systemMetadata.getReplicaList();
654
	                // only check if there are in fact replicas listed
655
	                if ( replicaList != null ) {
656
657
	                    if ( subject != null ) {
658
	                        // get the list of nodes with a matching node subject
659
	                        try {
660
	                            nodeListBySubject = listNodesBySubject(session.getSubject());
661
662
	                        } catch (BaseException e) {
663
	                            // Unexpected error contacting the CN via D1Client
664
	                            String msg = "Caught an unexpected error while trying "
665
	                                    + "to potentially authorize system metadata access "
666
	                                    + "based on the session subject. The error was "
667
	                                    + e.getMessage();
668
	                            logMetacat.error(msg);
669
	                            if (logMetacat.isDebugEnabled()) {
670
	                                e.printStackTrace();
671
672
	                            }
673
	                            // isAuthorized is still false
674
	                        }
675
676
	                    }
677
	                    if (nodeListBySubject != null) {
678
	                        // compare node ids to replica node ids
679
	                        outer: for (Replica replica : replicaList) {
680
	                            replicaNodeRef = replica.getReplicaMemberNode();
681
682
	                            for (Node node : nodeListBySubject) {
683
	                                if (node.getIdentifier().equals(replicaNodeRef)) {
684
	                                    // node id via session subject matches a replica node
685
	                                    isAuthorized = true;
686
	                                    break outer;
687
	                                }
688
	                            }
689
	                        }
690
	                    }
691
	                }
692
	            }
693
694
	            // if we still aren't authorized, then we are done
695
	            if (!isAuthorized) {
696
	                throw new NotAuthorized("1400", Permission.READ
697
	                        + " not allowed on " + pid.getValue());
698
	            }
699 7132 cjones
700 7145 leinfelder
	        } catch (RuntimeException e) {
701
	        	e.printStackTrace();
702
	            // convert hazelcast RuntimeException to ServiceFailure
703
	            throw new ServiceFailure("1090", "Unexpected error getting system metadata for: " +
704
	                pid.getValue());
705
	        }
706
707 7132 cjones
        }
708 7145 leinfelder
709 7132 cjones
        // It wasn't in the map
710
        if ( systemMetadata == null ) {
711
            throw new NotFound("1420", "No record found for: " + pid.getValue());
712
        }
713
714
        return systemMetadata;
715
    }
716 6572 cjones
717 6227 cjones
  /**
718 6865 cjones
   * Test if the user identified by the provided token has administrative authorization
719
   *
720
   * @param session - the Session object containing the credentials for the Subject
721
   *
722 7142 leinfelder
   * @return true if the user is admin
723 6865 cjones
   *
724
   * @throws ServiceFailure
725
   * @throws InvalidToken
726
   * @throws NotFound
727
   * @throws NotAuthorized
728
   * @throws NotImplemented
729
   */
730 7142 leinfelder
  public boolean isAdminAuthorized(Session session)
731 7079 leinfelder
      throws ServiceFailure, InvalidToken, NotAuthorized,
732 6865 cjones
      NotImplemented {
733
734
      boolean allowed = false;
735 7069 leinfelder
736
      // must have a session in order to check admin
737
      if (session == null) {
738 7142 leinfelder
         logMetacat.debug("In isAdminAuthorized(), session is null ");
739
         return false;
740 7069 leinfelder
      }
741
742 7072 cjones
      logMetacat.debug("In isAdminAuthorized(), checking CN or MN authorization for " +
743 7142 leinfelder
           session.getSubject().getValue());
744 7072 cjones
745 7330 leinfelder
      // check if this is the node calling itself (MN)
746
      allowed = isNodeAdmin(session);
747 6865 cjones
748 7330 leinfelder
      // check the CN list
749
      if (!allowed) {
750
	      // are we allowed to do this? only CNs are allowed
751
	      CNode cn = D1Client.getCN();
752
	      List<Node> nodes = cn.listNodes().getNodeList();
753
754
	      if ( nodes == null ) {
755
	          throw new ServiceFailure("4852", "Couldn't get node list.");
756
757
	      }
758
759
	      // find the node in the node list
760
	      for ( Node node : nodes ) {
761
762
	          NodeReference nodeReference = node.getIdentifier();
763
	          logMetacat.debug("In isAdminAuthorized(), Node reference is: " + nodeReference.getValue());
764
765
	          Subject subject = session.getSubject();
766
767
	          if (node.getType() == NodeType.CN) {
768
	              List<Subject> nodeSubjects = node.getSubjectList();
769
770
	              // check if the session subject is in the node subject list
771
	              for (Subject nodeSubject : nodeSubjects) {
772
	                  logMetacat.debug("In isAdminAuthorized(), comparing subjects: " +
773
	                      nodeSubject.getValue() + " and " + subject.getValue());
774
	                  if ( nodeSubject.equals(subject) ) {
775
	                      allowed = true; // subject of session == target node subject
776
	                      break;
777
778
	                  }
779
	              }
780
	          }
781
	      }
782 6865 cjones
      }
783
784
      return allowed;
785
  }
786
787
  /**
788 7162 leinfelder
   * Test if the user identified by the provided token has administrative authorization
789
   * on this node because they are calling themselves
790
   *
791
   * @param session - the Session object containing the credentials for the Subject
792
   *
793
   * @return true if the user is this node
794
   * @throws ServiceFailure
795
   * @throws NotImplemented
796
   */
797
  public boolean isNodeAdmin(Session session) throws NotImplemented, ServiceFailure {
798
799
      boolean allowed = false;
800
801
      // must have a session in order to check admin
802
      if (session == null) {
803
         logMetacat.debug("In isNodeAdmin(), session is null ");
804
         return false;
805
      }
806
807
      logMetacat.debug("In isNodeAdmin(), MN authorization for " +
808
           session.getSubject().getValue());
809
810
      Node node = MNodeService.getInstance(request).getCapabilities();
811
      NodeReference nodeReference = node.getIdentifier();
812
      logMetacat.debug("In isNodeAdmin(), Node reference is: " + nodeReference.getValue());
813
814
      Subject subject = session.getSubject();
815
816
      if (node.getType() == NodeType.MN) {
817
          List<Subject> nodeSubjects = node.getSubjectList();
818
819
          // check if the session subject is in the node subject list
820
          for (Subject nodeSubject : nodeSubjects) {
821
              logMetacat.debug("In isNodeAdmin(), comparing subjects: " +
822
                  nodeSubject.getValue() + " and " + subject.getValue());
823
              if ( nodeSubject.equals(subject) ) {
824
                  allowed = true; // subject of session == this node's subect
825
                  break;
826
              }
827
          }
828
      }
829
830
      return allowed;
831
  }
832
833
  /**
834 6227 cjones
   * Test if the user identified by the provided token has authorization
835 6865 cjones
   * for the operation on the specified object.
836 6227 cjones
   *
837
   * @param session - the Session object containing the credentials for the Subject
838
   * @param pid - The identifer of the resource for which access is being checked
839
   * @param operation - The type of operation which is being requested for the given pid
840
   *
841
   * @return true if the operation is allowed
842
   *
843
   * @throws ServiceFailure
844
   * @throws InvalidToken
845
   * @throws NotFound
846
   * @throws NotAuthorized
847
   * @throws NotImplemented
848
   * @throws InvalidRequest
849
   */
850
  public boolean isAuthorized(Session session, Identifier pid, Permission permission)
851
    throws ServiceFailure, InvalidToken, NotFound, NotAuthorized,
852 7029 leinfelder
    NotImplemented, InvalidRequest {
853 6174 cjones
854 6227 cjones
    boolean allowed = false;
855
856 7029 leinfelder
    if (permission == null) {
857
    	throw new InvalidRequest("1761", "Permission was not provided or is invalid");
858
    }
859
860 6816 leinfelder
    // permissions are hierarchical
861 6830 leinfelder
    List<Permission> expandedPermissions = null;
862 6816 leinfelder
863 7067 cjones
    // always allow CN access
864 7142 leinfelder
    if ( isAdminAuthorized(session) ) {
865 7067 cjones
        allowed = true;
866
        return allowed;
867
868
    }
869
870 7000 leinfelder
    // get the subject[s] from the session
871 7146 leinfelder
	//defer to the shared util for recursively compiling the subjects
872 7149 leinfelder
	Set<Subject> subjects = AuthUtils.authorizedClientSubjects(session);
873 6293 leinfelder
874 7398 leinfelder
	// track the identities we have checked against
875
	StringBuffer includedSubjects = new StringBuffer();
876
877 6227 cjones
    // get the system metadata
878 6241 cjones
    String pidStr = pid.getValue();
879 6227 cjones
    SystemMetadata systemMetadata = null;
880
    try {
881 6574 cjones
        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
882
883
    } catch (Exception e) {
884
        // convert Hazelcast RuntimeException to NotFound
885
        logMetacat.error("An error occurred while getting system metadata for identifier " +
886
            pid.getValue() + ". The error message was: " + e.getMessage());
887
        throw new NotFound("1800", "No record found for " + pidStr);
888
889 6858 cjones
    }
890 6596 leinfelder
891
    // throw not found if it was not found
892
    if (systemMetadata == null) {
893 6652 leinfelder
    	throw new NotFound("1800", "No system metadata could be found for given PID: " + pidStr);
894 6596 leinfelder
    }
895 6245 leinfelder
896 6293 leinfelder
    // do we own it?
897
    for (Subject s: subjects) {
898 7039 cjones
      logMetacat.debug("Comparing \t" +
899
                       systemMetadata.getRightsHolder().getValue() +
900
                       " \tagainst \t" + s.getValue());
901 7398 leinfelder
      	includedSubjects.append(s.getValue() + "; ");
902 6688 leinfelder
    	allowed = systemMetadata.getRightsHolder().equals(s);
903 6293 leinfelder
    	if (allowed) {
904
    		return allowed;
905
    	}
906
    }
907
908
    // otherwise check the access rules
909 6245 leinfelder
    try {
910
	    List<AccessRule> allows = systemMetadata.getAccessPolicy().getAllowList();
911 6293 leinfelder
	    search: // label break
912 6245 leinfelder
	    for (AccessRule accessRule: allows) {
913
	      for (Subject s: subjects) {
914 7059 leinfelder
	        logMetacat.debug("Checking allow access rule for subject: " + s.getValue());
915 6816 leinfelder
	        if (accessRule.getSubjectList().contains(s)) {
916 7059 leinfelder
	        	logMetacat.debug("Access rule contains subject: " + s.getValue());
917 6830 leinfelder
	        	for (Permission p: accessRule.getPermissionList()) {
918 7059 leinfelder
		        	logMetacat.debug("Checking permission: " + p.xmlValue());
919 6830 leinfelder
	        		expandedPermissions = expandPermissions(p);
920
	        		allowed = expandedPermissions.contains(permission);
921
	        		if (allowed) {
922 7059 leinfelder
			        	logMetacat.info("Permission granted: " + p.xmlValue() + " to " + s.getValue());
923 6830 leinfelder
	        			break search; //label break
924
	        		}
925 6816 leinfelder
	        	}
926
927 6245 leinfelder
	        }
928
	      }
929
	    }
930
    } catch (Exception e) {
931
    	// catch all for errors - safe side should be to deny the access
932
    	logMetacat.error("Problem checking authorization - defaulting to deny", e);
933
		allowed = false;
934 7039 cjones
935
    }
936 6227 cjones
937 6245 leinfelder
    // throw or return?
938 6227 cjones
    if (!allowed) {
939 7398 leinfelder
      throw new NotAuthorized("1820", permission + " not allowed on " + pidStr + " for subject[s]: " + includedSubjects.toString() );
940 6227 cjones
    }
941 6245 leinfelder
942 6227 cjones
    return allowed;
943 6241 cjones
944 6227 cjones
  }
945
946 6256 cjones
  /*
947 6227 cjones
   * parse a logEntry and get the relevant field from it
948
   *
949
   * @param fieldname
950
   * @param entry
951
   * @return
952
   */
953
  private String getLogEntryField(String fieldname, String entry) {
954
    String begin = "<" + fieldname + ">";
955
    String end = "</" + fieldname + ">";
956
    // logMetacat.debug("looking for " + begin + " and " + end +
957
    // " in entry " + entry);
958
    String s = entry.substring(entry.indexOf(begin) + begin.length(), entry
959
        .indexOf(end));
960
    logMetacat.debug("entry " + fieldname + " : " + s);
961
    return s;
962
  }
963 6174 cjones
964 6257 cjones
  /**
965 6241 cjones
   * Determine if a given object should be treated as an XML science metadata
966
   * object.
967
   *
968
   * @param sysmeta - the SystemMetadata describing the object
969
   * @return true if the object should be treated as science metadata
970
   */
971 6433 leinfelder
  public static boolean isScienceMetadata(SystemMetadata sysmeta) {
972 6241 cjones
973
    ObjectFormat objectFormat = null;
974
    boolean isScienceMetadata = false;
975
976
    try {
977 6561 leinfelder
      objectFormat = ObjectFormatCache.getInstance().getFormat(sysmeta.getFormatId());
978 6433 leinfelder
      if ( objectFormat.getFormatType().equals("METADATA") ) {
979 6402 cjones
      	isScienceMetadata = true;
980
981
      }
982 6241 cjones
983
984
    } catch (ServiceFailure e) {
985
      logMetacat.debug("There was a problem determining if the object identified by" +
986
          sysmeta.getIdentifier().getValue() +
987
          " is science metadata: " + e.getMessage());
988
989
    } catch (NotFound e) {
990
      logMetacat.debug("There was a problem determining if the object identified by" +
991
          sysmeta.getIdentifier().getValue() +
992
          " is science metadata: " + e.getMessage());
993
994
    }
995
996
    return isScienceMetadata;
997 6226 cjones
998 6241 cjones
  }
999
1000
  /**
1001 7315 leinfelder
   * Check fro whitespace in the given pid.
1002
   * null pids are also invalid by default
1003
   * @param pid
1004
   * @return
1005
   */
1006
  public static boolean isValidIdentifier(Identifier pid) {
1007
	  if (pid != null && pid.getValue() != null && pid.getValue().length() > 0) {
1008
		  return !pid.getValue().matches("\\s");
1009
	  }
1010
	  return false;
1011
  }
1012
1013
1014
  /**
1015 6241 cjones
   * Insert or update an XML document into Metacat
1016
   *
1017
   * @param xml - the XML document to insert or update
1018
   * @param pid - the identifier to be used for the resulting object
1019
   *
1020
   * @return localId - the resulting docid of the document created or updated
1021
   *
1022
   */
1023 6807 leinfelder
  public String insertOrUpdateDocument(String xml, Identifier pid,
1024 6241 cjones
    Session session, String insertOrUpdate)
1025
    throws ServiceFailure {
1026
1027
  	logMetacat.debug("Starting to insert xml document...");
1028
    IdentifierManager im = IdentifierManager.getInstance();
1029
1030
    // generate pid/localId pair for sysmeta
1031
    String localId = null;
1032
1033
    if(insertOrUpdate.equals("insert")) {
1034
      localId = im.generateLocalId(pid.getValue(), 1);
1035
1036
    } else {
1037
      //localid should already exist in the identifier table, so just find it
1038
      try {
1039
        logMetacat.debug("Updating pid " + pid.getValue());
1040
        logMetacat.debug("looking in identifier table for pid " + pid.getValue());
1041
1042
        localId = im.getLocalId(pid.getValue());
1043
1044
        logMetacat.debug("localId: " + localId);
1045
        //increment the revision
1046
        String docid = localId.substring(0, localId.lastIndexOf("."));
1047
        String revS = localId.substring(localId.lastIndexOf(".") + 1, localId.length());
1048
        int rev = new Integer(revS).intValue();
1049
        rev++;
1050
        docid = docid + "." + rev;
1051
        localId = docid;
1052
        logMetacat.debug("incremented localId: " + localId);
1053
1054
      } catch(McdbDocNotFoundException e) {
1055
        throw new ServiceFailure("1030", "D1NodeService.insertOrUpdateDocument(): " +
1056
            "pid " + pid.getValue() +
1057
            " should have been in the identifier table, but it wasn't: " +
1058
            e.getMessage());
1059
1060
      }
1061
1062
    }
1063
1064 6443 leinfelder
    params = new Hashtable<String, String[]>();
1065 6241 cjones
    String[] action = new String[1];
1066
    action[0] = insertOrUpdate;
1067
    params.put("action", action);
1068
    String[] docid = new String[1];
1069
    docid[0] = localId;
1070
    params.put("docid", docid);
1071
    String[] doctext = new String[1];
1072
    doctext[0] = xml;
1073
    params.put("doctext", doctext);
1074
1075 6532 leinfelder
    String username = Constants.SUBJECT_PUBLIC;
1076 6241 cjones
    String[] groupnames = null;
1077 6302 leinfelder
    if (session != null ) {
1078
    	username = session.getSubject().getValue();
1079 6532 leinfelder
    	if (session.getSubjectInfo() != null) {
1080
    		List<Group> groupList = session.getSubjectInfo().getGroupList();
1081 6302 leinfelder
    		if (groupList != null) {
1082
    			groupnames = new String[groupList.size()];
1083
    			for (int i = 0; i > groupList.size(); i++ ) {
1084
    				groupnames[i] = groupList.get(i).getGroupName();
1085
    			}
1086
    		}
1087
    	}
1088 6241 cjones
    }
1089
1090
    // do the insert or update action
1091 6443 leinfelder
    handler = new MetacatHandler(new Timer());
1092 6542 leinfelder
    String result = handler.handleInsertOrUpdateAction(request.getRemoteAddr(), request.getHeader("User-Agent"), null,
1093 7128 leinfelder
                        null, params, username, groupnames, false, false);
1094 6241 cjones
1095
    if(result.indexOf("<error>") != -1) {
1096
    	String detailCode = "";
1097
    	if ( insertOrUpdate.equals("insert") ) {
1098 7017 leinfelder
    		// make sure to remove the mapping so that subsequent attempts do not fail with IdentifierNotUnique
1099
    		im.removeMapping(pid.getValue(), localId);
1100 6241 cjones
    		detailCode = "1190";
1101
1102
    	} else if ( insertOrUpdate.equals("update") ) {
1103
    		detailCode = "1310";
1104
1105
    	}
1106
        throw new ServiceFailure(detailCode,
1107
          "Error inserting or updating document: " + result);
1108
    }
1109
    logMetacat.debug("Finsished inserting xml document with id " + localId);
1110
1111
    return localId;
1112
  }
1113
1114
  /**
1115
   * Insert a data document
1116
   *
1117
   * @param object
1118
   * @param pid
1119
   * @param sessionData
1120
   * @throws ServiceFailure
1121
   * @returns localId of the data object inserted
1122
   */
1123 6807 leinfelder
  public String insertDataObject(InputStream object, Identifier pid,
1124 6241 cjones
          Session session) throws ServiceFailure {
1125
1126 6532 leinfelder
    String username = Constants.SUBJECT_PUBLIC;
1127 6241 cjones
    String[] groupnames = null;
1128 6302 leinfelder
    if (session != null ) {
1129
    	username = session.getSubject().getValue();
1130 6532 leinfelder
    	if (session.getSubjectInfo() != null) {
1131
    		List<Group> groupList = session.getSubjectInfo().getGroupList();
1132 6302 leinfelder
    		if (groupList != null) {
1133
    			groupnames = new String[groupList.size()];
1134
    			for (int i = 0; i > groupList.size(); i++ ) {
1135
    				groupnames[i] = groupList.get(i).getGroupName();
1136
    			}
1137
    		}
1138
    	}
1139 6241 cjones
    }
1140
1141
    // generate pid/localId pair for object
1142
    logMetacat.debug("Generating a pid/localId mapping");
1143
    IdentifierManager im = IdentifierManager.getInstance();
1144
    String localId = im.generateLocalId(pid.getValue(), 1);
1145 6719 leinfelder
1146
    // Save the data file to disk using "localId" as the name
1147
    String datafilepath = null;
1148
	try {
1149
		datafilepath = PropertyService.getProperty("application.datafilepath");
1150
	} catch (PropertyNotFoundException e) {
1151
		ServiceFailure sf = new ServiceFailure("1190", "Lookup data file path" + e.getMessage());
1152
		sf.initCause(e);
1153
		throw sf;
1154
	}
1155
    boolean locked = false;
1156
	try {
1157
		locked = DocumentImpl.getDataFileLockGrant(localId);
1158
	} catch (Exception e) {
1159
		ServiceFailure sf = new ServiceFailure("1190", "Could not lock file for writing:" + e.getMessage());
1160
		sf.initCause(e);
1161
		throw sf;
1162
	}
1163
1164
    logMetacat.debug("Case DATA: starting to write to disk.");
1165
	if (locked) {
1166
1167 6241 cjones
          File dataDirectory = new File(datafilepath);
1168
          dataDirectory.mkdirs();
1169
1170
          File newFile = writeStreamToFile(dataDirectory, localId, object);
1171
1172
          // TODO: Check that the file size matches SystemMetadata
1173
          // long size = newFile.length();
1174
          // if (size == 0) {
1175
          //     throw new IOException("Uploaded file is 0 bytes!");
1176
          // }
1177
1178
          // Register the file in the database (which generates an exception
1179
          // if the localId is not acceptable or other untoward things happen
1180
          try {
1181
            logMetacat.debug("Registering document...");
1182
            DocumentImpl.registerDocument(localId, "BIN", localId,
1183
                    username, groupnames);
1184
            logMetacat.debug("Registration step completed.");
1185
1186
          } catch (SQLException e) {
1187
            //newFile.delete();
1188
            logMetacat.debug("SQLE: " + e.getMessage());
1189
            e.printStackTrace(System.out);
1190
            throw new ServiceFailure("1190", "Registration failed: " +
1191
            		e.getMessage());
1192
1193
          } catch (AccessionNumberException e) {
1194
            //newFile.delete();
1195
            logMetacat.debug("ANE: " + e.getMessage());
1196
            e.printStackTrace(System.out);
1197
            throw new ServiceFailure("1190", "Registration failed: " +
1198
            	e.getMessage());
1199
1200
          } catch (Exception e) {
1201
            //newFile.delete();
1202
            logMetacat.debug("Exception: " + e.getMessage());
1203
            e.printStackTrace(System.out);
1204
            throw new ServiceFailure("1190", "Registration failed: " +
1205
            	e.getMessage());
1206
          }
1207
1208
          logMetacat.debug("Logging the creation event.");
1209 6542 leinfelder
          EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, "create");
1210 6241 cjones
1211 7355 leinfelder
          // Schedule replication for this data file, the "insert" action is important here!
1212 6241 cjones
          logMetacat.debug("Scheduling replication.");
1213 7355 leinfelder
          ForceReplicationHandler frh = new ForceReplicationHandler(localId, "insert", false, null);
1214 6719 leinfelder
      }
1215
1216
      return localId;
1217 6241 cjones
1218
  }
1219 6255 cjones
1220 6256 cjones
  /**
1221
   * Insert a systemMetadata document and return its localId
1222
   */
1223 6807 leinfelder
  public void insertSystemMetadata(SystemMetadata sysmeta)
1224 6572 cjones
      throws ServiceFailure {
1225
1226
  	  logMetacat.debug("Starting to insert SystemMetadata...");
1227
      sysmeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1228
      logMetacat.debug("Inserting new system metadata with modified date " +
1229
          sysmeta.getDateSysMetadataModified());
1230
1231
      //insert the system metadata
1232
      try {
1233
        // note: the calling subclass handles the map hazelcast lock/unlock
1234
      	HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
1235
1236
      } catch (Exception e) {
1237
          throw new ServiceFailure("1190", e.getMessage());
1238
1239
	    }
1240 6256 cjones
  }
1241
1242
  /**
1243 6255 cjones
   * Update a systemMetadata document
1244
   *
1245
   * @param sysMeta - the system metadata object in the system to update
1246
   */
1247 6858 cjones
    protected void updateSystemMetadata(SystemMetadata sysMeta)
1248
        throws ServiceFailure {
1249
1250
        logMetacat.debug("D1NodeService.updateSystemMetadata() called.");
1251
        sysMeta.setDateSysMetadataModified(new Date());
1252
        try {
1253
            HazelcastService.getInstance().getSystemMetadataMap().lock(sysMeta.getIdentifier());
1254
            HazelcastService.getInstance().getSystemMetadataMap().put(sysMeta.getIdentifier(), sysMeta);
1255
1256
        } catch (Exception e) {
1257
            throw new ServiceFailure("4862", e.getMessage());
1258
1259
        } finally {
1260
            HazelcastService.getInstance().getSystemMetadataMap().unlock(sysMeta.getIdentifier());
1261
1262
        }
1263
1264
    }
1265 6816 leinfelder
1266
  /**
1267
   * Given a Permission, returns a list of all permissions that it encompasses
1268
   * Permissions are hierarchical so that WRITE also allows READ.
1269
   * @param permission
1270
   * @return list of included Permissions for the given permission
1271
   */
1272
  protected List<Permission> expandPermissions(Permission permission) {
1273
	  	List<Permission> expandedPermissions = new ArrayList<Permission>();
1274
	    if (permission.equals(Permission.READ)) {
1275
	    	expandedPermissions.add(Permission.READ);
1276
	    }
1277
	    if (permission.equals(Permission.WRITE)) {
1278
	    	expandedPermissions.add(Permission.READ);
1279
	    	expandedPermissions.add(Permission.WRITE);
1280
	    }
1281
	    if (permission.equals(Permission.CHANGE_PERMISSION)) {
1282
	    	expandedPermissions.add(Permission.READ);
1283
	    	expandedPermissions.add(Permission.WRITE);
1284
	    	expandedPermissions.add(Permission.CHANGE_PERMISSION);
1285
	    }
1286
	    return expandedPermissions;
1287
  }
1288 6255 cjones
1289
  /*
1290 6241 cjones
   * Write a stream to a file
1291
   *
1292
   * @param dir - the directory to write to
1293
   * @param fileName - the file name to write to
1294
   * @param data - the object bytes as an input stream
1295
   *
1296
   * @return newFile - the new file created
1297
   *
1298
   * @throws ServiceFailure
1299
   */
1300
  private File writeStreamToFile(File dir, String fileName, InputStream data)
1301
    throws ServiceFailure {
1302
1303
    File newFile = new File(dir, fileName);
1304
    logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
1305
1306
    try {
1307
        if (newFile.createNewFile()) {
1308
          // write data stream to desired file
1309
          OutputStream os = new FileOutputStream(newFile);
1310
          long length = IOUtils.copyLarge(data, os);
1311
          os.flush();
1312
          os.close();
1313
        } else {
1314
          logMetacat.debug("File creation failed, or file already exists.");
1315
          throw new ServiceFailure("1190", "File already exists: " + fileName);
1316
        }
1317
    } catch (FileNotFoundException e) {
1318
      logMetacat.debug("FNF: " + e.getMessage());
1319
      throw new ServiceFailure("1190", "File not found: " + fileName + " "
1320
                + e.getMessage());
1321
    } catch (IOException e) {
1322
      logMetacat.debug("IOE: " + e.getMessage());
1323
      throw new ServiceFailure("1190", "File was not written: " + fileName
1324
                + " " + e.getMessage());
1325
    }
1326
1327
    return newFile;
1328
  }
1329 7132 cjones
1330
  /*
1331
   * Returns a list of nodes that have been registered with the DataONE infrastructure
1332
   * that match the given session subject
1333
   * @return nodes - List of nodes from the registry with a matching session subject
1334
   *
1335
   * @throws ServiceFailure
1336
   * @throws NotImplemented
1337
   */
1338
  protected List<Node> listNodesBySubject(Subject subject)
1339
      throws ServiceFailure, NotImplemented {
1340 7136 leinfelder
      List<Node> nodeList = new ArrayList<Node>();
1341 7132 cjones
1342
      CNode cn = D1Client.getCN();
1343
      List<Node> nodes = cn.listNodes().getNodeList();
1344
1345
      // find the node in the node list
1346
      for ( Node node : nodes ) {
1347
1348
          List<Subject> nodeSubjects = node.getSubjectList();
1349 7139 leinfelder
          if (nodeSubjects != null) {
1350
	          // check if the session subject is in the node subject list
1351
	          for (Subject nodeSubject : nodeSubjects) {
1352
	              if ( nodeSubject.equals(subject) ) { // subject of session == node subject
1353
	                  nodeList.add(node);
1354
	              }
1355
	          }
1356
          }
1357 7132 cjones
      }
1358
1359
      return nodeList;
1360
1361
  }
1362
1363 7159 leinfelder
  /**
1364 7148 leinfelder
   * Archives an object, where the object is either a
1365
   * data object or a science metadata object.
1366
   *
1367
   * @param session - the Session object containing the credentials for the Subject
1368
   * @param pid - The object identifier to be archived
1369
   *
1370
   * @return pid - the identifier of the object used for the archiving
1371
   *
1372
   * @throws InvalidToken
1373
   * @throws ServiceFailure
1374
   * @throws NotAuthorized
1375
   * @throws NotFound
1376
   * @throws NotImplemented
1377
   * @throws InvalidRequest
1378
   */
1379
  public Identifier archive(Session session, Identifier pid)
1380
      throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
1381 7132 cjones
1382 7148 leinfelder
      String localId = null;
1383
      boolean allowed = false;
1384
      String username = Constants.SUBJECT_PUBLIC;
1385
      String[] groupnames = null;
1386
      if (session == null) {
1387
      	throw new InvalidToken("1330", "No session has been provided");
1388
      } else {
1389
          username = session.getSubject().getValue();
1390
          if (session.getSubjectInfo() != null) {
1391
              List<Group> groupList = session.getSubjectInfo().getGroupList();
1392
              if (groupList != null) {
1393
                  groupnames = new String[groupList.size()];
1394
                  for (int i = 0; i > groupList.size(); i++) {
1395
                      groupnames[i] = groupList.get(i).getGroupName();
1396
                  }
1397
              }
1398
          }
1399
      }
1400
1401
      // do we have a valid pid?
1402
      if (pid == null || pid.getValue().trim().equals("")) {
1403
          throw new ServiceFailure("1350", "The provided identifier was invalid.");
1404
      }
1405
1406
      // check for the existing identifier
1407
      try {
1408
          localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1409
      } catch (McdbDocNotFoundException e) {
1410
          throw new NotFound("1340", "The object with the provided " + "identifier was not found.");
1411
      }
1412
1413
      // does the subject have archive (a D1 CHANGE_PERMISSION level) privileges on the pid?
1414
      try {
1415
			allowed = isAuthorized(session, pid, Permission.CHANGE_PERMISSION);
1416
		} catch (InvalidRequest e) {
1417
          throw new ServiceFailure("1350", e.getDescription());
1418
		}
1419
1420
1421
      if (allowed) {
1422
          try {
1423
              // archive the document
1424 7236 cjones
              DocumentImpl.delete(localId, null, null, null, false);
1425 7148 leinfelder
              EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, Event.DELETE.xmlValue());
1426
1427
              // archive it
1428
              SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1429
              sysMeta.setArchived(true);
1430 7156 leinfelder
              sysMeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
1431 7148 leinfelder
              HazelcastService.getInstance().getSystemMetadataMap().put(pid, sysMeta);
1432
1433
          } catch (McdbDocNotFoundException e) {
1434
              throw new NotFound("1340", "The provided identifier was invalid.");
1435
1436
          } catch (SQLException e) {
1437
              throw new ServiceFailure("1350", "There was a problem archiving the object." + "The error message was: " + e.getMessage());
1438
1439
          } catch (InsufficientKarmaException e) {
1440
              throw new NotAuthorized("1320", "The provided identity does not have " + "permission to archive this object.");
1441
1442
          } catch (Exception e) { // for some reason DocumentImpl throws a general Exception
1443
              throw new ServiceFailure("1350", "There was a problem archiving the object." + "The error message was: " + e.getMessage());
1444
          }
1445
1446
      } else {
1447
          throw new NotAuthorized("1320", "The provided identity does not have " + "permission to archive the object on the Node.");
1448
      }
1449
1450
      return pid;
1451
  }
1452 7159 leinfelder
1453
  public Identifier archive(Identifier pid) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
1454
	  return archive(null, pid);
1455
  }
1456 7148 leinfelder
1457
1458 6241 cjones
}