Project

General

Profile

« Previous | Next » 

Revision 9148

Added by Jing Tao about 9 years ago

Make the REST api work for views.

View differences:

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
}

Also available in: Unified diff