Setting HTTP servlet context parameters

392 views
Skip to first unread message

t...@quarendon.net

unread,
Jun 16, 2016, 8:21:19 AM6/16/16
to bndtools-users
I'm trying to set some http servlet context parameters that a third party servlet requires, but can't figure out how to do it.

Reading the osgi http whiteboard spec, it looks like I can use register a ServletContextHelper component, but I can't find that class anywhere, I can't work out what to add to my bnd.bnd file in order that I can create a suitable component.
Where might I look?

Alternatively, is this the right route?
I guess I can do the same think through a ServletContextListener,and just programatically set the context parameters? (assuming I can get one of those to work).

Thanks.

t...@quarendon.net

unread,
Jun 16, 2016, 8:28:07 AM6/16/16
to bndtools-users
OK, calling getServletContext().setInitParameter within a ServletContextListener doesn't work, guessing that the http whiteboard ServletContext doesn't let you do that programatically. Can't tell, as can't step in with the debugger, as the source attachments won't work....

Timothy Ward

unread,
Jun 16, 2016, 9:40:41 AM6/16/16
to bndtool...@googlegroups.com
Hi,

Have you looked at the Http Whiteboard spec? The table 140.4 explains what the various service properties do. This one seems relevant to you.

servlet.init.* - Properties starting with this prefix are provided as optional init parameters to the javax.servlet.Servlet.init() method. The servlet.init. prefix is removed from the parameter name. See HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX.

Assuming that your servlet is using Declarative Services then these init properties can be set/changed using Configuration Admin.

Regards,

Tim

On 16 Jun 2016, at 13:28, t...@quarendon.net wrote:

OK, calling getServletContext().setInitParameter within a ServletContextListener doesn't work, guessing that the http whiteboard ServletContext doesn't let you do that programatically. Can't tell, as can't step in with the debugger, as the source attachments won't work....

--
You received this message because you are subscribed to the Google Groups "bndtools-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bndtools-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Raymond Auge

unread,
Jun 16, 2016, 10:26:53 AM6/16/16
to bndtool...@googlegroups.com
First of all you probably want this dependency:

<dependency>
    <groupId>org.osgi</groupId>
    <artifactId>org.osgi.service.http.whiteboard</artifactId>
    <version>1.0.0</version>
</dependency>

The lastest Apache Felix http and also the latest Equinox Http.Servlet both support this specification.

With this dependency will come:

org.osgi.service.http.context.ServletContextHelper

which you can publish an instance of with the context init params that you need like so (just using DS for simplicity):

import java.net.URL;
import java.util.Set;

import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.http.context.ServletContextHelper;
import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;

@Component(
    property = {
        Constants.SERVICE_RANKING + ":Integer=10",
        HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=default",
        HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH + "=/",
        HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_INIT_PARAM_PREFIX + "someInitParam=value"
    },
    service = ServletContextHelper.class
)
public class MyContext extends ServletContextHelper {

    public URL getResource(String name) {
        return delegate.getResource(name);
    }

    public Set<String> getResourcePaths(String path) {
        return delegate.getResourcePaths(path);
    }

    @Activate
    private void activate(BundleContext bundleContext) {
        delegate = new ServletContextHelper(bundleContext.getBundle()) {};
    }

    private ServletContextHelper delegate;

}

Keep adding more init params if you like.

This particular SCH overrides the default context, which means that each http whiteboard service you publish should bind to this context and see the specified init params (provided they are also binding to the default context, i.e. they are not targeting another context using the HTTP_WHITEBOARD_CONTEXT_SELECT property).

HTH
- Ray
--
Raymond Augé (@rotty3000)
Senior Software Architect Liferay, Inc. (@Liferay)
Board Member & EEG Co-Chair, OSGi Alliance (@OSGiAlliance)

t...@quarendon.net

unread,
Jun 17, 2016, 7:22:47 AM6/17/16
to bndtools-users


On Thursday, 16 June 2016 14:40:41 UTC+1, Tim Ward wrote:
Hi,

Have you looked at the Http Whiteboard spec? The table 140.4 explains what the various service properties do. This one seems relevant to you.

servlet.init.* - Properties starting with this prefix are provided as optional init parameters to the javax.servlet.Servlet.init() method. The servlet.init. prefix is removed from the parameter name. See HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX.

Assuming that your servlet is using Declarative Services then these init properties can be set/changed using Configuration Admin.


That's for setting servlet init parameters (so <init-param>), I need to set servlet context parameters (<context-param>) for which you need a ServletContextHelper.

t...@quarendon.net

unread,
Jun 17, 2016, 7:24:35 AM6/17/16
to bndtools-users


On Thursday, 16 June 2016 15:26:53 UTC+1, Raymond Auge wrote:
First of all you probably want this dependency:

<dependency>
    <groupId>org.osgi</groupId>
    <artifactId>org.osgi.service.http.whiteboard</artifactId>
    <version>1.0.0</version>
</dependency>

This is what is odd. I'm using Eclipse and bndtools etc. I don't use Maven. I just search for bundles in the repository list. It's odd that these bundles are published on Maven central, but that there's no obvious way of getting them, other than downloading them manually.

This is all I was missing I think, just finding a class definition.

Thanks.

Peter Kriens

unread,
Jun 17, 2016, 8:17:27 AM6/17/16
to bndtool...@googlegroups.com
If you follow the [OSGi enRoute Base tutorial][1] you can see how you can dependencies from Maven Central via JPM. You can also watch the [video][2]

Basically it is searching jpm4j.org and then dragging the version vignette on the Central repo which is JPM in OSGi enRoute.

Kind regards,

Peter Kriens


t...@quarendon.net

unread,
Jun 17, 2016, 8:59:21 AM6/17/16
to bndtools-users


On Friday, 17 June 2016 13:17:27 UTC+1, Peter Kriens wrote:
If you follow the [OSGi enRoute Base tutorial][1] you can see how you can dependencies from Maven Central via JPM. You can also watch the [video][2]

But I use the JMP4J search, and it doesn't find things. The base tutorial says "This is a view on the JPM repository, which contains Maven Central". Something must be not working then because I can find things on maven that I can't through jpm.

If I go to maven central and search for "org.osgi.service.jdbc", I find it easily. If I to go JMP4J and search for the same thing I don't find it.
 

Peter Kriens

unread,
Jun 17, 2016, 9:01:25 AM6/17/16
to bndtool...@googlegroups.com
This was already discussed on this list.  If you search for osgi:org.osgi.service.jdbc then you find the PAX JDBC because they used this symbolic name as well. However, when you then select the appropriate version it is the OSGi. JPM got confused by the fact that PAX is using an OSGi symbolic name, which is not the intention.

Kind regards,

Peter Kriens

t...@quarendon.net

unread,
Jun 17, 2016, 9:58:26 AM6/17/16
to bndtools-users
This particular SCH overrides the default context, which means that each http whiteboard service you publish should bind to this context and see the specified init params (provided they are also binding to the default context, i.e. they are not targeting another context using the HTTP_WHITEBOARD_CONTEXT_SELECT property).


Hmm.
Each SCH creates a new ServletContext. Thats not really what I wanted, I just wanted to inject some context params into the existing context that I'm using. I am currently using the following when I need to add a filter etc:
    HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT+"=(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME +"=org.osgi.service.http)"

I have tried putting 
    HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=org.osgi.service.http"

but it doesn't appear to make a difference.

I'm almost certainly misunderstanding something.

Raymond Auge

unread,
Jun 17, 2016, 10:31:41 AM6/17/16
to bndtool...@googlegroups.com
You can't "inject" init params... they belong to the creation/init of the context. If you can't set them during creation of the context how else would you expect to be able to add them?

You really need to go to the creation of the context which presumably is somewhere where the first SCH is created and add the service properties!

Who created the SCH with name = "org.osgi.service.http" ???

Baring all these recommendations not helping you, as Peter always says, a reference to a repo we can look at is by far the most useful way for us to assist you.


Sincerely,
- Ray

--
You received this message because you are subscribed to the Google Groups "bndtools-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bndtools-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

t...@quarendon.net

unread,
Jun 17, 2016, 11:33:38 AM6/17/16
to bndtools-users
I have worked around the problem and used some methods that the third party servlet provide for more programmatic configuration.

Thanks.
Reply all
Reply to author
Forward
0 new messages