Search code examples
javaxmlgroovyexchangewebservices

EWS SOAP request fails on updateItem if Category string has accented chars


I would like to know how can I post the below request via a java/groovy script:

SOAP Request sent (size: 1232)

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <RequestServerVersion xmlns="http://schemas.microsoft.com/exchange/services/2006/types" Version="Exchange2013_SP1"></RequestServerVersion>
  </soap:Header>
  <soap:Body>
    <UpdateItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages" MessageDisposition="SaveOnly" ConflictResolution="AutoResolve">
      <ItemChanges>
        <t:ItemChange xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
          <t:ItemId Id="AAMkADVmY2M3YTNmLTRmNTAtNDgxOS05N2ZhLWMyNTc0YWZlMDhlNwBGAAAAAAAQBgKUNgxZT6CYzuo2SsPlBwDW6sia8LrPRrmNln3i877OAAAAAAEMAAAT3G5bCC7PSJ3ZbmUh540OAACBI4lIAAA=" ChangeKey="CQAAABYAAAAT3G5bCC7PSJ3ZbmUh540OAACBK17k"></t:ItemId>
          <t:Updates>
            <t:SetItemField>
              <t:FieldURI FieldURI="item:Categories"></t:FieldURI>
              <t:Message>
                <t:Categories>
                  <t:String>ÁÉÍÓÖÚÜ</t:String>
                </t:Categories>
              </t:Message>
            </t:SetItemField>
          </t:Updates>
        </t:ItemChange>
      </ItemChanges>
    </UpdateItem>
  </soap:Body>
</soap:Envelope>

HTTP/1.1 500 Internal Server Error

  Server: Microsoft-IIS/7.5.
  request-id: 9baa3ecb-99c1-42a9-98b6-1b5b74af4c9d.
  X-AspNet-Version: 4.0.30319.
  Cache-Control: private.
  Content-Type: text/xml; charset=utf-8.
  Date: Fri, 11 Dec 2020 01:49:20 GMT.
  Transfer-Encoding: chunked.
  Persistent-Auth: true.
  Connection: close.
  Set-Cookie: exchangecookie=7fe34835e0d84ccaaa4bafdbd70b424a; expires=Sat, 11-Dec-2021 01:49:20 GMT; path=/; HttpOnly.
  Set-Cookie: X-BackEndCookie=S-1-5-21-1275210071-2000478354-682003330-2272537=u56Lnp2ejJqBnZmdyZ7MyM/SmcvIyNLLysaa0p6dm5zSyMnIzs7GyJmeyJyagYHNz83O0s/O0s7Pq8/OxcvGxc3P; expires=Sun, 10-Jan-2021 01:49:20 GMT; path=/ews; secure; HttpOnly.
  X-Powered-By: ASP.NET.

Same request works fine using SOAPe. Same request passess thru fine when sent with no special accented chars. I tried encoding chars to no avail.


Solution

  • Found this which works:

    static String escapeSpecialChars(String inputData){ 
        def value = inputData.toString();
        Pattern p = Pattern.compile('[^#|()-_&\'"!. A-Za-z0-9]');
        Matcher m = p.matcher(value);
        boolean b = m.find();
         if (b == true){
    
                def input = value;
                def output=''
                for(int l=0;l<input.length();l++){
                    char temp = input.getAt(l);
                    int ascii = temp
                    boolean check = p.matcher(temp.toString());
                    if(check == true){
                     output += '&#'+ascii+';';
                     println '&#'+ascii+';'
                    }else{
                        output += temp;
                    }
               
                }
                value = output;
        }
        inputData = value;
        return inputData; }
    

    but a more elegant solution might be wrapping the categories in a CDATA like so:

    cat = cat ==~ /[#|()-_&\'\"!. A-Za-z0-9]*/ ? "$cat" : "<![CDATA[$cat]]>"