Search in sources :

Example 1 with RenderException

use of org.eclipse.smarthome.ui.classic.render.RenderException in project smarthome by eclipse.

the class WebAppServlet method service.

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    logger.debug("Servlet request received!");
    // read request parameters
    String sitemapName = req.getParameter("sitemap");
    String widgetId = req.getParameter("w");
    boolean async = "true".equalsIgnoreCase(req.getParameter("__async"));
    boolean poll = "true".equalsIgnoreCase(req.getParameter("poll"));
    // if there are no parameters, display the "default" sitemap
    if (sitemapName == null) {
        sitemapName = config.getDefaultSitemap();
    }
    StringBuilder result = new StringBuilder();
    Sitemap sitemap = null;
    for (SitemapProvider sitemapProvider : sitemapProviders) {
        sitemap = sitemapProvider.getSitemap(sitemapName);
        if (sitemap != null) {
            break;
        }
    }
    try {
        if (sitemap == null) {
            throw new RenderException("Sitemap '" + sitemapName + "' could not be found");
        }
        logger.debug("reading sitemap {} widgetId {} async {} poll {}", sitemap.getName(), widgetId, async, poll);
        if (widgetId == null || widgetId.isEmpty() || widgetId.equals("Home")) {
            // we are at the homepage, so we render the children of the sitemap root node
            String label = sitemap.getLabel() != null ? sitemap.getLabel() : sitemapName;
            EList<Widget> children = renderer.getItemUIRegistry().getChildren(sitemap);
            if (poll && waitForChanges(children) == false) {
                // we have reached the timeout, so we do not return any content as nothing has changed
                res.getWriter().append(getTimeoutResponse()).close();
                return;
            }
            result.append(renderer.processPage("Home", sitemapName, label, children, async));
        } else if (!widgetId.equals("Colorpicker")) {
            // we are on some subpage, so we have to render the children of the widget that has been selected
            Widget w = renderer.getItemUIRegistry().getWidget(sitemap, widgetId);
            if (w != null) {
                if (!(w instanceof LinkableWidget)) {
                    throw new RenderException("Widget '" + w + "' can not have any content");
                }
                LinkableWidget lw = (LinkableWidget) w;
                EList<Widget> children = renderer.getItemUIRegistry().getChildren(lw);
                EList<Widget> parentAndChildren = new BasicEList<Widget>();
                parentAndChildren.add(lw);
                parentAndChildren.addAll(children);
                if (poll && waitForChanges(parentAndChildren) == false) {
                    // we have reached the timeout, so we do not return any content as nothing has changed
                    res.getWriter().append(getTimeoutResponse()).close();
                    return;
                }
                String label = renderer.getItemUIRegistry().getLabel(w);
                if (label == null) {
                    label = "undefined";
                }
                result.append(renderer.processPage(renderer.getItemUIRegistry().getWidgetId(w), sitemapName, label, children, async));
            }
        }
    } catch (RenderException e) {
        throw new ServletException(e.getMessage(), e);
    }
    if (async) {
        res.setContentType("application/xml;charset=UTF-8");
    } else {
        res.setContentType("text/html;charset=UTF-8");
    }
    res.getWriter().append(result);
    res.getWriter().close();
}
Also used : LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget) ServletException(javax.servlet.ServletException) Sitemap(org.eclipse.smarthome.model.sitemap.Sitemap) RenderException(org.eclipse.smarthome.ui.classic.render.RenderException) BasicEList(org.eclipse.emf.common.util.BasicEList) EList(org.eclipse.emf.common.util.EList) SitemapProvider(org.eclipse.smarthome.model.sitemap.SitemapProvider) Widget(org.eclipse.smarthome.model.sitemap.Widget) LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget)

Example 2 with RenderException

use of org.eclipse.smarthome.ui.classic.render.RenderException in project smarthome by eclipse.

the class AbstractWidgetRenderer method getSnippet.

/**
 * This method provides the html snippet for a given elementType of the sitemap model.
 *
 * @param elementType the name of the model type (e.g. "Group" or "Switch")
 * @return the html snippet to be used in the UI (including placeholders for variables)
 * @throws RenderException if snippet could not be read
 */
protected synchronized String getSnippet(String elementType) throws RenderException {
    String lowerCaseElementType = elementType.toLowerCase();
    String snippet = SNIPPET_CACHE.get(lowerCaseElementType);
    if (snippet == null) {
        String snippetLocation = SNIPPET_LOCATION + lowerCaseElementType + SNIPPET_EXT;
        URL entry = WebAppActivator.getContext().getBundle().getEntry(snippetLocation);
        if (entry != null) {
            try {
                snippet = IOUtils.toString(entry.openStream());
                if (!config.isHtmlCacheDisabled()) {
                    SNIPPET_CACHE.put(lowerCaseElementType, snippet);
                }
            } catch (IOException e) {
                logger.warn("Cannot load snippet for element type '{}'", lowerCaseElementType, e);
            }
        } else {
            throw new RenderException("Cannot find a snippet for element type '" + lowerCaseElementType + "'");
        }
    }
    return snippet;
}
Also used : RenderException(org.eclipse.smarthome.ui.classic.render.RenderException) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

RenderException (org.eclipse.smarthome.ui.classic.render.RenderException)2 IOException (java.io.IOException)1 URL (java.net.URL)1 ServletException (javax.servlet.ServletException)1 BasicEList (org.eclipse.emf.common.util.BasicEList)1 EList (org.eclipse.emf.common.util.EList)1 LinkableWidget (org.eclipse.smarthome.model.sitemap.LinkableWidget)1 Sitemap (org.eclipse.smarthome.model.sitemap.Sitemap)1 SitemapProvider (org.eclipse.smarthome.model.sitemap.SitemapProvider)1 Widget (org.eclipse.smarthome.model.sitemap.Widget)1