Filter request URL before any processing in CQ5.6

℡╲_俬逩灬. 提交于 2020-01-04 09:56:27

问题


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.

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

and so on....

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. ?


回答1:


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() {
    }
}

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.




回答2:


You can do this via Sling URL Mapping without the need for a filter. The simpliest way to achieve this is to create a node under the /etc/map directory with a resource type of sling:Mapping & called www.mysite.fr.

This then takes a property of sling:internalRedirect — if an incoming request matches the node name, this property is appended to the path to continue with internal resource resolution.

<map>
    <http jcr:primaryType="sling:OrderedFolder">
        <www.mysite.fr 
            jcr:primaryType="sling:Mapping"
            sling:internalRedirect="/content/mysite/fr"/>
    </http>
</map>

The above will ensure any request coming to www.mysite.fr is resolved to www.mysite.fr/content/mysite/fr.

You can also pattern matching based on regex properties rather than names & include port numbers or schemes too. The full documentation is available on the Sling website.



来源:https://stackoverflow.com/questions/18440287/filter-request-url-before-any-processing-in-cq5-6

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!