Project

General

Profile

« Previous | Next » 

Revision 6270

throw exceptions up the call stack rather than catching and handling them differently for each possible rest path

View differences:

src/edu/ucsb/nceas/metacat/restservice/CNResourceHandler.java
29 29
import java.io.InputStream;
30 30
import java.io.OutputStream;
31 31
import java.io.PrintWriter;
32
import java.io.UnsupportedEncodingException;
32 33
import java.util.Date;
33 34
import java.util.Hashtable;
34 35
import java.util.Map;
......
157 158

  
158 159
                if (resource.equals(RESOURCE_ACCESS_RULES) && httpVerb == PUT) {
159 160
                    logMetacat.debug("Setting access policy");
160
                    setaccess();
161
                    setAccess();
161 162
                    status = true;
162 163
                    logMetacat.debug("done setting access");
163 164
                    
......
299 300
                    if (httpVerb == GET) {
300 301
                    
301 302
                        String guid = request.getPathInfo();
302
                        if (guid != null && guid.length() > 1)
303
                        if (guid != null && guid.length() > 1) {
303 304
                            guid = request.getPathInfo().substring(1); //trim the slash
304
                        
305
                        Identifier guidid = new Identifier();
306
                        guidid.setValue(guid);
307
                        logMetacat.debug("getting checksum for object " + guid);
308
                        try {
309
                            Checksum c = CNodeService.getInstance().getChecksum(session, guidid);
310
                            logMetacat.debug("got checksum " + c.getValue());
311
                            response.setStatus(200);
312
                            logMetacat.debug("serializing response");
313
                            serializeServiceType(Checksum.class, c, response.getOutputStream());
314
                            logMetacat.debug("done serializing response.");
315 305
                        }
316
                        catch(NotAuthorized na)
317
                        {
318
                            na.setDetail_code("1400");
319
                            serializeException(na, response.getOutputStream());
320
                        }
321
                        catch(NotFound nf)
322
                        {
323
                            nf.setDetail_code("1420");
324
                            serializeException(nf, response.getOutputStream());
325
                        }
326
                        catch(InvalidRequest ir)
327
                        {
328
                            ir.setDetail_code("1402");
329
                            serializeException(ir, response.getOutputStream());
330
                        }
331
                        catch(ServiceFailure sf)
332
                        {
333
                            sf.setDetail_code("1410");
334
                            serializeException(sf, response.getOutputStream());
335
                        }
336
                        catch(InvalidToken it)
337
                        {
338
                            it.setDetail_code("1430");
339
                            serializeException(it, response.getOutputStream());
340
                        }
306
                        checksum(guid);
341 307
                        status = true;
342 308
                    }
343 309
                } 
344 310
                    
345 311
                if (!status) {
346
                    response.setStatus(400);
347
                    printError("Incorrect parameters!", response);
312
                	throw new ServiceFailure("0000", "Unknown error, status=" + status);
348 313
                }
349 314
            } else {
350
                response.setStatus(400);
351
                printError("Incorrect resource!", response);
315
            	throw new InvalidRequest("0000", "No resource matched for " + resource);
352 316
            }
353 317
        } catch (BaseException be) {
354
        	// report Exceptions as clearly as possible
318
        	// report Exceptions as clearly and generically as possible
355 319
        	OutputStream out = null;
356 320
			try {
357 321
				out = response.getOutputStream();
358
			} catch (IOException e) {
359
				logMetacat.error("Could not get output stream from response", e);
322
			} catch (IOException ioe) {
323
				logMetacat.error("Could not get output stream from response", ioe);
360 324
			}
361 325
            serializeException(be, out);
362 326
        } catch (Exception e) {
327
            // report Exceptions as clearly and generically as possible
363 328
            logMetacat.error(e.getClass() + ": " + e.getMessage(), e);
329
        	OutputStream out = null;
330
			try {
331
				out = response.getOutputStream();
332
			} catch (IOException ioe) {
333
				logMetacat.error("Could not get output stream from response", ioe);
334
			}
335
			ServiceFailure se = new ServiceFailure("0000", e.getMessage());
336
            serializeException(se, out);
364 337
        }
365 338
    }
366 339
    
340
    /**
341
     * Get the checksum for the given guid
342
     * 
343
     * @param guid
344
     * @throws NotImplemented 
345
     * @throws InvalidRequest 
346
     * @throws NotFound 
347
     * @throws NotAuthorized 
348
     * @throws ServiceFailure 
349
     * @throws InvalidToken 
350
     * @throws IOException 
351
     * @throws JiBXException 
352
     */
353
    private void checksum(String guid) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, InvalidRequest, NotImplemented, JiBXException, IOException {
354
    	Identifier guidid = new Identifier();
355
        guidid.setValue(guid);
356
        logMetacat.debug("getting checksum for object " + guid);
357
        Checksum c = CNodeService.getInstance().getChecksum(session, guidid);
358
        logMetacat.debug("got checksum " + c.getValue());
359
        response.setStatus(200);
360
        logMetacat.debug("serializing response");
361
        serializeServiceType(Checksum.class, c, response.getOutputStream());
362
        logMetacat.debug("done serializing response.");
363
        
364
    }
365
    
367 366
	/**
368 367
     * get the logs from the CrudService based on passed params.  Available 
369 368
     * params are token, fromDate, toDate, event.  See 
370 369
     * http://mule1.dataone.org/ArchitectureDocs/mn_api_crud.html#MN_crud.getLogRecords
371 370
     * for more info
371
	 * @throws NotImplemented 
372
	 * @throws InvalidRequest 
373
	 * @throws NotAuthorized 
374
	 * @throws ServiceFailure 
375
	 * @throws InvalidToken 
376
	 * @throws IOException 
377
	 * @throws JiBXException 
372 378
     */
373
    private void getLog()
379
    private void getLog() throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest, NotImplemented, IOException, JiBXException
374 380
    {
375
        OutputStream out = null;
376
        try
377
        {
378
            out = response.getOutputStream();
379
            response.setStatus(200);
380
            response.setContentType("text/xml");
381
            String fromDateS = params.get("fromDate")[0];
382
            logMetacat.debug("param fromDateS: " + fromDateS);
383
            Date fromDate = null;
384
            String toDateS = params.get("toDate")[0];
385
            logMetacat.debug("param toDateS: " + toDateS);
386
            Date toDate = null;
387
            String eventS = params.get("event")[0];
388
            Event event = null;
389
            if(fromDateS != null)
390
            {
391
                //fromDate = dateFormat.parse(fromDateS);
392
                fromDate = parseDateAndConvertToGMT(fromDateS);
393
            }
394
            if(toDateS != null)
395
            {
396
                //toDate = dateFormat.parse(toDateS);
397
                toDate = parseDateAndConvertToGMT(toDateS);
398
            }
399
            if(eventS != null)
400
            {
401
                event = Event.convert(eventS);
402
            }
403
            logMetacat.debug("fromDate: " + fromDate + " toDate: " + toDate);
404
            
405
            Integer start = null;
406
            Integer count = null;
407
            try {
408
            	start =  Integer.parseInt(params.get("start")[0]);
409
            } catch (Exception e) {
410
				logMetacat.warn("Could not parse start: " + e.getMessage());
411
			}
412
            try {
413
            	count =  Integer.parseInt(params.get("count")[0]);
414
            } catch (Exception e) {
415
				logMetacat.warn("Could not count start: " + e.getMessage());
416
			}
417
            
418
            logMetacat.debug("calling crudservice.getLogRecords");
419
            Log log = CNodeService.getInstance().getLogRecords(session, fromDate, toDate, event, start, count);
420
            serializeServiceType(Log.class, log, out);
381
        
382
        Date fromDate = null;
383
        Date toDate = null;
384
        Event event = null;
385
        Integer start = null;
386
        Integer count = null;
387
        
388
        try {
389
        	String fromDateS = params.get("fromDate")[0];
390
            System.out.println("param fromDateS: " + fromDateS);
391
            fromDate = parseDateAndConvertToGMT(fromDateS);
392
        } catch (Exception e) {
393
        	logMetacat.warn("Could not parse fromDate: " + e.getMessage());
421 394
        }
422
        catch(Exception e)
423
        {
424
            String msg = "Could not get logs from CrudService: " + e.getClass() + ": " + e.getMessage();
425
            response.setStatus(500);
426
            ServiceFailure sf = new ServiceFailure("1490", msg);
427
            logMetacat.error(msg);
428
            e.printStackTrace();
429
            serializeException(sf, out);
430
        }
395
        try {
396
        	String toDateS = params.get("toDate")[0];
397
            System.out.println("param toDateS: " + toDateS);
398
            toDate = parseDateAndConvertToGMT(toDateS);
399
        } catch (Exception e) {
400
        	logMetacat.warn("Could not parse toDate: " + e.getMessage());
401
		}
402
        try {
403
        	String eventS = params.get("event")[0];
404
            event = Event.convert(eventS);
405
        } catch (Exception e) {
406
        	logMetacat.warn("Could not parse event: " + e.getMessage());
407
		}
408
        System.out.println("fromDate: " + fromDate + " toDate: " + toDate);
409
        
410
        try {
411
        	start =  Integer.parseInt(params.get("start")[0]);
412
        } catch (Exception e) {
413
			logMetacat.warn("Could not parse start: " + e.getMessage());
414
		}
415
        try {
416
        	count =  Integer.parseInt(params.get("count")[0]);
417
        } catch (Exception e) {
418
			logMetacat.warn("Could not parse count: " + e.getMessage());
419
		}
420
        
421
        System.out.println("calling getLogRecords");
422
        Log log = CNodeService.getInstance().getLogRecords(session, fromDate, toDate, event, start, count);
423
        
424
        OutputStream out = response.getOutputStream();
425
        response.setStatus(200);
426
        response.setContentType("text/xml");
427
        
428
        serializeServiceType(Log.class, log, out);
429
  
431 430
    }
432 431

  
433 432
    /**
434 433
     * Implements REST version of DataONE CRUD API --> get
435 434
     * @param guid ID of data object to be read
435
     * @throws NotImplemented 
436
     * @throws InvalidRequest 
437
     * @throws NotFound 
438
     * @throws NotAuthorized 
439
     * @throws ServiceFailure 
440
     * @throws InvalidToken 
441
     * @throws IOException 
436 442
     */
437
    protected void getObject(String guid) {
438
        OutputStream out = null;
439
        try {
440
            out = response.getOutputStream();
441
            response.setStatus(200);
443
    protected void getObject(String guid) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, InvalidRequest, NotImplemented, IOException {
444

  
445
        
446
        if (guid != null) { //get a specific document                
447
            Identifier id = new Identifier();
448
            id.setValue(guid);
449
                
450
            SystemMetadata sm = CNodeService.getInstance().getSystemMetadata(session, id);
442 451
            
443
            if(guid != null)
444
            { //get a specific document                
445
                Identifier id = new Identifier();
446
                id.setValue(guid);
447
                try
448
                {
449
                    
450
                    SystemMetadata sm = CNodeService.getInstance().getSystemMetadata(session, id);
451
                    
452
                    //set the content type
453
                    if(sm.getObjectFormat().getFmtid().getValue().trim().equals(
454
                    		ObjectFormatCache.getInstance().getFormat("text/csv").getFmtid().getValue()))
455
                    {
456
                        response.setContentType("text/csv");
457
                        response.setHeader("Content-Disposition", "inline; filename=" + id.getValue() + ".csv");
458
                    }
459
                    else if(sm.getObjectFormat().getFmtid().getValue().trim().equals(
460
                    		ObjectFormatCache.getInstance().getFormat("text/plain").getFmtid().getValue()))
461
                    {
462
                        response.setContentType("text/plain");
463
                        response.setHeader("Content-Disposition", "inline; filename=" + id.getValue() + ".txt");
464
                    } 
465
                    else if(sm.getObjectFormat().getFmtid().getValue().trim().equals(
466
                    		ObjectFormatCache.getInstance().getFormat("application/octet-stream").getFmtid().getValue()))
467
                    {
468
                        response.setContentType("application/octet-stream");
469
                    }
470
                    else
471
                    {
472
                        response.setContentType("text/xml");
473
                        response.setHeader("Content-Disposition", "inline; filename=" + id.getValue() + ".xml");
474
                    }
475
                    
476
                    InputStream data = CNodeService.getInstance().get(session, id);
477
                    IOUtils.copyLarge(data, response.getOutputStream());
478
                }
479
                catch(InvalidToken it)
480
                {
481
                    response.setStatus(500);
482
                    serializeException(it, out); 
483
                }
484
                catch(ServiceFailure sf)
485
                {
486
                    response.setStatus(500);
487
                    serializeException(sf, out); 
488
                }
489
                catch(NotAuthorized na)
490
                {
491
                    response.setStatus(500);
492
                    serializeException(na, out); 
493
                }
494
                catch(NotFound nf)
495
                {
496
                    response.setStatus(500);
497
                    serializeException(nf, out); 
498
                }
499
                catch(NotImplemented ni)
500
                {
501
                    response.setStatus(500);
502
                    serializeException(ni, out); 
503
                }
504
                catch(Exception e)
505
                {
506
                    response.setStatus(500);
507
                    logMetacat.error("Error with Crud.get().  " +
508
                            "If this is an 'Exception producing data' error, " +
509
                            "go to CrudService.get() for better debugging.  " +
510
                            "Here's the error: " + e.getClass() + ": " + e.getMessage());
511
                    e.printStackTrace();
512
                    ServiceFailure sf = new ServiceFailure("1030", 
513
                            "IO Error in D1ResourceHandler.getObject: " + e.getClass() + ": " + e.getMessage());
514
                    serializeException(sf, out); 
515
                }
452
            //set the content type
453
            if(sm.getObjectFormat().getFmtid().getValue().trim().equals(
454
            		ObjectFormatCache.getInstance().getFormat("text/csv").getFmtid().getValue()))
455
            {
456
                response.setContentType("text/csv");
457
                response.setHeader("Content-Disposition", "inline; filename=" + id.getValue() + ".csv");
516 458
            }
459
            else if(sm.getObjectFormat().getFmtid().getValue().trim().equals(
460
            		ObjectFormatCache.getInstance().getFormat("text/plain").getFmtid().getValue()))
461
            {
462
                response.setContentType("text/plain");
463
                response.setHeader("Content-Disposition", "inline; filename=" + id.getValue() + ".txt");
464
            } 
465
            else if(sm.getObjectFormat().getFmtid().getValue().trim().equals(
466
            		ObjectFormatCache.getInstance().getFormat("application/octet-stream").getFmtid().getValue()))
467
            {
468
                response.setContentType("application/octet-stream");
469
            }
470
            else
471
            {
472
                response.setContentType("text/xml");
473
                response.setHeader("Content-Disposition", "inline; filename=" + id.getValue() + ".xml");
474
            }
517 475
            
518
        } catch (IOException e) {
519
            e.printStackTrace();
520
            response.setStatus(500);
521
            ServiceFailure sf = new ServiceFailure("1030", 
522
                    "IO Error in D1ResourceHandler.getObject: " + e.getMessage());
523
            serializeException(sf, out); 
524
        } catch(NumberFormatException ne) {
525
            response.setStatus(500);
526
            InvalidRequest ir = new InvalidRequest("1030", "Invalid format for parameter: " + ne.getMessage());
527
            serializeException(ir, out);
528
        } catch (Exception e) {
529
            e.printStackTrace();
530
            response.setStatus(500);
531
            ServiceFailure sf = new ServiceFailure("1030", 
532
                    "Exception " + e.getClass().getName() + " raised while handling listObjects request: " + 
533
                    e.getMessage());
534
            serializeException(sf, out);
476
            InputStream data = CNodeService.getInstance().get(session, id);
477
            
478
            OutputStream out = response.getOutputStream();
479
            response.setStatus(200);
480
            IOUtils.copyLarge(data, out);
481

  
535 482
        }
483
            
536 484
    }
537 485
    
538 486

  
539 487
    /**
540 488
     * Implements REST version of DataONE CRUD API --> getSystemMetadata
541 489
     * @param guid ID of data object to be read
490
     * @throws NotImplemented 
491
     * @throws InvalidRequest 
492
     * @throws NotFound 
493
     * @throws NotAuthorized 
494
     * @throws ServiceFailure 
495
     * @throws InvalidToken 
496
     * @throws IOException 
497
     * @throws JiBXException 
542 498
     */
543
    protected void getSystemMetadataObject(String guid) {
544
        OutputStream out = null;
545
        try {
546
            response.setContentType("text/xml");
547
            response.setStatus(200);
548
            out = response.getOutputStream();
549
            Identifier id = new Identifier();
550
            id.setValue(guid);
551
            SystemMetadata sysmeta = CNodeService.getInstance().getSystemMetadata(session, id);
552
            
553
            // Serialize and write it to the output stream
554
            try {
555
                //TODO: look at the efficiency of this method.  The system metadata
556
                //is read from metacat (in CrudService) as xml, then serialized
557
                //to a SystemMetadat object, then returned here, then serizlized
558
                //back to XML to be sent to the response.
559
                serializeServiceType(SystemMetadata.class, sysmeta, out);
560
            } catch (JiBXException e) {
561
                throw new ServiceFailure("1190", "Failed to serialize SystemMetadata: " + e.getMessage());
562
            }
563
        } catch (BaseException e) {
564
            response.setStatus(500);
565
                serializeException(e, out);
566
        } catch (IOException e) {
567
            response.setStatus(500);
568
            ServiceFailure sf = new ServiceFailure("1030", 
569
                    "IO Error in D1ResourceHandler.getSystemMetadataObject: " + e.getMessage());
570
            serializeException(sf, out);
571
        } finally {
572
            IOUtils.closeQuietly(out);
573
        }
574
    }
499
    protected void getSystemMetadataObject(String guid) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, InvalidRequest, NotImplemented, IOException, JiBXException {
500

  
501
        Identifier id = new Identifier();
502
        id.setValue(guid);
503
        SystemMetadata sysmeta = CNodeService.getInstance().getSystemMetadata(session, id);
504
        
505
        response.setContentType("text/xml");
506
        response.setStatus(200);
507
        OutputStream out = response.getOutputStream();
508
        
509
        // Serialize and write it to the output stream
510
       serializeServiceType(SystemMetadata.class, sysmeta, out);
511
   }
575 512
    
576 513
    /**
577 514
     * Earthgrid API > Put Service >Put Function : calls MetacatHandler > handleInsertOrUpdateAction 
......
603 540
        File objFile = files.get("object");
604 541
        object = new FileInputStream(objFile);
605 542
       
606
        if ( action.equals(FUNCTION_NAME_INSERT)) { //handle inserts
543
        if (action.equals(FUNCTION_NAME_INSERT)) { //handle inserts
607 544

  
608 545
            logMetacat.debug("Commence creation...");
609 546
            SystemMetadata smd = (SystemMetadata) deserializeServiceType(SystemMetadata.class, sysmeta);
......
626 563

  
627 564
    /**
628 565
     * List the object formats registered with the system
566
     * @throws NotImplemented 
567
     * @throws InsufficientResources 
568
     * @throws NotFound 
569
     * @throws ServiceFailure 
570
     * @throws InvalidRequest 
571
     * @throws IOException 
572
     * @throws JiBXException 
629 573
     */
630
		private void listFormats() {
574
	private void listFormats() throws InvalidRequest, ServiceFailure, NotFound, InsufficientResources, NotImplemented, IOException, JiBXException {
631 575
      logMetacat.debug("Entering listFormats()");
576

  
577
      ObjectFormatList objectFormatList = CNodeService.getInstance().listFormats();
632 578
      // get the response output stream
633
      OutputStream out = null;
579
      OutputStream out = response.getOutputStream();
580
      response.setStatus(200);
581
      response.setContentType("text/xml");
634 582
      
635
      try {
636
	      out = response.getOutputStream();
637
	      response.setStatus(200);
638
	      response.setContentType("text/xml");
639
	      
640
      } catch (IOException ioe) {
641
      	logMetacat.error("Could not get the output stream for writing" +
642
      	  "in D1ResourceHandler.listFormats()");
643
      
644
      }
645

  
646
      // get the object format list
647
      try {
648
	      ObjectFormatList objectFormatList = CNodeService.getInstance().listFormats();
649
	      serializeServiceType(ObjectFormatList.class, objectFormatList, out);
650
      
651
      } catch (InvalidRequest e) {
652
      	response.setStatus(200);
653
      	serializeException(e, out);
654
      	
655
      } catch (ServiceFailure e) {
656
      	response.setStatus(501);
657
      	serializeException(e, out);
658
      
659
      } catch (NotFound e) {
660
      	response.setStatus(200);
661
      	serializeException(e, out);
662
      
663
      } catch (InsufficientResources e) {
664
      	response.setStatus(200);
665
      	serializeException(e, out);
666
      
667
      } catch (NotImplemented e) {
668
      	response.setStatus(200);
669
      	serializeException(e, out);
670
      
671
      } catch (JiBXException jibxe) {
672
      	response.setStatus(501);
673
      	ServiceFailure e = 
674
      		new ServiceFailure("4841", "Unexpected exception from the service - " + 
675
        	                   jibxe.getClass() + ": " + jibxe.getMessage());
676
      	serializeException(e, out);
677
      	
678
      }
679
      
583
      serializeServiceType(ObjectFormatList.class, objectFormatList, out);
584
            
680 585
    }
681 586

  
682 587
		/**
683 588
     * Return the requested object format
684 589
     * 
685 590
     * @param fmtidStr the requested format identifier as a string
591
		 * @throws NotImplemented 
592
		 * @throws InsufficientResources 
593
		 * @throws NotFound 
594
		 * @throws ServiceFailure 
595
		 * @throws InvalidRequest 
596
		 * @throws IOException 
597
		 * @throws JiBXException 
686 598
     */
687
    private void getFormat(String fmtidStr) {
599
    private void getFormat(String fmtidStr) throws InvalidRequest, ServiceFailure, NotFound, InsufficientResources, NotImplemented, IOException, JiBXException {
688 600
      logMetacat.debug("Entering listFormats()");
689 601
      
690
      // get the response output stream
691
      OutputStream out = null;
692
      try {
693
	      out = response.getOutputStream();
694
	      response.setStatus(200);
695
	      response.setContentType("text/xml");
696
	      
697
      } catch (IOException ioe) {
698
      	logMetacat.error("Could not get the output stream for writing" +
699
      	  "in D1ResourceHandler.listFormats()");
700
      
701
      }
702
      
703 602
      ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
704 603
      fmtid.setValue(fmtidStr);
705 604
      
706
      try {
707
      	// get the specified object format
708
	      ObjectFormat objectFormat = CNodeService.getInstance().getFormat(fmtid);
709
	      serializeServiceType(ObjectFormat.class, objectFormat, out);
710

  
711
      } catch (InvalidRequest e) {
712
      	response.setStatus(200);
713
      	serializeException(e, out);
605
	  // get the specified object format
606
      ObjectFormat objectFormat = CNodeService.getInstance().getFormat(fmtid);
714 607
      
715
      } catch (ServiceFailure e) {
716
      	response.setStatus(501);
717
      	serializeException(e, out);
608
      OutputStream out = response.getOutputStream();
609
      response.setStatus(200);
610
      response.setContentType("text/xml");
718 611
      
719
      } catch (NotFound e) {
720
      	response.setStatus(200);
721
      	serializeException(e, out);
612
      serializeServiceType(ObjectFormat.class, objectFormat, out);
722 613
      
723
      } catch (InsufficientResources e) {
724
      	response.setStatus(200);
725
      	serializeException(e, out);
726
      
727
      } catch (NotImplemented e) {
728
      	response.setStatus(200);
729
      	serializeException(e, out);
730
      
731
      } catch (JiBXException jibxe) {
732
      	ServiceFailure e = 
733
      		new ServiceFailure("4841", "Unexpected exception from the service - " + 
734
        	                   jibxe.getClass() + ": " + jibxe.getMessage());
735
      	serializeException(e, out);
736

  
737
      }
738
	    
739 614
    }
740 615
    
741 616
    /**
......
747 622
     * @throws NotImplemented
748 623
     * @throws InvalidRequest
749 624
     * @throws IOException 
625
     * @throws JiBXException 
750 626
     */
751
    private void reserve() throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, NotImplemented, InvalidRequest, IOException {
627
    private void reserve() throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, NotImplemented, InvalidRequest, IOException, JiBXException {
752 628
    	String id = params.get("pid")[0];
753 629
    	String format = params.get("format")[0];
754 630
		String scope = params.get("scope")[0];
......
758 634
		OutputStream out = response.getOutputStream();
759 635
		response.setStatus(200);
760 636
		response.setContentType("text/xml");
761
		try {
762
			serializeServiceType(Identifier.class, retPid, out);
763
		} catch (JiBXException e) {
764
	      throw new ServiceFailure("4210", 
765
	      				"Unexpected exception from the service - " + 
766
	        	                   e.getClass() + ": " + e.getMessage());
767
		}
637
		serializeServiceType(Identifier.class, retPid, out);
768 638
		
769 639
    }
770 640
    
771
    private void resolve(String id) throws InvalidRequest, InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented, IOException {
641
    /**
642
     * 
643
     * @param id
644
     * @throws InvalidRequest
645
     * @throws InvalidToken
646
     * @throws ServiceFailure
647
     * @throws NotAuthorized
648
     * @throws NotFound
649
     * @throws NotImplemented
650
     * @throws IOException
651
     * @throws JiBXException 
652
     */
653
    private void resolve(String id) throws InvalidRequest, InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented, IOException, JiBXException {
772 654
		Identifier pid = new Identifier();
773 655
		pid.setValue(id);
774 656
		ObjectLocationList locationList = CNodeService.getInstance().resolve(session, pid);
775 657
	    OutputStream out = response.getOutputStream();
776 658
		response.setStatus(200);
777 659
		response.setContentType("text/xml");
778
		try {
779
			serializeServiceType(ObjectLocationList.class, locationList, out);
780
		} catch (JiBXException e) {
781
	      throw new ServiceFailure("4150", 
782
	      				"Unexpected exception from the service - " + 
783
	        	                   e.getClass() + ": " + e.getMessage());
784
		}
660
		serializeServiceType(ObjectLocationList.class, locationList, out);
661
		
785 662
    }
786 663

  
664
    /**
665
     * Assert that a relationship exists between two resources
666
     * @param id
667
     * @return
668
     * @throws InvalidToken
669
     * @throws ServiceFailure
670
     * @throws NotAuthorized
671
     * @throws NotFound
672
     * @throws InvalidRequest
673
     * @throws NotImplemented
674
     */
787 675
    private boolean assertRelation(String id) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, InvalidRequest, NotImplemented {
788 676
		Identifier pidOfSubject = new Identifier();
789 677
		pidOfSubject.setValue(id);
678
		String relationship = null;
679
		try {
680
			relationship = params.get("relationship")[0];
681
		} catch (Exception e) {
682
			logMetacat.warn("relationship not specified");
683
		}
790 684
		Identifier pidOfObject = new Identifier();
791
		String relationship;
792
		relationship = params.get("relationship")[0];
793
		String objPid = params.get("pidOfObject")[0];
794
		pidOfObject.setValue(objPid);
685
		try {
686
			String objPid = params.get("pidOfObject")[0];
687
			pidOfObject.setValue(objPid);
688
		} catch (Exception e) {
689
			logMetacat.warn("pidOfObject not specified");
690
		}
795 691
		boolean result = CNodeService.getInstance().assertRelation(session, pidOfSubject, relationship, pidOfObject);
796 692
		response.setStatus(200);
797 693
		response.setContentType("text/xml");
798 694
		return result;
799 695
    }
800 696
    
697
    /**
698
     * Set the owner of a resource
699
     * @param id
700
     * @throws JiBXException
701
     * @throws InvalidToken
702
     * @throws ServiceFailure
703
     * @throws NotFound
704
     * @throws NotAuthorized
705
     * @throws NotImplemented
706
     * @throws InvalidRequest
707
     * @throws IOException
708
     */
801 709
    private void owner(String id) throws JiBXException, InvalidToken, ServiceFailure, NotFound, NotAuthorized, NotImplemented, InvalidRequest, IOException {
802 710
		Identifier pid = new Identifier();
803 711
		pid.setValue(id);
......
807 715
		OutputStream out = response.getOutputStream();
808 716
		response.setStatus(200);
809 717
		response.setContentType("text/xml");
810
		try {
811
			serializeServiceType(Identifier.class, retPid, out);
812
		} catch (JiBXException e) {
813
	      throw new ServiceFailure("4490", 
814
	      				"Unexpected exception from the service - " + 
815
	        	                   e.getClass() + ": " + e.getMessage());
816
		}
817
		
718
		serializeServiceType(Identifier.class, retPid, out);		
818 719
    }
819 720
    
721
    /**
722
     * Processes the authorization check for given id
723
     * @param id
724
     * @return
725
     * @throws ServiceFailure
726
     * @throws InvalidToken
727
     * @throws NotFound
728
     * @throws NotAuthorized
729
     * @throws NotImplemented
730
     * @throws InvalidRequest
731
     */
820 732
    private boolean isAuthorized(String id) throws ServiceFailure, InvalidToken, NotFound, NotAuthorized, NotImplemented, InvalidRequest {
821 733
		Identifier pid = new Identifier();
822 734
		pid.setValue(id);
......
858 770
    
859 771
    /**
860 772
     * set the access perms on a document
861
     * @throws Exception
773
     * @throws JiBXException 
774
     * @throws UnsupportedEncodingException 
775
     * @throws InvalidRequest 
776
     * @throws NotImplemented 
777
     * @throws NotAuthorized 
778
     * @throws NotFound 
779
     * @throws ServiceFailure 
780
     * @throws InvalidToken 
862 781
     */
863
    protected void setaccess() throws Exception
864
    {
865
        try
866
        {
867
            String guid = params.get("guid")[0];
868
            Identifier id = new Identifier();
869
            id.setValue(guid);
870
            String accesspolicy = params.get("accesspolicy")[0];
871
            AccessPolicy accessPolicy = (AccessPolicy) deserializeServiceType(AccessPolicy.class, new ByteArrayInputStream(accesspolicy.getBytes("UTF-8")));
872
            CNodeService.getInstance().setAccessPolicy(session, id, accessPolicy);
873
        }
874
        catch(Exception e)
875
        {
876
            response.setStatus(500);
877
            printError("Error setting access in D1ResourceHandler: " + e.getClass() + ": " + e.getMessage(), response);
878
            throw e;
879
        }
782
    protected void setAccess() throws UnsupportedEncodingException, JiBXException, InvalidToken, ServiceFailure, NotFound, NotAuthorized, NotImplemented, InvalidRequest {
783

  
784
        String guid = params.get("guid")[0];
785
        Identifier id = new Identifier();
786
        id.setValue(guid);
787
        String accesspolicy = params.get("accesspolicy")[0];
788
        AccessPolicy accessPolicy = (AccessPolicy) deserializeServiceType(AccessPolicy.class, new ByteArrayInputStream(accesspolicy.getBytes("UTF-8")));
789
        CNodeService.getInstance().setAccessPolicy(session, id, accessPolicy);
790

  
880 791
    }
881 792
    
882 793
    /**

Also available in: Unified diff