Class XmlCodec

java.lang.Object
org.acemq.amqp.codec.xml.XmlCodec
All Implemented Interfaces:
Codec

public final class XmlCodec extends Object implements Codec
Reads and writes XML.

Here because most estates have something that speaks XML and will not be rewritten, and a messaging library that cannot talk to it forces a translation layer nobody wants to own. New services should publish JSON; this exists so that the ones that cannot are not a special case.

Also the second implementation of CodecProvider, which is the point. One implementation never proves a seam: it proves that a class compiles against an interface written around it. Two, in separate modules, discovered the same way, is what shows that a third can be added by somebody who does not work on this library.

External entities are disabled. An XML parser that resolves them will read files off the machine and open connections on behalf of whoever sent the message, and a queue is exactly the sort of place a message from somewhere unexpected arrives. This is not configurable, because the configuration would only ever be wrong.

  • Constructor Details

    • XmlCodec

      public XmlCodec()
      Uses a mapper that ignores unknown elements and refuses external entities.
    • XmlCodec

      public XmlCodec(com.fasterxml.jackson.dataformat.xml.XmlMapper mapper)
      Parameters:
      mapper - the mapper to use; disable external entity resolution on its factory, or a message from anywhere can read files off the machine it is handled on
  • Method Details

    • defaultMapper

      public static com.fasterxml.jackson.dataformat.xml.XmlMapper defaultMapper()
      Returns:
      a mapper that tolerates unknown elements and cannot be talked into resolving external entities
    • contentType

      public String contentType()
      Specified by:
      contentType in interface Codec
      Returns:
      the content type this codec produces, for example application/json
    • encode

      public byte[] encode(Object payload)
      Specified by:
      encode in interface Codec
      Parameters:
      payload - object to encode; never null
      Returns:
      encoded bytes
    • decode

      public <T> T decode(byte[] body, Class<T> target)
      Specified by:
      decode in interface Codec
      Type Parameters:
      T - target type
      Parameters:
      body - encoded bytes
      target - type to produce
      Returns:
      the decoded payload
    • canDecode

      public boolean canDecode(@Nullable String contentType)
      Specified by:
      canDecode in interface Codec
      Parameters:
      contentType - content type of a received message, or null when the sender did not set one
      Returns:
      whether this codec can decode it
    • toString

      public String toString()
      Overrides:
      toString in class Object