Package rocks.xmpp.util
Class XmppUtils
- java.lang.Object
-
- rocks.xmpp.util.XmppUtils
-
public final class XmppUtils extends Object
Utility class with static factory methods.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static InputStreamcreateBranchedInputStream(InputStream source, OutputStream target)Creates an branchedInputStream, which means that everything read by the source stream is written to the targetOutputStream.static OutputStreamcreateBranchedOutputStream(OutputStream out, OutputStream branch)Creates a branchedOutputStream, which means that everything written to the original stream is also written to the branched stream.static javax.xml.bind.JAXBContextcreateContext(Iterable<Class<?>> additionalClasses)Creates aJAXBContextby loading allXmppContextimplementations on the classpath.static ThreadFactorycreateNamedThreadFactory(String threadName)Creates a thread factory which creates named daemon threads.static XMLStreamWritercreateXmppStreamWriter(XMLStreamWriter xmlStreamWriter)Creates aXMLStreamWriterinstance, which writes XML without namespace prefixes.static XMLStreamWritercreateXmppStreamWriter(XMLStreamWriter xmlStreamWriter, boolean writeStreamNamepace)Creates aXMLStreamWriterinstance, which writes XML without namespace prefixes.static Stringhash(byte[] bytes)Creates a hex encoded SHA-1 hash.static ReadernewBranchedReader(Reader reader, Writer branch)Creates a new branchedReader, which writes every character read from the given reader to the writer.static WriternewBranchedWriter(Writer writer, Writer branch)Creates a new branchedWriter, which writes every character written to the first writer to the second writer.static <T extends EventObject>
voidnotifyEventListeners(Iterable<Consumer<T>> eventListeners, T e)Invokes listeners in a fail-safe way.
-
-
-
Method Detail
-
createXmppStreamWriter
public static XMLStreamWriter createXmppStreamWriter(XMLStreamWriter xmlStreamWriter, boolean writeStreamNamepace)
Creates aXMLStreamWriterinstance, 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.
-
createXmppStreamWriter
public static XMLStreamWriter createXmppStreamWriter(XMLStreamWriter xmlStreamWriter)
Creates aXMLStreamWriterinstance, which writes XML without namespace prefixes.- Parameters:
xmlStreamWriter- The underlying XML stream writer.- Returns:
- The prefix-free canonicalization writer.
- See Also:
createXmppStreamWriter(XMLStreamWriter, boolean)
-
createBranchedInputStream
public static InputStream createBranchedInputStream(InputStream source, OutputStream target)
Creates an branchedInputStream, which means that everything read by the source stream is written to the targetOutputStream.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 branchedOutputStream, 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 branchedReader, which writes every character read from the given reader to the writer.- Parameters:
reader- The original readerbranch- The branched writer.- Returns:
- The branched reader.
-
newBranchedWriter
public static Writer newBranchedWriter(Writer writer, Writer branch)
Creates a new branchedWriter, 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 aJAXBContextby loading allXmppContextimplementations on the classpath.XmppContexts are found by usingServiceLoader.- Parameters:
additionalClasses- The additional classes to be bound to the context.- Returns:
- The JAXBContext
- See Also:
ServiceLoader,XmppContext
-
-