Skip to content

Instantly share code, notes, and snippets.

@metadaddy
Created November 2, 2011 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metadaddy/1334017 to your computer and use it in GitHub Desktop.
Save metadaddy/1334017 to your computer and use it in GitHub Desktop.
Set login IP ranges via the Metadata API
package com.force.samples;
import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.metadata.AsyncRequestState;
import com.sforce.soap.metadata.AsyncResult;
import com.sforce.soap.metadata.MetadataConnection;
import com.sforce.soap.metadata.Profile;
import com.sforce.soap.metadata.ProfileLoginIpRange;
import com.sforce.soap.metadata.UpdateMetadata;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
public class MetadataSample {
private MetadataConnection metadataConnection;
private void run() throws ConnectionException {
login();
String[][] ranges = { { "1.2.3.0", "1.2.3.255" },
{ "2.3.4.0", "2.3.4.255" } };
setLoginIpRanges("Standard", ranges);
}
public static void main(String[] args) throws ConnectionException {
MetadataSample samples1 = new MetadataSample();
samples1.run();
}
/**
* The login call is used to obtain a token from Salesforce. This token must
* be passed to all other calls to provide authentication.
*/
private void login() throws ConnectionException {
String username = System.getenv("USERNAME");
String password = System.getenv("PASSWORD");
ConnectorConfig config = new ConnectorConfig();
config.setUsername(username);
config.setPassword(password);
EnterpriseConnection connection;
connection = Connector.newConnection(config);
ConnectorConfig metadataConfig = new ConnectorConfig();
metadataConfig.setSessionId(connection.getSessionHeader()
.getSessionId());
metadataConnection = com.sforce.soap.metadata.Connector
.newConnection(metadataConfig);
}
private void setLoginIpRanges(String profileName, String[][] ranges)
throws ConnectionException {
try {
ProfileLoginIpRange[] loginIpRanges = new ProfileLoginIpRange[ranges.length];
for (int i = 0; i < ranges.length; i++) {
loginIpRanges[i] = new ProfileLoginIpRange();
loginIpRanges[i].setStartAddress(ranges[i][0]);
loginIpRanges[i].setEndAddress(ranges[i][1]);
}
Profile profile = new Profile();
profile.setLoginIpRanges(loginIpRanges);
UpdateMetadata updateMetadata = new UpdateMetadata();
updateMetadata.setMetadata(profile);
updateMetadata.setCurrentName(profileName);
AsyncResult[] ars = metadataConnection
.update(new UpdateMetadata[] { updateMetadata });
AsyncResult asyncResult = ars[0];
long waitTimeMilliSecs = 1000;
while (!asyncResult.isDone()) {
Thread.sleep(waitTimeMilliSecs);
waitTimeMilliSecs *= 2;
asyncResult = metadataConnection
.checkStatus(new String[] { asyncResult.getId() })[0];
System.out.println("Status is: " + asyncResult.getState());
}
if (asyncResult.getState() != AsyncRequestState.Completed) {
System.out.println(asyncResult.getStatusCode() + " msg: "
+ asyncResult.getMessage());
}
} catch (InterruptedException ie) {
ie.printStackTrace();
} catch (ConnectionException ce) {
ce.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment