2013-03-09 254 views
0

我使用mule中的cxf服务启动了wsdl。我将客户端IP地址作为变量传递给xml mule config中的java类。 我实现了一个可调用的类来获取IP地址并在数据库中插入IP。 为了测试,我创建了一个wsdl骡子的web服务。 在这个过程的持续时间,正好当我创建类的wsdl,客户端IP插入数据库。 不幸的是,当我使用Web服务和我发送请求时,没有在数据库中插入IP。 我的目标:我想在每次请求后将客户端IP插入数据库。获取mule中的每个请求的客户端IP地址

我使用Mule 3.3.0 CE。和我的代码波纹管:

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" 
xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" 
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" 
xmlns:mule-ss="http://www.mulesoft.org/schema/mule/spring-security" 
xmlns:ss="http://www.springframework.org/schema/security" 
xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.3.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation=" 
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd 
http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd 
http://www.mulesoft.org/schema/mule/spring-security http://www.mulesoft.org/schema/mule/spring-security/3.3/mule-spring-security.xsd 
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd 
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd 
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd "> 

<global-property name="allowed" value="192.168.3.76,192.168.3.74,192.168.3.75" /> 

<configuration> 
    <expression-language> 
     <global-functions> 
      def parseIp(fullIp) { 
      return fullIp.substring(fullIp.indexOf('/') + 1, fullIp.indexOf(':')) 
      } 
     </global-functions> 
    </expression-language> 
</configuration> 

<http:connector name="httpConnector" doc:name="HTTP\HTTPS"> 
    <service-overrides sessionHandler="org.mule.session.NullSessionHandler" /> 
</http:connector> 

<mule-ss:security-manager> 
    <mule-ss:delegate-security-provider 
     name="memory-dao" delegate-ref="authenticationManager" /> 
</mule-ss:security-manager> 
<spring:beans> 
    <ss:authentication-manager 
     xmlns:ss="http://www.springframework.org/schema/security" alias="authenticationManager"> 
     <ss:authentication-provider> 
      <ss:user-service id="userService"> 
       <ss:user name="weather" password="weather" authorities="ROLE_ADMIN" /> 
      </ss:user-service> 
     </ss:authentication-provider> 
    </ss:authentication-manager> 
</spring:beans> 

<flow name="Weather" doc:name="Weather"> 
    <http:inbound-endpoint host="localhost" port="9091" 
     path="/web-service/weather" exchange-pattern="request-response" doc:name="HTTP"> 
    <mule-ss:http-security-filter realm="mule-realm" /> 
    </http:inbound-endpoint> 
    <expression-filter 
     expression="#['${allowed}'.contains(parseIp(message.inboundProperties['MULE_REMOTE_CLIENT_ADDRESS']))]" 
     doc:name="Expression" /> 

    <set-variable variableName="remoteClientAddress" 
     value="#[parseIp(message.inboundProperties['MULE_REMOTE_CLIENT_ADDRESS'])]" /> 
    <message-properties-transformer doc:name="myproperty" 
     scope="session"> 
     <add-message-property key="message.payload.remoteClientAddress" 
      value="#[parseIp(message.inboundProperties['MULE_REMOTE_CLIENT_ADDRESS'])]" /> 
    </message-properties-transformer> 

    <component doc:name="classTest" class="org.mule.example.scripting.IpClient" />  

    <cxf:proxy-service 
     service="Weather" 
     wsdlLocation="http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl" 
     namespace="http://ws.cdyne.com/WeatherWS/" 
     validationEnabled="true" doc:name="SOAP"> 
    </cxf:proxy-service> 

    <copy-properties propertyName="SOAPAction" doc:name="Property" /> 

    <cxf:proxy-client doc:name="SOAP" /> 

    <outbound-endpoint 
     address="http://wsf.cdyne.com/WeatherWS/Weather.asmx" 
     exchange-pattern="request-response" doc:name="Generic"> 
    </outbound-endpoint> 

</flow> 

和我的java类:

public class IpClient implements Callable { 

    @Override 
    public Object onCall(MuleEventContext eventContext) throws Exception { 

    MuleMessage msg = eventContext.getMessage(); 

String remClient = msg.getProperty("remoteClientAddress", PropertyScope.INVOCATION); 

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
Date date = new Date(); 

ConnectDB c = new ConnectDB("localhost:3306", "report", "root", "123456"); 

String sql = " INSERT INTO oml_news (ip, date) VALUES (?,?) "; 

PreparedStatement ps = c.getConnnection().prepareStatement(sql); 

ps.setString(1, remClient); 

ps.setString(2, dateFormat.format(date).toString()); 

    ps.executeUpdate(); 

    c.close(); 

    return true; 

    } 
+0

问:您的Java函数是否被调用?问:如果没有,为什么不呢?问:你有什么调试/记录工具?你尝试过哪些调试步骤? – paulsm4 2013-03-09 07:37:37

+0

是@ paulsm4,我的Java函数被调用。我只是用骡子工作室。我再次使用soapui pro进行测试。你能解释更多吗? – 2013-03-09 07:51:43

+0

您是否调试过在Java组件中查看remClient的值? – Seba 2013-03-09 17:16:12

回答

2

你的问题肯定是由于你没有发送SOAP请求骡子服务。

检查SOAPui中的端点URL。

+0

谢谢你的回答。 – 2013-03-14 23:40:45

+1

我在SoapUI中的链接是[link](http://wsf.cdyne.com/WeatherWS/Weather.asmx),而不是mule url(http:// localhost:9091/web-service/weather)。 – 2013-03-14 23:50:35