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.FileInputStream;
|
28 |
|
|
import java.io.FileNotFoundException;
|
29 |
|
|
import java.io.FileOutputStream;
|
30 |
|
|
import java.io.IOException;
|
31 |
6174
|
cjones
|
import java.io.InputStream;
|
32 |
6241
|
cjones
|
import java.io.OutputStream;
|
33 |
6226
|
cjones
|
import java.sql.SQLException;
|
34 |
6225
|
leinfelder
|
import java.util.ArrayList;
|
35 |
6186
|
leinfelder
|
import java.util.Calendar;
|
36 |
6174
|
cjones
|
import java.util.Date;
|
37 |
6226
|
cjones
|
import java.util.Enumeration;
|
38 |
|
|
import java.util.Hashtable;
|
39 |
6194
|
leinfelder
|
import java.util.List;
|
40 |
6226
|
cjones
|
import java.util.Timer;
|
41 |
|
|
import java.util.TimerTask;
|
42 |
6186
|
leinfelder
|
import java.util.Vector;
|
43 |
6174
|
cjones
|
|
44 |
6226
|
cjones
|
import javax.servlet.http.HttpServletRequest;
|
45 |
|
|
|
46 |
6241
|
cjones
|
import org.apache.commons.io.IOUtils;
|
47 |
6186
|
leinfelder
|
import org.apache.log4j.Logger;
|
48 |
6241
|
cjones
|
import org.dataone.client.ObjectFormatCache;
|
49 |
|
|
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 |
6174
|
cjones
|
import org.dataone.service.types.AccessPolicy;
|
60 |
6194
|
leinfelder
|
import org.dataone.service.types.AccessRule;
|
61 |
6174
|
cjones
|
import org.dataone.service.types.Event;
|
62 |
|
|
import org.dataone.service.types.Identifier;
|
63 |
6225
|
leinfelder
|
import org.dataone.service.types.Group;
|
64 |
6174
|
cjones
|
import org.dataone.service.types.Log;
|
65 |
6186
|
leinfelder
|
import org.dataone.service.types.LogEntry;
|
66 |
|
|
import org.dataone.service.types.NodeReference;
|
67 |
6241
|
cjones
|
import org.dataone.service.types.ObjectFormat;
|
68 |
6174
|
cjones
|
import org.dataone.service.types.Permission;
|
69 |
6225
|
leinfelder
|
import org.dataone.service.types.Person;
|
70 |
6174
|
cjones
|
import org.dataone.service.types.Session;
|
71 |
6186
|
leinfelder
|
import org.dataone.service.types.Subject;
|
72 |
6245
|
leinfelder
|
import org.dataone.service.types.SubjectList;
|
73 |
6174
|
cjones
|
import org.dataone.service.types.SystemMetadata;
|
74 |
|
|
|
75 |
6241
|
cjones
|
import edu.ucsb.nceas.metacat.AccessionNumberException;
|
76 |
|
|
import edu.ucsb.nceas.metacat.DocumentImpl;
|
77 |
6186
|
leinfelder
|
import edu.ucsb.nceas.metacat.EventLog;
|
78 |
|
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
79 |
6194
|
leinfelder
|
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
|
80 |
6226
|
cjones
|
import edu.ucsb.nceas.metacat.McdbException;
|
81 |
|
|
import edu.ucsb.nceas.metacat.MetacatHandler;
|
82 |
|
|
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
|
83 |
|
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
84 |
6241
|
cjones
|
import edu.ucsb.nceas.metacat.replication.ForceReplicationHandler;
|
85 |
|
|
import edu.ucsb.nceas.metacat.util.SessionData;
|
86 |
6254
|
cjones
|
import edu.ucsb.nceas.metacat.util.SystemUtil;
|
87 |
6226
|
cjones
|
import edu.ucsb.nceas.utilities.ParseLSIDException;
|
88 |
|
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
89 |
6186
|
leinfelder
|
|
90 |
6174
|
cjones
|
public abstract class D1NodeService {
|
91 |
6227
|
cjones
|
|
92 |
6226
|
cjones
|
private static Logger logMetacat = Logger.getLogger(D1NodeService.class);
|
93 |
6186
|
leinfelder
|
|
94 |
6241
|
cjones
|
/* the metacat URL as a string (including ending /d1 service)*/
|
95 |
6254
|
cjones
|
protected String metacatUrl;
|
96 |
6241
|
cjones
|
|
97 |
6226
|
cjones
|
/* reference to the metacat handler */
|
98 |
|
|
private MetacatHandler handler;
|
99 |
6174
|
cjones
|
|
100 |
6226
|
cjones
|
/* parameters set in the incoming request */
|
101 |
|
|
private Hashtable<String, String[]> params;
|
102 |
|
|
|
103 |
6241
|
cjones
|
/**
|
104 |
|
|
* Constructor - used to set the metacatUrl from a subclass extending D1NodeService
|
105 |
|
|
*
|
106 |
|
|
* @param metacatUrl - the URL of the metacat service, including the ending /d1
|
107 |
|
|
*/
|
108 |
6254
|
cjones
|
public D1NodeService() {
|
109 |
|
|
this.metacatUrl = "";
|
110 |
|
|
|
111 |
|
|
try {
|
112 |
|
|
this.metacatUrl = SystemUtil.getContextURL() +
|
113 |
|
|
PropertyService.getProperty("dataone.serviceName");
|
114 |
|
|
} catch (PropertyNotFoundException e) {
|
115 |
|
|
throw new RuntimeException("Error getting the servlet url in D1NodeService: " +
|
116 |
|
|
e.getMessage());
|
117 |
|
|
|
118 |
|
|
}
|
119 |
6241
|
cjones
|
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
/**
|
123 |
|
|
* Adds a new object to the Node, where the object is either a data
|
124 |
|
|
* object or a science metadata object. This method is called by clients
|
125 |
|
|
* to create new data objects on Member Nodes or internally for Coordinating
|
126 |
|
|
* Nodes
|
127 |
|
|
*
|
128 |
|
|
* @param session - the Session object containing the credentials for the Subject
|
129 |
|
|
* @param pid - The object identifier to be created
|
130 |
|
|
* @param object - the object bytes
|
131 |
|
|
* @param sysmeta - the system metadata that describes the object
|
132 |
|
|
*
|
133 |
|
|
* @return pid - the object identifier created
|
134 |
|
|
*
|
135 |
|
|
* @throws InvalidToken
|
136 |
|
|
* @throws ServiceFailure
|
137 |
|
|
* @throws NotAuthorized
|
138 |
|
|
* @throws IdentifierNotUnique
|
139 |
|
|
* @throws UnsupportedType
|
140 |
|
|
* @throws InsufficientResources
|
141 |
|
|
* @throws InvalidSystemMetadata
|
142 |
|
|
* @throws NotImplemented
|
143 |
|
|
* @throws InvalidRequest
|
144 |
|
|
*/
|
145 |
|
|
public Identifier create(Session session, Identifier pid, InputStream object,
|
146 |
|
|
SystemMetadata sysmeta)
|
147 |
|
|
throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique,
|
148 |
|
|
UnsupportedType, InsufficientResources, InvalidSystemMetadata,
|
149 |
|
|
NotImplemented, InvalidRequest {
|
150 |
6174
|
cjones
|
|
151 |
6241
|
cjones
|
Identifier resultPid = null;
|
152 |
|
|
String localId = null;
|
153 |
|
|
Subject subject = session.getSubject();
|
154 |
|
|
boolean allowed = false;
|
155 |
|
|
|
156 |
|
|
// be sure the user is authenticated for create()
|
157 |
|
|
if (subject.getValue() == null ||
|
158 |
|
|
subject.getValue().toLowerCase().equals("public") ) {
|
159 |
|
|
throw new NotAuthorized("1100", "The provided identity does not have " +
|
160 |
|
|
"permission to WRITE to the Member Node.");
|
161 |
|
|
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
// verify that pid == SystemMetadata.getIdentifier()
|
165 |
|
|
logMetacat.debug("Comparing pid|sysmeta_pid: " +
|
166 |
|
|
pid.getValue() + "|" + sysmeta.getIdentifier().getValue());
|
167 |
|
|
if (!pid.getValue().equals(sysmeta.getIdentifier().getValue())) {
|
168 |
|
|
throw new InvalidSystemMetadata("1180",
|
169 |
|
|
"The supplied system metadata is invalid. " +
|
170 |
|
|
"The identifier " + pid.getValue() + " does not match identifier" +
|
171 |
|
|
"in the system metadata identified by " +
|
172 |
|
|
sysmeta.getIdentifier().getValue() + ".");
|
173 |
|
|
|
174 |
|
|
}
|
175 |
|
|
|
176 |
|
|
logMetacat.debug("Checking if identifier exists...");
|
177 |
|
|
// Check that the identifier does not already exist
|
178 |
|
|
IdentifierManager im = IdentifierManager.getInstance();
|
179 |
|
|
if (im.identifierExists(pid.getValue())) {
|
180 |
|
|
throw new IdentifierNotUnique("1120",
|
181 |
|
|
"The requested identifier " + pid.getValue() +
|
182 |
|
|
" is already used by another object and" +
|
183 |
|
|
"therefore can not be used for this object. Clients should choose" +
|
184 |
|
|
"a new identifier that is unique and retry the operation or " +
|
185 |
|
|
"use CN_crud.reserveIdentifier() to reserve one.");
|
186 |
|
|
|
187 |
|
|
}
|
188 |
|
|
|
189 |
|
|
// check for permission
|
190 |
|
|
try {
|
191 |
|
|
allowed = isAuthorized(session, pid, Permission.WRITE);
|
192 |
|
|
|
193 |
|
|
} catch (NotFound e) {
|
194 |
|
|
// The identifier doesn't exist, writing should be fine.
|
195 |
|
|
allowed = true;
|
196 |
|
|
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
// we have the go ahead
|
200 |
|
|
if ( allowed ) {
|
201 |
|
|
|
202 |
|
|
// Science metadata (XML) or science data object?
|
203 |
|
|
// TODO: there are cases where certain object formats are science metadata
|
204 |
|
|
// but are not XML (netCDF ...). Handle this.
|
205 |
|
|
if ( isScienceMetadata(sysmeta) ) {
|
206 |
|
|
|
207 |
|
|
// CASE METADATA:
|
208 |
|
|
String objectAsXML = "";
|
209 |
|
|
try {
|
210 |
|
|
objectAsXML = IOUtils.toString(object, "UTF-8");
|
211 |
|
|
localId = insertOrUpdateDocument(objectAsXML, pid, session, "insert");
|
212 |
|
|
//localId = im.getLocalId(pid.getValue());
|
213 |
|
|
|
214 |
|
|
} catch (IOException e) {
|
215 |
|
|
String msg = "The Node is unable to create the object. " +
|
216 |
|
|
"There was a problem converting the object to XML";
|
217 |
|
|
logMetacat.info(msg);
|
218 |
|
|
throw new ServiceFailure("1190", msg + ": " + e.getMessage());
|
219 |
|
|
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
|
225 |
|
|
} else {
|
226 |
|
|
|
227 |
|
|
// DEFAULT CASE: DATA (needs to be checked and completed)
|
228 |
|
|
//localId = insertDataObject(object, pid, session);
|
229 |
|
|
|
230 |
|
|
}
|
231 |
|
|
|
232 |
|
|
// setting the resulting identifier failed
|
233 |
|
|
if (resultPid == null ) {
|
234 |
|
|
throw new ServiceFailure("1190", "The Node is unable to create the object. " +
|
235 |
|
|
"The resulting identifier was null.");
|
236 |
|
|
|
237 |
|
|
}
|
238 |
|
|
|
239 |
|
|
return resultPid;
|
240 |
|
|
}
|
241 |
|
|
|
242 |
6227
|
cjones
|
/**
|
243 |
|
|
* Return the log records associated with a given event between the start and
|
244 |
|
|
* end dates listed given a particular Subject listed in the Session
|
245 |
|
|
*
|
246 |
|
|
* @param session - the Session object containing the credentials for the Subject
|
247 |
|
|
* @param fromDate - the start date of the desired log records
|
248 |
|
|
* @param toDate - the end date of the desired log records
|
249 |
|
|
* @param event - restrict log records of a specific event type
|
250 |
|
|
* @param start - zero based offset from the first record in the
|
251 |
|
|
* set of matching log records. Used to assist with
|
252 |
|
|
* paging the response.
|
253 |
|
|
* @param count - maximum number of log records to return in the response.
|
254 |
|
|
* Used to assist with paging the response.
|
255 |
|
|
*
|
256 |
|
|
* @return the desired log records
|
257 |
|
|
*
|
258 |
|
|
* @throws InvalidToken
|
259 |
|
|
* @throws ServiceFailure
|
260 |
|
|
* @throws NotAuthorized
|
261 |
|
|
* @throws InvalidRequest
|
262 |
|
|
* @throws NotImplemented
|
263 |
|
|
*/
|
264 |
|
|
public Log getLogRecords(Session session, Date fromDate, Date toDate,
|
265 |
6179
|
cjones
|
Event event, Integer start, Integer count) throws InvalidToken, ServiceFailure,
|
266 |
6227
|
cjones
|
NotAuthorized, InvalidRequest, NotImplemented {
|
267 |
6174
|
cjones
|
|
268 |
6186
|
leinfelder
|
|
269 |
6227
|
cjones
|
Log log = new Log();
|
270 |
|
|
Vector<LogEntry> logs = new Vector<LogEntry>();
|
271 |
|
|
IdentifierManager im = IdentifierManager.getInstance();
|
272 |
|
|
EventLog el = EventLog.getInstance();
|
273 |
|
|
if (fromDate == null) {
|
274 |
|
|
logMetacat.debug("setting fromdate from null");
|
275 |
|
|
fromDate = new Date(1);
|
276 |
|
|
}
|
277 |
|
|
if (toDate == null) {
|
278 |
|
|
logMetacat.debug("setting todate from null");
|
279 |
|
|
toDate = new Date();
|
280 |
|
|
}
|
281 |
6186
|
leinfelder
|
|
282 |
6227
|
cjones
|
logMetacat.debug("fromDate: " + fromDate);
|
283 |
|
|
logMetacat.debug("toDate: " + toDate);
|
284 |
6186
|
leinfelder
|
|
285 |
6227
|
cjones
|
String report = el.getReport(null, null, null, null,
|
286 |
|
|
new java.sql.Timestamp(fromDate.getTime()),
|
287 |
|
|
new java.sql.Timestamp(toDate.getTime()), false);
|
288 |
6186
|
leinfelder
|
|
289 |
6227
|
cjones
|
logMetacat.debug("report: " + report);
|
290 |
6186
|
leinfelder
|
|
291 |
6227
|
cjones
|
String logEntry = "<logEntry>";
|
292 |
|
|
String endLogEntry = "</logEntry>";
|
293 |
|
|
int startIndex = 0;
|
294 |
|
|
int foundIndex = report.indexOf(logEntry, startIndex);
|
295 |
|
|
while (foundIndex != -1) {
|
296 |
|
|
// parse out each entry
|
297 |
|
|
int endEntryIndex = report.indexOf(endLogEntry, foundIndex);
|
298 |
|
|
String entry = report.substring(foundIndex, endEntryIndex);
|
299 |
|
|
logMetacat.debug("entry: " + entry);
|
300 |
|
|
startIndex = endEntryIndex + endLogEntry.length();
|
301 |
|
|
foundIndex = report.indexOf(logEntry, startIndex);
|
302 |
6186
|
leinfelder
|
|
303 |
6227
|
cjones
|
String entryId = getLogEntryField("entryid", entry);
|
304 |
|
|
String ipAddress = getLogEntryField("ipAddress", entry);
|
305 |
|
|
String principal = getLogEntryField("principal", entry);
|
306 |
|
|
String docid = getLogEntryField("docid", entry);
|
307 |
|
|
String eventS = getLogEntryField("event", entry);
|
308 |
|
|
String dateLogged = getLogEntryField("dateLogged", entry);
|
309 |
6186
|
leinfelder
|
|
310 |
6227
|
cjones
|
LogEntry le = new LogEntry();
|
311 |
6186
|
leinfelder
|
|
312 |
6227
|
cjones
|
Event e = Event.convert(eventS);
|
313 |
|
|
if (e == null) { // skip any events that are not Dataone Crud events
|
314 |
|
|
continue;
|
315 |
|
|
}
|
316 |
|
|
le.setEvent(e);
|
317 |
|
|
Identifier entryid = new Identifier();
|
318 |
|
|
entryid.setValue(entryId);
|
319 |
|
|
le.setEntryId(entryid);
|
320 |
|
|
Identifier identifier = new Identifier();
|
321 |
|
|
try {
|
322 |
6241
|
cjones
|
logMetacat.debug("converting docid '" + docid + "' to a pid.");
|
323 |
6227
|
cjones
|
if (docid == null || docid.trim().equals("") || docid.trim().equals("null")) {
|
324 |
|
|
continue;
|
325 |
|
|
}
|
326 |
|
|
docid = docid.substring(0, docid.lastIndexOf("."));
|
327 |
|
|
identifier.setValue(im.getGUID(docid, im.getLatestRevForLocalId(docid)));
|
328 |
|
|
} catch (Exception ex) {
|
329 |
6241
|
cjones
|
// try to get the pid, if that doesn't
|
330 |
6227
|
cjones
|
// work, just use the local id
|
331 |
|
|
// throw new ServiceFailure("1030",
|
332 |
6241
|
cjones
|
// "Error getting pid for localId " +
|
333 |
6227
|
cjones
|
// docid + ": " + ex.getMessage());\
|
334 |
6186
|
leinfelder
|
|
335 |
6241
|
cjones
|
// skip it if the pid can't be found
|
336 |
6227
|
cjones
|
continue;
|
337 |
|
|
}
|
338 |
6186
|
leinfelder
|
|
339 |
6227
|
cjones
|
le.setIdentifier(identifier);
|
340 |
|
|
le.setIpAddress(ipAddress);
|
341 |
|
|
Calendar c = Calendar.getInstance();
|
342 |
|
|
String year = dateLogged.substring(0, 4);
|
343 |
|
|
String month = dateLogged.substring(5, 7);
|
344 |
|
|
String date = dateLogged.substring(8, 10);
|
345 |
|
|
logMetacat.debug("year: " + year + " month: " + month + " day: " + date);
|
346 |
|
|
c.set(new Integer(year).intValue(), new Integer(month).intValue(),
|
347 |
|
|
new Integer(date).intValue());
|
348 |
|
|
Date logDate = c.getTime();
|
349 |
|
|
le.setDateLogged(logDate);
|
350 |
|
|
NodeReference memberNode = new NodeReference();
|
351 |
|
|
memberNode.setValue(ipAddress);
|
352 |
|
|
le.setMemberNode(memberNode);
|
353 |
|
|
Subject princ = new Subject();
|
354 |
|
|
princ.setValue(principal);
|
355 |
|
|
le.setSubject(princ);
|
356 |
|
|
le.setUserAgent("metacat/RESTService");
|
357 |
6186
|
leinfelder
|
|
358 |
6227
|
cjones
|
if (event == null) {
|
359 |
|
|
logs.add(le);
|
360 |
|
|
}
|
361 |
6186
|
leinfelder
|
|
362 |
6227
|
cjones
|
if (event != null
|
363 |
|
|
&& e.toString().toLowerCase().trim().equals(
|
364 |
|
|
event.toString().toLowerCase().trim())) {
|
365 |
|
|
logs.add(le);
|
366 |
|
|
}
|
367 |
|
|
}
|
368 |
6186
|
leinfelder
|
|
369 |
6227
|
cjones
|
log.setLogEntryList(logs);
|
370 |
|
|
logMetacat.info("getLogRecords");
|
371 |
|
|
return log;
|
372 |
|
|
}
|
373 |
6241
|
cjones
|
|
374 |
6227
|
cjones
|
/**
|
375 |
|
|
* Return the object identified by the given object identifier
|
376 |
|
|
*
|
377 |
|
|
* @param session - the Session object containing the credentials for the Subject
|
378 |
|
|
* @param pid - the object identifier for the given object
|
379 |
|
|
*
|
380 |
|
|
* TODO: The D1 Authorization API doesn't provide information on which
|
381 |
|
|
* authentication system the Subject belongs to, and so it's not possible to
|
382 |
|
|
* discern which Person or Group is a valid KNB LDAP DN. Fix this.
|
383 |
|
|
*
|
384 |
|
|
* @return inputStream - the input stream of the given object
|
385 |
|
|
*
|
386 |
|
|
* @throws InvalidToken
|
387 |
|
|
* @throws ServiceFailure
|
388 |
|
|
* @throws NotAuthorized
|
389 |
|
|
* @throws InvalidRequest
|
390 |
|
|
* @throws NotImplemented
|
391 |
|
|
*/
|
392 |
|
|
public InputStream get(Session session, Identifier pid)
|
393 |
|
|
throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
394 |
|
|
NotImplemented, InvalidRequest {
|
395 |
6226
|
cjones
|
|
396 |
6227
|
cjones
|
InputStream inputStream = null; // bytes to be returned
|
397 |
6226
|
cjones
|
handler = new MetacatHandler(new Timer());
|
398 |
6227
|
cjones
|
boolean allowed = false;
|
399 |
|
|
String localId; // the metacat docid for the pid
|
400 |
6226
|
cjones
|
|
401 |
6227
|
cjones
|
// get the local docid from Metacat
|
402 |
|
|
try {
|
403 |
|
|
localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
|
404 |
|
|
|
405 |
6226
|
cjones
|
} catch (McdbDocNotFoundException e) {
|
406 |
|
|
throw new NotFound("1020", "The object specified by " +
|
407 |
6227
|
cjones
|
pid.getValue() +
|
408 |
|
|
"does not exist at this node.");
|
409 |
6226
|
cjones
|
}
|
410 |
|
|
|
411 |
|
|
// check for authorization
|
412 |
6227
|
cjones
|
allowed = isAuthorized(session, pid, Permission.READ);
|
413 |
|
|
|
414 |
|
|
// if the person is authorized, perform the read
|
415 |
|
|
if ( allowed ) {
|
416 |
|
|
|
417 |
|
|
// get the object bytes
|
418 |
|
|
// TODO: stream to file to stream conversion throughout Metacat needs to
|
419 |
|
|
// be resolved
|
420 |
6226
|
cjones
|
File tmpDir;
|
421 |
|
|
try
|
422 |
|
|
{
|
423 |
|
|
tmpDir = new File(PropertyService.getProperty("application.tempDir"));
|
424 |
|
|
}
|
425 |
|
|
catch(PropertyNotFoundException pnfe)
|
426 |
|
|
{
|
427 |
|
|
logMetacat.error("D1NodeService.get(): " +
|
428 |
|
|
"application.tmpDir not found. Using /tmp instead.");
|
429 |
|
|
tmpDir = new File("/tmp");
|
430 |
|
|
}
|
431 |
|
|
|
432 |
|
|
Date d = new Date();
|
433 |
|
|
final File outputFile = new File(tmpDir, "metacat.output." + d.getTime());
|
434 |
|
|
FileOutputStream dataSink;
|
435 |
|
|
try {
|
436 |
6227
|
cjones
|
dataSink = new FileOutputStream(outputFile);
|
437 |
6233
|
cjones
|
handler.read(localId);
|
438 |
6174
|
cjones
|
|
439 |
6226
|
cjones
|
} catch (FileNotFoundException e) {
|
440 |
|
|
throw new ServiceFailure("1020", "The object specified by " +
|
441 |
|
|
pid.getValue() +
|
442 |
|
|
"could not be returned due to a file read error: " +
|
443 |
|
|
e.getMessage());
|
444 |
|
|
|
445 |
|
|
} catch (PropertyNotFoundException e) {
|
446 |
|
|
throw new ServiceFailure("1020", "The object specified by " +
|
447 |
|
|
pid.getValue() +
|
448 |
|
|
"could not be returned due to an internal error: " +
|
449 |
|
|
e.getMessage());
|
450 |
|
|
|
451 |
|
|
} catch (ClassNotFoundException e) {
|
452 |
|
|
throw new ServiceFailure("1020", "The object specified by " +
|
453 |
|
|
pid.getValue() +
|
454 |
|
|
"could not be returned due to an internal error: " +
|
455 |
|
|
e.getMessage());
|
456 |
|
|
|
457 |
|
|
} catch (IOException e) {
|
458 |
|
|
throw new ServiceFailure("1020", "The object specified by " +
|
459 |
|
|
pid.getValue() +
|
460 |
|
|
"could not be returned due to a file read error: " +
|
461 |
|
|
e.getMessage());
|
462 |
|
|
|
463 |
|
|
} catch (SQLException e) {
|
464 |
|
|
throw new ServiceFailure("1020", "The object specified by " +
|
465 |
|
|
pid.getValue() +
|
466 |
|
|
"could not be returned due to a database error: " +
|
467 |
|
|
e.getMessage());
|
468 |
|
|
|
469 |
|
|
} catch (McdbException e) {
|
470 |
|
|
throw new ServiceFailure("1020", "The object specified by " +
|
471 |
|
|
pid.getValue() +
|
472 |
|
|
"could not be returned due to database error: " +
|
473 |
|
|
e.getMessage());
|
474 |
|
|
|
475 |
|
|
} catch (ParseLSIDException e) {
|
476 |
|
|
throw new ServiceFailure("1020", "The object specified by " +
|
477 |
|
|
pid.getValue() +
|
478 |
|
|
"could not be returned due to a parse error: " +
|
479 |
|
|
e.getMessage());
|
480 |
|
|
|
481 |
|
|
}
|
482 |
|
|
|
483 |
|
|
//set a timer to clean up the temp files
|
484 |
|
|
Timer t = new Timer();
|
485 |
|
|
TimerTask tt = new TimerTask() {
|
486 |
|
|
@Override
|
487 |
|
|
public void run()
|
488 |
|
|
{
|
489 |
|
|
outputFile.delete();
|
490 |
|
|
}
|
491 |
|
|
};
|
492 |
|
|
t.schedule(tt, 20000); //schedule after 20 secs
|
493 |
|
|
|
494 |
|
|
try {
|
495 |
6227
|
cjones
|
inputStream = new FileInputStream(outputFile);
|
496 |
6226
|
cjones
|
} catch (FileNotFoundException e) {
|
497 |
|
|
throw new ServiceFailure("1020", "The object specified by " +
|
498 |
|
|
pid.getValue() +
|
499 |
|
|
"could not be returned due to a file read error: " +
|
500 |
|
|
e.getMessage());
|
501 |
|
|
|
502 |
6227
|
cjones
|
}
|
503 |
|
|
|
504 |
|
|
}
|
505 |
6226
|
cjones
|
|
506 |
6227
|
cjones
|
// if we fail to set the input stream
|
507 |
|
|
if ( inputStream == null ) {
|
508 |
6226
|
cjones
|
throw new NotFound("1020", "The object specified by " +
|
509 |
|
|
pid.getValue() +
|
510 |
|
|
"does not exist at this node.");
|
511 |
6227
|
cjones
|
}
|
512 |
|
|
|
513 |
|
|
return inputStream;
|
514 |
|
|
}
|
515 |
6174
|
cjones
|
|
516 |
6227
|
cjones
|
/**
|
517 |
|
|
* Return the system metadata for a given object
|
518 |
|
|
*
|
519 |
|
|
* @param session - the Session object containing the credentials for the Subject
|
520 |
|
|
* @param pid - the object identifier for the given object
|
521 |
|
|
*
|
522 |
|
|
* @return inputStream - the input stream of the given system metadata object
|
523 |
|
|
*
|
524 |
|
|
* @throws InvalidToken
|
525 |
|
|
* @throws ServiceFailure
|
526 |
|
|
* @throws NotAuthorized
|
527 |
|
|
* @throws NotFound
|
528 |
|
|
* @throws InvalidRequest
|
529 |
|
|
* @throws NotImplemented
|
530 |
|
|
*/
|
531 |
|
|
public SystemMetadata getSystemMetadata(Session session, Identifier pid)
|
532 |
6174
|
cjones
|
throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
533 |
|
|
InvalidRequest, NotImplemented {
|
534 |
|
|
|
535 |
6227
|
cjones
|
if (!isAuthorized(session, pid, Permission.READ)) {
|
536 |
|
|
throw new NotAuthorized("1400", Permission.READ + " not allowed on " + pid.getValue());
|
537 |
|
|
}
|
538 |
|
|
SystemMetadata systemMetadata = null;
|
539 |
|
|
try {
|
540 |
|
|
systemMetadata = IdentifierManager.getInstance().getSystemMetadata(pid.getValue());
|
541 |
|
|
} catch (McdbDocNotFoundException e) {
|
542 |
|
|
throw new NotFound("1420", "No record found for: " + pid.getValue());
|
543 |
|
|
}
|
544 |
|
|
|
545 |
|
|
return systemMetadata;
|
546 |
6174
|
cjones
|
}
|
547 |
6241
|
cjones
|
|
548 |
6227
|
cjones
|
/**
|
549 |
|
|
* Set access for a given object using the object identifier and a Subject
|
550 |
|
|
* under a given Session.
|
551 |
|
|
*
|
552 |
|
|
* @param session - the Session object containing the credentials for the Subject
|
553 |
|
|
* @param pid - the object identifier for the given object to apply the policy
|
554 |
|
|
* @param policy - the access policy to be applied
|
555 |
|
|
*
|
556 |
|
|
* @return true if the application of the policy succeeds
|
557 |
|
|
* @throws InvalidToken
|
558 |
|
|
* @throws ServiceFailure
|
559 |
|
|
* @throws NotFound
|
560 |
|
|
* @throws NotAuthorized
|
561 |
|
|
* @throws NotImplemented
|
562 |
|
|
* @throws InvalidRequest
|
563 |
|
|
*/
|
564 |
|
|
public boolean setAccessPolicy(Session session, Identifier pid,
|
565 |
6179
|
cjones
|
AccessPolicy accessPolicy)
|
566 |
6227
|
cjones
|
throws InvalidToken, ServiceFailure, NotFound, NotAuthorized,
|
567 |
|
|
NotImplemented, InvalidRequest {
|
568 |
6174
|
cjones
|
|
569 |
6227
|
cjones
|
boolean success = false;
|
570 |
|
|
|
571 |
|
|
// get the subject
|
572 |
|
|
Subject subject = session.getSubject();
|
573 |
|
|
// get the system metadata
|
574 |
6241
|
cjones
|
String pidStr = pid.getValue();
|
575 |
6227
|
cjones
|
|
576 |
|
|
// are we allowed to do this?
|
577 |
|
|
if (!isAuthorized(session, pid, Permission.CHANGE_PERMISSION)) {
|
578 |
6241
|
cjones
|
throw new NotAuthorized("4420", "not allowed by " + subject.getValue() + " on " + pidStr);
|
579 |
6227
|
cjones
|
}
|
580 |
|
|
|
581 |
|
|
SystemMetadata systemMetadata = null;
|
582 |
|
|
try {
|
583 |
6241
|
cjones
|
systemMetadata = IdentifierManager.getInstance().getSystemMetadata(pidStr);
|
584 |
6227
|
cjones
|
} catch (McdbDocNotFoundException e) {
|
585 |
6241
|
cjones
|
throw new NotFound("4400", "No record found for: " + pid);
|
586 |
6227
|
cjones
|
}
|
587 |
|
|
|
588 |
|
|
// set the access policy
|
589 |
|
|
systemMetadata.setAccessPolicy(accessPolicy);
|
590 |
|
|
|
591 |
|
|
// update the metadata
|
592 |
|
|
try {
|
593 |
|
|
IdentifierManager.getInstance().updateSystemMetadata(systemMetadata);
|
594 |
|
|
success = true;
|
595 |
|
|
} catch (McdbDocNotFoundException e) {
|
596 |
|
|
throw new ServiceFailure("4430", e.getMessage());
|
597 |
|
|
}
|
598 |
6194
|
leinfelder
|
|
599 |
6227
|
cjones
|
return success;
|
600 |
|
|
}
|
601 |
|
|
|
602 |
|
|
/**
|
603 |
|
|
* Test if the user identified by the provided token has authorization
|
604 |
|
|
* for operation on the specified object.
|
605 |
|
|
*
|
606 |
|
|
* @param session - the Session object containing the credentials for the Subject
|
607 |
|
|
* @param pid - The identifer of the resource for which access is being checked
|
608 |
|
|
* @param operation - The type of operation which is being requested for the given pid
|
609 |
|
|
*
|
610 |
|
|
* @return true if the operation is allowed
|
611 |
|
|
*
|
612 |
|
|
* @throws ServiceFailure
|
613 |
|
|
* @throws InvalidToken
|
614 |
|
|
* @throws NotFound
|
615 |
|
|
* @throws NotAuthorized
|
616 |
|
|
* @throws NotImplemented
|
617 |
|
|
* @throws InvalidRequest
|
618 |
|
|
*/
|
619 |
|
|
public boolean isAuthorized(Session session, Identifier pid, Permission permission)
|
620 |
|
|
throws ServiceFailure, InvalidToken, NotFound, NotAuthorized,
|
621 |
|
|
NotImplemented, InvalidRequest {
|
622 |
6174
|
cjones
|
|
623 |
6227
|
cjones
|
boolean allowed = false;
|
624 |
|
|
|
625 |
|
|
// get the subjects from the session
|
626 |
|
|
List<Subject> subjects = new ArrayList<Subject>();
|
627 |
6245
|
leinfelder
|
Subject subject = session.getSubject();
|
628 |
|
|
if (subject != null) {
|
629 |
|
|
subjects.add(subject);
|
630 |
6227
|
cjones
|
}
|
631 |
6245
|
leinfelder
|
SubjectList subjectList = session.getSubjectList();
|
632 |
|
|
if (subjectList != null) {
|
633 |
|
|
List<Person> personList = subjectList.getPersonList();
|
634 |
|
|
if (personList != null) {
|
635 |
|
|
for (Person p: personList) {
|
636 |
|
|
subjects.add(p.getSubject());
|
637 |
|
|
}
|
638 |
|
|
}
|
639 |
|
|
List<Group> groupList = subjectList.getGroupList();
|
640 |
|
|
if (groupList != null) {
|
641 |
|
|
for (Group g: groupList) {
|
642 |
|
|
subjects.add(g.getSubject());
|
643 |
|
|
}
|
644 |
|
|
}
|
645 |
6227
|
cjones
|
}
|
646 |
|
|
|
647 |
|
|
// get the system metadata
|
648 |
6241
|
cjones
|
String pidStr = pid.getValue();
|
649 |
6227
|
cjones
|
SystemMetadata systemMetadata = null;
|
650 |
|
|
try {
|
651 |
6241
|
cjones
|
systemMetadata = IdentifierManager.getInstance().getSystemMetadata(pidStr);
|
652 |
6227
|
cjones
|
} catch (McdbDocNotFoundException e) {
|
653 |
6245
|
leinfelder
|
throw new NotFound("1800", "No record found for " + pidStr);
|
654 |
6227
|
cjones
|
}
|
655 |
6245
|
leinfelder
|
|
656 |
|
|
try {
|
657 |
|
|
List<AccessRule> allows = systemMetadata.getAccessPolicy().getAllowList();
|
658 |
|
|
for (AccessRule accessRule: allows) {
|
659 |
|
|
for (Subject s: subjects) {
|
660 |
|
|
if (accessRule.getSubjectList().contains(s)) {
|
661 |
|
|
allowed = accessRule.getPermissionList().contains(permission);
|
662 |
|
|
if (allowed) {
|
663 |
|
|
break;
|
664 |
|
|
}
|
665 |
|
|
}
|
666 |
|
|
}
|
667 |
|
|
}
|
668 |
|
|
} catch (Exception e) {
|
669 |
|
|
// catch all for errors - safe side should be to deny the access
|
670 |
|
|
logMetacat.error("Problem checking authorization - defaulting to deny", e);
|
671 |
|
|
allowed = false;
|
672 |
|
|
}
|
673 |
6227
|
cjones
|
|
674 |
6245
|
leinfelder
|
// throw or return?
|
675 |
6227
|
cjones
|
if (!allowed) {
|
676 |
6245
|
leinfelder
|
throw new NotAuthorized("1820", permission + " not allowed on " + pidStr);
|
677 |
6227
|
cjones
|
}
|
678 |
6245
|
leinfelder
|
|
679 |
6227
|
cjones
|
return allowed;
|
680 |
6241
|
cjones
|
|
681 |
6227
|
cjones
|
}
|
682 |
|
|
|
683 |
6256
|
cjones
|
/*
|
684 |
6227
|
cjones
|
* parse a logEntry and get the relevant field from it
|
685 |
|
|
*
|
686 |
|
|
* @param fieldname
|
687 |
|
|
* @param entry
|
688 |
|
|
* @return
|
689 |
|
|
*/
|
690 |
|
|
private String getLogEntryField(String fieldname, String entry) {
|
691 |
|
|
String begin = "<" + fieldname + ">";
|
692 |
|
|
String end = "</" + fieldname + ">";
|
693 |
|
|
// logMetacat.debug("looking for " + begin + " and " + end +
|
694 |
|
|
// " in entry " + entry);
|
695 |
|
|
String s = entry.substring(entry.indexOf(begin) + begin.length(), entry
|
696 |
|
|
.indexOf(end));
|
697 |
|
|
logMetacat.debug("entry " + fieldname + " : " + s);
|
698 |
|
|
return s;
|
699 |
|
|
}
|
700 |
6174
|
cjones
|
|
701 |
6257
|
cjones
|
/**
|
702 |
6241
|
cjones
|
* Determine if a given object should be treated as an XML science metadata
|
703 |
|
|
* object.
|
704 |
|
|
*
|
705 |
|
|
* @param sysmeta - the SystemMetadata describing the object
|
706 |
|
|
* @return true if the object should be treated as science metadata
|
707 |
|
|
*/
|
708 |
6257
|
cjones
|
protected boolean isScienceMetadata(SystemMetadata sysmeta) {
|
709 |
6241
|
cjones
|
|
710 |
|
|
ObjectFormat objectFormat = null;
|
711 |
|
|
boolean isScienceMetadata = false;
|
712 |
|
|
|
713 |
|
|
try {
|
714 |
|
|
objectFormat = ObjectFormatCache.getInstance().getFormat(sysmeta.getObjectFormat().getFmtid());
|
715 |
|
|
isScienceMetadata = objectFormat.isScienceMetadata();
|
716 |
|
|
|
717 |
|
|
} catch (InvalidRequest e) {
|
718 |
|
|
logMetacat.debug("There was a problem determining if the object identified by" +
|
719 |
|
|
sysmeta.getIdentifier().getValue() +
|
720 |
|
|
" is science metadata: " + e.getMessage());
|
721 |
|
|
|
722 |
|
|
} catch (ServiceFailure e) {
|
723 |
|
|
logMetacat.debug("There was a problem determining if the object identified by" +
|
724 |
|
|
sysmeta.getIdentifier().getValue() +
|
725 |
|
|
" is science metadata: " + e.getMessage());
|
726 |
|
|
|
727 |
|
|
} catch (NotFound e) {
|
728 |
|
|
logMetacat.debug("There was a problem determining if the object identified by" +
|
729 |
|
|
sysmeta.getIdentifier().getValue() +
|
730 |
|
|
" is science metadata: " + e.getMessage());
|
731 |
|
|
|
732 |
|
|
} catch (InsufficientResources e) {
|
733 |
|
|
logMetacat.debug("There was a problem determining if the object identified by" +
|
734 |
|
|
sysmeta.getIdentifier().getValue() +
|
735 |
|
|
" is science metadata: " + e.getMessage());
|
736 |
|
|
|
737 |
|
|
} catch (NotImplemented e) {
|
738 |
|
|
logMetacat.debug("There was a problem determining if the object identified by" +
|
739 |
|
|
sysmeta.getIdentifier().getValue() +
|
740 |
|
|
" is science metadata: " + e.getMessage());
|
741 |
|
|
|
742 |
|
|
}
|
743 |
|
|
|
744 |
|
|
return isScienceMetadata;
|
745 |
6226
|
cjones
|
|
746 |
6241
|
cjones
|
}
|
747 |
|
|
|
748 |
|
|
/**
|
749 |
|
|
* Insert or update an XML document into Metacat
|
750 |
|
|
*
|
751 |
|
|
* @param xml - the XML document to insert or update
|
752 |
|
|
* @param pid - the identifier to be used for the resulting object
|
753 |
|
|
*
|
754 |
|
|
* @return localId - the resulting docid of the document created or updated
|
755 |
|
|
*
|
756 |
|
|
*/
|
757 |
|
|
protected String insertOrUpdateDocument(String xml, Identifier pid,
|
758 |
|
|
Session session, String insertOrUpdate)
|
759 |
|
|
throws ServiceFailure {
|
760 |
|
|
|
761 |
|
|
logMetacat.debug("Starting to insert xml document...");
|
762 |
|
|
IdentifierManager im = IdentifierManager.getInstance();
|
763 |
|
|
|
764 |
|
|
// generate pid/localId pair for sysmeta
|
765 |
|
|
String localId = null;
|
766 |
|
|
|
767 |
|
|
if(insertOrUpdate.equals("insert")) {
|
768 |
|
|
localId = im.generateLocalId(pid.getValue(), 1);
|
769 |
|
|
|
770 |
|
|
} else {
|
771 |
|
|
//localid should already exist in the identifier table, so just find it
|
772 |
|
|
try {
|
773 |
|
|
logMetacat.debug("Updating pid " + pid.getValue());
|
774 |
|
|
logMetacat.debug("looking in identifier table for pid " + pid.getValue());
|
775 |
|
|
|
776 |
|
|
localId = im.getLocalId(pid.getValue());
|
777 |
|
|
|
778 |
|
|
logMetacat.debug("localId: " + localId);
|
779 |
|
|
//increment the revision
|
780 |
|
|
String docid = localId.substring(0, localId.lastIndexOf("."));
|
781 |
|
|
String revS = localId.substring(localId.lastIndexOf(".") + 1, localId.length());
|
782 |
|
|
int rev = new Integer(revS).intValue();
|
783 |
|
|
rev++;
|
784 |
|
|
docid = docid + "." + rev;
|
785 |
|
|
localId = docid;
|
786 |
|
|
logMetacat.debug("incremented localId: " + localId);
|
787 |
|
|
|
788 |
|
|
} catch(McdbDocNotFoundException e) {
|
789 |
|
|
throw new ServiceFailure("1030", "D1NodeService.insertOrUpdateDocument(): " +
|
790 |
|
|
"pid " + pid.getValue() +
|
791 |
|
|
" should have been in the identifier table, but it wasn't: " +
|
792 |
|
|
e.getMessage());
|
793 |
|
|
|
794 |
|
|
}
|
795 |
|
|
|
796 |
|
|
}
|
797 |
|
|
|
798 |
|
|
String[] action = new String[1];
|
799 |
|
|
action[0] = insertOrUpdate;
|
800 |
|
|
params.put("action", action);
|
801 |
|
|
String[] docid = new String[1];
|
802 |
|
|
docid[0] = localId;
|
803 |
|
|
params.put("docid", docid);
|
804 |
|
|
String[] doctext = new String[1];
|
805 |
|
|
doctext[0] = xml;
|
806 |
|
|
params.put("doctext", doctext);
|
807 |
|
|
|
808 |
|
|
String username = "public";
|
809 |
|
|
String[] groupnames = null;
|
810 |
|
|
if( session != null ) {
|
811 |
|
|
username = session.getSubject().getValue();
|
812 |
|
|
List<Group> groupList = session.getSubjectList().getGroupList();
|
813 |
|
|
for ( int i = 0; i > groupList.size(); i++ ) {
|
814 |
|
|
groupnames[i] = groupList.get(i).getGroupName();
|
815 |
|
|
|
816 |
|
|
}
|
817 |
|
|
}
|
818 |
|
|
|
819 |
|
|
// do the insert or update action
|
820 |
|
|
String result = handler.handleInsertOrUpdateAction(metacatUrl, null,
|
821 |
|
|
null, params, username, groupnames);
|
822 |
|
|
|
823 |
|
|
if(result.indexOf("<error>") != -1) {
|
824 |
|
|
String detailCode = "";
|
825 |
|
|
if ( insertOrUpdate.equals("insert") ) {
|
826 |
|
|
detailCode = "1190";
|
827 |
|
|
|
828 |
|
|
} else if ( insertOrUpdate.equals("update") ) {
|
829 |
|
|
detailCode = "1310";
|
830 |
|
|
|
831 |
|
|
}
|
832 |
|
|
throw new ServiceFailure(detailCode,
|
833 |
|
|
"Error inserting or updating document: " + result);
|
834 |
|
|
}
|
835 |
|
|
logMetacat.debug("Finsished inserting xml document with id " + localId);
|
836 |
|
|
|
837 |
|
|
return localId;
|
838 |
|
|
}
|
839 |
|
|
|
840 |
|
|
/**
|
841 |
|
|
* Insert a data document
|
842 |
|
|
*
|
843 |
|
|
* @param object
|
844 |
|
|
* @param pid
|
845 |
|
|
* @param sessionData
|
846 |
|
|
* @throws ServiceFailure
|
847 |
|
|
* @returns localId of the data object inserted
|
848 |
|
|
*/
|
849 |
6255
|
cjones
|
protected String insertDataObject(InputStream object, Identifier pid,
|
850 |
6241
|
cjones
|
Session session) throws ServiceFailure {
|
851 |
|
|
|
852 |
|
|
String username = "public";
|
853 |
|
|
String[] groupnames = null;
|
854 |
|
|
if( session != null ) {
|
855 |
|
|
username = session.getSubject().getValue();
|
856 |
|
|
List<Group> groupList = session.getSubjectList().getGroupList();
|
857 |
|
|
for ( int i = 0; i > groupList.size(); i++ ) {
|
858 |
|
|
groupnames[i] = groupList.get(i).getGroupName();
|
859 |
|
|
|
860 |
|
|
}
|
861 |
|
|
|
862 |
|
|
}
|
863 |
|
|
|
864 |
|
|
// generate pid/localId pair for object
|
865 |
|
|
logMetacat.debug("Generating a pid/localId mapping");
|
866 |
|
|
IdentifierManager im = IdentifierManager.getInstance();
|
867 |
|
|
String localId = im.generateLocalId(pid.getValue(), 1);
|
868 |
|
|
|
869 |
|
|
try {
|
870 |
|
|
logMetacat.debug("Case DATA: starting to write to disk.");
|
871 |
|
|
if (DocumentImpl.getDataFileLockGrant(localId)) {
|
872 |
|
|
|
873 |
|
|
// Save the data file to disk using "localId" as the name
|
874 |
|
|
try {
|
875 |
|
|
String datafilepath = PropertyService.getProperty("application.datafilepath");
|
876 |
|
|
|
877 |
|
|
File dataDirectory = new File(datafilepath);
|
878 |
|
|
dataDirectory.mkdirs();
|
879 |
|
|
|
880 |
|
|
File newFile = writeStreamToFile(dataDirectory, localId, object);
|
881 |
|
|
|
882 |
|
|
// TODO: Check that the file size matches SystemMetadata
|
883 |
|
|
// long size = newFile.length();
|
884 |
|
|
// if (size == 0) {
|
885 |
|
|
// throw new IOException("Uploaded file is 0 bytes!");
|
886 |
|
|
// }
|
887 |
|
|
|
888 |
|
|
// Register the file in the database (which generates an exception
|
889 |
|
|
// if the localId is not acceptable or other untoward things happen
|
890 |
|
|
try {
|
891 |
|
|
logMetacat.debug("Registering document...");
|
892 |
|
|
DocumentImpl.registerDocument(localId, "BIN", localId,
|
893 |
|
|
username, groupnames);
|
894 |
|
|
logMetacat.debug("Registration step completed.");
|
895 |
|
|
|
896 |
|
|
} catch (SQLException e) {
|
897 |
|
|
//newFile.delete();
|
898 |
|
|
logMetacat.debug("SQLE: " + e.getMessage());
|
899 |
|
|
e.printStackTrace(System.out);
|
900 |
|
|
throw new ServiceFailure("1190", "Registration failed: " +
|
901 |
|
|
e.getMessage());
|
902 |
|
|
|
903 |
|
|
} catch (AccessionNumberException e) {
|
904 |
|
|
//newFile.delete();
|
905 |
|
|
logMetacat.debug("ANE: " + e.getMessage());
|
906 |
|
|
e.printStackTrace(System.out);
|
907 |
|
|
throw new ServiceFailure("1190", "Registration failed: " +
|
908 |
|
|
e.getMessage());
|
909 |
|
|
|
910 |
|
|
} catch (Exception e) {
|
911 |
|
|
//newFile.delete();
|
912 |
|
|
logMetacat.debug("Exception: " + e.getMessage());
|
913 |
|
|
e.printStackTrace(System.out);
|
914 |
|
|
throw new ServiceFailure("1190", "Registration failed: " +
|
915 |
|
|
e.getMessage());
|
916 |
|
|
}
|
917 |
|
|
|
918 |
|
|
logMetacat.debug("Logging the creation event.");
|
919 |
|
|
EventLog.getInstance().log(metacatUrl, username, localId, "create");
|
920 |
|
|
|
921 |
|
|
// Schedule replication for this data file
|
922 |
|
|
logMetacat.debug("Scheduling replication.");
|
923 |
|
|
ForceReplicationHandler frh = new ForceReplicationHandler(
|
924 |
|
|
localId, "create", false, null);
|
925 |
|
|
|
926 |
|
|
} catch (PropertyNotFoundException e) {
|
927 |
|
|
throw new ServiceFailure("1190", "Could not lock file for writing:" +
|
928 |
|
|
e.getMessage());
|
929 |
|
|
|
930 |
|
|
}
|
931 |
|
|
}
|
932 |
|
|
return localId;
|
933 |
|
|
} catch (Exception e) {
|
934 |
|
|
// Could not get a lock on the document, so we can not update the file now
|
935 |
|
|
throw new ServiceFailure("1190", "Failed to lock file: " + e.getMessage());
|
936 |
|
|
}
|
937 |
|
|
|
938 |
|
|
}
|
939 |
6255
|
cjones
|
|
940 |
6256
|
cjones
|
/**
|
941 |
|
|
* Insert a systemMetadata document and return its localId
|
942 |
|
|
*/
|
943 |
|
|
protected void insertSystemMetadata(SystemMetadata sysmeta)
|
944 |
|
|
throws ServiceFailure {
|
945 |
|
|
|
946 |
|
|
logMetacat.debug("Starting to insert SystemMetadata...");
|
947 |
|
|
sysmeta.setDateSysMetadataModified(new Date());
|
948 |
|
|
logMetacat.debug("Inserting new system metadata with modified date " +
|
949 |
|
|
sysmeta.getDateSysMetadataModified());
|
950 |
|
|
|
951 |
|
|
//insert the system metadata
|
952 |
|
|
try {
|
953 |
|
|
IdentifierManager.getInstance().createSystemMetadata(sysmeta);
|
954 |
|
|
IdentifierManager.getInstance().updateSystemMetadata(sysmeta);
|
955 |
|
|
|
956 |
|
|
} catch (McdbDocNotFoundException e) {
|
957 |
|
|
throw new ServiceFailure("1030", "Error inserting system metadata: " +
|
958 |
|
|
e.getClass() + ": " + e.getMessage());
|
959 |
|
|
|
960 |
|
|
}
|
961 |
|
|
|
962 |
|
|
}
|
963 |
|
|
|
964 |
|
|
/**
|
965 |
6255
|
cjones
|
* Update a systemMetadata document
|
966 |
|
|
*
|
967 |
|
|
* @param sysMeta - the system metadata object in the system to update
|
968 |
|
|
*/
|
969 |
|
|
protected void updateSystemMetadata(SystemMetadata sysMeta)
|
970 |
|
|
throws ServiceFailure {
|
971 |
|
|
|
972 |
|
|
logMetacat.debug("D1NodeService.updateSystemMetadata() called.");
|
973 |
|
|
sysMeta.setDateSysMetadataModified(new Date());
|
974 |
|
|
try {
|
975 |
|
|
IdentifierManager.getInstance().updateSystemMetadata(sysMeta);
|
976 |
|
|
|
977 |
|
|
} catch (McdbDocNotFoundException e) {
|
978 |
|
|
|
979 |
|
|
throw new ServiceFailure("1030", "Error updating system metadata: " +
|
980 |
|
|
e.getClass() + ": " + e.getMessage());
|
981 |
|
|
|
982 |
|
|
}
|
983 |
|
|
|
984 |
|
|
}
|
985 |
|
|
|
986 |
|
|
/*
|
987 |
6241
|
cjones
|
* Write a stream to a file
|
988 |
|
|
*
|
989 |
|
|
* @param dir - the directory to write to
|
990 |
|
|
* @param fileName - the file name to write to
|
991 |
|
|
* @param data - the object bytes as an input stream
|
992 |
|
|
*
|
993 |
|
|
* @return newFile - the new file created
|
994 |
|
|
*
|
995 |
|
|
* @throws ServiceFailure
|
996 |
|
|
*/
|
997 |
|
|
private File writeStreamToFile(File dir, String fileName, InputStream data)
|
998 |
|
|
throws ServiceFailure {
|
999 |
|
|
|
1000 |
|
|
File newFile = new File(dir, fileName);
|
1001 |
|
|
logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
|
1002 |
|
|
|
1003 |
|
|
try {
|
1004 |
|
|
if (newFile.createNewFile()) {
|
1005 |
|
|
// write data stream to desired file
|
1006 |
|
|
OutputStream os = new FileOutputStream(newFile);
|
1007 |
|
|
long length = IOUtils.copyLarge(data, os);
|
1008 |
|
|
os.flush();
|
1009 |
|
|
os.close();
|
1010 |
|
|
} else {
|
1011 |
|
|
logMetacat.debug("File creation failed, or file already exists.");
|
1012 |
|
|
throw new ServiceFailure("1190", "File already exists: " + fileName);
|
1013 |
|
|
}
|
1014 |
|
|
} catch (FileNotFoundException e) {
|
1015 |
|
|
logMetacat.debug("FNF: " + e.getMessage());
|
1016 |
|
|
throw new ServiceFailure("1190", "File not found: " + fileName + " "
|
1017 |
|
|
+ e.getMessage());
|
1018 |
|
|
} catch (IOException e) {
|
1019 |
|
|
logMetacat.debug("IOE: " + e.getMessage());
|
1020 |
|
|
throw new ServiceFailure("1190", "File was not written: " + fileName
|
1021 |
|
|
+ " " + e.getMessage());
|
1022 |
|
|
}
|
1023 |
|
|
|
1024 |
|
|
return newFile;
|
1025 |
|
|
}
|
1026 |
|
|
|
1027 |
|
|
}
|