Search in sources :

Example 1 with VirtualHost

use of com.enonic.xp.web.vhost.VirtualHost in project xp by enonic.

the class GetCurrentIdProviderKeyHandler method retrieveIdProviderKey.

private IdProviderKey retrieveIdProviderKey(final HttpServletRequest req) {
    final IdProvider idProvider = this.request.getIdProvider();
    if (idProvider != null) {
        return idProvider.getKey();
    }
    final VirtualHost virtualHost = VirtualHostHelper.getVirtualHost(req);
    if (virtualHost != null) {
        return virtualHost.getDefaultIdProviderKey();
    }
    return null;
}
Also used : IdProvider(com.enonic.xp.security.IdProvider) VirtualHost(com.enonic.xp.web.vhost.VirtualHost)

Example 2 with VirtualHost

use of com.enonic.xp.web.vhost.VirtualHost in project xp by enonic.

the class IdentityHandler method doHandle.

@Override
protected PortalResponse doHandle(final WebRequest webRequest, final WebResponse webResponse, final WebHandlerChain webHandlerChain) throws Exception {
    final String restPath = findRestPath(webRequest);
    final Matcher matcher = PATTERN.matcher(restPath);
    if (!matcher.find()) {
        throw WebException.notFound("Not a valid idprovider url pattern");
    }
    final IdProviderKey idProviderKey = IdProviderKey.from(matcher.group(ID_PROVIDER_GROUP_INDEX));
    final VirtualHost virtualHost = VirtualHostHelper.getVirtualHost(webRequest.getRawRequest());
    if (!(virtualHost == null || virtualHost.getIdProviderKeys().contains(idProviderKey))) {
        throw WebException.forbidden(String.format("'%s' id provider is forbidden", idProviderKey));
    }
    String idProviderFunction = matcher.group(2);
    final PortalRequest portalRequest = webRequest instanceof PortalRequest ? (PortalRequest) webRequest : new PortalRequest(webRequest);
    portalRequest.setContextPath(findPreRestPath(portalRequest) + "/" + matcher.group(ID_PROVIDER_GROUP_INDEX));
    if (idProviderFunction != null) {
        checkTicket(portalRequest);
    }
    if (idProviderFunction == null) {
        idProviderFunction = webRequest.getMethod().toString().toLowerCase();
    }
    final IdentityHandlerWorker worker = new IdentityHandlerWorker(portalRequest);
    worker.idProviderKey = idProviderKey;
    worker.idProviderFunction = idProviderFunction;
    worker.contentResolver = new ContentResolver(contentService);
    worker.idProviderControllerService = this.idProviderControllerService;
    final Trace trace = Tracer.newTrace("portalRequest");
    if (trace == null) {
        return worker.execute();
    }
    trace.put("path", webRequest.getPath());
    trace.put("method", webRequest.getMethod().toString());
    trace.put("host", webRequest.getHost());
    trace.put("httpRequest", webRequest);
    trace.put("httpResponse", webResponse);
    trace.put("context", ContextAccessor.current());
    return Tracer.traceEx(trace, () -> {
        final PortalResponse response = worker.execute();
        addTraceInfo(trace, response);
        return response;
    });
}
Also used : Trace(com.enonic.xp.trace.Trace) PortalResponse(com.enonic.xp.portal.PortalResponse) Matcher(java.util.regex.Matcher) IdProviderKey(com.enonic.xp.security.IdProviderKey) VirtualHost(com.enonic.xp.web.vhost.VirtualHost) PortalRequest(com.enonic.xp.portal.PortalRequest) ContentResolver(com.enonic.xp.portal.impl.ContentResolver)

Example 3 with VirtualHost

use of com.enonic.xp.web.vhost.VirtualHost in project xp by enonic.

the class IdProviderControllerServiceImplTest method executeWithVirtualHost.

@Test
public void executeWithVirtualHost() throws IOException {
    final HttpServletRequest httpServletRequest = createHttpServletRequest();
    final VirtualHost virtualHost = Mockito.mock(VirtualHost.class);
    Mockito.when(virtualHost.getDefaultIdProviderKey()).thenReturn(IdProviderKey.from("myuserstore"));
    Mockito.when(virtualHost.getTarget()).thenReturn("/");
    Mockito.when(httpServletRequest.getAttribute(VirtualHost.class.getName())).thenReturn(virtualHost);
    VirtualHostHelper.setVirtualHost(httpServletRequest, virtualHost);
    final IdProviderControllerExecutionParams executionParams = IdProviderControllerExecutionParams.create().servletRequest(httpServletRequest).functionName("myfunction").build();
    final PortalResponse portalResponse = idProviderControllerService.execute(executionParams);
    assertNotNull(portalResponse);
    assertEquals(HttpStatus.OK, portalResponse.getStatus());
    assertEquals("myapplication/myfunction", portalResponse.getBody());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) PortalResponse(com.enonic.xp.portal.PortalResponse) VirtualHost(com.enonic.xp.web.vhost.VirtualHost) IdProviderControllerExecutionParams(com.enonic.xp.portal.idprovider.IdProviderControllerExecutionParams) AbstractControllerTest(com.enonic.xp.portal.impl.controller.AbstractControllerTest) Test(org.junit.jupiter.api.Test)

Example 4 with VirtualHost

use of com.enonic.xp.web.vhost.VirtualHost in project xp by enonic.

the class ServletRequestUrlHelper method rewriteUri.

public static UriRewritingResult rewriteUri(final HttpServletRequest req, final String uri) {
    UriRewritingResult.Builder resultBuilder = UriRewritingResult.create().rewrittenUri(uri);
    if (req == null) {
        return resultBuilder.build();
    }
    final VirtualHost vhost = VirtualHostHelper.getVirtualHost(req);
    if (vhost == null) {
        return resultBuilder.build();
    }
    final String targetPath = vhost.getTarget();
    if (uri.startsWith(targetPath)) {
        final String result = uri.substring(targetPath.length());
        final String newUri = normalizePath(vhost.getSource() + ("/".equals(targetPath) ? "/" : "") + result);
        return resultBuilder.rewrittenUri(newUri).deletedUriPrefix(targetPath).newUriPrefix(normalizePath(vhost.getSource())).build();
    }
    return resultBuilder.rewrittenUri(normalizePath(uri)).outOfScope(true).build();
}
Also used : VirtualHost(com.enonic.xp.web.vhost.VirtualHost)

Example 5 with VirtualHost

use of com.enonic.xp.web.vhost.VirtualHost in project xp by enonic.

the class VirtualHostResolverImplTest method testResolve.

@Test
public void testResolve() {
    final List<VirtualHost> virtualHosts = new ArrayList<>();
    virtualHosts.add(createVirtualHostMapping("a", "localhost", "/", "/other/a", 0));
    virtualHosts.add(createVirtualHostMapping("b", "enonic.com ~example\\.(?<cc>.+)", "/", "/other/d", 0));
    when(virtualHostService.getVirtualHosts()).thenReturn(virtualHosts);
    HttpServletRequest req = mock(HttpServletRequest.class);
    when(req.getServerName()).thenReturn("eXampLe.com");
    final VirtualHostResolver virtualHostResolver = new VirtualHostResolverImpl(virtualHostService);
    final VirtualHost mapping = virtualHostResolver.resolveVirtualHost(req);
    assertNotNull(mapping);
    assertEquals("b", mapping.getName());
}
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)

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