Re: Dynamically registering a WAB

2023-02-13 Thread Grzegorz Grzybek
Hello

Somehow I thought you're using Pax Web 8 which actually has working context
selection. Problems with context registration/selection were the MAIN
reason I started refactoring of Pax Web 7 into Pax Web 8.

Your example is _canonical_ example of Whiteboard usage and should work
perfectly fine with Pax Web 8 (included in Karaf 4.4.3).

regards
Grzegorz Grzybek

wt., 14 lut 2023 o 02:54 Andrew Lienhard 
napisał(a):

> 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 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 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)
> 
>

Re: Dynamically registering a WAB

2023-02-13 Thread Andrew Lienhard
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 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 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)



On Mon, Feb 13, 2023 at 12:58 AM Grzegorz Grzybek 
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 properties = new Hashtable<>();
> properties.put("osgi.http.whiteboard.context.name", "my-context");
> properties.put("osgi.http.whiteboard.context.path", "/sample");
>
> ServiceRegistration reg =
> context.registerService(ServletContextHelper.class, new
> ServletContextHelper() {}, properties);
>
> register resources into this context path:
>
> Dictionary properties = 

Re: Dynamically registering a WAB

2023-02-12 Thread Grzegorz Grzybek
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 properties = new Hashtable<>();
properties.put("osgi.http.whiteboard.context.name", "my-context");
properties.put("osgi.http.whiteboard.context.path", "/sample");

ServiceRegistration reg =
context.registerService(ServletContextHelper.class, new
ServletContextHelper() {}, properties);

register resources into this context path:

Dictionary 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 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 
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=/sample
> "
>
>
>
> From 
>
>
> 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 
> 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
>>   
>> org.apache.felix
>> maven-bundle-plugin
>> true
>> 
>> 
>> <_wab>src/main/webapp
>>
>> {maven-resources},OSGI-INF/blueprint/blueprint.xml=${project.build.directory}/classes/OSGI-INF/blueprint/blueprint.xml
>>
>> *;scope=compile|runtime
>> WEB-INF/lib
>> /
>> 
>> 
>> 
>>
>> --
>> --
>> --
>> 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
>> 
>> .
>>
> --
> --
> --
> 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
> 
> .
>

-- 
-- 
--
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.


Re: Dynamically registering a WAB

2023-02-12 Thread Andrew Lienhard
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=/sample
"



>From 

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  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
>   
> org.apache.felix
> maven-bundle-plugin
> true
> 
> 
> <_wab>src/main/webapp
>
> {maven-resources},OSGI-INF/blueprint/blueprint.xml=${project.build.directory}/classes/OSGI-INF/blueprint/blueprint.xml
>
> *;scope=compile|runtime
> WEB-INF/lib
> /
> 
> 
> 
>
> --
> --
> --
> 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
> 
> .
>

-- 
-- 
--
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.