title: Proper way to decode response in Dojo cometd java client
body: <p>I want to push arbitrary XMLEncoded java objects back and forth using Dojo cometd channels, and I have a problem decoding the payload properly.</p>
  
  <p>In order to do this I have this send method in a stripped down version of the chat room client demonstration program:</p>
  
  <pre><code>private void send(String string) {
    Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
    map.put("intArray", new int[] {1});
  
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLEncoder xmlEncoder = new XMLEncoder(baos);
    xmlEncoder.writeObject(map);
    xmlEncoder.close();
    Map&lt;String, Object&gt; encodedMap = new HashMap&lt;String, Object&gt;();
    try {
      String encoded = baos.toString("UTF-8");
      encodedMap.put("xmlpayload", encoded);
    } catch (Exception e) {
     throw new RuntimeException("could not use UTF-8", e);
    } 
    bayreuxClient.publish("/prototype/a", encodedMap, String.valueOf(System.currentTimeMillis()));
  }
  </code></pre>
  
  <p>Which for now creates a XML snippet flattened to an UTF-8 encoded byte stream (I am aware I need to take care of the encoding too, but that is not the issue now).</p>
  
  <p>The message listener accepting this looks like:</p>
  
  <pre><code>listener = new MessageListener() {
    @Override
    public void deliver(Client fromClient, Client toClient, Message msg) {
      if (!_connected) {
        _connected = true;
        synchronized (this) {
        this.notify();
      }
    }
    Object data = msg.getData();
    if (data instanceof Map) {
      Map map = (Map) data;
      Object rawPayload = map.get("xmlpayload");
      if (rawPayload instanceof String) {
        System.out.println("xmlpayload = " + rawPayload);
        ByteArrayInputStream bais;
        try {
          String xmlPayload = ((String) rawPayload).replaceAll("&amp;gt;","&gt;").replaceAll("&amp;lt;", "&lt;").replaceAll("&amp;amp;","&amp;");
          bais = new ByteArrayInputStream(xmlPayload.getBytes("UTF-8"));
          XMLDecoder xmlDecoder = new XMLDecoder(bais);
          Object o = xmlDecoder.readObject();
          xmlDecoder.close();
          System.out.println("&gt;&gt; decoded payload=" + o + ", class=" + o.getClass());
        } catch (UnsupportedEncodingException e) {
          throw new RuntimeException("no UTF-8", e);
        }
       }
     }
    }
   };
   address = new Address("localhost", 8080);
   bayreuxClient = new BayeuxClient(httpClient, address, "/cometd/cometd");
   bayreuxClient.addListener(listener);
  </code></pre>
  
  <p>As you can see I have found by trial and error that the string sent has its less than, greater than, and ampersand characters protected which I then unprotect and call XMLDecode on.</p>
  
  <p>The output is:</p>
  
  <pre><code>xmlpayload = &amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt; 
  &amp;lt;java version="1.6.0_16" class="java.beans.XMLDecoder"&amp;gt; 
   &amp;lt;object class="java.util.HashMap"&amp;gt; 
    &amp;lt;void method="put"&amp;gt; 
     &amp;lt;string&amp;gt;intArray&amp;lt;/string&amp;gt; 
     &amp;lt;array class="int" length="1"&amp;gt; 
      &amp;lt;void index="0"&amp;gt; 
       &amp;lt;int&amp;gt;1&amp;lt;/int&amp;gt; 
      &amp;lt;/void&amp;gt; 
     &amp;lt;/array&amp;gt; 
    &amp;lt;/void&amp;gt; 
   &amp;lt;/object&amp;gt; 
  &amp;lt;/java&amp;gt; 
  
  &gt;&gt; decoded payload={intArray=[I@2f1e75}, class=class java.util.HashMap
  </code></pre>
  
  <p>so it is reversible.  My experiments with putting these characters inside string objects being XMLEncoded/XMLDecoded were not good - they were not double proctected - so my brute decoding also affected them.</p>
  
  <p>The question is:  What is the <em>proper</em> way to decode these strings?  Is there a JSON library I need to use instead of my quick and dirty code?  Might I have hit an error in the Java client library for cometd?</p>
  
  <p>(Note:  I have been using the cometd-1.0.0rc0 libraries for this experiment).</p>
  
  <hr>
  
  <p>EDIT: It turned out that the communication happened through another deployment in the same webserver than the one I <em>thought</em> took care of the cometd communications, and <em>that</em> deployment contained filter.json which did the XML protection.   </p>
  
  <p><a href="http://groups.google.com/group/cometd-users/browse_thread/thread/6fbdaae669e5f9d3" rel="nofollow noreferrer">http://groups.google.com/group/cometd-users/browse_thread/thread/6fbdaae669e5f9d3</a></p>
  
  <p>In other words a configuration issue outside my code.  Hopefully a revised deployment will remedy this issue.</p>