WSO2 How to create custom mediation sequence to serve static content

核能气质少年 提交于 2019-12-11 05:13:44

问题


I'm trying to create an "pass-through" WSO2 API to serve some static files on an internal server to the outside. As I understand it, WSO2 requires me to configure multiple {resources} under /{context}/{version}/{resourcePath}.

Assuming my context and version are 'files' and 'v1' I need to map these external paths to these internal paths on my backend server.

  • /files/v1/js/api.js -> /js/api.js
  • /files/v1/css/file.css -> /css/file.css
  • /files/v1/api/2.0/auth/signin -> /api/2.0/auth/signin

Basically all I need to do is strip away the preceding context & version so I tried this custom mediation sequence in the InSequence

<sequence xmlns="http://ws.apache.org/ns/synapse" name="custom-seq">
  <log level="full">
    <property name="IN_MESSAGE" value="IN_MESSAGE"
  </log>
  <property name="resource_ep" 
            expression="get-property('axis2','REST_URL_POSTFIX')"/>
  <header name="To" expression="get-property('resource_ep')"/>
  <log level="custom">
    <property name="to" expression="get-property('To')"/>
  </log>
</sequence>

Is it possible to configure resourcePath that match the whole directory tree?

Instead of: /files/v1/api/{ver}/{auth}/{signIn} I would like /files/v1/api/**

My problem is that I have approx. 6 top-level directories to serve files from and some of the directories are 15 levels deep.


回答1:


I was able to solve it with this In Flow Mediation sequence definition.

<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="custom-seq">
<property> name="resource_ep"
           expression="get-property('axis2', 'REST_URL_POSTFIX')"/>
 <switch source="get-property('axis2', 'REST_URL_POSTFIX')">
  <case > regex=".*/api.*">
    <log level="custom">
      <property name="My-Mediator" value="/api"/>
    </log>
    <header name="To" expression="get-property('resource_ep')"/>
  </case>
  <case regex=".*/js.*">
    <log level="custom">
      <property name="My-Mediator" value="/js"/>
    </log>
    <header name="To" expression="get-property('resource_ep')"/>
  </case>
  <default>
    <log level="custom">
      <property name="Tableau-Mediator" value="default (ignored)"/>
    </log>
  </default>
 </switch>
</sequence>

Each case block defines a top-level directory that I want to serve via /files/v1/ (or just /files/ if I make v1 the default version).

The default block should ignore everything else.

I still have to adapt my client app to send requests to

/files/v1/css/somedir/somefile.css

instead of

/css/somedir/somefile.css



来源:https://stackoverflow.com/questions/46187504/wso2-how-to-create-custom-mediation-sequence-to-serve-static-content

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!