Skip to content

Commit

Permalink
Merge pull request #1247 from ashitsalesforce/master
Browse files Browse the repository at this point in the history
split server url setting into 2 settings for production and sandbox
  • Loading branch information
ashitsalesforce authored Sep 2, 2024
2 parents 1f50b58 + e2d9c4c commit 1c79a7a
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 60 deletions.
24 changes: 15 additions & 9 deletions src/main/java/com/salesforce/dataloader/client/PartnerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -596,11 +596,10 @@ private boolean login() throws ConnectionException, ApiFault {
} catch (ConnectionException e) {
logger.warn(Messages.getMessage(this.getClass(), "failedUsernamePasswordAuth",
origEndpoint, Config.ENDPOINT, e.getMessage()));
if (Config.PROD_ENVIRONMENT_VAL.equals(config.getString(Config.SELECTED_AUTH_ENVIRONMENT))
&& !Config.DEFAULT_ENDPOINT_URL.equalsIgnoreCase(origEndpoint)) {
if (!config.isDefaultAuthEndpoint(origEndpoint)) {
// retry with default endpoint URL only if user is attempting production login
config.setAuthEndpoint(Config.DEFAULT_ENDPOINT_URL);
logger.info(Messages.getMessage(this.getClass(), "retryUsernamePasswordAuth", Config.DEFAULT_ENDPOINT_URL, Config.ENDPOINT));
config.setAuthEndpoint(config.getDefaultAuthEndpoint());
logger.info(Messages.getMessage(this.getClass(), "retryUsernamePasswordAuth", config.getDefaultAuthEndpoint(), Config.ENDPOINT));
login();
} else {
logger.error("Failed to get user info using manually configured session id", e);
Expand All @@ -616,7 +615,7 @@ private boolean login() throws ConnectionException, ApiFault {
private boolean dologin() throws ConnectionException, ApiFault {
// Attempt the login giving the user feedback
logger.info(Messages.getString("Client.sforceLogin")); //$NON-NLS-1$
final ConnectorConfig cc = getLoginConnectorConfig();
ConnectorConfig cc = getLoginConnectorConfig();
boolean savedIsTraceMessage = cc.isTraceMessage();
cc.setTraceMessage(false);
PartnerConnection conn = Connector.newConnection(cc);
Expand All @@ -626,6 +625,10 @@ private boolean dologin() throws ConnectionException, ApiFault {
String oauthAccessToken = config.getString(Config.OAUTH_ACCESSTOKEN);
try {
if (oauthAccessToken != null && oauthAccessToken.trim().length() > 0) {
cc = getLoginConnectorConfig(config.getString(Config.OAUTH_INSTANCE_URL));
savedIsTraceMessage = cc.isTraceMessage();
cc.setTraceMessage(false);
conn = Connector.newConnection(cc);
conn = setConfiguredSessionId(conn, oauthAccessToken, null);
} else if (config.getBoolean(Config.SFDC_INTERNAL) && config.getBoolean(Config.SFDC_INTERNAL_IS_SESSION_ID_LOGIN)) {
conn = setConfiguredSessionId(conn, config.getString(Config.SFDC_INTERNAL_SESSION_ID), null);
Expand Down Expand Up @@ -881,13 +884,16 @@ public static String getServicePath() {
}

private synchronized ConnectorConfig getLoginConnectorConfig() {
return getLoginConnectorConfig(getDefaultServer());
}

private synchronized ConnectorConfig getLoginConnectorConfig(String serverURL) {
this.connectorConfig = getConnectorConfig();
String serverUrl = getDefaultServer();
this.connectorConfig.setAuthEndpoint(serverUrl + getServicePath());
this.connectorConfig.setServiceEndpoint(serverUrl + getServicePath());
this.connectorConfig.setAuthEndpoint(serverURL + getServicePath());
this.connectorConfig.setServiceEndpoint(serverURL + getServicePath());
return this.connectorConfig;
}

private String getDefaultServer() {
String serverUrl = config.getAuthEndpoint();
if (serverUrl == null || serverUrl.length() == 0) {
Expand Down
74 changes: 60 additions & 14 deletions src/main/java/com/salesforce/dataloader/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,13 @@ public class Config {
public static final int DEFAULT_BULK_API_IMPORT_BATCH_SIZE = 2000;

public static final long DEFAULT_BULK_API_CHECK_STATUS_INTERVAL = 5000L;
public static final String DEFAULT_ENDPOINT_URL = "https://login.salesforce.com";
public static final String DEFAULT_ENDPOINT_URL_PROD = "https://login.salesforce.com";
public static final String DEFAULT_ENDPOINT_URL_SANDBOX = "https://test.salesforce.com";
public static final String LIGHTNING_ENDPOINT_URL_PART_VAL = "lightning.force.com";
public static final String MYSF_ENDPOINT_URL_PART_VAL = "mysalesforce.com";
public static final String PROD_ENVIRONMENT_VAL = "Production";
public static final String SB_ENVIRONMENT_VAL = "Sandbox";

public static final String OAUTH_PROD_SERVER_VAL = "https://login.salesforce.com/";
public static final String OAUTH_SB_SERVER_VAL = "https://test.salesforce.com/";

public static final String OAUTH_PROD_REDIRECTURI_VAL = "https://login.salesforce.com/services/oauth2/success";
public static final String OAUTH_SB_REDIRECTURI_VAL = "https://test.salesforce.com/services/oauth2/success";

Expand Down Expand Up @@ -230,6 +228,8 @@ public class Config {
public static final String USERNAME = "sfdc.username"; //$NON-NLS-1$
public static final String PASSWORD = "sfdc.password"; //$NON-NLS-1$
public static final String ENDPOINT = "sfdc.endpoint"; //$NON-NLS-1$
public static final String ENDPOINT_PROD = "sfdc.endpoint.production"; //$NON-NLS-1$
public static final String ENDPOINT_SANDBOX = "sfdc.endpoint.sandbox"; //$NON-NLS-1$
public static final String PROXY_HOST = "sfdc.proxyHost"; //$NON-NLS-1$
public static final String PROXY_PORT = "sfdc.proxyPort"; //$NON-NLS-1$
public static final String PROXY_USERNAME = "sfdc.proxyUsername"; //$NON-NLS-1$
Expand Down Expand Up @@ -337,7 +337,7 @@ public class Config {
public static final String READ_CHARSET = "dataAccess.readCharset";

public static final String API_VERSION_PROP="salesforce.api.version";

public static final String OAUTH_INSTANCE_URL="salesforce.oauth.instanceURL";

/**
* =============== PILOT config properties ========
Expand Down Expand Up @@ -475,7 +475,8 @@ public class Config {
DAO_READ_PREPROCESSOR_SCRIPT,
DAO_WRITE_POSTPROCESSOR_SCRIPT,
ENFORCE_WIZARD_WIDTH_HEIGHT_CONFIG,
DELETE_WITH_EXTERNALID
DELETE_WITH_EXTERNALID,
OAUTH_INSTANCE_URL
};

/**
Expand Down Expand Up @@ -512,6 +513,11 @@ private Config(String filename, Map<String, String> overridesMap) throws ConfigI

// Properties initialization completed. Configure OAuth environment next
setOAuthEnvironment(getString(SELECTED_AUTH_ENVIRONMENT));
String endpoint_prod = getString(Config.ENDPOINT_PROD);
if (Config.DEFAULT_ENDPOINT_URL_PROD.equalsIgnoreCase(endpoint_prod)) {
endpoint_prod = getString(Config.ENDPOINT);
setValue(ENDPOINT_PROD, endpoint_prod);
}
}

private String getLastRunPrefix() {
Expand Down Expand Up @@ -564,7 +570,10 @@ private void setDefaults() {
setDefaultValue(CSV_DELIMITER_OTHER_VALUE, "-");
setDefaultValue(CSV_DELIMITER_FOR_QUERY_RESULTS, AppUtil.COMMA);

setDefaultValue(ENDPOINT, DEFAULT_ENDPOINT_URL);
setDefaultValue(ENDPOINT, DEFAULT_ENDPOINT_URL_PROD);
setDefaultValue(ENDPOINT_PROD, getString(ENDPOINT));
setDefaultValue(ENDPOINT_SANDBOX, DEFAULT_ENDPOINT_URL_SANDBOX);

setDefaultValue(IMPORT_BATCH_SIZE, useBulkApiByDefault() ? DEFAULT_BULK_API_IMPORT_BATCH_SIZE : DEFAULT_LOAD_BATCH_SIZE);
setDefaultValue(LOAD_ROW_TO_START_AT, 0);
setDefaultValue(TIMEOUT_SECS, DEFAULT_TIMEOUT_SECS);
Expand Down Expand Up @@ -597,8 +606,8 @@ private void setDefaults() {
setDefaultValue(SFDC_INTERNAL_SESSION_ID, (String) null);

//oauth settings
setDefaultValue(OAUTH_SERVER, DEFAULT_ENDPOINT_URL);
setDefaultValue(OAUTH_REDIRECTURI, DEFAULT_ENDPOINT_URL);
setDefaultValue(OAUTH_SERVER, DEFAULT_ENDPOINT_URL_PROD);
setDefaultValue(OAUTH_REDIRECTURI, DEFAULT_ENDPOINT_URL_PROD);
setDefaultValue(SELECTED_AUTH_ENVIRONMENT, PROD_ENVIRONMENT_VAL);
setDefaultValue(AUTH_ENVIRONMENTS, PROD_ENVIRONMENT_VAL + AppUtil.COMMA + SB_ENVIRONMENT_VAL);

Expand Down Expand Up @@ -641,6 +650,7 @@ private void setDefaults() {
setDefaultValue(CACHE_DESCRIBE_GLOBAL_RESULTS, true);
setDefaultValue(PROCESS_EXIT_WITH_ERROR_ON_FAILED_ROWS_BATCH_MODE, false);
setDefaultValue(INCLUDE_RICH_TEXT_FIELD_DATA_IN_QUERY_RESULTS, false);
setDefaultValue(OAUTH_INSTANCE_URL, false);
}

/**
Expand Down Expand Up @@ -1177,15 +1187,51 @@ public void save() throws IOException, GeneralSecurityException {
}

public void setAuthEndpoint(String authEndpoint) {
// TODO - check current environment and set environment-specific endpoint
this.setValue(Config.ENDPOINT, authEndpoint);
this.setAuthEndpointForEnv(authEndpoint, getString(Config.SELECTED_AUTH_ENVIRONMENT));
}

public void setAuthEndpointForEnv(String authEndpoint, String env) {
if (env != null && env.equalsIgnoreCase(getString(Config.SB_ENVIRONMENT_VAL))) {
this.setValue(Config.ENDPOINT_SANDBOX, authEndpoint);
} else {
this.setValue(Config.ENDPOINT_PROD, authEndpoint);
}
}

public String getAuthEndpoint() {
// TODO - return the auth endpoint for current environment
String endpoint = getString(Config.ENDPOINT);
if (Config.SB_ENVIRONMENT_VAL.equals(this.getString(Config.SELECTED_AUTH_ENVIRONMENT))) {
endpoint = getString(Config.ENDPOINT_SANDBOX);
if (endpoint == null || endpoint.isBlank()) {
endpoint = getDefaultAuthEndpoint();
}
} else {
endpoint = getString(Config.ENDPOINT_PROD);
if (endpoint == null || endpoint.isBlank()) {
endpoint = getDefaultAuthEndpoint();
}
}
return endpoint;
}

public String getDefaultAuthEndpoint() {
if (Config.SB_ENVIRONMENT_VAL.equals(this.getString(Config.SELECTED_AUTH_ENVIRONMENT))) {
return Config.DEFAULT_ENDPOINT_URL_SANDBOX;
} else { // assume production is the only alternate environment
return Config.DEFAULT_ENDPOINT_URL_PROD;
}
}

public boolean isDefaultAuthEndpoint(String endpoint) {
if (endpoint == null || endpoint.isBlank()) {
return false;
}
if (Config.SB_ENVIRONMENT_VAL.equals(this.getString(Config.SELECTED_AUTH_ENVIRONMENT))) {
return Config.DEFAULT_ENDPOINT_URL_SANDBOX.equalsIgnoreCase(endpoint);
} else { // assume production is the only alternate environment
return Config.DEFAULT_ENDPOINT_URL_PROD.equalsIgnoreCase(endpoint);
}
}

private void removeUnsupportedProperties() {
// do not save a value for enabling Bulk V2
Expand Down Expand Up @@ -1519,7 +1565,7 @@ public void setOAuthEnvironment(String environment) {
String envSpecificOAuthServerURL = getOAuthEnvironmentString(environment, OAUTH_PARTIAL_SERVER);
if (envSpecificOAuthServerURL != null
&& !envSpecificOAuthServerURL.contains(Config.OAUTH_DEFAULT_SERVER_DNS)
&& !envSpecificOAuthServerURL.contains(Config.DEFAULT_ENDPOINT_URL)) {
&& !envSpecificOAuthServerURL.contains(Config.DEFAULT_ENDPOINT_URL_PROD)) {
endpointURL = envSpecificOAuthServerURL;
}
setValue(OAUTH_SERVER, endpointURL);
Expand All @@ -1528,7 +1574,7 @@ public void setOAuthEnvironment(String environment) {
String redirectURI = "";
if (envSpecificOAuthRedirectURI != null
&& !envSpecificOAuthServerURL.contains(Config.OAUTH_DEFAULT_SERVER_DNS)
&& !envSpecificOAuthServerURL.contains(Config.DEFAULT_ENDPOINT_URL)) {
&& !envSpecificOAuthServerURL.contains(Config.DEFAULT_ENDPOINT_URL_PROD)) {
redirectURI = envSpecificOAuthRedirectURI;
} else {
redirectURI = endpointURL + Config.OAUTH_REDIRECT_URI_SUFFIX;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public static boolean handleCompletedUrl(String url, Config config) throws URISy

config.setValue(Config.OAUTH_ACCESSTOKEN, token.getAccessToken());
config.setValue(Config.OAUTH_REFRESHTOKEN, token.getRefreshToken());
config.setAuthEndpoint(token.getInstanceUrl());

config.setValue(Config.OAUTH_INSTANCE_URL, token.getInstanceUrl());
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.salesforce.dataloader.controller.Controller;
import com.salesforce.dataloader.util.AppUtil;
import com.salesforce.dataloader.util.LoggingUtil;
import com.sforce.soap.partner.Connector;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
Expand Down Expand Up @@ -60,8 +59,6 @@
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
Expand All @@ -75,7 +72,8 @@ public class AdvancedSettingsDialog extends BaseDialog {
private Button buttonNulls;
private Text labelNulls;
private Text textRule;
private Text textEndpoint;
private Text textProdEndpoint;
private Text textSBEndpoint;
private Button buttonCompression;
private Button buttonResetUrl;
private Text textTimeout;
Expand All @@ -93,7 +91,6 @@ public class AdvancedSettingsDialog extends BaseDialog {
private Text textSandboxBulkClientID;
private Text textWizardWidth;
private Text textWizardHeight;
private final String defaultServer;

private Button buttonShowWelcomeScreen;
private Button buttonShowLoaderUpgradeScreen;
Expand Down Expand Up @@ -137,16 +134,6 @@ public class AdvancedSettingsDialog extends BaseDialog {
*/
public AdvancedSettingsDialog(Shell parent, Controller controller) {
super(parent, controller);

URI uri;
String server = "";
try {
uri = new URI(Connector.END_POINT);
server = uri.getScheme() + "://" + uri.getHost(); //$NON-NLS-1$
} catch (URISyntaxException e) {
logger.error("", e);
}
defaultServer = server;
}

private final Map<Button, Composite> apiOptionsMap = new HashMap<Button, Composite>();
Expand Down Expand Up @@ -599,17 +586,28 @@ public void verifyText(VerifyEvent event) {
textRule.setText(config.getString(Config.ASSIGNMENT_RULE));
textRule.setToolTipText(Labels.getString("AdvancedSettingsDialog.TooltipAssignmentRule"));

//endpoint
createLabel(restComp, "serverURL", null, null);
textEndpoint = new Text(restComp, SWT.BORDER);
//endpoints
createLabel(restComp, "prodServerURL", null, null);
textProdEndpoint = new Text(restComp, SWT.BORDER);
data = new GridData(GridData.FILL_HORIZONTAL);
data.widthHint = 30 * textSize.x;
textProdEndpoint.setLayoutData(data);
String endpoint = config.getString(Config.ENDPOINT_PROD);
if ("".equals(endpoint)) { //$NON-NLS-1$
endpoint = Config.DEFAULT_ENDPOINT_URL_PROD;
}
textProdEndpoint.setText(endpoint);

createLabel(restComp, "sandboxServerURL", null, null);
textSBEndpoint = new Text(restComp, SWT.BORDER);
data = new GridData(GridData.FILL_HORIZONTAL);
data.widthHint = 30 * textSize.x;
textEndpoint.setLayoutData(data);
String endpoint = config.getAuthEndpoint();
textSBEndpoint.setLayoutData(data);
endpoint = config.getString(Config.ENDPOINT_SANDBOX);
if ("".equals(endpoint)) { //$NON-NLS-1$
endpoint = defaultServer;
endpoint = Config.DEFAULT_ENDPOINT_URL_SANDBOX;
}
textEndpoint.setText(endpoint);
textSBEndpoint.setText(endpoint);

//reset url on login
createLabel(restComp, "resetUrlOnLogin", null, null);
Expand Down Expand Up @@ -944,10 +942,19 @@ public void verifyText(VerifyEvent event) {
public void widgetSelected(SelectionEvent event) {
Config config = getController().getConfig();

String currentTextEndpoint = textEndpoint.getText();
if (currentTextEndpoint != null && !currentTextEndpoint.isEmpty() && !AppUtil.isValidHttpsUrl(currentTextEndpoint)) {
String currentTextProdEndpoint = textProdEndpoint.getText();
if (currentTextProdEndpoint != null && !currentTextProdEndpoint.isEmpty() && !AppUtil.isValidHttpsUrl(currentTextProdEndpoint)) {
MessageDialog alert = new MessageDialog(getParent().getShell(), "Warning", null,
Labels.getFormattedString("AdvancedSettingsDialog.serverURLInfo", currentTextProdEndpoint),
MessageDialog.ERROR, new String[]{"OK"}, 0);
alert.open();
return;

}
String currentTextSBEndpoint = textProdEndpoint.getText();
if (currentTextSBEndpoint != null && !currentTextSBEndpoint.isEmpty() && !AppUtil.isValidHttpsUrl(currentTextSBEndpoint)) {
MessageDialog alert = new MessageDialog(getParent().getShell(), "Warning", null,
Labels.getFormattedString("AdvancedSettingsDialog.serverURLInfo", currentTextEndpoint),
Labels.getFormattedString("AdvancedSettingsDialog.serverURLInfo", currentTextSBEndpoint),
MessageDialog.ERROR, new String[]{"OK"}, 0);
alert.open();
return;
Expand Down Expand Up @@ -980,7 +987,8 @@ public void widgetSelected(SelectionEvent event) {
config.setValue(Config.CSV_DELIMITER_OTHER, isOtherDelimiterSpecified);

config.setValue(Config.EXPORT_BATCH_SIZE, textExportBatchSize.getText());
config.setAuthEndpoint(currentTextEndpoint);
config.setAuthEndpointForEnv(currentTextProdEndpoint, Config.PROD_ENVIRONMENT_VAL);
config.setAuthEndpointForEnv(currentTextSBEndpoint, Config.SB_ENVIRONMENT_VAL);
config.setValue(Config.ASSIGNMENT_RULE, textRule.getText());
config.setValue(Config.LOAD_ROW_TO_START_AT, textRowToStart.getText());
config.setValue(Config.RESET_URL_ON_LOGIN, buttonResetUrl.getSelection());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ public OAuthBrowserLoginRunner(Config config, boolean skipUserCodePage) throws I
startBrowserLogin(config, skipUserCodePage);
} catch (Exception ex) {
logger.warn(Messages.getMessage(this.getClass(), "failedAuthStart", origEndpoint, ex.getMessage()));
if (Config.PROD_ENVIRONMENT_VAL.equals(config.getString(Config.SELECTED_AUTH_ENVIRONMENT))
&& !Config.DEFAULT_ENDPOINT_URL.equalsIgnoreCase(origEndpoint)) {
if (!config.isDefaultAuthEndpoint(origEndpoint)) {
// retry with default endpoint URL only if user is attempting production login
retryBrowserLoginWithDefaultURL(config, skipUserCodePage);
}
Expand All @@ -87,9 +86,8 @@ public OAuthBrowserLoginRunner(Config config, boolean skipUserCodePage) throws I
}

private void retryBrowserLoginWithDefaultURL(Config config, boolean skipUserCodePage) throws IOException, ParameterLoadException, OAuthBrowserLoginRunnerException {
String oAuthServer = Config.DEFAULT_ENDPOINT_URL;
logger.info(Messages.getMessage(this.getClass(), "retryAuthStart", oAuthServer));
config.setAuthEndpoint(oAuthServer);
logger.info(Messages.getMessage(this.getClass(), "retryAuthStart", config.getDefaultAuthEndpoint()));
config.setAuthEndpoint(config.getDefaultAuthEndpoint());
startBrowserLogin(config, skipUserCodePage);
}

Expand Down Expand Up @@ -337,6 +335,6 @@ public static void processSuccessfulLogin(InputStream httpResponseInputStream, C
OAuthToken token = gson.fromJson(jsonTokenResult, OAuthToken.class);
config.setValue(Config.OAUTH_ACCESSTOKEN, token.getAccessToken());
config.setValue(Config.OAUTH_REFRESHTOKEN, token.getRefreshToken());
config.setAuthEndpoint(token.getInstanceUrl());
config.setValue(Config.OAUTH_INSTANCE_URL, token.getInstanceUrl());
}
}
Loading

0 comments on commit 1c79a7a

Please sign in to comment.