public final class XmppUtils
extends java.lang.Object
Utility class with static factory methods.
Modifier and Type | Method and Description |
---|---|
static java.io.InputStream |
createBranchedInputStream(java.io.InputStream source,
java.io.OutputStream target)
Creates an branched
InputStream , which means that everything read by the source stream is written to the target OutputStream . |
static java.io.OutputStream |
createBranchedOutputStream(java.io.OutputStream out,
java.io.OutputStream branch)
Creates a branched
OutputStream , which means that everything written to the original stream is also written to the branched stream. |
static java.util.concurrent.ThreadFactory |
createNamedThreadFactory(java.lang.String threadName)
Creates a thread factory which creates named daemon threads.
|
static javax.xml.stream.XMLStreamWriter |
createXmppStreamWriter(javax.xml.stream.XMLStreamWriter xmlStreamWriter)
Creates a
XMLStreamWriter instance, which writes XML without namespace prefixes. |
static javax.xml.stream.XMLStreamWriter |
createXmppStreamWriter(javax.xml.stream.XMLStreamWriter xmlStreamWriter,
boolean writeStreamNamepace)
Creates a
XMLStreamWriter instance, which writes XML without namespace prefixes. |
static java.lang.String |
hash(byte[] bytes)
Creates a hex encoded SHA-1 hash.
|
static <T extends java.util.EventObject> |
notifyEventListeners(java.lang.Iterable<java.util.function.Consumer<T>> eventListeners,
T e)
Invokes listeners in a fail-safe way.
|
public static javax.xml.stream.XMLStreamWriter createXmppStreamWriter(javax.xml.stream.XMLStreamWriter xmlStreamWriter, boolean writeStreamNamepace)
Creates a XMLStreamWriter
instance, which writes XML without namespace prefixes.
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>
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.public static javax.xml.stream.XMLStreamWriter createXmppStreamWriter(javax.xml.stream.XMLStreamWriter xmlStreamWriter)
Creates a XMLStreamWriter
instance, which writes XML without namespace prefixes.
xmlStreamWriter
- The underlying XML stream writer.createXmppStreamWriter(XMLStreamWriter, boolean)
public static java.io.InputStream createBranchedInputStream(java.io.InputStream source, java.io.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
.
source
- The source stream.target
- The target stream.public static java.io.OutputStream createBranchedOutputStream(java.io.OutputStream out, java.io.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.
out
- The original stream.branch
- The branched stream.public static java.lang.String hash(byte[] bytes)
Creates a hex encoded SHA-1 hash.
bytes
- The data.public static java.util.concurrent.ThreadFactory createNamedThreadFactory(java.lang.String threadName)
Creates a thread factory which creates named daemon threads.
threadName
- The thread name.public static <T extends java.util.EventObject> void notifyEventListeners(java.lang.Iterable<java.util.function.Consumer<T>> eventListeners, T e)
Invokes listeners in a fail-safe way.
T
- The event object type.eventListeners
- The event listeners.e
- The event object.Copyright © 2014–2019 XMPP.rocks. All rights reserved.