Prashanth Tiramareddi
Prashanth Tiramareddi

Reputation: 31

How to Convert Surrogate Pairs in Servlet Response? How can I read Surrogate Pairs?

I have emoji as Surrogate Pairs received in the response.

I want to change these Surrogates Pairs to a Unicode so that the WebSphere Portal 7 will understand the Unicodes.

Added a filter to modify the response, to convert Surrogates to Unicode but I'm unable to convert in this situation.

Filter:

    public void doFilter(
            ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException
    {
        CharResponseWrapper wrappedResponse = new CharResponseWrapper(
                (HttpServletResponse)response);

        chain.doFilter(request, wrappedResponse);
       byte[] bytes = wrappedResponse.getByteArray();
         String out = new String(bytes);      
         String inputXml = new String(out.getBytes("UTF-8")); 
         // Need to Convert the Surrogates to Unicode from the XML String here and write it back to Response
         out = surrogatesToUnicode(inputXml);      
         response.getOutputStream().write(out.getBytes());
    }

Response:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <xxxxx>
            <xx>
                <xx>
                    <xx></xx>
                    <xx>100420</xx>
                    <xx>xxx</xx>
                    <xx>4</xx>
                    <xx>false</xx>
                    <xx>false</xx>
                    <xx>false</xx>
                    <***QuestionHere***>Agent - Please run and advise with initial update when available; thank you for accepting! &#55357;&#56842;</***QuestionHere***>

I want to convert the data &#55357 ;&#56842 ; into Unicode. And I am using this function but it is unable to recognize the surrogate pairs at the charCount method and convert from the response String.

 public String surrogatesToUnicode(String str) {
        StringBuilder sb = new StringBuilder();           

        for(int i = 0; i < str.length(); i ++) {
            int cp = str.codePointAt(i);   
            if(Character.charCount(cp) == 2) {
                sb.append("&#" + cp + ";");
                i++;
            }        
           else {
            sb.append(Character.toChars(cp));
            }
        }
        return sb.toString();
    }

Upvotes: 1

Views: 68

Answers (0)

Related Questions