Search in sources :

Example 11 with VirtualHost

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) VirtualHostResolver(com.enonic.xp.web.vhost.VirtualHostResolver) ArrayList(java.util.ArrayList) VirtualHost(com.enonic.xp.web.vhost.VirtualHost) Test(org.junit.jupiter.api.Test)

Example 12 with VirtualHost

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);
    }
}
Also used : VirtualHostMapping(com.enonic.xp.web.vhost.impl.mapping.VirtualHostMapping) VirtualHost(com.enonic.xp.web.vhost.VirtualHost) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 13 with VirtualHost

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);
}
Also used : VirtualHost(com.enonic.xp.web.vhost.VirtualHost) ResourceKey(com.enonic.xp.resource.ResourceKey) AssetUrlParams(com.enonic.xp.portal.url.AssetUrlParams) Test(org.junit.jupiter.api.Test)

Example 14 with VirtualHost

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);
}
Also used : VirtualHost(com.enonic.xp.web.vhost.VirtualHost) IdentityUrlParams(com.enonic.xp.portal.url.IdentityUrlParams) Test(org.junit.jupiter.api.Test)

Example 15 with VirtualHost

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());
}
Also used : VirtualHost(com.enonic.xp.web.vhost.VirtualHost) Test(org.junit.jupiter.api.Test)

Aggregations

VirtualHost (com.enonic.xp.web.vhost.VirtualHost)32 Test (org.junit.jupiter.api.Test)27 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 VirtualHostResolver (com.enonic.xp.web.vhost.VirtualHostResolver)8 ArrayList (java.util.ArrayList)8 VirtualHostServiceImpl (com.enonic.xp.web.vhost.impl.config.VirtualHostServiceImpl)6 BaseHandlerTest (com.enonic.xp.web.handler.BaseHandlerTest)3 PortalResponse (com.enonic.xp.portal.PortalResponse)2 IdProviderKey (com.enonic.xp.security.IdProviderKey)2 WebResponse (com.enonic.xp.web.WebResponse)2 HashMap (java.util.HashMap)2 RequestDispatcher (javax.servlet.RequestDispatcher)2 PortalRequest (com.enonic.xp.portal.PortalRequest)1 IdProviderControllerExecutionParams (com.enonic.xp.portal.idprovider.IdProviderControllerExecutionParams)1 ContentResolver (com.enonic.xp.portal.impl.ContentResolver)1 AbstractControllerTest (com.enonic.xp.portal.impl.controller.AbstractControllerTest)1 AssetUrlParams (com.enonic.xp.portal.url.AssetUrlParams)1 GenerateUrlParams (com.enonic.xp.portal.url.GenerateUrlParams)1 IdentityUrlParams (com.enonic.xp.portal.url.IdentityUrlParams)1 ResourceKey (com.enonic.xp.resource.ResourceKey)1