Class 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("Data to send to remote server")
         .build();
     
    Class instances can also be "rebuilt", allowing them to be used as templates when building many similar messages:
     ComposableBody body2 = body.rebuild()
         .setPayloadXML("More data to send")
         .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.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  ComposableBody.Builder
      Class instance builder, after the builder pattern.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map<BodyQName,​java.lang.String> attrs
      Map of all attributes to their values.
      private static java.util.regex.Pattern BOSH_START
      Pattern used to identify the beginning body element of a BOSH message.
      private java.util.concurrent.atomic.AtomicReference<java.lang.String> computed
      Computed raw XML.
      private java.lang.String payload
      Payload XML.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ComposableBody​(java.util.Map<BodyQName,​java.lang.String> attrMap, java.lang.String payloadXML)
      Prevent direct construction.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static ComposableBody.Builder builder()
      Create a builder instance to build new instances of this class.
      private java.lang.String computeXML()
      Generate a String representation of the message body.
      private java.lang.String escape​(java.lang.String value)
      Escape the value of an attribute to ensure we maintain valid XML syntax.
      (package private) static ComposableBody fromStaticBody​(StaticBody body)
      Parse a static body instance into a composable instance.
      java.util.Map<BodyQName,​java.lang.String> getAttributes()
      Get a map of all defined attribute names with their corresponding values.
      java.lang.String getPayloadXML()
      Get the paylaod XML in String form.
      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.
      java.lang.String toXML()
      Get an XML String representation of this message.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • BOSH_START

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

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

        private final java.lang.String payload
        Payload XML.
      • computed

        private final java.util.concurrent.atomic.AtomicReference<java.lang.String> computed
        Computed raw XML.
    • Constructor Detail

      • ComposableBody

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

      • 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 java.util.Map<BodyQName,​java.lang.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 java.lang.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 java.lang.String getPayloadXML()
        Get the paylaod XML in String form.
        Returns:
        payload XML
      • escape

        private java.lang.String escape​(java.lang.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 java.lang.String computeXML()
        Generate a String representation of the message body.
        Returns:
        XML string representation of the body