Hello Achim,
Thank you very much! In couldn't figure out what I was doing wrong!
I have a couple of follow-ups if you don't mind.
When I set up two servlets (one with a HttpContext, one without -> I
wanted to see if I could forward a request from one servlet to another
with filters using OSGi), I am getting the following exception:
17:44:21,278 | DEBUG | lixDispatchQueue | pax-web-extender-whiteboard
| ? ? | 106 -
org.ops4j.pax.web.pax-web-extender-whiteboard - 1.0.4 | FrameworkEve
nt ERRORjava.lang.IllegalStateException: Http context already used.
Context params can be set only before first usage
at
org.ops4j.pax.web.service.internal.HttpServiceStarted.setContextParam(HttpServiceStarted.java:353)[62:org.ops4j.pax.web.pax-web-runtime:1.0.4]
What could cause this? Here is my code:
@Override
public void start(BundleContext context) throws Exception {
Hashtable<String,String> props = new Hashtable<String,String>();
props.put(ExtenderConstants.PROPERTY_HTTP_CONTEXT_ID,
"myServiceContext");
this.contextRegistration =
context.registerService(HttpContext.class.getName(), new
MyHttpContext(), props);
props = new Hashtable<String,String>();
props.put(ExtenderConstants.PROPERTY_ALIAS, "/myservice");
props.put(ExtenderConstants.PROPERTY_SERVLET_NAMES,"My Servlet");
props.put(ExtenderConstants.PROPERTY_HTTP_CONTEXT_ID,
"myServiceContext");
this.registration =
context.registerService(Servlet.class.getName(), new MyServlet(),
props);
props = new Hashtable<String,String>();
props.put(ExtenderConstants.PROPERTY_SERVLET_NAMES,"Forward
Servlet");
props.put(ExtenderConstants.PROPERTY_ALIAS, "/forwardservlet");
>> Exception called here>> this.forwardRegistration =
>> context.registerService(Servlet.class.getName(), new ForwardServlet(),
>> props);
}
Anything obvious I am doing wrong?
As a final question on OSGi and http, I understand that in OSGi 4.2
support for web application bundles (which I understand PAX implements
for the major OSGi implementations?). Given this fact is there any
advantage to not use wab and stick with the base OSGi Http Service (+
PAX extensions)? From my research (I could be looking in the wrong
place), it doesn't appear that the base OSGi Http Service is used
widely (e.g. I haven't been able to find a serious security
implementation done using the HttpContext, just very basic examples).
Again, thank you for very much for your help!
regards,
Gareth
On Mon, Jul 25, 2011 at 5:38 PM, Achim Nierbeck <[email protected]> wrote:
> Hi Gareth,
>
> sorry that it took me so long to answer.
> But I also am making it short :-)
>
> You are absolutely right you need to register Servlet and Context
> within the same bundle. The Whiteboard extension doesn't behave any
> different
> then the HTTP Service. The only difference and this really helps is
> that you just register you're own Servlet-Service where the whiteboard
> extension jumps in and does all the rest for you.
> With the classical HTTP Service you have to do all the work by yourself.
>
> Regards, Achim
>
>
> Am 23.07.2011 05:25, schrieb Gareth Collins:
>>
>> Hello,
>> I am trying to understand how to use the PAX Web Whiteboard correctly.
>> I was hoping, with the whiteboard pattern, I could completely separate
>> my code for auth (HttpContext) from my Servlet. I am currently using
>> PAX web 1.0.4 (which comes packaged with Karaf 2.2.2).
>>
>> So to test out the whiteboard pattern I created two bundles, each with
>> two java files:
>>
>> - Bundle1
>> + Activator.java - The activator start method:
>>
>> @Override
>> public void start(BundleContext context) throws Exception {
>> Hashtable<String,Object> props = new Hashtable<String,Object>();
>> props.put(ExtenderConstants.PROPERTY_HTTP_CONTEXT_ID, "myContext");
>> this.registration =
>> context.registerService(HttpContext.class.getName(), new
>> MyHttpContext(), props);
>> }
>> + MyHttpContext.java
>>
>> - Bundle2
>> + Activator.java - The activator start method:
>>
>> @Override
>> public void start(BundleContext context) throws Exception {
>> Hashtable<String,Object> props = new Hashtable<String,Object>();
>> props.put(ExtenderConstants.PROPERTY_ALIAS, "/myservice");
>> props.put(ExtenderConstants.PROPERTY_SERVLET_NAMES,"My Great
>> Servlet");
>> props.put(ExtenderConstants.PROPERTY_HTTP_CONTEXT_ID, "myContext");
>> this.registration =
>> context.registerService(Servlet.class.getName(), new MyHttpServlet(),
>> props);
>> }
>>
>> + MyHttpServlet.java
>>
>> When I try and run these two bundles in pax/karaf and access the web
>> page (http://localhost:8181/myservice), it doesn't work. I see in the
>> log the following:
>>
>> 15:51:56,778 | DEBUG | /profileservice | ServerModel
>> | eb.service.spi.model.ServerModel 296 | 61 -
>> org.ops4j.pax.web.pax-web-spi - 1.0.4 | Path [/myservice] does not
>> match any context
>>
>> I went back to the pax web whiteboard example and noticed that in the
>> example both the sample Servlet and sample HttpContext were defined in
>> the one bundle, so I changed my code to register MyHttpServlet and
>> MyHttpContext in one bundle...and it worked (the handleSecurity for
>> the context was called before running service on the Servlet)!
>>
>> So, given my testing, it appears that I need to register the servlet
>> and context together in the one bundle. Is that correct...or have I
>> misunderstood something (which is very possible as I am new to this
>> :))?
>>
>> If anyone could provide any guidance, it would be a big help.
>>
>> thanks in advance,
>> Gareth
>>
>> _______________________________________________
>> general mailing list
>> [email protected]
>> http://lists.ops4j.org/mailman/listinfo/general
>
>
> --
> --
> *Achim Nierbeck*
>
>
> Apache Karaf<http://karaf.apache.org/> Committer& PMC
> OPS4J Pax Web<http://wiki.ops4j.org/display/paxweb/Pax+Web/> Committer&
> Project Lead
> blog<http://notizblog.nierbeck.de/>
>
>
> _______________________________________________
> general mailing list
> [email protected]
> http://lists.ops4j.org/mailman/listinfo/general
>
_______________________________________________
general mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/general