Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Wednesday, June 1, 2011

Unable to connect some MS CRM instance from SOAP UI tool.


The following soap request is executing well with almost all CRM4 AD instance. 

We have one customer with MSCRM 4 AD, but the following request is failed with error 401 Unauthorized access..
We have tried the following request from another language Obj C, its executed successfully and got the response

Why we are unable to connect some CRM instances from Soap UI or by java code ?

//we can provide the CRM 4 instance url privately to test to find whats the difference with others
from Java, we are using Axis to convert the java classes and to execute.
this is the error from java code
- ntlm authentication scheme selected
- Failure authenticating with NTLM @hostname:port
- Error in ezimg service.  AxisFault caught.  MessageId: null
AxisFault
 faultCode: {http://xml.apache.org/axis/}HTTP
 faultSubcode: 
 faultString: (401)Unauthorized 

Other MS CRM are connecting well after showing the  'ntlm authentication scheme selected'


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>
 <Execute xmlns="http://schemas.microsoft.com/crm/2007/CrmDiscoveryService">
  <Request xsi:type="ns1:RetrieveOrganizationsRequest" xmlns:ns1="http://schemas.microsoft.com/crm/2007/CrmDiscoveryService">
  <UserId xsi:type="xsd:string">Domain\userUserId>
  <Password xsi:type="xsd:string">passwdPassword>
  Request>
 Execute>
 soapenv:Body>
soapenv:Envelope>

-------------------------------------------Added on 2011-11-01---------

The above issue is soap ui is not supporting the ntlm2 authentication.
Also the httpClient-3.1 jar is not supporting the ntlm v2 authentication

We can implement our java code by using the jcifs.jar which is handling the ntlm2 authentication.

So we can call the ntlm class of jcifs from the httpClient-3.1.jar

This process is well written in this site






Wednesday, January 19, 2011

Java- Connect MS CRM Using CrmDiscoveryService



TypeValueDescription
AD0Specifies Active Directory authentication.
Passport1Specifies Windows Live ID authentication.
Spla2Specifies Internet-Facing Deployment authentication

1. Active Directory authentication.
Sample code
  try{

String username = "domain\\user";
String password = "password";
String authType = "AD";
String OrganizationName = "orgName";
String hostBame = "http://xxx.xxx.x.xxx";
String CrmDiscoveryURL = hostBame + "/MSCRMServices/2007/"+ authType + "/CrmDiscoveryService.asmx";


CrmDiscoveryURL = CrmDiscoveryURL.toLowerCase();

CrmDiscoveryServiceLocator discoveryServiceLoc = new CrmDiscoveryServiceLocator();
discoveryServiceLoc.setCrmDiscoveryServiceSoapEndpointAddress(CrmDiscoveryURL);
CrmDiscoveryService discoveryService = (CrmDiscoveryService) discoveryServiceLoc;
CrmDiscoveryServiceSoapStub discoveryServiceSoap = (CrmDiscoveryServiceSoapStub) discoveryService.getCrmDiscoveryServiceSoap();


discoveryServiceSoap.setUsername(username);
discoveryServiceSoap.setPassword(password);

RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
orgRequest.setUserId(username);
orgRequest.setPassword(password);
RetrieveOrganizationsResponse orgResp = (RetrieveOrganizationsResponse) discoveryServiceSoap.execute(orgRequest);

OrganizationDetail orgInfo = null;
com.microsoft.schemas.crm._2007.CrmDiscoveryService.ArrayOfOrganizationDetail arrayOfDetail=  orgResp.getOrganizationDetails();
OrganizationDetail[] orgdetails = arrayOfDetail.getOrganizationDetail();


for (int i = 0; i < orgdetails.length; i++) {


System.out.println("orgdetails[i].getOrganizationName() = "+ orgdetails[i].getOrganizationName());
if (orgdetails[i].getOrganizationName().equalsIgnoreCase(OrganizationName)) {

orgInfo = orgdetails[i];
break;
}
}

int AD = 0;
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.setAuthenticationType(AD);
token.setOrganizationName(OrganizationName);


String CrmServiceUrl = orgInfo.getCrmServiceUrl();




CrmServiceSoapStub adminBinding = (CrmServiceSoapStub) new CrmServiceLocator().getCrmServiceSoap(new URL(CrmServiceUrl));

adminBinding.setHeader("http://schemas.microsoft.com/crm/2007/WebServices","CrmAuthenticationToken", token);

adminBinding.setUsername(username);
adminBinding.setPassword(password);

WhoAmIRequest whoRequest = new WhoAmIRequest();
WhoAmIResponse whoResp = (WhoAmIResponse) adminBinding.execute(whoRequest);

String UserId = whoResp.getUserId();

System.out.println("userid = "+ UserId);

}catch(Exception e){
e.printStackTrace();
}

2. Passport Authentication 
Sample code  

String authType = "Passport";
CrmDiscoveryServiceLocator discoveryServiceLoc = new CrmDiscoveryServiceLocator();
discoveryServiceLoc.setCrmDiscoveryServiceSoapEndpointAddress("http://" + hostName + "/MSCRMServices/2007/" + authType + "/CrmDiscoveryService.asmx");

CrmDiscoveryService discoveryService = (CrmDiscoveryService) discoveryServiceLoc;
CrmDiscoveryServiceSoapStub discoveryServiceSoap = (CrmDiscoveryServiceSoapStub) discoveryService.getCrmDiscoveryServiceSoap();

// Retrieve Policy Request
RetrievePolicyRequest policyRequest = new RetrievePolicyRequest();
RetrievePolicyResponse policyResponse = (RetrievePolicyResponse) discoveryServiceSoap.execute(policyRequest);

String passportTicket = getPassportTicket(policyResponse.getPolicy(),userName, password);

RetrieveCrmTicketRequest crmTicketRequest = new RetrieveCrmTicketRequest();
crmTicketRequest.setOrganizationName(organization);
crmTicketRequest.setPassportTicket(passportTicket);
crmTicketResponse = (RetrieveCrmTicketResponse) discoveryServiceSoap.execute(crmTicketRequest);

if (crmTicketResponse != null) {

        CrmAuthenticationToken token = new CrmAuthenticationToken();

        int PASSPORT = 1;
        token.setAuthenticationType(PASSPORT);
token.setCallerId("00000000-0000-0000-0000-000000000000");
token.setOrganizationName(crmTicketResponse.getOrganizationDetail().getOrganizationName());
token.setCrmTicket(crmTicketResponse.getCrmTicket());
String crmServiceUrl = crmTicketResponse.getOrganizationDetail().getCrmServiceUrl();// we can store it in db w.r.t user,pass,org  and  retrieve
CrmServiceSoapStub crmServiceSoapStub = (CrmServiceSoapStub) new CrmServiceLocator().getCrmServiceSoap(new URL(crmServiceUrl));


crmServiceSoapStub.setHeader("http://schemas.microsoft.com/crm/2007/WebServices","CrmAuthenticationToken", token);

WhoAmIRequest whoRequest = new WhoAmIRequest();
whoRequest.setOptionalParameters(new OptionalParameter[] {});

WhoAmIResponse whoResp = (WhoAmIResponse) crmServiceSoapStub.execute(whoRequest);
System.out.println("----------getUserId()----------------->"+ whoResp.getOrganizationId());
}

private String getPassportTicket(String policy, String userName, String password) {

String ticket= null;


try{

String passportDomain = "crm.dynamics.com";//partner
String environment = "Production";
ticket = new MSLogonManager().logon(userName, password, passportDomain, policy, environment);
System.out.println("ticket = "+ticket);

}catch(Exception e){

e.printStackTrace();
}
return ticket;


3. IFD Authentication 

String hostBame = "https://xx.xxxx.xxx";
String OrganizationName = "orgName";
String username = "usernam";
String password = "password";

String CrmDiscoveryURL = hostBame+"/mscrmservices/2007/spla/crmdiscoveryservice.asmx";

CrmDiscoveryServiceLocator discoveryServiceLoc = new CrmDiscoveryServiceLocator();
discoveryServiceLoc.setCrmDiscoveryServiceSoapEndpointAddress(CrmDiscoveryURL);
CrmDiscoveryService discoveryService = (CrmDiscoveryService) discoveryServiceLoc;
CrmDiscoveryServiceSoapStub discoveryServiceSoap = (CrmDiscoveryServiceSoapStub) discoveryService.getCrmDiscoveryServiceSoap();

discoveryServiceSoap.setUsername(username);
discoveryServiceSoap.setPassword(password);

RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
orgRequest.setUserId(username);
orgRequest.setPassword(password);
RetrieveOrganizationsResponse orgResp = (RetrieveOrganizationsResponse) discoveryServiceSoap.execute(orgRequest);

OrganizationDetail orgInfo = null;
com.microsoft.schemas.crm._2007.CrmDiscoveryService.ArrayOfOrganizationDetail arrayOfDetail = orgResp.getOrganizationDetails();
OrganizationDetail[] orgdetails = arrayOfDetail.getOrganizationDetail();


for (int i = 0; i < orgdetails.length; i++) {

System.out.println(" getIFDBindingToCrm orgdetails[i].getOrganizationName()  "+ orgdetails[i].getOrganizationName());

if (orgdetails[i].getOrganizationName().equalsIgnoreCase(OrganizationName)) {

orgInfo = orgdetails[i];
break;
}
}

RetrieveCrmTicketRequest crmTicketRequest = new RetrieveCrmTicketRequest();
crmTicketRequest.setOrganizationName(OrganizationName);//orgInfo.getOrganizationName());
crmTicketRequest.setUserId(username);
crmTicketRequest.setPassword(password);
RetrieveCrmTicketResponse crmTicketResponse = (RetrieveCrmTicketResponse) discoveryServiceSoap.execute(crmTicketRequest);

if (crmTicketResponse != null) {//

int IFD = 2;
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.setAuthenticationType(IFD);
token.setOrganizationName(orgInfo.getOrganizationName());
token.setCrmTicket(crmTicketResponse.getCrmTicket());

String CrmServiceUrl = orgInfo.getCrmServiceUrl();
System.out.println("CrmServiceUrl-" + CrmServiceUrl);

CrmServiceSoapStub adminBinding = (CrmServiceSoapStub) new CrmServiceLocator().getCrmServiceSoap(new URL(CrmServiceUrl));

adminBinding.setHeader("http://schemas.microsoft.com/crm/2007/WebServices","CrmAuthenticationToken", token);

adminBinding.setUsername(username);
adminBinding.setPassword(password);

WhoAmIRequest whoRequest = new WhoAmIRequest();
        WhoAmIResponse whoResp = (WhoAmIResponse) adminBinding.execute(whoRequest);
String UserId = whoResp.getUserId();

System.out.println("UserId="+UserId);



--------
Edited: Mar 20, 2013

In the above code, we have used the MSLogonManager class(its the bridge to dll file which is created using .net sdk) to get the ticket.

 
Another way to get the ticket is:

#2. we can  create a proxy service that handles auth and the connection and get the ticket using the CRM services in .NET and then you define your web service SOAP interface you want Java to use.  

#3. Use the following xml request.. this is get by http tracker


private String getPassportTicket(String policy, String userName, String password) {
String ticket = null;
        try{
            URL url = new URL( "https://login.live.com/RST2.srf" );
            HttpURLConnection rc = (HttpURLConnection)url.openConnection();

            rc.setRequestMethod("POST");
            rc.setDoOutput( true );
            rc.setDoInput( true );
            rc.setRequestProperty( "Content-Type", "text/xml; charset=utf-8" );

            String reqStr = new String("                                        "xmlns:wst=\"http://schemas.xmlsoap.org/ws/2005/02/trust\"> "+
                                        " "+
                                        "http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue"+
                                        "HTTPS://login.live.com:443//RST2.srf"+
                                        ""+
                                        ""+userName+""+
                                        ""+password+""+
                                        "
"+
                                        "
"+
                                        "
"+
                                        ""+
                                        ""+
                                        "http://schemas.xmlsoap.org/ws/2005/02/trust/Issue"+
                                        ""+

                                        ""+
                                        "passportDomain"+
                                        "
"+

                                        "
"+

                                        ""+
                                        "
"+
                                        "
"+
                                        "
");
            int len = reqStr.length();
            rc.setRequestProperty( "Content-Length", Integer.toString( len ) );

           
             rc.connect();   
            
             OutputStreamWriter out = new OutputStreamWriter( rc.getOutputStream() );
             out.write( reqStr, 0, len );
             out.flush();

            InputStreamReader read = new InputStreamReader( rc.getInputStream() );
            

            StringBuffer  sb = new StringBuffer();  
            int ch = read.read();
            while( ch != -1 ){
              sb.append((char)ch);
              ch = read.read();
             }
            String response = sb.toString();
            read.close();
            rc.disconnect();
           
           
            ticket = response.substring(response.indexOf("")+ "".length(),
                    response.indexOf("
"));
            System.out.println("passportTicket = "+result);

 
 return ticket;
}

 

Monday, December 27, 2010

How to change Tomcat default character encoding

We faced an issue last week on linux machine,

In a tomcat deployed server, The non-english characters getting from web services are not displaying correctly.

We write a main program in java and retrieved the data through web-service. its parsed well and correctly displayed the non-english character.

So the issue is with tomcat, after investigation we found this.


In the catalina.bat (catalina.sh) add the java args...

set JAVA_OPTS=-Djavax.servlet.request.encoding=UTF-8 -Dfile.encoding=UTF-8

then all encodings seem to come across as UTF-8 by default.. 
This resolved the problem.

source: http://marc.info/?l=tomcat-user&m=108576957301771&w=3

----------------------
+20110113
Added some more info:

Configuring Tomcat's URI encoding

By default, Tomcat uses ISO-8859-1 character encoding when decoding URLs received from a browser. This can cause problems when Confluence's encoding is UTF-8, and you are using international characters in attachment or page names.
  1. Edit conf/server.xml and find the line where the Coyote HTTP Connector is defined. It will look something like this, possibly with more parameters:
    <Connector port="8080"/>
  2. Add a URIEncoding="UTF-8" property to the connector:
    <Connector port="8080" URIEncoding="UTF-8"/>
  3. Restart Tomcat
-------------------
Also check this

Tuesday, June 29, 2010

Connecting MS CRM 4.0 using Java webservices

Continue from connecting with MS CRM 3.0

The following is the sample code to connect MSCRM 4.0

String password = "password";

String username = "user";
String OrganizationName = "orgname";  //Whats Org Name

String endpointURL = "http://server:port/MSCrmServices/2007/CrmService.asmx"

CrmAuthenticationToken token = new CrmAuthenticationToken();
token.setCallerId("00000000-0000-0000-0000-000000000000");
token.setOrganizationName(OrganizationName);
//token.setAuthenticationType(0);

CrmServiceSoapStub bindingStub = (CrmServiceSoapStub) new CrmServiceLocator().getCrmServiceSoap(new URL(endpointURL));
bindingStub.setHeader("http://schemas.microsoft.com/crm/2007/WebServices",
"CrmAuthenticationToken", token);

bindingStub.setUsername(username);
bindingStub.setPassword(password);

WhoAmIRequest whoRequest = new WhoAmIRequest();
WhoAmIResponse whoResp = (WhoAmIResponse)bindingStub.execute(whoRequest);
String userid = whoResp.getUserId();
System.out.println("userid = "+userid );

I will provide the sample code for connecting MS CRM Services for all types of deployment

Monday, November 30, 2009

java.lang.OutOfMemoryError: PermGen space on java Tomcat server, linux


I got this error last day when try to open an jsp page,

server is Tomcat5.5 and os is linux

I found this link http://www.mkyong.com/tomcat/tomcat-javalangoutofmemoryerror-permgen-space/ it was very helpful to fix the issue

1. I just added the following code to my catalina.sh file

JAVA_OPTS="$JAVA_OPTS -XX:MaxPermSize=256m"


2.
You can create jsp page with the following code to evalute your memory details


# <%@ page import="java.lang.management.*, java.util.*" %><%

# response.setContentType("text/html");

# Iterator iter = ManagementFactory.getMemoryPoolMXBeans().iterator();

# while(iter.hasNext()){

# MemoryPoolMXBean item = (MemoryPoolMXBean) iter.next();

# MemoryUsage mu = item.getUsage();

# long used = mu.getUsed();

# long committed = mu.getCommitted();

# long max = mu.getMax();

# %>

# MEMORY TYPE: <%=item.getName()%>

# Used: <%=used%>

# Committed: <%= committed%>

# Max: <%=max%>

# <%}%>



The output look like

localhost
MEMORY TYPE: Code Cache
Used: 4 mb
Committed: 4 mb
Max: 48 mb
------------------------------------
MEMORY TYPE: PS Eden Space
Used: 25 mb
Committed: 33 mb
Max: 54 mb
------------------------------------
MEMORY TYPE: PS Survivor Space
Used: 0 mb
Committed: 1 mb
Max: 1 mb
------------------------------------
MEMORY TYPE: PS Old Gen
Used: 10 mb
Committed: 227 mb
Max: 455 mb
------------------------------------
MEMORY TYPE: PS Perm Gen
Used: 14 mb
Committed: 16 mb
Max: 64 mb
------------------------------------


After add this code JAVA_OPTS="$JAVA_OPTS -XX:MaxPermSize=256m"
I got PS Perm Gen size as

MEMORY TYPE: PS Perm Gen
Used: 14 mb
Committed: 16 mb
Max: 256 mb