在Sling中映射过滤器和servlet

时间:2012-04-12 07:02:54

标签: java servlets osgi servlet-filters sling

我有一个要求,即我的页面请求将被截取过滤器过滤。根据不同的条件,必须将请求重定向到不同的页面。

我创建了一个bundle并在bundle激活器中注册了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(..)时,会再次调用相同的过滤器,进入循环。

  3. 我正在开发这个OSGi包,以便在DayCQ环境中部署。

2 个答案:

答案 0 :(得分:2)

当你使用Sling时,你应该使用Sling标准的基于SCR的方法来注册你的组件,在http://sling.apache.org/site/filters.html描述过滤器和http://sling.apache.org/site/servlets.html描述servlet。

直接使用HttpService注册servlet和过滤器就像你正在做的那样将与SlingMainServlet“对抗”,SlingMainServlet应该在Sling应用程序中获取所有请求。

答案 1 :(得分:1)

你的Filter和Servlet应该有相同的URL映射。当然,过滤器将首先被调用。

让Filter预处理数据,让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); 
            }

这是经过试验和测试的代码