예제 #1
0
  /** {@inheritDoc} */
  @Override
  public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    String snippet = getSnippet("selection");

    snippet = StringUtils.replace(snippet, "%category%", getCategory(w));
    snippet = StringUtils.replace(snippet, "%icon_type%", config.getIconType());
    snippet = StringUtils.replace(snippet, "%state%", getState(w));
    snippet = StringUtils.replace(snippet, "%value_map%", getMappingsJSON((Selection) w));
    snippet = StringUtils.replace(snippet, "%label_header%", getLabel(w));
    snippet = StringUtils.replace(snippet, "%item%", w.getItem() != null ? w.getItem() : "");

    String state = itemUIRegistry.getState(w).toString();
    Selection selection = (Selection) w;
    String mappingLabel = null;

    StringBuilder rowSB = new StringBuilder();
    for (Mapping mapping : selection.getMappings()) {
      String rowSnippet = getSnippet("selection_row");
      String command = mapping.getCmd() != null ? mapping.getCmd() : "";
      rowSnippet =
          StringUtils.replace(rowSnippet, "%item%", w.getItem() != null ? w.getItem() : "");
      rowSnippet = StringUtils.replace(rowSnippet, "%cmd%", escapeHtml(command));
      rowSnippet =
          StringUtils.replace(
              rowSnippet, "%label%", mapping.getLabel() != null ? mapping.getLabel() : "");
      if (state.equals(mapping.getCmd())) {
        mappingLabel = mapping.getLabel();
        rowSnippet = StringUtils.replace(rowSnippet, "%checked%", "checked=\"true\"");
      } else {
        rowSnippet = StringUtils.replace(rowSnippet, "%checked%", "");
      }
      rowSB.append(rowSnippet);
    }
    snippet = StringUtils.replace(snippet, "%rows%", rowSB.toString());
    snippet = StringUtils.replace(snippet, "%value%", mappingLabel != null ? mappingLabel : "");

    // Process the color tags
    snippet = processColor(w, snippet);

    sb.append(snippet);
    return null;
  }
예제 #2
0
  /** {@inheritDoc} */
  public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    Colorpicker cp = (Colorpicker) w;

    String snippetName = "colorpicker";

    String snippet = getSnippet(snippetName);

    // set the default send-update frequency to 200ms
    String frequency = cp.getFrequency() == 0 ? "200" : Integer.toString(cp.getFrequency());

    // get RGB hex value
    State state = itemUIRegistry.getState(cp);
    String hexValue = "#ffffff";
    if (state instanceof HSBType) {
      HSBType hsbState = (HSBType) state;
      Color color = hsbState.toColor();
      hexValue = "#" + Integer.toHexString(color.getRGB()).substring(2);
    }
    String label = getLabel(cp);
    String purelabel = label;
    if (label.contains("<span>")) {
      purelabel = purelabel.substring(0, label.indexOf("<span>"));
    }

    snippet = StringUtils.replace(snippet, "%id%", itemUIRegistry.getWidgetId(cp));
    snippet = StringUtils.replace(snippet, "%icon%", escapeURLPath(itemUIRegistry.getIcon(cp)));
    snippet = StringUtils.replace(snippet, "%item%", w.getItem());
    snippet = StringUtils.replace(snippet, "%label%", label);
    snippet = StringUtils.replace(snippet, "%purelabel%", purelabel);
    snippet = StringUtils.replace(snippet, "%state%", hexValue);
    snippet = StringUtils.replace(snippet, "%frequency%", frequency);
    snippet = StringUtils.replace(snippet, "%servletname%", WebAppServlet.SERVLET_NAME);

    String style = "";
    String color = itemUIRegistry.getLabelColor(w);
    if (color != null) {
      style = "color:" + color;
    }
    snippet = StringUtils.replace(snippet, "%labelstyle%", style);

    style = "";
    color = itemUIRegistry.getValueColor(w);
    if (color != null) {
      style = "color:" + color;
    }
    snippet = StringUtils.replace(snippet, "%valuestyle%", style);

    sb.append(snippet);
    return null;
  }
예제 #3
0
 /**
  * Collects all items that are represented by a given list of widgets
  *
  * @param widgets the widget list to get the items for added to all bundles containing REST
  *     resources
  * @return all items that are represented by the list of widgets
  */
 private Set<GenericItem> getAllItems(EList<Widget> widgets) {
   Set<GenericItem> items = new HashSet<GenericItem>();
   if (itemUIRegistry != null) {
     for (Widget widget : widgets) {
       String itemName = widget.getItem();
       if (itemName != null) {
         try {
           Item item = itemUIRegistry.getItem(itemName);
           if (item instanceof GenericItem) {
             final GenericItem gItem = (GenericItem) item;
             items.add(gItem);
           }
         } catch (ItemNotFoundException e) {
           // ignore
         }
       } else {
         if (widget instanceof Frame) {
           items.addAll(getAllItems(((Frame) widget).getChildren()));
         }
       }
     }
   }
   return items;
 }
예제 #4
0
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String sitemapName = request.getParameter("sitemap");
    String widgetId = request.getParameter("widgetId");

    if (sitemapName == null) {
      throw new ServletException("Parameter 'sitemap' must be provided!");
    }
    if (widgetId == null) {
      throw new ServletException("Parameter 'widget' must be provided!");
    }

    String uriString = null;

    Sitemap sitemap = (Sitemap) modelRepository.getModel(sitemapName);
    if (sitemap != null) {
      Widget widget = itemUIRegistry.getWidget(sitemap, widgetId);
      if (widget instanceof Image) {
        Image image = (Image) widget;
        uriString = image.getUrl();
      } else if (widget instanceof Video) {
        Video video = (Video) widget;
        uriString = video.getUrl();
      } else {
        if (widget == null) {
          throw new ServletException("Widget '" + widgetId + "' could not be found!");
        } else {
          throw new ServletException(
              "Widget type '" + widget.getClass().getName() + "' is not supported!");
        }
      }
    } else {
      throw new ServletException("Sitemap '" + sitemapName + "' could not be found!");
    }

    HttpClient httpClient = new HttpClient();

    try {
      // check if the uri uses credentials and configure the http client accordingly
      URI uri = URI.create(uriString);

      if (uri.getUserInfo() != null) {
        String[] userInfo = uri.getUserInfo().split(":");
        httpClient.getParams().setAuthenticationPreemptive(true);
        Credentials creds = new UsernamePasswordCredentials(userInfo[0], userInfo[1]);
        httpClient
            .getState()
            .setCredentials(
                new AuthScope(uri.getHost(), uri.getPort(), AuthScope.ANY_REALM), creds);
      }
    } catch (IllegalArgumentException e) {
      throw new ServletException("URI '" + uriString + "' is not valid: " + e.getMessage());
    }

    // do the client request
    GetMethod method = new GetMethod(uriString);
    httpClient.executeMethod(method);

    // copy all headers
    for (Header header : method.getResponseHeaders()) {
      response.setHeader(header.getName(), header.getValue());
    }

    // now copy/stream the body content
    IOUtils.copy(method.getResponseBodyAsStream(), response.getOutputStream());
    method.releaseConnection();
  }
예제 #5
0
  private WidgetDTO createWidgetBean(
      String sitemapName,
      Widget widget,
      boolean drillDown,
      URI uri,
      String widgetId,
      Locale locale) {
    // Test visibility
    if (itemUIRegistry.getVisiblity(widget) == false) {
      return null;
    }

    WidgetDTO bean = new WidgetDTO();
    if (widget.getItem() != null) {
      try {
        Item item = itemUIRegistry.getItem(widget.getItem());
        if (item != null) {
          bean.item =
              EnrichedItemDTOMapper.map(item, false, UriBuilder.fromUri(uri).build(), locale);
        }
      } catch (ItemNotFoundException e) {
        logger.debug(e.getMessage());
      }
    }
    bean.widgetId = widgetId;
    bean.icon = itemUIRegistry.getCategory(widget);
    bean.labelcolor = itemUIRegistry.getLabelColor(widget);
    bean.valuecolor = itemUIRegistry.getValueColor(widget);
    bean.label = itemUIRegistry.getLabel(widget);
    bean.type = widget.eClass().getName();
    if (widget instanceof LinkableWidget) {
      LinkableWidget linkableWidget = (LinkableWidget) widget;
      EList<Widget> children = itemUIRegistry.getChildren(linkableWidget);
      if (widget instanceof Frame) {
        int cntWidget = 0;
        for (Widget child : children) {
          widgetId += "_" + cntWidget;
          WidgetDTO subWidget =
              createWidgetBean(sitemapName, child, drillDown, uri, widgetId, locale);
          if (subWidget != null) {
            bean.widgets.add(subWidget);
            cntWidget++;
          }
        }
      } else if (children.size() > 0) {
        String pageName = itemUIRegistry.getWidgetId(linkableWidget);
        bean.linkedPage =
            createPageBean(
                sitemapName,
                itemUIRegistry.getLabel(widget),
                itemUIRegistry.getCategory(widget),
                pageName,
                drillDown ? children : null,
                drillDown,
                isLeaf(children),
                uri,
                locale);
      }
    }
    if (widget instanceof Switch) {
      Switch switchWidget = (Switch) widget;
      for (Mapping mapping : switchWidget.getMappings()) {
        MappingDTO mappingBean = new MappingDTO();
        mappingBean.command = mapping.getCmd();
        mappingBean.label = mapping.getLabel();
        bean.mappings.add(mappingBean);
      }
    }
    if (widget instanceof Selection) {
      Selection selectionWidget = (Selection) widget;
      for (Mapping mapping : selectionWidget.getMappings()) {
        MappingDTO mappingBean = new MappingDTO();
        mappingBean.command = mapping.getCmd();
        mappingBean.label = mapping.getLabel();
        bean.mappings.add(mappingBean);
      }
    }
    if (widget instanceof Slider) {
      Slider sliderWidget = (Slider) widget;
      bean.sendFrequency = sliderWidget.getFrequency();
      bean.switchSupport = sliderWidget.isSwitchEnabled();
    }
    if (widget instanceof List) {
      List listWidget = (List) widget;
      bean.separator = listWidget.getSeparator();
    }
    if (widget instanceof Image) {
      Image imageWidget = (Image) widget;
      String wId = itemUIRegistry.getWidgetId(widget);
      if (uri.getPort() < 0 || uri.getPort() == 80) {
        bean.url =
            uri.getScheme()
                + "://"
                + uri.getHost()
                + "/proxy?sitemap="
                + sitemapName
                + ".sitemap&widgetId="
                + wId;
      } else {
        bean.url =
            uri.getScheme()
                + "://"
                + uri.getHost()
                + ":"
                + uri.getPort()
                + "/proxy?sitemap="
                + sitemapName
                + ".sitemap&widgetId="
                + wId;
      }
      if (imageWidget.getRefresh() > 0) {
        bean.refresh = imageWidget.getRefresh();
      }
    }
    if (widget instanceof Video) {
      Video videoWidget = (Video) widget;
      String wId = itemUIRegistry.getWidgetId(widget);
      if (videoWidget.getEncoding() != null) {
        bean.encoding = videoWidget.getEncoding();
      }
      if (uri.getPort() < 0 || uri.getPort() == 80) {
        bean.url =
            uri.getScheme()
                + "://"
                + uri.getHost()
                + "/proxy?sitemap="
                + sitemapName
                + ".sitemap&widgetId="
                + wId;
      } else {
        bean.url =
            uri.getScheme()
                + "://"
                + uri.getHost()
                + ":"
                + uri.getPort()
                + "/proxy?sitemap="
                + sitemapName
                + ".sitemap&widgetId="
                + wId;
      }
    }
    if (widget instanceof Webview) {
      Webview webViewWidget = (Webview) widget;
      bean.url = webViewWidget.getUrl();
      bean.height = webViewWidget.getHeight();
    }
    if (widget instanceof Mapview) {
      Mapview mapViewWidget = (Mapview) widget;
      bean.height = mapViewWidget.getHeight();
    }
    if (widget instanceof Chart) {
      Chart chartWidget = (Chart) widget;
      bean.service = chartWidget.getService();
      bean.period = chartWidget.getPeriod();
      if (chartWidget.getRefresh() > 0) {
        bean.refresh = chartWidget.getRefresh();
      }
    }
    if (widget instanceof Setpoint) {
      Setpoint setpointWidget = (Setpoint) widget;
      bean.minValue = setpointWidget.getMinValue();
      bean.maxValue = setpointWidget.getMaxValue();
      bean.step = setpointWidget.getStep();
    }
    return bean;
  }
예제 #6
0
 private PageDTO getPageBean(String sitemapName, String pageId, URI uri, Locale locale) {
   Sitemap sitemap = getSitemap(sitemapName);
   if (sitemap != null) {
     if (pageId.equals(sitemap.getName())) {
       return createPageBean(
           sitemapName,
           sitemap.getLabel(),
           sitemap.getIcon(),
           sitemap.getName(),
           sitemap.getChildren(),
           false,
           isLeaf(sitemap.getChildren()),
           uri,
           locale);
     } else {
       Widget pageWidget = itemUIRegistry.getWidget(sitemap, pageId);
       if (pageWidget instanceof LinkableWidget) {
         EList<Widget> children = itemUIRegistry.getChildren((LinkableWidget) pageWidget);
         PageDTO pageBean =
             createPageBean(
                 sitemapName,
                 itemUIRegistry.getLabel(pageWidget),
                 itemUIRegistry.getCategory(pageWidget),
                 pageId,
                 children,
                 false,
                 isLeaf(children),
                 uri,
                 locale);
         EObject parentPage = pageWidget.eContainer();
         while (parentPage instanceof Frame) {
           parentPage = parentPage.eContainer();
         }
         if (parentPage instanceof Widget) {
           String parentId = itemUIRegistry.getWidgetId((Widget) parentPage);
           pageBean.parent = getPageBean(sitemapName, parentId, uri, locale);
           pageBean.parent.widgets = null;
           pageBean.parent.parent = null;
         } else if (parentPage instanceof Sitemap) {
           pageBean.parent = getPageBean(sitemapName, sitemap.getName(), uri, locale);
           pageBean.parent.widgets = null;
         }
         return pageBean;
       } else {
         if (logger.isDebugEnabled()) {
           if (pageWidget == null) {
             logger.debug(
                 "Received HTTP GET request at '{}' for the unknown page id '{}'.", uri, pageId);
           } else {
             logger.debug(
                 "Received HTTP GET request at '{}' for the page id '{}'. "
                     + "This id refers to a non-linkable widget and is therefore no valid page id.",
                 uri,
                 pageId);
           }
         }
         throw new WebApplicationException(404);
       }
     }
   } else {
     logger.info(
         "Received HTTP GET request at '{}' for the unknown sitemap '{}'.", uri, sitemapName);
     throw new WebApplicationException(404);
   }
 }