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
import java.text.SimpleDateFormat;
39

    
40
import javax.servlet.ServletContext;
41
import javax.servlet.http.HttpServletRequest;
42
import javax.servlet.http.HttpServletResponse;
43

    
44
import org.apache.commons.io.IOUtils;
45
import org.apache.log4j.Logger;
46
import org.dataone.service.exceptions.IdentifierNotUnique;
47
import org.dataone.service.exceptions.InsufficientResources;
48
import org.dataone.service.exceptions.InvalidRequest;
49
import org.dataone.service.exceptions.InvalidSystemMetadata;
50
import org.dataone.service.exceptions.InvalidToken;
51
import org.dataone.service.exceptions.NotAuthorized;
52
import org.dataone.service.exceptions.NotFound;
53
import org.dataone.service.exceptions.NotImplemented;
54
import org.dataone.service.exceptions.ServiceFailure;
55
import org.dataone.service.exceptions.UnsupportedType;
56
import org.dataone.service.mn.MemberNodeCrud;
57
import org.dataone.service.types.*;
58
import org.jibx.runtime.BindingDirectory;
59
import org.jibx.runtime.IBindingFactory;
60
import org.jibx.runtime.IMarshallingContext;
61
import org.jibx.runtime.IUnmarshallingContext;
62
import org.jibx.runtime.JiBXException;
63

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

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

    
85
/**
86
 * 
87
 * Implements DataONE MemberNode CRUD API for Metacat. 
88
 * 
89
 * @author Matthew Jones
90
 */
91
public class CrudService implements MemberNodeCrud
92
{
93
    private static CrudService crudService = null;
94

    
95
    private MetacatHandler handler;
96
    private Hashtable<String, String[]> params;
97
    private Logger logMetacat = null;
98
    private Logger logCrud = null;
99
    
100
    private String metacatUrl;
101
    
102
    private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
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
     */
120
    public CrudService() {
121
    //change crud service into a singleton.  dont pass servlet data structures here
122
        logMetacat = Logger.getLogger(CrudService.class);
123
        logCrud = Logger.getLogger("DataOneLogger");
124
        try
125
        {
126
            String server = PropertyService.getProperty("server.name");
127
            String port = PropertyService.getProperty("server.httpPort");
128
            String context = PropertyService.getProperty("application.context");
129
            metacatUrl = "http://" + server + ":" + port + "/" + context;
130
            logMetacat.debug("Initializing CrudService with url " + metacatUrl);
131
        }
132
        catch(Exception e)
133
        {
134
            logMetacat.error("Could not find servlet url in CrudService: " + e.getMessage());
135
            e.printStackTrace();
136
            throw new RuntimeException("Error getting servlet url in CrudService: " + e.getMessage());
137
        }
138
        
139
        /*this.servletContext = servletContext;
140
        this.request = request;
141
        this.response = response;*/
142
        
143
        params = new Hashtable<String, String[]>();
144

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

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

    
269
        logMetacat.debug("Starting CrudService.create()...");
270
        
271
        // authenticate & get user info
272
        SessionData sessionData = getSessionData(token);
273
        String username = sessionData.getUserName();
274
        String[] groups = sessionData.getGroupNames();
275
        String localId = null;
276

    
277
        if (username == null || username.equals("public"))
278
        {
279
            throw new NotAuthorized("1000", "User " + username + " is not authorized to create content." +
280
                    "  If you are not logged in, please do so and retry the request.");
281
        }
282
        
283
        // verify that guid == SystemMetadata.getIdentifier()
284
        logMetacat.debug("Comparing guid|sysmeta_guid: " + guid.getValue() + "|" + sysmeta.getIdentifier().getValue());
285
        if (!guid.getValue().equals(sysmeta.getIdentifier().getValue())) {
286
            throw new InvalidSystemMetadata("1180", 
287
                "GUID in method call does not match GUID in system metadata.");
288
        }
289

    
290
        logMetacat.debug("Checking if identifier exists...");
291
        // Check that the identifier does not already exist
292
        IdentifierManager im = IdentifierManager.getInstance();
293
        if (im.identifierExists(guid.getValue())) {
294
            throw new IdentifierNotUnique("1120", 
295
                "GUID is already in use by an existing object.");
296
        }
297

    
298
        // Check if we are handling metadata or data
299
        boolean isScienceMetadata = isScienceMetadata(sysmeta);
300
        
301
        if (isScienceMetadata) {
302
            // CASE METADATA:
303
            try {
304
                this.insertDocument(object, guid, sessionData);
305
                localId = im.getLocalId(guid.getValue());
306
            } catch (IOException e) {
307
                String msg = "Could not create string from XML stream: " +
308
                    " " + e.getMessage();
309
                logMetacat.debug(msg);
310
                throw new ServiceFailure("1190", msg);
311
            } catch(Exception e) {
312
                String msg = "Unexpected error in CrudService.create: " + e.getMessage();
313
                logMetacat.debug(msg);
314
                throw new ServiceFailure("1190", msg);
315
            }
316
            
317

    
318
        } else {
319
            // DEFAULT CASE: DATA (needs to be checked and completed)
320
            insertDataObject(object, guid, sessionData);
321
            
322
        }
323

    
324
        // For Metadata and Data, insert the system metadata into the object store too
325
        insertSystemMetadata(sysmeta, sessionData);
326
        logMetacat.debug("Returning from CrudService.create()");
327
        EventLog.getInstance().log(metacatUrl,
328
                username, localId, "create");
329
        logCrud.info("create localId:" + localId + " guid:" + guid.getValue());
330
        return guid;
331
    }
332
    
333
    /**
334
     * update an existing object with a new object.  Change the system metadata
335
     * to reflect the changes and update it as well.
336
     */
337
    public Identifier update(AuthToken token, Identifier guid, 
338
            InputStream object, Identifier obsoletedGuid, SystemMetadata sysmeta) 
339
            throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, 
340
            UnsupportedType, InsufficientResources, NotFound, InvalidSystemMetadata, 
341
            NotImplemented {
342
        try
343
        {
344
            SessionData sessionData = getSessionData(token);
345
            
346
            //find the old systemmetadata (sm.old) document id (the one linked to obsoletedGuid)
347
            SystemMetadata sm = getSystemMetadata(token, obsoletedGuid);
348
            //change sm.old's obsoletedBy field 
349
            List l = sm.getObsoletedByList();
350
            l.add(guid);
351
            sm.setObsoletedByList(l);
352
            //update sm.old
353
            updateSystemMetadata(sm, sessionData);
354
            
355
            //change the obsoletes field of the new systemMetadata (sm.new) to point to the id of the old one
356
            sysmeta.addObsolete(obsoletedGuid);
357
            //insert sm.new
358
            insertSystemMetadata(sysmeta, sessionData);
359
            
360
            boolean isScienceMetadata = isScienceMetadata(sysmeta);
361
            if(isScienceMetadata)
362
            {
363
                //update the doc
364
                updateDocument(object, obsoletedGuid, guid, sessionData);
365
            }
366
            else
367
            {
368
                //update a data file, not xml
369
                insertDataObject(object, guid, sessionData);
370
            }
371
            
372
            IdentifierManager im = IdentifierManager.getInstance();
373
            String username = sessionData.getUserName();
374
            EventLog.getInstance().log(metacatUrl,
375
                    username, im.getLocalId(guid.getValue()), "update");
376
            logCrud.info("update localId:" + im.getLocalId(guid.getValue()) + " guid:" + guid.getValue());
377
            return guid;
378
        }
379
        catch(Exception e)
380
        {
381
            throw new ServiceFailure("1030", "Error updating document in CrudService: " + e.getMessage());
382
        }
383
    }
384
    
385
    /**
386
     * set the permission on the document
387
     * @param token
388
     * @param principal
389
     * @param permission
390
     * @param permissionType
391
     * @param permissionOrder
392
     * @return
393
     */
394
    public void setAccess(AuthToken token, Identifier id, String principal, String permission,
395
            String permissionType, String permissionOrder, boolean setSystemMetadata)
396
      throws ServiceFailure
397
    {
398
        try
399
        {
400
            IdentifierManager im = IdentifierManager.getInstance();
401
            String docid = im.getLocalId(id.getValue());
402
            final SessionData sessionData = getSessionData(token);
403
            String permNum = "0";
404
            if(permission.equals("read"))
405
            {
406
                permNum = "4";
407
            }
408
            else if(permission.equals("write"))
409
            {
410
                permNum = "6";
411
            }
412
            System.out.println("user " + sessionData.getUserName() + 
413
                    " is setting access level " + permNum + " for permission " + 
414
                    permissionType + " on doc with localid " + docid);
415
            handler.setAccess(metacatUrl, sessionData.getUserName(), docid, 
416
                    principal, permNum, permissionType, permissionOrder);
417
            if(setSystemMetadata)
418
            {
419
                //set the same perms on the system metadata doc
420
                String smlocalid = im.getSystemMetadataLocalId(id.getValue());
421
                System.out.println("setting access on SM doc with localid " + smlocalid);
422
                //cs.setAccess(token, smid, principal, permission, permissionType, permissionOrder);
423
                handler.setAccess(metacatUrl, sessionData.getUserName(), smlocalid,
424
                        principal, permNum, permissionType, permissionOrder);
425
            }
426
            String username = sessionData.getUserName();
427
            EventLog.getInstance().log(metacatUrl,
428
                    username, im.getLocalId(id.getValue()), "setAccess");
429
            logCrud.info("setAccess");
430
        }
431
        catch(Exception e)
432
        {
433
            e.printStackTrace();
434
            throw new ServiceFailure("1000", "Could not set access on the document with id " + id.getValue());
435
        }
436
    }
437
    
438
    /**
439
     *  Retrieve the list of objects present on the MN that match the calling 
440
     *  parameters. This method is required to support the process of Member 
441
     *  Node synchronization. At a minimum, this method should be able to 
442
     *  return a list of objects that match:
443
     *  startTime <= SystemMetadata.dateSysMetadataModified
444
     *  but is expected to also support date range (by also specifying endTime), 
445
     *  and should also support slicing of the matching set of records by 
446
     *  indicating the starting index of the response (where 0 is the index 
447
     *  of the first item) and the count of elements to be returned.
448
     *  
449
     *  If startTime or endTime is null, the query is not restricted by that parameter.
450
     *  
451
     * @see http://mule1.dataone.org/ArchitectureDocs/mn_api_replication.html#MN_replication.listObjects
452
     * @param token
453
     * @param startTime
454
     * @param endTime
455
     * @param objectFormat
456
     * @param replicaStatus
457
     * @param start
458
     * @param count
459
     * @return ObjectList
460
     * @throws NotAuthorized
461
     * @throws InvalidRequest
462
     * @throws NotImplemented
463
     * @throws ServiceFailure
464
     * @throws InvalidToken
465
     */
466
    public ObjectList listObjects(AuthToken token, Date startTime, Date endTime, 
467
        ObjectFormat objectFormat, boolean replicaStatus, int start, int count)
468
      throws NotAuthorized, InvalidRequest, NotImplemented, ServiceFailure, InvalidToken
469
    {
470
      ObjectList ol = new ObjectList();
471
      final SessionData sessionData = getSessionData(token);
472
      int totalAfterQuery = 0;
473
      try
474
      {
475
          
476
          params.clear();
477
          params.put("returndoctype", new String[] {"http://dataone.org/service/types/SystemMetadata/0.1"});
478
          params.put("qformat", new String[] {"xml"});
479
          params.put("returnfield", new String[] {"size", "originMemberNode", 
480
                  "identifier", "objectFormat", "dateSysMetadataModified", "checksum", "checksum/@algorithm"});
481
          params.put("anyfield", new String[] {"%"});
482
          
483
          //System.out.println("query is: metacatUrl: " + metacatUrl + " user: " + sessionData.getUserName() +
484
          //    " sessionid: " + sessionData.getId() + " params: " + params.toString());
485
          String username = "public";
486
          String[] groups = null;
487
          String sessionid = "";
488
          if(sessionData != null)
489
          {
490
              username = sessionData.getUserName();
491
              groups = sessionData.getGroupNames();
492
              sessionid = sessionData.getId();
493
          }
494
          
495
          MetacatResultSet rs = handler.query(metacatUrl, params, username, 
496
                  groups, sessionid);
497
          List docs = rs.getDocuments();
498
          if(count == 1000)
499
          {
500
              count = docs.size();
501
          }
502
          
503
          //System.out.println("query returned " + docs.size() + " documents.");
504
          Vector<Document> docCopy = new Vector<Document>();
505
          
506
          //preparse the list to remove any that don't match the query params
507
          for(int i=0; i<docs.size(); i++)
508
          {
509
              Document d = (Document)docs.get(i);
510
              ObjectFormat returnedObjectFormat = ObjectFormat.convert(d.getField("objectFormat"));
511
              
512
              if(returnedObjectFormat != null && 
513
                 objectFormat != null && 
514
                 !objectFormat.toString().trim().equals(returnedObjectFormat.toString().trim()))
515
              { //make sure the objectFormat is the one specified
516
                  continue;
517
              }
518
              
519
              String dateSMM = d.getField("dateSysMetadataModified");
520
              if((startTime != null || endTime != null) && dateSMM == null)
521
              {  //if startTime or endTime are not null, we need a date to compare to
522
                  continue;
523
              }
524
              
525
              //date parse
526
              Date dateSysMetadataModified = null;
527
              if(dateSMM != null)
528
              {
529
                  //dateSysMetadataModified = parseDate(dateSMM);
530
                  if(dateSMM.indexOf(".") != -1)
531
                  {  //strip the milliseconds
532
                      dateSMM = dateSMM.substring(0, dateSMM.indexOf(".")) + 'Z';
533
                  }
534
                  //System.out.println("dateSMM: " + dateSMM);
535
                  dateFormat.setTimeZone(TimeZone.getTimeZone("GMT-0"));
536
                  dateSysMetadataModified = dateFormat.parse(dateSMM);
537
                  //System.out.println("dateSysMetadataModified: " + dateSysMetadataModified);
538
                  
539
                  //dateSysMetadataModified = getDateInTimeZone(dateSysMetadataModified, TimeZone.getDefault().getID());
540
                  //System.out.println("dateSysMetadataModified after convertion to default timezone: " + dateSysMetadataModified);
541
              }
542
              /*System.out.println("docid: " + d.docid);
543
              System.out.println("dateSysMetadataModified: " + dateSysMetadataModified);
544
              System.out.println("startTime: " + startTime);
545
              System.out.println("endtime: " + endTime);*/
546
              
547
              int startDateComparison = 0;
548
              int endDateComparison = 0;
549
              if(startTime != null)
550
              {
551
                  Calendar zTime = Calendar.getInstance(TimeZone.getTimeZone("GMT-0"));
552
                  zTime.setTime(startTime);
553
                  startTime = zTime.getTime();
554
                  
555
                  if(dateSysMetadataModified == null)
556
                  {
557
                      startDateComparison = -1;
558
                  }
559
                  else
560
                  {
561
                      startDateComparison = dateSysMetadataModified.compareTo(startTime);
562
                  }
563
                  //System.out.println("startDateCom: " + startDateComparison);
564
              }
565
              else
566
              {
567
                  startDateComparison = 1;
568
              }
569
              
570
              if(endTime != null)
571
              {
572
                  Calendar zTime = Calendar.getInstance(TimeZone.getTimeZone("GMT-0"));
573
                  zTime.setTime(endTime);
574
                  endTime = zTime.getTime();
575
                  
576
                  if(dateSysMetadataModified == null)
577
                  {
578
                      endDateComparison = 1;
579
                  }
580
                  else
581
                  {
582
                      endDateComparison = dateSysMetadataModified.compareTo(endTime);
583
                  }
584
                  //System.out.println("endDateCom: " + endDateComparison);
585
              }
586
              else
587
              {
588
                  endDateComparison = -1;
589
              }
590
              
591
              
592
              if(startDateComparison < 0 || endDateComparison > 0)
593
              { 
594
                  continue;                  
595
              }
596
              
597
              docCopy.add((Document)docs.get(i));
598
          } //end pre-parse
599
          
600
          docs = docCopy;
601
          totalAfterQuery = docs.size();
602
          System.out.println("total after subquery: " + totalAfterQuery);
603
          
604
          //make sure we don't run over the end
605
          int end = start + count;
606
          if(end > docs.size())
607
          {
608
              end = docs.size();
609
          }
610
          
611
          for(int i=start; i<end; i++)
612
          {
613
              //get the document from the result
614
              Document d = (Document)docs.get(i);
615
              //System.out.println("processing doc " + d.docid);
616
              
617
              String dateSMM = d.getField("dateSysMetadataModified");
618
              Date dateSysMetadataModified = null;
619
              if(dateSMM != null)
620
              {
621
                  try
622
                  {
623
                      dateSysMetadataModified = parseDate(dateSMM);
624
                  }
625
                  catch(Exception e)
626
                  { //if we fail to parse the date, just ignore the value
627
                      dateSysMetadataModified = null;
628
                  }
629
              }
630
              ObjectFormat returnedObjectFormat = ObjectFormat.convert(d.getField("objectFormat"));
631
                
632
              
633
              ObjectInfo info = new ObjectInfo();
634
              //add the fields to the info object
635
              Checksum cs = new Checksum();
636
              cs.setValue(d.getField("checksum"));
637
              String csalg = d.getField("algorithm");
638
              if(csalg == null)
639
              {
640
                  csalg = "MD5";
641
              }
642
              ChecksumAlgorithm ca = ChecksumAlgorithm.convert(csalg);
643
              cs.setAlgorithm(ca);
644
              info.setChecksum(cs);
645
              info.setDateSysMetadataModified(dateSysMetadataModified);
646
              Identifier id = new Identifier();
647
              id.setValue(d.getField("identifier"));
648
              info.setIdentifier(id);
649
              info.setObjectFormat(returnedObjectFormat);
650
              String size = d.getField("size");
651
              if(size != null)
652
              {
653
                  info.setSize(new Long(size.trim()).longValue());
654
              }
655
              //add the ObjectInfo to the ObjectList
656
              if(info.getIdentifier().getValue() != null)
657
              { //id can be null from tests.  should not happen in production.
658
                  ol.addObjectInfo(info);
659
              }
660
             
661
          }
662
      }
663
      catch(Exception e)
664
      {
665
          e.printStackTrace();
666
          throw new ServiceFailure("1580", "Error retrieving ObjectList: " + e.getMessage());
667
      }
668
      String username = sessionData.getUserName();
669
      EventLog.getInstance().log(metacatUrl,
670
              username, null, "read");
671
      logCrud.info("listObjects");
672
      //System.out.println("ol.size: " + ol.sizeObjectInfoList());
673
      ol.setCount(count);
674
      ol.setStart(start);
675
      ol.setTotal(totalAfterQuery);
676
      System.out.println("returning " + ol.sizeObjectInfoList() + " items in the object list");
677
      return ol;
678
    }
679
    
680
    /**
681
     * Call listObjects with the default values for replicaStatus (true), start (0),
682
     * and count (1000).
683
     * @param token
684
     * @param startTime
685
     * @param endTime
686
     * @param objectFormat
687
     * @return
688
     * @throws NotAuthorized
689
     * @throws InvalidRequest
690
     * @throws NotImplemented
691
     * @throws ServiceFailure
692
     * @throws InvalidToken
693
     */
694
    public ObjectList listObjects(AuthToken token, Date startTime, Date endTime, 
695
        ObjectFormat objectFormat)
696
      throws NotAuthorized, InvalidRequest, NotImplemented, ServiceFailure, InvalidToken
697
    {
698
       return listObjects(token, startTime, endTime, objectFormat, true, 0, 1000);
699
    }
700

    
701
    /**
702
     * Delete a document.  NOT IMPLEMENTED
703
     */
704
    public Identifier delete(AuthToken token, Identifier guid)
705
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
706
            NotImplemented {
707
        logCrud.info("delete");
708
        throw new NotImplemented("1000", "This method not yet implemented.");
709
    }
710

    
711
    /**
712
     * describe a document.  NOT IMPLEMENTED
713
     */
714
    public DescribeResponse describe(AuthToken token, Identifier guid)
715
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
716
            NotImplemented {
717
        logCrud.info("describe");
718
        throw new NotImplemented("1000", "This method not yet implemented.");
719
    }
720
    
721
    /**
722
     * get a document with a specified guid.
723
     */
724
    public InputStream get(AuthToken token, Identifier guid)
725
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
726
            NotImplemented {
727
        
728
        // Retrieve the session information from the AuthToken
729
        // If the session is expired, then the user is 'public'
730
        final SessionData sessionData = getSessionData(token);
731
        
732
        // Look up the localId for this global identifier
733
        IdentifierManager im = IdentifierManager.getInstance();
734
        try {
735
            final String localId = im.getLocalId(guid.getValue());
736

    
737
            final InputStreamFromOutputStream<String> objectStream = 
738
                new InputStreamFromOutputStream<String>() {
739
                
740
                @Override
741
                public String produce(final OutputStream dataSink) throws Exception {
742

    
743
                    try {
744
                        handler.readFromMetacat(metacatUrl, null, 
745
                                dataSink, localId, "xml",
746
                                sessionData.getUserName(), 
747
                                sessionData.getGroupNames(), true, params);
748
                    } catch (PropertyNotFoundException e) {
749
                        e.printStackTrace();
750
                        throw new ServiceFailure("1030", "Error getting property from metacat: " + e.getMessage());
751
                    } catch (ClassNotFoundException e) {
752
                        e.printStackTrace();
753
                        throw new ServiceFailure("1030", "Class not found error when reading from metacat: " + e.getMessage());
754
                    } catch (IOException e) {
755
                        e.printStackTrace();
756
                        throw new ServiceFailure("1030", "IOException while reading from metacat: " + e.getMessage());
757
                    } catch (SQLException e) {
758
                        e.printStackTrace();
759
                        throw new ServiceFailure("1030", "SQLException while reading from metacat: " + e.getMessage());
760
                    } catch (McdbException e) {
761
                        e.printStackTrace();
762
                        throw new ServiceFailure("1030", "Metacat DB exception while reading from metacat: " + e.getMessage());
763
                    } catch (ParseLSIDException e) {
764
                        e.printStackTrace();
765
                        throw new NotFound("1020", "LSID parsing exception while reading from metacat: " + e.getMessage());
766
                    } catch (InsufficientKarmaException e) {
767
                        e.printStackTrace();
768
                        throw new NotAuthorized("1000", "User not authorized for get(): " + e.getMessage());
769
                    }
770

    
771
                    return "Completed";
772
                }
773
            };
774
            System.out.println("1");
775
            String username = sessionData.getUserName();
776
            EventLog.getInstance().log(metacatUrl,
777
                    username, im.getLocalId(guid.getValue()), "read");
778
            logCrud.info("get localId:" + localId + " guid:" + guid.getValue());
779
            return objectStream;
780

    
781
        } catch (McdbDocNotFoundException e) {
782
            throw new NotFound("1020", e.getMessage());
783
        }
784
    }
785

    
786
    /**
787
     * get the checksum for a document.  NOT IMPLEMENTED
788
     */
789
    public Checksum getChecksum(AuthToken token, Identifier guid)
790
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
791
            InvalidRequest, NotImplemented {
792
        logCrud.info("getChecksum");
793
        throw new NotImplemented("1000", "This method not yet implemented.");
794
    }
795

    
796
    /**
797
     * get the checksum for a document.  NOT IMPLEMENTED
798
     */
799
    public Checksum getChecksum(AuthToken token, Identifier guid, 
800
            String checksumAlgorithm) throws InvalidToken, ServiceFailure, 
801
            NotAuthorized, NotFound, InvalidRequest, NotImplemented {
802
        logCrud.info("getChecksum");
803
        throw new NotImplemented("1000", "This method not yet implemented.");
804
    }
805

    
806
    /**
807
     * get log records.  
808
     */
809
    public Log getLogRecords(AuthToken token, Date fromDate, Date toDate, Event event)
810
            throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest, 
811
            NotImplemented 
812
    {
813
        Log log = new Log();
814
        Vector<LogEntry> logs = new Vector<LogEntry>();
815
        IdentifierManager im = IdentifierManager.getInstance();
816
        EventLog el = EventLog.getInstance();
817
        if(fromDate == null)
818
        {
819
            System.out.println("setting fromdate from null");
820
            fromDate = new Date(1);
821
        }
822
        if(toDate == null)
823
        {
824
            System.out.println("setting todate from null");
825
            toDate = new Date();
826
        }
827
        
828
        System.out.println("fromDate: " + fromDate);
829
        System.out.println("toDate: " + toDate);
830
        
831
        String report = el.getReport(null, null, null, null, 
832
                new java.sql.Timestamp(fromDate.getTime()), 
833
                new java.sql.Timestamp(toDate.getTime()));
834
        
835
        //System.out.println("report: " + report);
836
        
837
        String logEntry = "<logEntry>";
838
        String endLogEntry = "</logEntry>";
839
        int startIndex = 0;
840
        int foundIndex = report.indexOf(logEntry, startIndex);
841
        while(foundIndex != -1)
842
        {
843
            //parse out each entry
844
            int endEntryIndex = report.indexOf(endLogEntry, foundIndex);
845
            String entry = report.substring(foundIndex, endEntryIndex);
846
            //System.out.println("entry: " + entry);
847
            startIndex = endEntryIndex + endLogEntry.length();
848
            foundIndex = report.indexOf(logEntry, startIndex);
849
            
850
            String entryId = getLogEntryField("entryid", entry);
851
            String ipAddress = getLogEntryField("ipAddress", entry);
852
            String principal = getLogEntryField("principal", entry);
853
            String docid = getLogEntryField("docid", entry);
854
            String eventS = getLogEntryField("event", entry);
855
            String dateLogged = getLogEntryField("dateLogged", entry);
856
            
857
            LogEntry le = new LogEntry();
858
            
859
            Event e = Event.convert(eventS);
860
            if(e == null)
861
            { //skip any events that are not Dataone Crud events
862
                continue;
863
            }
864
            le.setEvent(e);
865
            Identifier entryid = new Identifier();
866
            entryid.setValue(entryId);
867
            le.setEntryId(entryid);
868
            Identifier identifier = new Identifier();
869
            try
870
            {
871
                System.out.println("converting docid '" + docid + "' to a guid.");
872
                if(docid == null || docid.trim().equals("") || docid.trim().equals("null"))
873
                {
874
                    continue;
875
                }
876
                docid = docid.substring(0, docid.lastIndexOf("."));
877
                identifier.setValue(im.getGUID(docid, im.getLatestRevForLocalId(docid)));
878
            }
879
            catch(Exception ex)
880
            { //try to get the guid, if that doesn't work, just use the local id
881
                //throw new ServiceFailure("1030", "Error getting guid for localId " + 
882
                //        docid + ": " + ex.getMessage());\
883
                
884
                //skip it if the guid can't be found
885
                continue;
886
            }
887
            
888
            le.setIdentifier(identifier);
889
            le.setIpAddress(ipAddress);
890
            Calendar c = Calendar.getInstance();
891
            String year = dateLogged.substring(0, 4);
892
            String month = dateLogged.substring(5, 7);
893
            String date = dateLogged.substring(8, 10);
894
            //System.out.println("year: " + year + " month: " + month + " day: " + date);
895
            c.set(new Integer(year).intValue(), new Integer(month).intValue(), new Integer(date).intValue());
896
            Date logDate = c.getTime();
897
            le.setDateLogged(logDate);
898
            NodeReference memberNode = new NodeReference();
899
            memberNode.setValue(ipAddress);
900
            le.setMemberNode(memberNode);
901
            Principal princ = new Principal();
902
            princ.setValue(principal);
903
            le.setPrincipal(princ);
904
            le.setUserAgent("metacat/RESTService");
905
            
906
            if(event == null)
907
            {
908
                logs.add(le);
909
            }
910
            
911
            if(event != null &&
912
               e.toString().toLowerCase().trim().equals(event.toString().toLowerCase().trim()))
913
            {
914
              logs.add(le);
915
            }
916
        }
917
        
918
        log.setLogEntryList(logs);
919
        logCrud.info("getLogRecords");
920
        return log;
921
    }
922
    
923
    /**
924
     * parse a logEntry and get the relavent field from it
925
     * @param fieldname
926
     * @param entry
927
     * @return
928
     */
929
    private String getLogEntryField(String fieldname, String entry)
930
    {
931
        String begin = "<" + fieldname + ">";
932
        String end = "</" + fieldname + ">";
933
        //System.out.println("looking for " + begin + " and " + end + " in entry " + entry);
934
        String s = entry.substring(entry.indexOf(begin) + begin.length(), entry.indexOf(end));
935
        //System.out.println("entry " + fieldname + " : " + s);
936
        return s;
937
    }
938

    
939
    /**
940
     * get the system metadata for a document with a specified guid.
941
     */
942
    public SystemMetadata getSystemMetadata(AuthToken token, Identifier guid)
943
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
944
            InvalidRequest, NotImplemented {
945
        
946
        logMetacat.debug("CrudService.getSystemMetadata - for guid: " + guid.getValue());
947
        
948
        // Retrieve the session information from the AuthToken
949
        // If the session is expired, then the user is 'public'
950
        final SessionData sessionData = getSessionData(token);
951
                
952
        try {
953
            IdentifierManager im = IdentifierManager.getInstance();
954
            final String localId = im.getSystemMetadataLocalId(guid.getValue());
955
            
956
            // Read system metadata from metacat's db
957
            final InputStreamFromOutputStream<String> objectStream = 
958
                new InputStreamFromOutputStream<String>() {
959
                
960
                @Override
961
                public String produce(final OutputStream dataSink) throws Exception {
962
                    try {
963
                        handler.readFromMetacat(metacatUrl, null, 
964
                                dataSink, localId, "xml",
965
                                sessionData.getUserName(), 
966
                                sessionData.getGroupNames(), true, params);
967
                    } catch (PropertyNotFoundException e) {
968
                        e.printStackTrace();
969
                        throw new ServiceFailure("1030", "Property not found while reading system metadata from metacat: " + e.getMessage());
970
                    } catch (ClassNotFoundException e) {
971
                        e.printStackTrace();
972
                        throw new ServiceFailure("1030", "Class not found while reading system metadata from metacat: " + e.getMessage());
973
                    } catch (IOException e) {
974
                        e.printStackTrace();
975
                        throw new ServiceFailure("1030", "IOException while reading system metadata from metacat: " + e.getMessage());
976
                    } catch (SQLException e) {
977
                        e.printStackTrace();
978
                        throw new ServiceFailure("1030", "SQLException while reading system metadata from metacat: " + e.getMessage());
979
                    } catch (McdbException e) {
980
                        e.printStackTrace();
981
                        throw new ServiceFailure("1030", "Metacat DB Exception while reading system metadata from metacat: " + e.getMessage());
982
                    } catch (ParseLSIDException e) {
983
                        e.printStackTrace();
984
                        throw new NotFound("1020", "Error parsing LSID while reading system metadata from metacat: " + e.getMessage());
985
                    } catch (InsufficientKarmaException e) {
986
                        e.printStackTrace();
987
                        throw new NotAuthorized("1000", "User not authorized for get() on system metadata: " + e.getMessage());
988
                    }
989

    
990
                    return "Completed";
991
                }
992
            };
993
            
994
            // Deserialize the xml to create a SystemMetadata object
995
            SystemMetadata sysmeta = deserializeSystemMetadata(objectStream);
996
            String username = sessionData.getUserName();
997
            EventLog.getInstance().log(metacatUrl,
998
                    username, im.getLocalId(guid.getValue()), "read");
999
            logCrud.info("getSystemMetadata localId: " + localId + " guid:" + guid.getValue());
1000
            return sysmeta;
1001
            
1002
        } catch (McdbDocNotFoundException e) {
1003
            //e.printStackTrace();
1004
            throw new NotFound("1000", e.getMessage());
1005
        }                
1006
    }
1007
    
1008
    /**
1009
     * parse the date in the systemMetadata
1010
     * @param s
1011
     * @return
1012
     * @throws Exception
1013
     */
1014
    public Date parseDate(String s)
1015
      throws Exception
1016
    {
1017
        Date d = null;
1018
        int tIndex = s.indexOf("T");
1019
        int zIndex = s.indexOf("Z");
1020
        if(tIndex != -1 && zIndex != -1)
1021
        { //parse a date that looks like 2010-05-18T21:12:54.362Z
1022
            //System.out.println("original date: " + s);
1023
            
1024
            String date = s.substring(0, tIndex);
1025
            String year = date.substring(0, date.indexOf("-"));
1026
            String month = date.substring(date.indexOf("-") + 1, date.lastIndexOf("-"));
1027
            String day = date.substring(date.lastIndexOf("-") + 1, date.length());
1028
            /*System.out.println("date: " + "year: " + new Integer(year).intValue() + 
1029
                    " month: " + new Integer(month).intValue() + " day: " + 
1030
                    new Integer(day).intValue());
1031
            */
1032
            String time = s.substring(tIndex + 1, zIndex);
1033
            String hour = time.substring(0, time.indexOf(":"));
1034
            String minute = time.substring(time.indexOf(":") + 1, time.lastIndexOf(":"));
1035
            String seconds = "00";
1036
            String milliseconds = "00";
1037
            if(time.indexOf(".") != -1)
1038
            {
1039
                seconds = time.substring(time.lastIndexOf(":") + 1, time.indexOf("."));
1040
                milliseconds = time.substring(time.indexOf(".") + 1, time.length());
1041
            }
1042
            else
1043
            {
1044
                seconds = time.substring(time.lastIndexOf(":") + 1, time.length());
1045
            }
1046
            /*System.out.println("time: " + "hour: " + new Integer(hour).intValue() + 
1047
                    " minute: " + new Integer(minute).intValue() + " seconds: " + 
1048
                    new Integer(seconds).intValue() + " milli: " + 
1049
                    new Integer(milliseconds).intValue());*/
1050
            
1051
            //d = DateFormat.getDateTimeInstance().parse(date + " " + time);
1052
            Calendar c = Calendar.getInstance(/*TimeZone.getTimeZone("GMT-0")*/TimeZone.getDefault());
1053
            c.set(new Integer(year).intValue(), new Integer(month).intValue() - 1, 
1054
                  new Integer(day).intValue(), new Integer(hour).intValue(), 
1055
                  new Integer(minute).intValue(), new Integer(seconds).intValue());
1056
            c.set(Calendar.MILLISECOND, new Integer(milliseconds).intValue());
1057
            d = new Date(c.getTimeInMillis());
1058
            //System.out.println("d: " + d);
1059
            return d;
1060
        }
1061
        else
1062
        {  //if it's not in the expected format, try the formatter
1063
            return DateFormat.getDateTimeInstance().parse(s);
1064
        }
1065
    }
1066

    
1067
    /*
1068
     * Look up the information on the session using the token provided in
1069
     * the AuthToken.  The Session should have all relevant user information.
1070
     * If the session has expired or is invalid, the 'public' session will
1071
     * be returned, giving the user anonymous access.
1072
     */
1073
    public static SessionData getSessionData(AuthToken token) {
1074
        SessionData sessionData = null;
1075
        String sessionId = "PUBLIC";
1076
        if (token != null) {
1077
            sessionId = token.getToken();
1078
        }
1079
        
1080
        // if the session id is registered in SessionService, get the
1081
        // SessionData for it. Otherwise, use the public session.
1082
        //System.out.println("sessionid: " + sessionId);
1083
        if (sessionId != null &&
1084
            !sessionId.toLowerCase().equals("public") &&
1085
            SessionService.getInstance().isSessionRegistered(sessionId)) 
1086
        {
1087
            sessionData = SessionService.getInstance().getRegisteredSession(sessionId);
1088
        } else {
1089
            sessionData = SessionService.getInstance().getPublicSession();
1090
        }
1091
        
1092
        return sessionData;
1093
    }
1094

    
1095
    /** 
1096
     * Determine if a given object should be treated as an XML science metadata
1097
     * object. 
1098
     * 
1099
     * TODO: This test should be externalized in a configuration dictionary rather than being hardcoded.
1100
     * 
1101
     * @param sysmeta the SystemMetadata describig the object
1102
     * @return true if the object should be treated as science metadata
1103
     */
1104
    private boolean isScienceMetadata(SystemMetadata sysmeta) {
1105
        boolean scimeta = false;
1106
        switch (sysmeta.getObjectFormat()) {
1107
            case EML_2_1_0: scimeta = true; break;
1108
            case EML_2_0_1: scimeta = true; break;
1109
            case EML_2_0_0: scimeta = true; break;
1110
            case FGDC_STD_001_1_1999: scimeta = true; break;
1111
            case FGDC_STD_001_1998: scimeta = true; break;
1112
            case NCML_2_2: scimeta = true; break;
1113
        }
1114
        
1115
        return scimeta;
1116
    }
1117

    
1118
    /**
1119
     * insert a data doc
1120
     * @param object
1121
     * @param guid
1122
     * @param sessionData
1123
     * @throws ServiceFailure
1124
     */
1125
    private void insertDataObject(InputStream object, Identifier guid, 
1126
            SessionData sessionData) throws ServiceFailure {
1127
        
1128
        String username = sessionData.getUserName();
1129
        String[] groups = sessionData.getGroupNames();
1130

    
1131
        // generate guid/localId pair for object
1132
        logMetacat.debug("Generating a guid/localId mapping");
1133
        IdentifierManager im = IdentifierManager.getInstance();
1134
        String localId = im.generateLocalId(guid.getValue(), 1);
1135

    
1136
        try {
1137
            logMetacat.debug("Case DATA: starting to write to disk.");
1138
            if (DocumentImpl.getDataFileLockGrant(localId)) {
1139
    
1140
                // Save the data file to disk using "localId" as the name
1141
                try {
1142
                    String datafilepath = PropertyService.getProperty("application.datafilepath");
1143
    
1144
                    File dataDirectory = new File(datafilepath);
1145
                    dataDirectory.mkdirs();
1146
    
1147
                    File newFile = writeStreamToFile(dataDirectory, localId, object);
1148
    
1149
                    // TODO: Check that the file size matches SystemMetadata
1150
                    //                        long size = newFile.length();
1151
                    //                        if (size == 0) {
1152
                    //                            throw new IOException("Uploaded file is 0 bytes!");
1153
                    //                        }
1154
    
1155
                    // Register the file in the database (which generates an exception
1156
                    // if the localId is not acceptable or other untoward things happen
1157
                    try {
1158
                        logMetacat.debug("Registering document...");
1159
                        DocumentImpl.registerDocument(localId, "BIN", localId,
1160
                                username, groups);
1161
                        logMetacat.debug("Registration step completed.");
1162
                    } catch (SQLException e) {
1163
                        //newFile.delete();
1164
                        logMetacat.debug("SQLE: " + e.getMessage());
1165
                        e.printStackTrace(System.out);
1166
                        throw new ServiceFailure("1190", "Registration failed: " + e.getMessage());
1167
                    } catch (AccessionNumberException e) {
1168
                        //newFile.delete();
1169
                        logMetacat.debug("ANE: " + e.getMessage());
1170
                        e.printStackTrace(System.out);
1171
                        throw new ServiceFailure("1190", "Registration failed: " + e.getMessage());
1172
                    } catch (Exception e) {
1173
                        //newFile.delete();
1174
                        logMetacat.debug("Exception: " + e.getMessage());
1175
                        e.printStackTrace(System.out);
1176
                        throw new ServiceFailure("1190", "Registration failed: " + e.getMessage());
1177
                    }
1178
    
1179
                    logMetacat.debug("Logging the creation event.");
1180
                    EventLog.getInstance().log(metacatUrl,
1181
                            username, localId, "create");
1182
    
1183
                    // Schedule replication for this data file
1184
                    logMetacat.debug("Scheduling replication.");
1185
                    ForceReplicationHandler frh = new ForceReplicationHandler(
1186
                            localId, "create", false, null);
1187
    
1188
                } catch (PropertyNotFoundException e) {
1189
                    throw new ServiceFailure("1190", "Could not lock file for writing:" + e.getMessage());
1190
                }
1191
    
1192
            }
1193
        } catch (Exception e) {
1194
            // Could not get a lock on the document, so we can not update the file now
1195
            throw new ServiceFailure("1190", "Failed to lock file: " + e.getMessage());
1196
        }
1197
    }
1198

    
1199
    /**
1200
     * write a file to a stream
1201
     * @param dir
1202
     * @param fileName
1203
     * @param data
1204
     * @return
1205
     * @throws ServiceFailure
1206
     */
1207
    private File writeStreamToFile(File dir, String fileName, InputStream data) 
1208
        throws ServiceFailure {
1209
        
1210
        File newFile = new File(dir, fileName);
1211
        logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
1212

    
1213
        try {
1214
            if (newFile.createNewFile()) {
1215
                // write data stream to desired file
1216
                OutputStream os = new FileOutputStream(newFile);
1217
                long length = IOUtils.copyLarge(data, os);
1218
                os.flush();
1219
                os.close();
1220
            } else {
1221
                logMetacat.debug("File creation failed, or file already exists.");
1222
                throw new ServiceFailure("1190", "File already exists: " + fileName);
1223
            }
1224
        } catch (FileNotFoundException e) {
1225
            logMetacat.debug("FNF: " + e.getMessage());
1226
            throw new ServiceFailure("1190", "File not found: " + fileName + " " 
1227
                    + e.getMessage());
1228
        } catch (IOException e) {
1229
            logMetacat.debug("IOE: " + e.getMessage());
1230
            throw new ServiceFailure("1190", "File was not written: " + fileName 
1231
                    + " " + e.getMessage());
1232
        }
1233

    
1234
        return newFile;
1235
    }
1236
    
1237
    private static Date getDateInTimeZone(Date currentDate, String timeZoneId)
1238
    {
1239
        TimeZone tz = TimeZone.getTimeZone(timeZoneId);
1240
        Calendar mbCal = new GregorianCalendar(TimeZone.getTimeZone(timeZoneId));
1241
        mbCal.setTimeInMillis(currentDate.getTime());
1242

    
1243
        Calendar cal = Calendar.getInstance();
1244
        cal.set(Calendar.YEAR, mbCal.get(Calendar.YEAR));
1245
        cal.set(Calendar.MONTH, mbCal.get(Calendar.MONTH));
1246
        cal.set(Calendar.DAY_OF_MONTH, mbCal.get(Calendar.DAY_OF_MONTH));
1247
        cal.set(Calendar.HOUR_OF_DAY, mbCal.get(Calendar.HOUR_OF_DAY));
1248
        cal.set(Calendar.MINUTE, mbCal.get(Calendar.MINUTE));
1249
        cal.set(Calendar.SECOND, mbCal.get(Calendar.SECOND));
1250
        cal.set(Calendar.MILLISECOND, mbCal.get(Calendar.MILLISECOND));
1251

    
1252
        return cal.getTime();
1253
    }
1254

    
1255
    /**
1256
     * insert a systemMetadata doc
1257
     */
1258
    private void insertSystemMetadata(SystemMetadata sysmeta, SessionData sessionData) 
1259
        throws ServiceFailure 
1260
    {
1261
        logMetacat.debug("Starting to insert SystemMetadata...");
1262
    
1263
        // generate guid/localId pair for sysmeta
1264
        Identifier sysMetaGuid = new Identifier();
1265
        sysMetaGuid.setValue(DocumentUtil.generateDocumentId(1));
1266
        sysmeta.setDateSysMetadataModified(new Date());
1267
        System.out.println("****inserting new system metadata with modified date " + sysmeta.getDateSysMetadataModified());
1268

    
1269
        String xml = new String(serializeSystemMetadata(sysmeta).toByteArray());
1270
        String localId = insertDocument(xml, sysMetaGuid, sessionData);
1271
        System.out.println("sysmeta inserted with localId " + localId);
1272
        //insert the system metadata doc id into the identifiers table to 
1273
        //link it to the data or metadata document
1274
        IdentifierManager.getInstance().createSystemMetadataMapping(
1275
                sysmeta.getIdentifier().getValue(), sysMetaGuid.getValue());
1276
    }
1277
    
1278
    /**
1279
     * update a systemMetadata doc
1280
     */
1281
    private void updateSystemMetadata(SystemMetadata sm, SessionData sessionData)
1282
      throws ServiceFailure
1283
    {
1284
        try
1285
        {
1286
            String smId = IdentifierManager.getInstance().getSystemMetadataLocalId(sm.getIdentifier().getValue());
1287
            sm.setDateSysMetadataModified(new Date());
1288
            String xml = new String(serializeSystemMetadata(sm).toByteArray());
1289
            Identifier id = new Identifier();
1290
            id.setValue(smId);
1291
            String localId = updateDocument(xml, id, null, sessionData);
1292
            IdentifierManager.getInstance().updateSystemMetadataMapping(sm.getIdentifier().getValue(), localId);
1293
        }
1294
        catch(Exception e)
1295
        {
1296
            throw new ServiceFailure("1030", "Error updating system metadata: " + e.getMessage());
1297
        }
1298
    }
1299
    
1300
    /**
1301
     * insert a document
1302
     * NOTE: this method shouldn't be used from the update or create() methods.  
1303
     * we shouldn't be putting the science metadata or data objects into memory.
1304
     */
1305
    private String insertDocument(String xml, Identifier guid, SessionData sessionData)
1306
        throws ServiceFailure
1307
    {
1308
        return insertOrUpdateDocument(xml, guid, sessionData, "insert");
1309
    }
1310
    
1311
    /**
1312
     * insert a document from a stream
1313
     */
1314
    private String insertDocument(InputStream is, Identifier guid, SessionData sessionData)
1315
      throws IOException, ServiceFailure
1316
    {
1317
        //HACK: change this eventually.  we should not be converting the stream to a string
1318
        String xml = IOUtils.toString(is);
1319
        return insertDocument(xml, guid, sessionData);
1320
    }
1321
    
1322
    /**
1323
     * update a document
1324
     * NOTE: this method shouldn't be used from the update or create() methods.  
1325
     * we shouldn't be putting the science metadata or data objects into memory.
1326
     */
1327
    private String updateDocument(String xml, Identifier obsoleteGuid, Identifier guid, SessionData sessionData)
1328
        throws ServiceFailure
1329
    {
1330
        return insertOrUpdateDocument(xml, obsoleteGuid, sessionData, "update");
1331
    }
1332
    
1333
    /**
1334
     * update a document from a stream
1335
     */
1336
    private String updateDocument(InputStream is, Identifier obsoleteGuid, Identifier guid, SessionData sessionData)
1337
      throws IOException, ServiceFailure
1338
    {
1339
        //HACK: change this eventually.  we should not be converting the stream to a string
1340
        String xml = IOUtils.toString(is);
1341
        String localId = updateDocument(xml, obsoleteGuid, guid, sessionData);
1342
        IdentifierManager im = IdentifierManager.getInstance();
1343
        if(guid != null)
1344
        {
1345
          im.createMapping(guid.getValue(), localId);
1346
        }
1347
        return localId;
1348
    }
1349
    
1350
    /**
1351
     * insert a document, return the id of the document that was inserted
1352
     */
1353
    protected String insertOrUpdateDocument(String xml, Identifier guid, SessionData sessionData, String insertOrUpdate) 
1354
        throws ServiceFailure {
1355
        logMetacat.debug("Starting to insert xml document...");
1356
        IdentifierManager im = IdentifierManager.getInstance();
1357

    
1358
        // generate guid/localId pair for sysmeta
1359
        String localId = null;
1360
        if(insertOrUpdate.equals("insert"))
1361
        {
1362
            localId = im.generateLocalId(guid.getValue(), 1);
1363
        }
1364
        else
1365
        {
1366
            //localid should already exist in the identifier table, so just find it
1367
            try
1368
            {
1369
                localId = im.getLocalId(guid.getValue());
1370
                //increment the revision
1371
                String docid = localId.substring(0, localId.lastIndexOf("."));
1372
                String revS = localId.substring(localId.lastIndexOf(".") + 1, localId.length());
1373
                int rev = new Integer(revS).intValue();
1374
                rev++;
1375
                docid = docid + "." + rev;
1376
                localId = docid;
1377
            }
1378
            catch(McdbDocNotFoundException e)
1379
            {
1380
                throw new ServiceFailure("1030", "CrudService.insertOrUpdateDocument(): " +
1381
                    "guid " + guid.getValue() + " should have been in the identifier table, but it wasn't: " + e.getMessage());
1382
            }
1383
        }
1384
        logMetacat.debug("Metadata guid|localId: " + guid.getValue() + "|" +
1385
                localId);
1386

    
1387
        String[] action = new String[1];
1388
        action[0] = insertOrUpdate;
1389
        params.put("action", action);
1390
        String[] docid = new String[1];
1391
        docid[0] = localId;
1392
        params.put("docid", docid);
1393
        String[] doctext = new String[1];
1394
        doctext[0] = xml;
1395
        logMetacat.debug(doctext[0]);
1396
        params.put("doctext", doctext);
1397
        
1398
        // TODO: refactor handleInsertOrUpdateAction() to not output XML directly
1399
        // onto output stream, or alternatively, capture that and parse it to 
1400
        // generate the right exceptions
1401
        //ByteArrayOutputStream output = new ByteArrayOutputStream();
1402
        //PrintWriter pw = new PrintWriter(output);
1403
        String result = handler.handleInsertOrUpdateAction(metacatUrl, null, 
1404
                            null, params, sessionData.getUserName(), sessionData.getGroupNames());
1405
        //String outputS = new String(output.toByteArray());
1406
        logMetacat.debug("CrudService.insertDocument - Metacat returned: " + result);
1407
        logMetacat.debug("Finsished inserting xml document with id " + localId);
1408
        return localId;
1409
    }
1410
    
1411
    /**
1412
     * serialize a system metadata doc
1413
     * @param sysmeta
1414
     * @return
1415
     * @throws ServiceFailure
1416
     */
1417
    public static ByteArrayOutputStream serializeSystemMetadata(SystemMetadata sysmeta) 
1418
        throws ServiceFailure {
1419
        IBindingFactory bfact;
1420
        ByteArrayOutputStream sysmetaOut = null;
1421
        try {
1422
            bfact = BindingDirectory.getFactory(SystemMetadata.class);
1423
            IMarshallingContext mctx = bfact.createMarshallingContext();
1424
            sysmetaOut = new ByteArrayOutputStream();
1425
            mctx.marshalDocument(sysmeta, "UTF-8", null, sysmetaOut);
1426
        } catch (JiBXException e) {
1427
            e.printStackTrace();
1428
            throw new ServiceFailure("1190", "Failed to serialize and insert SystemMetadata: " + e.getMessage());
1429
        }
1430
        
1431
        return sysmetaOut;
1432
    }
1433
    
1434
    /**
1435
     * deserialize a system metadata doc
1436
     * @param xml
1437
     * @return
1438
     * @throws ServiceFailure
1439
     */
1440
    public static SystemMetadata deserializeSystemMetadata(InputStream xml) 
1441
        throws ServiceFailure {
1442
        try {
1443
            IBindingFactory bfact = BindingDirectory.getFactory(SystemMetadata.class);
1444
            IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
1445
            SystemMetadata sysmeta = (SystemMetadata) uctx.unmarshalDocument(xml, null);
1446
            return sysmeta;
1447
        } catch (JiBXException e) {
1448
            e.printStackTrace();
1449
            throw new ServiceFailure("1190", "Failed to deserialize and insert SystemMetadata: " + e.getMessage());
1450
        }    
1451
    }
1452
    
1453
    /**
1454
     * produce an md5 checksum for item
1455
     */
1456
    private String checksum(InputStream is)
1457
      throws Exception
1458
    {        
1459
        byte[] buffer = new byte[1024];
1460
        MessageDigest complete = MessageDigest.getInstance("MD5");
1461
        int numRead;
1462
        
1463
        do 
1464
        {
1465
          numRead = is.read(buffer);
1466
          if (numRead > 0) 
1467
          {
1468
            complete.update(buffer, 0, numRead);
1469
          }
1470
        } while (numRead != -1);
1471
        
1472
        
1473
        return getHex(complete.digest());
1474
    }
1475
    
1476
    /**
1477
     * convert a byte array to a hex string
1478
     */
1479
    private static String getHex( byte [] raw ) 
1480
    {
1481
        final String HEXES = "0123456789ABCDEF";
1482
        if ( raw == null ) {
1483
          return null;
1484
        }
1485
        final StringBuilder hex = new StringBuilder( 2 * raw.length );
1486
        for ( final byte b : raw ) {
1487
          hex.append(HEXES.charAt((b & 0xF0) >> 4))
1488
             .append(HEXES.charAt((b & 0x0F)));
1489
        }
1490
        return hex.toString();
1491
    }
1492
    
1493
    /**
1494
     * parse the metacat date which looks like 2010-06-08 (YYYY-MM-DD) into
1495
     * a proper date object
1496
     * @param date
1497
     * @return
1498
     */
1499
    private Date parseMetacatDate(String date)
1500
    {
1501
        String year = date.substring(0, 4);
1502
        String month = date.substring(5, 7);
1503
        String day = date.substring(8, 10);
1504
        Calendar c = Calendar.getInstance(TimeZone.getDefault());
1505
        c.set(new Integer(year).intValue(), 
1506
              new Integer(month).intValue(), 
1507
              new Integer(day).intValue());
1508
        System.out.println("time in parseMetacatDate: " + c.getTime());
1509
        return c.getTime();
1510
    }
1511
    
1512
    /**
1513
     * find the size (in bytes) of a stream
1514
     * @param is
1515
     * @return
1516
     * @throws IOException
1517
     */
1518
    private long sizeOfStream(InputStream is)
1519
        throws IOException
1520
    {
1521
        long size = 0;
1522
        byte[] b = new byte[1024];
1523
        int numread = is.read(b, 0, 1024);
1524
        while(numread != -1)
1525
        {
1526
            size += numread;
1527
            numread = is.read(b, 0, 1024);
1528
        }
1529
        return size;
1530
    }
1531
    
1532
    /**
1533
     * create system metadata with a specified id, doc and format
1534
     */
1535
    private SystemMetadata createSystemMetadata(String localId, AuthToken token)
1536
      throws Exception
1537
    {
1538
        IdentifierManager im = IdentifierManager.getInstance();
1539
        Hashtable<String, String> docInfo = im.getDocumentInfo(localId);
1540
        
1541
        //get the document text
1542
        int rev = im.getLatestRevForLocalId(localId);
1543
        Identifier identifier = new Identifier();
1544
        identifier.setValue(im.getGUID(localId, rev));
1545
        InputStream is = this.get(token, identifier);
1546
        
1547
        SystemMetadata sm = new SystemMetadata();
1548
        //set the id
1549
        sm.setIdentifier(identifier);
1550
        
1551
        //set the object format
1552
        String doctype = docInfo.get("doctype");
1553
        ObjectFormat format = ObjectFormat.convert(docInfo.get("doctype"));
1554
        if(format == null)
1555
        {
1556
            if(doctype.trim().equals("BIN"))
1557
            {
1558
                format = ObjectFormat.APPLICATIONOCTETSTREAM;
1559
            }
1560
            else
1561
            {
1562
                format = ObjectFormat.convert("text/plain");
1563
            }
1564
        }
1565
        sm.setObjectFormat(format);
1566
        
1567
        //create the checksum
1568
        String checksumS = checksum(is);
1569
        ChecksumAlgorithm ca = ChecksumAlgorithm.convert("MD5");
1570
        Checksum checksum = new Checksum();
1571
        checksum.setValue(checksumS);
1572
        checksum.setAlgorithm(ca);
1573
        sm.setChecksum(checksum);
1574
        
1575
        //set the size
1576
        is = this.get(token, identifier);
1577
        sm.setSize(sizeOfStream(is));
1578
        
1579
        //submitter
1580
        Principal p = new Principal();
1581
        p.setValue(docInfo.get("user_owner"));
1582
        sm.setSubmitter(p);
1583
        sm.setRightsHolder(p);
1584
        try
1585
        {
1586
            Date dateCreated = parseMetacatDate(docInfo.get("date_created"));
1587
            sm.setDateUploaded(dateCreated);
1588
            Date dateUpdated = parseMetacatDate(docInfo.get("date_updated"));
1589
            sm.setDateSysMetadataModified(dateUpdated);
1590
        }
1591
        catch(Exception e)
1592
        {
1593
            System.out.println("couldn't parse a date: " + e.getMessage());
1594
            Date dateCreated = new Date();
1595
            sm.setDateUploaded(dateCreated);
1596
            Date dateUpdated = new Date();
1597
            sm.setDateSysMetadataModified(dateUpdated);
1598
        }
1599
        NodeReference nr = new NodeReference();
1600
        nr.setValue("metacat");
1601
        sm.setOriginMemberNode(nr);
1602
        sm.setAuthoritativeMemberNode(nr);
1603
        return sm;
1604
    }
1605
}
(1-1/2)