Project

General

Profile

Revision 5353

Added by berkley over 11 years ago

implemented update in ResourceHandler and fixed a bug in CrudService where data files were not handled correctly on update

View differences:

src/edu/ucsb/nceas/metacat/restservice/ResourceHandler.java
50 50
import org.dataone.service.exceptions.NotImplemented;
51 51
import org.dataone.service.exceptions.ServiceFailure;
52 52
import org.dataone.service.exceptions.UnsupportedType;
53
import org.dataone.service.exceptions.NotFound;
53 54
import org.dataone.service.types.AuthToken;
54 55
import org.dataone.service.types.Identifier;
55 56
import org.dataone.service.types.SystemMetadata;
......
240 241
        while (paramlist.hasMoreElements()) {
241 242
            name = (String) paramlist.nextElement();
242 243
            value = request.getParameterValues(name);
244
            System.out.println("adding param: " + name + " = " + value);
243 245
            params.put(name, value);
244 246
        }
245 247

  
......
609 611
     */
610 612
    private void putObject(String guid, String action) {
611 613
        logMetacat.debug("Entering putObject: " + guid + "/" + action);
614
        System.out.println("entering putObject.  Action is " + action);
612 615
        
613 616
        // TODO: This function lacks proper handling of authz and authn, so it
614 617
        // seems that anyone can insert or update; interacts with 
......
623 626
            logMetacat.error("Could not get the output stream for writing in putObject");
624 627
        }
625 628
        try {
626
            if (action.equals(FUNCTION_NAME_UPDATE)
627
                    || action.equals(FUNCTION_NAME_INSERT)) {
629
            
630
            // Read the incoming data from its Mime Multipart encoding
631
            logMetacat.debug("Disassembling MIME multipart form");
632
            InputStream object = null;
633
            InputStream sysmeta = null;
634
            MimeMultipart mmp = new MimeMultipart(new InputStreamDataSource("message", request.getInputStream()));
635
            logMetacat.debug("MMP created.");
636
            mmp.writeTo(System.out);
637
            for (int i = 0; i < mmp.getCount(); i++) {
638
                logMetacat.debug("Looping over MMP parts: " + i);
639
                BodyPart part = mmp.getBodyPart(i);
640
                String name = part.getFileName();
641
                logMetacat.debug("Part name is: " + name);
642
                System.out.println("part name: " + name);
643
                logMetacat.debug("Part has class name: " + part.getClass().getName());
644
                if (name.equals("object")) {
645
                    object = part.getInputStream();
646
                    logMetacat.debug("Found object part, size is: " + part.getSize());
647
                } else if (name.equals("systemmetadata")) {
648
                    sysmeta = part.getInputStream();
649
                    logMetacat.debug("Found sysmeta part, size is: " + part.getSize());
650
                } else {
651
                    throw new InvalidRequest(1000, "Request had malformed MIME part with name: " + name);
652
                }
653
            }
654
            
655
            if ( action.equals(FUNCTION_NAME_INSERT)) { //handle inserts
628 656

  
629 657
                // Check if the objectId exists
630 658
                IdentifierManager im = IdentifierManager.getInstance();
......
632 660
                    throw new IdentifierNotUnique(1000, "Identifier is already in use: " + guid);
633 661
                }
634 662

  
635
                // TODO: For updates, need to check if the old id exists, and if not throw an exception
636

  
637
                // Read the incoming data from its Mime Multipart encoding
638
                logMetacat.debug("Disassembling MIME multipart form");
639
                InputStream object = null;
640
                InputStream sysmeta = null;
641
                MimeMultipart mmp = new MimeMultipart(new InputStreamDataSource("message", request.getInputStream()));
642
                logMetacat.debug("MMP created.");
643
                mmp.writeTo(System.out);
644
                for (int i = 0; i < mmp.getCount(); i++) {
645
                    logMetacat.debug("Looping over MMP parts: " + i);
646
                    BodyPart part = mmp.getBodyPart(i);
647
                    String name = part.getFileName();
648
                    logMetacat.debug("Part name is: " + name);
649
                    logMetacat.debug("Part has class name: " + part.getClass().getName());
650
                    if (name.equals("object")) {
651
                        object = part.getInputStream();
652
                        logMetacat.debug("Found object part, size is: " + part.getSize());
653
                    } else if (name.equals("systemmetadata")) {
654
                        sysmeta = part.getInputStream();
655
                        logMetacat.debug("Found sysmeta part, size is: " + part.getSize());
656
                    } else {
657
                        throw new InvalidRequest(1000, "Request had malformed MIME part with name: " + name);
658
                    }
659
                }
660

  
661 663
                // TODO: access control -- should be in CrudService et al. I think
662 664
                //if (username != null && !username.equals("public")) {
663 665
                if (username != null) {
......
679 681
                    logMetacat.debug("Unauthorized to create.");
680 682
                    throw new NotAuthorized(1000, "Permission denied for user " + username);
681 683
                }
684
            } else if (action.equals(FUNCTION_NAME_UPDATE)) { //handle updates
685
                IdentifierManager im = IdentifierManager.getInstance();
686
                CrudService cs = CrudService.getInstance();
687
                Identifier obsoletedGuid = new Identifier();
688
                Identifier id = new Identifier();
689
                id.setValue(guid);
690
                AuthToken token = null;
691
                
692
                //do some checks
693
                if(params.get("obsoletedGuid") == null)
694
                {
695
                    throw new InvalidRequest(1202, "obsoletedGuid must be contained in the request parameters.");
696
                }
697
                //get the obsoletedGuid
698
                String[] obsGuidS = params.get("obsoletedGuid");
699
                obsoletedGuid.setValue(obsGuidS[0]);
700
                
701
                if (!im.identifierExists(obsoletedGuid.getValue())) 
702
                {
703
                    throw new InvalidRequest(1202, "The guid you are trying to update does not exist.");
704
                }
705
                
706
                
707
                logMetacat.debug("Commence update...");
708
                
709
                //get the systemmetadata
710
                IBindingFactory bfact =
711
                        BindingDirectory.getFactory(SystemMetadata.class);
712
                    IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
713
                    SystemMetadata m = (SystemMetadata) uctx.unmarshalDocument(sysmeta, null);
714
                
715
                //do the update
716
                try
717
                {
718
                    cs.setParamsFromRequest(request);
719
                    Identifier rId = cs.update(token, id, object, obsoletedGuid, m);
720
                    System.out.println("id returned from update " + rId.getValue());
721
                }
722
                catch(NotFound e)
723
                {
724
                    throw new InvalidRequest(1202, "The guid you are trying to update does not exist.");
725
                }
726
                
682 727
            } else {
683 728
                throw new InvalidRequest(1000, "Operation must be create or update.");
684 729
            }
src/edu/ucsb/nceas/metacat/dataone/CrudService.java
232 232
        logMetacat.debug("Returning from CrudService.create()");
233 233
        return guid;
234 234
    }
235
    
236
    /**
237
     * update an existing object with a new object.  Change the system metadata
238
     * to reflect the changes and update it as well.
239
     */
240
    public Identifier update(AuthToken token, Identifier guid, 
241
            InputStream object, Identifier obsoletedGuid, SystemMetadata sysmeta) 
242
            throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, 
243
            UnsupportedType, InsufficientResources, NotFound, InvalidSystemMetadata, 
244
            NotImplemented {
245
        try
246
        {
247
            SessionData sessionData = getSessionData(token);
248
            
249
            //find the old systemmetadata (sm.old) document id (the one linked to obsoletedGuid)
250
            SystemMetadata sm = getSystemMetadata(token, obsoletedGuid);
251
            //change sm.old's obsoletedBy field 
252
            List l = sm.getObsoletedByList();
253
            l.add(guid);
254
            sm.setObsoletedByList(l);
255
            //update sm.old
256
            updateSystemMetadata(sm, sessionData);
257
            
258
            //change the obsoletes field of the new systemMetadata (sm.new) to point to the id of the old one
259
            sysmeta.addObsolete(obsoletedGuid);
260
            //insert sm.new
261
            insertSystemMetadata(sysmeta, sessionData);
262
            
263
            boolean isScienceMetadata = isScienceMetadata(sysmeta);
264
            if(isScienceMetadata)
265
            {
266
                //update the doc
267
                updateDocument(object, obsoletedGuid, guid, sessionData);
268
            }
269
            else
270
            {
271
                //update a data file, not xml
272
                insertDataObject(object, guid, sessionData);
273
            }
274
            return guid;
275
        }
276
        catch(Exception e)
277
        {
278
            throw new ServiceFailure(1030, "Error updating document in CrudService: " + e.getMessage());
279
        }
280
    }
235 281

  
236 282
    public Identifier delete(AuthToken token, Identifier guid)
237 283
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
......
370 416
        }                
371 417
    }
372 418

  
373
    /**
374
     * update an existing object with a new object.  Change the system metadata
375
     * to reflect the changes and update it as well.
376
     */
377
    public Identifier update(AuthToken token, Identifier guid, 
378
            InputStream object, Identifier obsoletedGuid, SystemMetadata sysmeta) 
379
            throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, 
380
            UnsupportedType, InsufficientResources, NotFound, InvalidSystemMetadata, 
381
            NotImplemented {
382
        try
383
        {
384
            SessionData sessionData = getSessionData(token);
385
            
386
            //find the old systemmetadata (sm.old) document id (the one linked to obsoletedGuid)
387
            SystemMetadata sm = getSystemMetadata(token, obsoletedGuid);
388
            //change sm.old's obsoletedBy field 
389
            List l = sm.getObsoletedByList();
390
            l.add(guid);
391
            sm.setObsoletedByList(l);
392
            //update sm.old
393
            updateSystemMetadata(sm, sessionData);
394
            
395
            //change the obsoletes field of the new systemMetadata (sm.new) to point to the id of the old one
396
            sysmeta.addObsolete(obsoletedGuid);
397
            //insert sm.new
398
            insertSystemMetadata(sysmeta, sessionData);
399
            
400
            //update the doc
401
            updateDocument(object, obsoletedGuid, guid, sessionData);
402
            return guid;
403
        }
404
        catch(Exception e)
405
        {
406
            throw new ServiceFailure(1030, "Error updating document in CrudService: " + e.getMessage());
407
        }
408
    }
409

  
410 419
    /*
411 420
     * Look up the information on the session using the token provided in
412 421
     * the AuthToken.  The Session should have all relevant user information.
......
488 497
                    // if the localId is not acceptable or other untoward things happen
489 498
                    try {
490 499
                        logMetacat.debug("Registering document...");
500
                        System.out.println("inserting data object: localId: " + localId);
491 501
                        DocumentImpl.registerDocument(localId, "BIN", localId,
492 502
                                username, groups);
493 503
                        logMetacat.debug("Registration step completed.");

Also available in: Unified diff