All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.textrecruit.ustackserver.util.TestCallParams Maven / Gradle / Ivy

There is a newer version: 1.0.31
Show newest version
package com.textrecruit.ustackserver.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.SortedMap;
import java.util.TreeMap;

import com.textrecruit.ustackserverapi.params.types.ParameterDefinitionInt;

public class TestCallParams
{
    SortedMap sortedParamMap = new TreeMap();
	String postData;
	
	public TestCallParams(String name, String val)
	{
		append(name, val);
	}

	public TestCallParams(String pd) {
		postData = pd;
	}

	public TestCallParams(ParameterDefinitionInt name, String val)
	{
		append(name, val);
	}
	
	public TestCallParams append(String name, String val)
	{
		sortedParamMap.put(name, val);
		return this;
	}
	
	public TestCallParams append(ParameterDefinitionInt name, String val)
	{
		sortedParamMap.put(name.getName(), val);
		return this;
	}

	public String getPostData() {
		return postData;
	}

	public void setPostData(String postData) {
		this.postData = postData;
	}

	public SortedMap getSortedParamMap() {
		return sortedParamMap;
	}

	public void setSortedParamMap(SortedMap sortedParamMap) {
		this.sortedParamMap = sortedParamMap;
	}

	public String getQueryString() throws UnsupportedEncodingException
	{
		StringBuffer q = new StringBuffer();
		
		Iterator it = sortedParamMap.keySet().iterator();
		while (it.hasNext())
		{
			if (q.length() > 0)
				q.append("&");

			String k = it.next();
			String v = sortedParamMap.get(k);
			q.append(urlEncodePair(k, v));
		}
		
		return q.toString();
	}
	
	private String urlEncodePair(String k, String v) throws UnsupportedEncodingException {
		
		if (k == null)
			return "";
		if (v == null)
			v = "";

		return String.format("%s=%s", 
				URLEncoder.encode(k, "UTF-8"), 
				URLEncoder.encode(v, "UTF-8"));
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy