import java.io.*;
import java.util.*;
import java.net.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.PartSource;
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;

public class getSearchRecords
{
	public static void main(String a[])
	{	
		try	
		{
			//----------------------------Fetch Ticket ----------------------
			String authtoken = "YOUR AUTH TOKEN";
			 String scope = "crmapi";

			String selectColumns ="Leads(Lead Owner,First Name,Last Name,Email,Company)";
			String newFormat = "1";
			String searchCondition = "(Email|contains|*@test.com*)";
			String fromIndex = "1";
			String toIndex = "50";

			String targetURL = "http://crm.zoho.com/crm/private/xml/Leads/getSearchRecords"; 
			String paramname = "content";
			PostMethod post = new PostMethod(targetURL);
			post.setParameter("authtoken",authtoken);
			post.setParameter("scope",scope);
			post.setParameter("fromIndex",fromIndex);
			post.setParameter("toIndex",toIndex);
			post.setParameter("newFormat",newFormat);
			post.setParameter("selectColumns",selectColumns);
			post.setParameter("searchCondition",searchCondition);
			
			HttpClient httpclient = new HttpClient();
			PrintWriter myout = null;

			// Execute http request
			try 
			{
				long t1 = System.currentTimeMillis();
				int result = httpclient.executeMethod(post);
				System.out.println("HTTP Response status code: " + result);
				System.out.println(">> Time taken " + (System.currentTimeMillis() - t1));

				// writing the response to a file
				myout = new PrintWriter(new File("response.xml"));
				myout.print(post.getResponseBodyAsString());

				//-----------------------Get response as a string ---------------
				String postResp = post.getResponseBodyAsString();
				System.out.println("postResp=======>"+postResp);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}	
			finally 
			{
				myout.close();
				post.releaseConnection();
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}	
	}
	//-------------------------Get IAM Ticket ---------------------------------
	public static String getIAMTicket(String serviceName, String loginId, String password) 
	{
		String strTicket = null;
		try {

			String iamUrl = "http://accounts.zoho.com/login?servicename="+serviceName+"&FROM_AGENT=true&LOGIN_ID="+loginId+"&PASSWORD="+password;
			URL u = new URL(iamUrl);
			HttpURLConnection c = (HttpURLConnection)u.openConnection();
			InputStream in = c.getInputStream();
			InputStreamReader ir=new InputStreamReader(in);
			BufferedReader br =new BufferedReader(ir);

			String strLine  = null;

			while ((strLine = br.readLine()) != null) {
				if(strLine != null && strLine.startsWith("TICKET")) {
					strTicket = strLine.substring(7);
				}
			}

			in.close();
		}
		catch (Exception e){
			e.printStackTrace();

		}
		return strTicket;
	}
}
