Search code examples
jsonrestjakarta-eeapache-wink

JSON parsing in JAVA problem with Control Characters


I'm receiving via HTTP a JSON petition. When coming from Internet Explorer 8 parsing fails with the exception:

InPart inPart = mp.next();

MyClass myClass = inPart.getBody(MyClass.class, null);

com.ctc.wstx.exc.WstxUnexpectedCharException: Illegal character ((CTRL-CHAR, code 12))    at [row,col {unknown-source}]: [1,101]]

The relevant code:

InPart inPart = mp.next();

String s = inPart.getBody(String.class, null);

providers.getMessageBodyReader(MyClass.class, null, null,
            MediaType.APPLICATION_JSON_TYPE).readFrom(MyClass.class, null, null,
            MediaType.APPLICATION_JSON_TYPE, headers.getRequestHeaders(),
                new ByteArrayInputStream(s.getBytes())); // Tried with s.getBytes("UTF-8")

com.ctc.wstx.exc.WstxUnexpectedCharException: Illegal character ((CTRL-CHAR, code 12)) at [row,col {unknown-source}]: [1,101]

Furthermore if I do:

String ss = s.replaceAll("\\p{Cntrl}", "");

ss.equals(s); // 

Outputs a true

lengths are the same.

I also tried:

private String removeControlChar(String in) {
    StringBuilder sb = new StringBuilder();
    for (char c : in.toCharArray())
    {
        if(!Character.isISOControl(c)) {
            sb.append(c);
        }
        else
        {
            // To delete
            int i = 0;
        }
    }
    return sb.toString();
}

InPart inPart = mp.next();

String s = inPart.getBody(String.class, null);
Strign ss = removeControlChar(s);
providers.getMessageBodyReader(MyClass.class, null, null,
            MediaType.APPLICATION_JSON_TYPE).readFrom(MyClass.class, null, null,
            MediaType.APPLICATION_JSON_TYPE, headers.getRequestHeaders(),
                new ByteArrayInputStream(ss.getBytes())); // Tried with s.getBytes("UTF-8")

If I debug, the character which fails is a \f as stated in the exception. But the error says it is an invalid XML character. Might this be the problem?

Any Ideas? This only seems to affect Internet Explorer.

Thank you.


Solution

  • I don't delete this question just in case anyone fall in the same trouble.

    The problem is the browser sending the path like C:\fakepath\ which in execution time, and after converting to XML it states \f .

    You can set up this behavior in you browser.

    Regards.