Hi. Thanks for taking the time to respond. I managed to get it working
using the API example you provided. I had a problem with the context aspect
though so I just mapped the path of the resources to include what would
have been the new context path. It looks like it always runs under the
default "/" context setup in the unomi wab. In the end, I just needed the
path, not anything specific to a servlet context since it's just a static
website.

I'm including some logs/code to show the issue with attaching the context,
if you have any ideas. But the good news is that I was able to get the path
stuff sorted out for the static files by simply not using it.

*Code:*

private static final String MY_CONTEXT_NAME = "my-context";

 /**
   * Registers a custom web context for use with this endpoint.
   *
   * @param context
   */
  private void registerCustomContext(BundleContext context) {
    Dictionary<String, String> contextProps = new Hashtable<>();
    contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME,
MY_CONTEXT_NAME);
    contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH,*
"/test" * );
    context.registerService(ServletContextHelper.class, new
ServletContextHelper () {}, contextProps);
    log.info("ContextInitializer registered a new context {}",
contextProps);
  }

  /**
   * Registers a static resource service.
   *
   * @param context
   */
  private void registerCustomResource(BundleContext context) {
    Dictionary<String, Object> resourceProps = new Hashtable<>();

resourceProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_RESOURCE_PATTERN,
new String[] {"/static/*"});

resourceProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_RESOURCE_PREFIX,
"/webapp/files");
    resourceProps.put(
        HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT,
        String.format("(%s=%s)",
HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME,  MY_CONTEXT_NAME   ));

    context.registerService(Object.class, new Object(), resourceProps);
    log.info("ContextInitializer registered a new resource {}",
resourceProps);
  }

  // blueprint.xml init-method="initConfig"
  public void initConfig() {
    BundleContext bundleContext =
FrameworkUtil.getBundle(ContextInitializer.class).getBundleContext();
    this.registerCustomContext(bundleContext);
    this.registerCustomResource(bundleContext);
  }

Custom context: /test
Resource mapping: /static/* -> /webapp/files
Request: /test/static/hello.txt

*Logs:*

          | 2023-02-13T21:15:08,785 | DEBUG | qtp707116334-265 |
ServerModel                      | 251 - org.ops4j.pax.web.pax-web-spi -
7.2.14 | Matching *[/test/static/hello.txt*]...
          | 2023-02-13T21:15:08,785 | DEBUG | qtp707116334-265 |
ServerModel                      | 251 - org.ops4j.pax.web.pax-web-spi -
7.2.14 | Path [ /test/static/hello.txt] *does not match any context*
          | 2023-02-13T21:15:08,785 | DEBUG | qtp707116334-265 |
HttpServiceContext               | 248 - org.ops4j.pax.web.pax-web-jetty -
7.2.14 | Handling request for [ /test/static/hello.txt]* using http context
[DefaultHttpContext [bundle=org.apache.cxf.cxf-rt-transports-http [112],
contextID=default]]*
         | 2023-02-13T21:15:08,785 | DEBUG | qtp707116334-265 |
HttpServiceServletHandler        | 248 - org.ops4j.pax.web.pax-web-jetty -
7.2.14 | handling request
org.ops4j.pax.web.service.jetty.internal.HttpServiceRequestWrapper@3d9340e9,
org.ops4j.pax.web.service.jetty.internal.HttpServiceResponseWrapper@6f53a229
         | 2023-02-13T21:15:08,786 | DEBUG | qtp707116334-265 |
DefaultHttpContext               | 250 - org.ops4j.pax.web.pax-web-runtime
- 7.2.14 | Searching bundle [org.apache.cxf.cxf-rt-transports-http [112]]* for
resource [ test/static/hello.txt]*

In the system console (Felix) I see the  ServletContextHelper and the
resource registered accordingly

[org.osgi.service.http.context.ServletContextHelper]
osgi.http.whiteboard.context.name my-context
osgi.http.whiteboard.context.path /test
service.bundleid 56
service.scope singleton


[Object]
osgi.http.whiteboard.context.select (osgi.http.whiteboard.context.name
=my-context)
osgi.http.whiteboard.resource.pattern /static/*
osgi.http.whiteboard.resource.prefix /webapp/files
service.bundleid 56
service.scope singleton
Using Bundles org.ops4j.pax.web.pax-web-extender-whiteboard (247)
<http://localhost:8181/cdp-server/system/console/bundles//247>


On Mon, Feb 13, 2023 at 12:58 AM Grzegorz Grzybek <gr.grzy...@gmail.com>
wrote:

> Hello
>
> If you need only static resources, maybe you should simply register
> Whiteboard-based resources? Something like this:
>
> register a context to "get" a context path:
>
> Dictionary<String, Object> properties = new Hashtable<>();
> properties.put("osgi.http.whiteboard.context.name", "my-context");
> properties.put("osgi.http.whiteboard.context.path", "/sample");
>
> ServiceRegistration<ServletContextHelper> reg =
> context.registerService(ServletContextHelper.class, new
> ServletContextHelper() {}, properties);
>
> register resources into this context path:
>
> Dictionary<String, Object> properties = new Hashtable<>();
> properties.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_RESOURCE_PATTERN,
> new String[] { "/static/*" });
> properties.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_RESOURCE_PREFIX,
> "/resources");
> properties.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT,
> String.format("(%s=my-context)",
> HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME));
>
> ServiceRegistration<Object> resReg = context.registerService(Object.class,
> new Object(), properties);
>
> Then you'll be able to get resources from bundle's "/resources" directory
> using URI's like "/sample/static/*"
>
> But sure - your WAR approach should work too.
>
> regards
> Grzegorz Grzybek
>
> pon., 13 lut 2023 o 04:49 Andrew Lienhard <andrew.lienh...@gmail.com>
> napisaƂ(a):
>
>> This seems to be the most promising approach. Repackage the WAB as a WAR
>> then deploy it at runtime as shown below. The URL parameters will assist
>> the OSGI Web URL Handler in automagically producing a WAB + MANIFEST.MF
>> with the correct web-contextpath header. Only I need to do it using the
>> bundleContext.installBundle(String location) API instead of in the console.
>> I also need to sort out the URL of the built war itself. Does this sound
>> correct?
>>
>>
>>
>> karaf@root()> bundle:install -s "webbundle:
>> http://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/sample.war?Bundle-SymbolicName=tomcat-sample&Web-ContextPath=/sample
>> "
>>
>>
>>
>> From <https://karaf.apache.org/manual/latest/webcontainer>
>>
>>
>> https://docs.osgi.org/specification/osgi.cmpn/7.0.0/service.war.html#i3100445
>>
>>
>>
>>
>>
>>
>> On Sun, Feb 12, 2023 at 4:29 PM Andrew Lienhard <alienh...@gmail.com>
>> wrote:
>>
>>> I have a WAB in a karaf container that I need to deploy using a context
>>> path based on environment variables (k8s helm). Currently its
>>> web-ContextPath is hardcoded in a pom.xml, so it's compile-time only which
>>> won't work for our deployments.
>>>
>>> I'm wondering if it's possible to register this WAB dynamically as one
>>> would a servlet using HttpService and/or pax-web. I've been digging through
>>> the documentation and examples but so far I haven't been able to work it
>>> out. Is this doable? If so, are there any examples of this? Ultimately, I
>>> don't care what form the service takes as long as it can serve static files
>>> from a directory.
>>>
>>> clip from the pom.xml
>>>       <plugin>
>>>                 <groupId>org.apache.felix</groupId>
>>>                 <artifactId>maven-bundle-plugin</artifactId>
>>>                 <extensions>true</extensions>
>>>                 <configuration>
>>>                     <instructions>
>>>                         <_wab>src/main/webapp</_wab>
>>>
>>> <Include-Resource>{maven-resources},OSGI-INF/blueprint/blueprint.xml=${project.build.directory}/classes/OSGI-INF/blueprint/blueprint.xml</Include-Resource>
>>>
>>> <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
>>>                         <Embed-Directory>WEB-INF/lib</Embed-Directory>
>>>                         <Web-ContextPath>/</Web-ContextPath>
>>>                     </instructions>
>>>                 </configuration>
>>>             </plugin>
>>>
>>> --
>>> --
>>> ------------------
>>> OPS4J - http://www.ops4j.org - ops4j@googlegroups.com
>>>
>>> ---
>>> You received this message because you are subscribed to the Google
>>> Groups "OPS4J" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to ops4j+unsubscr...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/ops4j/2e75aacf-be77-4753-a05c-126df2b7f487n%40googlegroups.com
>>> <https://groups.google.com/d/msgid/ops4j/2e75aacf-be77-4753-a05c-126df2b7f487n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
>> --
>> ------------------
>> OPS4J - http://www.ops4j.org - ops4j@googlegroups.com
>>
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "OPS4J" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to ops4j+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/ops4j/CADGfNtp_02pRaMrG7Oj0wJuYKA3O3p0QS6DHa3XSChA0qJNUFA%40mail.gmail.com
>> <https://groups.google.com/d/msgid/ops4j/CADGfNtp_02pRaMrG7Oj0wJuYKA3O3p0QS6DHa3XSChA0qJNUFA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> --
> ------------------
> OPS4J - http://www.ops4j.org - ops4j@googlegroups.com
>
> ---
> You received this message because you are subscribed to the Google Groups
> "OPS4J" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ops4j+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ops4j/CAAdXmhpzREW99MVkkyXxg8Bt8Owsyp-iYY26n%2BeiG3F4BEn_eg%40mail.gmail.com
> <https://groups.google.com/d/msgid/ops4j/CAAdXmhpzREW99MVkkyXxg8Bt8Owsyp-iYY26n%2BeiG3F4BEn_eg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
-- 
------------------
OPS4J - http://www.ops4j.org - ops4j@googlegroups.com

--- 
You received this message because you are subscribed to the Google Groups 
"OPS4J" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ops4j+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ops4j/CADGfNtqNM-x5B0XTrK2Vgo%2BdDwsB7erxhgVqKC7KrAth%2B%2BaesA%40mail.gmail.com.

Reply via email to