Is it possible to get vhost mappings in Java-part of XP app?

Enonic version: 6.12.2
OS: Windows 10

I am fiddling around with some stuff in a Java class included in one of my XP Apps. The class is initialized using the OSGi @Component-annotation and I am running an activator-method using the @Activate-annotation to get ahold of the BundleContext. I was looking into context.getServiceReferences() and trying to get the VirtualHostConfigImpl instance, so I could run .getMappings(), but I a bit of a OSGi noob and I am unable to get this “service” without running into problems.

What would the proper way to get the current VirtualHostConfig inside a @Component-class be?

I was hoping to be able to do something like:

@Activate
public void init(BundleContext context) throws Exception {
    ServiceReference<VirtualHostConfigImpl> ref = context.getServiceReference(VirtualHostConfigImpl.class);
    VirtualHostConfig config = context.getService(ref);

    parseMappings(config.getMappings());
}

Unfortunately when I run this on Enonic XP I get a NoClassDefFoundError: com/enonic/xp/web/vhost/impl/config/VirtualHostConfigImpl.

VirtualHostFilter is for internal use at the moment.

What do you want to accomplish? Retrieve all the mappings or the mapping for the current request?

If it is the latter, there is a class com.enonic.xp.web.vhost.VirtualHostHelper with a method getVirtualHost so you can get the virtual host for the current request.

To retrieve the current request, you could do:
HttpServletRequest request = PortalRequestAccessor.get().getRawRequest();

1 Like

I am working on a combination of RequestFilter java class and admin tool that allows an admin to define redirect rules that should be validated / executed for any request coming in.

When the user is defining each redirect rule in the admin tool, I allow the admin to select what host the redirect should be applied to and what path to redirect from. It would be nice for this host-dropdown to automatically be populated with all the virtual hosts that the server is configured to handle. This is why I was hoping to hook into the VirtualHostConfig from OSGi.