aspose file tools*
The moose likes Web Services and the fly likes Invoke .NET Web Service using Java/Axis passing SoapHeader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Invoke .NET Web Service using Java/Axis passing SoapHeader" Watch "Invoke .NET Web Service using Java/Axis passing SoapHeader" New topic
Author

Invoke .NET Web Service using Java/Axis passing SoapHeader

Vivek Jain
Ranch Hand

Joined: Oct 17, 2007
Posts: 34
Hi,
I'm trying to invoke ASP.NET web service using Java 1.6 & Axis 1.4
I'm passing user name & password through soap header but somehow the request genearted is as shown below:

And I need the request format as shown below:

Java code is given below:

It is giving authentication failed error response.

Can any one help me how to generate this request exactly as shown above?

Thanks!
R Srini
Ranch Hand

Joined: Feb 19, 2010
Posts: 215
Hi. Were you able to get this to work? To make your request look like the one you want:

1. Add the namespace to the envelope using ws.addNamespaceDeclaration(): http://java.sun.com/javase/6/docs/api/javax/xml/soap/SOAPElement.html#addNamespaceDeclaration(java.lang.String, java.lang.String)
2. If you do (1) above, there is no need to add "xmlns:pqr="http://abc.com" prefix to the accessInfo header
3. Try adding prefix pqr to serialRequest

I don't know why the mustUnderstand attribute is being added, but you could also try soapHeaderElement.setMustUnderstand(true).
Vivek Jain
Ranch Hand

Joined: Oct 17, 2007
Posts: 34
Hi,
Thanks for the reply. However, I do not see any such method name as addNamespaceDeclaration for ws.

Can you please tell me how to set namespace exactly?

Thanks!
R Srini
Ranch Hand

Joined: Feb 19, 2010
Posts: 215
Hi. The addNamespaceDeclaration is available in every SOAPElement. And SOAPEnvelope is a SOAPElement: http://java.sun.com/javase/6/docs/api/javax/xml/soap/SOAPEnvelope.html

Can you get the SOAPEnvelope for the request? Maybe ws.getEnvelope() or something like that? If you can get the envelope, then you should be able to say ((SOAPElement)envelope).addNamespaceDeclaration(...).
Vivek Jain
Ranch Hand

Joined: Oct 17, 2007
Posts: 34
Hi Srini,
Thanks again for the prompt reply. But I still have no luck. I'm still trying to create envelope object. But ws has only ferw properties such as _createCall(), _getCall(), _getProperty(String name), _getPropertyNames(), _getService(), _setProperty(String arg0, Object arg1), etc

My main method starts with following lines:

Is there any other way to create envelope?

Thanks!
R Srini
Ranch Hand

Joined: Feb 19, 2010
Posts: 215
Hi Vivek. AFAIK, the header processing is usually done in a handler (instead of directly in a client). I am not familiar with the locator stuff, so I don't have an answer for you right away. Please let me know the following:

1. What is the type of your ws object, and what interfaces does it implement?
2. Axis version
3. Steps used to generate the client. Did you use WSDL2Java to generate the client or did you write the client yourself? Its better to generate it.
4. Your target server (tomcat, GlassFish, etc.)

With the above information, I can try it myself later today (if you like) after work. I have been using Axis2 1.5.1 and tomcat/GlassFish for my tests and have not come across a locator.
Vivek Jain
Ranch Hand

Joined: Oct 17, 2007
Posts: 34
Hi Srini,
Here are the answers to your question:

1. What is the type of your ws object, and what interfaces does it implement?
ws is of type SubscriptionSoapStub which is generated using java org.apache.axis.wsdl.WSDL2Java wsdlURL where wsdlURL is the complete WSDL URL.

2. Axis version - v1.4

3. Steps used to generate the client. Did you use WSDL2Java to generate the client or did you write the client yourself? Its better to generate it.
I generated the client by running following command line:
java org.apache.axis.wsdl.WSDL2Java wsdlURL

4. Your target server (tomcat, GlassFish, etc.)
I'm writing this client program ro run it as stand alone as I'll be running it through task scheduler.

Please note that the server side web service is expecting the user & password under accessInfo in SoapHeader only. I would appreciate if you can provide me the working sample as I've been struggling with this!

Thanks!
R Srini
Ranch Hand

Joined: Feb 19, 2010
Posts: 215
Is this the old or new Axis (Axis2)? I think both have version 1.4. I am familiar with only the newer Axis2.

Also, this might be helpful.
Vivek Jain
Ranch Hand

Joined: Oct 17, 2007
Posts: 34
This is old axis 1.4. I'm not using axis2.

But I'm ok if you can provide any working sample using axis2 as well.

Thanks!
R Srini
Ranch Hand

Joined: Feb 19, 2010
Posts: 215
Ok. So you are able to use Axis2 if needed?
Vivek Jain
Ranch Hand

Joined: Oct 17, 2007
Posts: 34
Hi Srini,
I already looked at the link which you've provided & thats what I'm doing too. But my server side code expects the header in exactly same format. I need the request exactly in following format:


With the code given in the link which you've provided, it adds some extra attributes to accessInfo which I'm not sure how to remove. Also, the soap envelop has some different attributes. I need the request in exactly above format.

Thanks!
R Srini
Ranch Hand

Joined: Feb 19, 2010
Posts: 215
Ok. But are you ok if I try this in Axis2 1.5.1?
Vivek Jain
Ranch Hand

Joined: Oct 17, 2007
Posts: 34
Hi Srini,
I'll be more than happy if it is implemented in higher version. Only concern is that I need the request in the exact same format.

Thanks!
R Srini
Ranch Hand

Joined: Feb 19, 2010
Posts: 215
Hi Vivek. First of all, I don't think your request needs to be exactly as you say it should be. That doesn't sound right. I can only suggest that you use NetBeans/GlassFish to generate the client code, and start with a tutorial. This is not trivial code. Beyond a certain point, it is very difficult to help. Here are some links to get you started:

- http://netbeans.dzone.com/news/5-techniques-create-web-servic
- http://qa.netbeans.org/modules/j2ee/promo-g/end2end/numbers.html

I suggest that you download and use the above (if it is new to you, then again, there will be a learning curve), point to your wsdl, let it generate the request and use it.

Anyway, I am sorry to say that I am unable to find a clean solution to this. I don't know how to do it in old Axis 1.4 ... However, I used Java 1.6 (but 1.5 should also work) and Axis2 1.5.1. Here is what I did:
open command prompt
mkdir test (test directory that we can delete once we are done)
cd test
set axis2_home=C:\axis2-1.5.1\
set path=%axis2_home%\bin;%path%
mkdir gen (to hold the generated code)
- Look at the wsdl2java options. You may find some of them useful
wsdl2java -o gen -p mypkg -uri http://localhost:8080/HelloWs/GreeterWs?wsdl
wsdl2java -o gen -p mypkg -uri <WSDL URL>
My web service is called GreeterWs. So this generates two classes: GreeterWsStub.java and GreeterWsCallbackHandler.java
create a new Java project in Eclipse
copy over the generated code under gen/src to the source directory in Eclipse project
add the following jars to the build path: axiom-api*.jar, axiom-impl*.jar, axis2-adb*.jar, axis2-kernel*.jar, wsdl4j*.jar, XmlSchema*.jar, neethi*.jar, axis2-transport-local*.jar, axis2-transport-http*.jar, commons-httpclient*.jar, mail*.jar, httpcore*.jar, commons-codec*.jar
actually just add all the jars in the Axis2 lib directory
Search the generated Stub code for "toEnvelope" to see how the envelope is created
To add a namespace to the envelope, please look at this:
Search for "_messageContext.setEnvelope(env);" in the stub code, and then add these lines before the envelope is set:
To add custom headers, see this.
Write a simple test client. Here is the one that worked for me, so your code will look something like this:
Vivek Jain
Ranch Hand

Joined: Oct 17, 2007
Posts: 34
Hi Srini,
Thanks again for all your help! I tried with axis 2 & now, there is only last problem remaining. I didn't have to do anything to add the header. It looks like the axis2 sent the header info properly without adding any code. But the issue is that it is adding extra attribute called mustunderstand in header as shown below:


And I do not need this attribute at all. I need it as shown below:


This is the last remaining issue. Can you please help me in removing mustunderstand attribute in header?

Thanks!

R Srini
Ranch Hand

Joined: Feb 19, 2010
Posts: 215
Hi Vivek. So are you saying that your currently generated SOAP request works if you remove the mustUnderstand attribute and with no other change? I just want to confirm.
Vivek Jain
Ranch Hand

Joined: Oct 17, 2007
Posts: 34
yes
R Srini
Ranch Hand

Joined: Feb 19, 2010
Posts: 215
Hi Vivek, please see if this helps. If not I will try it a bit later. Please do let us know if it works though I will probably try it anyway to find a more elegant solution. BTW, when I tried it, it did not generate the mustUnderstand attribute. No idea why.
Vivek Jain
Ranch Hand

Joined: Oct 17, 2007
Posts: 34
Hi All & Srini,
My issue is resolved finally. The problem is in org.apache.axis2.client.Stub class. This class forcefully adds mustunderstand attribute even if it is set to 0. In my case, I do not need this attribute at all, so the solution is to override the addheader method & conditionally add the mustunderstand attribute.
Looks like this is a bug in axis stub class. Thanks for all your help, Srini!

Thanks!
R Srini
Ranch Hand

Joined: Feb 19, 2010
Posts: 215
Excellent. Good to hear that, Vivek!

Now as far as adding the mustUnderstand attribute, and you may already know this ... a web service (server) that is compliant with WSDL 1.1 or later (WSDL 2.0) - not sure of the exact versions - must be able to understand/interpret this attribute if its included, and not throw an error. This does not seem to be the case with the web service you are calling. I was reading here where the author suggested that maybe a corresponding SOAP module is not included/activated on the server side. So you may want to look into that to see if this is applicable.

Best wishes!
 
 
subject: Invoke .NET Web Service using Java/Axis passing SoapHeader