1
|
/**
|
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: leinfelder $'
|
7
|
* '$Date: 2012-03-06 13:35:46 -0800 (Tue, 06 Mar 2012) $'
|
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
|
import java.io.File;
|
27
|
import java.io.FileNotFoundException;
|
28
|
import java.io.FileOutputStream;
|
29
|
import java.io.IOException;
|
30
|
import java.io.InputStream;
|
31
|
import java.io.OutputStream;
|
32
|
import java.sql.SQLException;
|
33
|
import java.util.ArrayList;
|
34
|
import java.util.Calendar;
|
35
|
import java.util.Date;
|
36
|
import java.util.Hashtable;
|
37
|
import java.util.List;
|
38
|
import java.util.Timer;
|
39
|
import java.util.Vector;
|
40
|
|
41
|
import javax.servlet.http.HttpServletRequest;
|
42
|
|
43
|
|
44
|
import org.apache.commons.io.IOUtils;
|
45
|
import org.apache.log4j.Logger;
|
46
|
import org.dataone.client.CNode;
|
47
|
import org.dataone.client.D1Client;
|
48
|
import org.dataone.client.ObjectFormatCache;
|
49
|
import org.dataone.service.util.Constants;
|
50
|
import org.dataone.service.util.DateTimeMarshaller;
|
51
|
import org.dataone.service.exceptions.IdentifierNotUnique;
|
52
|
import org.dataone.service.exceptions.InsufficientResources;
|
53
|
import org.dataone.service.exceptions.InvalidRequest;
|
54
|
import org.dataone.service.exceptions.InvalidSystemMetadata;
|
55
|
import org.dataone.service.exceptions.InvalidToken;
|
56
|
import org.dataone.service.exceptions.NotAuthorized;
|
57
|
import org.dataone.service.exceptions.NotFound;
|
58
|
import org.dataone.service.exceptions.NotImplemented;
|
59
|
import org.dataone.service.exceptions.ServiceFailure;
|
60
|
import org.dataone.service.exceptions.UnsupportedType;
|
61
|
import org.dataone.service.types.v1.AccessPolicy;
|
62
|
import org.dataone.service.types.v1.AccessRule;
|
63
|
import org.dataone.service.types.v1.DescribeResponse;
|
64
|
import org.dataone.service.types.v1.Event;
|
65
|
import org.dataone.service.types.v1.Identifier;
|
66
|
import org.dataone.service.types.v1.Group;
|
67
|
import org.dataone.service.types.v1.Log;
|
68
|
import org.dataone.service.types.v1.LogEntry;
|
69
|
import org.dataone.service.types.v1.Node;
|
70
|
import org.dataone.service.types.v1.NodeReference;
|
71
|
import org.dataone.service.types.v1.NodeType;
|
72
|
import org.dataone.service.types.v1.ObjectFormat;
|
73
|
import org.dataone.service.types.v1.Permission;
|
74
|
import org.dataone.service.types.v1.Person;
|
75
|
import org.dataone.service.types.v1.Session;
|
76
|
import org.dataone.service.types.v1.Subject;
|
77
|
import org.dataone.service.types.v1.SubjectInfo;
|
78
|
import org.dataone.service.types.v1.SubjectList;
|
79
|
import org.dataone.service.types.v1.SystemMetadata;
|
80
|
import org.dataone.service.types.v1.util.ChecksumUtil;
|
81
|
|
82
|
import edu.ucsb.nceas.metacat.AccessionNumberException;
|
83
|
import edu.ucsb.nceas.metacat.DocumentImpl;
|
84
|
import edu.ucsb.nceas.metacat.EventLog;
|
85
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
86
|
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
|
87
|
import edu.ucsb.nceas.metacat.MetacatHandler;
|
88
|
import edu.ucsb.nceas.metacat.database.DBConnection;
|
89
|
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
|
90
|
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService;
|
91
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
92
|
import edu.ucsb.nceas.metacat.replication.ForceReplicationHandler;
|
93
|
import edu.ucsb.nceas.metacat.util.DocumentUtil;
|
94
|
import edu.ucsb.nceas.metacat.util.SystemUtil;
|
95
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
96
|
|
97
|
public abstract class D1NodeService {
|
98
|
|
99
|
private static Logger logMetacat = Logger.getLogger(D1NodeService.class);
|
100
|
|
101
|
/** For logging the operations */
|
102
|
protected HttpServletRequest request;
|
103
|
|
104
|
/* reference to the metacat handler */
|
105
|
protected MetacatHandler handler;
|
106
|
|
107
|
/* parameters set in the incoming request */
|
108
|
private Hashtable<String, String[]> params;
|
109
|
|
110
|
/**
|
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
|
public D1NodeService(HttpServletRequest request) {
|
116
|
this.request = request;
|
117
|
}
|
118
|
|
119
|
/**
|
120
|
* 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
|
* Low level, "are you alive" operation. A valid ping response is
|
153
|
* indicated by a HTTP status of 200.
|
154
|
*
|
155
|
* @return true if the service is alive
|
156
|
*
|
157
|
* @throws NotImplemented
|
158
|
* @throws ServiceFailure
|
159
|
* @throws InsufficientResources
|
160
|
*/
|
161
|
public Date ping()
|
162
|
throws NotImplemented, ServiceFailure, InsufficientResources {
|
163
|
|
164
|
// test if we can get a database connection
|
165
|
int serialNumber = -1;
|
166
|
DBConnection dbConn = null;
|
167
|
try {
|
168
|
dbConn = DBConnectionPool.getDBConnection("MNodeService.ping");
|
169
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
170
|
} catch (SQLException e) {
|
171
|
ServiceFailure sf = new ServiceFailure("", e.getMessage());
|
172
|
sf.initCause(e);
|
173
|
throw sf;
|
174
|
} finally {
|
175
|
// Return the database connection
|
176
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
177
|
}
|
178
|
|
179
|
return Calendar.getInstance().getTime();
|
180
|
}
|
181
|
|
182
|
/**
|
183
|
* Adds a new object to the Node, where the object is either a data
|
184
|
* object or a science metadata object. This method is called by clients
|
185
|
* to create new data objects on Member Nodes or internally for Coordinating
|
186
|
* Nodes
|
187
|
*
|
188
|
* @param session - the Session object containing the credentials for the Subject
|
189
|
* @param pid - The object identifier to be created
|
190
|
* @param object - the object bytes
|
191
|
* @param sysmeta - the system metadata that describes the object
|
192
|
*
|
193
|
* @return pid - the object identifier created
|
194
|
*
|
195
|
* @throws InvalidToken
|
196
|
* @throws ServiceFailure
|
197
|
* @throws NotAuthorized
|
198
|
* @throws IdentifierNotUnique
|
199
|
* @throws UnsupportedType
|
200
|
* @throws InsufficientResources
|
201
|
* @throws InvalidSystemMetadata
|
202
|
* @throws NotImplemented
|
203
|
* @throws InvalidRequest
|
204
|
*/
|
205
|
public Identifier create(Session session, Identifier pid, InputStream object,
|
206
|
SystemMetadata sysmeta)
|
207
|
throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique,
|
208
|
UnsupportedType, InsufficientResources, InvalidSystemMetadata,
|
209
|
NotImplemented, InvalidRequest {
|
210
|
|
211
|
Identifier resultPid = null;
|
212
|
String localId = null;
|
213
|
boolean allowed = false;
|
214
|
|
215
|
// check for null session
|
216
|
if (session == null) {
|
217
|
throw new InvalidToken("4894", "Session is required to WRITE to the Node.");
|
218
|
}
|
219
|
Subject subject = session.getSubject();
|
220
|
|
221
|
Subject publicSubject = new Subject();
|
222
|
publicSubject.setValue(Constants.SUBJECT_PUBLIC);
|
223
|
// be sure the user is authenticated for create()
|
224
|
if (subject == null || subject.getValue() == null ||
|
225
|
subject.equals(publicSubject) ) {
|
226
|
throw new NotAuthorized("1100", "The provided identity does not have " +
|
227
|
"permission to WRITE to the Node.");
|
228
|
|
229
|
}
|
230
|
|
231
|
// verify that pid == SystemMetadata.getIdentifier()
|
232
|
logMetacat.debug("Comparing pid|sysmeta_pid: " +
|
233
|
pid.getValue() + "|" + sysmeta.getIdentifier().getValue());
|
234
|
if (!pid.getValue().equals(sysmeta.getIdentifier().getValue())) {
|
235
|
throw new InvalidSystemMetadata("1180",
|
236
|
"The supplied system metadata is invalid. " +
|
237
|
"The identifier " + pid.getValue() + " does not match identifier" +
|
238
|
"in the system metadata identified by " +
|
239
|
sysmeta.getIdentifier().getValue() + ".");
|
240
|
|
241
|
}
|
242
|
|
243
|
logMetacat.debug("Checking if identifier exists...");
|
244
|
// Check that the identifier does not already exist
|
245
|
if (IdentifierManager.getInstance().identifierExists(pid.getValue())) {
|
246
|
throw new IdentifierNotUnique("1120",
|
247
|
"The requested identifier " + pid.getValue() +
|
248
|
" is already used by another object and" +
|
249
|
"therefore can not be used for this object. Clients should choose" +
|
250
|
"a new identifier that is unique and retry the operation or " +
|
251
|
"use CN.reserveIdentifier() to reserve one.");
|
252
|
|
253
|
}
|
254
|
|
255
|
// check that we are not attempting to subvert versioning
|
256
|
if (sysmeta.getObsoletes() != null && sysmeta.getObsoletes().getValue() != null) {
|
257
|
throw new InvalidSystemMetadata("1180",
|
258
|
"The supplied system metadata is invalid. " +
|
259
|
"The obsoletes field cannot have a value when creating entries.");
|
260
|
}
|
261
|
if (sysmeta.getObsoletedBy() != null && sysmeta.getObsoletedBy().getValue() != null) {
|
262
|
throw new InvalidSystemMetadata("1180",
|
263
|
"The supplied system metadata is invalid. " +
|
264
|
"The obsoletedBy field cannot have a value when creating entries.");
|
265
|
}
|
266
|
|
267
|
// TODO: this probably needs to be refined more
|
268
|
try {
|
269
|
allowed = isAuthorized(session, pid, Permission.WRITE);
|
270
|
|
271
|
} catch (NotFound e) {
|
272
|
// The identifier doesn't exist, writing should be fine.
|
273
|
allowed = true;
|
274
|
}
|
275
|
|
276
|
// verify checksum, only if we can reset the inputstream
|
277
|
if (object.markSupported()) {
|
278
|
String checksumAlgorithm = sysmeta.getChecksum().getAlgorithm();
|
279
|
String checksumValue = sysmeta.getChecksum().getValue();
|
280
|
try {
|
281
|
String computedChecksumValue = ChecksumUtil.checksum(object, checksumAlgorithm).getValue();
|
282
|
// it's very important that we don't consume the stream
|
283
|
object.reset();
|
284
|
if (!computedChecksumValue.equals(checksumValue)) {
|
285
|
throw new InvalidSystemMetadata("4896", "Checksum given does not match that of the object");
|
286
|
}
|
287
|
} catch (Exception e) {
|
288
|
String msg = "Error verifying checksum values";
|
289
|
logMetacat.error(msg, e);
|
290
|
throw new ServiceFailure("1190", msg + ": " + e.getMessage());
|
291
|
}
|
292
|
} else {
|
293
|
logMetacat.warn("mark is not supported on the object's input stream - cannot verify checksum without consuming stream");
|
294
|
}
|
295
|
|
296
|
// we have the go ahead
|
297
|
if ( allowed ) {
|
298
|
|
299
|
// Science metadata (XML) or science data object?
|
300
|
// TODO: there are cases where certain object formats are science metadata
|
301
|
// but are not XML (netCDF ...). Handle this.
|
302
|
if ( isScienceMetadata(sysmeta) ) {
|
303
|
|
304
|
// CASE METADATA:
|
305
|
String objectAsXML = "";
|
306
|
try {
|
307
|
objectAsXML = IOUtils.toString(object, "UTF-8");
|
308
|
localId = insertOrUpdateDocument(objectAsXML, pid, session, "insert");
|
309
|
//localId = im.getLocalId(pid.getValue());
|
310
|
|
311
|
} catch (IOException e) {
|
312
|
String msg = "The Node is unable to create the object. " +
|
313
|
"There was a problem converting the object to XML";
|
314
|
logMetacat.info(msg);
|
315
|
throw new ServiceFailure("1190", msg + ": " + e.getMessage());
|
316
|
|
317
|
}
|
318
|
|
319
|
} else {
|
320
|
|
321
|
// DEFAULT CASE: DATA (needs to be checked and completed)
|
322
|
localId = insertDataObject(object, pid, session);
|
323
|
}
|
324
|
|
325
|
}
|
326
|
|
327
|
// save the sysmeta
|
328
|
try {
|
329
|
// lock and unlock of the pid happens in the subclass
|
330
|
HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
|
331
|
|
332
|
} catch (Exception e) {
|
333
|
logMetacat.error("Problem creating system metadata: " + pid.getValue(), e);
|
334
|
throw new ServiceFailure("1190", e.getMessage());
|
335
|
}
|
336
|
|
337
|
// setting the resulting identifier failed
|
338
|
if (localId == null ) {
|
339
|
throw new ServiceFailure("1190", "The Node is unable to create the object. ");
|
340
|
}
|
341
|
|
342
|
resultPid = pid;
|
343
|
|
344
|
return resultPid;
|
345
|
}
|
346
|
|
347
|
/**
|
348
|
* Return the log records associated with a given event between the start and
|
349
|
* end dates listed given a particular Subject listed in the Session
|
350
|
*
|
351
|
* @param session - the Session object containing the credentials for the Subject
|
352
|
* @param fromDate - the start date of the desired log records
|
353
|
* @param toDate - the end date of the desired log records
|
354
|
* @param event - restrict log records of a specific event type
|
355
|
* @param start - zero based offset from the first record in the
|
356
|
* set of matching log records. Used to assist with
|
357
|
* paging the response.
|
358
|
* @param count - maximum number of log records to return in the response.
|
359
|
* Used to assist with paging the response.
|
360
|
*
|
361
|
* @return the desired log records
|
362
|
*
|
363
|
* @throws InvalidToken
|
364
|
* @throws ServiceFailure
|
365
|
* @throws NotAuthorized
|
366
|
* @throws InvalidRequest
|
367
|
* @throws NotImplemented
|
368
|
*/
|
369
|
public Log getLogRecords(Session session, Date fromDate, Date toDate,
|
370
|
Event event, Integer start, Integer count) throws InvalidToken, ServiceFailure,
|
371
|
NotAuthorized, InvalidRequest, NotImplemented {
|
372
|
|
373
|
|
374
|
Log log = new Log();
|
375
|
List<LogEntry> logs = new Vector<LogEntry>();
|
376
|
IdentifierManager im = IdentifierManager.getInstance();
|
377
|
EventLog el = EventLog.getInstance();
|
378
|
if ( fromDate == null ) {
|
379
|
logMetacat.debug("setting fromdate from null");
|
380
|
fromDate = new Date(1);
|
381
|
}
|
382
|
if ( toDate == null ) {
|
383
|
logMetacat.debug("setting todate from null");
|
384
|
toDate = new Date();
|
385
|
}
|
386
|
|
387
|
if ( start == null ) {
|
388
|
start = 0;
|
389
|
|
390
|
}
|
391
|
|
392
|
if ( count == null ) {
|
393
|
count = 1000;
|
394
|
|
395
|
}
|
396
|
|
397
|
logMetacat.debug("fromDate: " + fromDate);
|
398
|
logMetacat.debug("toDate: " + toDate);
|
399
|
|
400
|
String report = el.getReport(null, null, null, null,
|
401
|
new java.sql.Timestamp(fromDate.getTime()),
|
402
|
new java.sql.Timestamp(toDate.getTime()), false);
|
403
|
|
404
|
logMetacat.debug("report: " + report);
|
405
|
|
406
|
String logEntry = "<logEntry>";
|
407
|
String endLogEntry = "</logEntry>";
|
408
|
int startIndex = 0;
|
409
|
int foundIndex = report.indexOf(logEntry, startIndex);
|
410
|
while (foundIndex != -1) {
|
411
|
// parse out each entry
|
412
|
int endEntryIndex = report.indexOf(endLogEntry, foundIndex);
|
413
|
String entry = report.substring(foundIndex, endEntryIndex);
|
414
|
logMetacat.debug("entry: " + entry);
|
415
|
startIndex = endEntryIndex + endLogEntry.length();
|
416
|
foundIndex = report.indexOf(logEntry, startIndex);
|
417
|
|
418
|
String entryId = getLogEntryField("entryid", entry);
|
419
|
String ipAddress = getLogEntryField("ipAddress", entry);
|
420
|
String principal = getLogEntryField("principal", entry);
|
421
|
String userAgent = getLogEntryField("userAgent", entry);
|
422
|
String docid = getLogEntryField("docid", entry);
|
423
|
String eventS = getLogEntryField("event", entry);
|
424
|
String dateLogged = getLogEntryField("dateLogged", entry);
|
425
|
|
426
|
LogEntry le = new LogEntry();
|
427
|
|
428
|
Event e = Event.convert(eventS);
|
429
|
if (e == null) { // skip any events that are not Dataone Crud events
|
430
|
continue;
|
431
|
}
|
432
|
le.setEvent(e);
|
433
|
le.setEntryId(entryId);
|
434
|
Identifier identifier = new Identifier();
|
435
|
try {
|
436
|
logMetacat.debug("converting docid '" + docid + "' to a pid.");
|
437
|
if (docid == null || docid.trim().equals("") || docid.trim().equals("null")) {
|
438
|
continue;
|
439
|
}
|
440
|
String docidNoRev = DocumentUtil.getSmartDocId(docid);
|
441
|
//docid = docid.substring(0, docid.lastIndexOf("."));
|
442
|
int rev = DocumentUtil.getRevisionFromAccessionNumber(docid);
|
443
|
//int rev = im.getLatestRevForLocalId(docid);
|
444
|
String guid = im.getGUID(docidNoRev, rev);
|
445
|
identifier.setValue(guid);
|
446
|
} catch (Exception ex) {
|
447
|
// try to get the pid, if that doesn't
|
448
|
// work, just use the local id
|
449
|
// throw new ServiceFailure("1030",
|
450
|
// "Error getting pid for localId " +
|
451
|
// docid + ": " + ex.getMessage());\
|
452
|
|
453
|
// skip it if the pid can't be found
|
454
|
continue;
|
455
|
}
|
456
|
|
457
|
le.setIdentifier(identifier);
|
458
|
le.setIpAddress(ipAddress);
|
459
|
Date logDate = DateTimeMarshaller.deserializeDateToUTC(dateLogged);
|
460
|
le.setDateLogged(logDate);
|
461
|
NodeReference memberNode = new NodeReference();
|
462
|
String nodeId = "localhost";
|
463
|
try {
|
464
|
nodeId = PropertyService.getProperty("dataone.nodeId");
|
465
|
} catch (PropertyNotFoundException e1) {
|
466
|
// TODO Auto-generated catch block
|
467
|
e1.printStackTrace();
|
468
|
}
|
469
|
memberNode.setValue(nodeId);
|
470
|
le.setNodeIdentifier(memberNode);
|
471
|
Subject princ = new Subject();
|
472
|
princ.setValue(principal);
|
473
|
le.setSubject(princ);
|
474
|
le.setUserAgent(userAgent);
|
475
|
|
476
|
// event filtering?
|
477
|
if (event == null) {
|
478
|
logs.add(le);
|
479
|
} else if (le.getEvent().equals(event)) {
|
480
|
logs.add(le);
|
481
|
}
|
482
|
}
|
483
|
|
484
|
// d1 paging
|
485
|
int total = logs.size();
|
486
|
if (start != null && count != null) {
|
487
|
int toIndex = start + count;
|
488
|
if (toIndex <= total) {
|
489
|
logs = new ArrayList<LogEntry>(logs.subList(start, toIndex));
|
490
|
}
|
491
|
}
|
492
|
|
493
|
log.setLogEntryList(logs);
|
494
|
log.setStart(start);
|
495
|
log.setCount(logs.size());
|
496
|
log.setTotal(total);
|
497
|
logMetacat.info("getLogRecords");
|
498
|
return log;
|
499
|
}
|
500
|
|
501
|
/**
|
502
|
* Return the object identified by the given object identifier
|
503
|
*
|
504
|
* @param session - the Session object containing the credentials for the Subject
|
505
|
* @param pid - the object identifier for the given object
|
506
|
*
|
507
|
* TODO: The D1 Authorization API doesn't provide information on which
|
508
|
* authentication system the Subject belongs to, and so it's not possible to
|
509
|
* discern which Person or Group is a valid KNB LDAP DN. Fix this.
|
510
|
*
|
511
|
* @return inputStream - the input stream of the given object
|
512
|
*
|
513
|
* @throws InvalidToken
|
514
|
* @throws ServiceFailure
|
515
|
* @throws NotAuthorized
|
516
|
* @throws InvalidRequest
|
517
|
* @throws NotImplemented
|
518
|
*/
|
519
|
public InputStream get(Session session, Identifier pid)
|
520
|
throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
521
|
NotImplemented {
|
522
|
|
523
|
InputStream inputStream = null; // bytes to be returned
|
524
|
handler = new MetacatHandler(new Timer());
|
525
|
boolean allowed = false;
|
526
|
String localId; // the metacat docid for the pid
|
527
|
|
528
|
// get the local docid from Metacat
|
529
|
try {
|
530
|
localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
|
531
|
|
532
|
} catch (McdbDocNotFoundException e) {
|
533
|
throw new NotFound("1020", "The object specified by " +
|
534
|
pid.getValue() +
|
535
|
" does not exist at this node.");
|
536
|
}
|
537
|
|
538
|
// check for authorization
|
539
|
try {
|
540
|
allowed = isAuthorized(session, pid, Permission.READ);
|
541
|
} catch (InvalidRequest e) {
|
542
|
throw new ServiceFailure("1030", e.getDescription());
|
543
|
}
|
544
|
|
545
|
// if the person is authorized, perform the read
|
546
|
if (allowed) {
|
547
|
try {
|
548
|
inputStream = handler.read(localId);
|
549
|
} catch (Exception e) {
|
550
|
throw new ServiceFailure("1020", "The object specified by " +
|
551
|
pid.getValue() +
|
552
|
"could not be returned due to error: " +
|
553
|
e.getMessage());
|
554
|
}
|
555
|
}
|
556
|
|
557
|
// if we fail to set the input stream
|
558
|
if ( inputStream == null ) {
|
559
|
throw new NotFound("1020", "The object specified by " +
|
560
|
pid.getValue() +
|
561
|
"does not exist at this node.");
|
562
|
}
|
563
|
|
564
|
// log the read event
|
565
|
String principal = Constants.SUBJECT_PUBLIC;
|
566
|
if (session != null && session.getSubject() != null) {
|
567
|
principal = session.getSubject().getValue();
|
568
|
}
|
569
|
EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), principal, localId, "read");
|
570
|
|
571
|
return inputStream;
|
572
|
}
|
573
|
|
574
|
/**
|
575
|
* Return the system metadata for a given object
|
576
|
*
|
577
|
* @param session - the Session object containing the credentials for the Subject
|
578
|
* @param pid - the object identifier for the given object
|
579
|
*
|
580
|
* @return inputStream - the input stream of the given system metadata object
|
581
|
*
|
582
|
* @throws InvalidToken
|
583
|
* @throws ServiceFailure
|
584
|
* @throws NotAuthorized
|
585
|
* @throws NotFound
|
586
|
* @throws InvalidRequest
|
587
|
* @throws NotImplemented
|
588
|
*/
|
589
|
public SystemMetadata getSystemMetadata(Session session, Identifier pid)
|
590
|
throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
591
|
NotImplemented {
|
592
|
|
593
|
boolean isAuthorized = false;
|
594
|
try {
|
595
|
isAuthorized = isAuthorized(session, pid, Permission.READ);
|
596
|
} catch (InvalidRequest e) {
|
597
|
throw new ServiceFailure("1090", e.getDescription());
|
598
|
}
|
599
|
|
600
|
if (!isAuthorized) {
|
601
|
throw new NotAuthorized("1400", Permission.READ + " not allowed on " + pid.getValue());
|
602
|
}
|
603
|
SystemMetadata systemMetadata = null;
|
604
|
try {
|
605
|
systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
|
606
|
|
607
|
} catch (Exception e) {
|
608
|
// convert hazelcast RuntimeException to NotFound
|
609
|
throw new NotFound("1420", "No record found for: " + pid.getValue());
|
610
|
|
611
|
}
|
612
|
|
613
|
return systemMetadata;
|
614
|
}
|
615
|
|
616
|
/**
|
617
|
* Test if the user identified by the provided token has administrative authorization
|
618
|
* for the operation on the specified object.
|
619
|
*
|
620
|
* @param session - the Session object containing the credentials for the Subject
|
621
|
* @param pid - The identifer of the resource for which access is being checked
|
622
|
* @param operation - The type of operation which is being requested for the given pid
|
623
|
*
|
624
|
* @return true if the operation is allowed
|
625
|
*
|
626
|
* @throws ServiceFailure
|
627
|
* @throws InvalidToken
|
628
|
* @throws NotFound
|
629
|
* @throws NotAuthorized
|
630
|
* @throws NotImplemented
|
631
|
*/
|
632
|
protected boolean isAdminAuthorized(Session session, Identifier pid,
|
633
|
Permission permission)
|
634
|
throws ServiceFailure, InvalidToken, NotFound, NotAuthorized,
|
635
|
NotImplemented {
|
636
|
|
637
|
boolean allowed = false;
|
638
|
// are we allowed to do this? only CNs and target MNs are allowed
|
639
|
CNode cn = D1Client.getCN();
|
640
|
List<Node> nodes = cn.listNodes().getNodeList();
|
641
|
|
642
|
if ( nodes == null ) {
|
643
|
throw new ServiceFailure("4852", "Couldn't get node list.");
|
644
|
|
645
|
}
|
646
|
|
647
|
// find the node in the node list
|
648
|
for ( Node node : nodes ) {
|
649
|
|
650
|
NodeReference nodeReference = node.getIdentifier();
|
651
|
logMetacat.debug("In isAdminAuthorized(), Node reference is: " + nodeReference.getValue());
|
652
|
|
653
|
Subject subject = session.getSubject();
|
654
|
|
655
|
if (node.getType() == NodeType.CN) {
|
656
|
List<Subject> nodeSubjects = node.getSubjectList();
|
657
|
|
658
|
// check if the session subject is in the node subject list
|
659
|
for (Subject nodeSubject : nodeSubjects) {
|
660
|
if ( nodeSubject.equals(subject) ) {
|
661
|
allowed = true; // subject of session == target node subject
|
662
|
break;
|
663
|
|
664
|
}
|
665
|
}
|
666
|
}
|
667
|
}
|
668
|
|
669
|
|
670
|
return allowed;
|
671
|
}
|
672
|
|
673
|
/**
|
674
|
* Test if the user identified by the provided token has authorization
|
675
|
* for the operation on the specified object.
|
676
|
*
|
677
|
* @param session - the Session object containing the credentials for the Subject
|
678
|
* @param pid - The identifer of the resource for which access is being checked
|
679
|
* @param operation - The type of operation which is being requested for the given pid
|
680
|
*
|
681
|
* @return true if the operation is allowed
|
682
|
*
|
683
|
* @throws ServiceFailure
|
684
|
* @throws InvalidToken
|
685
|
* @throws NotFound
|
686
|
* @throws NotAuthorized
|
687
|
* @throws NotImplemented
|
688
|
* @throws InvalidRequest
|
689
|
*/
|
690
|
public boolean isAuthorized(Session session, Identifier pid, Permission permission)
|
691
|
throws ServiceFailure, InvalidToken, NotFound, NotAuthorized,
|
692
|
NotImplemented, InvalidRequest {
|
693
|
|
694
|
boolean allowed = false;
|
695
|
|
696
|
if (permission == null) {
|
697
|
throw new InvalidRequest("1761", "Permission was not provided or is invalid");
|
698
|
}
|
699
|
|
700
|
// permissions are hierarchical
|
701
|
List<Permission> expandedPermissions = null;
|
702
|
|
703
|
// for the "Verified" symbolic user
|
704
|
Subject verifiedSubject = new Subject();
|
705
|
verifiedSubject.setValue(Constants.SUBJECT_VERIFIED_USER);
|
706
|
|
707
|
// get the subject[s] from the session
|
708
|
List<Subject> subjects = new ArrayList<Subject>();
|
709
|
if (session != null) {
|
710
|
// primary subject
|
711
|
Subject subject = session.getSubject();
|
712
|
if (subject != null) {
|
713
|
subjects.add(subject);
|
714
|
}
|
715
|
// details about the subject
|
716
|
SubjectInfo subjectInfo = session.getSubjectInfo();
|
717
|
if (subjectInfo != null) {
|
718
|
// find subjectInfo for the primary subject
|
719
|
List<Person> personList = subjectInfo.getPersonList();
|
720
|
if (personList != null) {
|
721
|
for (Person p : personList) {
|
722
|
// for every person listed (isVerified is transitive)
|
723
|
logMetacat.debug("checking person");
|
724
|
logMetacat.debug("p.getVerified(): " + p.getVerified());
|
725
|
if (p.getVerified() != null && p.getVerified()) {
|
726
|
// add the verified symbolic user
|
727
|
if (!subjects.contains(verifiedSubject)) {
|
728
|
subjects.add(verifiedSubject);
|
729
|
}
|
730
|
}
|
731
|
// add the equivalent identities
|
732
|
List<Subject> equivList = p
|
733
|
.getEquivalentIdentityList();
|
734
|
if (equivList != null) {
|
735
|
for (Subject equiv : equivList) {
|
736
|
subjects.add(equiv);
|
737
|
}
|
738
|
}
|
739
|
// add the groups
|
740
|
List<Subject> groupList = p.getIsMemberOfList();
|
741
|
if (groupList != null) {
|
742
|
for (Subject g : groupList) {
|
743
|
subjects.add(g);
|
744
|
}
|
745
|
}
|
746
|
break;
|
747
|
}
|
748
|
}
|
749
|
}
|
750
|
|
751
|
// add the authenticated symbolic since we have a session
|
752
|
Subject authenticatedSubject = new Subject();
|
753
|
authenticatedSubject.setValue(Constants.SUBJECT_AUTHENTICATED_USER);
|
754
|
subjects.add(authenticatedSubject);
|
755
|
}
|
756
|
|
757
|
// add public subject for everyone
|
758
|
Subject publicSubject = new Subject();
|
759
|
publicSubject.setValue(Constants.SUBJECT_PUBLIC);
|
760
|
subjects.add(publicSubject);
|
761
|
|
762
|
// get the system metadata
|
763
|
String pidStr = pid.getValue();
|
764
|
SystemMetadata systemMetadata = null;
|
765
|
try {
|
766
|
systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
|
767
|
|
768
|
} catch (Exception e) {
|
769
|
// convert Hazelcast RuntimeException to NotFound
|
770
|
logMetacat.error("An error occurred while getting system metadata for identifier " +
|
771
|
pid.getValue() + ". The error message was: " + e.getMessage());
|
772
|
throw new NotFound("1800", "No record found for " + pidStr);
|
773
|
|
774
|
}
|
775
|
|
776
|
// throw not found if it was not found
|
777
|
if (systemMetadata == null) {
|
778
|
throw new NotFound("1800", "No system metadata could be found for given PID: " + pidStr);
|
779
|
}
|
780
|
|
781
|
// do we own it?
|
782
|
for (Subject s: subjects) {
|
783
|
logMetacat.debug("Comparing \t" +
|
784
|
systemMetadata.getRightsHolder().getValue() +
|
785
|
" \tagainst \t" + s.getValue());
|
786
|
allowed = systemMetadata.getRightsHolder().equals(s);
|
787
|
if (allowed) {
|
788
|
return allowed;
|
789
|
}
|
790
|
}
|
791
|
|
792
|
// otherwise check the access rules
|
793
|
try {
|
794
|
List<AccessRule> allows = systemMetadata.getAccessPolicy().getAllowList();
|
795
|
search: // label break
|
796
|
for (AccessRule accessRule: allows) {
|
797
|
for (Subject s: subjects) {
|
798
|
if (accessRule.getSubjectList().contains(s)) {
|
799
|
for (Permission p: accessRule.getPermissionList()) {
|
800
|
expandedPermissions = expandPermissions(p);
|
801
|
allowed = expandedPermissions.contains(permission);
|
802
|
if (allowed) {
|
803
|
break search; //label break
|
804
|
}
|
805
|
}
|
806
|
|
807
|
}
|
808
|
}
|
809
|
}
|
810
|
} catch (Exception e) {
|
811
|
// catch all for errors - safe side should be to deny the access
|
812
|
logMetacat.error("Problem checking authorization - defaulting to deny", e);
|
813
|
allowed = false;
|
814
|
|
815
|
}
|
816
|
|
817
|
// throw or return?
|
818
|
if (!allowed) {
|
819
|
throw new NotAuthorized("1820", permission + " not allowed on " + pidStr);
|
820
|
}
|
821
|
|
822
|
return allowed;
|
823
|
|
824
|
}
|
825
|
|
826
|
/*
|
827
|
* parse a logEntry and get the relevant field from it
|
828
|
*
|
829
|
* @param fieldname
|
830
|
* @param entry
|
831
|
* @return
|
832
|
*/
|
833
|
private String getLogEntryField(String fieldname, String entry) {
|
834
|
String begin = "<" + fieldname + ">";
|
835
|
String end = "</" + fieldname + ">";
|
836
|
// logMetacat.debug("looking for " + begin + " and " + end +
|
837
|
// " in entry " + entry);
|
838
|
String s = entry.substring(entry.indexOf(begin) + begin.length(), entry
|
839
|
.indexOf(end));
|
840
|
logMetacat.debug("entry " + fieldname + " : " + s);
|
841
|
return s;
|
842
|
}
|
843
|
|
844
|
/**
|
845
|
* Determine if a given object should be treated as an XML science metadata
|
846
|
* object.
|
847
|
*
|
848
|
* @param sysmeta - the SystemMetadata describing the object
|
849
|
* @return true if the object should be treated as science metadata
|
850
|
*/
|
851
|
public static boolean isScienceMetadata(SystemMetadata sysmeta) {
|
852
|
|
853
|
ObjectFormat objectFormat = null;
|
854
|
boolean isScienceMetadata = false;
|
855
|
|
856
|
try {
|
857
|
objectFormat = ObjectFormatCache.getInstance().getFormat(sysmeta.getFormatId());
|
858
|
if ( objectFormat.getFormatType().equals("METADATA") ) {
|
859
|
isScienceMetadata = true;
|
860
|
|
861
|
}
|
862
|
|
863
|
|
864
|
} catch (ServiceFailure e) {
|
865
|
logMetacat.debug("There was a problem determining if the object identified by" +
|
866
|
sysmeta.getIdentifier().getValue() +
|
867
|
" is science metadata: " + e.getMessage());
|
868
|
|
869
|
} catch (NotFound e) {
|
870
|
logMetacat.debug("There was a problem determining if the object identified by" +
|
871
|
sysmeta.getIdentifier().getValue() +
|
872
|
" is science metadata: " + e.getMessage());
|
873
|
|
874
|
}
|
875
|
|
876
|
return isScienceMetadata;
|
877
|
|
878
|
}
|
879
|
|
880
|
/**
|
881
|
* Insert or update an XML document into Metacat
|
882
|
*
|
883
|
* @param xml - the XML document to insert or update
|
884
|
* @param pid - the identifier to be used for the resulting object
|
885
|
*
|
886
|
* @return localId - the resulting docid of the document created or updated
|
887
|
*
|
888
|
*/
|
889
|
public String insertOrUpdateDocument(String xml, Identifier pid,
|
890
|
Session session, String insertOrUpdate)
|
891
|
throws ServiceFailure {
|
892
|
|
893
|
logMetacat.debug("Starting to insert xml document...");
|
894
|
IdentifierManager im = IdentifierManager.getInstance();
|
895
|
|
896
|
// generate pid/localId pair for sysmeta
|
897
|
String localId = null;
|
898
|
|
899
|
if(insertOrUpdate.equals("insert")) {
|
900
|
localId = im.generateLocalId(pid.getValue(), 1);
|
901
|
|
902
|
} else {
|
903
|
//localid should already exist in the identifier table, so just find it
|
904
|
try {
|
905
|
logMetacat.debug("Updating pid " + pid.getValue());
|
906
|
logMetacat.debug("looking in identifier table for pid " + pid.getValue());
|
907
|
|
908
|
localId = im.getLocalId(pid.getValue());
|
909
|
|
910
|
logMetacat.debug("localId: " + localId);
|
911
|
//increment the revision
|
912
|
String docid = localId.substring(0, localId.lastIndexOf("."));
|
913
|
String revS = localId.substring(localId.lastIndexOf(".") + 1, localId.length());
|
914
|
int rev = new Integer(revS).intValue();
|
915
|
rev++;
|
916
|
docid = docid + "." + rev;
|
917
|
localId = docid;
|
918
|
logMetacat.debug("incremented localId: " + localId);
|
919
|
|
920
|
} catch(McdbDocNotFoundException e) {
|
921
|
throw new ServiceFailure("1030", "D1NodeService.insertOrUpdateDocument(): " +
|
922
|
"pid " + pid.getValue() +
|
923
|
" should have been in the identifier table, but it wasn't: " +
|
924
|
e.getMessage());
|
925
|
|
926
|
}
|
927
|
|
928
|
}
|
929
|
|
930
|
params = new Hashtable<String, String[]>();
|
931
|
String[] action = new String[1];
|
932
|
action[0] = insertOrUpdate;
|
933
|
params.put("action", action);
|
934
|
String[] docid = new String[1];
|
935
|
docid[0] = localId;
|
936
|
params.put("docid", docid);
|
937
|
String[] doctext = new String[1];
|
938
|
doctext[0] = xml;
|
939
|
params.put("doctext", doctext);
|
940
|
|
941
|
String username = Constants.SUBJECT_PUBLIC;
|
942
|
String[] groupnames = null;
|
943
|
if (session != null ) {
|
944
|
username = session.getSubject().getValue();
|
945
|
if (session.getSubjectInfo() != null) {
|
946
|
List<Group> groupList = session.getSubjectInfo().getGroupList();
|
947
|
if (groupList != null) {
|
948
|
groupnames = new String[groupList.size()];
|
949
|
for (int i = 0; i > groupList.size(); i++ ) {
|
950
|
groupnames[i] = groupList.get(i).getGroupName();
|
951
|
}
|
952
|
}
|
953
|
}
|
954
|
}
|
955
|
|
956
|
// do the insert or update action
|
957
|
handler = new MetacatHandler(new Timer());
|
958
|
String result = handler.handleInsertOrUpdateAction(request.getRemoteAddr(), request.getHeader("User-Agent"), null,
|
959
|
null, params, username, groupnames, false);
|
960
|
|
961
|
if(result.indexOf("<error>") != -1) {
|
962
|
String detailCode = "";
|
963
|
if ( insertOrUpdate.equals("insert") ) {
|
964
|
// make sure to remove the mapping so that subsequent attempts do not fail with IdentifierNotUnique
|
965
|
im.removeMapping(pid.getValue(), localId);
|
966
|
detailCode = "1190";
|
967
|
|
968
|
} else if ( insertOrUpdate.equals("update") ) {
|
969
|
detailCode = "1310";
|
970
|
|
971
|
}
|
972
|
throw new ServiceFailure(detailCode,
|
973
|
"Error inserting or updating document: " + result);
|
974
|
}
|
975
|
logMetacat.debug("Finsished inserting xml document with id " + localId);
|
976
|
|
977
|
return localId;
|
978
|
}
|
979
|
|
980
|
/**
|
981
|
* Insert a data document
|
982
|
*
|
983
|
* @param object
|
984
|
* @param pid
|
985
|
* @param sessionData
|
986
|
* @throws ServiceFailure
|
987
|
* @returns localId of the data object inserted
|
988
|
*/
|
989
|
public String insertDataObject(InputStream object, Identifier pid,
|
990
|
Session session) throws ServiceFailure {
|
991
|
|
992
|
String username = Constants.SUBJECT_PUBLIC;
|
993
|
String[] groupnames = null;
|
994
|
if (session != null ) {
|
995
|
username = session.getSubject().getValue();
|
996
|
if (session.getSubjectInfo() != null) {
|
997
|
List<Group> groupList = session.getSubjectInfo().getGroupList();
|
998
|
if (groupList != null) {
|
999
|
groupnames = new String[groupList.size()];
|
1000
|
for (int i = 0; i > groupList.size(); i++ ) {
|
1001
|
groupnames[i] = groupList.get(i).getGroupName();
|
1002
|
}
|
1003
|
}
|
1004
|
}
|
1005
|
}
|
1006
|
|
1007
|
// generate pid/localId pair for object
|
1008
|
logMetacat.debug("Generating a pid/localId mapping");
|
1009
|
IdentifierManager im = IdentifierManager.getInstance();
|
1010
|
String localId = im.generateLocalId(pid.getValue(), 1);
|
1011
|
|
1012
|
// Save the data file to disk using "localId" as the name
|
1013
|
String datafilepath = null;
|
1014
|
try {
|
1015
|
datafilepath = PropertyService.getProperty("application.datafilepath");
|
1016
|
} catch (PropertyNotFoundException e) {
|
1017
|
ServiceFailure sf = new ServiceFailure("1190", "Lookup data file path" + e.getMessage());
|
1018
|
sf.initCause(e);
|
1019
|
throw sf;
|
1020
|
}
|
1021
|
boolean locked = false;
|
1022
|
try {
|
1023
|
locked = DocumentImpl.getDataFileLockGrant(localId);
|
1024
|
} catch (Exception e) {
|
1025
|
ServiceFailure sf = new ServiceFailure("1190", "Could not lock file for writing:" + e.getMessage());
|
1026
|
sf.initCause(e);
|
1027
|
throw sf;
|
1028
|
}
|
1029
|
|
1030
|
logMetacat.debug("Case DATA: starting to write to disk.");
|
1031
|
if (locked) {
|
1032
|
|
1033
|
File dataDirectory = new File(datafilepath);
|
1034
|
dataDirectory.mkdirs();
|
1035
|
|
1036
|
File newFile = writeStreamToFile(dataDirectory, localId, object);
|
1037
|
|
1038
|
// TODO: Check that the file size matches SystemMetadata
|
1039
|
// long size = newFile.length();
|
1040
|
// if (size == 0) {
|
1041
|
// throw new IOException("Uploaded file is 0 bytes!");
|
1042
|
// }
|
1043
|
|
1044
|
// Register the file in the database (which generates an exception
|
1045
|
// if the localId is not acceptable or other untoward things happen
|
1046
|
try {
|
1047
|
logMetacat.debug("Registering document...");
|
1048
|
DocumentImpl.registerDocument(localId, "BIN", localId,
|
1049
|
username, groupnames);
|
1050
|
logMetacat.debug("Registration step completed.");
|
1051
|
|
1052
|
} catch (SQLException e) {
|
1053
|
//newFile.delete();
|
1054
|
logMetacat.debug("SQLE: " + e.getMessage());
|
1055
|
e.printStackTrace(System.out);
|
1056
|
throw new ServiceFailure("1190", "Registration failed: " +
|
1057
|
e.getMessage());
|
1058
|
|
1059
|
} catch (AccessionNumberException e) {
|
1060
|
//newFile.delete();
|
1061
|
logMetacat.debug("ANE: " + e.getMessage());
|
1062
|
e.printStackTrace(System.out);
|
1063
|
throw new ServiceFailure("1190", "Registration failed: " +
|
1064
|
e.getMessage());
|
1065
|
|
1066
|
} catch (Exception e) {
|
1067
|
//newFile.delete();
|
1068
|
logMetacat.debug("Exception: " + e.getMessage());
|
1069
|
e.printStackTrace(System.out);
|
1070
|
throw new ServiceFailure("1190", "Registration failed: " +
|
1071
|
e.getMessage());
|
1072
|
}
|
1073
|
|
1074
|
logMetacat.debug("Logging the creation event.");
|
1075
|
EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, "create");
|
1076
|
|
1077
|
// Schedule replication for this data file
|
1078
|
logMetacat.debug("Scheduling replication.");
|
1079
|
ForceReplicationHandler frh = new ForceReplicationHandler(
|
1080
|
localId, "create", false, null);
|
1081
|
}
|
1082
|
|
1083
|
return localId;
|
1084
|
|
1085
|
}
|
1086
|
|
1087
|
/**
|
1088
|
* Insert a systemMetadata document and return its localId
|
1089
|
*/
|
1090
|
public void insertSystemMetadata(SystemMetadata sysmeta)
|
1091
|
throws ServiceFailure {
|
1092
|
|
1093
|
logMetacat.debug("Starting to insert SystemMetadata...");
|
1094
|
sysmeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
|
1095
|
logMetacat.debug("Inserting new system metadata with modified date " +
|
1096
|
sysmeta.getDateSysMetadataModified());
|
1097
|
|
1098
|
//insert the system metadata
|
1099
|
try {
|
1100
|
// note: the calling subclass handles the map hazelcast lock/unlock
|
1101
|
HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
|
1102
|
|
1103
|
} catch (Exception e) {
|
1104
|
throw new ServiceFailure("1190", e.getMessage());
|
1105
|
|
1106
|
}
|
1107
|
}
|
1108
|
|
1109
|
/**
|
1110
|
* Update a systemMetadata document
|
1111
|
*
|
1112
|
* @param sysMeta - the system metadata object in the system to update
|
1113
|
*/
|
1114
|
protected void updateSystemMetadata(SystemMetadata sysMeta)
|
1115
|
throws ServiceFailure {
|
1116
|
|
1117
|
logMetacat.debug("D1NodeService.updateSystemMetadata() called.");
|
1118
|
sysMeta.setDateSysMetadataModified(new Date());
|
1119
|
try {
|
1120
|
HazelcastService.getInstance().getSystemMetadataMap().lock(sysMeta.getIdentifier());
|
1121
|
HazelcastService.getInstance().getSystemMetadataMap().put(sysMeta.getIdentifier(), sysMeta);
|
1122
|
|
1123
|
} catch (Exception e) {
|
1124
|
throw new ServiceFailure("4862", e.getMessage());
|
1125
|
|
1126
|
} finally {
|
1127
|
HazelcastService.getInstance().getSystemMetadataMap().unlock(sysMeta.getIdentifier());
|
1128
|
|
1129
|
}
|
1130
|
|
1131
|
}
|
1132
|
|
1133
|
/**
|
1134
|
* Given a Permission, returns a list of all permissions that it encompasses
|
1135
|
* Permissions are hierarchical so that WRITE also allows READ.
|
1136
|
* @param permission
|
1137
|
* @return list of included Permissions for the given permission
|
1138
|
*/
|
1139
|
protected List<Permission> expandPermissions(Permission permission) {
|
1140
|
List<Permission> expandedPermissions = new ArrayList<Permission>();
|
1141
|
if (permission.equals(Permission.READ)) {
|
1142
|
expandedPermissions.add(Permission.READ);
|
1143
|
}
|
1144
|
if (permission.equals(Permission.WRITE)) {
|
1145
|
expandedPermissions.add(Permission.READ);
|
1146
|
expandedPermissions.add(Permission.WRITE);
|
1147
|
}
|
1148
|
if (permission.equals(Permission.CHANGE_PERMISSION)) {
|
1149
|
expandedPermissions.add(Permission.READ);
|
1150
|
expandedPermissions.add(Permission.WRITE);
|
1151
|
expandedPermissions.add(Permission.CHANGE_PERMISSION);
|
1152
|
}
|
1153
|
return expandedPermissions;
|
1154
|
}
|
1155
|
|
1156
|
/*
|
1157
|
* Write a stream to a file
|
1158
|
*
|
1159
|
* @param dir - the directory to write to
|
1160
|
* @param fileName - the file name to write to
|
1161
|
* @param data - the object bytes as an input stream
|
1162
|
*
|
1163
|
* @return newFile - the new file created
|
1164
|
*
|
1165
|
* @throws ServiceFailure
|
1166
|
*/
|
1167
|
private File writeStreamToFile(File dir, String fileName, InputStream data)
|
1168
|
throws ServiceFailure {
|
1169
|
|
1170
|
File newFile = new File(dir, fileName);
|
1171
|
logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
|
1172
|
|
1173
|
try {
|
1174
|
if (newFile.createNewFile()) {
|
1175
|
// write data stream to desired file
|
1176
|
OutputStream os = new FileOutputStream(newFile);
|
1177
|
long length = IOUtils.copyLarge(data, os);
|
1178
|
os.flush();
|
1179
|
os.close();
|
1180
|
} else {
|
1181
|
logMetacat.debug("File creation failed, or file already exists.");
|
1182
|
throw new ServiceFailure("1190", "File already exists: " + fileName);
|
1183
|
}
|
1184
|
} catch (FileNotFoundException e) {
|
1185
|
logMetacat.debug("FNF: " + e.getMessage());
|
1186
|
throw new ServiceFailure("1190", "File not found: " + fileName + " "
|
1187
|
+ e.getMessage());
|
1188
|
} catch (IOException e) {
|
1189
|
logMetacat.debug("IOE: " + e.getMessage());
|
1190
|
throw new ServiceFailure("1190", "File was not written: " + fileName
|
1191
|
+ " " + e.getMessage());
|
1192
|
}
|
1193
|
|
1194
|
return newFile;
|
1195
|
}
|
1196
|
|
1197
|
}
|