1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2000 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
*
|
6
|
* '$Author: $'
|
7
|
* '$Date: 2009-06-13 15:28:13 +0300 $'
|
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
|
package edu.ucsb.nceas.metacat.dataone;
|
24
|
|
25
|
import java.io.ByteArrayOutputStream;
|
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.io.PrintWriter;
|
33
|
import java.sql.SQLException;
|
34
|
import java.util.Date;
|
35
|
import java.util.Enumeration;
|
36
|
import java.util.Hashtable;
|
37
|
import java.util.Timer;
|
38
|
|
39
|
import javax.servlet.ServletContext;
|
40
|
import javax.servlet.http.HttpServletRequest;
|
41
|
import javax.servlet.http.HttpServletResponse;
|
42
|
|
43
|
import org.apache.commons.io.IOUtils;
|
44
|
import org.apache.log4j.Logger;
|
45
|
import org.dataone.service.exceptions.IdentifierNotUnique;
|
46
|
import org.dataone.service.exceptions.InsufficientResources;
|
47
|
import org.dataone.service.exceptions.InvalidRequest;
|
48
|
import org.dataone.service.exceptions.InvalidSystemMetadata;
|
49
|
import org.dataone.service.exceptions.InvalidToken;
|
50
|
import org.dataone.service.exceptions.NotAuthorized;
|
51
|
import org.dataone.service.exceptions.NotFound;
|
52
|
import org.dataone.service.exceptions.NotImplemented;
|
53
|
import org.dataone.service.exceptions.ServiceFailure;
|
54
|
import org.dataone.service.exceptions.UnsupportedType;
|
55
|
import org.dataone.service.mn.MemberNodeCrud;
|
56
|
import org.dataone.service.types.AuthToken;
|
57
|
import org.dataone.service.types.Checksum;
|
58
|
import org.dataone.service.types.DescribeResponse;
|
59
|
import org.dataone.service.types.Identifier;
|
60
|
import org.dataone.service.types.LogRecordSet;
|
61
|
import org.dataone.service.types.SystemMetadata;
|
62
|
import org.jibx.runtime.BindingDirectory;
|
63
|
import org.jibx.runtime.IBindingFactory;
|
64
|
import org.jibx.runtime.IMarshallingContext;
|
65
|
import org.jibx.runtime.IUnmarshallingContext;
|
66
|
import org.jibx.runtime.JiBXException;
|
67
|
|
68
|
import com.gc.iotools.stream.is.InputStreamFromOutputStream;
|
69
|
|
70
|
import edu.ucsb.nceas.metacat.AccessionNumberException;
|
71
|
import edu.ucsb.nceas.metacat.DocumentImpl;
|
72
|
import edu.ucsb.nceas.metacat.EventLog;
|
73
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
74
|
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
|
75
|
import edu.ucsb.nceas.metacat.McdbException;
|
76
|
import edu.ucsb.nceas.metacat.MetacatHandler;
|
77
|
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
|
78
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
79
|
import edu.ucsb.nceas.metacat.replication.ForceReplicationHandler;
|
80
|
import edu.ucsb.nceas.metacat.service.SessionService;
|
81
|
import edu.ucsb.nceas.metacat.util.DocumentUtil;
|
82
|
import edu.ucsb.nceas.metacat.util.SessionData;
|
83
|
import edu.ucsb.nceas.utilities.ParseLSIDException;
|
84
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
85
|
|
86
|
/**
|
87
|
*
|
88
|
* Implements DataONE MemberNode CRUD API for Metacat.
|
89
|
*
|
90
|
* @author Matthew Jones
|
91
|
*/
|
92
|
public class CrudService implements MemberNodeCrud {
|
93
|
|
94
|
/*private ServletContext servletContext;
|
95
|
private HttpServletRequest request;
|
96
|
private HttpServletResponse response;*/
|
97
|
|
98
|
private static CrudService crudService = null;
|
99
|
|
100
|
private MetacatHandler handler;
|
101
|
private Hashtable<String, String[]> params;
|
102
|
Logger logMetacat = null;
|
103
|
|
104
|
private String metacatUrl;
|
105
|
|
106
|
/**
|
107
|
* singleton accessor
|
108
|
*/
|
109
|
public static CrudService getInstance()
|
110
|
{
|
111
|
if(crudService == null)
|
112
|
{
|
113
|
crudService = new CrudService();
|
114
|
}
|
115
|
|
116
|
return crudService;
|
117
|
}
|
118
|
|
119
|
/**
|
120
|
* Initializes new instance by setting servlet context,request and response.
|
121
|
* TODO: remove dependency on Servlet infrastructure
|
122
|
* TODO: Make this a real service, and make it a Singleton
|
123
|
*/
|
124
|
public CrudService() {
|
125
|
//change crud service into a singleton. dont pass servlet data structures here
|
126
|
logMetacat = Logger.getLogger(CrudService.class);
|
127
|
try
|
128
|
{
|
129
|
String server = PropertyService.getProperty("server.name");
|
130
|
String port = PropertyService.getProperty("server.httpPort");
|
131
|
String context = PropertyService.getProperty("application.context");
|
132
|
metacatUrl = "http://" + server + ":" + port + "/" + context;
|
133
|
logMetacat.debug("Initializing CrudService with url " + metacatUrl);
|
134
|
}
|
135
|
catch(Exception e)
|
136
|
{
|
137
|
logMetacat.error("Could not find servlet url in CrudService: " + e.getMessage());
|
138
|
e.printStackTrace();
|
139
|
throw new RuntimeException("Error getting servlet url in CrudService: " + e.getMessage());
|
140
|
}
|
141
|
|
142
|
/*this.servletContext = servletContext;
|
143
|
this.request = request;
|
144
|
this.response = response;*/
|
145
|
|
146
|
params = new Hashtable<String, String[]>();
|
147
|
|
148
|
handler = new MetacatHandler(new Timer());
|
149
|
|
150
|
}
|
151
|
|
152
|
/**
|
153
|
* return the context url CrudService is using.
|
154
|
*/
|
155
|
public String getContextUrl()
|
156
|
{
|
157
|
return metacatUrl;
|
158
|
}
|
159
|
|
160
|
/**
|
161
|
* set the params for this service from an HttpServletRequest param list
|
162
|
*/
|
163
|
public void setParamsFromRequest(HttpServletRequest request)
|
164
|
{
|
165
|
Enumeration paramlist = request.getParameterNames();
|
166
|
while (paramlist.hasMoreElements()) {
|
167
|
String name = (String) paramlist.nextElement();
|
168
|
String[] value = (String[])request.getParameterValues(name);
|
169
|
params.put(name, value);
|
170
|
}
|
171
|
}
|
172
|
|
173
|
/**
|
174
|
* set the parameter values needed for this request
|
175
|
*/
|
176
|
public void setParameter(String name, String[] value)
|
177
|
{
|
178
|
params.put(name, value);
|
179
|
}
|
180
|
|
181
|
public Identifier create(AuthToken token, Identifier guid,
|
182
|
InputStream object, SystemMetadata sysmeta) throws InvalidToken,
|
183
|
ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType,
|
184
|
InsufficientResources, InvalidSystemMetadata, NotImplemented {
|
185
|
|
186
|
logMetacat.debug("Starting CrudService.create()...");
|
187
|
|
188
|
// authenticate & get user info
|
189
|
SessionData sessionData = getSessionData(token);
|
190
|
String username = sessionData.getUserName();
|
191
|
String[] groups = sessionData.getGroupNames();
|
192
|
|
193
|
// verify that guid == SystemMetadata.getIdentifier()
|
194
|
logMetacat.debug("Comparing guid|sysmeta_guid: " + guid.getValue() + "|" + sysmeta.getIdentifier().getValue());
|
195
|
if (!guid.getValue().equals(sysmeta.getIdentifier().getValue())) {
|
196
|
throw new InvalidSystemMetadata(1180,
|
197
|
"GUID in method call does not match GUID in system metadata.");
|
198
|
}
|
199
|
|
200
|
logMetacat.debug("Checking if identifier exists...");
|
201
|
// Check that the identifier does not already exist
|
202
|
IdentifierManager im = IdentifierManager.getInstance();
|
203
|
if (im.identifierExists(guid.getValue())) {
|
204
|
throw new IdentifierNotUnique(1120,
|
205
|
"GUID is already in use by an existing object.");
|
206
|
}
|
207
|
|
208
|
// Check if we are handling metadata or data
|
209
|
boolean isScienceMetadata = isScienceMetadata(sysmeta);
|
210
|
|
211
|
if (isScienceMetadata) {
|
212
|
// CASE METADATA:
|
213
|
try {
|
214
|
String xml = IOUtils.toString(object);
|
215
|
this.insertDocument(xml, guid, sessionData);
|
216
|
} catch (IOException e) {
|
217
|
String msg = "Could not create string from XML stream: " +
|
218
|
" " + e.getMessage();
|
219
|
logMetacat.debug(msg);
|
220
|
throw new ServiceFailure(1190, msg);
|
221
|
}
|
222
|
|
223
|
} else {
|
224
|
// DEFAULT CASE: DATA (needs to be checked and completed)
|
225
|
insertDataObject(object, guid, sessionData);
|
226
|
|
227
|
}
|
228
|
|
229
|
// For Metadata and Data, insert the system metadata into the object store too
|
230
|
insertSystemMetadata(sysmeta, sessionData);
|
231
|
|
232
|
logMetacat.debug("Returning from CrudService.create()");
|
233
|
return guid;
|
234
|
}
|
235
|
|
236
|
public Identifier delete(AuthToken token, Identifier guid)
|
237
|
throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
238
|
NotImplemented {
|
239
|
throw new NotImplemented(1000, "This method not yet implemented.");
|
240
|
}
|
241
|
|
242
|
public DescribeResponse describe(AuthToken token, Identifier guid)
|
243
|
throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
244
|
NotImplemented {
|
245
|
throw new NotImplemented(1000, "This method not yet implemented.");
|
246
|
}
|
247
|
|
248
|
public InputStream get(AuthToken token, Identifier guid)
|
249
|
throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
250
|
NotImplemented {
|
251
|
|
252
|
// Retrieve the session information from the AuthToken
|
253
|
// If the session is expired, then the user is 'public'
|
254
|
final SessionData sessionData = getSessionData(token);
|
255
|
|
256
|
// Look up the localId for this global identifier
|
257
|
IdentifierManager im = IdentifierManager.getInstance();
|
258
|
try {
|
259
|
final String localId = im.getLocalId(guid.getValue());
|
260
|
|
261
|
final InputStreamFromOutputStream<String> objectStream =
|
262
|
new InputStreamFromOutputStream<String>() {
|
263
|
|
264
|
@Override
|
265
|
public String produce(final OutputStream dataSink) throws Exception {
|
266
|
|
267
|
try {
|
268
|
handler.readFromMetacat(metacatUrl, null,
|
269
|
dataSink, localId, "xml",
|
270
|
sessionData.getUserName(),
|
271
|
sessionData.getGroupNames(), true, params);
|
272
|
} catch (PropertyNotFoundException e) {
|
273
|
throw new ServiceFailure(1030, e.getMessage());
|
274
|
} catch (ClassNotFoundException e) {
|
275
|
throw new ServiceFailure(1030, e.getMessage());
|
276
|
} catch (IOException e) {
|
277
|
throw new ServiceFailure(1030, e.getMessage());
|
278
|
} catch (SQLException e) {
|
279
|
throw new ServiceFailure(1030, e.getMessage());
|
280
|
} catch (McdbException e) {
|
281
|
throw new ServiceFailure(1030, e.getMessage());
|
282
|
} catch (ParseLSIDException e) {
|
283
|
throw new NotFound(1020, e.getMessage());
|
284
|
} catch (InsufficientKarmaException e) {
|
285
|
throw new NotAuthorized(1000, "Not authorized for get().");
|
286
|
}
|
287
|
|
288
|
return "Completed";
|
289
|
}
|
290
|
};
|
291
|
return objectStream;
|
292
|
|
293
|
} catch (McdbDocNotFoundException e) {
|
294
|
throw new NotFound(1020, e.getMessage());
|
295
|
}
|
296
|
}
|
297
|
|
298
|
public Checksum getChecksum(AuthToken token, Identifier guid)
|
299
|
throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
300
|
InvalidRequest, NotImplemented {
|
301
|
throw new NotImplemented(1000, "This method not yet implemented.");
|
302
|
}
|
303
|
|
304
|
public Checksum getChecksum(AuthToken token, Identifier guid,
|
305
|
String checksumAlgorithm) throws InvalidToken, ServiceFailure,
|
306
|
NotAuthorized, NotFound, InvalidRequest, NotImplemented {
|
307
|
throw new NotImplemented(1000, "This method not yet implemented.");
|
308
|
}
|
309
|
|
310
|
public LogRecordSet getLogRecords(AuthToken token, Date fromDate, Date toDate)
|
311
|
throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest,
|
312
|
NotImplemented {
|
313
|
throw new NotImplemented(1000, "This method not yet implemented.");
|
314
|
}
|
315
|
|
316
|
public SystemMetadata getSystemMetadata(AuthToken token, Identifier guid)
|
317
|
throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
|
318
|
InvalidRequest, NotImplemented {
|
319
|
|
320
|
logMetacat.debug("CrudService.getSystemMetadata - for guid: " + guid.getValue());
|
321
|
|
322
|
// Retrieve the session information from the AuthToken
|
323
|
// If the session is expired, then the user is 'public'
|
324
|
final SessionData sessionData = getSessionData(token);
|
325
|
|
326
|
// TODO: Check access control rules
|
327
|
|
328
|
try {
|
329
|
IdentifierManager im = IdentifierManager.getInstance();
|
330
|
System.out.println("looking up systemmetadata id for guid " + guid.getValue());
|
331
|
final String localId = im.getSystemMetadataId(guid.getValue());
|
332
|
System.out.println("localId: " + localId);
|
333
|
|
334
|
// Read system metadata from metacat's db
|
335
|
final InputStreamFromOutputStream<String> objectStream =
|
336
|
new InputStreamFromOutputStream<String>() {
|
337
|
|
338
|
@Override
|
339
|
public String produce(final OutputStream dataSink) throws Exception {
|
340
|
try {
|
341
|
handler.readFromMetacat(metacatUrl, null,
|
342
|
dataSink, localId, "xml",
|
343
|
sessionData.getUserName(),
|
344
|
sessionData.getGroupNames(), true, params);
|
345
|
} catch (PropertyNotFoundException e) {
|
346
|
throw new ServiceFailure(1030, e.getMessage());
|
347
|
} catch (ClassNotFoundException e) {
|
348
|
throw new ServiceFailure(1030, e.getMessage());
|
349
|
} catch (IOException e) {
|
350
|
throw new ServiceFailure(1030, e.getMessage());
|
351
|
} catch (SQLException e) {
|
352
|
throw new ServiceFailure(1030, e.getMessage());
|
353
|
} catch (McdbException e) {
|
354
|
throw new ServiceFailure(1030, e.getMessage());
|
355
|
} catch (ParseLSIDException e) {
|
356
|
throw new NotFound(1020, e.getMessage());
|
357
|
} catch (InsufficientKarmaException e) {
|
358
|
throw new NotAuthorized(1000, "Not authorized for get().");
|
359
|
}
|
360
|
|
361
|
return "Completed";
|
362
|
}
|
363
|
};
|
364
|
|
365
|
// Deserialize the xml to create a SystemMetadata object
|
366
|
SystemMetadata sysmeta = deserializeSystemMetadata(objectStream);
|
367
|
return sysmeta;
|
368
|
|
369
|
} catch (McdbDocNotFoundException e) {
|
370
|
//e.printStackTrace();
|
371
|
throw new NotFound(1000, e.getMessage());
|
372
|
}
|
373
|
}
|
374
|
|
375
|
public Identifier update(AuthToken token, Identifier guid,
|
376
|
InputStream object, Identifier obsoletedGuid, SystemMetadata sysmeta)
|
377
|
throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique,
|
378
|
UnsupportedType, InsufficientResources, NotFound, InvalidSystemMetadata,
|
379
|
NotImplemented {
|
380
|
throw new NotImplemented(1000, "This method not yet implemented.");
|
381
|
}
|
382
|
|
383
|
/*
|
384
|
* Look up the information on the session using the token provided in
|
385
|
* the AuthToken. The Session should have all relevant user information.
|
386
|
* If the session has expired or is invalid, the 'public' session will
|
387
|
* be returned, giving the user anonymous access.
|
388
|
*/
|
389
|
private static SessionData getSessionData(AuthToken token) {
|
390
|
SessionData sessionData = null;
|
391
|
String sessionId = "PUBLIC";
|
392
|
if (token != null) {
|
393
|
sessionId = token.getToken();
|
394
|
}
|
395
|
|
396
|
// if the session id is registered in SessionService, get the
|
397
|
// SessionData for it. Otherwise, use the public session.
|
398
|
if (SessionService.isSessionRegistered(sessionId)) {
|
399
|
sessionData = SessionService.getRegisteredSession(sessionId);
|
400
|
} else {
|
401
|
sessionData = SessionService.getPublicSession();
|
402
|
}
|
403
|
|
404
|
return sessionData;
|
405
|
}
|
406
|
|
407
|
/**
|
408
|
* Determine if a given object should be treated as an XML science metadata
|
409
|
* object.
|
410
|
*
|
411
|
* TODO: This test should be externalized in a configuration dictionary rather than being hardcoded.
|
412
|
*
|
413
|
* @param sysmeta the SystemMetadata describig the object
|
414
|
* @return true if the object should be treated as science metadata
|
415
|
*/
|
416
|
private boolean isScienceMetadata(SystemMetadata sysmeta) {
|
417
|
boolean scimeta = false;
|
418
|
switch (sysmeta.getObjectFormat()) {
|
419
|
case EML_2_1_0: scimeta = true; break;
|
420
|
case EML_2_0_1: scimeta = true; break;
|
421
|
case EML_2_0_0: scimeta = true; break;
|
422
|
case FGDC_STD_001_1_1999: scimeta = true; break;
|
423
|
case FGDC_STD_001_1998: scimeta = true; break;
|
424
|
case NCML_2_2: scimeta = true; break;
|
425
|
}
|
426
|
|
427
|
return scimeta;
|
428
|
}
|
429
|
|
430
|
private void insertDataObject(InputStream object, Identifier guid,
|
431
|
SessionData sessionData) throws ServiceFailure {
|
432
|
|
433
|
String username = sessionData.getUserName();
|
434
|
String[] groups = sessionData.getGroupNames();
|
435
|
|
436
|
// generate guid/localId pair for object
|
437
|
logMetacat.debug("Generating a guid/localId mapping");
|
438
|
IdentifierManager im = IdentifierManager.getInstance();
|
439
|
String localId = im.generateLocalId(guid.getValue(), 1);
|
440
|
|
441
|
try {
|
442
|
logMetacat.debug("Case DATA: starting to write to disk.");
|
443
|
if (DocumentImpl.getDataFileLockGrant(localId)) {
|
444
|
|
445
|
// Save the data file to disk using "localId" as the name
|
446
|
try {
|
447
|
String datafilepath = PropertyService.getProperty("application.datafilepath");
|
448
|
|
449
|
File dataDirectory = new File(datafilepath);
|
450
|
dataDirectory.mkdirs();
|
451
|
|
452
|
File newFile = writeStreamToFile(dataDirectory, localId, object);
|
453
|
|
454
|
// TODO: Check that the file size matches SystemMetadata
|
455
|
// long size = newFile.length();
|
456
|
// if (size == 0) {
|
457
|
// throw new IOException("Uploaded file is 0 bytes!");
|
458
|
// }
|
459
|
|
460
|
// Register the file in the database (which generates an exception
|
461
|
// if the localId is not acceptable or other untoward things happen
|
462
|
try {
|
463
|
logMetacat.debug("Registering document...");
|
464
|
DocumentImpl.registerDocument(localId, "BIN", localId,
|
465
|
username, groups);
|
466
|
logMetacat.debug("Registration step completed.");
|
467
|
} catch (SQLException e) {
|
468
|
//newFile.delete();
|
469
|
logMetacat.debug("SQLE: " + e.getMessage());
|
470
|
e.printStackTrace(System.out);
|
471
|
throw new ServiceFailure(1190, "Registration failed: " + e.getMessage());
|
472
|
} catch (AccessionNumberException e) {
|
473
|
//newFile.delete();
|
474
|
logMetacat.debug("ANE: " + e.getMessage());
|
475
|
e.printStackTrace(System.out);
|
476
|
throw new ServiceFailure(1190, "Registration failed: " + e.getMessage());
|
477
|
} catch (Exception e) {
|
478
|
//newFile.delete();
|
479
|
logMetacat.debug("Exception: " + e.getMessage());
|
480
|
e.printStackTrace(System.out);
|
481
|
throw new ServiceFailure(1190, "Registration failed: " + e.getMessage());
|
482
|
}
|
483
|
|
484
|
logMetacat.debug("Logging the creation event.");
|
485
|
EventLog.getInstance().log(metacatUrl,
|
486
|
username, localId, "create");
|
487
|
|
488
|
// Schedule replication for this data file
|
489
|
logMetacat.debug("Scheduling replication.");
|
490
|
ForceReplicationHandler frh = new ForceReplicationHandler(
|
491
|
localId, "insert", false, null);
|
492
|
|
493
|
} catch (PropertyNotFoundException e) {
|
494
|
throw new ServiceFailure(1190, "Could not lock file for writing:" + e.getMessage());
|
495
|
}
|
496
|
|
497
|
}
|
498
|
} catch (Exception e) {
|
499
|
// Could not get a lock on the document, so we can not update the file now
|
500
|
throw new ServiceFailure(1190, "Failed to lock file: " + e.getMessage());
|
501
|
}
|
502
|
}
|
503
|
|
504
|
private File writeStreamToFile(File dir, String fileName, InputStream data)
|
505
|
throws ServiceFailure {
|
506
|
|
507
|
File newFile = new File(dir, fileName);
|
508
|
logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
|
509
|
|
510
|
try {
|
511
|
if (newFile.createNewFile()) {
|
512
|
// write data stream to desired file
|
513
|
OutputStream os = new FileOutputStream(newFile);
|
514
|
long length = IOUtils.copyLarge(data, os);
|
515
|
os.flush();
|
516
|
os.close();
|
517
|
} else {
|
518
|
logMetacat.debug("File creation failed, or file already exists.");
|
519
|
throw new ServiceFailure(1190, "File already exists: " + fileName);
|
520
|
}
|
521
|
} catch (FileNotFoundException e) {
|
522
|
logMetacat.debug("FNF: " + e.getMessage());
|
523
|
throw new ServiceFailure(1190, "File not found: " + fileName + " "
|
524
|
+ e.getMessage());
|
525
|
} catch (IOException e) {
|
526
|
logMetacat.debug("IOE: " + e.getMessage());
|
527
|
throw new ServiceFailure(1190, "File was not written: " + fileName
|
528
|
+ " " + e.getMessage());
|
529
|
}
|
530
|
|
531
|
return newFile;
|
532
|
}
|
533
|
|
534
|
private void insertSystemMetadata(SystemMetadata sysmeta, SessionData sessionData)
|
535
|
throws ServiceFailure {
|
536
|
logMetacat.debug("Starting to insert SystemMetadata...");
|
537
|
|
538
|
// generate guid/localId pair for sysmeta
|
539
|
Identifier sysMetaGuid = new Identifier();
|
540
|
sysMetaGuid.setValue(DocumentUtil.generateDocumentId(1));
|
541
|
sysmeta.setDateSysMetadataModified(new Date());
|
542
|
|
543
|
String xml = new String(serializeSystemMetadata(sysmeta).toByteArray());
|
544
|
String localId = insertDocument(xml, sysMetaGuid, sessionData);
|
545
|
//insert the system metadata doc id into the identifiers table to
|
546
|
//link it to the data or metadata document
|
547
|
System.out.println("mapping systemmetadata id to docid in im.insertSystemMetadata");
|
548
|
IdentifierManager.getInstance().createSystemMetadataMapping(sysmeta.getIdentifier().getValue(), sysMetaGuid.getValue());
|
549
|
}
|
550
|
|
551
|
/**
|
552
|
* insert a document, return the id of the document that was inserted
|
553
|
*/
|
554
|
private String insertDocument(String xml, Identifier guid, SessionData sessionData)
|
555
|
throws ServiceFailure {
|
556
|
logMetacat.debug("Starting to insert xml document...");
|
557
|
IdentifierManager im = IdentifierManager.getInstance();
|
558
|
|
559
|
// generate guid/localId pair for sysmeta
|
560
|
String localId = im.generateLocalId(guid.getValue(), 1);
|
561
|
logMetacat.debug("Metadata guid|localId: " + guid.getValue() + "|" +
|
562
|
localId);
|
563
|
|
564
|
String[] action = new String[1];
|
565
|
action[0] = "insert";
|
566
|
params.put("action", action);
|
567
|
String[] docid = new String[1];
|
568
|
docid[0] = localId;
|
569
|
params.put("docid", docid);
|
570
|
String[] doctext = new String[1];
|
571
|
doctext[0] = xml;
|
572
|
logMetacat.debug(doctext[0]);
|
573
|
params.put("doctext", doctext);
|
574
|
|
575
|
// TODO: refactor handleInsertOrUpdateAction() to not output XML directly
|
576
|
// onto output stream, or alternatively, capture that and parse it to
|
577
|
// generate the right exceptions
|
578
|
//ByteArrayOutputStream output = new ByteArrayOutputStream();
|
579
|
//PrintWriter pw = new PrintWriter(output);
|
580
|
String result = handler.handleInsertOrUpdateAction(metacatUrl, null,
|
581
|
null, params, sessionData.getUserName(), sessionData.getGroupNames());
|
582
|
//String outputS = new String(output.toByteArray());
|
583
|
logMetacat.debug("CrudService.insertDocument - Metacat returned: " + result);
|
584
|
// if (!(outputS.indexOf("<success>") > 0 && outputS.indexOf(localId) > 0)) {
|
585
|
// throw new ServiceFailure(1190, outputS);
|
586
|
// }
|
587
|
logMetacat.debug("Finsished inserting xml document with id " + localId);
|
588
|
return localId;
|
589
|
}
|
590
|
|
591
|
public static ByteArrayOutputStream serializeSystemMetadata(SystemMetadata sysmeta)
|
592
|
throws ServiceFailure {
|
593
|
IBindingFactory bfact;
|
594
|
ByteArrayOutputStream sysmetaOut = null;
|
595
|
try {
|
596
|
bfact = BindingDirectory.getFactory(SystemMetadata.class);
|
597
|
IMarshallingContext mctx = bfact.createMarshallingContext();
|
598
|
sysmetaOut = new ByteArrayOutputStream();
|
599
|
mctx.marshalDocument(sysmeta, "UTF-8", null, sysmetaOut);
|
600
|
} catch (JiBXException e) {
|
601
|
throw new ServiceFailure(1190, "Failed to serialize and insert SystemMetadata: " + e.getMessage());
|
602
|
}
|
603
|
|
604
|
return sysmetaOut;
|
605
|
}
|
606
|
|
607
|
public static SystemMetadata deserializeSystemMetadata(InputStream xml)
|
608
|
throws ServiceFailure {
|
609
|
try {
|
610
|
IBindingFactory bfact = BindingDirectory.getFactory(SystemMetadata.class);
|
611
|
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
|
612
|
SystemMetadata sysmeta = (SystemMetadata) uctx.unmarshalDocument(xml, null);
|
613
|
return sysmeta;
|
614
|
} catch (JiBXException e) {
|
615
|
throw new ServiceFailure(1190, "Failed to serialize and insert SystemMetadata: " + e.getMessage());
|
616
|
}
|
617
|
}
|
618
|
}
|