Search in sources :

Example 1 with RenderException

use of org.eclipse.smarthome.ui.basic.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 lowerTypeElementType = elementType.toLowerCase();
    String snippet = SNIPPET_CACHE.get(lowerTypeElementType);
    if (snippet == null) {
        String snippetLocation = SNIPPET_LOCATION + lowerTypeElementType + SNIPPET_EXT;
        URL entry = WebAppActivator.getContext().getBundle().getEntry(snippetLocation);
        if (entry != null) {
            try {
                snippet = IOUtils.toString(entry.openStream());
                SNIPPET_CACHE.put(lowerTypeElementType, snippet);
            } catch (IOException e) {
                logger.warn("Cannot load snippet for element type '{}'", lowerTypeElementType, e);
            }
        } else {
            throw new RenderException("Cannot find a snippet for element type '" + lowerTypeElementType + "'");
        }
    }
    return snippet;
}
Also used : RenderException(org.eclipse.smarthome.ui.basic.render.RenderException) IOException(java.io.IOException) URL(java.net.URL)

Example 2 with RenderException

use of org.eclipse.smarthome.ui.basic.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");
    String subscriptionId = req.getParameter("subscriptionId");
    boolean async = "true".equalsIgnoreCase(req.getParameter("__async"));
    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) {
            showSitemapList(res);
            return;
        }
        logger.debug("reading sitemap {}", sitemap.getName());
        if (widgetId == null || widgetId.isEmpty() || widgetId.equals(sitemapName)) {
            // we are at the homepage, so we render the children of the sitemap root node
            if (subscriptionId != null) {
                if (subscriptions.exists(subscriptionId)) {
                    subscriptions.setPageId(subscriptionId, sitemap.getName(), sitemapName);
                } else {
                    logger.debug("Basic UI requested a non-existing event subscription id ({})", subscriptionId);
                }
            }
            String label = sitemap.getLabel() != null ? sitemap.getLabel() : sitemapName;
            EList<Widget> children = renderer.getItemUIRegistry().getChildren(sitemap);
            result.append(renderer.processPage(sitemapName, 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
            if (subscriptionId != null) {
                if (subscriptions.exists(subscriptionId)) {
                    subscriptions.setPageId(subscriptionId, sitemap.getName(), widgetId);
                } else {
                    logger.debug("Basic UI requested a non-existing event subscription id ({})", subscriptionId);
                }
            }
            Widget w = renderer.getItemUIRegistry().getWidget(sitemap, widgetId);
            if (w != null) {
                String label = renderer.getItemUIRegistry().getLabel(w);
                if (label == null) {
                    label = "undefined";
                }
                if (!(w instanceof LinkableWidget)) {
                    throw new RenderException("Widget '" + w + "' can not have any content");
                }
                EList<Widget> children = renderer.getItemUIRegistry().getChildren((LinkableWidget) w);
                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(CONTENT_TYPE_ASYNC);
    } else {
        res.setContentType(CONTENT_TYPE);
    }
    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.basic.render.RenderException) 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)

Aggregations

RenderException (org.eclipse.smarthome.ui.basic.render.RenderException)2 IOException (java.io.IOException)1 URL (java.net.URL)1 ServletException (javax.servlet.ServletException)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