Example usage for org.jdom2 Element Element

List of usage examples for org.jdom2 Element Element

Introduction

In this page you can find the example usage for org.jdom2 Element Element.

Prototype

public Element(final String name) 

Source Link

Document

Create a new element with the supplied (local) name and no namespace.

Usage

From source file:com.kixeye.kixmpp.client.module.muc.MucKixmppClientModule.java

License:Apache License

/**
 * Joins a room./*from  w  w w . j a  v  a2s .  co  m*/
 * 
 * @param roomJid
 * @param nickname
 */
public void joinRoom(KixmppJid roomJid, String nickname, Integer maxStanzas, Integer maxChars, Integer seconds,
        DateTime since) {
    Element presence = new Element("presence");
    presence.setAttribute("from", client.getJid().getFullJid());
    presence.setAttribute("to", roomJid + "/" + nickname);

    Element x = new Element("x", "http://jabber.org/protocol/muc");
    presence.addContent(x);

    Element history = new Element("history", "http://jabber.org/protocol/muc");
    if (maxStanzas != null) {
        history.setAttribute("maxstanzas", maxStanzas.toString());
    }
    if (maxChars != null) {
        history.setAttribute("maxchars", maxChars.toString());
    }
    if (seconds != null) {
        history.setAttribute("seconds", seconds.toString());
    }
    if (since != null) {
        history.setAttribute("since", XmppDateUtils.format(since));
    }
    x.addContent(history);

    client.sendStanza(presence);
}

From source file:com.kixeye.kixmpp.client.module.muc.MucKixmppClientModule.java

License:Apache License

/**
 * Sends a room message to a room./* ww  w. jav a2s  .  co  m*/
 * 
 * @param roomJid
 * @param roomMessage
 */
public void sendRoomMessage(KixmppJid roomJid, String roomMessage, String nickname) {
    Element message = new Element("message");
    message.setAttribute("from", client.getJid().toString());
    message.setAttribute("to", roomJid.toString());
    message.setAttribute("type", "groupchat");

    Element bodyElement = new Element("body", message.getNamespace());
    bodyElement.setText(roomMessage);
    message.addContent(bodyElement);

    client.sendStanza(message);
}

From source file:com.kixeye.kixmpp.client.module.presence.PresenceKixmppClientModule.java

License:Apache License

/**
 * Updates the current user's presence./*from www  .j  av a  2  s  . co m*/
 *     
 * @param presence
 */
public void updatePresence(Presence presence) {
    Element presenceElement = new Element("presence");

    if (presence.getType() != null) {
        presenceElement.setAttribute("type", presence.getType());
    }

    if (presence.getStatus() != null) {
        Element statusElement = new Element("status");
        statusElement.setText(presence.getStatus());

        presenceElement.addContent(statusElement);
    }

    if (presence.getShow() != null) {
        Element showElement = new Element("show");
        showElement.setText(presence.getShow());

        presenceElement.addContent(showElement);
    }

    client.sendStanza(presenceElement);
}

From source file:com.kixeye.kixmpp.server.cluster.message.PrivateChatTask.java

License:Apache License

/**
 * @see org.fusesource.hawtdispatch.Task#run()
 *///from   www .j  av  a 2  s.  co  m
public void run() {
    KixmppJid fromJid = KixmppJid.fromRawJid(this.fromJid);
    KixmppJid toJid = KixmppJid.fromRawJid(this.toJid);
    KixmppServer server = getKixmppServer();

    // broadcast message stanza to all channels of the recipient
    for (Channel toChannel : server.getChannels(toJid.getNode())) {
        Element messageElement = new Element("message");
        messageElement.setAttribute("type", "chat");
        messageElement.setAttribute("from", fromJid.getFullJid());
        messageElement.setAttribute("to", toJid.getFullJid());

        Element bodyElement = new Element("body");
        bodyElement.setText(body);

        messageElement.addContent(bodyElement);

        toChannel.writeAndFlush(messageElement);
    }

    // broadcast message stanza to all channels of the sender
    // except for the channel it was sent from
    for (Channel fromChannel : server.getChannels(fromJid.getNode())) {
        // skip the channel message was sent from
        if (fromChannel.attr(BindKixmppServerModule.JID).get().toString()
                .equalsIgnoreCase(fromJid.toString())) {
            continue;
        }
        Element messageElement = new Element("message");
        messageElement.setAttribute("type", "chat");
        messageElement.setAttribute("from", fromJid.getFullJid());
        messageElement.setAttribute("to", toJid.getFullJid());

        Element bodyElement = new Element("body");
        bodyElement.setText(body);

        messageElement.addContent(bodyElement);

        fromChannel.writeAndFlush(messageElement);
    }
}

From source file:com.kixeye.kixmpp.server.module.muc.DefaultMucRoomEventHandler.java

License:Apache License

private Element createMessage(String id, KixmppJid from, KixmppJid to, String type, String bodyText) {
    Element message = new Element("message");

    message.setAttribute("to", to.getFullJid());
    message.setAttribute("from", from.getFullJid());
    message.setAttribute("type", type);
    message.setAttribute("id", id);

    Element body = new Element("body");
    body.addContent(bodyText);/*from   w w w .  j  a  v a 2  s.co m*/

    message.addContent(body);

    return message;
}

From source file:com.kixeye.kixmpp.server.module.muc.MucRoom.java

License:Apache License

/**
 * A user requests to join the room.//w  w  w  .  j  a v a 2s  .  c  om
 *
 * @param channel
 * @param nickname
 * @param mucStanza
 */
public void join(final Channel channel, String nickname, Element mucStanza) {
    KixmppJid jid = channel.attr(BindKixmppServerModule.JID).get();

    if (settings.isOpen() && !jidRoles.containsKey(jid.withoutResource())) {
        addUser(jid, nickname, MucRole.Participant, MucAffiliation.Member);
    }

    verifyMembership(jid.withoutResource());
    checkForNicknameInUse(nickname, jid);

    User user = usersByNickname.get(nickname);

    boolean existingUser = true;
    if (user == null) {
        user = new User(nickname, jid.withoutResource());
        usersByNickname.put(nickname, user);
        MucRoomEventHandler handler = service.getServer().getMucRoomEventHandler();
        if (handler != null) {
            handler.userAdded(this, user);
        }
        existingUser = false;
    }

    Client client = user.addClient(new Client(jid, nickname, channel));

    // xep-0045 7.2.3 begin
    // self presence
    KixmppJid fromRoomJid = roomJid.withResource(nickname);
    channel.writeAndFlush(createPresence(fromRoomJid, jid, MucRole.Participant, null));

    if (settings.isPresenceEnabled() && !existingUser) {
        // Send presence from existing occupants to new occupant
        sendExistingOccupantsPresenceToNewOccupant(user, channel);

        // Send new occupant's presence to all occupants
        broadcastPresence(fromRoomJid, MucRole.Participant, null);
    }
    // xep-0045 7.2.3 end

    if (settings.getSubject() != null) {
        Element message = new Element("message");
        message.setAttribute("id", UUID.randomUUID().toString());
        message.setAttribute("from", roomJid.withResource(nickname).toString());
        message.setAttribute("to", channel.attr(BindKixmppServerModule.JID).get().toString());
        message.setAttribute("type", "groupchat");

        message.addContent(new Element("subject").setText(settings.getSubject()));

        channel.writeAndFlush(message);
    }

    if (mucStanza != null) {
        Element history = mucStanza.getChild("history", mucStanza.getNamespace());

        if (history != null) {
            MucHistoryProvider historyProvider = mucModule.getHistoryProvider();

            if (historyProvider != null) {
                Integer maxChars = null;
                Integer maxStanzas = null;
                Integer seconds = null;

                String parsableString = history.getAttributeValue("maxchars");
                if (parsableString != null) {
                    try {
                        maxChars = Integer.parseInt(parsableString);
                    } catch (Exception e) {
                    }
                }
                parsableString = history.getAttributeValue("maxstanzas");
                if (parsableString != null) {
                    try {
                        maxStanzas = Integer.parseInt(parsableString);
                    } catch (Exception e) {
                    }
                }
                parsableString = history.getAttributeValue("seconds");
                if (parsableString != null) {
                    try {
                        seconds = Integer.parseInt(parsableString);
                    } catch (Exception e) {
                    }
                }

                String since = history.getAttributeValue("since");

                historyProvider.getHistory(roomJid, user.getBareJid(), maxChars, maxStanzas, seconds, since)
                        .addListener(new GenericFutureListener<Future<List<MucHistory>>>() {
                            @Override
                            public void operationComplete(Future<List<MucHistory>> future) throws Exception {
                                if (future.isSuccess()) {
                                    List<MucHistory> historyItems = future.get();
                                    if (historyItems != null) {
                                        for (MucHistory historyItem : historyItems) {
                                            Element message = new Element("message")
                                                    .setAttribute("id", UUID.randomUUID().toString())
                                                    .setAttribute("from",
                                                            roomJid.withResource(historyItem.getNickname())
                                                                    .toString())
                                                    .setAttribute("to",
                                                            channel.attr(BindKixmppServerModule.JID).get()
                                                                    .toString())
                                                    .setAttribute("type", "groupchat");
                                            message.addContent(
                                                    new Element("body").setText(historyItem.getBody()));

                                            Element addresses = new Element("addresses", Namespace
                                                    .getNamespace("http://jabber.org/protocol/address"));
                                            addresses
                                                    .addContent(new Element("address", addresses.getNamespace())
                                                            .setAttribute("type", "ofrom").setAttribute("jid",
                                                                    historyItem.getFrom().toString()));
                                            message.addContent(addresses);

                                            message.addContent(new Element("delay",
                                                    Namespace.getNamespace("urn:xmpp:delay"))
                                                            .setAttribute("from", roomJid.toString())
                                                            .setAttribute("stamp", XmppDateUtils
                                                                    .format(historyItem.getTimestamp())));

                                            channel.write(message);
                                        }
                                        channel.flush();
                                    }
                                }
                            }
                        });
            }
        }
    }

    channel.closeFuture().addListener(new CloseChannelListener(client));
}

From source file:com.kixeye.kixmpp.server.module.muc.MucRoom.java

License:Apache License

private Element createPresence(KixmppJid from, KixmppJid to, MucRole role, String type) {
    Element presence = new Element("presence");

    presence.setAttribute("id", UUID.randomUUID().toString());
    presence.setAttribute("from", from.toString());
    presence.setAttribute("to", to.toString());

    Element x = new Element("x", Namespace.getNamespace("http://jabber.org/protocol/muc#user"));

    if (type != null) {
        presence.setAttribute("type", type);
    }// w w  w .  j  a v a 2 s  .  c  om

    Element item = new Element("item", Namespace.getNamespace("http://jabber.org/protocol/muc#user"));
    item.setAttribute("affiliation", "member");

    switch (role) {
    case Participant:
        item.setAttribute("role", "participant");
        break;
    case Moderator:
        item.setAttribute("role", "moderator");
        break;
    case Visitor:
        item.setAttribute("role", "visitor");
        break;
    }
    x.addContent(item);
    presence.addContent(x);

    return presence;
}

From source file:com.kixeye.kixmpp.server.module.muc.MucRoom.java

License:Apache License

/**
 * Sends an direct invitation to a user.  Note: The smack client does not recognize this as a valid room invite.
 *//* ww  w  .j av  a  2 s . com*/
public void sendDirectInvite(KixmppJid from, Channel userChannelToInvite, String reason) {
    Element message = new Element("message");
    message.setAttribute("to", userChannelToInvite.attr(BindKixmppServerModule.JID).get().getFullJid());
    if (from != null) {
        message.setAttribute("from", from.getFullJid());
    }

    Element x = new Element("x", Namespace.getNamespace("jabber:x:conference"));
    x.setAttribute("jid", roomJid.getFullJid());
    if (reason != null) {
        x.setAttribute("reason", reason);
    }

    message.addContent(x);

    userChannelToInvite.writeAndFlush(message);
}

From source file:com.kixeye.kixmpp.server.module.muc.MucRoom.java

License:Apache License

/**
 * Sends a mediated invitation to a user.
 *///from  www.  j a v a  2s  .  co  m
public void sendMediatedInvite(KixmppJid from, Channel userChannelToInvite, String reason) {

    Element invite = new Element("invite");
    invite.setAttribute("from", from.getFullJid());
    if (reason != null) {
        Element el = new Element("reason");
        el.setText(reason);
        invite.addContent(el);
    }

    Element x = new Element("x", Namespace.getNamespace("http://jabber.org/protocol/muc#user"));
    x.addContent(invite);

    Element message = new Element("message");
    message.setAttribute("to", userChannelToInvite.attr(BindKixmppServerModule.JID).get().getFullJid());
    message.setAttribute("from", roomJid.getFullJid());
    message.addContent(x);

    userChannelToInvite.writeAndFlush(message);
}

From source file:com.lapis.jsfexporter.xml.XMLExportType.java

License:Apache License

public XMLExportType() {
    rootElement = new Element("export");
    document = new Document(rootElement);
}