2013-03-09 217 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