Class XmppUtils


  • public final class XmppUtils
    extends Object
    Utility class with static factory methods.
    • Method Detail

      • createXmppStreamWriter

        public static XMLStreamWriter createXmppStreamWriter​(XMLStreamWriter xmlStreamWriter,
                                                             boolean writeStreamNamepace)
        Creates a XMLStreamWriter instance, which writes XML without namespace prefixes.

        Usage

        
         Writer writer = new StringWriter();
        
         XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newFactory().createXMLStreamWriter(writer);
         XMLStreamWriter xmppStreamWriter = XmppUtils.createXmppStreamWriter(xmlStreamWriter, true);
        
         JAXBContext jaxbContext = JAXBContext.newInstance(Message.class, Sent.class);
         Marshaller marshaller = jaxbContext.createMarshaller();
         marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        
         Message forwardedMessage = new Message(Jid.of("romeo@example.net"), Message.Type.CHAT, "Hi!!");
        
         Message message = new Message(Jid.of("juliet@example.net"));
         message.addExtension(new Sent(new Forwarded(forwardedMessage)));
        
         marshaller.marshal(message, xmppStreamWriter);
         xmppStreamWriter.flush();
         System.out.println(writer.toString());
         

        The output of this is:

        
         <message to="juliet@example.net">
             <sent xmlns="urn:xmpp:carbons:2">
                 <forwarded xmlns="urn:xmpp:forward:0">
                     <message xmlns="jabber:client" to="romeo@example.net" type="chat">
                         <body>Hi!!</body>
                     </message>
                 </forwarded>
             </sent>
         </message>
         
        Parameters:
        xmlStreamWriter - The underlying XML stream writer.
        writeStreamNamepace - If the stream namespace ('http://etherx.jabber.org/streams') should be written to the root element. This is usually only the case when writing the initial BOSH response with stream features.
        Returns:
        The prefix-free canonicalization writer.
      • createBranchedInputStream

        public static InputStream createBranchedInputStream​(InputStream source,
                                                            OutputStream target)
        Creates an branched InputStream, which means that everything read by the source stream is written to the target OutputStream.

        This is useful for reading the XMPP stream and writing the inbound XMPP traffic to an OutputStream.

        Parameters:
        source - The source stream.
        target - The target stream.
        Returns:
        The branched input stream.
      • createBranchedOutputStream

        public static OutputStream createBranchedOutputStream​(OutputStream out,
                                                              OutputStream branch)
        Creates a branched OutputStream, which means that everything written to the original stream is also written to the branched stream.

        This is useful for writing the outbound XMPP traffic to another stream.

        Parameters:
        out - The original stream.
        branch - The branched stream.
        Returns:
        The branched output stream.
      • newBranchedReader

        public static Reader newBranchedReader​(Reader reader,
                                               Writer branch)
        Creates a new branched Reader, which writes every character read from the given reader to the writer.
        Parameters:
        reader - The original reader
        branch - The branched writer.
        Returns:
        The branched reader.
      • newBranchedWriter

        public static Writer newBranchedWriter​(Writer writer,
                                               Writer branch)
        Creates a new branched Writer, which writes every character written to the first writer to the second writer.
        Parameters:
        writer - The original writer.
        branch - The branched writer.
        Returns:
        The branched writer.
      • hash

        public static String hash​(byte[] bytes)
        Creates a hex encoded SHA-1 hash.
        Parameters:
        bytes - The data.
        Returns:
        The hash.
      • createNamedThreadFactory

        public static ThreadFactory createNamedThreadFactory​(String threadName)
        Creates a thread factory which creates named daemon threads.
        Parameters:
        threadName - The thread name.
        Returns:
        The thread factory.
      • notifyEventListeners

        public static <T extends EventObject> void notifyEventListeners​(Iterable<Consumer<T>> eventListeners,
                                                                        T e)
        Invokes listeners in a fail-safe way.
        Type Parameters:
        T - The event object type.
        Parameters:
        eventListeners - The event listeners.
        e - The event object.
      • createContext

        public static javax.xml.bind.JAXBContext createContext​(Iterable<Class<?>> additionalClasses)
        Creates a JAXBContext by loading all XmppContext implementations on the classpath. XmppContexts are found by using ServiceLoader.
        Parameters:
        additionalClasses - The additional classes to be bound to the context.
        Returns:
        The JAXBContext
        See Also:
        ServiceLoader, XmppContext