<%@ include file="../../../admin/EgTrackerAJSON.jsp"%>
<%@ page import="java.io.*" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.io.File" %>
<%@ page import="com.eg.IniFile" %>
<%@ page import="com.egurkha.util.multipart.*" %>
<%@ page import="com.egurkha.util.EgWebServiceConnector" %>
<%@ page import="com.egurkha.util.StringUtils" %>
<%@ page import="com.egurkha.monitor.JsonDataParser" %>

<%@ page import="net.sf.json.JSONObject" %>
<%@ page import="net.sf.json.JSONArray" %>
<%@ page import="net.sf.json.JSONSerializer" %>
<%@ page language="java" contentType="application/json; charset=UTF-8" pageEncoding="UTF-8" %>

<%!

	List dateFormatsList=new ArrayList();
%>
<%	IniFile iniUI= new IniFile(EgInstallInfo.getInstallDir()+ "/manager/config/eg_ui.ini");
	String dateFormats[]=iniUI.getVariables("DATE_FORMATS_AVAILABLE");
	iniUI=null;
	if(dateFormats!=null && dateFormats.length<=0)
	{
		String dateFormatsArr1[]={"dd/MM/yyyy HH:mm","MM/dd/yyyy HH:mm","dd-MM-yyyy HH:mm","yyyy-MM-dd HH:mm","yy-MM-dd HH:mm","dd-MM-yy HH:mm","MM-dd-yy HH:mm","yy-M-d HH:mm","d-M-yy HH:mm","MMM dd, yyyy  HH:mm","yy-M-d HH:mm:ss","d-M-yy HH:mm:ss","dd/MM/yyyy HH:mm:ss","MM/dd/yyyy HH:mm:ss","dd-MM-yyyy HH:mm:ss","yyyy-MM-dd HH:mm:ss","yy-MM-dd HH:mm:ss","dd-MM-yy HH:mm:ss","MM-dd-yy HH:mm:ss","MMM dd, yyyy  HH:mm:ss"};
		dateFormats=dateFormatsArr1;
	}
	int dateFormatSize=dateFormats.length;
	
	for(int s=0;s<dateFormatSize;s++)
	{
		dateFormatsList.add(dateFormats[s]+" HH:mm:ss");
		dateFormatsList.add(dateFormats[s]+" HH:mm");
	}
%>
<%
	response.setHeader("Cache-Control","no-store");
	response.setHeader("Pragma","no-cache");
	response.setDateHeader("Expires", -1);
	response.setContentType("application/json;charset=UTF-8");

	String action=request.getParameter("action");
	JSONObject rootJson=new JSONObject();
	if(!StringUtils.isNull(action))
	{
		if(action.equalsIgnoreCase("loadDataForConfigWindow"))
		{
			EgWebServiceConnector wsc=new EgWebServiceConnector();

			String tmpProfName=request.getParameter("dashboardName");
			String restDSNName=request.getParameter("restDSNName");
			
			String selectedWidgetType=request.getParameter("selectedWidgetType");
			String selectedWidgetAttrs=request.getParameter("selectedWidgetAttrs");
			String headerRow=request.getParameter("headerRow");

			String restWS_URL=request.getParameter("restWS_URL");
			String restWS_Port=request.getParameter("restWS_Port");
			String ws_username=request.getParameter("ws_username");
			String ws_pwd=request.getParameter("ws_pwd");
			String isProxy=request.getParameter("isProxy");
			String proxyHostName=request.getParameter("proxyHostName");
			String proxyPort=request.getParameter("proxyPort");
			String proxyUserName=request.getParameter("proxyUserName");
			String proxyPassword=request.getParameter("proxyPassword");
			String isProxyAuthenticated=request.getParameter("isProxyAuthenticated");
			String httpMethod=request.getParameter("httpMethod");
			String configuredHeaders=request.getParameter("configuredHeaders");
			String selectedBodyType=request.getParameter("selectedBodyType");
			String rawJsonBody=request.getParameter("rawJsonBody");
			String configuredBodyUrlEncoded=request.getParameter("configuredBodyUrlEncoded");
			String configuredParams=request.getParameter("configuredParams");
			
			Hashtable wsConnParams=new Hashtable();
				
			if(!StringUtils.isNull(restWS_URL))
			{
				wsConnParams.put("ExtrnalDataSource_restWS_URL",restWS_URL);
			}
			if(!StringUtils.isNull(restWS_Port))
			{
				wsConnParams.put("ExtrnalDataSource_restWS_Port",restWS_Port);
			}
			if(!StringUtils.isNull(configuredHeaders))
			{
				wsConnParams.put("ExtrnalDataSource_configuredHeaders",configuredHeaders);
			}
			if(!StringUtils.isNull(rawJsonBody))
			{
				wsConnParams.put("ExtrnalDataSource_rawJsonBody",rawJsonBody);
			}
			if(!StringUtils.isNull(configuredBodyUrlEncoded))
			{
				wsConnParams.put("ExtrnalDataSource_configuredBodyUrlEncoded",configuredBodyUrlEncoded);
			}
			if(!StringUtils.isNull(ws_pwd))
			{
				wsConnParams.put("ExtrnalDataSource_ws_pwd",ws_pwd);
			}
			if(!StringUtils.isNull(ws_username))
			{
				wsConnParams.put("ExtrnalDataSource_ws_username",ws_username);
			}
			if(!StringUtils.isNull(isProxy))
			{
				wsConnParams.put("ExtrnalDataSource_isProxy",isProxy);
			}
			if(!StringUtils.isNull(proxyHostName))
			{
				wsConnParams.put("ExtrnalDataSource_proxyHostName",proxyHostName);
			}
			if(!StringUtils.isNull(proxyPort))
			{
				wsConnParams.put("ExtrnalDataSource_proxyPort",proxyPort);
			}
			if(!StringUtils.isNull(proxyUserName))
			{
				wsConnParams.put("ExtrnalDataSource_proxyUserName",proxyUserName);
			}
			if(!StringUtils.isNull(proxyPassword))
			{
				wsConnParams.put("ExtrnalDataSource_proxyPassword",proxyPassword);
			}
			if(!StringUtils.isNull(isProxyAuthenticated))
			{
				wsConnParams.put("ExtrnalDataSource_isProxyAuthenticated",isProxyAuthenticated);
			}
			if(!StringUtils.isNull(proxyHostName))
			{
				wsConnParams.put("ExtrnalDataSource_proxyHostName",proxyHostName);
			}
			if(!StringUtils.isNull(proxyPort))
			{
				wsConnParams.put("ExtrnalDataSource_proxyPort",proxyPort);
			}
			if(!StringUtils.isNull(proxyUserName))
			{
				wsConnParams.put("ExtrnalDataSource_proxyUserName",proxyUserName);
			}
			if(!StringUtils.isNull(proxyPassword))
			{
				wsConnParams.put("ExtrnalDataSource_proxyPassword",proxyPassword);
			}
			if(!StringUtils.isNull(httpMethod))
			{
				wsConnParams.put("ExtrnalDataSource_httpMethod",httpMethod);
			}
			if(!StringUtils.isNull(configuredParams))
			{
				wsConnParams.put("ExtrnalDataSource_configuredParams",configuredParams);
			}

			wsc.setWSParams(wsConnParams);
			String jsonStr=wsc.getResponseForRestURL();
			
			JSONObject jsonTreeObj=getJSONTree(jsonStr);
			rootJson.put("jsonkeys",jsonTreeObj);
			out.println(rootJson);
		}
		else if(action.equalsIgnoreCase("saveRESTDSN"))
		{
			String icTestName=request.getParameter("test");
			String restDSNName=request.getParameter("restDSNName");
			String selectedWidgetType=request.getParameter("selectedWidgetType");
			String selectedWidgetAttrs=request.getParameter("selectedWidgetAttrs");
			String headerRow=request.getParameter("headerRow");

			String restWS_URL=request.getParameter("restWS_URL");
			String restWS_Port=request.getParameter("restWS_Port");
			String ws_username=request.getParameter("ws_username");
			String ws_pwd=request.getParameter("ws_pwd");
			String isProxy=request.getParameter("isProxy");
			String proxyHostName=request.getParameter("proxyHostName");
			String proxyPort=request.getParameter("proxyPort");
			String proxyUserName=request.getParameter("proxyUserName");
			String proxyPassword=request.getParameter("proxyPassword");
			String isProxyAuthenticated=request.getParameter("isProxyAuthenticated");
			String httpMethod=request.getParameter("httpMethod");
			String configuredHeaders=request.getParameter("configuredHeaders");
			String selectedBodyType=request.getParameter("selectedBodyType");
			String rawJsonBody=request.getParameter("rawJsonBody");
			String configuredBodyUrlEncoded=request.getParameter("configuredBodyUrlEncoded");
			String configuredParams=request.getParameter("configuredParams");
			String restWS_Fields=request.getParameter("restWS_Fields");
			String dataFieldParentNode=request.getParameter("dataFieldParentNode");
			String dataFieldDataTypes=request.getParameter("dataFieldDataTypes");
			String validateRESTResponse=request.getParameter("validateRESTResponse");
			String basicAuth_Username=request.getParameter("basicAuth_Username");
			String basicAuth_pwd=request.getParameter("basicAuth_pwd");


			basicAuth_Username=StringUtils.decodeSecurityChar(basicAuth_Username);
			basicAuth_pwd=StringUtils.decodeSecurityChar(basicAuth_pwd);
			configuredParams=StringUtils.decodeSecurityChar(configuredParams);
			selectedWidgetAttrs=StringUtils.decodeSecurityChar(selectedWidgetAttrs);
			restWS_URL=StringUtils.decodeSecurityChar(restWS_URL);
			ws_username=StringUtils.decodeSecurityChar(ws_username);
			ws_pwd=StringUtils.decodeSecurityChar(ws_pwd);
			proxyHostName=StringUtils.decodeSecurityChar(proxyHostName);
			proxyUserName=StringUtils.decodeSecurityChar(proxyUserName);
			proxyPassword=StringUtils.decodeSecurityChar(proxyPassword);
			configuredHeaders=StringUtils.decodeSecurityChar(configuredHeaders);
			rawJsonBody=StringUtils.decodeSecurityChar(rawJsonBody);
			configuredBodyUrlEncoded=StringUtils.decodeSecurityChar(configuredBodyUrlEncoded);
			restWS_Fields=StringUtils.decodeSecurityChar(restWS_Fields);
			dataFieldParentNode=StringUtils.decodeSecurityChar(dataFieldParentNode);
			dataFieldDataTypes=StringUtils.decodeSecurityChar(dataFieldDataTypes);

			Hashtable wsInitParams=new Hashtable();
			if(!StringUtils.isNull(restWS_URL))
			{
				wsInitParams.put("ExtrnalDataSource_restWS_URL",restWS_URL);
			}
			if(!StringUtils.isNull(restWS_Port))
			{
				wsInitParams.put("ExtrnalDataSource_restWS_Port",restWS_Port);
			}
			if(!StringUtils.isNull(headerRow))
			{
				wsInitParams.put("ExtrnalDataSource_headerRow",headerRow);
			}
			if(!StringUtils.isNull(ws_pwd))
			{
				wsInitParams.put("ExtrnalDataSource_ws_pwd",ws_pwd);
			}
			if(!StringUtils.isNull(ws_username))
			{
				wsInitParams.put("ExtrnalDataSource_ws_username",ws_username);
			}
			if(!StringUtils.isNull(isProxy))
			{
				wsInitParams.put("ExtrnalDataSource_isProxy",isProxy);
			}
			if(!StringUtils.isNull(proxyHostName))
			{
				wsInitParams.put("ExtrnalDataSource_proxyHostName",proxyHostName);
			}
			if(!StringUtils.isNull(proxyPort))
			{
				wsInitParams.put("ExtrnalDataSource_proxyPort",proxyPort);
			}
			if(!StringUtils.isNull(proxyUserName))
			{
				wsInitParams.put("ExtrnalDataSource_proxyUserName",proxyUserName);
			}
			if(!StringUtils.isNull(proxyPassword))
			{
				wsInitParams.put("ExtrnalDataSource_proxyPassword",proxyPassword);
			}
			if(!StringUtils.isNull(isProxyAuthenticated))
			{
				wsInitParams.put("ExtrnalDataSource_isProxyAuthenticated",isProxyAuthenticated);
			}
			if(!StringUtils.isNull(proxyHostName))
			{
				wsInitParams.put("ExtrnalDataSource_proxyHostName",proxyHostName);
			}
			if(!StringUtils.isNull(proxyPort))
			{
				wsInitParams.put("ExtrnalDataSource_proxyPort",proxyPort);
			}
			if(!StringUtils.isNull(proxyUserName))
			{
				wsInitParams.put("ExtrnalDataSource_proxyUserName",proxyUserName);
			}
			if(!StringUtils.isNull(proxyPassword))
			{
				wsInitParams.put("ExtrnalDataSource_proxyPassword",proxyPassword);
			}
			if(!StringUtils.isNull(httpMethod))
			{
				wsInitParams.put("ExtrnalDataSource_httpMethod",httpMethod);
			}
			if(!StringUtils.isNull(configuredHeaders))
			{
				wsInitParams.put("ExtrnalDataSource_configuredHeaders",configuredHeaders);
			}
			if(!StringUtils.isNull(selectedBodyType))
			{
				wsInitParams.put("ExtrnalDataSource_selectedBodyType",selectedBodyType);
			}
			if(!StringUtils.isNull(rawJsonBody))
			{
				wsInitParams.put("ExtrnalDataSource_rawJsonBody",rawJsonBody);
			}
			if(!StringUtils.isNull(configuredBodyUrlEncoded))
			{
				wsInitParams.put("ExtrnalDataSource_configuredBodyUrlEncoded",configuredBodyUrlEncoded);
			}
			if(!StringUtils.isNull(configuredParams))
			{
				wsInitParams.put("ExtrnalDataSource_configuredParams",configuredParams);
			}
			if(!StringUtils.isNull(restWS_Fields))
			{
				wsInitParams.put("ExtrnalDataSource_selectedDataFields",restWS_Fields);
			}
			if(!StringUtils.isNull(dataFieldParentNode))
			{
				wsInitParams.put("ExtrnalDataSource_dataFieldParentNode",dataFieldParentNode);
			}
			if(!StringUtils.isNull(dataFieldDataTypes))
			{
				wsInitParams.put("ExtrnalDataSource_DataTypes",dataFieldDataTypes);
			}
			EgWebServiceConnector wsc=new EgWebServiceConnector();
			wsc.setWSParams(wsInitParams);
			String jsonStr="";
			JSONObject jsonTreeObj=new JSONObject();
			boolean saveDSConfig=true;
			if(validateRESTResponse!=null && validateRESTResponse.equalsIgnoreCase("yes"))
			{
				jsonStr=wsc.getResponseForRestURL();
				jsonTreeObj=getJSONTree(jsonStr);
				if(jsonTreeObj==null || jsonTreeObj.isEmpty())
				{
					saveDSConfig=false;
					rootJson.put("errorMsg","No Data Available");
				}
			}
			if(!saveDSConfig)
			{
				rootJson.put("errorMsg","No Data Available");
			}
			else
			{
				rootJson.put("errorMsg","-");

				IniFile iniFileSyncObj= EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_udtests.ini",true);
				String existingRestDSNName=iniFileSyncObj.getValue("REST_DSN",icTestName);
				HashSet dsnList=new HashSet();
				if(!StringUtils.isNull(existingRestDSNName))
				{
					StringTokenizer stkExistingDsns=new StringTokenizer(existingRestDSNName,",");
					while(stkExistingDsns.hasMoreTokens())
					{
						String existingDSN=stkExistingDsns.nextToken();
						dsnList.add(existingDSN);
					}
				}
				Vector widVars=new Vector();
				Vector widVals=new Vector();
				if(wsInitParams!=null && !wsInitParams.isEmpty())
				{
					Iterator itrWSParams=wsInitParams.entrySet().iterator();
					while(itrWSParams.hasNext())
					{
						Map.Entry mp=(Map.Entry)itrWSParams.next();
						if(mp!=null)
						{
							String wsParamKey=(String)mp.getKey();
							String wsParamVal=(String)mp.getValue();
							if(!StringUtils.isNull(wsParamVal))
							{
								widVars.add(wsParamKey);
								widVals.add(wsParamVal);
							}
						}
					}
					if(!StringUtils.isNull(basicAuth_Username))
					{
						widVars.add("basicAuth_Username");
						widVals.add(basicAuth_Username);
					}
					if(!StringUtils.isNull(basicAuth_pwd))
					{
						widVars.add("basicAuth_pwd");
						widVals.add(basicAuth_pwd);
					}
					try
					{
						iniFileSyncObj.setValues(icTestName+"_"+restDSNName,widVars,widVals,true);
						if(dsnList!=null && !dsnList.contains(restDSNName))
						{
							existingRestDSNName=existingRestDSNName+","+restDSNName;
						}
						else
						{
							existingRestDSNName=restDSNName;
						}
						if(existingRestDSNName!=null && existingRestDSNName.indexOf(",")==0)
						{
							existingRestDSNName=existingRestDSNName.substring(1);
						}
						iniFileSyncObj.setValue("REST_DSN",icTestName,existingRestDSNName);
						iniFileSyncObj.saveFile(true);
					}
					catch (Throwable t)
					{
						t.printStackTrace();
					}
					finally
					{
						EgConfigFilesSync.releaseInstance(iniFileSyncObj);
					}
				}
				JSONObject dsnObj=new JSONObject();
				JSONArray dsnConfigDetailsArr=new JSONArray();
				if(!StringUtils.isNull(restWS_URL))
				{
					JSONObject jRec=new JSONObject();
					jRec.put("displayName",_mapping.getTextDescription("monitor.common.URL"));
					jRec.put("key","URL");
					jRec.put("value",restWS_URL);
					dsnConfigDetailsArr.add(jRec);
				}
			/*	if(!StringUtils.isNull(restWS_Port))
				{
					JSONObject jRec=new JSONObject();
					jRec.put("displayName",_mapping.getTextDescription("monitor.common.Web Port"));
					jRec.put("key","Port");
					jRec.put("value",restWS_Port);
					dsnConfigDetailsArr.add(jRec);
				}*/
				if(!StringUtils.isNull(ws_username))
				{
					JSONObject jRec=new JSONObject();
					jRec.put("displayName",_mapping.getTextDescription("monitor.common.User name"));
					jRec.put("key","User name");
					jRec.put("value",ws_username);
					dsnConfigDetailsArr.add(jRec);
				}
				if(!StringUtils.isNull(ws_pwd))
				{
					JSONObject jRec=new JSONObject();
					jRec.put("displayName",_mapping.getTextDescription("monitor.common.Password"));
					jRec.put("key","Password");
					jRec.put("value",ws_pwd);
					dsnConfigDetailsArr.add(jRec);
				}
				if(!StringUtils.isNull(isProxy))
				{
					JSONObject jRec=new JSONObject();
					jRec.put("displayName",_mapping.getTextDescription("monitor.common.Is Proxy enabled")+"?");
					jRec.put("key","Is Proxy enabled?");
					jRec.put("value",isProxy);
					dsnConfigDetailsArr.add(jRec);
				}
				if(!StringUtils.isNull(proxyHostName))
				{
					JSONObject jRec=new JSONObject();
					jRec.put("displayName",_mapping.getTextDescription("monitor.common.Proxy Hostname"));
					jRec.put("key","proxyHostname");
					jRec.put("value",proxyHostName);
					dsnConfigDetailsArr.add(jRec);
				}
				if(!StringUtils.isNull(proxyPort))
				{
					JSONObject jRec=new JSONObject();
					jRec.put("displayName",_mapping.getTextDescription("monitor.common.Proxy Port"));
					jRec.put("key","proxyPort");
					jRec.put("value",proxyPort);
					dsnConfigDetailsArr.add(jRec);
				}
				if(!StringUtils.isNull(proxyUserName))
				{
					JSONObject jRec=new JSONObject();
					jRec.put("displayName",_mapping.getTextDescription("monitor.common.Proxy User name"));
					jRec.put("key","proxyUserName");
					jRec.put("value",proxyUserName);
					dsnConfigDetailsArr.add(jRec);
				}
				if(!StringUtils.isNull(proxyPassword))
				{
					JSONObject jRec=new JSONObject();
					jRec.put("displayName",_mapping.getTextDescription("monitor.common.Proxy Password"));
					jRec.put("key","proxyPassword");
					jRec.put("value",proxyPassword);
					dsnConfigDetailsArr.add(jRec);
				}
				if(!StringUtils.isNull(isProxyAuthenticated))
				{
					JSONObject jRec=new JSONObject();
					jRec.put("displayName",_mapping.getTextDescription("monitor.common.Is Proxy Authenticated")+"?");
					jRec.put("key","isProxyAuthenticated");
					jRec.put("value",isProxyAuthenticated);
					dsnConfigDetailsArr.add(jRec);
				}
				if(!StringUtils.isNull(httpMethod))
				{
					JSONObject jRec=new JSONObject();
					jRec.put("displayName",_mapping.getTextDescription("monitor.common.Http Method"));
					jRec.put("key","HTTP Method");
					jRec.put("value",httpMethod);
					dsnConfigDetailsArr.add(jRec);
				}
				if(!StringUtils.isNull(configuredHeaders))
				{
					JSONObject jRec=new JSONObject();
					jRec.put("displayName",_mapping.getTextDescription("monitor.common.Configured Headers"));
					jRec.put("key","ConfiguredHeaders");
					LinkedHashMap<String,String> headers=new LinkedHashMap<String,String>();
					
					JSONArray headerArr=new JSONArray();
					ArrayList headerConfigList=EgUtils.egTokenizer(configuredHeaders,"!^!",false,false,false);
					Iterator itrHeaderConfList=headerConfigList.iterator();
					while (itrHeaderConfList.hasNext())
					{ 
						String keyValHeader=(String)itrHeaderConfList.next();
						ArrayList keyValHeaderList=EgUtils.egTokenizer(keyValHeader,"$!$",false,false,false);
						Iterator itrkeyValHeaderList=keyValHeaderList.iterator();
						while(itrkeyValHeaderList.hasNext())
						 {
							String key=(String)itrkeyValHeaderList.next();
							String value=(String)itrkeyValHeaderList.next();
							headers.put(key,value);
						 }
					}
					if(headers!=null && !headers.isEmpty())
					{
						 for(Map.Entry m:headers.entrySet()){
							 JSONObject headerObj=new JSONObject();
						   String k=(String)m.getKey();
						   String v=(String)m.getValue();
						   headerObj.put("childTabKey",k);
						   headerObj.put("childTabValue",v);
						   headerArr.add(headerObj);
						  } 
					}
					jRec.put("value",headerArr);
					JSONArray chidHeaderObj=new JSONArray();
					JSONObject headerObj1=new JSONObject();
					headerObj1.put("headerName",_mapping.getTextDescription("monitor.common.Parameter name"));
					chidHeaderObj.add(headerObj1);
					headerObj1=new JSONObject();
					headerObj1.put("headerName",_mapping.getTextDescription("monitor.common.Parameter value"));
					chidHeaderObj.add(headerObj1);
					jRec.put("childTabHeaders",chidHeaderObj);
					dsnConfigDetailsArr.add(jRec);
				}
				if(!StringUtils.isNull(selectedBodyType))
				{
					JSONObject jRec=new JSONObject();
					jRec.put("displayName",_mapping.getTextDescription("monitor.common.Request Params"));
					jRec.put("key","SelectedBodyType");
					jRec.put("value",selectedBodyType);
					
					dsnConfigDetailsArr.add(jRec);
				}
				if(!StringUtils.isNull(rawJsonBody))
				{
					JSONObject jRec=new JSONObject();
					jRec.put("displayName",_mapping.getTextDescription("monitor.common.Request Body (Raw)"));
					jRec.put("key","RawJsonBody");
					jRec.put("value",rawJsonBody);					
					dsnConfigDetailsArr.add(jRec);
				}
				if(!StringUtils.isNull(configuredParams))
				{
					JSONObject jRec=new JSONObject();
					jRec.put("displayName",_mapping.getTextDescription("monitor.common.Request Params"));
					jRec.put("key","configuredParams");

					LinkedHashMap<String,String> headers=new LinkedHashMap<String,String>();

					JSONArray headerArr=new JSONArray();
					ArrayList headerConfigList=EgUtils.egTokenizer(configuredParams,"!^!",false,false,false);
					Iterator itrHeaderConfList=headerConfigList.iterator();
					while (itrHeaderConfList.hasNext())
					{ 
						String keyValHeader=(String)itrHeaderConfList.next();
						ArrayList keyValHeaderList=EgUtils.egTokenizer(keyValHeader,"$!$",false,false,false);
						Iterator itrkeyValHeaderList=keyValHeaderList.iterator();
						while(itrkeyValHeaderList.hasNext())
						{
							String key=(String)itrkeyValHeaderList.next();
							String value=(String)itrkeyValHeaderList.next();
							headers.put(key,value);
						}
					}
					if(headers!=null && !headers.isEmpty())
					{
						 for(Map.Entry m:headers.entrySet()){
							 JSONObject headerObj=new JSONObject();
						   String k=(String)m.getKey();
						   String v=(String)m.getValue();
						   headerObj.put("childTabKey",k);
						   headerObj.put("childTabValue",v);
						   headerArr.add(headerObj);
						  } 
					}
					jRec.put("value",headerArr);
					 JSONArray chidHeaderObj=new JSONArray();
					 JSONObject headerObj1=new JSONObject();
					 headerObj1.put("headerName",_mapping.getTextDescription("monitor.common.Parameter name"));
					 chidHeaderObj.add(headerObj1);
					 headerObj1=new JSONObject();
					 headerObj1.put("headerName",_mapping.getTextDescription("monitor.common.Parameter value"));
					 chidHeaderObj.add(headerObj1);
					jRec.put("childTabHeaders",chidHeaderObj);
					dsnConfigDetailsArr.add(jRec);
				}
				if(!StringUtils.isNull(configuredBodyUrlEncoded))
				{
					JSONObject jRec=new JSONObject();
					jRec.put("displayName",_mapping.getTextDescription("monitor.common.Request Body (x-www-form-urlencoded)"));
					jRec.put("key","ConfiguredBodyUrlEncoded");

					LinkedHashMap<String,String> headers=new LinkedHashMap<String,String>();

					JSONArray headerArr=new JSONArray();
					ArrayList headerConfigList=EgUtils.egTokenizer(configuredBodyUrlEncoded,"!^!",false,false,false);
					Iterator itrHeaderConfList=headerConfigList.iterator();
					while (itrHeaderConfList.hasNext())
					{ 
						String keyValHeader=(String)itrHeaderConfList.next();
						ArrayList keyValHeaderList=EgUtils.egTokenizer(keyValHeader,"$!$",false,false,false);
						Iterator itrkeyValHeaderList=keyValHeaderList.iterator();
						while(itrkeyValHeaderList.hasNext())
						 {
							String key=(String)itrkeyValHeaderList.next();
							String value=(String)itrkeyValHeaderList.next();
							headers.put(key,value);
						 }
					}
					if(headers!=null && !headers.isEmpty())
					{
						 for(Map.Entry m:headers.entrySet()){
							 JSONObject headerObj=new JSONObject();
						   String k=(String)m.getKey();
						   String v=(String)m.getValue();
						   headerObj.put("childTabKey",k);
						   headerObj.put("childTabValue",v);
						   headerArr.add(headerObj);
						  } 
					}
					jRec.put("value",headerArr);
					JSONArray chidHeaderObj=new JSONArray();
					JSONObject headerObj1=new JSONObject();
					headerObj1.put("headerName",_mapping.getTextDescription("monitor.common.Parameter name"));
					chidHeaderObj.add(headerObj1);
					headerObj1=new JSONObject();
					headerObj1.put("headerName",_mapping.getTextDescription("monitor.common.Parameter value"));
					chidHeaderObj.add(headerObj1);
					jRec.put("childTabHeaders",chidHeaderObj);
					dsnConfigDetailsArr.add(jRec);
				}
				if(!StringUtils.isNull(restWS_Fields))
				{
					JSONObject jRec=new JSONObject();
					jRec.put("key","SelectedDataFields");
					jRec.put("value",restWS_Fields);
					dsnConfigDetailsArr.add(jRec);
				}
				if(!StringUtils.isNull(dataFieldParentNode))
				{
					JSONObject jRec=new JSONObject();
					jRec.put("key","DataFieldParentNode");
					jRec.put("value",dataFieldParentNode);
					dsnConfigDetailsArr.add(jRec);
				}
				if(!StringUtils.isNull(dataFieldDataTypes))
				{
					JSONObject jRec=new JSONObject();
					jRec.put("key","DataTypes");
					jRec.put("value",dataFieldDataTypes);
					dsnConfigDetailsArr.add(jRec);
				}
				rootJson.put("DSN Name",restDSNName);
				rootJson.put("dsnConfigDetailsArr",dsnConfigDetailsArr);
			}
			out.println(rootJson);
		}
		else if(action.equalsIgnoreCase("modifyDSNConfiguration"))
		{
			String restDSNName=request.getParameter("restDSNName");
			String icTestName=request.getParameter("test");
			String loadDataGrid=request.getParameter("loadDataGrid");
			IniFile udTestIniObj= new IniFile(EgInstallInfo.getInstallDir()+"/manager/config/eg_udtests.ini");
			String restWS_URL=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_restWS_URL");
			String restWS_Port=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_restWS_Port");
			String ws_username=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_ws_username");
			String ws_pwd=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_ws_pwd");
			String isProxy=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_isProxy");
			String proxyHostName=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_proxyHostName");
			String proxyPort=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_proxyPort");
			String proxyUserName=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_proxyUserName");
			String proxyPassword=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_proxyPassword");
			String isProxyAuthenticated=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_isProxyAuthenticated");
			String httpMethod=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_httpMethod");
			String configuredHeaders=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_configuredHeaders");
			String selectedBodyType=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_selectedBodyType");
			String rawJsonBody=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_rawJsonBody");
			String configuredBodyUrlEncoded=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_configuredBodyUrlEncoded");
			String configuredParams=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_configuredParams");
			String restWS_Fields=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_selectedDataFields");
			String dataFieldParentNode=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_dataFieldParentNode");
			String dataFieldDataTypes=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_DataTypes");

			String basicAuth_Username=udTestIniObj.getValue(icTestName+"_"+restDSNName,"basicAuth_Username");
			String basicAuth_pwd=udTestIniObj.getValue(icTestName+"_"+restDSNName,"basicAuth_pwd");

			LinkedHashMap<String,String> headers=new LinkedHashMap<String,String>();
			LinkedHashMap<String,String> configBodyUrlForm=new LinkedHashMap<String,String>();
			JSONObject headerObj=new JSONObject();
			JSONArray headerArr=new JSONArray();
			JSONObject bodyUrlFormObj=new JSONObject();
			JSONArray bodyUrlFormArr=new JSONArray();
				
			JSONObject dsnConfigDetails=new JSONObject();
			if(!StringUtils.isNull(restWS_URL))
			{
				dsnConfigDetails.put("URL",restWS_URL);
			}
			if(!StringUtils.isNull(restWS_Port))
			{
				dsnConfigDetails.put("Port",restWS_Port);
			}
			if(!StringUtils.isNull(ws_username))
			{
				dsnConfigDetails.put("User name",ws_username);
			}
			if(!StringUtils.isNull(ws_pwd))
			{
				dsnConfigDetails.put("Password",ws_pwd);
			}
			if(!StringUtils.isNull(isProxy))
			{
				dsnConfigDetails.put("Is Proxy enabled?",isProxy);
			}
			if(!StringUtils.isNull(proxyHostName))
			{
				dsnConfigDetails.put("proxyHostname",proxyHostName);
			}
			if(!StringUtils.isNull(proxyPort))
			{
				dsnConfigDetails.put("proxyPort",proxyPort);
			}
			if(!StringUtils.isNull(proxyUserName))
			{
				dsnConfigDetails.put("proxyUserName",proxyUserName);
			}
			if(!StringUtils.isNull(proxyPassword))
			{
				dsnConfigDetails.put("proxyPassword",proxyPassword);
			}
			if(!StringUtils.isNull(isProxyAuthenticated))
			{
				dsnConfigDetails.put("isProxyAuthenticated",isProxyAuthenticated);
			}
			if(!StringUtils.isNull(proxyHostName))
			{
				dsnConfigDetails.put("proxyHostName",proxyHostName);
			}
			if(!StringUtils.isNull(httpMethod))
			{
				dsnConfigDetails.put("HTTP Method",httpMethod);
			}
			if(!StringUtils.isNull(basicAuth_Username))
			{
				dsnConfigDetails.put("basicAuth_Username",basicAuth_Username);
			}
			if(!StringUtils.isNull(basicAuth_pwd))
			{
				dsnConfigDetails.put("basicAuth_pwd",basicAuth_pwd);
			}

			if(!StringUtils.isNull(configuredHeaders))
			{
				 ArrayList headerConfigList=EgUtils.egTokenizer(configuredHeaders,"!^!",false,false,false);
				 Iterator itrHeaderConfList=headerConfigList.iterator();
				 while (itrHeaderConfList.hasNext()) { 
				 String keyValHeader=(String)itrHeaderConfList.next();
				 ArrayList keyValHeaderList=EgUtils.egTokenizer(keyValHeader,"$!$",false,false,false);
				 Iterator itrkeyValHeaderList=keyValHeaderList.iterator();
				 while(itrkeyValHeaderList.hasNext())
					 {
					 String key=(String)itrkeyValHeaderList.next();
					 String value=(String)itrkeyValHeaderList.next();
					 headers.put(key,value);
					 }
				 }
				 if(!headers.isEmpty())
				{
					 for(Map.Entry m:headers.entrySet()){  
					   String k=(String)m.getKey();
					   String v=(String)m.getValue();
					   headerObj.put("key",k);
					   headerObj.put("value",v);
					   headerArr.add(headerObj);
					  } 
				}
				dsnConfigDetails.put("ConfiguredHeaders",headerArr);
			}
			if(!StringUtils.isNull(configuredParams))
			{
				LinkedHashMap<String,String> configParamMap=new LinkedHashMap<String,String>();
				JSONObject configParamFormObj=new JSONObject();
				JSONArray configParamFormArr=new JSONArray();
				
				 ArrayList configuredParamsList=EgUtils.egTokenizer(configuredParams,"!^!",false,false,false);
				 Iterator itrParamsConfList=configuredParamsList.iterator();
				 while (itrParamsConfList.hasNext()) {  
				 String keyValParam=(String)itrParamsConfList.next();
				 ArrayList keyValParamList=EgUtils.egTokenizer(keyValParam,"$!$",false,false,false);
				 Iterator itrkeyValParamList=keyValParamList.iterator();
				 while(itrkeyValParamList.hasNext())
					 {
					 String key=(String)itrkeyValParamList.next();
					 String value=(String)itrkeyValParamList.next();
					 configParamMap.put(key,value);
					 }
				 } 
					
				if(configParamMap!=null && !configParamMap.isEmpty())
				{
					 for(Map.Entry m:configParamMap.entrySet()){  
					   String k=(String)m.getKey();
					   String v=(String)m.getValue();
					   configParamFormObj.put("key",k);
					   configParamFormObj.put("value",v);
					   configParamFormArr.add(configParamFormObj);
					  } 
				}
				dsnConfigDetails.put("configuredParams",configParamFormArr);
			}
			if(!StringUtils.isNull(selectedBodyType))
			{
				dsnConfigDetails.put("SelectedBodyType",selectedBodyType);
				if(!StringUtils.isNull(rawJsonBody))
				{
					dsnConfigDetails.put("RawJsonBody",rawJsonBody);
				}
				if(!StringUtils.isNull(configuredBodyUrlEncoded))
				{
					dsnConfigDetails.put("ConfiguredBodyUrlEncoded",configuredBodyUrlEncoded);
				}
				if(!StringUtils.isNull(selectedBodyType) && selectedBodyType.equalsIgnoreCase("raw"))
				{
					dsnConfigDetails.put("rawJsonBody",rawJsonBody);
				}
				else
				{
					if(!StringUtils.isNull(configuredBodyUrlEncoded))
					{
						 ArrayList bodyConfigList=EgUtils.egTokenizer(configuredBodyUrlEncoded,"!^!",false,false,false);
						 Iterator itrBodyConfList=bodyConfigList.iterator();
						 while (itrBodyConfList.hasNext()) {  
						 String keyValBody=(String)itrBodyConfList.next();
						 ArrayList keyValBodyList=EgUtils.egTokenizer(keyValBody,"$!$",false,false,false);
						 Iterator itrkeyValBodyList=keyValBodyList.iterator();
						 while(itrkeyValBodyList.hasNext())
							 {
							 String key=(String)itrkeyValBodyList.next();
							 String value=(String)itrkeyValBodyList.next();
							 configBodyUrlForm.put(key,value);
							 }
						 } 
					}
					
					if(configBodyUrlForm!=null && !configBodyUrlForm.isEmpty())
					{
						 for(Map.Entry m:configBodyUrlForm.entrySet()){  
						   String k=(String)m.getKey();
						   String v=(String)m.getValue();
						   bodyUrlFormObj.put("key",k);
						   bodyUrlFormObj.put("value",v);
						   bodyUrlFormArr.add(bodyUrlFormObj);
						  } 
					}
					dsnConfigDetails.put("configuredBodyUrlEncoded",bodyUrlFormArr);
				}
			}
			
			if(!StringUtils.isNull(restWS_Fields))
			{
				dsnConfigDetails.put("SelectedDataFields",restWS_Fields);
			}
			if(!StringUtils.isNull(dataFieldParentNode))
			{
				dsnConfigDetails.put("DataFieldParentNode",dataFieldParentNode);
			}
			if(!StringUtils.isNull(dataFieldDataTypes))
			{
				dsnConfigDetails.put("DataTypes",dataFieldDataTypes);
			}

			Hashtable wsConnParams=new Hashtable();
				
			if(!StringUtils.isNull(restWS_URL))
			{
				wsConnParams.put("ExtrnalDataSource_restWS_URL",restWS_URL);
			}
			if(!StringUtils.isNull(restWS_Port))
			{
				wsConnParams.put("ExtrnalDataSource_restWS_Port",restWS_Port);
			}
			if(!StringUtils.isNull(configuredHeaders))
			{
				wsConnParams.put("ExtrnalDataSource_configuredHeaders",configuredHeaders);
			}
			if(!StringUtils.isNull(rawJsonBody))
			{
				wsConnParams.put("ExtrnalDataSource_rawJsonBody",rawJsonBody);
			}
			if(!StringUtils.isNull(configuredBodyUrlEncoded))
			{
				wsConnParams.put("ExtrnalDataSource_configuredBodyUrlEncoded",configuredBodyUrlEncoded);
			}
			if(!StringUtils.isNull(ws_pwd))
			{
				wsConnParams.put("ExtrnalDataSource_ws_pwd",ws_pwd);
			}
			if(!StringUtils.isNull(ws_username))
			{
				wsConnParams.put("ExtrnalDataSource_ws_username",ws_username);
			}
			if(!StringUtils.isNull(isProxy))
			{
				wsConnParams.put("ExtrnalDataSource_isProxy",isProxy);
			}
			if(!StringUtils.isNull(proxyHostName))
			{
				wsConnParams.put("ExtrnalDataSource_proxyHostName",proxyHostName);
			}
			if(!StringUtils.isNull(proxyPort))
			{
				wsConnParams.put("ExtrnalDataSource_proxyPort",proxyPort);
			}
			if(!StringUtils.isNull(proxyUserName))
			{
				wsConnParams.put("ExtrnalDataSource_proxyUserName",proxyUserName);
			}
			if(!StringUtils.isNull(proxyPassword))
			{
				wsConnParams.put("ExtrnalDataSource_proxyPassword",proxyPassword);
			}
			if(!StringUtils.isNull(isProxyAuthenticated))
			{
				wsConnParams.put("ExtrnalDataSource_isProxyAuthenticated",isProxyAuthenticated);
			}
			if(!StringUtils.isNull(proxyHostName))
			{
				wsConnParams.put("ExtrnalDataSource_proxyHostName",proxyHostName);
			}
			if(!StringUtils.isNull(proxyPort))
			{
				wsConnParams.put("ExtrnalDataSource_proxyPort",proxyPort);
			}
			if(!StringUtils.isNull(proxyUserName))
			{
				wsConnParams.put("ExtrnalDataSource_proxyUserName",proxyUserName);
			}
			if(!StringUtils.isNull(proxyPassword))
			{
				wsConnParams.put("ExtrnalDataSource_proxyPassword",proxyPassword);
			}
			if(!StringUtils.isNull(httpMethod))
			{
				wsConnParams.put("ExtrnalDataSource_httpMethod",httpMethod);
			}
			if(!StringUtils.isNull(configuredParams))
			{
				wsConnParams.put("ExtrnalDataSource_configuredParams",configuredParams);
			}
			if(loadDataGrid!=null && loadDataGrid.equalsIgnoreCase("yes"))
			{
				EgWebServiceConnector wsc=new EgWebServiceConnector();
				wsc.setWSParams(wsConnParams);
				String jsonStr=wsc.getResponseForRestURL();
				
				JSONObject jsonTreeObj=getJSONTree(jsonStr);
				rootJson.put("jsonkeys",jsonTreeObj);
			}
			rootJson.put("dsnConfigDetails",dsnConfigDetails);
			rootJson.put("restDSNName",restDSNName);
			out.println(rootJson);
		}
		else if(action.equalsIgnoreCase("loadDSNConfiguration"))
		{
			String icTestName=request.getParameter("test");
			IniFile udTestIniObj= new IniFile(EgInstallInfo.getInstallDir()+"/manager/config/eg_udtests.ini");
			String restDSNNames=udTestIniObj.getValue("REST_DSN",icTestName);
			JSONArray dsnArr=new JSONArray();
			if(!StringUtils.isNull(restDSNNames))
			{
				StringTokenizer stkRestDSNS=new StringTokenizer(restDSNNames,",");
				while(stkRestDSNS.hasMoreTokens())
				{
					String restDSNName=stkRestDSNS.nextToken();
					JSONObject dsnObj=new JSONObject();
					JSONArray dsnConfigDetailsArr=new JSONArray();
					String restWS_URL=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_restWS_URL");
					String restWS_Port=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_restWS_Port");
					String ws_username=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_ws_username");
					String ws_pwd=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_ws_pwd");
					String isProxy=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_isProxy");
					String proxyHostName=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_proxyHostName");
					String proxyPort=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_proxyPort");
					String proxyUserName=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_proxyUserName");
					String proxyPassword=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_proxyPassword");
					String isProxyAuthenticated=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_isProxyAuthenticated");
					String httpMethod=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_httpMethod");
					String configuredHeaders=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_configuredHeaders");
					String selectedBodyType=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_selectedBodyType");
					String rawJsonBody=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_rawJsonBody");
					String configuredBodyUrlEncoded=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_configuredBodyUrlEncoded");
					String configuredParams=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_configuredParams");
					String restWS_Fields=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_selectedDataFields");
					String dataFieldParentNode=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_dataFieldParentNode");
					String dataFieldDataTypes=udTestIniObj.getValue(icTestName+"_"+restDSNName,"ExtrnalDataSource_DataTypes");
					String basicAuth_Username=udTestIniObj.getValue(icTestName+"_"+restDSNName,"basicAuth_Username");
					String basicAuth_pwd=udTestIniObj.getValue(icTestName+"_"+restDSNName,"basicAuth_pwd");
					
					if(!StringUtils.isNull(restWS_URL))
					{
						JSONObject jRec=new JSONObject();
						jRec.put("displayName",_mapping.getTextDescription("monitor.common.URL"));
						jRec.put("key","URL");
						jRec.put("value",restWS_URL);
						dsnConfigDetailsArr.add(jRec);
					}
				/*	if(!StringUtils.isNull(restWS_Port))
					{
						JSONObject jRec=new JSONObject();
						jRec.put("displayName",_mapping.getTextDescription("monitor.common.Web Port"));
						jRec.put("key","Port");
						jRec.put("value",restWS_Port);
						dsnConfigDetailsArr.add(jRec);
					}*/
					if(!StringUtils.isNull(ws_username))
					{
						JSONObject jRec=new JSONObject();
						jRec.put("displayName",_mapping.getTextDescription("monitor.common.User name"));
						jRec.put("key","User name");
						jRec.put("value",ws_username);
						dsnConfigDetailsArr.add(jRec);
					}
					if(!StringUtils.isNull(ws_pwd))
					{
						JSONObject jRec=new JSONObject();
						jRec.put("displayName",_mapping.getTextDescription("monitor.common.Password"));
						jRec.put("key","Password");
						jRec.put("value",ws_pwd);
						dsnConfigDetailsArr.add(jRec);
					}
					if(!StringUtils.isNull(isProxy))
					{
						JSONObject jRec=new JSONObject();
						jRec.put("displayName",_mapping.getTextDescription("monitor.common.Is Proxy enabled")+"?");
						jRec.put("key","Is Proxy enabled?");
						jRec.put("value",isProxy);
						dsnConfigDetailsArr.add(jRec);
					}
					if(!StringUtils.isNull(proxyHostName))
					{
						JSONObject jRec=new JSONObject();
						jRec.put("displayName",_mapping.getTextDescription("monitor.common.Proxy Hostname"));
						jRec.put("key","proxyHostname");
						jRec.put("value",proxyHostName);
						dsnConfigDetailsArr.add(jRec);
					}
					if(!StringUtils.isNull(proxyPort))
					{
						JSONObject jRec=new JSONObject();
						jRec.put("displayName",_mapping.getTextDescription("monitor.common.Proxy Port"));
						jRec.put("key","proxyPort");
						jRec.put("value",proxyPort);
						dsnConfigDetailsArr.add(jRec);
					}
					if(!StringUtils.isNull(proxyUserName))
					{
						JSONObject jRec=new JSONObject();
						jRec.put("displayName",_mapping.getTextDescription("monitor.common.Proxy User name"));
						jRec.put("key","proxyUserName");
						jRec.put("value",proxyUserName);
						dsnConfigDetailsArr.add(jRec);
					}
					if(!StringUtils.isNull(proxyPassword))
					{
						JSONObject jRec=new JSONObject();
						jRec.put("displayName",_mapping.getTextDescription("monitor.common.Proxy Password"));
						jRec.put("key","proxyPassword");
						jRec.put("value",proxyPassword);
						dsnConfigDetailsArr.add(jRec);
					}
					if(!StringUtils.isNull(isProxyAuthenticated))
					{
						JSONObject jRec=new JSONObject();
						jRec.put("displayName",_mapping.getTextDescription("monitor.common.Is Proxy Authenticated")+"?");
						jRec.put("key","isProxyAuthenticated");
						jRec.put("value",isProxyAuthenticated);
						dsnConfigDetailsArr.add(jRec);
					}
					if(!StringUtils.isNull(httpMethod))
					{
						JSONObject jRec=new JSONObject();
						jRec.put("displayName",_mapping.getTextDescription("monitor.common.Http Method"));
						jRec.put("key","HTTP Method");
						jRec.put("value",httpMethod);
						dsnConfigDetailsArr.add(jRec);
					}
					if(!StringUtils.isNull(configuredHeaders))
					{
						JSONObject jRec=new JSONObject();
						jRec.put("key","ConfiguredHeaders");
						jRec.put("displayName",_mapping.getTextDescription("monitor.common.Configured Headers"));
						LinkedHashMap<String,String> headers=new LinkedHashMap<String,String>();
						
						JSONArray headerArr=new JSONArray();
						ArrayList headerConfigList=EgUtils.egTokenizer(configuredHeaders,"!^!",false,false,false);
						Iterator itrHeaderConfList=headerConfigList.iterator();
						while (itrHeaderConfList.hasNext())
						{ 
							String keyValHeader=(String)itrHeaderConfList.next();
							ArrayList keyValHeaderList=EgUtils.egTokenizer(keyValHeader,"$!$",false,false,false);
							Iterator itrkeyValHeaderList=keyValHeaderList.iterator();
							while(itrkeyValHeaderList.hasNext())
							 {
								String key=(String)itrkeyValHeaderList.next();
								String value=(String)itrkeyValHeaderList.next();
								headers.put(key,value);
							 }
						}
						if(headers!=null && !headers.isEmpty())
						{
							 for(Map.Entry m:headers.entrySet()){
								 JSONObject headerObj=new JSONObject();
							   String k=(String)m.getKey();
							   String v=(String)m.getValue();
							   headerObj.put("childTabKey",k);
							   headerObj.put("childTabValue",v);
							   headerArr.add(headerObj);
							  } 
						}
						jRec.put("value",headerArr);
						 JSONArray chidHeaderObj=new JSONArray();
						 JSONObject headerObj1=new JSONObject();
						 headerObj1.put("headerName",_mapping.getTextDescription("monitor.common.Parameter name"));
						 chidHeaderObj.add(headerObj1);
						 headerObj1=new JSONObject();
						 headerObj1.put("headerName",_mapping.getTextDescription("monitor.common.Parameter value"));
						 chidHeaderObj.add(headerObj1);
						jRec.put("childTabHeaders",chidHeaderObj);
						dsnConfigDetailsArr.add(jRec);
					}
					if(!StringUtils.isNull(selectedBodyType))
					{
						JSONObject jRec=new JSONObject();
						jRec.put("displayName",_mapping.getTextDescription("monitor.common.Request Params"));
						jRec.put("key","SelectedBodyType");
						jRec.put("value",selectedBodyType);
					}
					if(!StringUtils.isNull(rawJsonBody))
					{
						JSONObject jRec=new JSONObject();
						jRec.put("displayName",_mapping.getTextDescription("monitor.common.Request Body (Raw)"));
						jRec.put("key","RawJsonBody");
						jRec.put("value",rawJsonBody);
						dsnConfigDetailsArr.add(jRec);
					}
					
					if(!StringUtils.isNull(configuredBodyUrlEncoded))
					{
						JSONObject jRec=new JSONObject();
						jRec.put("displayName",_mapping.getTextDescription("monitor.common.Request Body (x-www-form-urlencoded)"));
						jRec.put("key","ConfiguredBodyUrlEncoded");
						//jRec.put("value",configuredBodyUrlEncoded);

						LinkedHashMap<String,String> headers=new LinkedHashMap<String,String>();
						
						JSONArray headerArr=new JSONArray();
						ArrayList headerConfigList=EgUtils.egTokenizer(configuredBodyUrlEncoded,"!^!",false,false,false);
						Iterator itrHeaderConfList=headerConfigList.iterator();
						while (itrHeaderConfList.hasNext())
						{ 
							String keyValHeader=(String)itrHeaderConfList.next();
							ArrayList keyValHeaderList=EgUtils.egTokenizer(keyValHeader,"$!$",false,false,false);
							Iterator itrkeyValHeaderList=keyValHeaderList.iterator();
							while(itrkeyValHeaderList.hasNext())
							 {
								String key=(String)itrkeyValHeaderList.next();
								String value=(String)itrkeyValHeaderList.next();
								headers.put(key,value);
							 }
						}
						if(headers!=null && !headers.isEmpty())
						{
							 for(Map.Entry m:headers.entrySet()){
								 JSONObject headerObj=new JSONObject();
							   String k=(String)m.getKey();
							   String v=(String)m.getValue();
							   headerObj.put("childTabKey",k);
							   headerObj.put("childTabValue",v);
							   headerArr.add(headerObj);
							  } 
						}
						jRec.put("value",headerArr);
						 JSONArray chidHeaderObj=new JSONArray();
						 JSONObject headerObj1=new JSONObject();
						 headerObj1.put("headerName",_mapping.getTextDescription("monitor.common.Parameter name"));
						 chidHeaderObj.add(headerObj1);
						 headerObj1=new JSONObject();
						 headerObj1.put("headerName",_mapping.getTextDescription("monitor.common.Parameter value"));
						 chidHeaderObj.add(headerObj1);
						jRec.put("childTabHeaders",chidHeaderObj);
						dsnConfigDetailsArr.add(jRec);
					}
					if(!StringUtils.isNull(configuredParams))
					{
						JSONObject jRec=new JSONObject();
						jRec.put("displayName",_mapping.getTextDescription("monitor.common.Request Params"));
						jRec.put("key","configuredParams");
						//jRec.put("value",configuredBodyUrlEncoded);

						LinkedHashMap<String,String> headers=new LinkedHashMap<String,String>();
						
						JSONArray headerArr=new JSONArray();
						ArrayList headerConfigList=EgUtils.egTokenizer(configuredParams,"!^!",false,false,false);
						Iterator itrHeaderConfList=headerConfigList.iterator();
						while (itrHeaderConfList.hasNext())
						{ 
							String keyValHeader=(String)itrHeaderConfList.next();
							ArrayList keyValHeaderList=EgUtils.egTokenizer(keyValHeader,"$!$",false,false,false);
							Iterator itrkeyValHeaderList=keyValHeaderList.iterator();
							while(itrkeyValHeaderList.hasNext())
							 {
								String key=(String)itrkeyValHeaderList.next();
								String value=(String)itrkeyValHeaderList.next();
								headers.put(key,value);
							 }
						}
						if(headers!=null && !headers.isEmpty())
						{
							 for(Map.Entry m:headers.entrySet()){
								 JSONObject headerObj=new JSONObject();
							   String k=(String)m.getKey();
							   String v=(String)m.getValue();
							   headerObj.put("childTabKey",k);
							   headerObj.put("childTabValue",v);
							   headerArr.add(headerObj);
							  } 
						}
						jRec.put("value",headerArr);
						 JSONArray chidHeaderObj=new JSONArray();
						 JSONObject headerObj1=new JSONObject();
						 headerObj1.put("headerName",_mapping.getTextDescription("monitor.common.Parameter name"));
						 chidHeaderObj.add(headerObj1);
						 headerObj1=new JSONObject();
						 headerObj1.put("headerName",_mapping.getTextDescription("monitor.common.Parameter value"));
						 chidHeaderObj.add(headerObj1);
						jRec.put("childTabHeaders",chidHeaderObj);
						dsnConfigDetailsArr.add(jRec);
					}
					if(!StringUtils.isNull(restWS_Fields))
					{
						JSONObject jRec=new JSONObject();
						jRec.put("key","SelectedDataFields");
						jRec.put("value",restWS_Fields);
						dsnConfigDetailsArr.add(jRec);
					}
					if(!StringUtils.isNull(dataFieldParentNode))
					{
						JSONObject jRec=new JSONObject();
						jRec.put("key","DataFieldParentNode");
						jRec.put("value",dataFieldParentNode);
						dsnConfigDetailsArr.add(jRec);
					}
					if(!StringUtils.isNull(dataFieldDataTypes))
					{
						JSONObject jRec=new JSONObject();
						jRec.put("key","DataTypes");
						jRec.put("value",dataFieldDataTypes);
						dsnConfigDetailsArr.add(jRec);
					}
					dsnObj.put("dsnName",restDSNName);
					dsnObj.put("configDetails",dsnConfigDetailsArr);
					dsnArr.add(dsnObj);
				}
				rootJson.put("dsnArr",dsnArr);
				rootJson.put("errorMsg","-");
			}
			else
			{
				rootJson.put("errorMsg","No DSN Configured");
			}
			out.println(rootJson);
		}
	}
%>


<%!
	public JSONObject getJSONTree(String json)
	{
		JSONObject rootJson=new JSONObject();
		JSONObject restGridJson=new JSONObject();
		try
		{
			JsonDataParser jds=new JsonDataParser(json);
			//rootJson.put("restTreeNodeData",jds.getNodeRootJsonList());
			JSONObject treeJson=jds.generateJTree();
			//if(treeJson!=null && !treeJson.isEmpty())
			if(true)
			{
				JSONObject rootTreeNode=new JSONObject();
				//rootTreeNode.put("text","Root");
				rootTreeNode.put("text","<span class='selectedNode'>Root</span>");
				rootTreeNode.put("leaf","false");
				rootTreeNode.put("expanded","true");
				rootTreeNode.put("jPath","root");
				rootTreeNode.put("id","root");
				JSONArray childrenArr=(JSONArray)treeJson.get("children");
				rootTreeNode.put("children",childrenArr);
				rootJson.put("restTree",rootTreeNode);
			}
			ArrayList treeNodes=jds.getTreeNodes();
			treeNodes.add("root");
			Iterator itrHs=treeNodes.iterator();
			while(itrHs.hasNext())
			{
				String jpath=(String)itrHs.next();
				restGridJson.put(jpath,getRestGrid(json,jpath));
			}
			rootJson.put("restGrid",restGridJson);
			rootJson.put("resTreeNodefields",jds.getFieldsForJPath());
		}
		catch (Throwable t)
		{
			t.printStackTrace();
		}
		finally
		{
			return rootJson;
		}
	}
	public boolean isValidDate(String inDate)
	{
		boolean isValidDate=false;
		int dateFormatsLen=dateFormatsList.size();
		for(int i=0;i<dateFormatsLen;i++)
		{
			String dateFormatStr=(String)dateFormatsList.get(i);
			SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormatStr);
			try {
				java.util.Date dformat=dateFormat.parse(inDate.trim());
				//System.out.println("dformat---->"+dformat);
				isValidDate=true;
			} catch (ParseException pe) {
				//pe.printStackTrace();
			   isValidDate=false;
			}
			if(isValidDate)
			{
				break;
			}
		}

        return isValidDate;
    }

	public boolean isNumeric(String val)
	{
		// String string = "-1234.15";
        boolean numeric = true;

        numeric = val.matches("-?\\d+(\\.\\d+)?");

      return numeric;
	}
	public JSONObject getRestGrid(String json,String jpath)
	{
		JSONObject rootJson=new JSONObject();

		Object rawData = null;
		if(jpath!=null && (!jpath.equals("") && !jpath.equalsIgnoreCase("root")))
		{
			JsonDataParser jds=new JsonDataParser(json,jpath,null);
			JSONObject parentJsonNodeDataObj=jds.getNodeRootJsonList();
			if(jpath!=null && parentJsonNodeDataObj.containsKey(jpath))
			{
				rawData=parentJsonNodeDataObj.get(jpath);
			}
			else
			{
				rawData=parentJsonNodeDataObj;
			}
		}
		else
		{
			rawData=(Object)JSONSerializer.toJSON(json);
		}
		try
		{
			int measureInternalValue=1;
			JSONObject dataTypeMapping=new JSONObject();
			JSONObject measureInternalValueMapping=new JSONObject();
			JSONObject uniqueValuesForColumn=new JSONObject();
			HashSet measureInternalValueList=new HashSet();
			Object object = (Object)JSONSerializer.toJSON(rawData);
			String jsonPath = "";
			if(object!=null && !StringUtils.isNull(json))
			{
				ArrayList keysList=new ArrayList();
				JSONArray gridCols=new JSONArray();
				JSONArray gridData=new JSONArray();
				
				if(object instanceof JSONObject)
				{
					JSONObject jsonObj = (JSONObject)object;
					Iterator<String> keysItr = jsonObj.keys();
					String parentPath = jpath;
					while(keysItr.hasNext()) 
					{
						String key = keysItr.next();
						Object value = jsonObj.get(key);
						if(value!=null && (value instanceof JSONArray || value instanceof JSONObject)) {
						}
						else //is a value
						{
							String valStr=value.toString();
							if(!keysList.contains(key))
							{
								keysList.add(key);
								gridCols.add(key);
							}
						}
					}
					Iterator itrGridCols=gridCols.iterator();
					JSONObject rowData=new JSONObject();
					while(itrGridCols.hasNext())
					{
						String colNam=(String)itrGridCols.next();
						Object valObj=jsonObj.get(colNam);

						String colVal="";
						if(valObj!=null)
						{
							if(valObj instanceof String)
							{
								colVal=(String)valObj;
								Integer internalMeasVal=(Integer)measureInternalValueMapping.get(colVal);
								if(internalMeasVal==null)
								{
									internalMeasVal=new Integer(measureInternalValue);
									measureInternalValueMapping.put(colVal,internalMeasVal);
									uniqueValuesForColumn.put(colNam,colVal);
									measureInternalValue++;
								}
							}
							else if(valObj instanceof Integer)
							{
								Integer valIntObj=(Integer)valObj;
								colVal=valIntObj.toString();
							}
							else if(valObj instanceof Double)
							{
								Double valDoubleObj=(Double)valObj;
								colVal=valDoubleObj.toString();
							}
							else if(valObj instanceof JSONArray)
							{
								colVal=valObj.toString();
							}
						}

						if(StringUtils.isNull(colNam))
						{
							colVal="NA";
						}
						String dataType=(String)dataTypeMapping.get(colNam);

						if(StringUtils.isNull(dataType) || (!dataType.equalsIgnoreCase("String") && !dataType.equalsIgnoreCase("Datetime")))
						{
							if(isNumeric(colVal))
							{
								dataTypeMapping.put(colNam,"Number");
							}
							else if(isValidDate(colVal))
							{
								dataTypeMapping.put(colNam,"Datetime");
							}
							else
							{
								dataTypeMapping.put(colNam,"String");
							}

							if(colNam!=null && colNam.indexOf(" ")>-1)
							{
								String tmpColNam=colNam;
								tmpColNam=StringUtils.replace(tmpColNam," ","_bs_");
								if(isNumeric(colVal))
								{
									dataTypeMapping.put(tmpColNam,"Number");
								}
								else if(isValidDate(colVal))
								{
									dataTypeMapping.put(tmpColNam,"Datetime");
								}
								else
								{
									dataTypeMapping.put(tmpColNam,"String");
								}
							}
						}
						rowData.put(colNam,colVal);
					}
					gridData.add(rowData);
				}
				else if(object instanceof JSONArray)
				{
					JSONArray jsonArr = (JSONArray)object;
					for(int i = 0; i < jsonArr.size(); i++) 
					{
						Object obj = jsonArr.get(i);
						JSONObject jsonObj = (JSONObject)obj;
						Iterator<String> keysItr = jsonObj.keys();
						String parentPath = jpath;
						while(keysItr.hasNext()) {
							String key = keysItr.next();
							Object value = jsonObj.get(key);
							if(value!=null && (value instanceof JSONArray || value instanceof JSONObject)) {
							}
							else //is a value
							{
								String valStr=value.toString();
								if(!keysList.contains(key))
								{
									keysList.add(key);
									gridCols.add(key);
								}
							}
						}
					}
					JSONArray jsonValArr=(JSONArray)object;
					Iterator itrJsonArr=jsonValArr.iterator();
					while(itrJsonArr.hasNext())
					{
						JSONObject jsonObj=(JSONObject)itrJsonArr.next();
						Iterator itrGridCols=gridCols.iterator();
						JSONObject rowData=new JSONObject();
						while(itrGridCols.hasNext())
						{
							String colNam=(String)itrGridCols.next();
							Object valObj=jsonObj.get(colNam);

							String colVal="";
							if(valObj!=null)
							{
								if(valObj instanceof String)
								{
									colVal=(String)valObj;
									Integer internalMeasVal=(Integer)measureInternalValueMapping.get(colVal);
									if(internalMeasVal==null)
									{
										internalMeasVal=new Integer(measureInternalValue);
										measureInternalValueMapping.put(colVal,internalMeasVal);
										uniqueValuesForColumn.put(colNam,colVal);
										measureInternalValue++;
									}
								}
								else if(valObj instanceof Integer)
								{
									Integer valIntObj=(Integer)valObj;
									colVal=valIntObj.toString();
								}
								else if(valObj instanceof Double)
								{
									Double valDoubleObj=(Double)valObj;
									colVal=valDoubleObj.toString();
								}
								else if(valObj instanceof JSONArray)
								{
									colVal=valObj.toString();
								}
							}

							if(StringUtils.isNull(colNam))
							{
								colVal="NA";
							}
							String dataType=(String)dataTypeMapping.get(colNam);

							if(StringUtils.isNull(dataType) || (!dataType.equalsIgnoreCase("String") && !dataType.equalsIgnoreCase("Datetime")))
							{
								if(isNumeric(colVal))
								{
									dataTypeMapping.put(colNam,"Number");
								}
								else if(isValidDate(colVal))
								{
									dataTypeMapping.put(colNam,"Datetime");
								}
								else
								{
									dataTypeMapping.put(colNam,"String");
								}

								if(colNam!=null && colNam.indexOf(" ")>-1)
								{
									String tmpColNam=colNam;
									tmpColNam=StringUtils.replace(tmpColNam," ","_bs_");
									if(isNumeric(colVal))
									{
										dataTypeMapping.put(tmpColNam,"Number");
									}
									else if(isValidDate(colVal))
									{
										dataTypeMapping.put(tmpColNam,"Datetime");
									}
									else
									{
										dataTypeMapping.put(tmpColNam,"String");
									}
								}
							}
							rowData.put(colNam,colVal);
						}
						gridData.add(rowData);
					}
				}
				rootJson.put("gridCols",gridCols);
				rootJson.put("gridData",gridData);
				rootJson.put("dataTypeMapping",dataTypeMapping);
				rootJson.put("measureInternalValueMapping",measureInternalValueMapping);
				rootJson.put("uniqueValuesForColumn",uniqueValuesForColumn);
			}
		}
		catch (Throwable t)
		{
			t.printStackTrace();
		}
		finally
		{
			return rootJson;
		}

	}
%>