Saied Tazari
Saied Tazari

Reputation: 1

Change the base url / context path for a karaf/osgi blueprint web app

I have a Web app with several servlets configured in my OSGI-INF/blueprint/web-blueprint.xml using pax web. The web app consists of two pages /LoginPage.html and /Dashboard.html, but the latter supports several views, each associated with an own angular controller and related backend servlet.

I can reach the two pages by the URLs https://<ip>:<port>/LoginPage.html and https://<ip>:<port>/Dashboard.html. Accessing https://<ip>:<port>/ redirects to https://<ip>:<port>/LoginPage.html.

If we call https://<ip>:<port>/ the base URL, I would like now to change this base URL to something like https://<ip>:<port>/test/ so that

  1. the login and dashboard pages are available under https://<ip>:<port>/test/LoginPage.html and https://<ip>:<port>/test/Dashboard.html respectively, and
  2. https://<ip>:<port>/test simply redirects to https://<ip>:<port>/test/LoginPage.html.

Because the file etc/org.ops4j.pax.web.cfg specifies etc/jetty.xml as its config file, I was assuming that I can configure this in jetty.xml. The current jetty.xml is the standard one. jetty documentation says that one can set the context path by adding the following:

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/test</Set>
</Configure>

But, adding simply the above to the end of the standard jetty conf file causes that my web pages are not reachable at all. I cannot find any further documentation or example how I could get this work...

I would appreciate any help very much!

Upvotes: 0

Views: 425

Answers (1)

Grzegorz Grzybek
Grzegorz Grzybek

Reputation: 6237

Please specify which Pax Web version you're using. In Pax Web 8, whiteboard context configuration and selection is fully implemented.

There's a blueprint example that declares servlets, filters, error pages and welcome files but it doesn't do what you want (you want to register a ServletContextHelper whiteboard service).

There's also a whiteboard non-blueprint sample that registers a context using non-standard org.ops4j.pax.web.service.whiteboard.HttpContextMapping.

So I can't point you to proper sample, but in short words, you need three things:

  1. an implementation of org.osgi.service.http.context.ServletContextHelper

You have to extend this class, because it's abstract. You could also reuse org.ops4j.pax.web.service.spi.context.DefaultServletContextHelper

  1. a whiteboard context based on the above:
<service interface="org.osgi.service.http.context.ServletContextHelper">
    <service-properties>
        <entry key="osgi.http.whiteboard.context.name" value="my-name" />
        <entry key="osgi.http.whiteboard.context.path" value="/test" />
    </service-properties>
    <bean class="org.ops4j.pax.web.service.spi.context.DefaultServletContextHelper" />
</service>
  1. a servlet referring to the above context:
<service id="my-servlet" interface="javax.servlet.Servlet">
    <service-properties>
        <entry key="osgi.http.whiteboard.servlet.name" value="my-servlet" />
        <entry key="osgi.http.whiteboard.context.select" value="(osgi.http.whiteboard.context.name=my-context)" />
    </service-properties>
    <bean class="com.example.MyServlet" />
</service>

Pax Web 8 correctly handles context selection using standard Whiteboard service registration properties.

Upvotes: 0

Related Questions