We are consuming response from 3rd party service which has product related information.
Below is the code snippet we are using:
String newstr = responseData.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
newstr = responseData.replaceAll("\\+", "%2B");
log.debug("newstr val is : "+newstr);
str0 = URLDecoder.decode(new String((responseData).getBytes("ISO-8859-1"), "UTF-8"), "UTF-8");
log.debug("Service response str0 : "+str0);
str2 = URLDecoder.decode(new String((newstr).getBytes("ISO-8859-1"), "UTF-8"), "UTF-8");
log.debug("Service response str2 : "+str2);
str3 = URLDecoder.decode(new String((responseData).getBytes("UTF-8"), "ISO-8859-1"), "UTF-8");
log.debug("Service response str3 : "+str3);
str1 = URLDecoder.decode(new String((newstr).getBytes("UTF-8"), "ISO-8859-1"), "UTF-8");
log.debug("Service response str1 : "+str1);
//#1
//responseData = str2;
//#2
responseData = responseData;
Earlier I was sending the string - responseData directly, but it created an issue with translating some special characters of fr,de,it languages. Then I tried decoding it using URLDecoder, I know the encoding is supposed to be done in UTF-8.
But if I use str0, str1, str2, str3 I am able to see that we can decode the response string and special characters can be translated properly but when we have some product title having '%' special character in it, the code breaks giving following exception - URLDecoder throwing exception of illegal hex character in escape pattern (%) as shown in the attached image.
Is there any way I could resolve both the problems without manually altering the response string?