package queryTool;
// generic libraries
import java.security.*;
import java.io.*;
import java.util.*;
// HTTP client libraries
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.auth.AuthPolicy;

// callServer class
public class callServer {
	public int resultStat = 0;
	public String resultText = "Output not returned from server.";
	public String input = "";
	public String desti = "";

	// empty constructor
	public static void main(String[] args) {
	}
	
	/* execute method -- 
	 * accepts url, data and login credentials
	 * sets resultText -- return from server
	 * sets resultStat -- -1 for error, 1 for success
	 */
	//public void execute (String strURL, String InputData, String username, String password){
	public void execute (String strURL, HashMap InputData, String username, String password){
		final String myURL = strURL;
		//final String Input = InputData;
		final HashMap inputList = InputData;
		final String usern = username;
		final String passw = password;
		
		// Set security provider
		System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
		System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
	
		// Instantiate Client
		HttpClient httpclient = new HttpClient();
		httpclient.getState().setCredentials(
	            new AuthScope(AuthScope.ANY),
	            new UsernamePasswordCredentials(usern, passw)
	        );
		List authPrefs = new ArrayList(2);
		authPrefs.add(AuthPolicy.DIGEST);
		authPrefs.add(AuthPolicy.BASIC);
		httpclient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
		
		// Create and configure post object
		PostMethod post = new PostMethod(myURL);
		post.getParams().setParameter("http.method.retry-handler", new DefaultHttpMethodRetryHandler(3, false));
		post.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
		
		// create NameValuePairs and assign them to the requestBody
		Iterator it = inputList.keySet().iterator();
	    while (it.hasNext()) {
	        // Get key
	        String key = it.next().toString();
	        String value = inputList.get(key).toString();
	        post.setRequestBody(new NameValuePair[]{new NameValuePair(key,value)});
	    }
		
		//post.setRequestBody(new NameValuePair[]{new NameValuePair("idata",Input)});	
		post.setDoAuthentication( true );
		
		// post the data and read the results
		try {
			httpclient.executeMethod(post);
			resultText = post.getResponseBodyAsString();
			post.releaseConnection();
			resultStat = 1;
			}  catch (IOException e){
				post.releaseConnection();
				resultStat = -1;
			}finally {
			}
	            post.releaseConnection();
	        }
	}
