use of com.enonic.xp.web.vhost.VirtualHost in project xp by enonic.
the class VirtualHostResolverImplTest method testResolve_notFound.
@Test
public void testResolve_notFound() {
final List<VirtualHost> virtualHosts = new ArrayList<>();
virtualHosts.add(createVirtualHostMapping("a", "localhost", "/", "/other/a", 0));
virtualHosts.add(createVirtualHostMapping("b", "domain.com", "/", "/other/b", 0));
when(virtualHostService.getVirtualHosts()).thenReturn(virtualHosts);
HttpServletRequest req = mock(HttpServletRequest.class);
when(req.getServerName()).thenReturn("foo.no");
final VirtualHostResolver virtualHostResolver = new VirtualHostResolverImpl(virtualHostService);
final VirtualHost mapping = virtualHostResolver.resolveVirtualHost(req);
assertNull(mapping);
}
use of com.enonic.xp.web.vhost.VirtualHost in project xp by enonic.
the class VirtualHostFilter method doHandle.
@Override
protected void doHandle(final HttpServletRequest req, final HttpServletResponse res, final FilterChain chain) throws Exception {
if (virtualHostConfigService.isEnabled()) {
final VirtualHost virtualHost = virtualHostResolver.resolveVirtualHost(req);
if (virtualHost == null) {
LOG.warn("Virtual host mapping could not be resolved for host [" + req.getServerName() + "] and path [" + req.getRequestURI() + "]");
res.setStatus(HttpServletResponse.SC_NOT_FOUND);
} else {
VirtualHostHelper.setVirtualHost(req, virtualHost);
final String targetPath = VirtualHostInternalHelper.getFullTargetPath(virtualHost, req);
final RequestDispatcher dispatcher = req.getRequestDispatcher(targetPath);
dispatcher.forward(req, res);
}
} else {
final VirtualHostMapping defaultVirtualHostMapping = generateDefaultVirtualHostMapping(req);
VirtualHostHelper.setVirtualHost(req, defaultVirtualHostMapping);
chain.doFilter(req, res);
}
}
use of com.enonic.xp.web.vhost.VirtualHost in project xp by enonic.
the class PortalUrlServiceImpl_assetUrlTest method createUrl_withVirtualHost.
@Test
public void createUrl_withVirtualHost() {
final ResourceKey resourceKey = ResourceKey.from(ApplicationKey.from("myapplication"), "META-INF/MANIFEST.MF");
when(this.resourceService.getResource(resourceKey)).thenReturn(MockResource.empty(resourceKey, 1));
final AssetUrlParams params = new AssetUrlParams().portalRequest(this.portalRequest).path("css/my.css");
// Mocks a virtual host and the HTTP request
final VirtualHost virtualHost = mock(VirtualHost.class);
when(req.getAttribute(VirtualHost.class.getName())).thenReturn(virtualHost);
// Calls the method with a virtual mapping /main -> /
when(virtualHost.getSource()).thenReturn("/main");
when(virtualHost.getTarget()).thenReturn("/");
String url = this.service.assetUrl(params);
assertEquals("/main/site/default/draft/_/asset/myapplication:0000000000000001/css/my.css", url);
// Calls the method with a virtual mapping /main -> /site/default/draft/context
when(virtualHost.getSource()).thenReturn("/main");
when(virtualHost.getTarget()).thenReturn("/site");
url = this.service.assetUrl(params);
assertEquals("/main/default/draft/_/asset/myapplication:0000000000000001/css/my.css", url);
// Calls the method with a virtual mapping /main -> /site/default/draft/context
when(virtualHost.getSource()).thenReturn("/main");
when(virtualHost.getTarget()).thenReturn("/site/default/draft");
url = this.service.assetUrl(params);
assertEquals("/main/_/asset/myapplication:0000000000000001/css/my.css", url);
// Calls the method with a virtual mapping / -> /site/default/draft/context
when(virtualHost.getSource()).thenReturn("/");
when(virtualHost.getTarget()).thenReturn("/site/default/draft/context");
url = this.service.assetUrl(params);
assertEquals("/_/asset/myapplication:0000000000000001/css/my.css", url);
// Calls the method with a virtual mapping /main/path -> /site/default/draft/context/path
when(virtualHost.getSource()).thenReturn("/main/path");
when(virtualHost.getTarget()).thenReturn("/site/default/draft/context/path");
url = this.service.assetUrl(params);
assertEquals("/main/path/_/asset/myapplication:0000000000000001/css/my.css", url);
// Calls the method with a virtual mapping /site/default/draft/context/path -> /site/default/draft/context/path
when(virtualHost.getSource()).thenReturn("/site/default/draft/context/path");
when(virtualHost.getTarget()).thenReturn("/site/default/draft/context/path");
url = this.service.assetUrl(params);
assertEquals("/site/default/draft/context/path/_/asset/myapplication:0000000000000001/css/my.css", url);
}
use of com.enonic.xp.web.vhost.VirtualHost in project xp by enonic.
the class PortalUrlServiceImpl_identityUrlTest method createUrl_withVirtualHost.
@Test
public void createUrl_withVirtualHost() {
final IdentityUrlParams params = new IdentityUrlParams().portalRequest(this.portalRequest).idProviderKey(IdProviderKey.system()).idProviderFunction("login");
// Mocks a virtual host and the HTTP request
final VirtualHost virtualHost = Mockito.mock(VirtualHost.class);
when(req.getAttribute(VirtualHost.class.getName())).thenReturn(virtualHost);
// Calls the method with a virtual mapping /main -> /
Mockito.when(virtualHost.getSource()).thenReturn("/main");
Mockito.when(virtualHost.getTarget()).thenReturn("/");
String url = this.service.identityUrl(params);
assertEquals("/main/site/default/draft/_/idprovider/system/login", url);
// Calls the method with a virtual mapping /main -> /site/default/draft/context
Mockito.when(virtualHost.getSource()).thenReturn("/main");
Mockito.when(virtualHost.getTarget()).thenReturn("/site");
url = this.service.identityUrl(params);
assertEquals("/main/default/draft/_/idprovider/system/login", url);
// Calls the method with a virtual mapping /main -> /site/default/draft/context
Mockito.when(virtualHost.getSource()).thenReturn("/main");
Mockito.when(virtualHost.getTarget()).thenReturn("/site/default/draft");
url = this.service.identityUrl(params);
assertEquals("/main/_/idprovider/system/login", url);
// Calls the method with a virtual mapping / -> /site/default/draft/context
Mockito.when(virtualHost.getSource()).thenReturn("/");
Mockito.when(virtualHost.getTarget()).thenReturn("/site/default/draft/context");
url = this.service.identityUrl(params);
assertEquals("/_/idprovider/system/login", url);
// Calls the method with a virtual mapping /main/path -> /site/default/draft/context/path
Mockito.when(virtualHost.getSource()).thenReturn("/main/path");
Mockito.when(virtualHost.getTarget()).thenReturn("/site/default/draft/context/path");
url = this.service.identityUrl(params);
assertEquals("/main/path/_/idprovider/system/login", url);
// Calls the method with a virtual mapping /site/default/draft/context/path -> /site/default/draft/context/path
Mockito.when(virtualHost.getSource()).thenReturn("/site/default/draft/context/path");
Mockito.when(virtualHost.getTarget()).thenReturn("/site/default/draft/context/path");
url = this.service.identityUrl(params);
assertEquals("/site/default/draft/context/path/_/idprovider/system/login", url);
// Post treatment
ServletRequestHolder.setRequest(null);
}
use of com.enonic.xp.web.vhost.VirtualHost in project xp by enonic.
the class ServletRequestUrlHelperTest method rewriteUri_vhost.
@Test
public void rewriteUri_vhost() {
final VirtualHost vhost = mock(VirtualHost.class);
when(req.getAttribute(VirtualHost.class.getName())).thenReturn(vhost);
when(vhost.getTarget()).thenReturn("/");
when(vhost.getSource()).thenReturn("/admin");
final UriRewritingResult rewritingResult = ServletRequestUrlHelper.rewriteUri(req, "/path/to/page");
assertEquals("/admin/path/to/page", rewritingResult.getRewrittenUri());
assertFalse(rewritingResult.isOutOfScope());
when(vhost.getTarget()).thenReturn("/root/to/site");
final UriRewritingResult rewritingResult2 = ServletRequestUrlHelper.rewriteUri(req, "/path/to/page");
assertEquals("/path/to/page", rewritingResult2.getRewrittenUri());
assertTrue(rewritingResult2.isOutOfScope());
when(vhost.getTarget()).thenReturn("/path/to");
final UriRewritingResult rewritingResult3 = ServletRequestUrlHelper.rewriteUri(req, "/path/to/page");
assertEquals("/admin/page", rewritingResult3.getRewrittenUri());
assertFalse(rewritingResult3.isOutOfScope());
}
Aggregations