Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package apimanager; import ConnectionProtccol.HttpsClient; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import loggerapi.CustomLogger; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.json.simple.JSONObject; /** * * @author harsha-1916 */ public class ZohoReportsAPIManager { private static final CustomLogger loggerProperties = new CustomLogger(); private static Logger loggerObj; private static final String AUTHTOKEN = "authtoken"; static { initializeLoggerParams(); } private static void initializeLoggerParams() { boolean isValidLogger = loggerProperties.setLoggerProperties("ZohoReportsapi", "./Logs/ZohoReportsapi.%u.%g.txt"); if (isValidLogger) { loggerObj = loggerProperties.getLogger(); loggerObj.setUseParentHandlers(false); } } public void postaddSingleRow() { String URL = "https://reportsapi.zoho.com/api/harshavardhan.r@zohocorp.com/Test/Name_password?ZOHO_ACTION=ADDROW&ZOHO_OUTPUT_FORMAT=json&ZOHO_ERROR_FORMAT=json&authtoken=7ed717b94bc30455aad11ce5d31d34f9&ZOHO_API_VERSION=1.0"; CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost post = new HttpPost( "https://reportsapi.zoho.com/api/harshavardhan.r@zohocorp.com/Test/Name_password?ZOHO_ACTION=ADDROW&ZOHO_OUTPUT_FORMAT=json&ZOHO_ERROR_FORMAT=json&authtoken=7ed717b94bc30455aad11ce5d31d34f9&ZOHO_API_VERSION=1.0"); try { List<NameValuePair> nameValuePairs = new ArrayList<>(2); nameValuePairs.add(new BasicNameValuePair("Name", "harshaViaPostnet")); nameValuePairs.add(new BasicNameValuePair("Password", "harshaviaPost123net")); post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(post); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line = ""; while ((line = rd.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } public boolean postBulkJSONImport(JSONObject urlParams, JSONObject postParams) throws IOException { //CloseableHttpClient httpclient = HttpClients.createDefault(); loggerObj.log(Level.INFO, "Inisde postBulkJSONImport"); JSONObject httpPostObjectParams = new JSONObject(); String emailaddr = (String) urlParams.get("URLEmail"); String dbName = (String) urlParams.get("DBName"); String tableName = (String) urlParams.get("TableName"); String authToken = (String) urlParams.get(AUTHTOKEN); String url = "https://reportsapi.zoho.com/api/" + emailaddr + "/" + URLEncoder.encode(dbName, "UTF-8") + "/" + URLEncoder.encode(tableName, "UTF-8") + "?ZOHO_ACTION=IMPORT&ZOHO_OUTPUT_FORMAT=json&ZOHO_ERROR_FORMAT=json&authtoken=" + authToken + "&ZOHO_API_VERSION=1.0"; loggerObj.log(Level.INFO, "url params are:" + url); httpPostObjectParams.put("url", url); String jsonFileName = (String) postParams.get("jsonFileName"); FileBody jsonFile = new FileBody(new File(jsonFileName)); String ZOHO_IMPORT_FILETYPE = (String) postParams.get("ZOHO_IMPORT_FILETYPE"); String ZOHO_IMPORT_TYPE = (String) postParams.get("ZOHO_IMPORT_TYPE"); String ZOHO_AUTO_IDENTIFY = (String) postParams.get("ZOHO_AUTO_IDENTIFY"); String ZOHO_CREATE_TABLE = (String) postParams.get("ZOHO_CREATE_TABLE"); String ZOHO_ON_IMPORT_ERROR = (String) postParams.get("ZOHO_ON_IMPORT_ERROR"); String ZOHO_MATCHING_COLUMNS = (String) postParams.get("ZOHO_MATCHING_COLUMNS"); String ZOHO_DATE_FORMAT = (String) postParams.get("ZOHO_DATE_FORMAT"); String ZOHO_SELECTED_COLUMNS = (String) postParams.get("ZOHO_SELECTED_COLUMNS"); loggerObj.log(Level.INFO, "httpPost params are:" + postParams.toJSONString()); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addPart("ZOHO_FILE", jsonFile); builder.addTextBody("ZOHO_IMPORT_FILETYPE", ZOHO_IMPORT_FILETYPE); builder.addTextBody("ZOHO_IMPORT_TYPE", ZOHO_IMPORT_TYPE); builder.addTextBody("ZOHO_AUTO_IDENTIFY", ZOHO_AUTO_IDENTIFY); builder.addTextBody("ZOHO_CREATE_TABLE", ZOHO_CREATE_TABLE); builder.addTextBody("ZOHO_ON_IMPORT_ERROR", ZOHO_ON_IMPORT_ERROR); if (ZOHO_MATCHING_COLUMNS != null) { builder.addTextBody("ZOHO_MATCHING_COLUMNS", ZOHO_MATCHING_COLUMNS); } if (ZOHO_SELECTED_COLUMNS != null) { builder.addTextBody("ZOHO_SELECTED_COLUMNS", ZOHO_SELECTED_COLUMNS); } builder.addTextBody("ZOHO_DATE_FORMAT", ZOHO_DATE_FORMAT); httpPostObjectParams.put("multiPartEntityBuilder", builder); HttpsClient httpsClientObj = new HttpsClient(); boolean isSuccessfulPost = httpsClientObj.httpsPost(url, builder, null); return isSuccessfulPost; } public boolean delete(JSONObject urlParams, JSONObject criteria) { // CloseableHttpClient httpclient = HttpClients.createDefault(); // HttpPost post = new HttpPost("https://reportsapi.zoho.com/api/harshavardhan.r@zohocorp.com/Test/Name_password?ZOHO_ACTION=DELETE&ZOHO_OUTPUT_FORMAT=json&ZOHO_ERROR_FORMAT=json&authtoken=7ed717b94bc30455aad11ce5d31d34f9&ZOHO_API_VERSION=1.0"); /*try { List<NameValuePair> nameValuePairs = new ArrayList<>(1); nameValuePairs.add(new BasicNameValuePair("ZOHO_CRITERIA", "(\"Name\" = 'harshaViaPostimportNet15')")); post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(post); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line = ""; while ((line = rd.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); }*/ loggerObj.log(Level.INFO, "Inisde Delete method"); JSONObject httpPostObjectParams = new JSONObject(); String emailaddr = (String) urlParams.get("URLEmail"); String dbName = (String) urlParams.get("DBName"); String tableName = (String) urlParams.get("TableName"); String authToken = (String) urlParams.get(AUTHTOKEN); String url = "https://reportsapi.zoho.com/api/" + emailaddr + "/" + dbName + "/" + tableName + "?ZOHO_ACTION=DELETE&ZOHO_OUTPUT_FORMAT=json&ZOHO_ERROR_FORMAT=json&authtoken=" + authToken + "&ZOHO_API_VERSION=1.0"; loggerObj.log(Level.INFO, "url params are:" + url); httpPostObjectParams.put("url", url); JSONObject postParams = null; List<NameValuePair> nameValuePairs = new ArrayList<>(criteria.size()); //multiple criteria wont work. Need to test how how multiple criteria has to be given// for (Iterator iterator = criteria.keySet().iterator(); iterator.hasNext();) { String key = (String) iterator.next(); nameValuePairs.add(new BasicNameValuePair("ZOHO_CRITERIA", "(\"" + key + "\" = '" + (String) criteria.get(key) + "')")); } UrlEncodedFormEntity formParameters = null; try { formParameters = new UrlEncodedFormEntity(nameValuePairs); } catch (UnsupportedEncodingException ex) { loggerObj.log(Level.INFO, "The delete criteria cannot be encoded properly" + ex.toString()); return false; } loggerObj.log(Level.INFO, "httpPost form params are:" + nameValuePairs.toString()); HttpsClient httpsClientObj = new HttpsClient(); boolean isSuccessfulPost = httpsClientObj.httpsPost(url, null, formParameters); return isSuccessfulPost; } }