vADC Docs

Embedding RSS data into web content using Stingray

by on ‎02-21-2013 02:53 AM - edited on ‎07-14-2015 12:20 PM by PaulWallace (1,192 Views)

Many services now use RSS feeds to distribute frequently updated information like news stories and status reports. Stingray's powerful TrafficScript language lets you process RSS XML data, and this article describes how you can embed several RSS feeds into a web document.

 

It illustrates Stingray's response rewriting capabilities, XML processing and its ability to query several external datasources while processing a web request.

 

In this example, we'll show how you can embed special RSS tags within a static web document. Stingray will intercept these tags in the document and replace them with the appropriate RSS feed data:

 

<!RSS http://community.brocade.com/community/product-lines/stingray/view-browse-feed.jspa?browseSite=place-content&browseViewID=placeContent&userID=9503&containerType=14&containerID=2005&filterID=contentstatus%5Bpublished%5D~objecttype~objecttype%5Bthread%... !>

 

We'll use a TrafficScript rule to process web responses, seek out the RSS tag and retrieve, format and insert the appropriate RSS data.

 

Check the response

 

First, the TrafficScript rule needs to obtain the response data, and verify that the response is a simple HTML document. We don't want to process images or other document types!

 

1
2
3
4
5
6
7
8
9
# Check the response type
 
$contentType = http.getResponseHeader( "Content-Type" );
 
if( ! string.startsWith( $contentType, "text/html" ) ) break;
 
# Get the response data
 
$body = http.getResponseBody();

 

Find the embedded RSS tags

 

Next, we can use a regular expression to search through the response data and find any RSS tags in it:

 

(.*?)<!RSS\s+(.*?)\s+!>(.*)

 

Stingray supports Perl compatible regular expressions (regexs). This regex will find the first RSS tag in the document, and will assign text to the internal variables $1, $2 and $3:

 

  • $1: the text before the tag
  • $2: the RSS URL within the tag
  • $3: the text after the tag

 

The following code searches for RSS tags:

 

1
2
3
4
5
6
7
8
9
while( string.regexmatch( $body, '(.*?)<!RSS\s+(.*?)\s*!>(.*)' )) {
 
   $start = $1;
 
   $url = $2;
 
   $end = $3;
 
}

 

Retrieve the RSS data

 

An asynchronous HTTP request is sufficient to retrieve the RSS XML data&colon;

 

1
$rss = http.request.get( $url );

 

Transform the RSS data using an XSLT transform

 

The following XSLT transform can be used to extract the first 4 RSS items and format them up as an HTML <UL> list:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0"?>
 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
   <xsl:template match="/">
 
    <ul>
 
      <xsl:apply-templates select="//item[position()&lt;5]"/>
 
    </ul>
 
   </xsl:template>
 
    
 
   <xsl:template match="item">
 
      <xsl: param name="URL" select="link/text()"/>
 
      <xsl: param name="TITLE" select="title/text()"/>
 
      <li><a href="{$URL}"><xsl:value-of select="$TITLE"/></a></li>
 
   </xsl:template>
 
</xsl:stylesheet>

 

Store the XSLT file in the Stingray conf/extra directory, naming it 'rss.xslt', so that the rule can look it up using resource.get().

 

You can apply the XSLT transform to the XML data using the xml.xslt.transform function. The function returns the result with HTML entity encoding; use string.htmldecode to remove these:

 

1
2
3
$xsl = resource.get( "rss.xslt" );
 
$html= string.htmldecode( xml.xslt.transform( $rss, $xsl ) );

 

The entire rule

 

The entire response rule, with a little additional error checking, looks like this:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
$contentType = http.getResponseHeader( "Content-Type" );
 
if( ! string.startsWith( $contentType, "text/html" ) ) break;
 
$body = http.getResponseBody();
 
$new = "";
 
$changed = 0;
 
while( string.regexmatch( $body, '(.*?)<!RSS\s+(.*?)\s*!>(.*)' )) {
 
   $start = $1;
 
   $url = $2;
 
   $end = $3;
 
 
   $html = "<ul><li><b>RSS: ".$url."</b></li></ul>";
 
 
   $rss = http.request.get( $url );
 
   if( $1 != 200 ) {
 
      $html = "<ul><li><b>Failed to retreive RSS feed</b></li></ul>";
 
   } else {
 
      $xsl = resource.get( "rss.xslt" );
 
      $html = string.htmldecode( xml.xslt.transform( $rss, $xsl ) );
 
      if( $html == -1 ) {
 
          $html = "<ul><li><b>Failed to parse RSS feed</b></li></ul>";
 
      }
 
   }
 
 
   $new = $new . $start . $html;
 
   $body = $end;
 
   $changed = 1;
 
}
 
if( $changed )
 
  http.setresponsebody( $new . $body );
Contributors