ProxyServletService extends HttpServlet { URI uriFromRequest(HttpServletRequest request) { try { URI uri = (URI) request.getAttribute(ATTR_URI); if (uri != null) { return uri; } else { ProxyServletException pse = (ProxyServletException) request.getAttribute(ATTR_SERVLET_EXCEPTION); if (pse != null) { return null; } } String sitemapName = request.getParameter("sitemap"); if (sitemapName == null) { throw new ProxyServletException(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'sitemap' must be provided!"); } String widgetId = request.getParameter("widgetId"); if (widgetId == null) { throw new ProxyServletException(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'widgetId' must be provided!"); } Sitemap sitemap = (Sitemap) modelRepository.getModel(sitemapName); if (sitemap == null) { throw new ProxyServletException(HttpServletResponse.SC_NOT_FOUND, String.format("Sitemap '%s' could not be found!", sitemapName)); } Widget widget = itemUIRegistry.getWidget(sitemap, widgetId); if (widget == null) { throw new ProxyServletException(HttpServletResponse.SC_NOT_FOUND, String.format("Widget '%s' could not be found!", widgetId)); } String uriString = null; if (widget instanceof Image) { uriString = ((Image) widget).getUrl(); } else if (widget instanceof Video) { uriString = ((Video) widget).getUrl(); } else { throw new ProxyServletException(HttpServletResponse.SC_FORBIDDEN, String.format("Widget type '%s' is not supported!", widget.getClass().getName())); } String itemName = widget.getItem(); if (itemName != null) { State state = itemUIRegistry.getItemState(itemName); if (state != null && state instanceof StringType) { try { uri = createURIFromString(state.toString()); request.setAttribute(ATTR_URI, uri); return uri; } catch (MalformedURLException | URISyntaxException ex) { } } } try { uri = createURIFromString(uriString); request.setAttribute(ATTR_URI, uri); return uri; } catch (MalformedURLException | URISyntaxException ex) { throw new ProxyServletException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, String.format("URL '%s' is not a valid URL.", uriString)); } } catch (ProxyServletException pse) { request.setAttribute(ATTR_SERVLET_EXCEPTION, pse); return null; } }   static final String PROXY_ALIAS; static final String ATTR_URI; static final String ATTR_SERVLET_EXCEPTION; }
@Test public void testProxyUriUnexpectedWidgetType() { when(request.getParameter(eq("widgetId"))).thenReturn(SWITCH_WIDGET_ID); URI uri = service.uriFromRequest(request); assertNull(uri); }
@Test public void testProxyUriImageWithoutItemButValidUrl() { when(request.getParameter(eq("widgetId"))).thenReturn(IMAGE_WIDGET_ID); when(imageWidget.getUrl()).thenReturn(VALID_IMAGE_URL); when(imageWidget.getItem()).thenReturn(null); URI uri = service.uriFromRequest(request); assertNotNull(uri); assertEquals(uri.toString(), VALID_IMAGE_URL); }
@Test public void testProxyUriImageWithoutItemAndInvalidUrl() { when(request.getParameter(eq("widgetId"))).thenReturn(IMAGE_WIDGET_ID); when(imageWidget.getUrl()).thenReturn(INVALID_URL); when(imageWidget.getItem()).thenReturn(null); URI uri = service.uriFromRequest(request); assertNull(uri); }
@Test public void testProxyUriImageWithItemButUndefState() { when(request.getParameter(eq("widgetId"))).thenReturn(IMAGE_WIDGET_ID); when(imageWidget.getUrl()).thenReturn(VALID_IMAGE_URL); when(imageWidget.getItem()).thenReturn(ITEM_NAME_UNDEF_STATE); URI uri = service.uriFromRequest(request); assertNotNull(uri); assertEquals(uri.toString(), VALID_IMAGE_URL); }
@Test public void testProxyUriImageWithItemButNullState() { when(request.getParameter(eq("widgetId"))).thenReturn(IMAGE_WIDGET_ID); when(imageWidget.getUrl()).thenReturn(VALID_IMAGE_URL); when(imageWidget.getItem()).thenReturn(ITEM_NAME_NULL_STATE); URI uri = service.uriFromRequest(request); assertNotNull(uri); assertEquals(uri.toString(), VALID_IMAGE_URL); }
@Test public void testProxyUriImageWithItemButUnexpectedState() { when(request.getParameter(eq("widgetId"))).thenReturn(IMAGE_WIDGET_ID); when(imageWidget.getUrl()).thenReturn(VALID_IMAGE_URL); when(imageWidget.getItem()).thenReturn(ITEM_NAME_ON_STATE); URI uri = service.uriFromRequest(request); assertNotNull(uri); assertEquals(uri.toString(), VALID_IMAGE_URL); }
@Test public void testProxyUriImageWithItemButStateWithInvalidUrl() { when(request.getParameter(eq("widgetId"))).thenReturn(IMAGE_WIDGET_ID); when(imageWidget.getUrl()).thenReturn(VALID_IMAGE_URL); when(imageWidget.getItem()).thenReturn(ITEM_NAME_INVALID_URL); URI uri = service.uriFromRequest(request); assertNotNull(uri); assertEquals(uri.toString(), VALID_IMAGE_URL); }
@Test public void testProxyUriImageWithItemAndStateWithValidUrl() { when(request.getParameter(eq("widgetId"))).thenReturn(IMAGE_WIDGET_ID); when(imageWidget.getUrl()).thenReturn(VALID_IMAGE_URL); when(imageWidget.getItem()).thenReturn(ITEM_NAME_VALID_IMAGE_URL); URI uri = service.uriFromRequest(request); assertNotNull(uri); assertEquals(uri.toString(), ITEM_VALID_IMAGE_URL); }
@Test public void testProxyUriVideoWithoutItemButValidUrl() { when(request.getParameter(eq("widgetId"))).thenReturn(VIDEO_WIDGET_ID); when(videoWidget.getUrl()).thenReturn(VALID_VIDEO_URL); when(videoWidget.getItem()).thenReturn(null); URI uri = service.uriFromRequest(request); assertNotNull(uri); assertEquals(uri.toString(), VALID_VIDEO_URL); }
@Test public void testProxyUriVideoWithoutItemAndInvalidUrl() { when(request.getParameter(eq("widgetId"))).thenReturn(VIDEO_WIDGET_ID); when(videoWidget.getUrl()).thenReturn(INVALID_URL); when(videoWidget.getItem()).thenReturn(null); URI uri = service.uriFromRequest(request); assertNull(uri); }
@Test public void testProxyUriVideoWithItemButUndefState() { when(request.getParameter(eq("widgetId"))).thenReturn(VIDEO_WIDGET_ID); when(videoWidget.getUrl()).thenReturn(VALID_VIDEO_URL); when(videoWidget.getItem()).thenReturn(ITEM_NAME_UNDEF_STATE); URI uri = service.uriFromRequest(request); assertNotNull(uri); assertEquals(uri.toString(), VALID_VIDEO_URL); }
@Test public void testProxyUriVideoWithItemButNullState() { when(request.getParameter(eq("widgetId"))).thenReturn(VIDEO_WIDGET_ID); when(videoWidget.getUrl()).thenReturn(VALID_VIDEO_URL); when(videoWidget.getItem()).thenReturn(ITEM_NAME_NULL_STATE); URI uri = service.uriFromRequest(request); assertNotNull(uri); assertEquals(uri.toString(), VALID_VIDEO_URL); }
@Test public void testProxyUriVideoWithItemButUnexpectedState() { when(request.getParameter(eq("widgetId"))).thenReturn(VIDEO_WIDGET_ID); when(videoWidget.getUrl()).thenReturn(VALID_VIDEO_URL); when(videoWidget.getItem()).thenReturn(ITEM_NAME_ON_STATE); URI uri = service.uriFromRequest(request); assertNotNull(uri); assertEquals(uri.toString(), VALID_VIDEO_URL); }
@Test public void testProxyUriVideoWithItemButStateWithInvalidUrl() { when(request.getParameter(eq("widgetId"))).thenReturn(VIDEO_WIDGET_ID); when(videoWidget.getUrl()).thenReturn(VALID_VIDEO_URL); when(videoWidget.getItem()).thenReturn(ITEM_NAME_INVALID_URL); URI uri = service.uriFromRequest(request); assertNotNull(uri); assertEquals(uri.toString(), VALID_VIDEO_URL); }
@Test public void testProxyUriVideoWithItemAndStateWithValidUrl() { when(request.getParameter(eq("widgetId"))).thenReturn(VIDEO_WIDGET_ID); when(videoWidget.getUrl()).thenReturn(VALID_VIDEO_URL); when(videoWidget.getItem()).thenReturn(ITEM_NAME_VALID_VIDEO_URL); URI uri = service.uriFromRequest(request); assertNotNull(uri); assertEquals(uri.toString(), ITEM_VALID_VIDEO_URL); }