|
107 | 107 | * <td>x-forwarded-for</td> |
108 | 108 | * </tr> |
109 | 109 | * <tr> |
| 110 | + * <td>requestIdHeader</td> |
| 111 | + * <td>Name of the Http Header read by this servlet filter that holds a request ID passed by a proxy or the requesting |
| 112 | + * client</td> |
| 113 | + * <td>RequestIdHeader</td> |
| 114 | + * <td>Compliant http header name</td> |
| 115 | + * <td>x-request-id</td> |
| 116 | + * </tr> |
| 117 | + * <tr> |
110 | 118 | * <td>internalProxies</td> |
111 | 119 | * <td>Regular expression that matches the IP addresses of internal proxies. If they appear in the |
112 | 120 | * <code>remoteIpHeader</code> value, they will be trusted and will not appear in the <code>proxiesHeader</code> |
|
210 | 218 | * <param-name>protocolHeader</param-name> |
211 | 219 | * <param-value>x-forwarded-proto</param-value> |
212 | 220 | * </init-param> |
| 221 | + * <init-param> |
| 222 | + * <param-name>requestIdHeader</param-name> |
| 223 | + * <param-value>x-request-id</param-value> |
| 224 | + * </init-param> |
213 | 225 | * </filter> |
214 | 226 | * |
215 | 227 | * <filter-mapping> |
@@ -476,6 +488,8 @@ public static class XForwardedRequest extends HttpServletRequestWrapper { |
476 | 488 |
|
477 | 489 | protected int serverPort; |
478 | 490 |
|
| 491 | + protected String requestId; |
| 492 | + |
479 | 493 | public XForwardedRequest(HttpServletRequest request) { |
480 | 494 | super(request); |
481 | 495 | this.localName = request.getLocalName(); |
@@ -568,6 +582,15 @@ public String getRemoteHost() { |
568 | 582 | return this.remoteHost; |
569 | 583 | } |
570 | 584 |
|
| 585 | + @Override |
| 586 | + public String getRequestId() { |
| 587 | + if (this.requestId != null) { |
| 588 | + return this.requestId; |
| 589 | + } |
| 590 | + |
| 591 | + return super.getRequest().getRequestId(); |
| 592 | + } |
| 593 | + |
571 | 594 | @Override |
572 | 595 | public String getScheme() { |
573 | 596 | return scheme; |
@@ -617,6 +640,10 @@ public void setRemoteHost(String remoteHost) { |
617 | 640 | this.remoteHost = remoteHost; |
618 | 641 | } |
619 | 642 |
|
| 643 | + public void setRequestId(String requestId) { |
| 644 | + this.requestId = requestId; |
| 645 | + } |
| 646 | + |
620 | 647 | public void setScheme(String scheme) { |
621 | 648 | this.scheme = scheme; |
622 | 649 | } |
@@ -667,6 +694,8 @@ public StringBuffer getRequestURL() { |
667 | 694 |
|
668 | 695 | protected static final String REMOTE_IP_HEADER_PARAMETER = "remoteIpHeader"; |
669 | 696 |
|
| 697 | + protected static final String REQUEST_ID_HEADER_PARAMETER = "requestIdHeader"; |
| 698 | + |
670 | 699 | protected static final String TRUSTED_PROXIES_PARAMETER = "trustedProxies"; |
671 | 700 |
|
672 | 701 | protected static final String ENABLE_LOOKUPS_PARAMETER = "enableLookups"; |
@@ -717,6 +746,11 @@ public StringBuffer getRequestURL() { |
717 | 746 | */ |
718 | 747 | private String remoteIpHeader = "X-Forwarded-For"; |
719 | 748 |
|
| 749 | + /** |
| 750 | + * @see #setRequestIdHeader(String) |
| 751 | + */ |
| 752 | + private String requestIdHeader = ""; |
| 753 | + |
720 | 754 | /** |
721 | 755 | * @see #setRequestAttributesEnabled(boolean) |
722 | 756 | */ |
@@ -843,6 +877,14 @@ public void doFilter(HttpServletRequest request, HttpServletResponse response, F |
843 | 877 | } |
844 | 878 | } |
845 | 879 | } |
| 880 | + |
| 881 | + if (!requestIdHeader.isEmpty()) { |
| 882 | + String requestIdHeaderValue = request.getHeader(requestIdHeader); |
| 883 | + if (requestIdHeaderValue != null) { |
| 884 | + xRequest.setRequestId(requestIdHeaderValue); |
| 885 | + } |
| 886 | + } |
| 887 | + |
846 | 888 | request.setAttribute(Globals.REQUEST_FORWARDED_ATTRIBUTE, Boolean.TRUE); |
847 | 889 |
|
848 | 890 | if (log.isTraceEnabled()) { |
@@ -1016,6 +1058,10 @@ public void init() throws ServletException { |
1016 | 1058 | setRemoteIpHeader(getInitParameter(REMOTE_IP_HEADER_PARAMETER)); |
1017 | 1059 | } |
1018 | 1060 |
|
| 1061 | + if (getInitParameter(REQUEST_ID_HEADER_PARAMETER) != null) { |
| 1062 | + setRequestIdHeader(getInitParameter(REQUEST_ID_HEADER_PARAMETER)); |
| 1063 | + } |
| 1064 | + |
1019 | 1065 | if (getInitParameter(TRUSTED_PROXIES_PARAMETER) != null) { |
1020 | 1066 | setTrustedProxies(getInitParameter(TRUSTED_PROXIES_PARAMETER)); |
1021 | 1067 | } |
@@ -1221,6 +1267,17 @@ public void setRemoteIpHeader(String remoteIpHeader) { |
1221 | 1267 | this.remoteIpHeader = remoteIpHeader; |
1222 | 1268 | } |
1223 | 1269 |
|
| 1270 | + /** |
| 1271 | + * <p>Name of the http header from which the request id is extracted.</p> |
| 1272 | + * |
| 1273 | + * <p>Request id propagation is disabled by default. Set a value, e.g. <code>X-Request-Id</code>, to enable it.</p> |
| 1274 | + * |
| 1275 | + * @param requestIdHeader The header name |
| 1276 | + */ |
| 1277 | + public void setRequestIdHeader(String requestIdHeader) { |
| 1278 | + this.requestIdHeader = requestIdHeader; |
| 1279 | + } |
| 1280 | + |
1224 | 1281 | /** |
1225 | 1282 | * Should this filter set request attributes for IP address, Hostname, protocol and port used for the request? This |
1226 | 1283 | * are typically used in conjunction with an {@link AccessLog} which will otherwise log the original values. Default |
|
0 commit comments