ARCHIVE – Calling SugarCRM via REST from Java using Apache http

The information below may be outdated

Please contact us for further information

EMAIL: sales@providentcrm.com        PHONE: +353 1 693 0000

We are currently working with some Large IBM customers with legacy apps requiring integration with SugarCRM.  Their preferred method is via JAVA connector and we have recommended the SugarCRM REST API for a number of reasons including performance and ease of enhancement.

Generally SugarCRM REST API allows you to use both POST and GET request method for the same function.

As the POST method claims to be faster, we will use it for the example below:

Following Java code will send a POST request and receives a session ID in the response. Both request and response will be JSON encoded.  Thanks to Andreas Neu for his original contribution.

import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.LinkedHashMap;
import java.util.Map;import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;public class LoginPost {

/**
* This method logs in to the SugarCRM with a POST and returns the session
* id.
* @return {@code String} session id
* @throws NoSuchAlgorithmException
* @throws ClientProtocolException
* @throws IOException
*/
private String login() throws NoSuchAlgorithmException,
ClientProtocolException, IOException {
// user your credentials
String username = “myUsername”;
String password = “myPassword”;

MessageDigest md5 = MessageDigest.getInstance(“MD5”);
String passwordHash = new BigInteger(1, md5.digest(password.getBytes()))
.toString(16);

// the order is important, so use a ordered map
Map<String, String> userCredentials = new LinkedHashMap<String, String>();
userCredentials.put(“user_name”, username);
userCredentials.put(“password”, passwordHash);

// the order is important, so use a ordered map
Map<String, Object> request = new LinkedHashMap<String, Object>();
request.put(“user_auth”, userCredentials);
request.put(“application_name”, “RestClient”);

MultipartEntity multipartEntity = new MultipartEntity();
multipartEntity.addPart(“method”, new StringBody(“login”));
// define request encoding
multipartEntity.addPart(“input_type”, new StringBody(“JSON”));
// define response encoding
multipartEntity.addPart(“response_type”, new StringBody(“JSON”));
multipartEntity.addPart(“rest_data”,
new StringBody(JSONObject.toJSONString(request)));

// yourSugarCRM has to be changed to your SugarCRM instance
// something like localhost/sugarcrm
String url = “http://<yourSugarCRM>/service/v4_1/rest.php?“;
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(multipartEntity);

DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
HttpResponse execute = defaultHttpClient.execute(httpPost);

HttpEntity entity = execute.getEntity();

JSONObject parse = (JSONObject) JSONValue.parse(new InputStreamReader(
entity.getContent()));
return (String) parse.get(“id”);
}
}