KaiG - 2015-08-05

I wanted to create a ticket for this but it seems that I don't have permissions for doing so. I've therefore created a new ticket. It would be great if the MockServlet would use the encoding of the mock response for returning the response to the client. I've implemented a small change that works for me. See below for the diff.

Index: MockServlet.java
===================================================================
--- MockServlet.java    (revision 59919)
+++ MockServlet.java    (working copy)
@@ -13,11 +13,14 @@
 package net.sf.sripathi.ws.mock.servlet;

 import java.io.InputStream;
+import java.io.StringReader;

 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamReader;

 import net.sf.sripathi.ws.mock.Domain;
 import net.sf.sripathi.ws.mock.DomainFactory;
@@ -137,7 +140,17 @@
                resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            }

-           resp.getOutputStream().write(soapResp.getBytes());
+           try
+         {
+            XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(soapResp));
+            // looks like we got a valid xml, we'll use the encoding of the xml as the encoding to return
+            resp.getOutputStream().write(soapResp.getBytes(xmlStreamReader.getCharacterEncodingScheme()));
+         }
+         catch( Error e )
+         {
+            // ignore - we are just returning the response as is
+            resp.getOutputStream().write(soapResp.getBytes());
+         }
        }
        catch (Exception e) {
            e.printStackTrace();

If your mock response contains the "<?xml version="1.0" encoding="UTF-8"?>" tag the content will now be returned as UTF-8

 

Last edit: KaiG 2015-08-05