javaeclipseservletsosgideclarative-services

How do I implement properly a servlet using OSGi declarative services?


I'm trying (to learn how) to create a Servlet (javax.servlet.Servlet) using declarative services (DS), provided by OSGi, as below:

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.osgi.service.component.annotations.Component;

@Component(service = Servlet.class, property = { "alias:String=/hello" }, immediate = true)
public class LogsHTTPServlet extends HttpServlet {

    private static final long serialVersionUID = 2302372200114479484L;

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            resp.setContentType("text/plain");
            resp.getWriter().println("It works!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        resp.setStatus(200);
    }
}

When I run the project on Eclipse (2019-03 (4.11.0), build id: 20190314-1200) upon this target platform, using a custom run configuration, where I set up org.eclipse.jetty.server (and other required dependencies) with this VM argument (-Dorg.osgi.service.http.port=8080), I see something like below in the osgi console:

osgi> 2019-05-07 18:22:28.236:INFO::Start Level: Equinox Container: 0d7d3d67-401b-48eb-b016-6e6c0c16352c: Logging initialized @1590ms to org.eclipse.jetty.util.log.StdErrLog
2019-05-07 18:22:28.388:INFO:oejs.session:Start Level: Equinox Container: 0d7d3d67-401b-48eb-b016-6e6c0c16352c: DefaultSessionIdManager workerName=node0
2019-05-07 18:22:28.388:INFO:oejs.session:Start Level: Equinox Container: 0d7d3d67-401b-48eb-b016-6e6c0c16352c: No SessionScavenger set, using defaults
2019-05-07 18:22:28.389:INFO:oejs.session:Start Level: Equinox Container: 0d7d3d67-401b-48eb-b016-6e6c0c16352c: node0 Scavenging every 660000ms
2019-05-07 18:22:28.427:INFO:oejs.session:Start Level: Equinox Container: 0d7d3d67-401b-48eb-b016-6e6c0c16352c: node0 Scavenging every 660000ms

Now, when I perform this request using Postman (http://127.0.0.1:8080/hello) I get:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
        <title>Error 404 ProxyServlet: /hello</title>
    </head>
    <body>
        <h2>HTTP ERROR 404</h2>
        <p>Problem accessing /hello. Reason:

            <pre>    ProxyServlet: /hello</pre>
        </p>
        <hr>
        <a href="http://eclipse.org/jetty">Powered by Jetty:// 9.4.15.v20190215</a>
        <hr/>
    </body>
</html>

How can I solve this issue?


Solution

  • Instead of alias use the osgi.http.whiteboard.servlet.pattern property:

    @Component(service = Servlet.class, property = {"osgi.http.whiteboard.servlet.pattern=/hello/*" })
    

    Alternatively, with the current osgi compendium dependency (version 7.0.0), you can use the @org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletPattern annotation like this:

    @Component(service=Servlet.class)
    @org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletPattern("/hello/*")
    

    See https://osgi.org/specification/osgi.cmpn/7.0.0/service.http.whiteboard.html#service.http.whiteboard-i21223311