Solved

parsing url query params

Posted on 2010-09-22
4
427 Views
Last Modified: 2013-12-29
What’s the most efficient way to do this in java:

I need a method that would clean specified parameters from a url and another method that would keep the specified parameters in the url and remove all the other parameters.  So the signatures would be something like this:

public static String getUrlWithoutTheFollowingParameters (String url,  List<String> queryParams) ()

public static String getUrlWithOnlyTheFollowingParameters( String url, List<String> queryParams)()

I want to do this w/o using any third-party library.

Thanks!
0
Comment
Question by:aturetsky
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 10

Expert Comment

by:Hegemon
ID: 33738183
You can use a filter servlet to call your methods. Once they are called, just iterate the list of actual parameters, compare with your black/white lists and remove accordingly.
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33738227
split the url as per '?'

so if the url is

String url = "http://www.experts-exchange.com/Programming/Languages/Java/J2SE/Q_26492591.html?abc1=new1&abc2=new2&abc3=new3&abc4=new4&"

String urlItems = url.split("\?");
String queryString = urlItems[ 1 ];

//now you can get the query params pair
String queryParamsPair = queryString.split("&");

//construct a hashmap
Map queryParamsMap = new HashMap();

for ( int counter = 0; counter < queryParamsPair.length; counter++  )
{
    String items = queryParamsPair[ counter ].split("=");
    queryParamsMap.put( items[ 0 ], items[ 1 ] )
}

now you have got the hashmap, you can easily do add, remove of the query params and construct the url again
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33738551
How about this:
public static String getUrlWithoutTheFollowingParameters(String url, List<String> queryParams) {
	int i = url.indexOf("?") + 1;
	StringBuilder result = new StringBuilder(url.substring(0, i));

	for (int j = i; j < url.length(); j++) {
		if (url.charAt(j) == '=') {
			String key = url.substring(i, j);
			String param;

			while (j < url.length() && url.charAt(j) != '&') {
				j++;
			}

			param = url.substring(i, j);

			if (!queryParams.contains(key)) {
				result.append(url.substring(i, j));
				result.append('&');
			}

			i = j + 1;
		}
	}

	return result.toString().substring(0, result.length() - 1);
}

public static String getUrlWithOnlyTheFollowingParameters(String url, List<String> queryParams) {
	int i = url.indexOf("?") + 1;
	StringBuilder result = new StringBuilder(url.substring(0, i));

	for (int j = i; j < url.length(); j++) {
		if (url.charAt(j) == '=') {
			String key = url.substring(i, j);
			String param;

			while (j < url.length() && url.charAt(j) != '&') {
				j++;
			}

			param = url.substring(i, j);

			if (queryParams.contains(key)) {
				result.append(url.substring(i, j));
				result.append('&');
			}

			i = j + 1;
		}
	}

	return result.toString().substring(0, result.length() - 1);
}

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 33738701
Note, the only real difference between the two above is at the line:

    if (!queryParams.contains(key)) {

If you wanted, you could create an enum for a "mode" and then have one function:
public static String ScrubUrl(String url, List<String> queryParams, Mode scrubMode) {
	int i = url.indexOf("?") + 1;
	StringBuilder result = new StringBuilder(url.substring(0, i));

	for (int j = i; j < url.length(); j++) {
		if (url.charAt(j) == '=') {
			String key = url.substring(i, j);
			String param;

			while (j < url.length() && url.charAt(j) != '&') {
				j++;
			}

			param = url.substring(i, j);

			switch (scrubMode) {
				case KeepParams:
					if (queryParams.contains(key)) {
						result.append(url.substring(i, j));
						result.append('&');
					}
					break;
				case RemoveParams:
					if (!queryParams.contains(key)) {
						result.append(url.substring(i, j));
						result.append('&');
					}
					break;
			}

			i = j + 1;
		}
	}
	return result.toString().substring(0, result.length() - 1);
}

// Mode.java
public enum Mode
{
    KeepParams,
    RemoveParams,
}

Open in new window

0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question