Project

General

Profile

« Previous | Next » 

Revision 9148

Added by Jing Tao over 9 years ago

Make the REST api work for views.

View differences:

src/edu/ucsb/nceas/metacat/restservice/v2/MNResourceHandler.java
70 70
import org.dataone.service.types.v1_1.QueryEngineList;
71 71
import org.dataone.service.types.v2.Log;
72 72
import org.dataone.service.types.v2.Node;
73
import org.dataone.service.types.v2.OptionList;
73 74
import org.dataone.service.types.v2.SystemMetadata;
74 75
import org.dataone.service.util.Constants;
75 76
import org.dataone.service.util.DateTimeMarshaller;
......
139 140
    protected static final String RESOURCE_GENERATE_ID = "generate";
140 141
    protected static final String RESOURCE_PUBLISH = "publish";
141 142
    protected static final String RESOURCE_PACKAGE = "package";
142
    protected static final String RESOURCE_VIEWS = "views";
143 143
    protected static final String RESOURCE_TOKEN = "token";
144 144

  
145 145

  
......
590 590
                return;
591 591
    		} else {
592 592
    			// TODO: list the registered views
593
                BaseException ni = new NotImplemented("9999", "MN.listViews() is not implemented at this node");
594
				throw ni;
593
                //BaseException ni = new NotImplemented("9999", "MN.listViews() is not implemented at this node");
594
				//throw ni;
595
    		    OptionList list = mnode.listViews(session);
596
    	        
597
    	        response.setContentType("text/xml");
598
    	        response.setStatus(200);
599
    	        TypeMarshaller.marshalTypeToOutputStream(list, response.getOutputStream());
595 600
    		}
596 601
	    	
597 602
	        
src/edu/ucsb/nceas/metacat/restservice/v2/CNResourceHandler.java
60 60
import org.dataone.service.types.v1.Event;
61 61
import org.dataone.service.types.v1.Identifier;
62 62
import org.dataone.service.types.v2.Log;
63
import org.dataone.service.types.v2.OptionList;
63 64
import org.dataone.service.types.v1.NodeReference;
64 65
import org.dataone.service.types.v2.ObjectFormat;
65 66
import org.dataone.service.types.v1.ObjectFormatIdentifier;
......
80 81
import org.jibx.runtime.JiBXException;
81 82
import org.xml.sax.SAXException;
82 83

  
84
import edu.ucsb.nceas.metacat.common.query.stream.ContentTypeInputStream;
83 85
import edu.ucsb.nceas.metacat.dataone.CNodeService;
84 86
import edu.ucsb.nceas.metacat.properties.PropertyService;
85 87
import edu.ucsb.nceas.metacat.restservice.D1ResourceHandler;
......
383 385
                    extra = parseTrailing(resource, Constants.RESOURCE_REPLICATION_DELETE_REPLICA);
384 386
                    deleteReplica(extra);
385 387
                    status = true;
386
                }
388
                } else if (resource.startsWith(RESOURCE_VIEWS)) {
389
                    logMetacat.debug("Using resource " + RESOURCE_VIEWS);
390
                    // after the command
391
                    extra = parseTrailing(resource, RESOURCE_VIEWS);
392
                    logMetacat.debug("view extra: " + extra);
387 393

  
394
                    String format = null;
395
                    String pid = null;
396

  
397
                    if (extra != null) {
398
                        // get the format
399
                        int formatIndex = extra.length();
400
                        if (extra.indexOf("/") > -1) {
401
                            formatIndex = extra.indexOf("/");
402
                        }
403
                        format = extra.substring(0, formatIndex);
404
                        logMetacat.debug("view format: " + format);
405
                        
406
                        // get the pid if it is there
407
                        pid = extra.substring(formatIndex, extra.length());
408
                        if (pid != null && pid.length() == 0) {
409
                            pid = null;
410
                        } else {
411
                            if (pid.startsWith("/")) {
412
                                pid = pid.substring(1);
413
                            }
414
                        }
415
                        logMetacat.debug("pid: " + pid);
416

  
417
                    }
418
                    logMetacat.debug("verb:" + httpVerb);
419
                    if (httpVerb == GET) {
420
                        doViews(format, pid);
421
                        status = true;
422
                    }
423
                } 
424

  
388 425
                if (!status) {
389 426
                    throw new ServiceFailure("0000", "Unknown error, status = "
390 427
                            + status);
......
1743 1780
        
1744 1781
        CNodeService.getInstance(request).updateSystemMetadata(session, pid, systemMetadata);
1745 1782
    }
1783
    
1784
    private void doViews(String format, String pid) {
1785
        
1786
        OutputStream out = null;
1787
        CNodeService cnode = CNodeService.getInstance(request);
1746 1788

  
1789
        try {
1790
            // get a list of views
1791
            if (pid != null) {
1792
                Identifier identifier = new Identifier();
1793
                identifier.setValue(pid);
1794
                InputStream stream = cnode.view(session, format, identifier);
1795

  
1796
                // set the content-type if we have it from the implementation
1797
                if (stream instanceof ContentTypeInputStream) {
1798
                    response.setContentType(((ContentTypeInputStream) stream).getContentType());
1799
                }
1800
                response.setStatus(200);
1801
                out = response.getOutputStream();
1802
                // write the results to the output stream
1803
                IOUtils.copyLarge(stream, out);
1804
                return;
1805
            } else {
1806
                // TODO: list the registered views
1807
                //BaseException ni = new NotImplemented("9999", "MN.listViews() is not implemented at this node");
1808
                //throw ni;
1809
                OptionList list = cnode.listViews(session);
1810
                
1811
                response.setContentType("text/xml");
1812
                response.setStatus(200);
1813
                TypeMarshaller.marshalTypeToOutputStream(list, response.getOutputStream());
1814
            }
1815
            
1816
            
1817
        } catch (BaseException be) {
1818
            // report Exceptions as clearly as possible
1819
            try {
1820
                out = response.getOutputStream();
1821
            } catch (IOException e) {
1822
                logMetacat.error("Could not get output stream from response", e);
1823
            }
1824
            serializeException(be, out);
1825
        } catch (Exception e) {
1826
            // report Exceptions as clearly and generically as possible
1827
            logMetacat.error(e.getClass() + ": " + e.getMessage(), e);
1828
            try {
1829
                out = response.getOutputStream();
1830
            } catch (IOException ioe) {
1831
                logMetacat.error("Could not get output stream from response", ioe);
1832
            }
1833
            ServiceFailure se = new ServiceFailure("0000", e.getMessage());
1834
            serializeException(se, out);
1835
        }
1836
    }
1837

  
1747 1838
}
src/edu/ucsb/nceas/metacat/restservice/D1ResourceHandler.java
97 97
    
98 98
    protected static final String RESOURCE_IS_AUTHORIZED = "isAuthorized";
99 99
    protected static final String RESOURCE_ACCESS_RULES = "accessRules";
100
    
101
    protected static final String RESOURCE_VIEWS = "views";
100 102

  
101 103
    
102 104
    /*
src/edu/ucsb/nceas/metacat/dataone/D1NodeService.java
1924 1924
  public OptionList listViews(Session arg0) throws InvalidToken,
1925 1925
          ServiceFailure, NotAuthorized, InvalidRequest, NotImplemented {
1926 1926
      OptionList views = new OptionList();
1927
      views.setKey("views");
1928
      views.setDescription("List of views for objects on the node");
1927 1929
      Vector<String> skinNames = null;
1928 1930
      try {
1929 1931
          skinNames = SkinUtil.getSkinNames();

Also available in: Unified diff