Class ComposableBody

java.lang.Object
org.igniterealtime.jbosh.AbstractBody
org.igniterealtime.jbosh.ComposableBody

public final class ComposableBody extends AbstractBody
Implementation of the AbstractBody class which allows for the definition of messages from individual elements of a body.

A message is constructed by creating a builder, manipulating the configuration of the builder, and then building it into a class instance, as in the following example:

 ComposableBody body = ComposableBody.builder()
     .setNamespaceDefinition("foo", "http://foo.com/bar")
     .setPayloadXML("invalid input: '<'foo:data>Data to send to remote serverinvalid input: '<'/foo:data>")
     .build();
 
Class instances can also be "rebuilt", allowing them to be used as templates when building many similar messages:
 ComposableBody body2 = body.rebuild()
     .setPayloadXML("invalid input: '<'foo:data>More data to sendinvalid input: '<'/foo:data>")
     .build();
 
This class does only minimal syntactic and semantic checking with respect to what the generated XML will look like. It is up to the developer to protect against the definition of malformed XML messages when building instances of this class.

Instances of this class are immutable and thread-safe.

  • Field Details

    • BOSH_START

      private static final Pattern BOSH_START
      Pattern used to identify the beginning body element of a BOSH message.
    • attrs

      private final Map<BodyQName,String> attrs
      Map of all attributes to their values.
    • payload

      private final String payload
      Payload XML.
    • computed

      private final AtomicReference<String> computed
      Computed raw XML.
  • Constructor Details

    • ComposableBody

      private ComposableBody(Map<BodyQName,String> attrMap, String payloadXML)
      Prevent direct construction. This constructor is for body messages which are dynamically assembled.
  • Method Details

    • fromStaticBody

      static ComposableBody fromStaticBody(StaticBody body) throws BOSHException
      Parse a static body instance into a composable instance. This is an expensive operation and should not be used lightly.

      The current implementation does not obtain the payload XML by means of a proper XML parser. It uses some string pattern searching to find the first @{code body} element and the last element's closing tag. It is assumed that the static body's XML is well formed, etc.. This implementation may change in the future.

      Parameters:
      body - static body instance to convert
      Returns:
      composable body instance
      Throws:
      BOSHException
    • builder

      public static ComposableBody.Builder builder()
      Create a builder instance to build new instances of this class.
      Returns:
      AbstractBody instance
    • rebuild

      public ComposableBody.Builder rebuild()
      If this ComposableBody instance is a dynamic instance, uses this ComposableBody instance as a starting point, create a builder which can be used to create another ComposableBody instance based on this one. This allows a ComposableBody instance to be used as a template. Note that the use of the returned builder in no way modifies or manipulates the current ComposableBody instance.
      Returns:
      builder instance which can be used to build similar ComposableBody instances
    • getAttributes

      public Map<BodyQName,String> getAttributes()
      Get a map of all defined attribute names with their corresponding values.
      Specified by:
      getAttributes in class AbstractBody
      Returns:
      map of qualified attributes
    • toXML

      public String toXML()
      Get an XML String representation of this message.
      Specified by:
      toXML in class AbstractBody
      Returns:
      XML string representing the body message
    • getPayloadXML

      public String getPayloadXML()
      Get the paylaod XML in String form.
      Returns:
      payload XML
    • escape

      private String escape(String value)
      Escape the value of an attribute to ensure we maintain valid XML syntax.
      Parameters:
      value - value to escape
      Returns:
      escaped value
    • computeXML

      private String computeXML()
      Generate a String representation of the message body.
      Returns:
      XML string representation of the body