Wednesday, November 10, 2010

How to create redirector in WSO2 Stratos

The url's that we typed normally goes through the redirector and filters so if you want to add
some changes you must aware about these.there so many files with same name in different components
so we have to redirect correctly and carefully
Let's see how we can add redirector
First we have to open redirector component in idea


then we have to create redirector file as follows and add to filters

package org.wso2.stratos.redirector.servlet.ui.filters;
import org.wso2.stratos.common.constants.StratosConstants;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;

public class AboutPageFilter implements Filter {
    ServletContext context;
    public void init(FilterConfig filterConfig) throws ServletException {
        context = filterConfig.getServletContext();

    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse, FilterChain filterChain) throws
            IOException, ServletException {
        if (!(servletRequest instanceof HttpServletRequest)) {
            // no filtering
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        if (request.getAttribute(StratosConstants.TENANT_SPECIFIC_URL_RESOLVED) != null) {
            // if the tenant specifc url filter is passed, we are not calling the login.jsp filter

        String url = request.getRequestURI();
        // use the name admin in place of admin
        url = url.replaceAll("admin/docs/about.html", "tenant-dashboard/docs/about.html"); //url replace here
        RequestDispatcher requestDispatcher =
        requestDispatcher.forward(request, servletResponse);

    public void destroy() {
        // nothing to destory

Then we have to register that filter in file. We can add new method to utils file to register our filter
as below code registers our filter

public static void adminAboutRegisterServlet(BundleContext bundleContext) throws Exception {
        if (!CarbonUtils.isRemoteRegistry()) {
            HttpServlet reirectorServlet = new HttpServlet() {
                // the redirector filter will forward the request before this servlet is hit
                protected void doGet(HttpServletRequest request, HttpServletResponse response)
                        throws ServletException, IOException {
            Filter redirectorFilter = new AboutPageFilter();
            Dictionary redirectorServletAttributes = new Hashtable(2);
            Dictionary redirectorParams = new Hashtable(2);
            redirectorParams.put("url-pattern", "/carbon/admin/docs/about.html"); //This url pattern will identified and apply filter
            redirectorParams.put("associated-filter", redirectorFilter);
            redirectorParams.put("servlet-attributes", redirectorServletAttributes);
            redirectorServiceRegistration = bundleContext.registerService(Servlet.class.getName(),
                    reirectorServlet, redirectorParams);


Then we have to activate our filter. We can activate our filter in file

public class RedirectorServletUIComponent {
     private static Log log = LogFactory.getLog(RedirectorServletUIComponent.class);

    protected void activate(ComponentContext context) {
        try {

//Here we have activated our filter
            log.debug("******* Multitenancy Redirector Servlet UI bundle is activated ******* ");
        } catch (Exception e) {
            log.error("******* Multitenancy Redirector Servlet UI bundle failed activating ****", e);
    protected void deactivate(ComponentContext context) {
        log.debug("******* Multitenancy Redirector Servlet UI bundle is deactivated ******* ");

So now we have successfully add filter and activated it.Now you can use this as you need in different locations in WSO2 Stratos

1 comment:

  1. yay... thumbs up mate.. :)

    Kathiravelu Pradeeban.