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.io.StringBufferInputStream;
34
import java.security.MessageDigest;
35
import java.sql.SQLException;
36
import java.util.*;
37
import java.text.DateFormat;
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.*;
57
import org.jibx.runtime.BindingDirectory;
58
import org.jibx.runtime.IBindingFactory;
59
import org.jibx.runtime.IMarshallingContext;
60
import org.jibx.runtime.IUnmarshallingContext;
61
import org.jibx.runtime.JiBXException;
62

    
63
import com.gc.iotools.stream.is.InputStreamFromOutputStream;
64

    
65
import edu.ucsb.nceas.metacat.AccessionNumberException;
66
import edu.ucsb.nceas.metacat.MetacatResultSet;
67
import edu.ucsb.nceas.metacat.MetacatResultSet.Document;
68
import edu.ucsb.nceas.metacat.DocumentImpl;
69
import edu.ucsb.nceas.metacat.EventLog;
70
import edu.ucsb.nceas.metacat.IdentifierManager;
71
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
72
import edu.ucsb.nceas.metacat.McdbException;
73
import edu.ucsb.nceas.metacat.MetacatHandler;
74
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
75
import edu.ucsb.nceas.metacat.client.rest.MetacatRestClient;
76
import edu.ucsb.nceas.metacat.properties.PropertyService;
77
import edu.ucsb.nceas.metacat.replication.ForceReplicationHandler;
78
import edu.ucsb.nceas.metacat.service.SessionService;
79
import edu.ucsb.nceas.metacat.util.DocumentUtil;
80
import edu.ucsb.nceas.metacat.util.SessionData;
81
import edu.ucsb.nceas.utilities.ParseLSIDException;
82
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
83

    
84
/**
85
 * 
86
 * Implements DataONE MemberNode CRUD API for Metacat. 
87
 * 
88
 * @author Matthew Jones
89
 */
90
public class CrudService implements MemberNodeCrud {
91

    
92
    /*private ServletContext servletContext;
93
    private HttpServletRequest request;
94
    private HttpServletResponse response;*/
95
    
96
    private static CrudService crudService = null;
97

    
98
    private MetacatHandler handler;
99
    private Hashtable<String, String[]> params;
100
    Logger logMetacat = null;
101
    
102
    private String metacatUrl;
103

    
104
    /**
105
     * singleton accessor
106
     */
107
    public static CrudService getInstance()
108
    {
109
      if(crudService == null)
110
      {
111
        crudService = new CrudService();
112
      }
113
      
114
      return crudService;
115
    }
116
    
117
    /**
118
     * Initializes new instance by setting servlet context,request and response.
119
     * TODO: remove dependency on Servlet infrastructure
120
     * TODO: Make this a real service, and make it a Singleton
121
     */
122
    public CrudService() {
123
    //change crud service into a singleton.  dont pass servlet data structures here
124
        logMetacat = Logger.getLogger(CrudService.class);
125
        try
126
        {
127
            String server = PropertyService.getProperty("server.name");
128
            String port = PropertyService.getProperty("server.httpPort");
129
            String context = PropertyService.getProperty("application.context");
130
            metacatUrl = "http://" + server + ":" + port + "/" + context;
131
            logMetacat.debug("Initializing CrudService with url " + metacatUrl);
132
        }
133
        catch(Exception e)
134
        {
135
            logMetacat.error("Could not find servlet url in CrudService: " + e.getMessage());
136
            e.printStackTrace();
137
            throw new RuntimeException("Error getting servlet url in CrudService: " + e.getMessage());
138
        }
139
        
140
        /*this.servletContext = servletContext;
141
        this.request = request;
142
        this.response = response;*/
143
        
144
        params = new Hashtable<String, String[]>();
145

    
146
        handler = new MetacatHandler(new Timer());
147

    
148
    }
149
    
150
    /**
151
     * return the context url CrudService is using.
152
     */
153
    public String getContextUrl()
154
    {
155
        return metacatUrl;
156
    }
157
    
158
    /**
159
     * set the params for this service from an HttpServletRequest param list
160
     */
161
    public void setParamsFromRequest(HttpServletRequest request)
162
    {
163
        Enumeration paramlist = request.getParameterNames();
164
        while (paramlist.hasMoreElements()) {
165
            String name = (String) paramlist.nextElement();
166
            String[] value = (String[])request.getParameterValues(name);
167
            params.put(name, value);
168
        }
169
    }
170
    
171
    /**
172
     * Authenticate against metacat and get a token.
173
     * @param username
174
     * @param password
175
     * @return
176
     * @throws ServiceFailure
177
     */
178
    public AuthToken authenticate(String username, String password)
179
      throws ServiceFailure
180
    {
181
        try
182
        {
183
            MetacatRestClient restClient = new MetacatRestClient(getContextUrl());   
184
            String response = restClient.login(username, password);
185
            String sessionid = restClient.getSessionId();
186
            SessionService sessionService = SessionService.getInstance();
187
            sessionService.registerSession(new SessionData(sessionid, username, new String[0], password, "CrudServiceLogin"));
188
            AuthToken token = new AuthToken(sessionid);
189
            return token;
190
        }
191
        catch(Exception e)
192
        {
193
            throw new ServiceFailure("1000", "Error authenticating with metacat: " + e.getMessage());
194
        }
195
    }
196
    
197
    /**
198
     * set the parameter values needed for this request
199
     */
200
    public void setParameter(String name, String[] value)
201
    {
202
        params.put(name, value);
203
    }
204
    
205
    /**
206
     * Generate SystemMetadata for any object in the object store that does
207
     * not already have it.  SystemMetadata documents themselves, are, of course,
208
     * exempt.  This is a utility method for migration of existing object 
209
     * stores to DataONE where SystemMetadata is required for all objects.  See 
210
     * https://trac.dataone.org/ticket/591
211
     * 
212
     * @param token an authtoken with appropriate permissions to read all 
213
     * documents in the object store.  To work correctly, this should probably
214
     * be an adminstrative credential.
215
     */
216
    public void generateMissingSystemMetadata(AuthToken token)
217
    {
218
        IdentifierManager im = IdentifierManager.getInstance();
219
        //get the list of ids with no SM
220
        List<String> l = im.getLocalIdsWithNoSystemMetadata();
221
        for(int i=0; i<l.size(); i++)
222
        { //for each id, add a system metadata doc
223
            String localId = l.get(i);
224
            System.out.println("Creating SystemMetadata for localId " + localId);
225
            //get the document
226
            try
227
            {
228
                //generate required system metadata fields from the document
229
                SystemMetadata sm = createSystemMetadata(localId, token);
230
                System.out.println("sm: " + new String(serializeSystemMetadata(sm).toByteArray()));
231
                //insert the systemmetadata object
232
                SessionData sessionData = getSessionData(token);
233
                insertSystemMetadata(sm, sessionData);
234
            }
235
            catch(Exception e)
236
            {
237
                //e.printStackTrace();
238
                System.out.println("Exception generating missing system metadata: " + e.getMessage());
239
                logMetacat.error("Could not generate missing system metadata: " + e.getMessage());
240
            }
241
        }
242
    }
243
    
244
    /**
245
     * create an object via the crud interface
246
     */
247
    public Identifier create(AuthToken token, Identifier guid, 
248
            InputStream object, SystemMetadata sysmeta) throws InvalidToken, 
249
            ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType, 
250
            InsufficientResources, InvalidSystemMetadata, NotImplemented {
251

    
252
        logMetacat.debug("Starting CrudService.create()...");
253
        
254
        // authenticate & get user info
255
        SessionData sessionData = getSessionData(token);
256
        String username = sessionData.getUserName();
257
        String[] groups = sessionData.getGroupNames();
258

    
259
        if (username == null || username.equals("public"))
260
        {
261
            throw new NotAuthorized("1000", "User " + username + " is not authorized to create content." +
262
                    "  If you are not logged in, please do so and retry the request.");
263
        }
264
        
265
        // verify that guid == SystemMetadata.getIdentifier()
266
        logMetacat.debug("Comparing guid|sysmeta_guid: " + guid.getValue() + "|" + sysmeta.getIdentifier().getValue());
267
        if (!guid.getValue().equals(sysmeta.getIdentifier().getValue())) {
268
            throw new InvalidSystemMetadata("1180", 
269
                "GUID in method call does not match GUID in system metadata.");
270
        }
271

    
272
        logMetacat.debug("Checking if identifier exists...");
273
        // Check that the identifier does not already exist
274
        IdentifierManager im = IdentifierManager.getInstance();
275
        if (im.identifierExists(guid.getValue())) {
276
            throw new IdentifierNotUnique("1120", 
277
                "GUID is already in use by an existing object.");
278
        }
279

    
280
        // Check if we are handling metadata or data
281
        boolean isScienceMetadata = isScienceMetadata(sysmeta);
282
        
283
        if (isScienceMetadata) {
284
            // CASE METADATA:
285
            try {
286
                this.insertDocument(object, guid, sessionData);
287
            } catch (IOException e) {
288
                String msg = "Could not create string from XML stream: " +
289
                    " " + e.getMessage();
290
                logMetacat.debug(msg);
291
                throw new ServiceFailure("1190", msg);
292
            }
293

    
294
        } else {
295
            // DEFAULT CASE: DATA (needs to be checked and completed)
296
            insertDataObject(object, guid, sessionData);
297
            
298
        }
299

    
300
        // For Metadata and Data, insert the system metadata into the object store too
301
        insertSystemMetadata(sysmeta, sessionData);
302

    
303
        logMetacat.debug("Returning from CrudService.create()");
304
        return guid;
305
    }
306
    
307
    /**
308
     * update an existing object with a new object.  Change the system metadata
309
     * to reflect the changes and update it as well.
310
     */
311
    public Identifier update(AuthToken token, Identifier guid, 
312
            InputStream object, Identifier obsoletedGuid, SystemMetadata sysmeta) 
313
            throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, 
314
            UnsupportedType, InsufficientResources, NotFound, InvalidSystemMetadata, 
315
            NotImplemented {
316
        try
317
        {
318
            SessionData sessionData = getSessionData(token);
319
            
320
            //find the old systemmetadata (sm.old) document id (the one linked to obsoletedGuid)
321
            SystemMetadata sm = getSystemMetadata(token, obsoletedGuid);
322
            //change sm.old's obsoletedBy field 
323
            List l = sm.getObsoletedByList();
324
            l.add(guid);
325
            sm.setObsoletedByList(l);
326
            //update sm.old
327
            updateSystemMetadata(sm, sessionData);
328
            
329
            //change the obsoletes field of the new systemMetadata (sm.new) to point to the id of the old one
330
            sysmeta.addObsolete(obsoletedGuid);
331
            //insert sm.new
332
            insertSystemMetadata(sysmeta, sessionData);
333
            
334
            boolean isScienceMetadata = isScienceMetadata(sysmeta);
335
            if(isScienceMetadata)
336
            {
337
                //update the doc
338
                updateDocument(object, obsoletedGuid, guid, sessionData);
339
            }
340
            else
341
            {
342
                //update a data file, not xml
343
                insertDataObject(object, guid, sessionData);
344
            }
345
            return guid;
346
        }
347
        catch(Exception e)
348
        {
349
            throw new ServiceFailure("1030", "Error updating document in CrudService: " + e.getMessage());
350
        }
351
    }
352
    
353
    /**
354
     * set the permission on the document
355
     * @param token
356
     * @param principal
357
     * @param permission
358
     * @param permissionType
359
     * @param permissionOrder
360
     * @return
361
     */
362
    public void setAccess(AuthToken token, Identifier id, String principal, String permission,
363
            String permissionType, String permissionOrder)
364
      throws ServiceFailure
365
    {
366
        try
367
        {
368
            IdentifierManager im = IdentifierManager.getInstance();
369
            String docid = im.getLocalId(id.getValue());
370
            final SessionData sessionData = getSessionData(token);
371
            String permNum = "0";
372
            if(permission.equals("read"))
373
            {
374
                permNum = "4";
375
            }
376
            else if(permission.equals("write"))
377
            {
378
                permNum = "6";
379
            }
380
            handler.setAccess(metacatUrl, sessionData.getUserName(), docid, 
381
                    principal, permNum, permissionType, permissionOrder);
382
        }
383
        catch(Exception e)
384
        {
385
            e.printStackTrace();
386
            throw new ServiceFailure("1000", "Could not set access on the document with id " + id.getValue());
387
        }
388
    }
389
    
390
    /**
391
     *  Retrieve the list of objects present on the MN that match the calling 
392
     *  parameters. This method is required to support the process of Member 
393
     *  Node synchronization. At a minimum, this method should be able to 
394
     *  return a list of objects that match:
395
     *  startTime <= SystemMetadata.dateSysMetadataModified
396
     *  but is expected to also support date range (by also specifying endTime), 
397
     *  and should also support slicing of the matching set of records by 
398
     *  indicating the starting index of the response (where 0 is the index 
399
     *  of the first item) and the count of elements to be returned.
400
     *  
401
     *  If startTime or endTime is null, the query is not restricted by that parameter.
402
     *  
403
     * @see http://mule1.dataone.org/ArchitectureDocs/mn_api_replication.html#MN_replication.listObjects
404
     * @param token
405
     * @param startTime
406
     * @param endTime
407
     * @param objectFormat
408
     * @param replicaStatus
409
     * @param start
410
     * @param count
411
     * @return ObjectList
412
     * @throws NotAuthorized
413
     * @throws InvalidRequest
414
     * @throws NotImplemented
415
     * @throws ServiceFailure
416
     * @throws InvalidToken
417
     */
418
    public ObjectList listObjects(AuthToken token, Date startTime, Date endTime, 
419
        ObjectFormat objectFormat, boolean replicaStatus, int start, int count)
420
      throws NotAuthorized, InvalidRequest, NotImplemented, ServiceFailure, InvalidToken
421
    {
422
      ObjectList ol = new ObjectList();
423
      final SessionData sessionData = getSessionData(token);
424
      try
425
      {
426
          params.clear();
427
          params.put("returndoctype", new String[] {"http://dataone.org/service/types/SystemMetadata/0.1"});
428
          params.put("qformat", new String[] {"xml"});
429
          params.put("returnfield", new String[] {"size", "originMemberNode", 
430
                  "identifier", "objectFormat", "dateSysMetadataModified", "checksum", "@algorithm"});
431
          params.put("anyfield", new String[] {"%"});
432
          
433
          MetacatResultSet rs = handler.query(metacatUrl, params, sessionData.getUserName(), 
434
                  sessionData.getGroupNames(), sessionData.getId());
435
          List docs = rs.getDocuments();
436

    
437
          if(count == 1000)
438
          {
439
              count = docs.size();
440
          }
441
          for(int i=start; i<count; i++)
442
          {
443
              //get the document from the results
444
              Document d = (Document)docs.get(i);
445
              ObjectFormat returnedObjectFormat = ObjectFormat.convert(d.getField("objectFormat"));
446
              if(objectFormat != null && !objectFormat.toString().trim().equals(returnedObjectFormat.toString().trim()))
447
              { //make sure the objectFormat is the one specified
448
                  continue;
449
              }
450
              Date dateSysMetadataModified = parseDate(d.getField("dateSysMetadataModified"));
451
              int startDateComparison = 0;
452
              int endDateComparison = 0;
453
              if(startTime != null)
454
              {
455
                  startDateComparison = dateSysMetadataModified.compareTo(startTime);
456
              }
457
              
458
              if(endTime != null)
459
              {
460
                  endDateComparison = dateSysMetadataModified.compareTo(endTime);
461
              }
462
              
463
              if(startDateComparison < 0 || endDateComparison > 0)
464
              { //this date falls outside of the startTime and endTime params, so
465
                //skip it
466
                  continue;                  
467
              }
468
              
469
              ObjectInfo info = new ObjectInfo();
470
              //add the fields to the info object
471
              Checksum cs = new Checksum();
472
              cs.setValue(d.getField("checksum"));
473
              String csalg = d.getField("algorithm");
474
              if(csalg == null)
475
              {
476
                  csalg = "MD5";
477
              }
478
              ChecksumAlgorithm ca = ChecksumAlgorithm.convert(csalg);
479
              cs.setAlgorithm(ca);
480
              info.setChecksum(cs);
481
              info.setDateSysMetadataModified(dateSysMetadataModified);
482
              Identifier id = new Identifier();
483
              id.setValue(d.getField("identifier"));
484
              info.setIdentifier(id);
485
              info.setObjectFormat(returnedObjectFormat);
486
              info.setSize(new Long(d.getField("size").trim()).longValue());
487
              //add the ObjectInfo to the ObjectList
488
              ol.addObjectInfo(info);
489
              //System.out.println(d.toString());
490
          }
491
      }
492
      catch(Exception e)
493
      {
494
          e.printStackTrace();
495
          throw new ServiceFailure("1580", "Error retrieving ObjectList: " + e.getMessage());
496
      }
497
      return ol;
498
    }
499
    
500
    /**
501
     * Call listObjects with the default values for replicaStatus (true), start (0),
502
     * and count (1000).
503
     * @param token
504
     * @param startTime
505
     * @param endTime
506
     * @param objectFormat
507
     * @return
508
     * @throws NotAuthorized
509
     * @throws InvalidRequest
510
     * @throws NotImplemented
511
     * @throws ServiceFailure
512
     * @throws InvalidToken
513
     */
514
    public ObjectList listObjects(AuthToken token, Date startTime, Date endTime, 
515
        ObjectFormat objectFormat)
516
      throws NotAuthorized, InvalidRequest, NotImplemented, ServiceFailure, InvalidToken
517
    {
518
       return listObjects(token, startTime, endTime, objectFormat, true, 0, 1000);
519
    }
520

    
521
    /**
522
     * Delete a document.  NOT IMPLEMENTED
523
     */
524
    public Identifier delete(AuthToken token, Identifier guid)
525
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
526
            NotImplemented {
527
        throw new NotImplemented("1000", "This method not yet implemented.");
528
    }
529

    
530
    /**
531
     * describe a document.  NOT IMPLEMENTED
532
     */
533
    public DescribeResponse describe(AuthToken token, Identifier guid)
534
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
535
            NotImplemented {
536
        throw new NotImplemented("1000", "This method not yet implemented.");
537
    }
538
    
539
    /**
540
     * get a document with a specified guid.
541
     */
542
    public InputStream get(AuthToken token, Identifier guid)
543
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
544
            NotImplemented {
545
        
546
        // Retrieve the session information from the AuthToken
547
        // If the session is expired, then the user is 'public'
548
        final SessionData sessionData = getSessionData(token);
549
        
550
        // Look up the localId for this global identifier
551
        IdentifierManager im = IdentifierManager.getInstance();
552
        try {
553
            final String localId = im.getLocalId(guid.getValue());
554

    
555
            final InputStreamFromOutputStream<String> objectStream = 
556
                new InputStreamFromOutputStream<String>() {
557
                
558
                @Override
559
                public String produce(final OutputStream dataSink) throws Exception {
560

    
561
                    try {
562
                        handler.readFromMetacat(metacatUrl, null, 
563
                                dataSink, localId, "xml",
564
                                sessionData.getUserName(), 
565
                                sessionData.getGroupNames(), true, params);
566
                    } catch (PropertyNotFoundException e) {
567
                        throw new ServiceFailure("1030", e.getMessage());
568
                    } catch (ClassNotFoundException e) {
569
                        throw new ServiceFailure("1030", e.getMessage());
570
                    } catch (IOException e) {
571
                        throw new ServiceFailure("1030", e.getMessage());
572
                    } catch (SQLException e) {
573
                        throw new ServiceFailure("1030", e.getMessage());
574
                    } catch (McdbException e) {
575
                        throw new ServiceFailure("1030", e.getMessage());
576
                    } catch (ParseLSIDException e) {
577
                        throw new NotFound("1020", e.getMessage());
578
                    } catch (InsufficientKarmaException e) {
579
                        throw new NotAuthorized("1000", "Not authorized for get().");
580
                    }
581

    
582
                    return "Completed";
583
                }
584
            };
585
            return objectStream;
586

    
587
        } catch (McdbDocNotFoundException e) {
588
            throw new NotFound("1020", e.getMessage());
589
        }
590
    }
591

    
592
    /**
593
     * get the checksum for a document.  NOT IMPLEMENTED
594
     */
595
    public Checksum getChecksum(AuthToken token, Identifier guid)
596
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
597
            InvalidRequest, NotImplemented {
598
        throw new NotImplemented("1000", "This method not yet implemented.");
599
    }
600

    
601
    /**
602
     * get the checksum for a document.  NOT IMPLEMENTED
603
     */
604
    public Checksum getChecksum(AuthToken token, Identifier guid, 
605
            String checksumAlgorithm) throws InvalidToken, ServiceFailure, 
606
            NotAuthorized, NotFound, InvalidRequest, NotImplemented {
607
        throw new NotImplemented("1000", "This method not yet implemented.");
608
    }
609

    
610
    /**
611
     * get log records.  NOT IMPLEMENTED
612
     */
613
    public LogRecordSet getLogRecords(AuthToken token, Date fromDate, Date toDate)
614
            throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest, 
615
            NotImplemented {
616
        throw new NotImplemented("1000", "This method not yet implemented.");
617
    }
618

    
619
    /**
620
     * get the system metadata for a document with a specified guid.
621
     */
622
    public SystemMetadata getSystemMetadata(AuthToken token, Identifier guid)
623
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
624
            InvalidRequest, NotImplemented {
625
        
626
        logMetacat.debug("CrudService.getSystemMetadata - for guid: " + guid.getValue());
627
        
628
        // Retrieve the session information from the AuthToken
629
        // If the session is expired, then the user is 'public'
630
        final SessionData sessionData = getSessionData(token);
631

    
632
        // TODO: Check access control rules
633
                
634
        try {
635
            IdentifierManager im = IdentifierManager.getInstance();
636
            final String localId = im.getSystemMetadataId(guid.getValue());
637
            
638
            // Read system metadata from metacat's db
639
            final InputStreamFromOutputStream<String> objectStream = 
640
                new InputStreamFromOutputStream<String>() {
641
                
642
                @Override
643
                public String produce(final OutputStream dataSink) throws Exception {
644
                    try {
645
                        handler.readFromMetacat(metacatUrl, null, 
646
                                dataSink, localId, "xml",
647
                                sessionData.getUserName(), 
648
                                sessionData.getGroupNames(), true, params);
649
                    } catch (PropertyNotFoundException e) {
650
                        throw new ServiceFailure("1030", e.getMessage());
651
                    } catch (ClassNotFoundException e) {
652
                        throw new ServiceFailure("1030", e.getMessage());
653
                    } catch (IOException e) {
654
                        throw new ServiceFailure("1030", e.getMessage());
655
                    } catch (SQLException e) {
656
                        throw new ServiceFailure("1030", e.getMessage());
657
                    } catch (McdbException e) {
658
                        throw new ServiceFailure("1030", e.getMessage());
659
                    } catch (ParseLSIDException e) {
660
                        throw new NotFound("1020", e.getMessage());
661
                    } catch (InsufficientKarmaException e) {
662
                        throw new NotAuthorized("1000", "Not authorized for get().");
663
                    }
664

    
665
                    return "Completed";
666
                }
667
            };
668
            
669
            // Deserialize the xml to create a SystemMetadata object
670
            SystemMetadata sysmeta = deserializeSystemMetadata(objectStream);
671
            return sysmeta;
672
            
673
        } catch (McdbDocNotFoundException e) {
674
            //e.printStackTrace();
675
            throw new NotFound("1000", e.getMessage());
676
        }                
677
    }
678
    
679
    /**
680
     * parse the date in the systemMetadata
681
     * @param s
682
     * @return
683
     * @throws Exception
684
     */
685
    private Date parseDate(String s)
686
      throws Exception
687
    {
688
        Date d = null;
689
        int tIndex = s.indexOf("T");
690
        int zIndex = s.indexOf("Z");
691
        if(tIndex != -1 && zIndex != -1)
692
        { //parse a date that looks like 2010-05-18T21:12:54.362Z
693
            //System.out.println("original date: " + s);
694
            
695
            String date = s.substring(0, tIndex);
696
            String year = date.substring(0, date.indexOf("-"));
697
            String month = date.substring(date.indexOf("-") + 1, date.lastIndexOf("-"));
698
            String day = date.substring(date.lastIndexOf("-") + 1, date.length());
699
            /*System.out.println("date: " + "year: " + new Integer(year).intValue() + 
700
                    " month: " + new Integer(month).intValue() + " day: " + 
701
                    new Integer(day).intValue());
702
            */
703
            String time = s.substring(tIndex + 1, zIndex);
704
            String hour = time.substring(0, time.indexOf(":"));
705
            String minute = time.substring(time.indexOf(":") + 1, time.lastIndexOf(":"));
706
            String seconds = "00";
707
            String milliseconds = "00";
708
            if(time.indexOf(".") != -1)
709
            {
710
                seconds = time.substring(time.lastIndexOf(":") + 1, time.indexOf("."));
711
                milliseconds = time.substring(time.indexOf(".") + 1, time.length());
712
            }
713
            /*System.out.println("time: " + "hour: " + new Integer(hour).intValue() + 
714
                    " minute: " + new Integer(minute).intValue() + " seconds: " + 
715
                    new Integer(seconds).intValue() + " milli: " + 
716
                    new Integer(milliseconds).intValue());*/
717
            
718
            //d = DateFormat.getDateTimeInstance().parse(date + " " + time);
719
            Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT-0"));
720
            c.set(new Integer(year).intValue(), new Integer(month).intValue() - 1, 
721
                  new Integer(day).intValue(), new Integer(hour).intValue(), 
722
                  new Integer(minute).intValue(), new Integer(seconds).intValue());
723
            c.set(Calendar.MILLISECOND, new Integer(milliseconds).intValue());
724
            d = new Date(c.getTimeInMillis());
725
            //System.out.println("d: " + d);
726
            return d;
727
        }
728
        else
729
        {  //if it's not in the expected format, try the formatter
730
            return DateFormat.getDateTimeInstance().parse(s);
731
        }
732
    }
733

    
734
    /*
735
     * Look up the information on the session using the token provided in
736
     * the AuthToken.  The Session should have all relevant user information.
737
     * If the session has expired or is invalid, the 'public' session will
738
     * be returned, giving the user anonymous access.
739
     */
740
    private static SessionData getSessionData(AuthToken token) {
741
        SessionData sessionData = null;
742
        String sessionId = "PUBLIC";
743
        if (token != null) {
744
            sessionId = token.getToken();
745
        }
746
        
747
        // if the session id is registered in SessionService, get the
748
        // SessionData for it. Otherwise, use the public session.
749
        if (SessionService.getInstance().isSessionRegistered(sessionId)) {
750
            sessionData = SessionService.getInstance().getRegisteredSession(sessionId);
751
        } else {
752
            sessionData = SessionService.getInstance().getPublicSession();
753
        }
754
        
755
        return sessionData;
756
    }
757

    
758
    /** 
759
     * Determine if a given object should be treated as an XML science metadata
760
     * object. 
761
     * 
762
     * TODO: This test should be externalized in a configuration dictionary rather than being hardcoded.
763
     * 
764
     * @param sysmeta the SystemMetadata describig the object
765
     * @return true if the object should be treated as science metadata
766
     */
767
    private boolean isScienceMetadata(SystemMetadata sysmeta) {
768
        boolean scimeta = false;
769
        switch (sysmeta.getObjectFormat()) {
770
            case EML_2_1_0: scimeta = true; break;
771
            case EML_2_0_1: scimeta = true; break;
772
            case EML_2_0_0: scimeta = true; break;
773
            case FGDC_STD_001_1_1999: scimeta = true; break;
774
            case FGDC_STD_001_1998: scimeta = true; break;
775
            case NCML_2_2: scimeta = true; break;
776
        }
777
        
778
        return scimeta;
779
    }
780

    
781
    /**
782
     * insert a data doc
783
     * @param object
784
     * @param guid
785
     * @param sessionData
786
     * @throws ServiceFailure
787
     */
788
    private void insertDataObject(InputStream object, Identifier guid, 
789
            SessionData sessionData) throws ServiceFailure {
790
        
791
        String username = sessionData.getUserName();
792
        String[] groups = sessionData.getGroupNames();
793

    
794
        // generate guid/localId pair for object
795
        logMetacat.debug("Generating a guid/localId mapping");
796
        IdentifierManager im = IdentifierManager.getInstance();
797
        String localId = im.generateLocalId(guid.getValue(), 1);
798

    
799
        try {
800
            logMetacat.debug("Case DATA: starting to write to disk.");
801
            if (DocumentImpl.getDataFileLockGrant(localId)) {
802
    
803
                // Save the data file to disk using "localId" as the name
804
                try {
805
                    String datafilepath = PropertyService.getProperty("application.datafilepath");
806
    
807
                    File dataDirectory = new File(datafilepath);
808
                    dataDirectory.mkdirs();
809
    
810
                    File newFile = writeStreamToFile(dataDirectory, localId, object);
811
    
812
                    // TODO: Check that the file size matches SystemMetadata
813
                    //                        long size = newFile.length();
814
                    //                        if (size == 0) {
815
                    //                            throw new IOException("Uploaded file is 0 bytes!");
816
                    //                        }
817
    
818
                    // Register the file in the database (which generates an exception
819
                    // if the localId is not acceptable or other untoward things happen
820
                    try {
821
                        logMetacat.debug("Registering document...");
822
                        System.out.println("inserting data object: localId: " + localId);
823
                        DocumentImpl.registerDocument(localId, "BIN", localId,
824
                                username, groups);
825
                        logMetacat.debug("Registration step completed.");
826
                    } catch (SQLException e) {
827
                        //newFile.delete();
828
                        logMetacat.debug("SQLE: " + e.getMessage());
829
                        e.printStackTrace(System.out);
830
                        throw new ServiceFailure("1190", "Registration failed: " + e.getMessage());
831
                    } catch (AccessionNumberException e) {
832
                        //newFile.delete();
833
                        logMetacat.debug("ANE: " + e.getMessage());
834
                        e.printStackTrace(System.out);
835
                        throw new ServiceFailure("1190", "Registration failed: " + e.getMessage());
836
                    } catch (Exception e) {
837
                        //newFile.delete();
838
                        logMetacat.debug("Exception: " + e.getMessage());
839
                        e.printStackTrace(System.out);
840
                        throw new ServiceFailure("1190", "Registration failed: " + e.getMessage());
841
                    }
842
    
843
                    logMetacat.debug("Logging the creation event.");
844
                    EventLog.getInstance().log(metacatUrl,
845
                            username, localId, "create");
846
    
847
                    // Schedule replication for this data file
848
                    logMetacat.debug("Scheduling replication.");
849
                    ForceReplicationHandler frh = new ForceReplicationHandler(
850
                            localId, "insert", false, null);
851
    
852
                } catch (PropertyNotFoundException e) {
853
                    throw new ServiceFailure("1190", "Could not lock file for writing:" + e.getMessage());
854
                }
855
    
856
            }
857
        } catch (Exception e) {
858
            // Could not get a lock on the document, so we can not update the file now
859
            throw new ServiceFailure("1190", "Failed to lock file: " + e.getMessage());
860
        }
861
    }
862

    
863
    /**
864
     * write a file to a stream
865
     * @param dir
866
     * @param fileName
867
     * @param data
868
     * @return
869
     * @throws ServiceFailure
870
     */
871
    private File writeStreamToFile(File dir, String fileName, InputStream data) 
872
        throws ServiceFailure {
873
        
874
        File newFile = new File(dir, fileName);
875
        logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
876

    
877
        try {
878
            if (newFile.createNewFile()) {
879
                // write data stream to desired file
880
                OutputStream os = new FileOutputStream(newFile);
881
                long length = IOUtils.copyLarge(data, os);
882
                os.flush();
883
                os.close();
884
            } else {
885
                logMetacat.debug("File creation failed, or file already exists.");
886
                throw new ServiceFailure("1190", "File already exists: " + fileName);
887
            }
888
        } catch (FileNotFoundException e) {
889
            logMetacat.debug("FNF: " + e.getMessage());
890
            throw new ServiceFailure("1190", "File not found: " + fileName + " " 
891
                    + e.getMessage());
892
        } catch (IOException e) {
893
            logMetacat.debug("IOE: " + e.getMessage());
894
            throw new ServiceFailure("1190", "File was not written: " + fileName 
895
                    + " " + e.getMessage());
896
        }
897

    
898
        return newFile;
899
    }
900

    
901
    /**
902
     * insert a systemMetadata doc
903
     */
904
    private void insertSystemMetadata(SystemMetadata sysmeta, SessionData sessionData) 
905
        throws ServiceFailure 
906
    {
907
        logMetacat.debug("Starting to insert SystemMetadata...");
908
    
909
        // generate guid/localId pair for sysmeta
910
        Identifier sysMetaGuid = new Identifier();
911
        sysMetaGuid.setValue(DocumentUtil.generateDocumentId(1));
912
        sysmeta.setDateSysMetadataModified(new Date());
913

    
914
        String xml = new String(serializeSystemMetadata(sysmeta).toByteArray());
915
        String localId = insertDocument(xml, sysMetaGuid, sessionData);
916
        //insert the system metadata doc id into the identifiers table to 
917
        //link it to the data or metadata document
918
        IdentifierManager.getInstance().createSystemMetadataMapping(
919
                sysmeta.getIdentifier().getValue(), sysMetaGuid.getValue());
920
    }
921
    
922
    /**
923
     * update a systemMetadata doc
924
     */
925
    private void updateSystemMetadata(SystemMetadata sm, SessionData sessionData)
926
      throws ServiceFailure
927
    {
928
        try
929
        {
930
            String smId = IdentifierManager.getInstance().getSystemMetadataId(sm.getIdentifier().getValue());
931
            sm.setDateSysMetadataModified(new Date());
932
            String xml = new String(serializeSystemMetadata(sm).toByteArray());
933
            Identifier id = new Identifier();
934
            id.setValue(smId);
935
            String localId = updateDocument(xml, id, null, sessionData);
936
            IdentifierManager.getInstance().updateSystemMetadataMapping(sm.getIdentifier().getValue(), localId);
937
        }
938
        catch(Exception e)
939
        {
940
            throw new ServiceFailure("1030", "Error updating system metadata: " + e.getMessage());
941
        }
942
    }
943
    
944
    /**
945
     * insert a document
946
     * NOTE: this method shouldn't be used from the update or create() methods.  
947
     * we shouldn't be putting the science metadata or data objects into memory.
948
     */
949
    private String insertDocument(String xml, Identifier guid, SessionData sessionData)
950
        throws ServiceFailure
951
    {
952
        return insertOrUpdateDocument(xml, guid, sessionData, "insert");
953
    }
954
    
955
    /**
956
     * insert a document from a stream
957
     */
958
    private String insertDocument(InputStream is, Identifier guid, SessionData sessionData)
959
      throws IOException, ServiceFailure
960
    {
961
        //HACK: change this eventually.  we should not be converting the stream to a string
962
        String xml = IOUtils.toString(is);
963
        return insertDocument(xml, guid, sessionData);
964
    }
965
    
966
    /**
967
     * update a document
968
     * NOTE: this method shouldn't be used from the update or create() methods.  
969
     * we shouldn't be putting the science metadata or data objects into memory.
970
     */
971
    private String updateDocument(String xml, Identifier obsoleteGuid, Identifier guid, SessionData sessionData)
972
        throws ServiceFailure
973
    {
974
        return insertOrUpdateDocument(xml, obsoleteGuid, sessionData, "update");
975
    }
976
    
977
    /**
978
     * update a document from a stream
979
     */
980
    private String updateDocument(InputStream is, Identifier obsoleteGuid, Identifier guid, SessionData sessionData)
981
      throws IOException, ServiceFailure
982
    {
983
        //HACK: change this eventually.  we should not be converting the stream to a string
984
        String xml = IOUtils.toString(is);
985
        String localId = updateDocument(xml, obsoleteGuid, guid, sessionData);
986
        IdentifierManager im = IdentifierManager.getInstance();
987
        if(guid != null)
988
        {
989
          im.createMapping(guid.getValue(), localId);
990
        }
991
        return localId;
992
    }
993
    
994
    /**
995
     * insert a document, return the id of the document that was inserted
996
     */
997
    private String insertOrUpdateDocument(String xml, Identifier guid, SessionData sessionData, String insertOrUpdate) 
998
        throws ServiceFailure {
999
        logMetacat.debug("Starting to insert xml document...");
1000
        IdentifierManager im = IdentifierManager.getInstance();
1001

    
1002
        // generate guid/localId pair for sysmeta
1003
        String localId = null;
1004
        if(insertOrUpdate.equals("insert"))
1005
        {
1006
            localId = im.generateLocalId(guid.getValue(), 1);
1007
        }
1008
        else
1009
        {
1010
            //localid should already exist in the identifier table, so just find it
1011
            try
1012
            {
1013
                localId = im.getLocalId(guid.getValue());
1014
                //increment the revision
1015
                String docid = localId.substring(0, localId.lastIndexOf("."));
1016
                String revS = localId.substring(localId.lastIndexOf(".") + 1, localId.length());
1017
                int rev = new Integer(revS).intValue();
1018
                rev++;
1019
                docid = docid + "." + rev;
1020
                localId = docid;
1021
            }
1022
            catch(McdbDocNotFoundException e)
1023
            {
1024
                throw new ServiceFailure("1030", "CrudService.insertOrUpdateDocument(): " +
1025
                    "guid " + guid.getValue() + " should have been in the identifier table, but it wasn't: " + e.getMessage());
1026
            }
1027
        }
1028
        logMetacat.debug("Metadata guid|localId: " + guid.getValue() + "|" +
1029
                localId);
1030

    
1031
        String[] action = new String[1];
1032
        action[0] = insertOrUpdate;
1033
        params.put("action", action);
1034
        String[] docid = new String[1];
1035
        docid[0] = localId;
1036
        params.put("docid", docid);
1037
        String[] doctext = new String[1];
1038
        doctext[0] = xml;
1039
        logMetacat.debug(doctext[0]);
1040
        params.put("doctext", doctext);
1041
        
1042
        // TODO: refactor handleInsertOrUpdateAction() to not output XML directly
1043
        // onto output stream, or alternatively, capture that and parse it to 
1044
        // generate the right exceptions
1045
        //ByteArrayOutputStream output = new ByteArrayOutputStream();
1046
        //PrintWriter pw = new PrintWriter(output);
1047
        String result = handler.handleInsertOrUpdateAction(metacatUrl, null, 
1048
                            null, params, sessionData.getUserName(), sessionData.getGroupNames());
1049
        //String outputS = new String(output.toByteArray());
1050
        logMetacat.debug("CrudService.insertDocument - Metacat returned: " + result);
1051
//        if (!(outputS.indexOf("<success>") > 0 && outputS.indexOf(localId) > 0)) {
1052
//            throw new ServiceFailure(1190, outputS);
1053
//        }
1054
        logMetacat.debug("Finsished inserting xml document with id " + localId);
1055
        return localId;
1056
    }
1057
    
1058
    /**
1059
     * serialize a system metadata doc
1060
     * @param sysmeta
1061
     * @return
1062
     * @throws ServiceFailure
1063
     */
1064
    public static ByteArrayOutputStream serializeSystemMetadata(SystemMetadata sysmeta) 
1065
        throws ServiceFailure {
1066
        IBindingFactory bfact;
1067
        ByteArrayOutputStream sysmetaOut = null;
1068
        try {
1069
            bfact = BindingDirectory.getFactory(SystemMetadata.class);
1070
            IMarshallingContext mctx = bfact.createMarshallingContext();
1071
            sysmetaOut = new ByteArrayOutputStream();
1072
            mctx.marshalDocument(sysmeta, "UTF-8", null, sysmetaOut);
1073
        } catch (JiBXException e) {
1074
            throw new ServiceFailure("1190", "Failed to serialize and insert SystemMetadata: " + e.getMessage());
1075
        }
1076
        
1077
        return sysmetaOut;
1078
    }
1079
    
1080
    /**
1081
     * deserialize a system metadata doc
1082
     * @param xml
1083
     * @return
1084
     * @throws ServiceFailure
1085
     */
1086
    public static SystemMetadata deserializeSystemMetadata(InputStream xml) 
1087
        throws ServiceFailure {
1088
        try {
1089
            IBindingFactory bfact = BindingDirectory.getFactory(SystemMetadata.class);
1090
            IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
1091
            SystemMetadata sysmeta = (SystemMetadata) uctx.unmarshalDocument(xml, null);
1092
            return sysmeta;
1093
        } catch (JiBXException e) {
1094
            throw new ServiceFailure("1190", "Failed to serialize and insert SystemMetadata: " + e.getMessage());
1095
        }    
1096
    }
1097
    
1098
    /**
1099
     * produce an md5 checksum for item
1100
     */
1101
    private String checksum(InputStream is)
1102
      throws Exception
1103
    {        
1104
        byte[] buffer = new byte[1024];
1105
        MessageDigest complete = MessageDigest.getInstance("MD5");
1106
        int numRead;
1107
        
1108
        do 
1109
        {
1110
          numRead = is.read(buffer);
1111
          if (numRead > 0) 
1112
          {
1113
            complete.update(buffer, 0, numRead);
1114
          }
1115
        } while (numRead != -1);
1116
        
1117
        
1118
        return getHex(complete.digest());
1119
    }
1120
    
1121
    /**
1122
     * convert a byte array to a hex string
1123
     */
1124
    private static String getHex( byte [] raw ) 
1125
    {
1126
        final String HEXES = "0123456789ABCDEF";
1127
        if ( raw == null ) {
1128
          return null;
1129
        }
1130
        final StringBuilder hex = new StringBuilder( 2 * raw.length );
1131
        for ( final byte b : raw ) {
1132
          hex.append(HEXES.charAt((b & 0xF0) >> 4))
1133
             .append(HEXES.charAt((b & 0x0F)));
1134
        }
1135
        return hex.toString();
1136
    }
1137
    
1138
    /**
1139
     * parse the metacat date which looks like 2010-06-08 (YYYY-MM-DD) into
1140
     * a proper date object
1141
     * @param date
1142
     * @return
1143
     */
1144
    private Date parseMetacatDate(String date)
1145
    {
1146
        String year = date.substring(0, 4);
1147
        String month = date.substring(5, 7);
1148
        String day = date.substring(8, 10);
1149
        Calendar c = Calendar.getInstance();
1150
        c.set(new Integer(year).intValue(), 
1151
              new Integer(month).intValue(), 
1152
              new Integer(day).intValue());
1153
        return c.getTime();
1154
    }
1155
    
1156
    /**
1157
     * find the size (in bytes) of a stream
1158
     * @param is
1159
     * @return
1160
     * @throws IOException
1161
     */
1162
    private long sizeOfStream(InputStream is)
1163
        throws IOException
1164
    {
1165
        long size = 0;
1166
        byte[] b = new byte[1024];
1167
        int numread = is.read(b, 0, 1024);
1168
        while(numread != -1)
1169
        {
1170
            size += numread;
1171
            numread = is.read(b, 0, 1024);
1172
        }
1173
        return size;
1174
    }
1175
    
1176
    /**
1177
     * create system metadata with a specified id, doc and format
1178
     */
1179
    private SystemMetadata createSystemMetadata(String localId, AuthToken token)
1180
      throws Exception
1181
    {
1182
        IdentifierManager im = IdentifierManager.getInstance();
1183
        Hashtable<String, String> docInfo = im.getDocumentInfo(localId);
1184
        
1185
        //get the document text
1186
        int rev = im.getLatestRevForLocalId(localId);
1187
        Identifier identifier = new Identifier();
1188
        identifier.setValue(im.getGUID(localId, rev));
1189
        InputStream is = this.get(token, identifier);
1190
        
1191
        SystemMetadata sm = new SystemMetadata();
1192
        //set the id
1193
        sm.setIdentifier(identifier);
1194
        
1195
        //set the object format
1196
        String doctype = docInfo.get("doctype");
1197
        ObjectFormat format = ObjectFormat.convert(docInfo.get("doctype"));
1198
        if(format == null)
1199
        {
1200
            if(doctype.trim().equals("BIN"))
1201
            {
1202
                format = ObjectFormat.APPLICATIONOCTETSTREAM;
1203
            }
1204
            else
1205
            {
1206
                format = ObjectFormat.convert("text/plain");
1207
            }
1208
        }
1209
        sm.setObjectFormat(format);
1210
        
1211
        //create the checksum
1212
        String checksumS = checksum(is);
1213
        ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
1214
        Checksum checksum = new Checksum();
1215
        checksum.setValue(checksumS);
1216
        checksum.setAlgorithm(ca);
1217
        sm.setChecksum(checksum);
1218
        
1219
        //set the size
1220
        is = this.get(token, identifier);
1221
        sm.setSize(sizeOfStream(is));
1222
        
1223
        //submitter
1224
        Principal p = new Principal();
1225
        p.setValue(docInfo.get("user_owner"));
1226
        sm.setSubmitter(p);
1227
        sm.setRightsHolder(p);
1228
        try
1229
        {
1230
            Date dateCreated = parseMetacatDate(docInfo.get("date_created"));
1231
            sm.setDateUploaded(dateCreated);
1232
            Date dateUpdated = parseMetacatDate(docInfo.get("date_updated"));
1233
            sm.setDateSysMetadataModified(dateUpdated);
1234
        }
1235
        catch(Exception e)
1236
        {
1237
            System.out.println("couldn't parse a date: " + e.getMessage());
1238
            Date dateCreated = new Date();
1239
            sm.setDateUploaded(dateCreated);
1240
            Date dateUpdated = new Date();
1241
            sm.setDateSysMetadataModified(dateUpdated);
1242
        }
1243
        NodeReference nr = new NodeReference();
1244
        nr.setValue("metacat");
1245
        sm.setOriginMemberNode(nr);
1246
        sm.setAuthoritativeMemberNode(nr);
1247
        return sm;
1248
    }
1249
}
(1-1/2)