在CQ5.6中进行任何处理之前,先过滤请求URL [英] Filter request URL before any processing in CQ5.6

查看:76
本文介绍了在CQ5.6中进行任何处理之前,先过滤请求URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的CQ5.6应用程序中。用户点击网址后,我需要使用某些参数对其进行编辑。所有这些必须在Sling开始处理URL之前发生。

In my CQ5.6 application,. as soon as the user hits a URL, I need to edit it using a certain parameters. All this must happen before Sling starts processing the URL.

我基本上需要转换如下网址:
www.mysite.fr ->转换到-> / content / mysite / fr /

I basically need to convert the URL like: www.mysite.fr --> converts to --> /content/mysite/fr/

等等...

我知道我需要为此创建一个OSGi捆绑包,但是应该使用哪个API来确保URL首先由我的类过滤,然后由
满足吊索?

I understand I'll need to create an OSGi bundle for this, but which API should I use to ensure that the URL is filtered by my class first and then catered by Sling. ?

推荐答案

如果您想为多个网站提供基于代码的解决方案(并且不想管理/ etc / map ),您可以设置自己的过滤器:

if you want a code-based solution for multiple websites (and you don't want to manage /etc/map) you can setup your own Filter:

package your.package;

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

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;

@Component(immediate=true, enabled=true)
@Service(value=Filter.class)
@Properties({
    @Property(name="sling.filter.scope", value="REQUEST", propertyPrivate=true),
    @Property(name="service.ranking", intValue=-10000, propertyPrivate=true)
})
public class YourFilter implements Filter {
    private static final Logger log = LoggerFactory.getLogger(ProductSEOFilter.class);

    @Activate
    protected void activate(ComponentContext ctx) throws Exception {
    }

    @Deactivate
    protected void deactivate() throws Exception {
    }

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws java.io.IOException, ServletException {
        String lang = "en";

        // 1. get domain and path
        // 2. check if your conditions are met
        // 3. extract language from domain
        // 4. internal redirect

        RequestDispatcher dispatch = request.getRequestDispatcher("/content/mysite/" + lang);
        dispatch.forward(request, response);
    }

    public void destroy() {
    }
}

您无需费心检查和传递查询字符串-这些在分配器中进行。它只需要一个新的URL即可转发。

you don't need to bother checking for and passing querystrings--those are carried on in the dispatcher. it only needs a new url to forward to.

这篇关于在CQ5.6中进行任何处理之前,先过滤请求URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆