public void setForwardedHeader ( ) { StringBuilder headerValue = new StringBuilder ( ) ; if ( forwardedHeader == null ) { Object remoteAddressProperty = httpOutboundRequest . getProperty ( Constants . REMOTE_ADDRESS ) ; if ( remoteAddressProperty != null ) { String remoteAddress = ( ( InetSocketAddress ) remoteAddressProperty ) . getAddress ( ) . getHostAddress ( ) ; headerValue . append ( FOR ) . append ( resolveIP ( remoteAddress ) ) . append ( SEMI_COLON ) . append ( " " ) ; } headerValue . append ( BY ) . append ( resolveIP ( localAddress ) ) ; Object hostHeader = httpOutboundRequest . getProperty ( Constants . ORIGIN_HOST ) ; if ( hostHeader != null ) { headerValue . append ( SEMI_COLON + " " + HOST ) . append ( hostHeader . toString ( ) ) ; } Object protocolHeader = httpOutboundRequest . getProperty ( Constants . PROTOCOL ) ; if ( protocolHeader != null ) { headerValue . append ( SEMI_COLON + " " + PROTO ) . append ( protocolHeader . toString ( ) ) ; } httpOutboundRequest . setHeader ( Constants . FORWARDED , headerValue . toString ( ) ) ; return ; } String [ ] parts = forwardedHeader . split ( SEMI_COLON ) ; String previousForValue = null ; String previousByValue = null ; String previousHostValue = null ; String previousProtoValue = null ; for ( String part : parts ) { if ( part . toLowerCase ( Locale . getDefault ( ) ) . contains ( FOR ) ) { previousForValue = part . trim ( ) ; } else if ( part . toLowerCase ( Locale . getDefault ( ) ) . contains ( BY ) ) { previousByValue = part . trim ( ) . substring ( 3 ) ; } else if ( part . toLowerCase ( Locale . getDefault ( ) ) . contains ( HOST ) ) { previousHostValue = part . substring ( 5 ) ; } else if ( part . toLowerCase ( Locale . getDefault ( ) ) . contains ( PROTO ) ) { previousProtoValue = part . substring ( 6 ) ; } } if ( previousForValue == null ) { previousForValue = previousByValue != null ? FOR + previousByValue : null ; } else { previousForValue = previousByValue == null ? previousForValue : previousForValue + COMMA + " " + FOR + previousByValue ; } headerValue . append ( previousForValue != null ? previousForValue + SEMI_COLON + " " : null ) ; headerValue . append ( BY ) . append ( resolveIP ( localAddress ) ) ; if ( previousHostValue != null ) { headerValue . append ( SEMI_COLON + " " + HOST ) . append ( previousHostValue ) ; } if ( previousProtoValue != null ) { headerValue . append ( SEMI_COLON + " " + PROTO ) . append ( previousProtoValue ) ; } httpOutboundRequest . setHeader ( Constants . FORWARDED , headerValue . toString ( ) ) ; }
Set forwarded header to outbound request .