@Test public void testPreviewProduct() throws IOException { when(request.getHeader(HttpHeaders.REFERER)).thenReturn("/editor.html/path/to/valid/product/page.html"); when(requestPathInfo.getSelectors()).thenReturn(new String[] { "previewproduct" }); Resource resource = mock(Resource.class); when(resourceResolver.resolve("/path/to/valid/product/page.html")).thenReturn(resource); when(resource.adaptTo(Page.class)).thenReturn(mock(Page.class)); String PREVIEW_PRODUCT_URL = "/dummy/preview/url.key.html#variant_key"; when(urlProvider.toProductUrl(any(), any(), any())).thenReturn(PREVIEW_PRODUCT_URL); when(request.getParameter(UrlProvider.ID_PARAM)).thenReturn("testId"); when(request.getParameter(UrlProvider.SKU_PARAM)).thenReturn("testSku"); when(request.getParameter(UrlProvider.URL_KEY_PARAM)).thenReturn("testUrlKey"); when(request.getParameter(UrlProvider.VARIANT_SKU_PARAM)).thenReturn("testVariantSku"); when(request.getParameter(UrlProvider.VARIANT_URL_KEY_PARAM)).thenReturn("testVariantUrlKey"); when(request.getParameter(UrlProvider.URL_PATH_PARAM)).thenReturn("testUrlPath"); when(request.getParameter("invalidParam")).thenReturn("invalidValue"); servlet.doGet(request, response); ArgumentCaptor<Map> captor = ArgumentCaptor.forClass(Map.class); verify(urlProvider).toProductUrl(any(), any(), captor.capture()); Map<String, String> params = captor.getValue(); Assert.assertEquals("Incorrect number of parameters", 6, params.size()); Assert.assertEquals("Incorrect ID parameter", "testId", params.get(UrlProvider.ID_PARAM)); Assert.assertEquals("Incorrect SKU parameter", "testSku", params.get(UrlProvider.SKU_PARAM)); Assert.assertEquals("Incorrect URL_KEY parameter", "testUrlKey", params.get(UrlProvider.URL_KEY_PARAM)); Assert.assertEquals("Incorrect VARIANT_SKU parameter", "testVariantSku", params.get(UrlProvider.VARIANT_SKU_PARAM)); Assert.assertEquals("Incorrect VARIANT_URL_KEY parameter", "testVariantUrlKey", params.get(UrlProvider.VARIANT_URL_KEY_PARAM)); Assert.assertEquals("Incorrect URL_PATH parameter", "testUrlPath", params.get(UrlProvider.URL_PATH_PARAM)); verify(response).sendRedirect(PREVIEW_PRODUCT_URL); }
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException { if (WCMMode.DISABLED == WCMMode.fromRequest(request)) { response.sendError(HttpServletResponse.SC_FORBIDDEN, "The request not permitted"); return; } Page page = getRefererPage(request); if (page == null) { LOGGER.warn("The path of the edited page cannot be determined"); response.sendError(HttpServletResponse.SC_NOT_FOUND, "The path of the edited page cannot be determined"); return; } Map<String, String> params = new UrlProvider.ParamsBuilder() .id(request.getParameter((UrlProvider.ID_PARAM))) .sku(request.getParameter(UrlProvider.SKU_PARAM)) .variantSku(request.getParameter(UrlProvider.VARIANT_SKU_PARAM)) .urlKey(request.getParameter(UrlProvider.URL_KEY_PARAM)) .urlPath(request.getParameter(UrlProvider.URL_PATH_PARAM)) .variantUrlKey(request.getParameter(UrlProvider.VARIANT_URL_KEY_PARAM)) .map(); String previewSelector = request.getRequestPathInfo().getSelectors()[0]; String url = PREVIEW_PRODUCT.equals(previewSelector) ? urlProvider.toProductUrl(request, page, params) : urlProvider.toCategoryUrl(request, page, params); response.sendRedirect(url); }
PreviewServlet extends SlingSafeMethodsServlet { protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException { if (WCMMode.DISABLED == WCMMode.fromRequest(request)) { response.sendError(HttpServletResponse.SC_FORBIDDEN, "The request not permitted"); return; } Page page = getRefererPage(request); if (page == null) { LOGGER.warn("The path of the edited page cannot be determined"); response.sendError(HttpServletResponse.SC_NOT_FOUND, "The path of the edited page cannot be determined"); return; } Map<String, String> params = new UrlProvider.ParamsBuilder() .id(request.getParameter((UrlProvider.ID_PARAM))) .sku(request.getParameter(UrlProvider.SKU_PARAM)) .variantSku(request.getParameter(UrlProvider.VARIANT_SKU_PARAM)) .urlKey(request.getParameter(UrlProvider.URL_KEY_PARAM)) .urlPath(request.getParameter(UrlProvider.URL_PATH_PARAM)) .variantUrlKey(request.getParameter(UrlProvider.VARIANT_URL_KEY_PARAM)) .map(); String previewSelector = request.getRequestPathInfo().getSelectors()[0]; String url = PREVIEW_PRODUCT.equals(previewSelector) ? urlProvider.toProductUrl(request, page, params) : urlProvider.toCategoryUrl(request, page, params); response.sendRedirect(url); } }
PreviewServlet extends SlingSafeMethodsServlet { protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException { if (WCMMode.DISABLED == WCMMode.fromRequest(request)) { response.sendError(HttpServletResponse.SC_FORBIDDEN, "The request not permitted"); return; } Page page = getRefererPage(request); if (page == null) { LOGGER.warn("The path of the edited page cannot be determined"); response.sendError(HttpServletResponse.SC_NOT_FOUND, "The path of the edited page cannot be determined"); return; } Map<String, String> params = new UrlProvider.ParamsBuilder() .id(request.getParameter((UrlProvider.ID_PARAM))) .sku(request.getParameter(UrlProvider.SKU_PARAM)) .variantSku(request.getParameter(UrlProvider.VARIANT_SKU_PARAM)) .urlKey(request.getParameter(UrlProvider.URL_KEY_PARAM)) .urlPath(request.getParameter(UrlProvider.URL_PATH_PARAM)) .variantUrlKey(request.getParameter(UrlProvider.VARIANT_URL_KEY_PARAM)) .map(); String previewSelector = request.getRequestPathInfo().getSelectors()[0]; String url = PREVIEW_PRODUCT.equals(previewSelector) ? urlProvider.toProductUrl(request, page, params) : urlProvider.toCategoryUrl(request, page, params); response.sendRedirect(url); }  }
PreviewServlet extends SlingSafeMethodsServlet { protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException { if (WCMMode.DISABLED == WCMMode.fromRequest(request)) { response.sendError(HttpServletResponse.SC_FORBIDDEN, "The request not permitted"); return; } Page page = getRefererPage(request); if (page == null) { LOGGER.warn("The path of the edited page cannot be determined"); response.sendError(HttpServletResponse.SC_NOT_FOUND, "The path of the edited page cannot be determined"); return; } Map<String, String> params = new UrlProvider.ParamsBuilder() .id(request.getParameter((UrlProvider.ID_PARAM))) .sku(request.getParameter(UrlProvider.SKU_PARAM)) .variantSku(request.getParameter(UrlProvider.VARIANT_SKU_PARAM)) .urlKey(request.getParameter(UrlProvider.URL_KEY_PARAM)) .urlPath(request.getParameter(UrlProvider.URL_PATH_PARAM)) .variantUrlKey(request.getParameter(UrlProvider.VARIANT_URL_KEY_PARAM)) .map(); String previewSelector = request.getRequestPathInfo().getSelectors()[0]; String url = PREVIEW_PRODUCT.equals(previewSelector) ? urlProvider.toProductUrl(request, page, params) : urlProvider.toCategoryUrl(request, page, params); response.sendRedirect(url); }   }
PreviewServlet extends SlingSafeMethodsServlet { protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException { if (WCMMode.DISABLED == WCMMode.fromRequest(request)) { response.sendError(HttpServletResponse.SC_FORBIDDEN, "The request not permitted"); return; } Page page = getRefererPage(request); if (page == null) { LOGGER.warn("The path of the edited page cannot be determined"); response.sendError(HttpServletResponse.SC_NOT_FOUND, "The path of the edited page cannot be determined"); return; } Map<String, String> params = new UrlProvider.ParamsBuilder() .id(request.getParameter((UrlProvider.ID_PARAM))) .sku(request.getParameter(UrlProvider.SKU_PARAM)) .variantSku(request.getParameter(UrlProvider.VARIANT_SKU_PARAM)) .urlKey(request.getParameter(UrlProvider.URL_KEY_PARAM)) .urlPath(request.getParameter(UrlProvider.URL_PATH_PARAM)) .variantUrlKey(request.getParameter(UrlProvider.VARIANT_URL_KEY_PARAM)) .map(); String previewSelector = request.getRequestPathInfo().getSelectors()[0]; String url = PREVIEW_PRODUCT.equals(previewSelector) ? urlProvider.toProductUrl(request, page, params) : urlProvider.toCategoryUrl(request, page, params); response.sendRedirect(url); }    }