7 Replies Latest reply: May 2, 2012 8:32 AM by durganuvvula RSS

    Once apache webserver dispatches the request to CQ, where would it hit the CQ at first?

    durganuvvula Community Member

      I have a apache webserver which have a dispatcher configured. once dispatcher dispatches the request to CQ, where would it hit the CQ server at first. I need to write my own business logic at that place to do some business processing, where the request is intercepted for the first time in CQ server.

       

      I am yet to start working on this requirement but I assume it is sling servlet engine by looking at the architecture of CQ and I need to write my own business logic there in the sling servlet layer and bundle the sling jar and load my bundle jar into the apache felix to solve my use case.

       

      Any suggestions/pointers to solve this problem would be appreciable.

       

      --Durga

        • 1. Re: Once apache webserver dispatches the request to CQ, where would it hit the CQ at first?
          justin_at_adobe Adobe Employee

          Hi Durga,

          You would generally use a Servlet Filter for this. There are two potential insertion points:

           

          1) Before CQ (really Sling) gets the request. This type of Filter needs to be registered with the HttpService implementation. See http://felix.apache.org/site/apache-felix-http-service.html#ApacheFelixHTTPService-Usingth eExtHttpService and http://felix.apache.org/site/apache-felix-http-service.html#ApacheFelixHTTPService-Usingth eWhiteboard for information on this.

          2) After CQ (again, really Sling) authenticates the user and resolved the requested resource. This type of Filter is registered with the Sling Engine. See http://sling.apache.org/site/filters.html for information on Sling's Filter support.

           

          Which of these is appropriate depends upon the type of logic you need to inject.

           

          Justin

          • 2. Re: Once apache webserver dispatches the request to CQ, where would it hit the CQ at first?
            durganuvvula Community Member

            Thanks Justin for helping me out in the right direction. I would explore more on this.

             

            --Durga

            • 3. Re: Once apache webserver dispatches the request to CQ, where would it hit the CQ at first?
              durganuvvula Community Member

              My scenario is Apache webserver would direct the calls to CQ Servlet filter which i write. At the filter,

              I would do some business process to verify its the CQ pages or our webapp vanity pages. If it is a CQ page,

              forward to that CQ page or if it is a webapp vanity, i would redirect to that webapp page from CQ using 301 redirect..

               

               

              I want the apache webserver to hit my filter directly and then my filter should do the routing accordingly at the CQ.

               

               

              In my case I have choosen felix filter as I want my filter to intercept all the calls before actually go to sling engine. For testing purpose, I am hitting the filter and apache webserver configuration is not in place yet, I am trying to forward the request to /content/geometrixx/en/products.html from the filter and getting 500 error. I have looked into error.log, system.log and access.log but of no help. 

               

              Any pointers/suggestions towards the solution would be really appreciable. Am I doing any thing wrong ? Please suggest if any better ways to do this.

               

               

              public class Activator implements BundleActivator {

                  private static final Logger log = LoggerFactory.getLogger(Activator.class);

                  private ServiceRegistration registration;

               

               

                  public void start(BundleContext context) throws Exception {

                            ServiceReference sRef = context.getServiceReference(ExtHttpService.class.getName());

                                if (sRef != null)

                                {

                                  ExtHttpService service = (ExtHttpService) context.getService(sRef);

                                  service.registerFilter(new VanityUrlFilter(), "/testVanityFilter", null, 0, null);

                                }

                              }

                     

                  }

              }

               

               

              public class VanityUrlFilter implements Filter{

                         private static final Logger logger = LoggerFactory.getLogger(VanityUrlFilter.class);

               

               

                        public void doFilter(ServletRequest request, ServletResponse response,

                                            FilterChain chain) throws IOException, ServletException {

              logger.info("Inside Felix Filter");

                                  response.setContentType("text/plain");

                      response.setCharacterEncoding("UTF-8");

                                  response.getWriter().write("Filter got registered successfully");

                      response.getWriter().flush();

                     

                         RequestDispatcher requestDispatcher = request.getRequestDispatcher("/content/geometrixx/en/products.html");//tried with out html extension also.

                        //RequestDispatcher requestDispatcher = request.getRequestDispatcher("/content/geometrixx/en/products");

                        requestDispatcher.forward(request, response);

                        //even I have tried response.sendRedirect with no luck.

                        //response.sendRedirect("/content/geometrixx/en/products.html")

               

               

                        }

              }

               

               

              when I hit the url http://localhost:4502/totalVanityFilter

               

               

              Iam getting the following message in the browser.

               

               

              Inside Felix Filter

               

               

              <html><head><title>

              500

              Internal Server Error

              </title></head><body><h1>

              Internal Server Error

              </h1>

              </body></html>

               

               

              --Durga

              • 4. Re: Once apache webserver dispatches the request to CQ, where would it hit the CQ at first?
                justin_at_adobe Adobe Employee

                Durga-

                I believe the problem with your code is that the reponse is being flushed. This commits the response and you can't use forward after the response has been committed. See the JavaDoc here: http://docs.oracle.com/javaee/5/api/javax/servlet/RequestDispatcher.html#forward%28javax.s ervlet.ServletRequest,%20javax.servlet.ServletResponse%29

                 

                This is the exception I see logged:

                 

                30.04.2012 13:29:41.475 *INFO* [127.0.0.1 [1335806981472] GET /testVanityFilter HTTP/1.1] servletengine Servlet threw exception:  javax.servlet.ServletException: Response has been committed

                 

                Regards,

                Justin

                • 5. Re: Once apache webserver dispatches the request to CQ, where would it hit the CQ at first?
                  durganuvvula Community Member

                  Thanks Justin for your quick response. Now I could see that problem of 500 is solved.

                   

                  I could see that response.sendRedirect() is working fine and its redirecting to the products page. But my requirement does not suit the redirect as it is changing the url in the browser.

                   

                  But I could see that

                  RequestDispatcher requestDispatcher = request.getRequestDispatcher("/content/geometrixx/en/products.html");

                  requestDispatcher.forward(request, response);

                   

                  is yielding me the response

                   

                  The requested URL /content/geometrixx/en/products.html was not found on this server.

                   

                  When I look into the logs, I could see this.

                   

                  30.04.2012 12:39:44.854 *INFO* [0:0:0:0:0:0:0:1 [1335807584851] GET /content/geometrixx/en/products.html HTTP/1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Resource /testVanityFilter not found

                   

                  --Durga

                  • 6. Re: Once apache webserver dispatches the request to CQ, where would it hit the CQ at first?
                    durganuvvula Community Member

                    Thanks Justin for your responses. I am getting 500 error on using the SlingHttpServletRequest requestDispatcher forward. Please suggest.

                     

                    public void doFilter(ServletRequest request, ServletResponse response,

                                                  FilterChain chain) throws IOException, ServletException {

                     

                           SlingHttpServletRequest slingRequest = (SlingHttpServletRequest)request;
                            RequestDispatcherOptions requestDispatcherOptions = new RequestDispatcherOptions();
                            RequestDispatcher requestDispatcher = slingRequest.getRequestDispatcher("/content/geometrixx/en/products.html",requestDispatche rOptions);
                            requestDispatcher.forward(request, response);

                         }

                     

                    I have looked into the docuemntation for SlingHttpServletRequest to get the dispatcher. but there are no examples as such.

                     

                    http://dev.day.com/docs/en/cq/current/javadoc/org/apache/sling/api/Sli ngHttpServletRequest.html#getRequestDispatcher(org.apache.sling.api.re source.Resource, org.apache.sling.api.request.RequestDispatcherOptions)

                     

                    I have looked into the documentation of the sling servlet registration to understand the terms path, selectors, extensions etc.

                     

                    sling.servlet.paths = [ "/libs/sling/sample/html", "/libs/sling/sample/txt" ] sling.servlet.resourceTypes = [ "sling/unused" ] sling.servlet.selectors = [ "img" ] sling.servlet.extensions = [ "html", "txt", "json" ] 

                    A Servlet service registered with these properties is registered under the following paths:

                    • /libs/sling/sample/html
                    • /libs/sling/sample/txt

                     

                    RequestDispatchergetRequestDispatcher(Resource resource)
                              Same as getRequestDispatcher(Resource,RequestDispatcherOptions) but using empty options.
                    RequestDispatchergetRequestDispatcher(Resource resource, RequestDispatcherOptions options)
                              Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given resource.
                    RequestDispatchergetRequestDispatcher(String path, RequestDispatcherOptions options)
                              Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.

                     

                     

                    I have tried first 2 api's

                     

                    SlingHttpServletRequest slingRequest = (SlingHttpServletRequest)request;

                    Resource resource = slingRequest.getResourceResolver().getResource("/content/geometrixx/en/products.html");

                    RequestDispatcherOptions requestDispatcherOptions = new RequestDispatcherOptions();

                    RequestDispatcher requestDispatcher = slingRequest.getRequestDispatcher(resource,requestDispatcherOptions);

                     

                    Should I give the path in the resource with html extension. Here I am trying to render a CQ content page.

                     

                    Any pointers/suggestions to solve this requestDispatcher forward would be appreciable.

                     

                    --Durga

                    • 7. Re: Once apache webserver dispatches the request to CQ, where would it hit the CQ at first?
                      durganuvvula Community Member

                      Hi Justin,

                       

                      Pretty standard code of request dispatcher forwardign the request to products page works well with Sling Servlet but not with the filter I have registered with ExtHttpService.Is there any implications using the servlet rather than the filter from the security point of view(apache webserver hitting the servlet url vs filter url)

                       

                      /**

                      * @scr.component immediate="true" metatype="false"

                      * @scr.service interface="javax.servlet.Servlet"

                      * @scr.property name="sling.servlet.methods" values.0="GET"

                      * @scr.property name="sling.servlet.paths" values.0="/myvanity/forwardVanity"

                      *                values.1="/apps/myvanity/forwardVanity"

                      */

                      public class ForwardVanityServlet extends SlingSafeMethodsServlet {

                         @Override

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

                       

                                   try{

                                             RequestDispatcher rd = request.getRequestDispatcher("/content/geometrixx/en/products.html");

                                                    rd.forward(request, response);

                                   }

                                   catch(Exception re)

                                   {

                                             re.printStackTrace();

                                   }

                         }

                       

                      --Durga