Predicates in EAP 7 Undertow Configuration
Environment
- Red Hat JBoss Enterprise Application Server (EAP)
- 7.x
- Undertow
Issue
- What are predicates?
- What are the built-in predicates that can be used?
- What are the built-in exchange values that can be used in a predicate?
Resolution
Disclaimer: The following information has been provided by Red Hat, but is outside the scope of the posted This content is not included.Service Level Agreements and This content is not included.Scope of Coverage. Red Hat does not support software contained in the EPEL repository, or other third-party repositories, or from third-party non-Red Hat sources. The information is provided as-is without representations or warranties about the suitability or accuracy of the information provided. The intent is to provide information to accomplish the system's needs. Use of the information below is at the user's own risk.
Links contained herein to an external website(s) are provided for convenience only. Red Hat has not reviewed the links and is not responsible for the content or its availability. The inclusion of any link to an external website does not imply endorsement by Red Hat of the website or their entities, products or services. You agree that Red Hat is not responsible or liable for any loss or expenses that may result due to your use of (or reliance on) the external site or content.
Predicates provide a simple way of making a true/false decision based on an exchange. Many handlers have a requirement that they are applied conditionally, and predicates provide a general way to specify a condition. Predicates can be created programmatically (they are just java classes that implement the Predicate interface), however, there is also a simple language for specifying a predicate. Some examples below:
# regular expression match of the relative URL
regex['/resources/*.\.css']
# Matches requests with a text/.* content type
regex[pattern='text/.*', value='%{i,Content-Type}', full-match=true]
# Matches if the content type header is text/xml
equals[{'%{i,Content-Type}', 'text/xml'}]
# User agent contains MSIE and request URL ends with .js
contains[search='MSIE', value='%{i,User-Agent}'] and path-suffix['.js']
# regex match, with a reference to match group 1 later in the expression
regex['/resources/(*.)\.css'] and equals[{'$1', 'myCssFile'}]
In How to redirect HTTP to HTTPS in JBoss EAP 7 explains how to configure a rewrite rule using a predicate to redirect a non-secure HTTP URL to a secure https URL.
For example:
<filter-ref name="http-to-https" predicate="equals(%p,8080)"/>
The predicate, equals(%p,8080), returns true if the exchange attribute %p is 8080. The exchange attribute, %p, is the short form of %{LOCAL_PORT}.
<filter-ref name="Access-Control-Allow-Origin" predicate="contains(search='Firefox', value='%{i,User-Agent}')"/>
....
<response-header name="Access-Control-Allow-Origin" header-name="Access-Control-Allow-Origin" header-value="https://www.example.com"/>
The predicate, contains(search='Firefox', value='%{i,User-Agent}'), returns true if the incoming User-Agent header value contains Firefox string, and then only the Access-Control-Allow-Origin header will be added to the outgoing response.
For more information check Undertow documentation to check the table with the predicates:
- Content from undertow.io is not included.Undertow Predicates and Handlers
- Content from undertow.io is not included.Textual Representation of Predicates
- Content from undertow.io is not included.Exchange Attributes
Root Cause
- Customers are configuring rewrite rules in the Undertow Subsystem and have inquired about the different predicates available for use in the configuration.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.