2012-04-12 129 views
2

我有一個要求,其中我對頁面的請求是通過攔截過濾器進行過濾。根據不同的條件,請求必須重定向到不同的頁面。映射過濾器和servlet在吊帶

我創建了一個包和註冊的servlet和過濾器在包激活,採取了線索,從here

以下是代碼片段:

Hashtable initParams = new Hashtable(); // to pass a dictionary object to 
             // service.register 
initParams.put("sling.servlet.resourceTypes","/login"); 
initParams.put("sling.servlet.extensions","jsp"); 
service.registerServlet("/myServlet", this.myServlet, initParams, null); 
initParams = new Hashtable(); 
initParams.put("sling.filter.scope","REQUEST"); 
service.registerFilter(this.checkPageRequest, "/.*",null, 2, null); 
service.registerFilter(this.checkValidSession, "/.*", null, 1, null); 

我面臨兩個問題:

  1. 我無法確定我在哪裏映射一個過濾器,相應的servlet。我的理解是,將調用checkPageRequest過濾器,然後調用checkValidSession。如果沒有requestdispatcher.forward(..),那麼myServlet servlet將被命中。在我的情況下,使用filterChain.doFilter(..)按預期調用過濾器,但該servlet沒有被調用。

  2. 由於我的過濾器必須攔截所有的頁面請求,所以當我對任何頁面執行requestdispatcher.forward(..)時,會再次調用相同的過濾器,進入循環。

我正在開發這個OSGi bundle來部署在DayCQ環境中。

回答

1

嗯,你的過濾器和Servlet應該有相同的URL映射。過濾器會先被調用。

讓預處理的數據篩選,並讓該servlet在請求對象添加數據,並派遣它的JSP。參見實施例下面,

@SlingFilter(order=1) 

@Properties({ 
    @Property(name="service.pid", value="com.videojet.hiresite.filters.AddNewUserFilter",propertyPrivate=false), 
    @Property(name="service.description",value="Preproceesing Filter", propertyPrivate=false), 
    @Property(name="service.vendor",value="XXX Tech", propertyPrivate=false), 
    @Property(name="pattern",value="/services/videojet/v1/.*", propertyPrivate=false) 
// The Property Above property Maps your Filter to URL 
}) 
public class AddNewUserFilter implements javax.servlet.Filter{ 

    private final Logger log = LoggerFactory.getLogger(this.getClass()); 
    public void destroy() { 
     // TODO Auto-generated method stub 

    } 

    public void doFilter(ServletRequest request, ServletResponse response, 
      FilterChain chain) throws IOException, ServletException { 

     log.info("AddNewUserFilter Invoked Invoked"); 
// Preprocessing here and just call chain.doFilter(xxx); 

並且servlet

@SlingServlet(
     paths={"/services/videojet/v1/AddNewUserController/view","/services/videojet/v1/AddNewUserController/userExists" 
       ,"/services/videojet/v1/AddNewUserController/addUser"} 
) 
@Properties({ 
    @Property(name="service.pid", value="com.videojet.hiresite.controllers.AddNewUserController",propertyPrivate=false), 
    @Property(name="service.description",value="Adds new user", propertyPrivate=false), 
    @Property(name="service.vendor",value="XXX Tech", propertyPrivate=false) 
}) 
public class AddNewUserController extends SlingAllMethodsServlet{ 

    /** 
    * @author Oliver Kaunds 
    */ 
    ///content/dam/videojetdocuments 
    private static final long serialVersionUID = 1L; 

    private final Logger log = LoggerFactory.getLogger(this.getClass()); 

    @Reference 
    protected AddNewUserService addNewUserService; 

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


     log.info(":: Do GET Called "); 
     String path =request.getRequestURI(); 
     log.info("PATH :: "+ path); 
     try{ 
      if(path.equals("/services/videojet/v1/AddNewUserController/view")) 
      { 
       /* Do the dispatching here One Servlet can have more than one Request 
       Mappings . My Each mapping serves a purpose. */ 

       List<HRRepresentative> list =addNewUserService.getHRRepresentative(); 
       request.setAttribute("hrList",list); 
       HttpSession session = request.getSession(); 
       String userOperation =(String)session.getAttribute("userOp"); 
       request.setAttribute("userOp", userOperation); 
       session.removeAttribute("userOp"); 
       //throw new Exception("My Exception"); 
       RequestDispatcher dispatcher =request.getRequestDispatcher("/content/videojet/en/addnewuser.html"); // Dispatch to JSP here 
       dispatcher.forward(request, response); 
      } 

這是一個嘗試和測試碼