@Test public void testRequestWidgetUseCacheImage() { Widget widget = new Widget(); widget.setName("Widget1"); widget.setUrl("/url/widget1"); widget.setCacheType(Widget.TYPE_IMAGE); widget.setLastCacheUpdate(new Date()); Widget sample = new Widget(); sample.setName("Widget1"); when(widgetDao.findByExample(sample, true)).thenReturn(Arrays.asList(widget)); Widget actual = widgetService.requestWidget("/url/widget1", "Widget1", new MockHttpServletRequest()); assertEquals(widget, actual); }
@Override 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; 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..."); return widget; } else { 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); } Map<String, Object> param = new HashMap<String, Object>(); if (StringUtil.isEmpty(IPAddress)) param.put("IPAddress", IPAddress); UrlResponseObject response = WidgetUtil.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()); String html = new String(response.getResponseBody()); String hostname = UrlUtil.getHostname(url); Matcher matcher = pattern.matcher(html); while (matcher.find()) { String imageUrl = matcher.group(1); String cacheUrl = imageUrl; if (!UrlUtil.hasProtocol(cacheUrl)) { if (!imageUrl.startsWith("/")) { cacheUrl = "http: } else { cacheUrl = "http: } } String imageName = this.addCache(cacheUrl, req, widget); 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; } } } return null; }
WidgetServiceImpl extends BaseCrudServiceImpl<Widget> implements
		WidgetService { @Override 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; 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..."); return widget; } else { 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); } Map<String, Object> param = new HashMap<String, Object>(); if (StringUtil.isEmpty(IPAddress)) param.put("IPAddress", IPAddress); UrlResponseObject response = WidgetUtil.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()); String html = new String(response.getResponseBody()); String hostname = UrlUtil.getHostname(url); Matcher matcher = pattern.matcher(html); while (matcher.find()) { String imageUrl = matcher.group(1); String cacheUrl = imageUrl; if (!UrlUtil.hasProtocol(cacheUrl)) { if (!imageUrl.startsWith("/")) { cacheUrl = "http: } else { cacheUrl = "http: } } String imageName = this.addCache(cacheUrl, req, widget); 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; } } } return null; } }
WidgetServiceImpl extends BaseCrudServiceImpl<Widget> implements
		WidgetService { @Override 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; 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..."); return widget; } else { 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); } Map<String, Object> param = new HashMap<String, Object>(); if (StringUtil.isEmpty(IPAddress)) param.put("IPAddress", IPAddress); UrlResponseObject response = WidgetUtil.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()); String html = new String(response.getResponseBody()); String hostname = UrlUtil.getHostname(url); Matcher matcher = pattern.matcher(html); while (matcher.find()) { String imageUrl = matcher.group(1); String cacheUrl = imageUrl; if (!UrlUtil.hasProtocol(cacheUrl)) { if (!imageUrl.startsWith("/")) { cacheUrl = "http: } else { cacheUrl = "http: } } String imageName = this.addCache(cacheUrl, req, widget); 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; } } } return null; }  }
WidgetServiceImpl extends BaseCrudServiceImpl<Widget> implements
		WidgetService { @Override 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; 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..."); return widget; } else { 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); } Map<String, Object> param = new HashMap<String, Object>(); if (StringUtil.isEmpty(IPAddress)) param.put("IPAddress", IPAddress); UrlResponseObject response = WidgetUtil.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()); String html = new String(response.getResponseBody()); String hostname = UrlUtil.getHostname(url); Matcher matcher = pattern.matcher(html); while (matcher.find()) { String imageUrl = matcher.group(1); String cacheUrl = imageUrl; if (!UrlUtil.hasProtocol(cacheUrl)) { if (!imageUrl.startsWith("/")) { cacheUrl = "http: } else { cacheUrl = "http: } } String imageName = this.addCache(cacheUrl, req, widget); 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; } } } return null; }  @Override Widget findByName(String name); @Override Widget findByUrl(String url); @Override List<Widget> getCurrentUserWidgets(); @Override Widget requestWidget(String widgetUrl, String name,
			HttpServletRequest req); @Override int getColumnConfig(); @Override List<Widget> findDefaultWidget(BaseUser user); @Override List<Widget> findWidgetWithAccessCode(List<String> accessCodes); void setIPAddress(String iPAddress); String getWidgetColumn(); void setWidgetColumn(String widgetColumn); }
WidgetServiceImpl extends BaseCrudServiceImpl<Widget> implements
		WidgetService { @Override 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; 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..."); return widget; } else { 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); } Map<String, Object> param = new HashMap<String, Object>(); if (StringUtil.isEmpty(IPAddress)) param.put("IPAddress", IPAddress); UrlResponseObject response = WidgetUtil.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()); String html = new String(response.getResponseBody()); String hostname = UrlUtil.getHostname(url); Matcher matcher = pattern.matcher(html); while (matcher.find()) { String imageUrl = matcher.group(1); String cacheUrl = imageUrl; if (!UrlUtil.hasProtocol(cacheUrl)) { if (!imageUrl.startsWith("/")) { cacheUrl = "http: } else { cacheUrl = "http: } } String imageName = this.addCache(cacheUrl, req, widget); 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; } } } return null; }  @Override Widget findByName(String name); @Override Widget findByUrl(String url); @Override List<Widget> getCurrentUserWidgets(); @Override Widget requestWidget(String widgetUrl, String name,
			HttpServletRequest req); @Override int getColumnConfig(); @Override List<Widget> findDefaultWidget(BaseUser user); @Override List<Widget> findWidgetWithAccessCode(List<String> accessCodes); void setIPAddress(String iPAddress); String getWidgetColumn(); void setWidgetColumn(String widgetColumn);  }