001/*
002 * Copyright (c) 2015 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2017-08-23 22:42:39 -0700 (Wed, 23 Aug 2017) $' 
007 * '$Revision: 1375 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029package org.kepler.webview.server.handler;
030
031import java.net.HttpURLConnection;
032
033import org.kepler.webview.server.WebViewId;
034import org.kepler.webview.server.WebViewServer;
035
036import io.vertx.ext.web.RoutingContext;
037import ptolemy.actor.CompositeActor;
038import ptolemy.kernel.util.IllegalActionException;
039
040/** A HTTP Server handler that generates a table of contents for
041 *  the WebViewServer showing all the available workflows.
042 *  
043 *  @author Daniel Crawl
044 *  @version $Id: TableOfContentsHandler.java 1375 2017-08-24 05:42:39Z crawl $
045 */
046public class TableOfContentsHandler extends BaseHandler {
047
048    public TableOfContentsHandler(WebViewServer server) {
049        super(server);
050    }
051
052    @Override
053    public void handle(RoutingContext context) {
054
055        long timestamp = System.currentTimeMillis();
056        
057        StringBuilder buf = new StringBuilder("<html>\n<body>\n");
058        
059        if(WebViewServer._models.isEmpty()) {
060            buf.append("<h2>No workflows loaded!<h2>\n");
061        } else {
062            buf.append("  <ul>\n");
063            for(CompositeActor model : WebViewServer._models.values()) {
064                String name = model.getName();
065                String id;
066                try {
067                    id = WebViewId.getId(model);
068                } catch (IllegalActionException e) {
069                    // TODO Auto-generated catch block
070                    e.printStackTrace();
071                    continue;
072                }
073                buf.append("    <li><a href=\"/wf/")
074                    .append(id)
075                    .append("\">")
076                    .append(name)
077                    .append("</a></li>\n");
078            }
079            buf.append("  </ul>\n");
080        }
081        buf.append("</body>\n</html>\n");
082        context.response()
083            // add headers to prevent this page from being cached by the browser
084            .putHeader("Cache-Control", "no-cache, no-store, must-revalidate")
085            .putHeader("Pragma", "no-cache")
086            .putHeader("Expires", "0")
087            .end(buf.toString());
088        
089        _server.log(context.request(), context.user(), HttpURLConnection.HTTP_OK, timestamp);            
090    }
091}