Content Pipeline
The response content pipeline is essentially a configurable sequence of transformations. By default, it is empty. When the BaseWeb
content rule is used, and its onResponse
method is called, it is populated with a number of transformations for URL mapping. Adapter rules can customize the content pipeline by adding their own transforms.
TransformSequence
The transform sequence can be accessed in onResponse
through the request context object:
var transforms = ctx.rootSequence;
Adapters can add transforms by calling addTransform:
transform = new PlainTextReplacement('foo', 'bar'); transforms.addTransform(new TransformNode(transform, 'example replacement'));
The supplied transform must be wrapped in a TransformNode
object, which essentially gives the transform a name – this will be used later for rule debugging and tracing.
addTransformsToPipeline
The most common types of transforms are PlainTextReplacement
, sometimes in combination with PlainTextRange
, so a convenience method addTransformsToPipeline
is supplied (by tomcat/webapps/ROOT/WEB-INF/classes/webrules/system/lib/ContentReplacementUtils.js
). This method accepts an array of transform definitions:
Field | Type | Required | Default | Description |
name |
String | yes | A unique (not enforced) name for the transform. | |
find |
String | yes | The string to be searched for in the response content. This will be substituted by the replace string. | |
replace |
String | yes | This is used as a substitute for the find string. Transforms run after this one will see this string instead of the find one in any incoming content. | |
caseSensitive |
Boolean | no | true | Indicate whether the string matching for find should be case sensitive. |
countStart |
int | no | 0 | For values greater than zero, replacements will only be made once this many matches have been made. If no matches are found, or the number of matches is less than this number, then no replacements will be made. |
countStop |
int | no | 0 | For values greater than zero, replacements will stop after this many matches have been made. If this value is equal to countStart then 1 replacement will be made. If this value is less than countStart, then no replacements will be made. |
range |
Object | no | This is the equivalent of wrapping the transform in a PlainTextRange. | |
range.start |
String | yes | A string literal to be searched for in the input buffer, indicating the start of the range. | |
range.stop |
String | yes | A string literal to be searched for in the input buffer, once the start has been found. This marks the end of the range. | |
range.caseSensitive |
Boolean | no | true | Indicate whether the string matching for this range’s start and stop should be case sensitive. |
The equivalent of the example in the section above would be:
addTransformsToPipeline(ctx, [ { name: 'example replacement', find: 'foo', replace: 'bar' } ]);
Injected Scripts
Sometimes it is necessary to inject JavaScript into an application to facilitate it operating correctly within the EdgeCore interface. A few utility methods – defined in ContentReplacementUtils.js
are made available to content rules to facilitate this.
Note: These can only currently be used in the non-SSO content rules because the URL mappings afterward will prepend the endpoint paths.
Method | Arguments | Description |
injectScript |
ctx – request context object
src – link to the script name – a descriptive name for the script |
Inject a reference to the specified script conditionally, based on whether a script tag is found in the document being retrieved. |
injectScriptAtHead |
ctx – request context object
src – link to the script name – a descriptive name for the script |
Inject a reference to the specified script conditionally, based on whether a head tag is present in the document being retrieved. A new script tag will be inserted at the start of the head, immediately following the initial head tag. |
injectEdgeWebUtils |
ctx – request context object | Uses injectScript to inject a reference to the edgeWeb utils.js script. |
injectEdgeWebUtilsAtHead |
ctx – request context object | Uses injectScriptAtHead to inject a reference to the edgeWeb utils.js script. |
utils.js
Currently edgeWeb ships with one script – utils.js
– that is intended to be injected into applications by adapter content rules. It implements a single module called edgeWeb
which exports two methods:
Method | Arguments | Returns | Description |
getTop |
window | Returns the top frame as far as the target application should be concerned. On a page, this is the iframe where the web content is being displayed. In the web content feed preview, this is the iframe in which the preview is being displayed. | |
getFrameName |
window | string | A ‘safe’ method for getting the supplied frame’s name – never returns null; returns a blank string instead. This is used by getTop, but made public in case it is useful to other code. |
Transforms
The transforms described here are all Java objects. Definitions are already imported and available to be used by adapter content rules (via ContentReplacementUtils.js
), and can be created similar to this:
var transform = new TransformType( ... );
Replacement Transforms
StandardRegexTransform
Introduced in the 3.8.5 version
This transform searches for text matching the supplied regular expression and replaces it using a ‘normal’ regex replacement string. Refer to the Matcher documentation for valid syntax.
Attribute | Type | Description |
matchExpr |
String | A regular expression to match against the input buffer. See the Java Pattern reference for more details on valid patterns. |
replacementString |
String | A regular expression replacement string. See the Java Matcher reference for more details on valid replacement strings. |
Create and add a StandardRegexTransform
to the response content pipeline like this:
var transform1 = new StandardRegexTransform("foo", "bar"); ctx.rootSequence.addTransform(new TransformNode(transform1, "foo bar mapper")); var transform2 = new StandardRegexTransform("(foo) (bar)", "$2 $1"); ctx.rootSequence.addTransform(new TransformNode(transform2, "foo bar reverser"));
JsRegexTransform
Introduced in the 3.8.5 version
This transform searches for text matching the supplied regular expression and replaces it using a supplied function that returns a string.
Attribute | Type | Description |
matchExpr |
String | A regular expression to match against the input buffer. See the Java Pattern reference for more details on valid patterns. |
getReplacementStringFn |
function(ProxyRequestContext, MatchResult) | A two-argument function invoked whenever a match is found, to generate and return the replacement string. The string is treated as a literal. Note that the MatchResult can be used to reference capture groups in matchExpr. |
Create and add a JsRegexTransform
to the response content pipeline like this:
var transform = new JsRegexTransform("(http(s){0,1})://([\w._-]+)(:(\d*)){0,1}", function(requestContext, matchResult) { return ""; }); ctx.rootSequence.addTransform(new TransformNode(transform, "Naive absolute URL mapper"));
MatchResult
The result of a match operation.
This interface contains query methods used to determine the results of a match against a regular expression. The match boundaries, groups, and group boundaries can be seen but not modified through a MatchResult.
match.group()
is whatever the whole regex match was, and match.group(0)
is the same.
If there are capturing groups, match.group(1)
is the first.
How to use it:
ctx.rootSequence.addTransform(new TransformNode(new JsRegexTransform( '(https?)://([^/]*)/', function(req, match) { logger.debug(match.group()); logger.debug(match.group(0)); logger.debug(match.group(1)); logger.debug(match.group(2));
If the body of the response contained https://www.edgeti.com/ somewhere, the output would look like this:
match.group(): https://www.edgeti.com/ match.group(0): https://www.edgeti.com/ match.group(1): https match.group(2): www.edgeti.com
BasicRegexTransform
This transform searches for text matching the supplied regular expression and replaces it using a supplied function that returns a replacement string. Take care when using this transform, as it can produce unexpected results. When the response content contains characters that will be interpreted by the Java Matcher#appendReplacement
method, you may find that backslash characters (used for escaping) are removed, and dollar signs cause errors (which are quietly ignored, and the input content is passed out unmodified). If necessary, consider using Matcher#quoteReplacementString
. In most cases, you should probably use JsRegexTransform
or StandardRegexTransform
, where the transformation logic is clearer.
Attribute | Type | Description |
matchExpr |
String | A regular expression to match against the input buffer. See the Java Pattern reference for more details on valid patterns. |
getReplacementStringFn |
function(ProxyRequestContext, MatchResult) | A two-argument function invoked whenever a match is found, to generate and return the replacement string. The returned string can contain group references to the match pattern. See the Java Matcher reference for more details on valid replacement strings. |
The BasicRegexTransform
can be added to the response content pipeline in the same way as JsRegexTransform
.
PlainTextReplacement
This transform searches for a fixed string value in the input buffer, and delegates replacement to another transform.
Attribute | Type | Default | Description |
matchText |
String | A string literal to be searched for in the input buffer. | |
countStart |
int | 0 | For values greater than zero, replacements will only be made once this many matches have been made. If no matches are found, or the number of matches is less than this number, then no replacements will be made. |
countStop |
int | 0 | For values greater than zero, replacements will stop after this many matches have been made. If this value is equal to countStart then 1 replacement will be made. If this value is less than countStart, then no replacements will be made. |
caseSensitive |
Boolean | true | Indicate whether the string matching for matchText should be case sensitive. |
child |
Transform | The transform to be used for replacing the matched text. |
Create a PlainTextReplacement
transform like this:
// replace every occurrence of 'foo' with 'bar' var transform1 = new PlainTextReplacement('foo', new FullTextReplacement('bar')); // just replace the 5th occurrence of 'foo' in the document with 'bar' var transform2 = new PlainTextReplacement('foo', 5, 5, new FullTextReplacement('bar')); // modify transform2 so that it ignores case transform2.setCaseSensitive(false);
FullTextReplacement
This transform replaces the input buffer with a fixed string value.
Attribute | Type | Default | Description |
replacementString |
String | The string to be used as the replacement text for this transform. Whenever it is run, the transform replaces the input buffer with this string. |
VariableSubstituter
Identical to FullTextReplacement
, except that a function is used to provide the replacement text, rather than a string literal.
Attribute | Type | Default | Description |
stringSupplier |
function() | A function that returns the string to be used as the replacement text for this transform. Whenever it is run, the transform replaces the input buffer with this string. This function is called each time the transform is invoked. |
RangeTransforms
PlainTextRange
Attribute | Type | Default | Description |
startText |
String | A string literal to be searched for in the input buffer, indicating the start of the range. | |
stopText |
String | A string literal to be searched for in the input buffer, once the startText has been found. This marks the end of the range. | |
inclusive |
Boolean | false | Indicate whether the start/end match text should be included in the range passed to the children of this range. |
inclusiveReplacement |
Boolean | false | Indicate whether replacement should replace the startText and stopText strings that delimit the range. |
caseSensitive |
Boolean | true | Indicate whether the string matching for startText and stopText should be case sensitive |
children |
List<Transform> |
HtmlTagTransform
Attribute | Type | Description |
tagName |
String | The HTML tag to be searched for. |
children |
List<Transform> | Transforms to be applied within the scope of this HTML tag. Typically instances of HtmlAttributeTransform. |
HtmlAttributeTransform
This is a very specific transform which is most effectively used as a child of an HtmlTagTransform
. It is used to identify the text range representing the value for the named attribute. Any Transform
can be supplied to perform the replacement in the context of that range.
Attribute | Type | Description |
attributeName |
String | The HTML attribute to be searched for. |
children |
List<Transform> | Transforms to be applied within the scope of this attribute’s value. |