+ Start a Discussion
EclipseTalkEclipseTalk 

Sample client implementation for Axis 2

Hello,

i'm surprised to see that there is no sample client implementation based on AXis2. Has someone already  ported the login sample to axis2 ? Thank you for sharing

 

Best Answer chosen by Admin (Salesforce Developers) 
ClimbatizeClimbatize
Here is my method with enterprise.wsdl and axis 2 under windows. I use wsdl2java to generate classes.
Add Java and Axis home to your environment path, for example:


JAVA_HOME = C:\Program Files\Java\jdk1.6.0_07
AXIS2_HOME = Q:\SalesForce\WSDL2JAVA_Generation\axis2-1.4.1-bin\axis2-1.4.1


then, create a directory to place your datas. In this directory I make a subdirectory to put my WSDL. Then you can launch this command:


%AXIS2_HOME%/bin/wsdl2java -t -uri wsdl/enterprise.wsdl -l java -d xmlbeans -p com.salesforce.webservice -sn SforceService -o generated/Salesforce


where:
- "wsdl/enterprise.wsdl" is the relative path to my wsdl
- "com.salesforce.webservice" is the package you want to assign to you generated classes
- "generated/Salesforce" is the relative subdirectory of my generated classes

In my example, a Salesforce subdirectory is created in the "generated" directory. This subdirectory contains:
- [src] that contains your generated classes
- [resources] that contains ressources to connect to salesforce
- [test] that contains test classes, but not important in our case

zip the content of resources (not the directory itself) into a resources.jar file.

Compile the classes contained in src in a directory, and zip the content of this directory in a file named salesforce.jar (for example).

Then, you have to integrate resources.jar, salesforce.jar and Axis2 librairies to your classpath. For that, everyone has his method, for my part I use Eclipse (to generated salesforce.jar too).

And there is my implementation:

This is the constructor of my Salesforece Session object:

public static final String PROD_SERVER = "https://www.salesforce.com/services/Soap/c/16.0";
public static final String SANDBOX_SERVER = "https://test.salesforce.com/services/Soap/c/16.0";

public Exception exception;

private Login login;

private SessionHeader sessionHeader;

private SessionHeaderDocument sessionHeaderDocument;

private String endpoint;

private String UserName, PassWord;

private int Progress;

private String ProgressText;

private String server;

private SforceServiceStub stub;


/**
* This constructor initializes a connexion with Saleforce using user name
* and password.
*
* @param UN
* Salesforce User Name
* @param PW
* Salesforce User Password
* @throws Exception
*/
public Salesforce_session(final String UN, final char[] PW) throws Exception
{

UserName = UN;
PassWord = String.valueOf(PW);


try
{

setProgress(0, "Loging");
if (UN.endsWith(".sand"))
{
endpoint = SANDBOX_SERVER;
} else
{
endpoint = PROD_SERVER;
}

stub = new SforceServiceStub(endpoint);

LoginDocument loginDocument = LoginDocument.Factory.newInstance();

login = Login.Factory.newInstance();

login.setUsername(UserName);

login.setPassword(PassWord);

loginDocument.setLogin(login);

LoginResponseDocument loginResponseDocument = stub.login(loginDocument, null);

LoginResponse loginResponse = loginResponseDocument.getLoginResponse();

LoginResult loginResult = loginResponse.getResult();

sessionHeader = SessionHeader.Factory.newInstance();

sessionHeader.setSessionId(loginResult.getSessionId());

sessionHeaderDocument = SessionHeaderDocument.Factory.newInstance();

sessionHeaderDocument.setSessionHeader(sessionHeader);

setProgress(10, "Loading service");

fireMyEvent(new MyEvent("Loading Service..."));
stub = new SforceServiceStub(loginResult.getServerUrl());

Options options = stub._getServiceClient().getOptions();
fireMyEvent(new MyEvent("Setting Gzip Compression IN..."));
options.setProperty(HTTPConstants.MC_ACCEPT_GZIP, Boolean.TRUE);

options.setProperty(HTTPConstants.MC_GZIP_REQUEST, Boolean.TRUE);

fireMyEvent(new MyEvent("Connexion established"));

} catch (AxisFault e)
{
exception = e;

} catch (RemoteException e)
{
exception = e;
} catch (InvalidIdFault e)
{
exception = e;
} catch (UnexpectedErrorFault e)
{
exception = e;
} catch (diva.salesforce.webservice.LoginFault e)
{
exception = e;
} catch (ExceptionInInitializerError e)
{
exception = (Exception) e.getException();
}



}


and my query method


/**
* Use this method to send SOQL queries to salesforce.
*
* @param querystr
* the SOQL query to send to salesforce.
* @return Query Result
* @throws RemoteException
* @throws InvalidSObjectFault
* @throws MalformedQueryFault
* @throws InvalidIdFault
* @throws InvalidFieldFault
* @throws UnexpectedErrorFault
* @throws InvalidQueryLocatorFault
*/
public QueryResult Query(final String querystr) throws RemoteException, InvalidSObjectFault, MalformedQueryFault,
InvalidIdFault, InvalidFieldFault, UnexpectedErrorFault, InvalidQueryLocatorFault
{

Query query;
QueryDocument queryDocument;
QueryResponseDocument queryResponseDocument;
QueryResponse queryResponse;
QueryResult queryResult = null;

query = Query.Factory.newInstance();

query.setQueryString(querystr);

queryDocument = QueryDocument.Factory.newInstance();

queryDocument.setQuery(query);

queryResponseDocument = stub.query(queryDocument, sessionHeaderDocument, null, null, null);

queryResponse = queryResponseDocument.getQueryResponse();

queryResult = queryResponse.getResult();

return queryResult;

}



same for insert:


/**
* Use this method to insert several {@link SObject} in salesforce.
*
* @param sObjectsArray
* @return a {@link SaveResult} array for each inserted objects
* @throws RemoteException
* @throws InvalidSObjectFault
* @throws InvalidIdFault
* @throws InvalidFieldFault
* @throws UnexpectedErrorFault
*/
public SaveResult[] Insert(SObject[] sObjectsArray) throws RemoteException, InvalidSObjectFault, InvalidIdFault,
InvalidFieldFault, UnexpectedErrorFault
{

SaveResult[] saveResultArray = null;
// Invoke the create call

CreateDocument createDocument = CreateDocument.Factory.newInstance();
Create create = createDocument.addNewCreate();

create.setSObjectsArray(sObjectsArray);

CreateResponseDocument createResponseDocument = stub.create(createDocument, sessionHeaderDocument, null, null,
null, null, null, null);

CreateResponse createResponse = createResponseDocument.getCreateResponse();
saveResultArray = createResponse.getResultArray();

return saveResultArray;

}


update:


/**
* Use this method to update several {@link SObject} in salesforce.
*
* @param sObjectsArray
* @return
* @throws RemoteException
* @throws InvalidSObjectFault
* @throws InvalidIdFault
* @throws InvalidFieldFault
* @throws UnexpectedErrorFault
*/
public SaveResult[] Update(SObject[] sObjectsArray) throws RemoteException, InvalidSObjectFault, InvalidIdFault,
InvalidFieldFault, UnexpectedErrorFault
{

SaveResult[] updateResultArray = null;
// Invoke the update call

UpdateDocument updateDocument = UpdateDocument.Factory.newInstance();
Update update = updateDocument.addNewUpdate();

update.setSObjectsArray(sObjectsArray);

UpdateResponseDocument updateResponseDocument = stub.update(updateDocument, sessionHeaderDocument, null, null,
null, null, null, null);
UpdateResponse updateResponse = updateResponseDocument.getUpdateResponse();
updateResultArray = updateResponse.getResultArray();

return updateResultArray;

}


Delete:

/**
* This method take an array of object ids to delete ins salesforce. Deleted
* object can be retrieved in the recycle bin.
*
* @param sObjectsIdArray
* @return
* @throws RemoteException
* @throws UnexpectedErrorFault
*/
public DeleteResult[] Delete(String[] sObjectsIdArray) throws RemoteException, UnexpectedErrorFault
{
DeleteResult[] deleteResultArray = null;
// Invoke the delete call

DeleteDocument deleteDocument = DeleteDocument.Factory.newInstance();
com.sforce.soap.enterprise.DeleteDocument.Delete delete = deleteDocument.addNewDelete();

delete.setIdsArray(sObjectsIdArray);

DeleteResponseDocument deleteResponseDocument = stub.delete(deleteDocument, sessionHeaderDocument, null, null,
null, null, null);
DeleteResponse deleteResponse = deleteResponseDocument.getDeleteResponse();
deleteResultArray = deleteResponse.getResultArray();

return deleteResultArray;

}


As you can see, each method is quite the same. Hope it helped you.

All Answers

mpiercempierce

I'm not aware of any axis2 sample code out there. Axis 1 is really really old, and I couldn't figure out how to get Axis 2 to do what I needed, so I chose to use JAX-WS instead. That's worked out fine for me. I have a tutorial showing how to use JAX-WS: http://eng.genius.com/blog/2009/05/23/salesforcecom-partner-soap-api-jax-ws-tutorial-part-1/

 

If you're in a position to change your soap toolkit, give JAX-WS a try. 

EclipseTalkEclipseTalk

Thanks, yes I'm aware of this tutorial this is a great help.

However there must be somewhere a sample based on Axis 2. I can't believe the sample client hasn't been ported to Axis 2. Can anyone comment on that?

Thanks in advance

SuperfellSuperfell

I did a sample a while back, but the programming model for axis2 kept changing, so its unlikely that it still works

http://www.pocketsoap.com/weblog/2006/05/1623.html 

EclipseTalkEclipseTalk
Thank you, I'll have a look.
ClimbatizeClimbatize
Here is my method with enterprise.wsdl and axis 2 under windows. I use wsdl2java to generate classes.
Add Java and Axis home to your environment path, for example:


JAVA_HOME = C:\Program Files\Java\jdk1.6.0_07
AXIS2_HOME = Q:\SalesForce\WSDL2JAVA_Generation\axis2-1.4.1-bin\axis2-1.4.1


then, create a directory to place your datas. In this directory I make a subdirectory to put my WSDL. Then you can launch this command:


%AXIS2_HOME%/bin/wsdl2java -t -uri wsdl/enterprise.wsdl -l java -d xmlbeans -p com.salesforce.webservice -sn SforceService -o generated/Salesforce


where:
- "wsdl/enterprise.wsdl" is the relative path to my wsdl
- "com.salesforce.webservice" is the package you want to assign to you generated classes
- "generated/Salesforce" is the relative subdirectory of my generated classes

In my example, a Salesforce subdirectory is created in the "generated" directory. This subdirectory contains:
- [src] that contains your generated classes
- [resources] that contains ressources to connect to salesforce
- [test] that contains test classes, but not important in our case

zip the content of resources (not the directory itself) into a resources.jar file.

Compile the classes contained in src in a directory, and zip the content of this directory in a file named salesforce.jar (for example).

Then, you have to integrate resources.jar, salesforce.jar and Axis2 librairies to your classpath. For that, everyone has his method, for my part I use Eclipse (to generated salesforce.jar too).

And there is my implementation:

This is the constructor of my Salesforece Session object:

public static final String PROD_SERVER = "https://www.salesforce.com/services/Soap/c/16.0";
public static final String SANDBOX_SERVER = "https://test.salesforce.com/services/Soap/c/16.0";

public Exception exception;

private Login login;

private SessionHeader sessionHeader;

private SessionHeaderDocument sessionHeaderDocument;

private String endpoint;

private String UserName, PassWord;

private int Progress;

private String ProgressText;

private String server;

private SforceServiceStub stub;


/**
* This constructor initializes a connexion with Saleforce using user name
* and password.
*
* @param UN
* Salesforce User Name
* @param PW
* Salesforce User Password
* @throws Exception
*/
public Salesforce_session(final String UN, final char[] PW) throws Exception
{

UserName = UN;
PassWord = String.valueOf(PW);


try
{

setProgress(0, "Loging");
if (UN.endsWith(".sand"))
{
endpoint = SANDBOX_SERVER;
} else
{
endpoint = PROD_SERVER;
}

stub = new SforceServiceStub(endpoint);

LoginDocument loginDocument = LoginDocument.Factory.newInstance();

login = Login.Factory.newInstance();

login.setUsername(UserName);

login.setPassword(PassWord);

loginDocument.setLogin(login);

LoginResponseDocument loginResponseDocument = stub.login(loginDocument, null);

LoginResponse loginResponse = loginResponseDocument.getLoginResponse();

LoginResult loginResult = loginResponse.getResult();

sessionHeader = SessionHeader.Factory.newInstance();

sessionHeader.setSessionId(loginResult.getSessionId());

sessionHeaderDocument = SessionHeaderDocument.Factory.newInstance();

sessionHeaderDocument.setSessionHeader(sessionHeader);

setProgress(10, "Loading service");

fireMyEvent(new MyEvent("Loading Service..."));
stub = new SforceServiceStub(loginResult.getServerUrl());

Options options = stub._getServiceClient().getOptions();
fireMyEvent(new MyEvent("Setting Gzip Compression IN..."));
options.setProperty(HTTPConstants.MC_ACCEPT_GZIP, Boolean.TRUE);

options.setProperty(HTTPConstants.MC_GZIP_REQUEST, Boolean.TRUE);

fireMyEvent(new MyEvent("Connexion established"));

} catch (AxisFault e)
{
exception = e;

} catch (RemoteException e)
{
exception = e;
} catch (InvalidIdFault e)
{
exception = e;
} catch (UnexpectedErrorFault e)
{
exception = e;
} catch (diva.salesforce.webservice.LoginFault e)
{
exception = e;
} catch (ExceptionInInitializerError e)
{
exception = (Exception) e.getException();
}



}


and my query method


/**
* Use this method to send SOQL queries to salesforce.
*
* @param querystr
* the SOQL query to send to salesforce.
* @return Query Result
* @throws RemoteException
* @throws InvalidSObjectFault
* @throws MalformedQueryFault
* @throws InvalidIdFault
* @throws InvalidFieldFault
* @throws UnexpectedErrorFault
* @throws InvalidQueryLocatorFault
*/
public QueryResult Query(final String querystr) throws RemoteException, InvalidSObjectFault, MalformedQueryFault,
InvalidIdFault, InvalidFieldFault, UnexpectedErrorFault, InvalidQueryLocatorFault
{

Query query;
QueryDocument queryDocument;
QueryResponseDocument queryResponseDocument;
QueryResponse queryResponse;
QueryResult queryResult = null;

query = Query.Factory.newInstance();

query.setQueryString(querystr);

queryDocument = QueryDocument.Factory.newInstance();

queryDocument.setQuery(query);

queryResponseDocument = stub.query(queryDocument, sessionHeaderDocument, null, null, null);

queryResponse = queryResponseDocument.getQueryResponse();

queryResult = queryResponse.getResult();

return queryResult;

}



same for insert:


/**
* Use this method to insert several {@link SObject} in salesforce.
*
* @param sObjectsArray
* @return a {@link SaveResult} array for each inserted objects
* @throws RemoteException
* @throws InvalidSObjectFault
* @throws InvalidIdFault
* @throws InvalidFieldFault
* @throws UnexpectedErrorFault
*/
public SaveResult[] Insert(SObject[] sObjectsArray) throws RemoteException, InvalidSObjectFault, InvalidIdFault,
InvalidFieldFault, UnexpectedErrorFault
{

SaveResult[] saveResultArray = null;
// Invoke the create call

CreateDocument createDocument = CreateDocument.Factory.newInstance();
Create create = createDocument.addNewCreate();

create.setSObjectsArray(sObjectsArray);

CreateResponseDocument createResponseDocument = stub.create(createDocument, sessionHeaderDocument, null, null,
null, null, null, null);

CreateResponse createResponse = createResponseDocument.getCreateResponse();
saveResultArray = createResponse.getResultArray();

return saveResultArray;

}


update:


/**
* Use this method to update several {@link SObject} in salesforce.
*
* @param sObjectsArray
* @return
* @throws RemoteException
* @throws InvalidSObjectFault
* @throws InvalidIdFault
* @throws InvalidFieldFault
* @throws UnexpectedErrorFault
*/
public SaveResult[] Update(SObject[] sObjectsArray) throws RemoteException, InvalidSObjectFault, InvalidIdFault,
InvalidFieldFault, UnexpectedErrorFault
{

SaveResult[] updateResultArray = null;
// Invoke the update call

UpdateDocument updateDocument = UpdateDocument.Factory.newInstance();
Update update = updateDocument.addNewUpdate();

update.setSObjectsArray(sObjectsArray);

UpdateResponseDocument updateResponseDocument = stub.update(updateDocument, sessionHeaderDocument, null, null,
null, null, null, null);
UpdateResponse updateResponse = updateResponseDocument.getUpdateResponse();
updateResultArray = updateResponse.getResultArray();

return updateResultArray;

}


Delete:

/**
* This method take an array of object ids to delete ins salesforce. Deleted
* object can be retrieved in the recycle bin.
*
* @param sObjectsIdArray
* @return
* @throws RemoteException
* @throws UnexpectedErrorFault
*/
public DeleteResult[] Delete(String[] sObjectsIdArray) throws RemoteException, UnexpectedErrorFault
{
DeleteResult[] deleteResultArray = null;
// Invoke the delete call

DeleteDocument deleteDocument = DeleteDocument.Factory.newInstance();
com.sforce.soap.enterprise.DeleteDocument.Delete delete = deleteDocument.addNewDelete();

delete.setIdsArray(sObjectsIdArray);

DeleteResponseDocument deleteResponseDocument = stub.delete(deleteDocument, sessionHeaderDocument, null, null,
null, null, null);
DeleteResponse deleteResponse = deleteResponseDocument.getDeleteResponse();
deleteResultArray = deleteResponse.getResultArray();

return deleteResultArray;

}


As you can see, each method is quite the same. Hope it helped you.
This was selected as the best answer
airrick3airrick3

Thanks Climbatize, great post!!!  it was very helpful getting me started.

 

I think it's important to note that the default databinding in ecplise for Axis2 is ADB,  which produces very succinct code but does not provide interfaces for the Objects (Account, Contact, etc) the only object you get is SObject.

 

Which might be why you used the xmlbeams databinding???