Ví dụ sau đây sử dụng 3 chính sách Gán thông báo:

  1. Tạo ba biến flow trong yêu cầu, với các giá trị tĩnh
  2. Tự động lấy các biến flow trong chính sách thứ hai trong flow yêu cầu
  3. Đặt các giá trị này trong tải trọng của phản hồi
<!-- Policy #1: Set variables in the request -->
<AssignMessage continueOnError="false" enabled="true" name="set-variables">
    <!-- Create a variable named myAppSecret -->
    <AssignVariable>
        <Name>myAppSecret</Name>
        <Value>42</Value>
    </AssignVariable>
    <!-- Create a variable named config.environment -->
    <AssignVariable>
        <Name>config.environment</Name>
        <Value>test</Value>
    </AssignVariable>
    <!-- Create a variable named config.protocol -->
    <AssignVariable>
        <Name>config.protocol</Name>
        <Value>gopher</Value>
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

Trong chính sách đầu tiên, phần tử <AssignVariable> sẽ tạo và đặt ba biến trong yêu cầu. Mỗi phần tử <Name> chỉ định tên biến và <Value> chỉ định giá trị.

Chính sách thứ hai sử dụng phần tử <AssignVariable> để đọc các giá trị và tạo ba biến mới:

<!-- Policy #2: Get variables from the request -->
<AssignMessage continueOnError="false" enabled="true" name="get-variables">
  <AssignTo createNew="false" transport="http" type="request"/>
  <!-- Get the value of myAppSecret and create a new variable, secret -->
  <AssignVariable>
    <Name>secret</Name>
    <Ref>myAppSecret</Ref>
    <Value>0</Value>
  </AssignVariable>
  <!-- Get the value of config.environment and create a new variable, environment -->
  <AssignVariable>
    <Name>environment</Name>
    <Ref>config.environment</Ref>
    <Value>default</Value>
  </AssignVariable>
  <!-- Get the value of config.protocol and create a new variable, protocol -->
  <AssignVariable>
    <Name>protocol</Name>
    <Ref>config.protocol</Ref>
    <Value>default</Value>
  </AssignVariable>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>

Trong chính sách thứ hai, phần tử <Ref> tham chiếu đến biến nguồn và các phần tử <Name> chỉ định tên của các biến mới. Nếu không truy cập được biến được tham chiếu bởi phần tử <Ref>, bạn có thể sử dụng giá trị do phần tử <Value> chỉ định.

Cách dùng thử bộ chính sách này:

  1. Thêm chính sách #1 và #2 vào quy trình yêu cầu. Hãy nhớ đặt chính sách số 1 trước chính sách số 2.
  2. Thêm chính sách thứ ba trong luồng phản hồi.
  3. Chính sách thứ ba sử dụng phần tử <Set> để thêm các biến vào phản hồi. Ví dụ sau đây tạo một tải trọng XML trong phản hồi mà Edge trả về cho ứng dụng khách:
    <!-- Policy #3: Add variables to the response -->
    <AssignMessage continueOnError="false" enabled="true" name="put-em-in-the-payload">
      <DisplayName>put-em-in-the-payload</DisplayName>
      <Set>
        <Payload contentType="application/xml">
          <wrapper>
            <secret>{secret}</secret>
            <config>
              <environment>{environment}</environment>
              <protocol>{protocol}</protocol>
            </config>
          </wrapper>
        </Payload>
      </Set>
      <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
      <AssignTo createNew="false" transport="http" type="response"/>
    </AssignMessage>

    Xin lưu ý rằng cú pháp để truy cập các biến flow trong <Set> là gói các biến đó trong dấu ngoặc nhọn.

    Hãy nhớ đặt thuộc tính contentType của phần tử <Payload> thành "application/xml".

  4. Gửi yêu cầu đến proxy API; ví dụ:
    curl -vL https://ahamilton-eval-test.apigee.net/myproxy

    Nếu muốn, bạn có thể chuyển kết quả qua một tiện ích như xmllint để XML hiển thị theo cấu trúc được định dạng đẹp mắt:

    curl -vL https://ahamilton-eval-test.apigee.net/myproxy | xmllint --format -

    Nội dung của phản hồi sẽ có dạng như sau:

    <wrapper>
      <secret>42</secret>
      <config>
        <environment>test</environment>
        <protocol>gopher</protocol>
      </config>
    </wrapper>