Project

General

Profile

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
        System.out.println("sysmeta object format: " + sysmeta.getObjectFormat());
188
        
189
        // authenticate & get user info
190
        SessionData sessionData = getSessionData(token);
191
        String username = sessionData.getUserName();
192
        String[] groups = sessionData.getGroupNames();
193

    
194
        // verify that guid == SystemMetadata.getIdentifier()
195
        logMetacat.debug("Comparing guid|sysmeta_guid: " + guid.getValue() + "|" + sysmeta.getIdentifier().getValue());
196
        if (!guid.getValue().equals(sysmeta.getIdentifier().getValue())) {
197
            throw new InvalidSystemMetadata(1180, 
198
                "GUID in method call does not match GUID in system metadata.");
199
        }
200

    
201
        logMetacat.debug("Checking if identifier exists...");
202
        // Check that the identifier does not already exist
203
        IdentifierManager im = IdentifierManager.getInstance();
204
        if (im.identifierExists(guid.getValue())) {
205
            throw new IdentifierNotUnique(1120, 
206
                "GUID is already in use by an existing object.");
207
        }
208

    
209
        // Check if we are handling metadata or data
210
        boolean isScienceMetadata = isScienceMetadata(sysmeta);
211
        
212
        if (isScienceMetadata) {
213
            // CASE METADATA:
214
            try {
215
                String xml = IOUtils.toString(object);
216
                this.insertDocument(xml, guid, sessionData);
217
            } catch (IOException e) {
218
                String msg = "Could not create string from XML stream: " +
219
                    " " + e.getMessage();
220
                logMetacat.debug(msg);
221
                throw new ServiceFailure(1190, msg);
222
            }
223

    
224
        } else {
225
            // DEFAULT CASE: DATA (needs to be checked and completed)
226
            insertDataObject(object, guid, sessionData);
227
            
228
        }
229

    
230
        // For Metadata and Data, insert the system metadata into the object store too
231
        insertSystemMetadata(sysmeta, sessionData);
232

    
233
        logMetacat.debug("Returning from CrudService.create()");
234
        return guid;
235
    }
236

    
237
    public Identifier delete(AuthToken token, Identifier guid)
238
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
239
            NotImplemented {
240
        throw new NotImplemented(1000, "This method not yet implemented.");
241
    }
242

    
243
    public DescribeResponse describe(AuthToken token, Identifier guid)
244
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
245
            NotImplemented {
246
        throw new NotImplemented(1000, "This method not yet implemented.");
247
    }
248
    
249
    public InputStream get(AuthToken token, Identifier guid)
250
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
251
            NotImplemented {
252
        
253
        // Retrieve the session information from the AuthToken
254
        // If the session is expired, then the user is 'public'
255
        final SessionData sessionData = getSessionData(token);
256
        
257
        // Look up the localId for this global identifier
258
        IdentifierManager im = IdentifierManager.getInstance();
259
        try {
260
            final String localId = im.getLocalId(guid.getValue());
261

    
262
            final InputStreamFromOutputStream<String> objectStream = 
263
                new InputStreamFromOutputStream<String>() {
264
                
265
                @Override
266
                public String produce(final OutputStream dataSink) throws Exception {
267

    
268
                    try {
269
                        handler.readFromMetacat(/*request.getRemoteAddr()*/metacatUrl, null, 
270
                                dataSink, localId, "xml",
271
                                sessionData.getUserName(), 
272
                                sessionData.getGroupNames(), true, params);
273
                    } catch (PropertyNotFoundException e) {
274
                        throw new ServiceFailure(1030, e.getMessage());
275
                    } catch (ClassNotFoundException e) {
276
                        throw new ServiceFailure(1030, e.getMessage());
277
                    } catch (IOException e) {
278
                        throw new ServiceFailure(1030, e.getMessage());
279
                    } catch (SQLException e) {
280
                        throw new ServiceFailure(1030, e.getMessage());
281
                    } catch (McdbException e) {
282
                        throw new ServiceFailure(1030, e.getMessage());
283
                    } catch (ParseLSIDException e) {
284
                        throw new NotFound(1020, e.getMessage());
285
                    } catch (InsufficientKarmaException e) {
286
                        throw new NotAuthorized(1000, "Not authorized for get().");
287
                    }
288

    
289
                    return "Completed";
290
                }
291
            };
292
            return objectStream;
293

    
294
        } catch (McdbDocNotFoundException e) {
295
            throw new NotFound(1020, e.getMessage());
296
        }
297
    }
298

    
299
    public Checksum getChecksum(AuthToken token, Identifier guid)
300
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
301
            InvalidRequest, NotImplemented {
302
        throw new NotImplemented(1000, "This method not yet implemented.");
303
    }
304

    
305
    public Checksum getChecksum(AuthToken token, Identifier guid, 
306
            String checksumAlgorithm) throws InvalidToken, ServiceFailure, 
307
            NotAuthorized, NotFound, InvalidRequest, NotImplemented {
308
        throw new NotImplemented(1000, "This method not yet implemented.");
309
    }
310

    
311
    public LogRecordSet getLogRecords(AuthToken token, Date fromDate, Date toDate)
312
            throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest, 
313
            NotImplemented {
314
        throw new NotImplemented(1000, "This method not yet implemented.");
315
    }
316

    
317
    public SystemMetadata getSystemMetadata(AuthToken token, Identifier guid)
318
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
319
            InvalidRequest, NotImplemented {
320
        
321
        logMetacat.debug("CrudService.getSystemMetadata - for guid: " + guid.getValue());
322
        
323
        // Retrieve the session information from the AuthToken
324
        // If the session is expired, then the user is 'public'
325
        final SessionData sessionData = getSessionData(token);
326

    
327
        // TODO: Check access control rules
328
                
329
        try {
330
            IdentifierManager im = IdentifierManager.getInstance();
331
            // This is a test id of an existing sysmeta document -- for temporary testing
332
            String testGuid = "autogen.20101192441566.1";
333
            // TODO: Look up ID of system metadata based on guid
334
            // TODO: Initially from document, later from entry in table?
335
//            String localId = im.getLocalId(guid.getValue());
336
            final String localId = im.getLocalId(testGuid);
337
            
338
            // Read system metadata from metacat's db
339
            final InputStreamFromOutputStream<String> objectStream = 
340
                new InputStreamFromOutputStream<String>() {
341
                
342
                @Override
343
                public String produce(final OutputStream dataSink) throws Exception {
344
                    try {
345
                        handler.readFromMetacat(/*request.getRemoteAddr()*/metacatUrl, null, 
346
                                dataSink, localId, "xml",
347
                                sessionData.getUserName(), 
348
                                sessionData.getGroupNames(), true, params);
349
                    } catch (PropertyNotFoundException e) {
350
                        throw new ServiceFailure(1030, e.getMessage());
351
                    } catch (ClassNotFoundException e) {
352
                        throw new ServiceFailure(1030, e.getMessage());
353
                    } catch (IOException e) {
354
                        throw new ServiceFailure(1030, e.getMessage());
355
                    } catch (SQLException e) {
356
                        throw new ServiceFailure(1030, e.getMessage());
357
                    } catch (McdbException e) {
358
                        throw new ServiceFailure(1030, e.getMessage());
359
                    } catch (ParseLSIDException e) {
360
                        throw new NotFound(1020, e.getMessage());
361
                    } catch (InsufficientKarmaException e) {
362
                        throw new NotAuthorized(1000, "Not authorized for get().");
363
                    }
364

    
365
                    return "Completed";
366
                }
367
            };
368
            
369
            // Deserialize the xml to create a SystemMetadata object
370
            SystemMetadata sysmeta = deserializeSystemMetadata(objectStream);
371
            return sysmeta;
372
            
373
        } catch (McdbDocNotFoundException e) {
374
            throw new NotFound(1000, e.getMessage());
375
        }                
376
    }
377

    
378
    public Identifier update(AuthToken token, Identifier guid, 
379
            InputStream object, Identifier obsoletedGuid, SystemMetadata sysmeta) 
380
            throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, 
381
            UnsupportedType, InsufficientResources, NotFound, InvalidSystemMetadata, 
382
            NotImplemented {
383
        throw new NotImplemented(1000, "This method not yet implemented.");
384
    }
385

    
386
    /*
387
     * Look up the information on the session using the token provided in
388
     * the AuthToken.  The Session should have all relevant user information.
389
     * If the session has expired or is invalid, the 'public' session will
390
     * be returned, giving the user anonymous access.
391
     */
392
    private static SessionData getSessionData(AuthToken token) {
393
        SessionData sessionData = null;
394
        String sessionId = "PUBLIC";
395
        if (token != null) {
396
            sessionId = token.getToken();
397
        }
398
        
399
        // if the session id is registered in SessionService, get the
400
        // SessionData for it. Otherwise, use the public session.
401
        if (SessionService.isSessionRegistered(sessionId)) {
402
            sessionData = SessionService.getRegisteredSession(sessionId);
403
        } else {
404
            sessionData = SessionService.getPublicSession();
405
        }
406
        
407
        return sessionData;
408
    }
409

    
410
    /** 
411
     * Determine if a given object should be treated as an XML science metadata
412
     * object. 
413
     * 
414
     * TODO: This test should be externalized in a configuration dictionary rather than being hardcoded.
415
     * 
416
     * @param sysmeta the SystemMetadata describig the object
417
     * @return true if the object should be treated as science metadata
418
     */
419
    private boolean isScienceMetadata(SystemMetadata sysmeta) {
420
        boolean scimeta = false;
421
        System.out.println("sysmeta: " + sysmeta.toString());
422
        System.out.println("sysmeta format: " + sysmeta.getObjectFormat());
423
        switch (sysmeta.getObjectFormat()) {
424
            case EML_2_1_0: scimeta = true; break;
425
            case EML_2_0_1: scimeta = true; break;
426
            case EML_2_0_0: scimeta = true; break;
427
            case FGDC_STD_001_1_1999: scimeta = true; break;
428
            case FGDC_STD_001_1998: scimeta = true; break;
429
            case NCML_2_2: scimeta = true; break;
430
        }
431
        
432
        return scimeta;
433
    }
434

    
435
    private void insertDataObject(InputStream object, Identifier guid, 
436
            SessionData sessionData) throws ServiceFailure {
437
        
438
        String username = sessionData.getUserName();
439
        String[] groups = sessionData.getGroupNames();
440

    
441
        // generate guid/localId pair for object
442
        logMetacat.debug("Generating a guid/localId mapping");
443
        IdentifierManager im = IdentifierManager.getInstance();
444
        String localId = im.generateLocalId(guid.getValue(), 1);
445

    
446
        try {
447
            logMetacat.debug("Case DATA: starting to write to disk.");
448
            if (DocumentImpl.getDataFileLockGrant(localId)) {
449
    
450
                // Save the data file to disk using "localId" as the name
451
                try {
452
                    String datafilepath = PropertyService.getProperty("application.datafilepath");
453
    
454
                    File dataDirectory = new File(datafilepath);
455
                    dataDirectory.mkdirs();
456
    
457
                    File newFile = writeStreamToFile(dataDirectory, localId, object);
458
    
459
                    // TODO: Check that the file size matches SystemMetadata
460
                    //                        long size = newFile.length();
461
                    //                        if (size == 0) {
462
                    //                            throw new IOException("Uploaded file is 0 bytes!");
463
                    //                        }
464
    
465
                    // Register the file in the database (which generates an exception
466
                    // if the localId is not acceptable or other untoward things happen
467
                    try {
468
                        logMetacat.debug("Registering document...");
469
                        DocumentImpl.registerDocument(localId, "BIN", localId,
470
                                username, groups);
471
                        logMetacat.debug("Registration step completed.");
472
                    } catch (SQLException e) {
473
                        //newFile.delete();
474
                        logMetacat.debug("SQLE: " + e.getMessage());
475
                        e.printStackTrace(System.out);
476
                        throw new ServiceFailure(1190, "Registration failed: " + e.getMessage());
477
                    } catch (AccessionNumberException e) {
478
                        //newFile.delete();
479
                        logMetacat.debug("ANE: " + e.getMessage());
480
                        e.printStackTrace(System.out);
481
                        throw new ServiceFailure(1190, "Registration failed: " + e.getMessage());
482
                    } catch (Exception e) {
483
                        //newFile.delete();
484
                        logMetacat.debug("Exception: " + e.getMessage());
485
                        e.printStackTrace(System.out);
486
                        throw new ServiceFailure(1190, "Registration failed: " + e.getMessage());
487
                    }
488
    
489
                    logMetacat.debug("Logging the creation event.");
490
                    EventLog.getInstance().log(/*request.getRemoteAddr()*/metacatUrl,
491
                            username, localId, "create");
492
    
493
                    // Schedule replication for this data file
494
                    logMetacat.debug("Scheduling replication.");
495
                    ForceReplicationHandler frh = new ForceReplicationHandler(
496
                            localId, "insert", false, null);
497
    
498
                } catch (PropertyNotFoundException e) {
499
                    throw new ServiceFailure(1190, "Could not lock file for writing:" + e.getMessage());
500
                }
501
    
502
            }
503
        } catch (Exception e) {
504
            // Could not get a lock on the document, so we can not update the file now
505
            throw new ServiceFailure(1190, "Failed to lock file: " + e.getMessage());
506
        }
507
    }
508

    
509
    private File writeStreamToFile(File dir, String fileName, InputStream data) 
510
        throws ServiceFailure {
511
        
512
        File newFile = new File(dir, fileName);
513
        logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
514

    
515
        try {
516
            if (newFile.createNewFile()) {
517
                // write data stream to desired file
518
                OutputStream os = new FileOutputStream(newFile);
519
                long length = IOUtils.copyLarge(data, os);
520
                os.flush();
521
                os.close();
522
            } else {
523
                logMetacat.debug("File creation failed, or file already exists.");
524
                throw new ServiceFailure(1190, "File already exists: " + fileName);
525
            }
526
        } catch (FileNotFoundException e) {
527
            logMetacat.debug("FNF: " + e.getMessage());
528
            throw new ServiceFailure(1190, "File not found: " + fileName + " " 
529
                    + e.getMessage());
530
        } catch (IOException e) {
531
            logMetacat.debug("IOE: " + e.getMessage());
532
            throw new ServiceFailure(1190, "File was not written: " + fileName 
533
                    + " " + e.getMessage());
534
        }
535

    
536
        return newFile;
537
    }
538

    
539
    private void insertSystemMetadata(SystemMetadata sysmeta, SessionData sessionData) 
540
        throws ServiceFailure {
541
        logMetacat.debug("Starting to insert SystemMetadata...");
542
    
543
        // generate guid/localId pair for sysmeta
544
        Identifier sysMetaGuid = new Identifier();
545
        sysMetaGuid.setValue(DocumentUtil.generateDocumentId(1));
546
        sysmeta.setDateSysMetadataModified(new Date());
547

    
548
        String xml = new String(serializeSystemMetadata(sysmeta).toByteArray());
549
        String localId = insertDocument(xml, sysMetaGuid, sessionData);
550
        //insert the system metadata doc id into the identifiers table to 
551
        //link it to the data or metadata document
552
        IdentifierManager.getInstance().createSystemMetadataMapping(sysMetaGuid.getValue(), localId);
553
    }
554
    
555
    /**
556
     * insert a document, return the id of the document that was inserted
557
     */
558
    private String insertDocument(String xml, Identifier guid, SessionData sessionData) 
559
        throws ServiceFailure {
560
        logMetacat.debug("Starting to insert xml document...");
561
        IdentifierManager im = IdentifierManager.getInstance();
562

    
563
        // generate guid/localId pair for sysmeta
564
        String localId = im.generateLocalId(guid.getValue(), 1);
565
        logMetacat.debug("Metadata guid|localId: " + guid.getValue() + "|" +
566
                localId);
567

    
568
        String[] action = new String[1];
569
        action[0] = "insert";
570
        params.put("action", action);
571
        String[] docid = new String[1];
572
        docid[0] = localId;
573
        params.put("docid", docid);
574
        String[] doctext = new String[1];
575
        doctext[0] = xml;
576
        logMetacat.debug(doctext[0]);
577
        params.put("doctext", doctext);
578
        
579
        // TODO: refactor handleInsertOrUpdateAction() to not output XML directly
580
        // onto output stream, or alternatively, capture that and parse it to 
581
        // generate the right exceptions
582
        //ByteArrayOutputStream output = new ByteArrayOutputStream();
583
        //PrintWriter pw = new PrintWriter(output);
584
        String result = handler.handleInsertOrUpdateAction(/*request.getRemoteAddr()*/metacatUrl, null, 
585
                            null, params, sessionData.getUserName(), sessionData.getGroupNames());
586
        //String outputS = new String(output.toByteArray());
587
        logMetacat.debug("CrudService.insertDocument - Metacat returned: " + result);
588
//        if (!(outputS.indexOf("<success>") > 0 && outputS.indexOf(localId) > 0)) {
589
//            throw new ServiceFailure(1190, outputS);
590
//        }
591
        logMetacat.debug("Finsished inserting xml document with id " + localId);
592
        return localId;
593
    }
594
    
595
    public static ByteArrayOutputStream serializeSystemMetadata(SystemMetadata sysmeta) 
596
        throws ServiceFailure {
597
        IBindingFactory bfact;
598
        ByteArrayOutputStream sysmetaOut = null;
599
        try {
600
            bfact = BindingDirectory.getFactory(SystemMetadata.class);
601
            IMarshallingContext mctx = bfact.createMarshallingContext();
602
            sysmetaOut = new ByteArrayOutputStream();
603
            mctx.marshalDocument(sysmeta, "UTF-8", null, sysmetaOut);
604
        } catch (JiBXException e) {
605
            throw new ServiceFailure(1190, "Failed to serialize and insert SystemMetadata: " + e.getMessage());
606
        }
607
        
608
        return sysmetaOut;
609
    }
610
    
611
    public static SystemMetadata deserializeSystemMetadata(InputStream xml) 
612
        throws ServiceFailure {
613
        try {
614
            IBindingFactory bfact = BindingDirectory.getFactory(SystemMetadata.class);
615
            IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
616
            SystemMetadata sysmeta = (SystemMetadata) uctx.unmarshalDocument(xml, null);
617
            return sysmeta;
618
        } catch (JiBXException e) {
619
            throw new ServiceFailure(1190, "Failed to serialize and insert SystemMetadata: " + e.getMessage());
620
        }    
621
    }
622
}
    (1-1/1)