4 Replies Latest reply on Apr 3, 2013 10:47 PM by Sudharsan S

    How to convert old Java Servlets to OSGi "Servlet" Bundles

    Thomas_PNC

      Hello I'm looking for some help/insight on the best way to convert some of our old Java Servlet code into new OSGi Bundles that can be accessed as servlets.  I'd like to be able to install the new bundles then access them on one of our CQ instance using something like "/servlet/servlet_name".

       

      So far I've been able to create a new bundle, and install it into CQ but I haven't been able to access the bundle at the servlet mapping URL that I would expect.  So far what I've found online has lead me to believe that I need to use the Felix Annotations to describe my servlet.

       

      @Component(immediate = true, metatype = true, label = "Servlet Name")

      @Service

      @Properties({

          @Property(name = Constants.SERVICE_DESCRIPTION, value = "Servlet Name"),

          @Property(name = "sling.servlet.methods", value={"GET"}),

          @Property(name = "sling.servlet.paths", value = "/servlet/my_servlet_mapping"),

          @Property(name = Constants.SERVICE_VENDOR, value = "VendorName")

      })

       

      public class TellAFriend extends HttpServlet {...

       

      public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException

          {....

       

      Once I have this installed, and running without an error in the logs for CQ I tried to go on my local instance to http://localhost:4502/servlet/my_servlet_mapping but I only get 404 Resource Not Found errors.  I've even gone to the Sling Resource Resolver and it doesn't resolve to anything either.

       

      Is there more than the Servlet Information need into the Annotations to convert "old" code into a new OSGi Servlet Mapped Bundle?

        • 1. Re: How to convert old Java Servlets to OSGi "Servlet" Bundles
          pchamoun Level 1

          Here is how to create a sling servlet. I guess you are not making use of SlingSafeMethodsServlet. Check these examples too (http://blogs.adobe.com/aaa/2012/09/cq-tips-and-tricks-1-how-to-define-a-slingservlet-cq5-5 -5-6.html)

           

          import java.io.IOException;

          import javax.jcr.Repository;

          import javax.servlet.ServletException;

          import org.apache.felix.scr.annotations.Reference;

          import org.apache.felix.scr.annotations.sling.SlingServlet;

          import org.apache.sling.api.SlingHttpServletRequest;

          import org.apache.sling.api.SlingHttpServletResponse;

          import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

           

          @SlingServlet(paths = "/someAbsolutePath", methods = "GET")

          public class MySafeMethodServlet extends SlingSafeMethodsServlet {

           

               @Reference

               private Repository repository;

           

               @Override

                       protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)throws ServletException, IOException {

                    response.setHeader("Content-Type", "application/json");

                    response.getWriter().print("whatever");

               }

          }

          1 person found this helpful
          • 2. Re: How to convert old Java Servlets to OSGi "Servlet" Bundles
            Thomas_PNC Level 1

            I must be missing something else configuration wise since I created a new Servlet basically using the code you provided above.  I deployed it as part of the bundle that I'm trying to create.

             

            The bundle installs and stars fine but I still can't access the servlet.  Are there CRXDE based configurations that are requried too to configure Servlets. 

             

            Here's my test servlet file.  I tried going to http://localhost:4502/servlet/TestCQServlet and just get the same 404 error I'm getting for the other 2 Servlets Java classes in my Bundle.

             

             

            import java.io.IOException;

            import javax.servlet.ServletException;

            import org.apache.felix.scr.annotations.sling.SlingServlet;

            import org.apache.sling.api.SlingHttpServletRequest;

            import org.apache.sling.api.SlingHttpServletResponse;

            import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

             

            @SlingServlet(paths = "/servlet/TestCQServlet", methods = "GET")

             

            public class TestCQServlet extends SlingSafeMethodsServlet {

             

                // @Reference

                // private Repository repository;

             

                /**

                 *

                 */

                private static final long serialVersionUID = 6239754936223276983L;

             

                @Override

                protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

                    response.setHeader("Content-Type", "application/json");

                    response.getWriter().print("whatever");

                }

             

            }

            • 3. Re: How to convert old Java Servlets to OSGi "Servlet" Bundles
              Jörg Hoh Adobe Employee

              By default you can register servlets only in /bin, /libs, /apps and /system. You can modify this on http://localhost:4502/system/console/configMgr/org.apache.sling.servlets.resolver.SlingSer vletResolver

               

              Jörg

              1 person found this helpful
              • 4. Re: How to convert old Java Servlets to OSGi "Servlet" Bundles
                Sudharsan S Level 1

                Hi

                you have to register your servlet if you are using path attribute in the annotation, you can also use  resourceType instead of path attribute in your annotations as below

                 

                @SlingServlet(resourceType= "/servlet/TestCQServlet", methods = "GET")

                 

                -Sudharsan