@Test
public void testVanityURLResolving() throws Exception {
JCRSessionWrapper session = JCRSessionFactory.getInstance()
.getCurrentUserSession(null, Locale.ENGLISH);
JahiaSite site = ServicesRegistry.getInstance().getJahiaSitesService().getSiteByKey(TESTSITE_NAME);
JCRNodeWrapper pageNode = session.getNode(SITECONTENT_ROOT_NODE
+ "/testPage");
VanityUrl englishVanityUrl = new VanityUrl("/testpage", TESTSITE_NAME,
"en");
englishVanityUrl.setActive(true);
VanityUrl englishVanityUrl2 = new VanityUrl("/testpage2",
TESTSITE_NAME, "en");
englishVanityUrl2.setActive(false);
VanityUrl englishVanityUrl3 = new VanityUrl("/testpage/page3",
TESTSITE_NAME, "en");
englishVanityUrl3.setActive(true);
VanityUrl frenchVanityUrl = new VanityUrl("/testpage/french",
TESTSITE_NAME, "fr");
frenchVanityUrl.setActive(true);
VanityUrl frenchVanityUrl2 = new VanityUrl("/testpage/french2",
TESTSITE_NAME, "fr");
frenchVanityUrl2.setDefaultMapping(true);
frenchVanityUrl2.setActive(true);
List<VanityUrl> vanityUrls = new ArrayList<VanityUrl>();
vanityUrls.add(englishVanityUrl);
vanityUrls.add(englishVanityUrl2);
vanityUrls.add(englishVanityUrl3);
vanityUrls.add(frenchVanityUrl);
vanityUrls.add(frenchVanityUrl2);
Set<String> languages = new HashSet<String>();
languages.add("en");
languages.add("fr");
getVanityUrlService().saveVanityUrlMappings(pageNode, vanityUrls,
languages);
assertTrue("Wrong URL returned", getVanityUrlService()
.getVanityUrlForWorkspaceAndLocale(pageNode,
Constants.EDIT_WORKSPACE, Locale.ENGLISH).getUrl()
.equals("/testpage"));
assertTrue("Wrong URL returned", getVanityUrlService()
.getVanityUrlForWorkspaceAndLocale(pageNode,
Constants.EDIT_WORKSPACE, Locale.FRENCH).getUrl()
.equals("/testpage/french2"));
URLResolver urlResolver = getUrlResolverFactory().createURLResolver("/edit/default/testpage", site.getServerName(), (HttpServletRequest) new MockHttpServletRequest("GET","/edit/default/testpage"));
JCRNodeWrapper resolvedNode = urlResolver.getNode();
assertEquals("Node should not be returned as edit servlet does not resolve vanity URLs",
"/", resolvedNode.getPath());
urlResolver = getUrlResolverFactory().createURLResolver("/render/live/testpage", site.getServerName(), (HttpServletRequest) new MockHttpServletRequest("GET","/render/live/testpage"));
resolvedNode = urlResolver.getNode();
assertEquals("Node should not be returned as it is not published yet", "/",
resolvedNode.getPath());
languages.clear();
languages.add("en");
ServicesRegistry.getInstance().getJCRPublicationService().publishByMainId(
pageNode.getIdentifier(), Constants.EDIT_WORKSPACE,
Constants.LIVE_WORKSPACE, languages, true, null);
urlResolver = getUrlResolverFactory().createURLResolver("/render/live/testpage", site.getServerName(), (HttpServletRequest) new MockHttpServletRequest("GET","/render/live/testpage"));
resolvedNode = urlResolver.getNode();
assertTrue("Wrong node or language returned", pageNode
.equals(resolvedNode)
&& "en".equals(resolvedNode.getLanguage()));
urlResolver = getUrlResolverFactory().createURLResolver("/render/live/testpage2", site.getServerName(), (HttpServletRequest) new MockHttpServletRequest("GET","/render/live/testpage2"));
resolvedNode = urlResolver.getNode();
assertEquals("Node should not be returned as mapping is not active", "/",
resolvedNode.getPath());
urlResolver = getUrlResolverFactory().createURLResolver("/render/live/testpage/page3", site.getServerName(), (HttpServletRequest) new MockHttpServletRequest("GET","/render/live/testpage/page3"));
resolvedNode = urlResolver.getNode();
assertTrue("Wrong node or language returned", pageNode
.equals(resolvedNode)
&& "en".equals(resolvedNode.getLanguage()));
urlResolver = getUrlResolverFactory().createURLResolver("/render/live/testpage/french2", site.getServerName(), (HttpServletRequest) new MockHttpServletRequest("GET","/render/live/testpage/french2"));
try {
resolvedNode = urlResolver.getNode();
assertTrue("Wrong node or language returned - all vanity urls are published, not just the one of the published language", pageNode
.equals(resolvedNode)
&& "fr".equals(resolvedNode.getLanguage()));
} catch (AccessDeniedException e) {
}
languages.clear();
languages.add("fr");
ServicesRegistry.getInstance().getJCRPublicationService().publishByMainId(
pageNode.getIdentifier(), Constants.EDIT_WORKSPACE,
Constants.LIVE_WORKSPACE, languages, true, null);
urlResolver = getUrlResolverFactory().createURLResolver("/render/live/testpage/french2", site.getServerName(), (HttpServletRequest) new MockHttpServletRequest("GET","/render/live/testpage/french2"));
resolvedNode = urlResolver.getNode();
assertTrue("Wrong node or language returned", pageNode
.equals(resolvedNode)
&& "fr".equals(resolvedNode.getLanguage()));
urlResolver = getUrlResolverFactory().createURLResolver("/render/live/testpage/french", site.getServerName(), (HttpServletRequest) new MockHttpServletRequest("GET","/render/live/testpage/french"));
resolvedNode = urlResolver.getNode();
assertTrue("Wrong node or language returned", pageNode
.equals(resolvedNode)
&& "fr".equals(resolvedNode.getLanguage()));
}