本文介绍了如何检测Axis2中使用了哪种transportReceiver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用tomcat7和axis2部署Java Web服务.

I am using tomcat7 and axis2 to deploy java web services.

在server.xml中,我配置了三个连接器(http,https和服务器端证书以及客户端证书,仅具有服务器端证书的https):

In server.xml I configured three connectors (http, https wich server and client side certificates, https with server side certificate only):

<Connector port="8181" protocol="HTTP/1.1" />
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" scheme="https" clientAuth="true"  keystoreFile=... truststoreFile=... />
<Connector port="8444" protocol="HTTP/1.1" SSLEnabled="true" scheme="https" clientAuth="false" keystoreFile=...  />

在axis2.xml中,我配置了三个transportReceivers:

In axis2.xml I configured three transportReceivers:

<transportReceiver name="http" class="org.apache.axis2.transport.http.AxisServletListener">
    <parameter name="port">8181</parameter>
    <parameter name="c1">none</parameter>
</transportReceiver>

<transportReceiver name="https" class="org.apache.axis2.transport.http.AxisServletListener">
    <parameter name="port">8443</parameter>
    <parameter name="c2">serverAndClient</parameter>
</transportReceiver>

<transportReceiver name="https" class="org.apache.axis2.transport.http.AxisServletListener">
    <parameter name="port">8444</parameter>
    <parameter name="c3">serverOnly</parameter>
</transportReceiver>

那很好!我所有的Web服务都可以按预期在所有端口上访问.

That works fine ! All my web services are accessible on all ports as expected.

不幸的是,我只想在8443上允许不使用密码服务登录"(因为客户端将获得证书),而在8444上允许使用密码登录".

Unfortunately I would like to allow "login without password service" on 8443 only (because client will have certificate), and "login with password" on 8444.

在Java中,我尝试过:

In Java I tried:

MessageContext msg = MessageContext.getCurrentMessageContext();
TransportInDescription tin = msg.getTransportIn();
String str = tin.getParameters().toString();

但我总是得到:

[Parameter : port=8444, Parameter : c3=serverOnly]

似乎一个传输接收者总是处理我的消息.有没有办法检测哪个传输接收器(或端口)真正用于当前调用?

It seems that one transport receiver always handles my messages. Is there a way to detect which transport receiver (or port) is really used for current invocation ?

BR,雅克(Jacek)

BR,Jacek

推荐答案

最后我找到了解决方法.

Finally I found a solution.

我错了,只有一个传输接收器可以处理所有消息.有两个(一个用于http,一个用于https).不幸的是,您无法定义第二个https传输,因为axis2在哈希图中保留了所有传输描述符,并且name是键,因此"https"始终只有一个条目,您将获得8181或8444.

I was wrong that only one transport receiver handles all messages. There are two (one for http and one for https). Unfortunately you can not define second https transport because axis2 holds all transport descriptors in hash map and name is a key so there will be always only one entry for "https" and you will get 8181 or 8444.

为解决此问题,您可以创建自己的servlet,并在消息到达时将端口号存储在MessageContext中.那很简单:

To solve this you can create your own servlet and store port number in MessageContext when message arrives. That is very easy:

public class MyServlet extends AxisServlet
{   
    protected MessageContext createMessageContext( HttpServletRequest request, HttpServletResponse response, boolean invocationType ) throws IOException
    {
        MessageContext mc = super.createMessageContext( request, response, invocationType );
        URL url = new URL( request.getRequestURL().toString() );
        mc.setProperty( "myPort", url.getPort() );
        return mc;      
    }
}

当然,您必须将您的类名称放在axis2/.../web.xml中,然后重新启动tomcat.然后,您可以检查任何axis2调用内的端口号:

Of course you must put your class name in axis2/.../web.xml and restart tomcat.Then you can check port number inside any axis2 invocation:

MessageContext mc = MessageContext.getCurrentMessageContext();
int port = ( Integer ) mc.getProperty( "myPort" );

现在我真的得到了8181、8443或8444:).

Now I really get 8181, 8443 or 8444 :).

BR,Jacek

这篇关于如何检测Axis2中使用了哪种transportReceiver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 22:51