3 Replies Latest reply on May 21, 2018 9:51 AM by arunp99088702

    AEM 6.4 Servlet POST request not hitting servlet

    grantc99475970

      My problem is similar to the one described here: AEM 6.4 - Java sling servlet does not work with uber 6.4 jar, but does with uber 6.3

      I have the following Servlet:

       

      @Component(service=Servlet.class,
          property={
              Constants.SERVICE_DESCRIPTION + "=Video servlet",
              "sling.servlet.methods=" + HttpConstants.METHOD_POST,
              "sling.servlet.paths="+ "/bin/damUpload/video"
          }
      )
      public class VideoServlet extends SlingAllMethodsServlet {
      // ...
       @Override protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
                  System.out.println("POST TO /bin/damUpload/video");
                   // ...
      

       

      When I do a POST to localhost:4502/bin/damUpload/video, I get a 500 error:

       

      18.05.2018 14:47:24.020 *ERROR* [0:0:0:0:0:0:0:1 [1526676444017] POST /bin/damUpload/video HTTP/1.1] org.apache.sling.servlets.post.impl.operations.ModifyOperation Unable to create resource named damUpload in /bin
      18.05.2018 14:47:24.020 *ERROR* [0:0:0:0:0:0:0:1 [1526676444017] POST /bin/damUpload/video HTTP/1.1] org.apache.sling.servlets.post.impl.operations.ModifyOperation Exception during response processing.
      org.apache.sling.api.resource.PersistenceException: Unable to create node at /bin/damUpload
          at org.apache.sling.jcr.resource.internal.helper.jcr.JcrResourceProvider.create(JcrResourceProvider.java:480) [org.apache.sling.jcr.resource:3.0.8]
          at org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider.create(AuthenticatedResourceProvider.java:182) [org.apache.sling.resourceresolver:1.5.34]
          at org.apache.sling.resourceresolver.impl.helper.ResourceResolverControl.create(ResourceResolverControl.java:381) [org.apache.sling.resourceresolver:1.5.34]
      

       

      I don't see the output for the System.out.println in my stdout.log, so I don't think it ever gets hit.

       

      My bundle appears to be installed and active, and my service appears to be registered:

       

        • 1. Re: AEM 6.4 Servlet POST request not hitting servlet
          arunp99088702 Level 4

          Hi,

          I tried your code for me its working.

          Are you using new osgi annotation or felix annotation? Please check Jorg's answer in related Post.

           

          Thanks

          Arun

          • 2. Re: AEM 6.4 Servlet POST request not hitting servlet
            grantc99475970 Level 1

            arunp99088702

            I think I'm using the new osgi annotation. I have also tried the solution in Jorg's answer on the related post before asking this question.

            My imports for the servlet are these:

             

             

            import org.apache.sling.api.SlingHttpServletRequest;
            import org.apache.sling.api.SlingHttpServletResponse;
            import org.apache.sling.jcr.api.SlingRepository;
            import org.apache.sling.api.servlets.SlingAllMethodsServlet;
            import org.osgi.service.component.annotations.Component;
            import org.osgi.service.component.annotations.Reference;
            import java.io.IOException;
            import javax.jcr.Session;
            import javax.servlet.Servlet;
            import javax.servlet.ServletException;
            import org.apache.sling.api.servlets.HttpConstants;
            import org.osgi.framework.Constants;
            

             

            These are in my pom file:

             

            <dependency>
                        <groupId>org.apache.felix</groupId>
                        <artifactId>org.apache.felix.scr</artifactId>
                        <version>1.6.0</version>
                        <scope>provided</scope>
                    </dependency>
                    <dependency>
                        <groupId>org.apache.felix</groupId>
                        <artifactId>org.apache.felix.scr.annotations</artifactId>
                        <version>1.9.6</version>
                        <scope>provided</scope>
                    </dependency>
                    <dependency>
                        <groupId>biz.aQute</groupId>
                        <artifactId>bndlib</artifactId>
                        <version>1.50.0</version>
                        <scope>provided</scope>
                    </dependency>
                    <dependency>
                        <groupId>org.osgi</groupId>
                        <artifactId>org.osgi.core</artifactId>
                        <version>4.2.0</version>
                        <scope>provided</scope>
                    </dependency>
                    <dependency> 
                        <groupId>org.osgi</groupId> 
                        <artifactId>org.osgi.service.component.annotations</artifactId>
                        <version>1.3.0</version>
                    </dependency>
                    <dependency>
                        <groupId>org.osgi</groupId>
                        <artifactId>org.osgi.service.metatype.annotations</artifactId>
                        <version>1.3.0</version> 
                    </dependency>
                    <dependency>
                        <groupId>org.osgi</groupId>
                        <artifactId>org.osgi.annotation</artifactId>
                        <version>6.0.0</version>
                    </dependency>
                    <dependency>
                        <groupId>org.osgi</groupId>
                        <artifactId>org.osgi.compendium</artifactId>
                        <version>4.2.0</version>
            <scope>provided</scope>
                    </dependency>
            
            • 3. Re: AEM 6.4 Servlet POST request not hitting servlet
              arunp99088702 Level 4

              Yours imports are fine. could you please try to invoke simple servlet with Get method for testing, to check whether issue is with code or something else.

               

              @Component(service=Servlet.class,

              property={

              Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",

              "sling.servlet.methods=" + HttpConstants.METHOD_POST,

              "sling.servlet.paths="+ "/bin/damUpload/video",

              })

               

              public class SimplePostServlet extends SlingAllMethodsServlet {

              /**

                *

                */

                private static final long serialVersionUID = 1L;

               

              @Override

              protected void doGet(final SlingHttpServletRequest req,

              final SlingHttpServletResponse resp) throws ServletException, IOException {

                      resp.setContentType("text/plain");

                      resp.getWriter().write("Servlet found!");

                      System.out.println("Servlet found!");

                  }

               

              }