Revision 5447
Added by Matt Jones over 14 years ago
src/edu/ucsb/nceas/metacat/restservice/ResourceHandler.java | ||
---|---|---|
622 | 622 |
* Implements REST version of DataONE CRUD API --> get |
623 | 623 |
* @param guid ID of data object to be read |
624 | 624 |
*/ |
625 |
private void getObject(String guid) { |
|
626 |
CrudService cs = CrudService.getInstance(); |
|
627 |
cs.setParamsFromRequest(request); |
|
628 |
AuthToken token = new AuthToken(sessionId); |
|
629 |
OutputStream out = null; |
|
630 |
try { |
|
631 |
out = response.getOutputStream(); |
|
632 |
if(guid != null) |
|
633 |
{ //get a specific document |
|
634 |
Identifier id = new Identifier(); |
|
635 |
id.setValue(guid); |
|
636 |
try |
|
637 |
{ |
|
638 |
if(token == null) |
|
639 |
{ |
|
640 |
token = new AuthToken("Public"); |
|
641 |
} |
|
642 |
InputStream data = cs.get(token, id); |
|
643 |
IOUtils.copyLarge(data, response.getOutputStream()); |
|
644 |
} |
|
645 |
catch(InvalidToken it) |
|
646 |
{ |
|
647 |
serializeException(it, out); |
|
648 |
} |
|
649 |
catch(ServiceFailure sf) |
|
650 |
{ |
|
651 |
serializeException(sf, out); |
|
652 |
} |
|
653 |
catch(NotAuthorized na) |
|
654 |
{ |
|
655 |
serializeException(na, out); |
|
656 |
} |
|
657 |
catch(NotFound nf) |
|
658 |
{ |
|
659 |
serializeException(nf, out); |
|
660 |
} |
|
661 |
catch(NotImplemented ni) |
|
662 |
{ |
|
663 |
serializeException(ni, out); |
|
664 |
} |
|
665 |
catch(Exception e) |
|
666 |
{ |
|
667 |
System.out.println("Error with Crud.get(). " + |
|
668 |
"If this is an 'Exception producing data' error, " + |
|
669 |
"go to CrudService.get() for better debugging. " + |
|
670 |
"Here's the error: " + e.getMessage()); |
|
671 |
e.printStackTrace(); |
|
672 |
ServiceFailure sf = new ServiceFailure("1030", |
|
673 |
"IO Error in ResourceHandler.getObject: " + e.getMessage()); |
|
674 |
serializeException(sf, out); |
|
675 |
} |
|
676 |
} |
|
677 |
else |
|
678 |
{ //call listObjects with specified params |
|
679 |
Date startTime = null; |
|
680 |
Date endTime = null; |
|
681 |
ObjectFormat objectFormat = null; |
|
682 |
boolean replicaStatus = false; |
|
683 |
int start = 0; |
|
684 |
int count = 1000; |
|
685 |
Enumeration paramlist = request.getParameterNames(); |
|
686 |
while (paramlist.hasMoreElements()) |
|
687 |
{ //parse the params and make the crud call |
|
688 |
String name = (String) paramlist.nextElement(); |
|
689 |
String[] value = (String[])request.getParameterValues(name); |
|
690 |
/*for(int i=0; i<value.length; i++) |
|
691 |
{ |
|
692 |
System.out.println("name: " + name + " value: " + value[i]); |
|
693 |
}*/ |
|
694 |
if(name.equals("startTime") && value != null) |
|
695 |
{ |
|
696 |
try |
|
697 |
{ |
|
698 |
//startTime = dateFormat.parse(value[0]); |
|
699 |
startTime = parseDateAndConvertToGMT(value[0]); |
|
700 |
} |
|
701 |
catch(Exception e) |
|
702 |
{ //if we can't parse it, just don't use the startTime param |
|
703 |
System.out.println("Could not parse startTime: " + value[0]); |
|
704 |
startTime = null; |
|
705 |
} |
|
706 |
} |
|
707 |
else if(name.equals("endTime") && value != null) |
|
708 |
{ |
|
709 |
try |
|
710 |
{ |
|
711 |
//endTime = dateFormat.parse(value[0]); |
|
712 |
endTime = parseDateAndConvertToGMT(value[0]); |
|
713 |
} |
|
714 |
catch(Exception e) |
|
715 |
{ //if we can't parse it, just don't use the endTime param |
|
716 |
System.out.println("Could not parse endTime: " + value[0]); |
|
717 |
endTime = null; |
|
718 |
} |
|
719 |
} |
|
720 |
else if(name.equals("objectFormat") && value != null) |
|
721 |
{ |
|
722 |
objectFormat = ObjectFormat.convert(value[0]); |
|
723 |
} |
|
724 |
else if(name.equals("replicaStatus") && value != null) |
|
725 |
{ |
|
726 |
if(value != null && |
|
727 |
value.length > 0 && |
|
728 |
(value[0].equals("true") || value[0].equals("TRUE") || value[0].equals("YES"))) |
|
729 |
{ |
|
730 |
replicaStatus = true; |
|
731 |
} |
|
732 |
} |
|
733 |
else if(name.equals("start") && value != null) |
|
734 |
{ |
|
735 |
start = new Integer(value[0]).intValue(); |
|
736 |
} |
|
737 |
else if(name.equals("count") && value != null) |
|
738 |
{ |
|
739 |
count = new Integer(value[0]).intValue(); |
|
740 |
} |
|
741 |
} |
|
742 |
//make the crud call |
|
743 |
/*System.out.println("token: " + token + " startTime: " + startTime + |
|
744 |
" endtime: " + endTime + " objectFormat: " + |
|
745 |
objectFormat + " replicaStatus: " + replicaStatus + |
|
746 |
" start: " + start + " count: " + count); |
|
747 |
*/ |
|
748 |
ObjectList ol = cs.listObjects(token, startTime, endTime, |
|
749 |
objectFormat, replicaStatus, start, count); |
|
750 |
ol = cs.listObjects(token, startTime, endTime, objectFormat, replicaStatus, start, count); |
|
751 |
|
|
752 |
StringReader sr = new StringReader(ol.toString()); |
|
753 |
out = response.getOutputStream(); |
|
754 |
// Serialize and write it to the output stream |
|
755 |
|
|
756 |
try { |
|
757 |
serializeServiceType(ObjectList.class, ol, out); |
|
758 |
} catch (JiBXException e) { |
|
759 |
throw new ServiceFailure("1190", "Failed to serialize ObjectList: " + e.getMessage()); |
|
760 |
} |
|
761 |
} |
|
762 |
} catch (BaseException e) { |
|
763 |
serializeException(e, out); |
|
764 |
} catch (IOException e) { |
|
765 |
e.printStackTrace(); |
|
766 |
ServiceFailure sf = new ServiceFailure("1030", |
|
767 |
"IO Error in ResourceHandler.getObject: " + e.getMessage()); |
|
768 |
serializeException(sf, out); |
|
769 |
} catch(NumberFormatException ne) { |
|
770 |
InvalidRequest ir = new InvalidRequest("1030", "Invalid format for parameter: " + ne.getMessage()); |
|
771 |
serializeException(ir, out); |
|
772 |
} catch (Exception e) { |
|
773 |
e.printStackTrace(); |
|
774 |
ServiceFailure sf = new ServiceFailure("1030", |
|
775 |
"Exception " + e.getClass().getName() + " raised while handling listObjects request: " + |
|
776 |
e.getMessage()); |
|
777 |
serializeException(sf, out); |
|
778 |
} |
|
779 |
} |
|
625 |
private void getObject(String guid) { |
|
626 |
CrudService cs = CrudService.getInstance(); |
|
627 |
cs.setParamsFromRequest(request); |
|
628 |
AuthToken token = new AuthToken(sessionId); |
|
629 |
OutputStream out = null; |
|
630 |
try { |
|
631 |
out = response.getOutputStream(); |
|
632 |
if (guid != null) { // get a specific document |
|
633 |
Identifier id = new Identifier(); |
|
634 |
id.setValue(guid); |
|
635 |
try { |
|
636 |
if (token == null) { |
|
637 |
token = new AuthToken("Public"); |
|
638 |
} |
|
639 |
InputStream data = cs.get(token, id); |
|
640 |
IOUtils.copyLarge(data, response.getOutputStream()); |
|
641 |
} catch (InvalidToken it) { |
|
642 |
serializeException(it, out); |
|
643 |
} catch (ServiceFailure sf) { |
|
644 |
serializeException(sf, out); |
|
645 |
} catch (NotAuthorized na) { |
|
646 |
serializeException(na, out); |
|
647 |
} catch (NotFound nf) { |
|
648 |
serializeException(nf, out); |
|
649 |
} catch (NotImplemented ni) { |
|
650 |
serializeException(ni, out); |
|
651 |
} catch (Exception e) { |
|
652 |
System.out |
|
653 |
.println("Error with Crud.get(). " |
|
654 |
+ "If this is an 'Exception producing data' error, " |
|
655 |
+ "go to CrudService.get() for better debugging. " |
|
656 |
+ "Here's the error: " + e.getMessage()); |
|
657 |
e.printStackTrace(); |
|
658 |
ServiceFailure sf = new ServiceFailure("1030", |
|
659 |
"IO Error in ResourceHandler.getObject: " |
|
660 |
+ e.getMessage()); |
|
661 |
serializeException(sf, out); |
|
662 |
} |
|
663 |
} else { // call listObjects with specified params |
|
664 |
Date startTime = null; |
|
665 |
Date endTime = null; |
|
666 |
ObjectFormat objectFormat = null; |
|
667 |
boolean replicaStatus = false; |
|
668 |
int start = 0; |
|
669 |
int count = 1000; |
|
670 |
Enumeration paramlist = request.getParameterNames(); |
|
671 |
while (paramlist.hasMoreElements()) { |
|
672 |
// parse the params and make the crud call |
|
673 |
String name = (String) paramlist.nextElement(); |
|
674 |
String[] value = (String[]) request |
|
675 |
.getParameterValues(name); |
|
676 |
/* |
|
677 |
* for(int i=0; i<value.length; i++) { |
|
678 |
* System.out.println("name: " + name + " value: " + |
|
679 |
* value[i]); } |
|
680 |
*/ |
|
681 |
if (name.equals("startTime") && value != null) { |
|
682 |
try { |
|
683 |
// startTime = dateFormat.parse(value[0]); |
|
684 |
startTime = parseDateAndConvertToGMT(value[0]); |
|
685 |
} catch (Exception e) { // if we can't parse it, just |
|
686 |
// don't use the startTime param |
|
687 |
System.out.println("Could not parse startTime: " |
|
688 |
+ value[0]); |
|
689 |
startTime = null; |
|
690 |
} |
|
691 |
} else if (name.equals("endTime") && value != null) { |
|
692 |
try { |
|
693 |
// endTime = dateFormat.parse(value[0]); |
|
694 |
endTime = parseDateAndConvertToGMT(value[0]); |
|
695 |
} catch (Exception e) { // if we can't parse it, just |
|
696 |
// don't use the endTime param |
|
697 |
System.out.println("Could not parse endTime: " |
|
698 |
+ value[0]); |
|
699 |
endTime = null; |
|
700 |
} |
|
701 |
} else if (name.equals("objectFormat") && value != null) { |
|
702 |
objectFormat = ObjectFormat.convert(value[0]); |
|
703 |
} else if (name.equals("replicaStatus") && value != null) { |
|
704 |
if (value != null |
|
705 |
&& value.length > 0 |
|
706 |
&& (value[0].equals("true") |
|
707 |
|| value[0].equals("TRUE") || value[0] |
|
708 |
.equals("YES"))) { |
|
709 |
replicaStatus = true; |
|
710 |
} |
|
711 |
} else if (name.equals("start") && value != null) { |
|
712 |
start = new Integer(value[0]).intValue(); |
|
713 |
} else if (name.equals("count") && value != null) { |
|
714 |
count = new Integer(value[0]).intValue(); |
|
715 |
} |
|
716 |
} |
|
717 |
// make the crud call |
|
718 |
/* |
|
719 |
* System.out.println("token: " + token + " startTime: " + |
|
720 |
* startTime + " endtime: " + endTime + " objectFormat: " + |
|
721 |
* objectFormat + " replicaStatus: " + replicaStatus + |
|
722 |
* " start: " + start + " count: " + count); |
|
723 |
*/ |
|
724 |
// TODO: why is cs.listObjects called twice here? Either redundant, or a bug. |
|
725 |
ObjectList ol = cs.listObjects(token, startTime, endTime, |
|
726 |
objectFormat, replicaStatus, start, count); |
|
727 |
ol = cs.listObjects(token, startTime, endTime, objectFormat, |
|
728 |
replicaStatus, start, count); |
|
729 |
|
|
730 |
StringReader sr = new StringReader(ol.toString()); |
|
731 |
out = response.getOutputStream(); |
|
732 |
|
|
733 |
// Serialize and write it to the output stream in XML format |
|
734 |
// TODO: support other request serializations other than XML |
|
735 |
response.setContentType("text/xml"); |
|
736 |
try { |
|
737 |
serializeServiceType(ObjectList.class, ol, out); |
|
738 |
} catch (JiBXException e) { |
|
739 |
throw new ServiceFailure("1190", |
|
740 |
"Failed to serialize ObjectList: " + e.getMessage()); |
|
741 |
} |
|
742 |
} |
|
743 |
} catch (BaseException e) { |
|
744 |
serializeException(e, out); |
|
745 |
} catch (IOException e) { |
|
746 |
e.printStackTrace(); |
|
747 |
ServiceFailure sf = new ServiceFailure("1030", |
|
748 |
"IO Error in ResourceHandler.getObject: " + e.getMessage()); |
|
749 |
serializeException(sf, out); |
|
750 |
} catch (NumberFormatException ne) { |
|
751 |
InvalidRequest ir = new InvalidRequest("1030", |
|
752 |
"Invalid format for parameter: " + ne.getMessage()); |
|
753 |
serializeException(ir, out); |
|
754 |
} catch (Exception e) { |
|
755 |
e.printStackTrace(); |
|
756 |
ServiceFailure sf = new ServiceFailure("1030", "Exception " |
|
757 |
+ e.getClass().getName() |
|
758 |
+ " raised while handling listObjects request: " |
|
759 |
+ e.getMessage()); |
|
760 |
serializeException(sf, out); |
|
761 |
} |
|
762 |
} |
|
780 | 763 |
|
781 | 764 |
/** |
782 | 765 |
* parse a date and return it in GMT if it ends with a 'Z' |
Also available in: Unified diff
Set content type on listObjects to be XML so that it is recognized by clients.
Reformatted method to use original file conventions.