2015-01-28 2 views

답변

2

먼저 커넥터에 enableCookies="true"이 있는지 확인하십시오.

cookiesLorg.apache.commons.httpclient.Cookie 인 inboundProperty가 있습니다.

액세스하려면 #[message.inboundProperties['cookies'].

1

새로운 http : listener 구성 요소에서는 더 이상 작동하지 않습니다. 속성을 줄 것이다 설정 :

org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute 'enableCookies' is not allowed to appear in element 'http:listener'. 

그래서 새로운 http로이 작업을 수행하는 방법 : 수신기 구성 요소 ... 은 뮬 만 제공 내가 가진 문제는 내가 와서 쿠키를 액세스하는 데 필요한 것이되었다 형식이 지정되지 않은 문자열의 쿠키.

package transformers; 

import java.net.URI; 
import java.net.URISyntaxException; 
import java.util.Arrays; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

import org.mule.api.MuleMessage; 
import org.mule.api.transformer.TransformerException; 
import org.mule.api.transport.PropertyScope; 
import org.mule.transformer.AbstractMessageTransformer; 
import org.mule.transport.http.CookieHelper; 
import org.apache.commons.httpclient.Cookie; 

public class CookieGrabber extends AbstractMessageTransformer { 

    public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException { 
     Object    _CookieHeader = message.getInboundProperty("Cookie"); 
     List<Cookie>  _CookieList  = null; 
     Map<String,String> _CookieMap  = new HashMap<String,String>(); 

     try { 
      //Grab the cookies from the header and put them into a List 
      _CookieList = (List<Cookie>) Arrays.asList(CookieHelper.parseCookiesAsAServer(_CookieHeader.toString(), 
                          new URI("" + message.getInboundProperty("host")))); 
      //And put them in a convenient List which can be accessed from the flow 
      message.setProperty("incomingCookieList", _CookieList, PropertyScope.SESSION); 

      //Let's also put them in a nice Map, since incoming cookies will 
      //usually only contain a name and a value, so let's get easy access to them by their name. 
      for (Cookie _Cookie : _CookieList){ 
       _CookieMap.put(_Cookie.getName(), _Cookie.getValue()); 
      } 
      message.setProperty("incomingCookieMap", _CookieMap, PropertyScope.SESSION); 

     } catch (URISyntaxException e) { 
      e.printStackTrace(); 
     } 
     return message; 
    } 
} 

다음이를 사용하는 방법을 보여줍니다이 흐름 예제가있다 :

그래서이 내가 흐름 쿠키에 쉽게 액세스를 얻기 위해 약간 강화 된 내 친구가 개발 한 옵션입니다 코드 스 니펫. 일부 쿠키를 설정하고이를 쿠키를 읽을 "프록시"로 전달하는 청취자를 포함하지만 다른 끝점으로 요청을 전달하여 투명 프록시로 만들고 프로세스에서 쿠키를 읽는 수신기를 포함합니다. 이 도움이 http://localhost:8080/setCookies

희망 :

<?xml version="1.0" encoding="UTF-8"?> 
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" 
     xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 
     xmlns:http="http://www.mulesoft.org/schema/mule/http" 
     xmlns="http://www.mulesoft.org/schema/mule/core" 
     xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" 
     xmlns:spring="http://www.springframework.org/schema/beans" 
     version="EE-3.6.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd 
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd 
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd"> 
    <custom-transformer  class="transformers.CookieGrabber" 
          name="MyCookieTranformer" 
          doc:name="Java"/> 
    <http:listener-config name="HTTP_Configuration_CookieHandlerExample" 
          host="0.0.0.0" 
          port="8080" 
          doc:name="HTTP Listener Configuration"/> 
    <http:request-config name="HTTP_Request_Configuration" 
          host="localhost" 
          port="8080" 
          doc:name="HTTP Request Configuration"/> 
    <flow name="CookieSetterFlow"> 
     <http:listener  config-ref="HTTP_Configuration_CookieHandlerExample" 
          path="/setCookies/*" 
          doc:name="setCookies" 
          doc:description="Call this module by entering http://localhost:8080/setCookie"/> 
     <message-properties-transformer doc:name="Set the cookies" 
             doc:description="Set some random cookies in the header"> 
      <add-message-property  key="Cookie" 
             value="token=abcde; name=dennis"/> 
     </message-properties-transformer> 
     <http:request  config-ref="HTTP_Request_Configuration" 
          path="/proxyCookie" 
          method="GET" 
          doc:name="call proxyCookies" 
          doc:description="Invoke the cookieReceiver with the cookies we've just set. Note the failure status code validator with a non-existing http code. It's a nasty bug, but it works like this..."> 
      <http:failure-status-code-validator values="00000"/> 
     </http:request> 
    </flow> 
    <flow name="CookieProxyFlow"> 
     <http:listener  config-ref="HTTP_Configuration_CookieHandlerExample" 
          path="/proxyCookie" 
          doc:name="proxyCookies" 
          doc:description="This connector will proxy the cookieReceiver"/> 
     <transformer  ref="MyCookieTranformer" 
          doc:name="GrabCookies" 
          doc:description="Use our custom transformers.CookieGrabber class to put the cookies in a nice java.util.List in a session variable."/> 
     <logger    message="CookieProxy: Value of cookie &quot;token&quot;: &quot;#[sessionVars.incomingCookieMap.get('token')]&quot;." 
          level="INFO" 
          doc:name="Have a cookie!" 
          doc:description="Let get a cookie value, simply by referring the name of it as the key from our map"/> 
     <flow-ref   name="copy-and-clean-headers" 
          doc:name="copy-and-clean-headers" 
          doc:description="Cope the headers and clean the Mule stuff from the headers to forward it clean to the receiver."/> 
     <set-property  propertyName="host" 
          value="localhost" 
          doc:name="Set Host" 
          doc:description="Now not really necessary, but you'll probably want to set the hostname to the actual service endpoint."/> 
     <http:request  config-ref="HTTP_Request_Configuration" 
          path="/receiveCookie" 
          method="GET" 
          doc:name="forward to receiveCookies" 
          doc:description="Invoke the cookieReceiver."> 
      <http:failure-status-code-validator values="00000"/> 
     </http:request> 
     <flow-ref   name="copy-and-clean-headers" 
          doc:name="copy-and-clean-headers" 
          doc:description="Again copy the headers and clean the Mule http stuff."/> 
    </flow> 
    <sub-flow name="copy-and-clean-headers" > 
     <copy-properties propertyName="*" 
          doc:name="Copy All HTTP Headers"/> 
     <remove-property propertyName="Content-Length" 
          doc:name="Remove Content Length"/> 
     <remove-property propertyName="MULE_*" 
          doc:name="Remove MULE Properties"/> 
     <remove-property propertyName="X_MULE*" 
          doc:name="Remove X_MULE Properties"/> 
     <remove-property propertyName="http.*" 
          doc:name="Remove http Properties"/> 
    </sub-flow> 
    <flow name="CookieReceiverFlow"> 
     <http:listener  config-ref="HTTP_Configuration_CookieHandlerExample" 
          path="/receiveCookie" 
          doc:name="receiveCookies" 
          doc:description="This connector receives the cookies we've just set"/> 
     <transformer  ref="MyCookieTranformer" 
          doc:name="GrabCookies" 
          doc:description="Use our custom transformers.CookieGrabber class to put the cookies in a nice java.util.List in a session variable."/> 
     <logger    message="CookieReceiver: Value of cookie &quot;token&quot;: &quot;#[sessionVars.incomingCookieMap.get('token')]&quot;. Yep, still there :)" 
          level="INFO" 
          doc:name="Have a cookie!" 
          doc:description="Let get a cookie value, simply by referring the name of it as the key from our map"/> 
     <set-payload  value="#[sessionVars.incomingCookieList.toArray(String)]" 
          doc:name="Put CookieList to payload" 
          doc:description="Put the session vairable List that contains the cookies in the payload"/> 
     <json:object-to-json-transformer returnClass="java.lang.String" 
              doc:name="Object to JSON" 
              doc:description="Convert our payload to a JSON object"/> 
    </flow> 
</mule> 

당신은 그것을 실행하고이 페이지를 열어 테스트 할 수 있습니다.

2

다음은 맞춤 자바 클래스가없는 세션 변수에서 응답의 쿠키를 저장하는 방법입니다.

<set-session-variable variableName="incomingCookies" value="#[org.mule.transport.http.CookieHelper.parseCookiesAsAClient(message.inboundProperties['set-cookie'],null)]" doc:name="Set incomingCookies as Session Variable"/> 
<set-variable variableName="cookie-name" value="#[org.mule.transport.http.CookieHelper.getCookieValueFromCookies(incomingCookieMap,'cookie-name')]" doc:name=“Set cookie-name as Flow Variable”/> 

당신은 CookieHelper 클래스의 parseCookiesAsAServer 방법을 사용하여 나머지 요청에서 쿠키를 추출하는 유사한 방법을 사용할 수 있습니다.

CookieHelper 클래스에 대한 자세한 내용은 여기 https://www.mulesoft.org/docs/site/3.8.0/apidocs/org/mule/transport/http/CookieHelper.html

관련 문제