All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rocks.xmpp.util.XmppUtils Maven / Gradle / Ivy

There is a newer version: 0.9.1
Show newest version
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2016 Christian Schudt
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package rocks.xmpp.util;

import javax.xml.bind.DatatypeConverter;
import javax.xml.stream.XMLStreamWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.EventObject;
import java.util.concurrent.ThreadFactory;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Utility class with static factory methods.
 *
 * @author Christian Schudt
 */
public final class XmppUtils {

    private static final Logger logger = Logger.getLogger(XmppUtils.class.getName());

    private XmppUtils() {
    }

    /**
     * Creates a {@link XMLStreamWriter} instance, which writes XML without namespace prefixes.
     * 

Sample usage:

*
     * {@code
     * 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("[email protected]"), Message.Type.CHAT, "Hi!!");
     *
     * Message message = new Message(Jid.of("[email protected]"));
     * message.addExtension(new Sent(new Forwarded(forwardedMessage)));
     *
     * marshaller.marshal(message, xmppStreamWriter);
     * xmppStreamWriter.flush();
     * System.out.println(writer.toString());
     * }
     * 
* The output of this is: *
     * {@code
     * 
     *     
     *         
     *             
     *                 Hi!!
     *             
     *         
     *     
     * 
     * }
     * 
* * @param xmlStreamWriter The underlying XML stream writer. * @param contentNamespace The content namspace, e.g. "jabber:client". * @param 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. * @return The prefix-free canonicalization writer. */ public static XMLStreamWriter createXmppStreamWriter(XMLStreamWriter xmlStreamWriter, String contentNamespace, boolean writeStreamNamepace) { return new PrefixFreeCanonicalizationWriter(xmlStreamWriter, contentNamespace, writeStreamNamepace); } /** * Creates a {@link XMLStreamWriter} instance, which writes XML without namespace prefixes. * * @param xmlStreamWriter The underlying XML stream writer. * @param contentNamespace The content namspace, e.g. "jabber:client". * @return The prefix-free canonicalization writer. * @see #createXmppStreamWriter(XMLStreamWriter, String, boolean) */ public static XMLStreamWriter createXmppStreamWriter(XMLStreamWriter xmlStreamWriter, String contentNamespace) { return createXmppStreamWriter(xmlStreamWriter, contentNamespace, false); } /** * Creates a {@link XMLStreamWriter} instance, which writes XML without namespace prefixes. *

* The content namespace is "jabber:client". * * @param xmlStreamWriter The underlying XML stream writer. * @return The prefix-free canonicalization writer. * @see #createXmppStreamWriter(XMLStreamWriter, String, boolean) */ public static XMLStreamWriter createXmppStreamWriter(XMLStreamWriter xmlStreamWriter) { return createXmppStreamWriter(xmlStreamWriter, "jabber:client"); } /** * Creates an branched {@link java.io.InputStream}, which means that everything read by the source stream is written to the target {@link java.io.OutputStream}. *

This is useful for reading the XMPP stream and writing the inbound XMPP traffic to an {@link java.io.OutputStream}.

* * @param source The source stream. * @param target The target stream. * @return The branched input stream. */ public static InputStream createBranchedInputStream(InputStream source, OutputStream target) { return new BranchedInputStream(source, target); } /** * Creates a branched {@link java.io.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.

* * @param out The original stream. * @param branch The branched stream. * @return The branched output stream. */ public static OutputStream createBranchedOutputStream(OutputStream out, OutputStream branch) { return new BranchedOutputStream(out, branch); } /** * Creates a hex encoded SHA-1 hash. * * @param bytes The data. * @return The hash. */ public static String hash(byte[] bytes) { try { MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); messageDigest.reset(); return DatatypeConverter.printHexBinary(messageDigest.digest(bytes)).toLowerCase(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } /** * Creates a thread factory which creates named daemon threads. * * @param threadName The thread name. * @return The thread factory. */ public static ThreadFactory createNamedThreadFactory(final String threadName) { return r -> { Thread thread = new Thread(r, threadName); thread.setDaemon(true); return thread; }; } /** * Invokes listeners in a fail-safe way. * * @param eventListeners The event listeners. * @param e The event object. * @param The event object type. */ public static void notifyEventListeners(Iterable> eventListeners, T e) { eventListeners.forEach(listener -> { try { listener.accept(e); } catch (Exception ex) { logger.log(Level.WARNING, ex.getMessage(), ex); } }); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy