Project

General

Profile

« Previous | Next » 

Revision 5450

Added by berkley over 14 years ago

set the content type to text/xml for the rest interface

View differences:

src/edu/ucsb/nceas/metacat/restservice/ResourceHandler.java
394 394
        try
395 395
        {
396 396
            out = response.getOutputStream();
397
            response.setContentType("text/xml");
397 398
            AuthToken token = new AuthToken(sessionId);
398 399
            String fromDateS = params.get("fromDate")[0];
399 400
            System.out.println("param fromDateS: " + fromDateS);
......
542 543
        
543 544
        params.put("docid", new String[] { localId });
544 545
        PrintWriter out = response.getWriter();
546
        response.setContentType("text/xml");
545 547
        handler.handleIdIsRegisteredAction(out, params, response);
546 548
        out.close();
547 549
    }
......
553 555
     */
554 556
    private void getAllDocIds() throws IOException {
555 557
        PrintWriter out = response.getWriter();
558
        response.setContentType("text/xml");
556 559
        handler.handleGetAllDocidsAction(out, params, response);
557 560
        out.close();
558 561
    }
......
567 570
    {
568 571
        params.put("docid", new String[] { guid });
569 572
        PrintWriter out = response.getWriter();
573
        response.setContentType("text/xml");
570 574
        //handler.handleGetRevisionAndDocTypeAction(out, params);
571 575

  
572 576
        try {
......
614 618
     */
615 619
    private void getNextObject() throws IOException {
616 620
        PrintWriter out = response.getWriter();
621
        response.setContentType("text/xml");
617 622
        handler.handleGetMaxDocidAction(out, params, response);
618 623
        out.close();
619 624
    }
......
622 627
     * Implements REST version of DataONE CRUD API --> get
623 628
     * @param guid ID of data object to be read
624 629
     */
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
	}
630
    private void getObject(String guid) {
631
        CrudService cs = CrudService.getInstance();
632
        cs.setParamsFromRequest(request);
633
        AuthToken token = new AuthToken(sessionId);
634
        OutputStream out = null;
635
        try {
636
            out = response.getOutputStream();
637
            response.setContentType("text/xml");
638
            if(guid != null)
639
            { //get a specific document                
640
                Identifier id = new Identifier();
641
                id.setValue(guid);
642
                try
643
                {
644
                    if(token == null)
645
                    {
646
                        token = new AuthToken("Public");
647
                    }
648
                    InputStream data = cs.get(token, id);
649
                    IOUtils.copyLarge(data, response.getOutputStream());
650
                }
651
                catch(InvalidToken it)
652
                {
653
                    serializeException(it, out); 
654
                }
655
                catch(ServiceFailure sf)
656
                {
657
                    serializeException(sf, out); 
658
                }
659
                catch(NotAuthorized na)
660
                {
661
                    serializeException(na, out); 
662
                }
663
                catch(NotFound nf)
664
                {
665
                    serializeException(nf, out); 
666
                }
667
                catch(NotImplemented ni)
668
                {
669
                    serializeException(ni, out); 
670
                }
671
                catch(Exception e)
672
                {
673
                    System.out.println("Error with Crud.get().  " +
674
                            "If this is an 'Exception producing data' error, " +
675
                            "go to CrudService.get() for better debugging.  " +
676
                            "Here's the error: " + e.getMessage());
677
                    e.printStackTrace();
678
                    ServiceFailure sf = new ServiceFailure("1030", 
679
                            "IO Error in ResourceHandler.getObject: " + e.getMessage());
680
                    serializeException(sf, out); 
681
                }
682
            }
683
            else
684
            { //call listObjects with specified params
685
                Date startTime = null;
686
                Date endTime = null;
687
                ObjectFormat objectFormat = null;
688
                boolean replicaStatus = false;
689
                int start = 0;
690
                int count = 1000;
691
                Enumeration paramlist = request.getParameterNames();
692
                while (paramlist.hasMoreElements()) 
693
                { //parse the params and make the crud call
694
                    String name = (String) paramlist.nextElement();
695
                    String[] value = (String[])request.getParameterValues(name);
696
                    /*for(int i=0; i<value.length; i++)
697
                    {
698
                        System.out.println("name: " + name + " value: " + value[i]);
699
                    }*/
700
                    if(name.equals("startTime") && value != null)
701
                    {
702
                        try
703
                        {
704
                          //startTime = dateFormat.parse(value[0]);
705
                            startTime = parseDateAndConvertToGMT(value[0]);
706
                        }
707
                        catch(Exception e)
708
                        {  //if we can't parse it, just don't use the startTime param
709
                            System.out.println("Could not parse startTime: " + value[0]);
710
                            startTime = null;
711
                        }
712
                    }
713
                    else if(name.equals("endTime") && value != null)
714
                    {
715
                        try
716
                        {
717
                          //endTime = dateFormat.parse(value[0]);
718
                            endTime = parseDateAndConvertToGMT(value[0]);
719
                        }
720
                        catch(Exception e)
721
                        {  //if we can't parse it, just don't use the endTime param
722
                            System.out.println("Could not parse endTime: " + value[0]);
723
                            endTime = null;
724
                        }
725
                    }
726
                    else if(name.equals("objectFormat") && value != null)
727
                    {
728
                        objectFormat = ObjectFormat.convert(value[0]);
729
                    }
730
                    else if(name.equals("replicaStatus") && value != null)
731
                    {
732
                        if(value != null && 
733
                           value.length > 0 && 
734
                           (value[0].equals("true") || value[0].equals("TRUE") || value[0].equals("YES")))
735
                        {
736
                            replicaStatus = true;
737
                        }
738
                    }
739
                    else if(name.equals("start") && value != null)
740
                    {
741
                        start = new Integer(value[0]).intValue();
742
                    }
743
                    else if(name.equals("count") && value != null)
744
                    {
745
                        count = new Integer(value[0]).intValue();
746
                    }
747
                }
748
                //make the crud call
749
                /*System.out.println("token: " + token + " startTime: " + startTime +
750
                        " endtime: " + endTime + " objectFormat: " + 
751
                        objectFormat + " replicaStatus: " + replicaStatus + 
752
                        " start: " + start + " count: " + count);
753
                */
754
                ObjectList ol = cs.listObjects(token, startTime, endTime, 
755
                        objectFormat, replicaStatus, start, count);
756
                ol = cs.listObjects(token, startTime, endTime, objectFormat, replicaStatus, start, count);
757
                
758
                StringReader sr = new StringReader(ol.toString());                
759
                out = response.getOutputStream();      
760
                response.setContentType("text/xml");
761
                // Serialize and write it to the output stream
762
                
763
                try {
764
                    serializeServiceType(ObjectList.class, ol, out);
765
                } catch (JiBXException e) {
766
                    throw new ServiceFailure("1190", "Failed to serialize ObjectList: " + e.getMessage());
767
                }
768
            }
769
        } catch (BaseException e) {
770
                serializeException(e, out);
771
        } catch (IOException e) {
772
            e.printStackTrace();
773
            ServiceFailure sf = new ServiceFailure("1030", 
774
                    "IO Error in ResourceHandler.getObject: " + e.getMessage());
775
            serializeException(sf, out); 
776
        } catch(NumberFormatException ne) {
777
            InvalidRequest ir = new InvalidRequest("1030", "Invalid format for parameter: " + ne.getMessage());
778
            serializeException(ir, out);
779
        } catch (Exception e) {
780
            e.printStackTrace();
781
            ServiceFailure sf = new ServiceFailure("1030", 
782
                    "Exception " + e.getClass().getName() + " raised while handling listObjects request: " + 
783
                    e.getMessage());
784
            serializeException(sf, out);
785
        }
786
    }
763 787
    
764 788
    /**
765 789
     * parse a date and return it in GMT if it ends with a 'Z'
......
813 837
        AuthToken token = new AuthToken(sessionId);
814 838
        OutputStream out = null;
815 839
        try {
840
            response.setContentType("text/xml");
816 841
            out = response.getOutputStream();
817 842
            Identifier id = new Identifier();
818 843
            id.setValue(guid);
......
965 990
        OutputStream out = null;
966 991
        try {
967 992
            out = response.getOutputStream();
993
            response.setContentType("text/xml");
968 994
        } catch (IOException e1) {
969 995
            logMetacat.error("Could not get the output stream for writing in putObject");
970 996
        }
......
1152 1178
        
1153 1179
        params.put("docid", new String[] { localId });
1154 1180
        PrintWriter out = response.getWriter();
1181
        response.setContentType("text/xml");
1155 1182
        handler.handleDeleteAction(out, params, request, response, username,
1156 1183
                groupNames);
1157 1184
        out.close();
......
1197 1224
     */
1198 1225
    private void login() throws IOException {
1199 1226
        PrintWriter out = response.getWriter();
1227
        response.setContentType("text/xml");
1200 1228
        handler.handleLoginAction(out, params, request, response);
1201 1229
        out.close();
1202 1230
    }
......
1208 1236
     */
1209 1237
    private void logout() throws IOException {
1210 1238
        PrintWriter out = response.getWriter();
1239
        response.setContentType("text/xml");
1211 1240
        handler.handleLogoutAction(out, params, request, response);
1212 1241
        out.close();
1213 1242
    }

Also available in: Unified diff