Skip to content

Instantly share code, notes, and snippets.

@djuang1
Created December 8, 2014 21:00
Show Gist options
  • Save djuang1/30e5b5a05dc6b6c86a03 to your computer and use it in GitHub Desktop.
Save djuang1/30e5b5a05dc6b6c86a03 to your computer and use it in GitHub Desktop.
Mule - Groovy - MSCRM 4.0 SOAP Request - Retrieve Account
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.auth.params.AuthPNames;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.AuthPolicy;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
String ACCOUNT_RETRIEVE_SOAP_TEMPLATE =
" <soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:web=\"http://schemas.microsoft.com/crm/2006/WebServices\" xmlns:cor=\"http://schemas.microsoft.com/crm/2006/CoreTypes\"> " +
" <soap:Body>" +
" <entityName xmlns=\"http://schemas.microsoft.com/crm/2006/WebServices\" xmlns:ns2=\"http://schemas.microsoft.com/crm/2006/Query\" xmlns:ns3=\"http://schemas.microsoft.com/crm/2006/CoreTypes\" xmlns:ns4=\"http://schemas.microsoft.com/crm/2006/Scheduling\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">account</entityName>" +
" <id xmlns=\"http://schemas.microsoft.com/crm/2006/WebServices\" xmlns:ns2=\"http://schemas.microsoft.com/crm/2006/Query\" xmlns:ns3=\"http://schemas.microsoft.com/crm/2006/CoreTypes\" xmlns:ns4=\"http://schemas.microsoft.com/crm/2006/Scheduling\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">B0248E98-9772-E411-A8BD-22000B5D89F2</id>" +
" <columnSet xmlns=\"http://schemas.microsoft.com/crm/2006/WebServices\" xmlns:ns2=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"ns2:ColumnSet\" xmlns:ns3=\"http://schemas.microsoft.com/crm/2006/CoreTypes\" xmlns:ns4=\"http://schemas.microsoft.com/crm/2006/Scheduling\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
" <ns2:Attributes>" +
" <ns2:Attribute>name</ns2:Attribute>" +
" </ns2:Attributes>" +
" </columnSet>" +
" </soap:Body>" +
" </soap:Envelope>";
String soapMessage = ACCOUNT_RETRIEVE_SOAP_TEMPLATE;
String soapaction = "http://schemas.microsoft.com/crm/2006/WebServices/Retrieve";
DefaultHttpClient httpClient = new DefaultHttpClient();
// Authentication
List<String> authPreferences = new ArrayList<String>();
authPreferences.add(AuthPolicy.NTLM);
authPreferences.add(AuthPolicy.BASIC);
httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, authPreferences);
NTCredentials credentials = new NTCredentials("${ntlm.user}", "${ntlm.password}", "${ntlm.workstation}", "${ntlm.domain}");
httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials);
// Prepare Request
HttpPost request = new HttpPost( "/MSCrmServices/2006/CrmService.asmx" );
request.setEntity(new StringEntity(soapMessage));
request.addHeader("Content-Type", "text/xml;charset=UTF-8");
request.addHeader("SOAPAction", soapaction);
// Execute
HttpResponse response = httpClient.execute(new HttpHost("${mscrm.host}"), request);
HttpEntity entity = response.getEntity();
// Process Response
InputStreamReader inputStreamReader = new InputStreamReader(entity.getContent());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String nextLine = null;
while ((nextLine = bufferedReader.readLine()) != null) {
stringBuilder.append(nextLine);
}
return stringBuilder.toString();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment