Package org.opentides.bean

Examples of org.opentides.bean.Widget


      HttpServletResponse res) throws Exception {
    String name = req.getParameter("name");
    Map<String, Object> model = new HashMap<String, Object>();
    if (!StringUtil.isEmpty(name)) {
      String widgetUrl = req.getRequestURL().toString();
      Widget widget = widgetService
          .requestWidget(widgetUrl, name, req);
      if (widget != null) {
        if (widget.getCacheType().startsWith(
            Widget.TYPE_IMAGE)) {
          // return as image
          res.setContentType(widget.getCacheType());
          OutputStream outputStream = res.getOutputStream();
          outputStream.write(widget.getCache());
          outputStream.flush();
          return null;
        }
        model.put("body", new String(widget.getCache()));
        model.put("cacheDate", widget.getLastCacheUpdate());
      } else {
        model.put("body", "Unable to load widget. Name [" + name
            + "] not found");
        model.put("cacheDate", new Date());
      }
View Full Code Here


      throws Exception {
    String action = getAction(request);
    if (READ.equals(action) || UPDATE.equals(action)) {
      return getService().load(request.getParameter(ID));
    } else {
      Widget object = new Widget();
      object.setCacheDuration(3600);
      object.setIsUserDefined(true);
      return object;
    }
  }
View Full Code Here

   *
   * @see org.springframework.validation.Validator#validate(java.lang.Object,
   *      org.springframework.validation.Errors)
   */
  public void validate(Object clazz, Errors e) {
    Widget widget = (Widget) clazz;

    if(!StringUtil.isEmpty(widget.getName()) && isDuplicateName(widget))
      e.reject("error.duplicate-name", new Object[]{"\""+widget.getName()+"\"","name"}, "\""+widget.getName() +"\" already exists. Please try a different name.");
 
    if(!StringUtil.isEmpty(widget.getTitle()) && isDuplicateUrl(widget))
      e.reject("error.duplicate-url", new Object[]{"\""+widget.getUrl()+"\"","url"}, "\""+widget.getUrl() +"\" already exists. Please update the existing url.");
   
   
    ValidationUtils.rejectIfEmpty(e, "name", "error.required", new Object[] { "Name" });
    ValidationUtils.rejectIfEmpty(e, "title", "error.required", new Object[] { "Title" });
    ValidationUtils.rejectIfEmptyOrWhitespace(e, "url", "error.required", new Object[] { "Url" });   
View Full Code Here

   *
   * @param fieldName name of field
   * @return boolean returns true if duplicate name was found, false otherwise
   */
  public boolean isDuplicateName(Widget widget){
    Widget key;
    try {
      key = this.widgetService.findByName(widget.getName());
      if (key != null){
        if(!key.getId().equals(widget.getId()))
          return true;
      }
    } catch (EntityNotFoundException e) {
      return false;
    }
View Full Code Here

   *
   * @param fieldName name of field
   * @return boolean returns true if duplicate name was found, false otherwise
   */
  public boolean isDuplicateUrl(Widget widget){
    Widget key;
    try {
      key = this.widgetService.findByUrl(widget.getUrl());
      if (key != null){
        if (!key.getId().equals(widget.getId()))
          return true;
      }
    } catch (EntityNotFoundException e) {
      return false;
    }
View Full Code Here

  private static Pattern pattern = Pattern.compile("<img\\s[^>]*src=\"?(.*?)[\" ]",
      Pattern.CASE_INSENSITIVE|Pattern.MULTILINE);
 
  @Transactional(readOnly = true)
  public Widget findByName(String name) {
    Widget example = new Widget();
    example.setName(name);
    List<Widget> dashboardList = getDao().findByExample(example, true);
    if (dashboardList != null && dashboardList.size() > 0) {
      return dashboardList.get(0);
    }
    return null;
View Full Code Here

    return null;
  }
 
  @Transactional(readOnly = true)
  public Widget findByUrl(String url) {
    Widget example = new Widget();
    example.setUrl(url);
    List<Widget> dashboardList = getDao().findByExample(example, true);
    if (dashboardList != null && dashboardList.size() > 0) {
      return dashboardList.get(0);
    }
    return null;
View Full Code Here

   * @see
   * org.opentides.service.WidgetService#requestWidget(java.lang
   * .String)
   */
  public Widget requestWidget(String widgetUrl, String name, HttpServletRequest req) {
    Widget widget = findByName(name);
    if (widget != null) {
      long now = System.currentTimeMillis();
      Date lastCacheDate = widget.getLastCacheUpdate();

      boolean useCache = false;
      // check if we should use cache or not
      if (lastCacheDate != null) {
        long expire = widget.getLastCacheUpdate().getTime()
            + widget.getCacheDuration() * 1000;
        if (widget.getCacheType().startsWith(
            Widget.TYPE_IMAGE)
            || now < expire)
          useCache = true;
      }
      if (useCache) {
        _log.debug("Reusing widget [" + widget.getName()
            + "] from cache...");
        // retrieve from cache
        return widget;
      } else {
        // retrieve from url
        String url = widget.getUrl();
        if (!UrlUtil.hasProtocol(url)) {
          String slash = "/";
          if (!url.startsWith("/")) {
            url = slash + url;
          }
          url = req.getContextPath().toString() + url;
          if (req.getServerPort() != 80) {
            url = ":" + Integer.toString(req.getServerPort()) + url;
          }
          url = UrlUtil.ensureProtocol(req.getServerName() + url);
        }
       
        // set the IP Address as param
        Map<String, Object> param = new HashMap<String, Object>();       
        if (StringUtil.isEmpty(IPAddress))
          param.put("IPAddress", IPAddress);
       
        UrlResponseObject response = UrlUtil.getPage(url, req, param);
        if (response==null)
          return null;
        if (response.getResponseType()
            .startsWith(Widget.TYPE_IMAGE)) {
          _log.debug("Retrieving image [" + widget.getName()
              + "] from url [" + widget.getUrl() + "]...");
          widget.setCacheType(response.getResponseType());
          widget.setCache(response.getResponseBody());
          widget.setLastCacheUpdate(new Date());
          save(widget);
          _log.debug("Saved image [" + widget.getName()
              + "] to cache...");
          return widget;
        } else {
          _log.debug("Retrieving widget [" + widget.getName()
              + "] from url [" + widget.getUrl() + "]...");
          widget.setCacheType(response.getResponseType());
          // check for image inside the html
          String html = new String(response.getResponseBody());
          String hostname = UrlUtil.getHostname(url);
          Matcher matcher = pattern.matcher(html);
          while (matcher.find()) {
            // add image link to cache
            String imageUrl = matcher.group(1);
            String cacheUrl = imageUrl;
            if (!UrlUtil.hasProtocol(cacheUrl)) {
              if (!imageUrl.startsWith("/")) {
                cacheUrl = "http://" + hostname + "/" + imageUrl;
              } else {
                cacheUrl = "http://" + hostname  + imageUrl;
              }
            }
            String imageName = this.addCache(cacheUrl, req, widget);
            // replace html that reference to image with cached image
            String newUrl = widgetUrl+"?name="+imageName;
            html = html.replace(imageUrl, newUrl);
          }
          widget.setCache(html.getBytes());
          widget.setLastCacheUpdate(new Date());
          save(widget);
          _log.debug("Saved widget [" + widget.getName()
              + "] to cache...");
          return widget;
        }
      }
    }
View Full Code Here

   *
   * @param imageUrl
   * @return
   */
  private String addCache(String url, HttpServletRequest req, Widget parentSettings) {
    Widget settings = this.findByUrl(url);
    if (settings == null)
      settings = new Widget(url, parentSettings);
    UrlResponseObject response = UrlUtil.getPage(url, req, null);
    if (response.getResponseType().startsWith(Widget.TYPE_IMAGE))
      settings.setCacheType(Widget.TYPE_IMAGE);
    else
      settings.setCacheType(Widget.TYPE_HTML);
    settings.setCache(response.getResponseBody());
    this.save(settings);
    settings.setName(""+settings.getId());
    this.save(settings);
    return settings.getName();
  }
View Full Code Here

   * to the current user.
   * @return
   */
  @Transactional(readOnly=true)
  public List<Widget> getCurrentUserWidgets() {
    Widget example = new Widget();
    example.setIsUserDefined(true);
    List<Widget> widgets = new ArrayList<Widget>();
    for (Widget widget:findByExample(example, true)) {
      if (StringUtil.isEmpty(widget.getAccessCode()))
        widgets.add(widget);
      else if (SecurityUtil.currentUserHasPermission(widget.getAccessCode()))
View Full Code Here

TOP

Related Classes of org.opentides.bean.Widget

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.