Web whiteboard: use whiteboard context instead of alias for servlet

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Web whiteboard: use whiteboard context instead of alias for servlet

Steinar Bang
I'm trying to replace alias with web context for a whiteboard servlet
started with DS.  I tried to copy-paste an example that works and edit
it as needed.

However the end result didn't work (I just got 404 on the path), and I
wonder where I went wrong...?

I have this servlet that works, ie. I get the expected results on
"/ukelonn/*":
@Component(
    property= {
        HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN+"=/*",
        HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT + "=(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME +"=ukelonn)",
        HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME+"=ukelonn"},
    service=VaadinServlet.class,
    immediate=true
)
public class UkelonnServlet extends TouchKitServlet {
  ...
}

(it sort of works if the service is Servlet.class as well, just not as
well)

So I have this:
@Component(service={Servlet.class}, property={"alias=/frontend-karaf-demo"} )
public class ReactServlet extends HttpServlet {
   ...
}

and tried replacing this with:
@Component(
    property= {
        HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN+"=/*",
        HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT + "=(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME +"=frontend-karaf-demo)",
        HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME+"=frontend-karaf-demo"},
    service=Servlet.class,
    immediate=true
)
public class ReactServlet extends HttpServlet {
   ...
}

But that just gave me 404 on /frontend-karaf-demo and
/frontend-karaf-demo/

So where did I go wrong?

Thanks!

Reply | Threaded
Open this post in threaded view
|

Re: Web whiteboard: use whiteboard context instead of alias for servlet

luke
Hey,
I see in your code examples servlets but I can not see HttpContext instance. You specify org.osgi.service.http.HttpContext to be different than default but do not register service which have such id.

Cheers,
Łukasz
--
Code-House
http://code-house.org

> On 16 Jun 2018, at 18:31, Steinar Bang <[hidden email]> wrote:
>
> I'm trying to replace alias with web context for a whiteboard servlet
> started with DS.  I tried to copy-paste an example that works and edit
> it as needed.
>
> However the end result didn't work (I just got 404 on the path), and I
> wonder where I went wrong...?
>
> I have this servlet that works, ie. I get the expected results on
> "/ukelonn/*":
> @Component(
>    property= {
>        HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN+"=/*",
>        HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT + "=(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME +"=ukelonn)",
>        HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME+"=ukelonn"},
>    service=VaadinServlet.class,
>    immediate=true
> )
> public class UkelonnServlet extends TouchKitServlet {
>  ...
> }
>
> (it sort of works if the service is Servlet.class as well, just not as
> well)
>
> So I have this:
> @Component(service={Servlet.class}, property={"alias=/frontend-karaf-demo"} )
> public class ReactServlet extends HttpServlet {
>   ...
> }
>
> and tried replacing this with:
> @Component(
>    property= {
>        HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN+"=/*",
>        HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT + "=(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME +"=frontend-karaf-demo)",
>        HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME+"=frontend-karaf-demo"},
>    service=Servlet.class,
>    immediate=true
> )
> public class ReactServlet extends HttpServlet {
>   ...
> }
>
> But that just gave me 404 on /frontend-karaf-demo and
> /frontend-karaf-demo/
>
> So where did I go wrong?
>
> Thanks!
>
Reply | Threaded
Open this post in threaded view
|

Re: Web whiteboard: use whiteboard context instead of alias for servlet

Tim Ward-2
Hi Steinar,

The OSGi Http Whiteboard requires your service to be registered as a javax.servlet.Servlet. In your code snippet you register it as a VaadinServlet, which is not part of the spec. My guess is that this is either being handled by a Vaadin component somehow, or that the PAX Web container goes beyond the limits of the OSGi spec.

The next thing to comment on is that the servlet is targeting a specific Context. You can see what this means in the section about Common Whiteboard Properties, but in essence your servlet is ineligible to run unless a Servlet Context exists with the name "frontend-karaf-demo”.

You can see how to register a custom Context using Servlet Context Properties, but my question to you is “do you actually need a custom context?”. If you remove the context selection filter and use a servlet pattern of “/frontend-karaf-demo/*” then you should get a servlet listening on the correct path.

Best Regards,

Tim


On 17 Jun 2018, at 09:18, Łukasz Dywicki <[hidden email]> wrote:

Hey,
I see in your code examples servlets but I can not see HttpContext instance. You specify org.osgi.service.http.HttpContext to be different than default but do not register service which have such id.

Cheers,
Łukasz
--
Code-House
http://code-house.org

On 16 Jun 2018, at 18:31, Steinar Bang <[hidden email]> wrote:

I'm trying to replace alias with web context for a whiteboard servlet
started with DS.  I tried to copy-paste an example that works and edit
it as needed.

However the end result didn't work (I just got 404 on the path), and I
wonder where I went wrong...?

I have this servlet that works, ie. I get the expected results on
"/ukelonn/*":
@Component(
  property= {
      HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN+"=/*",
      HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT + "=(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME +"=ukelonn)",
      HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME+"=ukelonn"},
  service=VaadinServlet.class,
  immediate=true
)
public class UkelonnServlet extends TouchKitServlet {
...
}

(it sort of works if the service is Servlet.class as well, just not as
well)

So I have this:
@Component(service={Servlet.class}, property={"alias=/frontend-karaf-demo"} )
public class ReactServlet extends HttpServlet {
 ...
}

and tried replacing this with:
@Component(
  property= {
      HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN+"=/*",
      HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT + "=(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME +"=frontend-karaf-demo)",
      HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME+"=frontend-karaf-demo"},
  service=Servlet.class,
  immediate=true
)
public class ReactServlet extends HttpServlet {
 ...
}

But that just gave me 404 on /frontend-karaf-demo and
/frontend-karaf-demo/

So where did I go wrong?

Thanks!


Reply | Threaded
Open this post in threaded view
|

Re: Web whiteboard: use whiteboard context instead of alias for servlet

Steinar Bang
In reply to this post by luke
>>>>> Łukasz Dywicki <[hidden email]>:

> I see in your code examples servlets but I can not see HttpContext
> instance. You specify org.osgi.service.http.HttpContext to be
> different than default but do not register service which have such id.

Yes, my bad!

I didn't remember exactly how I got the code I was copy pasting from
 working, and didn't google myself well enough before asking the question:
 https://groups.google.com/d/msg/ops4j/Y8YwekymQ-Q/VF6QhzJSBgAJ

Luckily I wrote it all down...:-)

The ops4j mailing list message from 31 March this year, explains what
my copy-pasted code needed, probably step 1, the ServletContextHelper...?

Thanks!


- Steinar

Reply | Threaded
Open this post in threaded view
|

Re: Web whiteboard: use whiteboard context instead of alias for servlet

Steinar Bang
In reply to this post by Tim Ward-2
>>>>> Tim Ward <[hidden email]>:

> The OSGi Http Whiteboard requires your service to be registered as a
> javax.servlet.Servlet. In your code snippet you register it as a
> VaadinServlet, which is not part of the spec. My guess is that this is
> either being handled by a Vaadin component somehow, or that the PAX
> Web container goes beyond the limits of the OSGi spec.

You're right!  The VaadinServlet service is probably handled by the
Vaadin framework somehow.  In some ways it behaves as a whiteboard
servlet so Vaadin probably plays by the rules there, but there is a lot
of other magic stuff going on.

> The next thing to comment on is that the servlet is targeting a
> specific Context. You can see what this means in the section about
> Common Whiteboard Properties
> <https://osgi.org/specification/osgi.cmpn/7.0.0/service.http.whiteboard.html#service.http.whiteboard.common.properties>,
> but in essence your servlet is ineligible to run unless a Servlet
> Context exists with the name "frontend-karaf-demo”.

Right! Creating such a context would be the first step of this message I
wrote to the OPS4J mailing list on 31 March this year? Ie. the
ServletContextHelper component?
 https://groups.google.com/d/msg/ops4j/Y8YwekymQ-Q/VF6QhzJSBgAJ

(life would be so much simpler if I could just remember everything I did
a couple of months back...:-) )

> You can see how to register a custom Context using Servlet Context
> Properties
> <https://osgi.org/specification/osgi.cmpn/7.0.0/service.http.whiteboard.html#service.http.whiteboard.servletcontext.helper.properties>,
> but my question to you is “do you actually need a custom context?”. If
> you remove the context selection filter and use a servlet pattern of
> “/frontend-karaf-demo/*” then you should get a servlet listening on
> the correct path.

That's what I ended up doing for the karaf-frontend-demo.
 https://github.com/steinarb/frontend-karaf-demo

The reason ukelonn had a servlet context was that it was using an apache
shiro filter in front of the servlet to do authentication and
authorization:
 https://github.com/steinarb/ukelonn

Also the VAADIN static resources worked a lot better with a context,
because that let me serve two Vaadin applications from the same jetty
instance when they were based on different Vaadin versions (if I didn't
use a context Vaadin would look for resources at /VAADIN, with a context
Vaadin would look for resources at /ukelonn/VAADIN)

Thanks!


- Steinar