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

1

then we have to create redirector file AboutPageFilter.java 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;
import java.io.IOException;

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
            return;
        }
        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
            return;
        }

        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 =
                request.getRequestDispatcher(url);
        requestDispatcher.forward(request, servletResponse);
    }

    public void destroy() {
        // nothing to destory
    }
}

Then we have to register that filter in utils.java file. We can add new method to utils file to register our filter
as follows.so 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 RedirectorServletUIComponent.java file

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

    protected void activate(ComponentContext context) {
        try {
            Util.domainRegisterServlet(context.getBundleContext());
            Util.loginJspRegisterServlet(context.getBundleContext());
            Util.indexJspRegisterServlet(context.getBundleContext());
            Util.adminDocsRegisterServlet(context.getBundleContext());
           Util.adminAboutRegisterServlet(context.getBundleContext());

//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:

Empowering the Future of API Management: Unveiling the Journey of WSO2 API Platform for Kubernetes (APK) Project and the Anticipated Alpha Release

  Introduction In the ever-evolving realm of API management, our journey embarked on the APK project eight months ago, and now, with great a...