5 Nisan 2013 Cuma

WSO2 ESB + ClientAccesPolicy.xml + CrossDomain.xml

ClientAccesPolicy.xml and CrossDomain.xml files have to be found at root of your services Silverlight and Flex clients to be able to access your services. WSO2 seems doesn't allow hosting files at root, but it can. This article gives a sample about how to host these files. 

Modify your main sequence like below: (gist here)

<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="main" trace="disable">
  <in>
    <log level="headers">
      <property name="?" value="[main] in: started"/>
      <property xmlns:ns="http://org.apache.synapse/xsd"
           name="To"
           expression="get-property('To')"/>
      <property xmlns:ns="http://org.apache.synapse/xsd" name="PAYLOAD" expression="/*"/>
    </log>
    <switch xmlns:ns="http://org.apache.synapse/xsd" source="get-property('To')">
      <case regex="(/services)?/[A-Za-z]+.xml">
        <log level="headers">
          <property name="?" value="[main] in: File from root requested"/>
          <property name="To" expression="get-property('To')"/>
        </log>
        <switch source="get-property('To')">
          <case regex="/services/[A-Za-z]+.xml">
            <property name="FileName"
              expression="substring-after(get-property('To'), 'services/')"
              scope="default"
              type="STRING"/>
          </case>
          <case regex="/[A-Za-z]+.xml">
            <property name="FileName"
              expression="substring-after(get-property('To'), '/')"
              scope="default"
              type="STRING"/>
          </case>
          <default>
            <log level="headers">
              <property name="?" value="[main] in: unexpected request!"/>
              <property name="To" expression="get-property('To')"/>
            </log>
            <drop/>
          </default>
        </switch>
        <log level="headers">
          <property name="?" value="[main] in: File from root requested"/>
          <property name="FileName" expression="get-property('FileName')"/>
        </log>
        <switch source="get-property('FileName')">
          <case regex="clientaccesspolicy\.xml">
            <script language="js">mc.setPayloadXML(                                        
            &lt;?xml version="1.0" encoding="utf-8"?&gt;                                            
               &lt;access-policy&gt;                                                  
                &lt;cross-domain-access&gt;                                                        
                 &lt;policy&gt;                                                              
                  &lt;allow-from http-request-headers="SOAPAction"&gt;                                                                    
                     &lt;domain uri="*"/&gt;                                                              
                  &lt;/allow-from&gt;                                                              
                  &lt;grant-to&gt;                                                                    
                     &lt;resource path="/" include-subpaths="true"/&gt;                                                              
                  &lt;/grant-to&gt;                                                        
                 &lt;/policy&gt;                                                  
                &lt;/cross-domain-access&gt;                                            
               &lt;/access-policy&gt;);</script>
          </case>
          <case regex="crossdomain\.xml">
            <script language="js">mc.setPayloadXML(                                              
                &lt;?xml version="1.0"?&gt;                                                  
                 &lt;!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"&gt;                                                    
                  &lt;cross-domain-policy&gt;                                                            
                     &lt;allow-http-request-headers-from domain="*" headers="SOAPAction,Content-Type"&gt;&lt;/allow&gt;                                                    
                  &lt;/cross-domain-policy&gt;);</script>
          </case>
          <default>
            <log level="headers">
              <property name="?" value="[main] in: (?) unknown file requested"/>
              <property name="Filename" expression="get-property('FileName')"/>
            </log>
            <drop/>
          </default>
        </switch>
        <header name="To" action="remove"/>
        <property name="NO_ENTITY_BODY" scope="axis2" action="remove"/>
        <property name="RESPONSE" value="true"/>
        <send/>
      </case>
      <default>
        <log level="headers">
          <property name="?" value="[main] in: (?) unknown request"/>
        </log>
      </default>
    </switch>
  </in>
  <out>
    <log level="headers">
      <property name="?" value="[main] out: started"/>
      <property name="PAYLOAD" expression="//*"/>
    </log>
    <send/>
    <log level="headers">
      <property name="?" value="[main] out: ended"/>
    </log>
  </out>
</sequence>

What is going on here?

  1. Outer <switch source="get-property('To')"> inspects which URL requested.
  2. <case regex="(/services)?/[A-Za-z]+.xml"> looks for something like /clientaccesspolicy.xml or /services/clientaccesspolicy.xml
  3. Inner <switch source="get-property('To')"> inspects URL again to extract file name.
  4. <case regex="/services/[A-Za-z]+.xml"> and <case regex="/[A-Za-z]+.xml"> detects exact pattern and extracts file name to FileName property.
  5. <switch source="get-property('FileName')"> puts a proper payload according to requested file name.
  6. Then, we send the payload back to the client. You can see previous post about this part.

WSO2 ESB: Simple Sequence To Send A Static XML Response

Aim

Sometimes we meet some requests coming to ESB which have to be responded by a pre-defined XML, or an XML which is created easily in a dynamic way on the fly. In a such case we don't want to call a web service. We want to send our response back directly without need to a pinp-pong wall like echo service (as in my previous approach).



Sample Sequence (gist here)

<sequence xmlns="http://ws.apache.org/ns/synapse" name="main" trace="disable">
    <in>
        <log level="headers">
            <property name="?" value="[main] in: started"/>
            <property name="To" expression="get-property('To')"/>
            <property name="PAYLOAD" expression="/*"/>
        </log>
        <script language="js"><![CDATA[
            mc.setPayloadXML(<greeting>Hello World</greeting>);
        ]]></script>
        <header name="To" action="remove"/>
        <property name="NO_ENTITY_BODY" action="remove" scope="axis2"/>
        <property name="RESPONSE" value="true"/>
        <send/>
        <log level="headers">
            <property name="?" value="[main] in: ended"/>
            <property name="PAYLOAD" expression="/*"/>
        </log>
    </in>
    <out>
        <log level="headers">
            <property name="?" value="[main] out: started"/>
            <property name="RESPONSE" expression="/*"/>
        </log>
        <send/>
        <log level="headers">
            <property name="?" value="[main] out: ended"/>
        </log>
    </out>
</sequence>

Sample Response

Practical Usage Scenario

ClientAccesPolicy.xml and CrossDomain.xml. I will write about them at next post.





WSO2 ESB + ClientAccesPolicy.xml + CrossDomain.xml

ClientAccesPolicy.xml and CrossDomain.xml files have to be found at root of your services Silverlight and Flex clients to be able to acce...