3.02.2006

WSDL2Java & TCPMON Tutorial

Generating Java Proxies from the WSDL
Sometimes when working with web services integration, it's easier for Java developers to utilize Java proxies and use them to interact with the service provider because learning the Java API might be faster and easier than learning about the structure of SOAP Envelope through WSDL. Creating the proxies is very easy with WSDL2Java tool. Once you finish creating the proxies, you can start writing a simple client to interact with the service provider, and you can use the TCPMON tool to intercept the SOAP messages that are exchanged between the service provider and consumer.

To create the Java proxies, there's a WSDL2Java plugin for Eclipse endorsed by SalesForce.com (com.myspotter.wsdl2java_1.2.0.zip). Once you unzip the file into your eclipse/plugins directory, restart your Eclipse. To generate the stubs, all you have to do is to locate the WSDL file with Eclipse package explorer, right click, and choose "Generate". This will generate the Java proxies on your project root folder. You can move these proxies to your Java source folder.


Setting up TPCMON
To intercept the messages, you can use the TCPMON (tcpmon.jar). Double click on the JAR file, you will see the Swing console to appear on your screen. To set up this TCPMON will be a little bit tricky but not too difficult. You will see the following input fields:
  • Local Port: Use any unused local port number (such as 8181).
  • Server Name: Put the IP address of the services provider (the url/IP address of the service endpoint. For example, Salesforce's service endpoint is "na1-api.salesforce.com")
  • Server Port: Usually just use "80" as default service endpoint unless they specify something else.
Writing Simple Web Services Client
Having the proxies generated from WSDL to Java and the TCPMON set up, you are ready to write simple web services client in Java. The following is a simple tutorial of how to write Java web services client code using the generated proxies to connect to SalesForce CRM OnDemand. You need to have an active account (free to register for developer account at SalesForce website).

You can also download the wsdl2java_tcpmon.zip that contains:
  • com.myspotter.wsdl2java_1.2.0.zip (Eclipse plugin for WSDL2Java)
  • tcpmon.jar (TCPMON to intercept SOAP Message)
  • createPriceBook.jsp (Simple example to create PriceBook at SalesForce.com)
  • jaxrpc_client.ppt (Slides I used to use for my past lecture).
Let's start writing the codes

// don't forget to import the generated proxies
// Get the instantiate the ServiceLocator object
SforceService service = new SforceServiceLocator();


To intercept the SOAP message using the TCPMON, we need to modify the service endpoint by using method available from SoapBindingStub.

// get SoapBindingStub
SoapBindingStub binding = (SoapBindingStub) service.getSoap();


With the binding object, now we can change the service endpoint to point to our TCPMON. Previously, the stub is trying to send the SOAP message directly to SalesForce with the following URL:

https://na1-api.salesforce.com/services/Soap/c/6.0


Replace the service endpoint above with our TCPMON by using the following URL (assuming you pick port 8181 for the TCPMON local port):

http://localhost:8181/services/Soap/c/6.0


Now, using the local service endpoint, you can set it to the binding object.

//To set the local endpoint to the binding object
binding._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY,
"http://localhost:8181/services/Soap/c/6.0");


Now, before we start calling the web services API, we need to login first. To login:

// Login
LoginResult loginResult =
binding.login("username@company.com", "password");


We need to get the session ID from the loginResult object and set it into the SOAP Header.

// Instantiate a SessionHeader object
SessionHeader sh = new SessionHeader();

// set the session id into the SessionHeader object
sh.setSessionId(loginResult.getSessionId());

// set the SessionHeader object into the binding object
binding.setHeader(
new SforceServiceLocator().getServiceName().getNamespaceURI(),
"SessionHeader", sh);


Now, the rest will be easy. For example, to store a new record at SalesForce, we just need to use the SalesForce object (such as Pricebook2 or Product2).


// Instantiate Pricebook2 object and give a name.
Pricebook2 pb = new Pricebook2();
pb.setName("The Price Book - " + System.currentTimeMillis());

// call the Create API from the binding object
SaveResult[] saveResults = binding.create(new SObject[] { pb });


The steps above will let you create a new price book, and you can see how the SOAP Request and Response are being structured on the TCPMON tool.

7 comments:

Kristi said...

*scroll scroll* looking for something I can understand...

*processing* ... nothing is found.

hahahahaha how many blogs do you have? Hey you should add google ads on this one, and make this blog an information source for Java related.

Anonymous said...

hahaha, i was not expecting u to visit this blog. that's why i decided to seperate technical discussion out from my personal blog. thx for the google ad advise. i think i will do it later once i have more contents for this blog :)

tatat said...

hey Danny how about a big picture summary of the two technologies for the un-enlightened? We want to know what you're really up to here man :)

Anonymous said...

dude,
you made my day ,I tried zillions of plugins for eclipse to generate stubs but none of them work.

thanks for your post,
god bless you.
chintan.

Anonymous said...

hi danny,
while generating stubs using wsdl2java ,I am getting the errors in the generated classes "FaultCode.java" and "ExceptionCode.java " in the package com.sforce.soap.enterprise.fault.

I am just following your instructions ,so I assume you may have also encountered the same errors ,

could you please let me know how to resolve these errors and have the stubs compile succesfully ?

I will be checking your blog ,so feel free to put your comments on the blog.

you can also email me if you want ,my email address is

chintan_bhagda@yahoo.com.

thanks,
chintan desai.

Imby said...

Thanks for the article
TCPMon Tutorial

Presh said...

You should see Walmart Photo Christmas Cards