This is a small Adobe CQ's OSGi component for parsing URL Query String withing CQ template.

 

What does it do?

It takes a request.getQueryString(), which is a String, parses it, and stores the list of url query string parameters on a HashMap<String, String>.

How to use it?

  1. On your CQ's global.jsp <%@page import="com.citrix.readymarketplace.QueryStringParser"%>
  2. On your CQ component or Page component, initialize and call the getter function as below:
    QueryStringParser qStrParser = sling.getService(QueryStringParser.class);
    out.print(qStrParser.get("cq_ck", request.getQueryString())); // cq_ck is the url query string parameter that you want to get/access its value.

Below is the detailed codes of the CQ's OSGi component:

package com.mycompany.mypackage;

/**
 * @author Sothea Nim
 * @date July 3, 2014
 * @description for parsing URL query string into a HashMap<String, String>. It is being included in global.jsp
 */ 

import java.util.HashMap;

public class QueryStringParser {

    private static String qStr = "";
    private static HashMap<String, String> requestParams;

    /**
     * @param String qName
     * @param String requestQstr
     * @return String output
     */

    public static String get(String qName, String requestQstr)
    {
        if (requestParams == null)
        {
            qStr = requestQstr;
            requestParams =  new HashMap();
            parsing();
        }

        if (requestParams.containsKey(qName))
        {
            return requestParams.get(qName);
        }

        return "";
    }


    /**
     * Parses string (request.getQueryString()) and store in a HashMap<String, String> requestParams
     */
    private static void parsing()
    {
        String[] qStrList;
        if(qStr != null)
        {
            qStrList = qStr.split("&");

            for(int i = 0; i < qStrList.length; i++)
            {
                String[] pair = qStrList[i].split("=");

                if (pair.length == 1)
                {
                    requestParams.put(pair[0], "");
                }
                else if (pair.length == 2)
                {
                    requestParams.put(pair[0], pair[1]);
                }
            }
        }
    }
}