Java tutorial
/* Cirrostratus: Platform independent SOQL Editor for use with SalesForce and Dreamforce cloud databases Copyright (C) 2011 Steve S Gee Jr <ioexcept@gmail.com> Jim Daly <thejamesdaly@gmail.com> Bryan Leboff <leboff@gmail.com> http://cirrostratus.sourceforge.net/ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.html>. */ package org.geefive.salesforce.soqleditor; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.util.HashMap; import java.util.Iterator; import java.util.Properties; import java.util.Vector; import java.util.logging.LogManager; import java.util.logging.Logger; import javax.xml.namespace.QName; import javax.xml.ws.WebServiceException; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.GetMethod; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONTokener; import com.sforce.soap.partner.*; import com.sforce.ws.ConnectorConfig; public class SFDCConnectionManager extends Thread { private final String REST_QUERY = "/services/data/v20.0/query"; private final String REST_SOBJECT = "/services/data/v20.0/sobjects"; private String INSTANCE_ENDPOINT = null; private final String PRODUCTION_ENDPOINT = "https://login.salesforce.com/services/Soap/u/22.0"; private final String DEVELOPMENT_ENDPOINT = "https://test.salesforce.com/services/Soap/u/22.0"; private Properties preferences = new Properties(); private Logger logger = null; private String restfulURLTarget = null; private HttpClient restClient = null; private String SID = null; private String serverDomain = ""; private LogInInterface loginInterface = null; private boolean loginStatus = false; private String userName = ""; private String password = ""; private String exceptionMessage = ""; private boolean isProduction = false; public SFDCOAuth getOAuthObject() { return new SFDCOAuth(serverDomain, REST_SOBJECT, REST_QUERY, SID); } public SFDCConnectionManager(LogInInterface loginIface, String uname, String pword, boolean isPrd) { InputStream propertyInputStream = null; userName = uname; password = pword; isProduction = isPrd; loginInterface = loginIface; try { File logDir = new File("log"); if (!logDir.exists()) { logDir.mkdir(); } propertyInputStream = this.getClass().getResourceAsStream("logging.properties"); LogManager.getLogManager().readConfiguration(propertyInputStream); preferences.load(propertyInputStream); logger = Logger.getLogger("Cirrostratus"); } catch (IOException ex) { logger.info("WARNING: Could not open configuration file"); logger.info("WARNING: Logging not configured (console output only)"); ex.printStackTrace(); } finally { try { propertyInputStream.close(); } catch (Exception ex) { } } logger.info("starting Cirrostratus"); } public void run() { try { LoginResult loginResponse = null; updateStatus("Executing Enterprise SOAP WSDL Login Call with credentials"); updateStatus("Username: " + userName); // if (isProduction) { INSTANCE_ENDPOINT = PRODUCTION_ENDPOINT; } else { INSTANCE_ENDPOINT = DEVELOPMENT_ENDPOINT; } System.out.println("TARGET ENDPOINT IS SET TO: " + INSTANCE_ENDPOINT); updateStatus("TARGET ENDPOINT IS SET TO: " + INSTANCE_ENDPOINT); ConnectorConfig config = new ConnectorConfig(); config.setPassword(password); config.setUsername(userName); config.setServiceEndpoint(INSTANCE_ENDPOINT); config.setAuthEndpoint(INSTANCE_ENDPOINT); PartnerConnection connector = new PartnerConnection(config); loginResponse = connector.login(userName, password); updateStatus("Login was successfull."); SID = loginResponse.getSessionId(); String fullServerURL = loginResponse.getServerUrl(); serverDomain = fullServerURL.substring(0, (fullServerURL.indexOf("services") - 1)); updateStatus("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"); updateStatus("Full Server URL: " + fullServerURL); updateStatus("Server Domain: " + serverDomain); updateStatus("The returned session id is: " + loginResponse.getSessionId()); updateStatus("Your logged in user id is: " + loginResponse.getUserId()); updateStatus("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"); restClient = new HttpClient(); restClient.getParams().setCookiePolicy(CookiePolicy.RFC_2109); restClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000); // String restfulURLTarget = serverDomain + REST_LOGIN; restfulURLTarget = serverDomain + REST_QUERY; updateStatus("RESTful URL Target: " + restfulURLTarget); updateStatus("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% EXIT %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"); loginStatus = true; } catch (Exception ex) { ex.printStackTrace(); exceptionMessage = ex.getMessage(); } loginInterface.euthanize(loginStatus); } public String fetchExceptionMessage() { return exceptionMessage; } public void updateStatus(String msg) { System.out.println(msg); if (loginInterface != null) loginInterface.updateStatus(msg); } public void updateStatus(Exception ex) { ex.printStackTrace(); if (loginInterface != null) { StackTraceElement[] elements = ex.getStackTrace(); int counter = 0; loginInterface.updateStatus(ex.getMessage()); while (counter < elements.length && counter < 20) { loginInterface.updateStatus(elements[counter].toString()); counter++; } } } }