Search in sources :

Example 1 with VirtualHostMapping

use of com.enonic.xp.web.vhost.impl.mapping.VirtualHostMapping in project xp by enonic.

the class VirtualHostResolverImplTest method testMatches_wrongHost.

@Test
public void testMatches_wrongHost() {
    this.virtualHostMapping = new VirtualHostMapping("mymapping", "foo.no", "/", "/a", VirtualHostIdProvidersMapping.create().build(), 0);
    when(virtualHostService.getVirtualHosts()).thenReturn(List.of(this.virtualHostMapping));
    VirtualHostResolver virtualHostResolver = new VirtualHostResolverImpl(virtualHostService);
    HttpServletRequest req = mock(HttpServletRequest.class);
    when(req.getServerName()).thenReturn("localhost");
    when(req.getRequestURI()).thenReturn("/a/b");
    assertNull(virtualHostResolver.resolveVirtualHost(req));
}
Also used : VirtualHostResolver(com.enonic.xp.web.vhost.VirtualHostResolver) HttpServletRequest(javax.servlet.http.HttpServletRequest) VirtualHostMapping(com.enonic.xp.web.vhost.impl.mapping.VirtualHostMapping) Test(org.junit.jupiter.api.Test)

Example 2 with VirtualHostMapping

use of com.enonic.xp.web.vhost.impl.mapping.VirtualHostMapping in project xp by enonic.

the class VirtualHostFilterTest method addMapping.

private void addMapping() {
    final VirtualHostMapping mapping = new VirtualHostMapping("test", "enonic.com", "/rest", "/admin/rest", VirtualHostIdProvidersMapping.create().build(), 0);
    this.virtualHosts.add(mapping);
}
Also used : VirtualHostMapping(com.enonic.xp.web.vhost.impl.mapping.VirtualHostMapping)

Example 3 with VirtualHostMapping

use of com.enonic.xp.web.vhost.impl.mapping.VirtualHostMapping in project xp by enonic.

the class VirtualHostResolverImplTest method testMatches_wrongSource.

@Test
public void testMatches_wrongSource() {
    this.virtualHostMapping = new VirtualHostMapping("mymapping", "foo.no", "/b", "/a", VirtualHostIdProvidersMapping.create().build(), 0);
    when(virtualHostService.getVirtualHosts()).thenReturn(List.of(this.virtualHostMapping));
    VirtualHostResolver virtualHostResolver = new VirtualHostResolverImpl(virtualHostService);
    HttpServletRequest req = mock(HttpServletRequest.class);
    when(req.getServerName()).thenReturn("foo.no");
    when(req.getRequestURI()).thenReturn("/a");
    assertNull(virtualHostResolver.resolveVirtualHost(req));
}
Also used : VirtualHostResolver(com.enonic.xp.web.vhost.VirtualHostResolver) HttpServletRequest(javax.servlet.http.HttpServletRequest) VirtualHostMapping(com.enonic.xp.web.vhost.impl.mapping.VirtualHostMapping) Test(org.junit.jupiter.api.Test)

Example 4 with VirtualHostMapping

use of com.enonic.xp.web.vhost.impl.mapping.VirtualHostMapping 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 5 with VirtualHostMapping

use of com.enonic.xp.web.vhost.impl.mapping.VirtualHostMapping in project xp by enonic.

the class VirtualHostConfigMap method buildMapping.

private VirtualHostMapping buildMapping(final String name) {
    final String prefix = "mapping." + name + ".";
    final String hostString = getString(prefix + "host");
    final String host = hostString != null ? hostString : "localhost";
    final String source = normalizePath(getString(prefix + "source"));
    final String target = normalizePath(getString(prefix + "target"));
    final VirtualHostIdProvidersMapping idProvidersMapping = getHostIdProvidersMapping(prefix);
    final int order = getInt(prefix + "order", Integer.MAX_VALUE);
    return new VirtualHostMapping(name, host, source, target, idProvidersMapping, order);
}
Also used : VirtualHostMapping(com.enonic.xp.web.vhost.impl.mapping.VirtualHostMapping) VirtualHostIdProvidersMapping(com.enonic.xp.web.vhost.impl.mapping.VirtualHostIdProvidersMapping)

Aggregations

VirtualHostMapping (com.enonic.xp.web.vhost.impl.mapping.VirtualHostMapping)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 Test (org.junit.jupiter.api.Test)5 VirtualHostResolver (com.enonic.xp.web.vhost.VirtualHostResolver)3 VirtualHost (com.enonic.xp.web.vhost.VirtualHost)1 VirtualHostIdProvidersMapping (com.enonic.xp.web.vhost.impl.mapping.VirtualHostIdProvidersMapping)1 RequestDispatcher (javax.servlet.RequestDispatcher)1