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;
}
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;
});
}
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());
}
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();
}
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());
}
Aggregations