private static Charset getTargetCharset ( String messagePayload ) throws UnsupportedEncodingException { String defaultEncoding = System . getProperty ( Citrus . CITRUS_FILE_ENCODING_PROPERTY , System . getenv ( Citrus . CITRUS_FILE_ENCODING_ENV ) ) ; if ( StringUtils . hasText ( defaultEncoding ) ) { return Charset . forName ( defaultEncoding ) ; } // trim incoming payload String payload = messagePayload . trim ( ) ; char doubleQuote = ' ' ; char singleQuote = ' ' ; // make sure payload has an XML encoding string String encodingKey = "encoding" ; if ( payload . startsWith ( "<?xml" ) && payload . contains ( encodingKey ) && payload . contains ( "?>" ) && ( payload . indexOf ( encodingKey ) < payload . indexOf ( "?>" ) ) ) { // extract only encoding part, as otherwise the rest of the complete pay load will be load String encoding = payload . substring ( payload . indexOf ( encodingKey ) + encodingKey . length ( ) , payload . indexOf ( "?>" ) ) ; char quoteChar = doubleQuote ; int idxDoubleQuote = encoding . indexOf ( doubleQuote ) ; int idxSingleQuote = encoding . indexOf ( singleQuote ) ; // check which character is the first one, allowing for <encoding = 'UTF-8'> white spaces if ( idxSingleQuote >= 0 && ( idxDoubleQuote < 0 || idxSingleQuote < idxDoubleQuote ) ) { quoteChar = singleQuote ; } // build encoding using the found character encoding = encoding . substring ( encoding . indexOf ( quoteChar ) + 1 ) ; encoding = encoding . substring ( 0 , encoding . indexOf ( quoteChar ) ) ; // check if it has a valid char set if ( ! Charset . availableCharsets ( ) . containsKey ( encoding ) ) { throw new UnsupportedEncodingException ( "Found unsupported encoding: '" + encoding + "'" ) ; } // should be a valid encoding return Charset . forName ( encoding ) ; } // return as encoding the default UTF-8 return Charset . forName ( "UTF-8" ) ; }
Try to find target encoding in XML declaration .