2013-12-12 3 views
1

다시. 기술적 인 연습으로 WSO2 ESB를 사용하여 웹 트래픽을 프록시하려고 시도하고 있습니다. 특히, 나는 프록시 웹 트래픽을 시도하는 등처럼 즉석에서 반환 된 응답을 변경하고 있습니다 :WSO2 ESB (Apache Synapse) 응답 처리가있는 프록시

  • 유무 ESB는
  • 가받을 특정 서버에 HTTP 요청을
  • 프록시 요청을받을 응답
  • 는 "슬픈"라는 단어의 발생을 찾아 "행복"(대소 문자 구분 정규식)
  • 브라우저
로 다시 변경된 응답을 통과로 교체 0

하나는 이것이 단순한 정규식이나 XSLT 연산이라고 생각 하겠지만, 이것은 내가 생각했던 것보다 훨씬 더 어려워지고있다. 순간,이 풍성 작업을 사용하여 부여

<?xml version="1.0" encoding="UTF-8"?> 
<proxy xmlns="http://ws.apache.org/ns/synapse" 
     name="PassProxy" 
     transports="https http" 
     startOnLoad="true" 
     trace="disable"> 
    <description>Route content from a web server through the ESB service and alter it</description> 
    <target> 
     <endpoint> 
     <address uri="http://server.yoyodyne.com/"/> 
     </endpoint> 
     <inSequence/> 
     <outSequence> 
     <property name="TheContentIncludingTheSoapEnvelope" expression="."/> 
     <property xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
        xmlns:ns="http://org.apache.synapse/xsd" 
        name="TheContentFromSoapBodyButNotReally" 
        expression="//soapenv:Envelope/soapenv:Body/*"/> 
     <property name="TheContent" 
        value="An initial value that should be replaced" 
        scope="default" 
        type="STRING"/> 
     <enrich> 
      <source type="body" clone="true"/> 
      <target type="property" property="TheContent"/> 
     </enrich> 
     <property name="ContentType" expression="$trp:Content-Type"/> 
     <property name="ContentLength" expression="$trp:Content-Length"/> 
     <log level="custom"> 
      <property name="ContentType" expression="$trp:Content-Type"/> 
      <property name="ContentLength" expression="$trp:Content-Length"/> 
      <property name="MessageVar" value="TheContent"/> 
      <property name="TargetMessage" expression="get-property('TheContent')"/> 
     </log> 
     <script language="js"> 
      //hack because xpath fn:replace does not work in property tags and fn:translate works on chars not whole strings 
      var contentType=mc.getProperty('ContentType'); 
      var contentObject=mc.getProperty('TheContent'); //how to get the text of this? And do it in un-escaped format??? 
      if(/text/i.test(contentType)) { 
       if(!contentObject) { 
        mc.setProperty('TheAlteredContent','Well that didn\'t work as expected'); 
       } else { 
        if(typeof contentObject == 'object' || typeof contentObject == 'undefined') { 
         var returnMessage=''; 
         for (var key in contentObject) { 
          returnMessage=returnMessage+'found key "'+key+'"\n'; 
         } //end object key for 
         returnMessage='Can\'t work with this type of input - '+typeof contentObject+'n\Available keys to try:\n'+returnMessage; 
         contentObject=returnMessage; 
        } else { 
         contentObject=contentObject.replaceAll('sad', 'happy'); 
         //more regex statements to go here 
        } //end typeof if 
       } //end property check if 
      } else { 
       //not text - do nothing 
       contentObject='binary content (I think). Found content type of "'+contentType+'"'; 
      } //end content type check if 
      //send the content back 
      mc.setProperty('TheAlteredContent',contentObject); 
     </script> 
     <enrich> 
      <!-- need to figure out how to replace the content and not append to the end of it. Replace tag on target keeps getting removed for some reason --> 
      <source type="property" property="TheAlteredContent" clone="true"/> 
      <target type="body"/> 
     </enrich> 
     <!-- doctype headers in the HTML cause logging to fail, so no debugging for you --> 
     <!--<log level="full"/>--> 
     <send/> 
     </outSequence> 
    </target> 
</proxy> 

아마이 문제를 처리하는 가장 좋은 방법이 아니다 ... 내가 사용하고 프록시 스크립트이지만, 당시 좋은 생각처럼 보였다. 무슨 일이 일어나는지는 응답의 HTML 부분이 이스케이프 된 내용을 가진 객체로서 JS 코드에 전달된다는 것입니다 (또는 ???). "contentObject"var가 객체이기 때문에 정규 표현식이 실패합니다. toString()을 사용하여 문자열에 "contentObject"를 강제 실행해도 작동하지 않습니다. 그것이 작동하더라도, HTML 컨텐트는 여전히 이스케이프 된 형태이며 변환은 HTML 형식으로 머물러 있어야 할 수도있는 HTML 코드에서 이스케이프 된 항목이있을 수 있으므로 문제가 될 수 있습니다. 여기서 마지막으로 문제는 "TheAlteredContent"속성의 콘텐츠가 콘텐츠에 추가되고 대체되지 않는다는 것입니다. "action = replace"속성이 최종 풍부 작업에 추가 될 때도 (ESB는 실제로 제거합니다).

누구든지이 작업을 수행하는 더 좋은 방법, 또는 위의 코드를 작동시키는 방법을 알고 있습니까?

답변

0

저는 여러분이 백엔드에서 HTML 응답을 받고 있다고 생각합니다. 간단히 outpath에서 사용자 정의 클래스 조정자를 사용하여 내용을 변경하십시오. mediate() 코드에서 String.replace를 사용합니다. 그것은 자바 스크립트를 사용하여 프록시 구성에서 전체 복잡한 코딩 로직을 만드는 것보다 상당히 간단합니다.

문제점을 해결할 수 있습니까?

+0

아마도이 문제를 조사해야 할 것입니다. 정말로 이것은 ESB를 더 잘 배우기 위해 스스로 배정한 과제만큼이나 큰 문제는 아닙니다. –