困扰很久的ROR调用Axis2 web service失去初步解决

困扰很久的ROR调用Axis2 web service得到初步解决
问题:ROR如何调用Axis2的web service

目前遇到这个问题,在Google和百度搜了无数次,国内国外的都看过,几乎没有任何有价值的资料,唯一有所帮助的是一篇关于ROR调用.Net服务的文章(http://qiezi.iteye.com/blog/26642)。仿照该文章的方法,做如下尝试:
java写的service
package sample;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
public class HelloWorld {
       public OMElement SayHello(OMElement in){
              String name=in.getText();
              String info=name+"HelloWorld!";
              OMFactory fac=OMAbstractFactory.getOMFactory();
              OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");
              OMElement resp=fac.createOMElement("SayHelloResponse",omNs);
              resp.setText(info);
              return resp;
       }
}


在rails项目的app/apis目录下,建立test_api.rb:
class TestApi < ActionWebService::API::Base
  api_method :SayHello, :expects =>[:string], :returns=>[:string]
end

在controller下,建立test_controller.rb:
class TestController < ApplicationController
   web_client_api :test, :soap, "http://172.23.77.12:8080/axis2/services/HelloWorld",
                :namespace => "http://helloworld.com/",
                :soap_action_base => "http://172.23.77.12:8080/axis2/services/HelloWorld",
                :driver_options => {:default_encodingstyle => SOAP::EncodingStyle::SOAPHandler::Namespace }
  def hello
    render_text test.SayHello("You!")
  end
end


完成以上部分后,将HelloWorld.aar 部署到tomcat,启动WEBrick,访问Http://localhost:3000/test/hello
结果得到如下信息:
Operation Not found EPR is http://172.23.77.12:8080/axis2/services/HelloWorld and WSA Action =  http://172.23.77.12:8080/axis2/services/HelloWorld/SayHello

得到这条信息不能说是坏事,首先,这表明ror与axis2之间已经有了通信,但是服务端未找到ror希望调用的功能,怎么回事呢?问题很有可能出在soap协议上!因为调用什么方法显然是在soap协议里,另外,在看了“头脑歪歪”的博客中遇到的问题后,觉得抓包,看一看soap协议里到底藏着什么。
首先用可以正常调用哪个服务的java的客户端进行对比试验,其中java客户端代码如下:
package example.client;
//Guess what i will do?
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

public class TestClient {
       private static EndpointReference targetEPR=new EndpointReference
         ("http://172.23.77.12:8080/axis2/services/HelloWorld");
       public static OMElement getSayHelloOMElement(){
              OMFactory fac=OMAbstractFactory.getOMFactory();
              OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");
              OMElement method=fac.createOMElement("SayHello",omNs);
              method.setText("You!");
              return method;
       }
       public static void main(String[] args){
              try{
                     Options options=new Options();
                     options.setTo(targetEPR);
                     ServiceClient sender=new ServiceClient();
                     sender.setOptions(options);
                     OMElement sayHello=TestClient.getSayHelloOMElement();
                     OMElement result=sender.sendReceive(SayHello);
                     System.out.println(result);
              }
              catch(Exception axisFault){
                     axisFault.printStackTrace();
              }
       }
}

测试结果正确,抓取包中的soap如下:
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header />
<soapenv:Body>
<hw:sayHello xmlns:hw="http://helloworld.com/">You!</hw:sayHello>
</soapenv:Body>
</soapenv:Envelope>

测试ROR的service调用,结果仍然是“Operation Not found EPR ....”,包内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <env:Body>
    <n1:SayHello xmlns:n1="http://helloworld.com/"
        env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <param0 xsi:type="xsd:string">You!</param0>
    </n1:SayHello>
  </env:Body>
</env:Envelope>

显然!问题正如头脑歪歪在调用.Net服务时一样。命名空间的问题!而另外一点不同的问题是namespace "env",不过,该问题并不会
影响服务的调用,所以,根本问题发生在"n1"这个命名空间上。根据java service端的定义
 OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");

明明空间应为hw,正如头脑歪歪说的那样,.net不能理解n1,而axis2同样不知所措!
采用他的方法,去lib/ruby/1.8/soap/rpc中,把proxy.rb做更改:
    conn_data.send_contenttype = mime.headers['content-type'].str
 end
    conn_data.send_string.gsub!(/:n1/,':hw')
    conn_data.send_string.gsub!(/n1:/,'hw:')
    conn_data.send_string.gsub!(/<param0 xsi:type="xsd:string">/,'')
    conn_data.send_string.gsub!(/<\/param0>/,'')
    conn_data.send_string.gsub!(/\n/,'')

后5行为我添加的代码
注意,除了更改了n1命名空间外,还去掉了<param0>这个标签,主要是由于在实际情况中,参数的传递在以java客户端为例的包当中,是直接给出的:
<hw:sayHello xmlns:hw="http://helloworld.com/">You!</hw:sayHello>

而在ror调用时,ror用param0进行了一次包装,在包装完成后,axis2同样手足无措了,在采取in.getText()这个方法时,没能够得到参数"You!",无奈之下,我只好把param0标签去掉。
在做完这一切之后,重启WEBrick,然后再次访问http://localhost:3000/test/hello
久违的结果出现了:
You! HelloWorld!

啊!长出一口气,这么多天的郁闷终于没有了~
总结:
1.对于web service我并没有太深入的了解,以上的都是照葫芦画瓢瞎搞出来的,有什么弊端和改良的地方希望懂得朋友指点,尤其是命名空间那一部分,感觉该ruby的源码并不是好的主意~
2.ror在生成soap时,好像还残留着ror本身的一些特点,表现在方法名上,其实,我本来服务端的java方法不是SayHello,而是sayHello的,但是即使在test_api及test_controller中写明了是"sayHello",soap协议中仍然是SayHello!无奈之下修改了服务,终于满足了ROR。
3.抓包的问题,我用的是Ethereal 0.99,最重要的一点,这种抓包工具是抓不到回路包的,所以,在测试时你需要把你的axis2部署到另外一台机器上,虚机我没用,估计可以吧。172.23.77.12是我的另一台局域网主机的地址。
4.如果你的service的编译jre版本和tomcat的不一致,是不会部署成功的。我开始用jre1.6.0进行编译,另一台服务器上tomcat的jre是1.5.0,出错,重新用1.5.0编译后部署即可

好了,基本上就是这些,由于本人遇到这样一个问题而苦于无人给出较详细的解答,贴在这里,希望对大家有帮助,也希望各位指出文中的不足,大家一起交流进步!