mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-18 02:02:04 +01:00
Importing.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@1777 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
d025187016
commit
f3837581f8
25 changed files with 4522 additions and 0 deletions
BIN
build/lib/xpp.jar
Normal file
BIN
build/lib/xpp.jar
Normal file
Binary file not shown.
230
source/org/jivesoftware/smack/Chat.java
Normal file
230
source/org/jivesoftware/smack/Chat.java
Normal file
|
@ -0,0 +1,230 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack;
|
||||
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smack.filter.ThreadFilter;
|
||||
|
||||
/**
|
||||
* A chat is a series of messages sent between two users. Each chat has
|
||||
* a unique ID, which is used to track which messages are part of the chat.
|
||||
*
|
||||
* @see XMPPConnection#createChat(String)
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class Chat {
|
||||
|
||||
/**
|
||||
* A prefix helps to make sure that ID's are unique across mutliple instances.
|
||||
*/
|
||||
private static String prefix = StringUtils.randomString(3);
|
||||
|
||||
/**
|
||||
* Keeps track of the current increment, which is appended to the prefix to
|
||||
* forum a unique ID.
|
||||
*/
|
||||
private static long id = 0;
|
||||
|
||||
/**
|
||||
* Returns the next unique id. Each id made up of a short alphanumeric
|
||||
* prefix along with a unique numeric value.
|
||||
*
|
||||
* @return the next id.
|
||||
*/
|
||||
private static synchronized String nextID() {
|
||||
return prefix + Long.toString(id++);
|
||||
}
|
||||
|
||||
private XMPPConnection connection;
|
||||
private String chatID;
|
||||
private String participant;
|
||||
private PacketCollector messageCollector;
|
||||
|
||||
/**
|
||||
* Creates a new chat with the specified user.
|
||||
*
|
||||
* @param connection the connection the chat will use.
|
||||
* @param participant the user to chat with.
|
||||
*/
|
||||
public Chat(XMPPConnection connection, String participant) {
|
||||
this.connection = connection;
|
||||
this.participant = participant;
|
||||
// Automatically assign the next chat ID.
|
||||
chatID = nextID();
|
||||
|
||||
messageCollector = connection.getPacketReader().createPacketCollector(
|
||||
new ThreadFilter(chatID));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new chat with the specified user and chat ID (the XMPP "thread).
|
||||
*
|
||||
* @param connection the connection the chat will use.
|
||||
* @param participant the user to chat with.
|
||||
* @param chatID the chat ID to use.
|
||||
*/
|
||||
public Chat(XMPPConnection connection, String participant, String chatID) {
|
||||
this.connection = connection;
|
||||
this.participant = participant;
|
||||
this.chatID = chatID;
|
||||
|
||||
messageCollector = connection.getPacketReader().createPacketCollector(
|
||||
new ThreadFilter(chatID));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the unique id of this chat, which corresponds to the
|
||||
* <tt>thread</tt> field of XMPP messages.
|
||||
*
|
||||
* @return the unique ID of this chat.
|
||||
*/
|
||||
public String getChatID() {
|
||||
return chatID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the user the chat is with.
|
||||
*
|
||||
* @return the name of the user the chat is occuring with.
|
||||
*/
|
||||
public String getParticipant() {
|
||||
return participant;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the specified text as a message to the other chat participant.
|
||||
* This is a convenience method for:
|
||||
*
|
||||
* <pre>
|
||||
* Message message = chat.createMessage();
|
||||
* message.setBody(messageText);
|
||||
* chat.sendMessage(message);
|
||||
* </pre>
|
||||
*
|
||||
* @param text the text to send.
|
||||
* @throws XMPPException if sending the message fails.
|
||||
*/
|
||||
public void sendMessage(String text) throws XMPPException {
|
||||
Message message = createMessage();
|
||||
message.setBody(text);
|
||||
connection.getPacketWriter().sendPacket(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new Message to the chat participant. The message returned
|
||||
* will have its thread property set with this chat ID.
|
||||
*
|
||||
* @return a new message addressed to the chat participant and
|
||||
* using the correct thread value.
|
||||
* @see #sendMessage(Message)
|
||||
*/
|
||||
public Message createMessage() {
|
||||
Message message = new Message(participant, Message.CHAT);
|
||||
message.setThread(chatID);
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to the other chat participant. The thread property of
|
||||
* the message will automatically set to this chat ID in case the Message
|
||||
* was not created using the {@link #createMessage() createMessage} method.
|
||||
*
|
||||
* @param message
|
||||
* @throws XMPPException
|
||||
*/
|
||||
public void sendMessage(Message message) throws XMPPException {
|
||||
// Force the chatID since the user elected to send the message
|
||||
// through this chat object.
|
||||
message.setThread(chatID);
|
||||
connection.getPacketWriter().sendPacket(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Polls for and returns the next message, or <tt>null</tt> if there isn't
|
||||
* a message immediately available. This method provides significantly different
|
||||
* functionalty than the {@link #nextMessage()} method since it's non-blocking.
|
||||
* In other words, the method call will always return immediately, whereas the
|
||||
* nextMessage method will return only when a message is available (or after
|
||||
* a specific timeout).
|
||||
*
|
||||
* @return the next message if one is immediately available and
|
||||
* <tt>null</tt> otherwise.
|
||||
*/
|
||||
public Message pollMessage() {
|
||||
return (Message)messageCollector.pollResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next available message in the chat. The method will block
|
||||
* indefinitely (won't return) until a message is available.
|
||||
*
|
||||
* @return the next message.
|
||||
*/
|
||||
public Message nextMessage() {
|
||||
return (Message)messageCollector.nextResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next available message in the chat. The method will block
|
||||
* for up to the timeout. If a message still isn't available then, <tt>null</tt>
|
||||
* will be returned.
|
||||
*
|
||||
* @param timeout the maximum amount of time to wait for the next message.
|
||||
* @return the next message, or <tt>null</tt> if the timeout elapses without a
|
||||
* message becoming available.
|
||||
*/
|
||||
public Message nextMessage(long timeout) {
|
||||
return (Message)messageCollector.nextResult(timeout);
|
||||
}
|
||||
}
|
189
source/org/jivesoftware/smack/GroupChat.java
Normal file
189
source/org/jivesoftware/smack/GroupChat.java
Normal file
|
@ -0,0 +1,189 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack;
|
||||
|
||||
import org.jivesoftware.smack.packet.Presence;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.filter.*;
|
||||
|
||||
/**
|
||||
* A GroupChat is a conversation that takes plaaces among many users in a virtual
|
||||
* room. When joining a group chat, you specify a nickname, which is the identity
|
||||
* that other chat room users see.
|
||||
*
|
||||
* @see XMPPConnection#createGroupChat(String)
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class GroupChat {
|
||||
|
||||
private XMPPConnection connection;
|
||||
private String room;
|
||||
private String nickname = null;
|
||||
private boolean joined = false;
|
||||
|
||||
private PacketCollector collector;
|
||||
|
||||
/**
|
||||
* Creates a new group chat with the specified connection and room name.
|
||||
*
|
||||
* @param connection
|
||||
* @param room the name of the room in the form "roomName@service", where
|
||||
* "service" is the hostname at which the multi-user chat
|
||||
* service is running.
|
||||
*/
|
||||
public GroupChat(XMPPConnection connection, String room) {
|
||||
this.connection = connection;
|
||||
this.room = room;
|
||||
collector = connection.getPacketReader().createPacketCollector(
|
||||
new FromContainsFilter(room));
|
||||
}
|
||||
|
||||
/**
|
||||
* Joins the chat room using the specified nickname. If already joined as
|
||||
* another nickname, will leave as that name first before joining under the new
|
||||
* name.
|
||||
*
|
||||
* @param nickname the nicknam to use.
|
||||
* @throws XMPPException if an error occurs joining the room.
|
||||
*/
|
||||
public synchronized void join(String nickname) throws XMPPException {
|
||||
if (nickname == null || nickname.equals("")) {
|
||||
throw new IllegalArgumentException("Nickname must not be null or blank.");
|
||||
}
|
||||
// If we've already joined the room, leave it before joining under a new
|
||||
// nickname.
|
||||
if (joined) {
|
||||
leave();
|
||||
}
|
||||
// We join a room by sending a presence packet where the "to"
|
||||
// field is in the form "roomName@service/nickname"
|
||||
Presence joinPresence = new Presence(true);
|
||||
joinPresence.setTo(room + "/" + nickname);
|
||||
connection.getPacketWriter().sendPacket(joinPresence);
|
||||
// Wait for a presence packet back from the server.
|
||||
PacketFilter responseFilter = new AndFilter(
|
||||
new FromContainsFilter(room + "/" + nickname),
|
||||
new PacketTypeFilter(Presence.class));
|
||||
PacketCollector response = connection.getPacketReader().createPacketCollector(
|
||||
responseFilter);
|
||||
// Wait up to five seconds for a reply.
|
||||
Presence presence = (Presence)response.nextResult(5000);
|
||||
if (presence == null) {
|
||||
throw new XMPPException("No response from server.");
|
||||
}
|
||||
else if (presence.getError() != null) {
|
||||
throw new XMPPException(presence.getError().getMessage());
|
||||
|
||||
}
|
||||
this.nickname = nickname;
|
||||
joined = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Leave the chat room.
|
||||
*/
|
||||
public synchronized void leave() {
|
||||
// If not joined already, do nothing.
|
||||
if (!joined) {
|
||||
return;
|
||||
}
|
||||
// We leave a room by sending a presence packet where the "to"
|
||||
// field is in the form "roomName@service/nickname"
|
||||
Presence leavePresence = new Presence(false);
|
||||
leavePresence.setTo(room + "/" + nickname);
|
||||
connection.getPacketWriter().sendPacket(leavePresence);
|
||||
nickname = null;
|
||||
joined = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the nickname that was used to join the room, or <tt>null</tt> if not
|
||||
* currently joined.
|
||||
*
|
||||
* @return the nickname currently being used..
|
||||
*/
|
||||
public String getNickname() {
|
||||
return nickname;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to the chat room.
|
||||
*
|
||||
* @param text the text of the message to send.
|
||||
* @throws XMPPException if sending the message fails.
|
||||
*/
|
||||
public void sendMessage(String text) throws XMPPException {
|
||||
Message message = new Message(room, Message.CHAT);
|
||||
message.setBody(text);
|
||||
connection.getPacketWriter().sendPacket(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new Message to send to the chat room.
|
||||
*
|
||||
* @return a new Message addressed to the chat room.
|
||||
*/
|
||||
public Message createMessage() {
|
||||
return new Message(room, Message.CHAT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a Message to the chat room.
|
||||
*
|
||||
* @param message the message.
|
||||
* @throws XMPPException if sending the message fails.
|
||||
*/
|
||||
public void sendMessage(Message message) throws XMPPException {
|
||||
connection.getPacketWriter().sendPacket(message);
|
||||
}
|
||||
}
|
186
source/org/jivesoftware/smack/PacketCollector.java
Normal file
186
source/org/jivesoftware/smack/PacketCollector.java
Normal file
|
@ -0,0 +1,186 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack;
|
||||
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.filter.PacketFilter;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* Provides a mechanism to collect packets into a result queue that pass a
|
||||
* specified filter. The collector lets you perform blocking and polling
|
||||
* operations on the result queue. So, a PacketCollector is more suitable to
|
||||
* use than a {@link PacketListener} when you need to wait for a specific
|
||||
* result.
|
||||
*
|
||||
* @see PacketReader#createPacketCollector(PacketFilter)
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class PacketCollector {
|
||||
|
||||
private PacketFilter packetFilter;
|
||||
private LinkedList resultQueue;
|
||||
private PacketReader packetReader;
|
||||
private boolean cancelled = false;
|
||||
|
||||
/**
|
||||
* Creates a new packet collector. If the packet filter is <tt>null</tt>, then
|
||||
* all packets will match this collector.
|
||||
*
|
||||
* @param packetReader the packetReader the collector is tied to.
|
||||
* @param packetFilter determines which packets will be returned by this collector.
|
||||
*/
|
||||
protected PacketCollector(PacketReader packetReader, PacketFilter packetFilter) {
|
||||
this.packetReader = packetReader;
|
||||
this.packetFilter = packetFilter;
|
||||
this.resultQueue = new LinkedList();
|
||||
|
||||
// Add the collector to the packet reader's list of active collector.
|
||||
synchronized (packetReader.collectors) {
|
||||
packetReader.collectors.add(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Explicitly cancels the packet collector so that no more results are
|
||||
* queued up. Once a packet collector has been cancelled, it cannot be
|
||||
* re-enabled. Instead, a new packet collector must be created.
|
||||
*/
|
||||
public void cancel() {
|
||||
// If the packet collector has already been cancelled, do nothing.
|
||||
if (cancelled) {
|
||||
return;
|
||||
}
|
||||
else {
|
||||
cancelled = true;
|
||||
// Remove object from collectors list by setting the value in the
|
||||
// list at the correct index to null. The collector thread will
|
||||
// automatically remove the actual list entry when it can.
|
||||
int index = packetReader.collectors.indexOf(this);
|
||||
packetReader.collectors.set(index, null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the packet filter associated with this packet collector. The packet
|
||||
* filter is used to determine what packets are queued as results.
|
||||
*
|
||||
* @return the packet filter.
|
||||
*/
|
||||
public PacketFilter getPacketFilter() {
|
||||
return packetFilter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Polls to see if a result is currently available and returns it, or
|
||||
* immediately returns <tt>null</tt> if no packets are currently in the
|
||||
* result queue.
|
||||
*
|
||||
* @return the next packet result, or <tt>null</tt> if there are no more
|
||||
* results.
|
||||
*/
|
||||
public synchronized Packet pollResult() {
|
||||
if (resultQueue.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return (Packet)resultQueue.removeLast();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized Packet nextResult() {
|
||||
// Wait indefinitely until there is a result to return.
|
||||
while (resultQueue.isEmpty()) {
|
||||
try {
|
||||
wait();
|
||||
}
|
||||
catch (InterruptedException ie) { }
|
||||
}
|
||||
return (Packet)resultQueue.removeLast();
|
||||
}
|
||||
|
||||
public synchronized Packet nextResult(long timeout) {
|
||||
// Wait up to the specified amount of time for a result.
|
||||
if (resultQueue.isEmpty()) {
|
||||
try {
|
||||
wait(timeout);
|
||||
}
|
||||
catch (InterruptedException ie) { }
|
||||
}
|
||||
// If still no result, return null.
|
||||
if (resultQueue.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return (Packet)resultQueue.removeLast();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes a packet to see if it meets the criteria for this packet collector.
|
||||
* If so, the packet is added to the result queue.
|
||||
*
|
||||
* @param packet the packet to process.
|
||||
*/
|
||||
protected synchronized void processPacket(Packet packet) {
|
||||
if (packet == null) {
|
||||
return;
|
||||
}
|
||||
if (packetFilter == null || packetFilter.accept(packet)) {
|
||||
resultQueue.addFirst(packet);
|
||||
// Notify waiting threads a result is available.
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
77
source/org/jivesoftware/smack/PacketListener.java
Normal file
77
source/org/jivesoftware/smack/PacketListener.java
Normal file
|
@ -0,0 +1,77 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack;
|
||||
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.filter.PacketFilter;
|
||||
|
||||
/**
|
||||
* Provides a mechanism to listen for packets that pass a specified filter.
|
||||
* This allows event-style programming -- every time a new packet is found,
|
||||
* the {@link #processPacket(Packet)} method will be called. This is the
|
||||
* opposite approach to the functionality provided by a {@link PacketCollector}
|
||||
* which lets you block while waiting for results.
|
||||
*
|
||||
* @see PacketReader#addPacketListener(PacketListener, PacketFilter)
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public interface PacketListener {
|
||||
|
||||
/**
|
||||
* Process the next packet sent to this packet listener.
|
||||
*
|
||||
* @param packet the packet to process.
|
||||
*/
|
||||
public void processPacket(Packet packet);
|
||||
|
||||
}
|
460
source/org/jivesoftware/smack/PacketReader.java
Normal file
460
source/org/jivesoftware/smack/PacketReader.java
Normal file
|
@ -0,0 +1,460 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack;
|
||||
|
||||
import org.xmlpull.v1.*;
|
||||
import java.util.*;
|
||||
import org.jivesoftware.smack.packet.*;
|
||||
import org.jivesoftware.smack.filter.PacketFilter;
|
||||
|
||||
/**
|
||||
* Listens for XML traffic from the XMPP server, and parses it into packet objects.
|
||||
*
|
||||
* @see XMPPConnection#getPacketReader()
|
||||
* @see PacketCollector
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class PacketReader {
|
||||
|
||||
private Thread readerThread;
|
||||
private Thread listenerThread;
|
||||
|
||||
private XMPPConnection connection;
|
||||
private XmlPullParser parser;
|
||||
private boolean done = false;
|
||||
protected List collectors = new ArrayList();
|
||||
private List listeners = Collections.synchronizedList(new ArrayList());
|
||||
|
||||
private String connectionID = null;
|
||||
private Object connectionIDLock = new Object();
|
||||
|
||||
protected PacketReader(XMPPConnection connection) {
|
||||
this.connection = connection;
|
||||
|
||||
readerThread = new Thread() {
|
||||
public void run() {
|
||||
parsePackets();
|
||||
}
|
||||
};
|
||||
readerThread.setName("Smack Packet Reader");
|
||||
readerThread.setDaemon(true);
|
||||
|
||||
listenerThread = new Thread() {
|
||||
public void run() {
|
||||
processListeners();
|
||||
}
|
||||
};
|
||||
listenerThread.setName("Smack Listener Processor");
|
||||
listenerThread.setDaemon(true);
|
||||
|
||||
try {
|
||||
XmlPullParserFactory factory = XmlPullParserFactory.newInstance(
|
||||
System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);
|
||||
factory.setNamespaceAware(true);
|
||||
parser = factory.newPullParser();
|
||||
parser.setInput(connection.reader);
|
||||
|
||||
}
|
||||
catch (XmlPullParserException xppe) {
|
||||
xppe.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public PacketCollector createPacketCollector(PacketFilter packetFilter) {
|
||||
return new PacketCollector(this, packetFilter);
|
||||
}
|
||||
|
||||
public void addPacketListener(PacketListener packetListener, PacketFilter packetFilter) {
|
||||
// TODO: implement
|
||||
}
|
||||
|
||||
public void removePacketListener(PacketListener packetListener) {
|
||||
// TODO: implement
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the packet reader thread and returns once a connection to the server
|
||||
* has been established. A connection will be attempted for a maximum of five
|
||||
* seconds. An XMPPException will be thrown if the connection fails.
|
||||
*
|
||||
* @throws XMPPException if the server fails to send an opening stream back
|
||||
* for more than five seconds.
|
||||
*/
|
||||
public void startup() throws XMPPException {
|
||||
readerThread.start();
|
||||
listenerThread.start();
|
||||
// Wait for stream tag before returing. We'll wait a maximum of five seconds before
|
||||
// giving up and throwing an error.
|
||||
try {
|
||||
synchronized(connectionIDLock) {
|
||||
connectionIDLock.wait(5000);
|
||||
}
|
||||
}
|
||||
catch (InterruptedException ie) { }
|
||||
if (connectionID == null) {
|
||||
throw new XMPPException("Connection failed. No response from server.");
|
||||
}
|
||||
else {
|
||||
connection.connectionID = connectionID;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shuts the packet reader down.
|
||||
*/
|
||||
public void shutdown() {
|
||||
done = true;
|
||||
}
|
||||
|
||||
private void processListeners() {
|
||||
boolean processedPacket = false;
|
||||
while (true) {
|
||||
synchronized(listeners) {
|
||||
int size = listeners.size();
|
||||
for (int i=0; i<size; i++) {
|
||||
|
||||
}
|
||||
}
|
||||
if (!processedPacket) {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException ie) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse top-level packets in order to process them further.
|
||||
*/
|
||||
private void parsePackets() {
|
||||
try {
|
||||
int eventType = parser.getEventType();
|
||||
do {
|
||||
if (eventType == parser.START_TAG) {
|
||||
if (parser.getName().equals("message")) {
|
||||
processPacket(parseMessage(parser));
|
||||
}
|
||||
else if (parser.getName().equals("iq")) {
|
||||
processPacket(parseIQ(parser));
|
||||
}
|
||||
else if (parser.getName().equals("presence")) {
|
||||
processPacket(parsePresence(parser));
|
||||
}
|
||||
// We found an opening stream. Record information about it, then notify
|
||||
// the connectionID lock so that the packet reader startup can finish.
|
||||
else if (parser.getName().equals("stream")) {
|
||||
// Ensure the correct jabber:client namespace is being used.
|
||||
if ("jabber:client".equals(parser.getNamespace(null))) {
|
||||
// Check to see if the server supports SASL. We don't actually
|
||||
// do anything with this information at the moment.
|
||||
boolean supportsSASL = parser.getNamespace("sasl") != null;
|
||||
// Get the connection id.
|
||||
for (int i=0; i<parser.getAttributeCount(); i++) {
|
||||
if (parser.getAttributeName(i).equals("id")) {
|
||||
// Save the connectionID and notify that we've gotten it.
|
||||
connectionID = parser.getAttributeValue(i);
|
||||
synchronized(connectionIDLock) {
|
||||
connectionIDLock.notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (eventType == parser.END_TAG) {
|
||||
if (parser.getName().equals("stream")) {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
eventType = parser.next();
|
||||
} while (eventType != parser.END_DOCUMENT && !done);
|
||||
}
|
||||
catch (Exception e) {
|
||||
// An exception occurred while parsing. Print the error an close the
|
||||
// connection.
|
||||
e.printStackTrace();
|
||||
if (!done) {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes a packet after it's been fully parsed by looping through the installed
|
||||
* packet collectors and letting them examine the packet to see if they are a match.
|
||||
*
|
||||
* @param packet the packet to process.
|
||||
*/
|
||||
private void processPacket(Packet packet) {
|
||||
// Loop through all collectors and notify the appropriate ones.
|
||||
synchronized (collectors) {
|
||||
// Loop through packet collectors backwards.
|
||||
int size = collectors.size();
|
||||
for (int i=0; i<size; i++) {
|
||||
PacketCollector collector = (PacketCollector)collectors.get(i);
|
||||
if (collector != null) {
|
||||
// Have the collector process the packet to see if it wants to handle it.
|
||||
collector.processPacket(packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an IQ packet.
|
||||
*
|
||||
* @param parser the XML parser, positioned at the start of an IQ packet.
|
||||
* @return an IQ object.
|
||||
* @throws Exception if an exception occurs while parsing the packet.
|
||||
*/
|
||||
private static Packet parseIQ(XmlPullParser parser) throws Exception {
|
||||
String id = null;
|
||||
String to = null;
|
||||
String from = null;
|
||||
IQ.Type type = null;
|
||||
// Parse attributes of the opening iq tag.
|
||||
for (int i=0; i<parser.getAttributeCount(); i++) {
|
||||
String name = parser.getAttributeName(i);
|
||||
if (name.equals("id")) {
|
||||
id = parser.getAttributeValue(i);
|
||||
}
|
||||
else if (name.equals("type")) {
|
||||
type = IQ.Type.fromString(parser.getAttributeValue(i));
|
||||
}
|
||||
else if (name.equals("to")) {
|
||||
to = parser.getAttributeValue(i);
|
||||
}
|
||||
else if (name.equals("from")) {
|
||||
from = parser.getAttributeValue(i);
|
||||
}
|
||||
}
|
||||
boolean done = false;
|
||||
while (!done) {
|
||||
int eventType = parser.next();
|
||||
if (eventType == parser.START_TAG) {
|
||||
if (parser.getName().equals("query")) {
|
||||
String namespace = parser.getNamespace();
|
||||
|
||||
}
|
||||
if (parser.getName().equals("error")) {
|
||||
// TODO: parse error here
|
||||
}
|
||||
}
|
||||
else if (eventType == parser.END_TAG) {
|
||||
if (parser.getName().equals("iq")) {
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a message packet.
|
||||
*
|
||||
* @param parser the XML parser, positioned at the start of a message packet.
|
||||
* @return an Message object.
|
||||
* @throws Exception if an exception occurs while parsing the packet.
|
||||
*/
|
||||
private static Packet parseMessage(XmlPullParser parser) throws Exception {
|
||||
Message message = new Message();
|
||||
// Parse attributes of the opening message tag.
|
||||
for (int i=0; i<parser.getAttributeCount(); i++) {
|
||||
String name = parser.getAttributeName(i);
|
||||
if (name.equals("to")) {
|
||||
message.setRecipient(parser.getAttributeValue(i));
|
||||
}
|
||||
else if (name.equals("from")) {
|
||||
message.setSender(parser.getAttributeValue(i));
|
||||
}
|
||||
else if (name.equals("type")) {
|
||||
message.setType(Message.Type.fromString(parser.getAttributeValue(i)));
|
||||
}
|
||||
}
|
||||
// Parse sub-elements
|
||||
boolean done = false;
|
||||
String subject = null;
|
||||
String body = null;
|
||||
String thread = null;
|
||||
while (!done) {
|
||||
int eventType = parser.next();
|
||||
if (eventType == parser.START_TAG) {
|
||||
if (parser.getName().equals("subject")) {
|
||||
if (subject == null) {
|
||||
subject = parser.nextText();
|
||||
}
|
||||
}
|
||||
else if (parser.getName().equals("body")) {
|
||||
if (body == null) {
|
||||
body = parser.nextText();
|
||||
}
|
||||
}
|
||||
else if (parser.getName().equals("thread")) {
|
||||
if (thread == null) {
|
||||
thread = parser.nextText();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (eventType == parser.END_TAG) {
|
||||
if (parser.getName().equals("message")) {
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
message.setSubject(subject);
|
||||
message.setBody(body);
|
||||
message.setThread(thread);
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a presence packet.
|
||||
*
|
||||
* @param parser the XML parser, positioned at the start of a presence packet.
|
||||
* @return an Presence object.
|
||||
* @throws Exception if an exception occurs while parsing the packet.
|
||||
*/
|
||||
private static Packet parsePresence(XmlPullParser parser) throws Exception {
|
||||
String type = "available";
|
||||
String to = null;
|
||||
String from = null;
|
||||
String id = null;
|
||||
// Parse attributes of the opening message tag.
|
||||
for (int i=0; i<parser.getAttributeCount(); i++) {
|
||||
String name = parser.getAttributeName(i);
|
||||
if (name.equals("type")) {
|
||||
type = parser.getAttributeValue(i);
|
||||
}
|
||||
else if (name.equals("to")) {
|
||||
to = parser.getAttributeValue(i);
|
||||
}
|
||||
else if (name.equals("from")) {
|
||||
from = parser.getAttributeValue(i);
|
||||
}
|
||||
else if (name.equals("id")) {
|
||||
id = parser.getAttributeValue(i);
|
||||
}
|
||||
}
|
||||
|
||||
// We only handle "available" or "unavailable" packets for now.
|
||||
if (!(type.equals("available") || type.equals("unavailable"))) {
|
||||
System.out.println("FOUND OTHER PRESENCE TYPE: " + type);
|
||||
}
|
||||
Presence presence = new Presence(type.equals("available"));
|
||||
presence.setTo(to);
|
||||
presence.setFrom(from);
|
||||
presence.setPacketID(id);
|
||||
|
||||
// Parse sub-elements
|
||||
boolean done = false;
|
||||
while (!done) {
|
||||
int eventType = parser.next();
|
||||
if (eventType == parser.START_TAG) {
|
||||
if (parser.getName().equals("status")) {
|
||||
presence.setStatus(parser.nextText());
|
||||
}
|
||||
else if (parser.getName().equals("priority")) {
|
||||
try {
|
||||
int priority = Integer.parseInt(parser.nextText());
|
||||
presence.setPriority(priority);
|
||||
}
|
||||
catch (NumberFormatException nfe) { }
|
||||
}
|
||||
else if (parser.getName().equals("show")) {
|
||||
presence.setMode(Presence.Mode.fromString(parser.nextText()));
|
||||
}
|
||||
}
|
||||
else if (eventType == parser.END_TAG) {
|
||||
if (parser.getName().equals("presence")) {
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return presence;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a properties sub-packet. If any errors occur while de-serializing Java object
|
||||
* properties, an exception will be printed and not thrown since a thrown
|
||||
* exception will shut down the entire connection. ClassCastExceptions will occur
|
||||
* when both the sender and receiver of the packet don't have identical versions
|
||||
* of the same class.
|
||||
*
|
||||
* @param parser the XML parser, positioned at the start of a properties sub-packet.
|
||||
* @param packet the packet being parsed.
|
||||
* @throws Exception if an error occurs while parsing the properties.
|
||||
*/
|
||||
private static void parseProperties(XmlPullParser parser, Packet packet) throws Exception {
|
||||
boolean done = false;
|
||||
while (!done) {
|
||||
int eventType = parser.next();
|
||||
if (eventType == parser.START_TAG) {
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class PacketListenerWrapper {
|
||||
|
||||
private PacketListener packetListener;
|
||||
private PacketCollector packetCollector;
|
||||
|
||||
public PacketListenerWrapper(PacketReader packetReader, PacketListener packetListener,
|
||||
PacketFilter packetFilter)
|
||||
{
|
||||
this.packetListener = packetListener;
|
||||
this.packetCollector = new PacketCollector(packetReader, packetFilter);
|
||||
}
|
||||
}
|
||||
}
|
145
source/org/jivesoftware/smack/PacketWriter.java
Normal file
145
source/org/jivesoftware/smack/PacketWriter.java
Normal file
|
@ -0,0 +1,145 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.io.*;
|
||||
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
|
||||
/**
|
||||
* Writes packets to a Jabber server.
|
||||
*
|
||||
* @see XMPPConnection#getPacketWriter()
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class PacketWriter extends Thread {
|
||||
|
||||
private Writer writer;
|
||||
private XMPPConnection connection;
|
||||
private LinkedList queue;
|
||||
private boolean done = false;
|
||||
|
||||
protected PacketWriter(XMPPConnection connection) {
|
||||
this.connection = connection;
|
||||
this.writer = connection.writer;
|
||||
this.queue = new LinkedList();
|
||||
// This should be a daemon thread.
|
||||
setDaemon(true);
|
||||
}
|
||||
|
||||
public void sendPacket(Packet packet) {
|
||||
synchronized(queue) {
|
||||
queue.addFirst(packet);
|
||||
queue.notify();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next available packet from the queue for writing.
|
||||
*
|
||||
* @return the next packet for writing.
|
||||
*/
|
||||
private Packet nextPacket() {
|
||||
synchronized(queue) {
|
||||
while (queue.size() == 0) {
|
||||
try {
|
||||
queue.wait();
|
||||
}
|
||||
catch (InterruptedException ie) { }
|
||||
}
|
||||
return (Packet)queue.removeLast();
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
// Open the stream.
|
||||
StringBuffer stream = new StringBuffer();
|
||||
stream.append("<stream:stream ");
|
||||
stream.append("xmlns=\"jabber:client\" ");
|
||||
stream.append("xmlns:stream=\"http://etherx.jabber.org/streams\" ");
|
||||
stream.append("xmlns:sasl=\"http://www.iana.org/assignments/sasl-mechanisms\" ");
|
||||
stream.append("to=\"" + connection.getHost() + "\">");
|
||||
writer.write(stream.toString());
|
||||
writer.flush();
|
||||
// Write out packets from the queue.
|
||||
while (!done) {
|
||||
Packet packet = nextPacket();
|
||||
writer.write(packet.toXML());
|
||||
writer.flush();
|
||||
}
|
||||
// Close the stream.
|
||||
try {
|
||||
writer.write("</stream>");
|
||||
writer.flush();
|
||||
}
|
||||
catch (Exception e) { }
|
||||
finally {
|
||||
try {
|
||||
writer.close();
|
||||
}
|
||||
catch (Exception e) { }
|
||||
}
|
||||
}
|
||||
catch (IOException ioe){
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
176
source/org/jivesoftware/smack/SSLXMPPConnection.java
Normal file
176
source/org/jivesoftware/smack/SSLXMPPConnection.java
Normal file
|
@ -0,0 +1,176 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.cert.*;
|
||||
import javax.net.ssl.*;
|
||||
import javax.net.*;
|
||||
|
||||
/**
|
||||
* Creates an SSL connection to a XMPP (Jabber) server.
|
||||
*
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class SSLXMPPConnection extends XMPPConnection {
|
||||
|
||||
public SSLXMPPConnection(String host) throws XMPPException {
|
||||
this(host, 5223);
|
||||
}
|
||||
|
||||
public SSLXMPPConnection(String host, int port) throws XMPPException {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
try {
|
||||
SSLSocketFactory sslFactory = new DummySSLSocketFactory();
|
||||
this.socket = (SSLSocket)sslFactory.createSocket(host, port);
|
||||
}
|
||||
catch (UnknownHostException uhe) {
|
||||
throw new XMPPException("Could not connect to " + host + ":" + port + ".", uhe);
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
throw new XMPPException("Error connecting to " + host + ":" + port + ".", ioe);
|
||||
}
|
||||
super.init();
|
||||
}
|
||||
|
||||
/**
|
||||
* An SSL socket factory that will let any certifacte past, even if it's expired or
|
||||
* not singed by a root CA.
|
||||
*/
|
||||
private static class DummySSLSocketFactory extends SSLSocketFactory {
|
||||
|
||||
private SSLSocketFactory factory;
|
||||
|
||||
public DummySSLSocketFactory() {
|
||||
|
||||
try {
|
||||
SSLContext sslcontent = SSLContext.getInstance("TLS");
|
||||
sslcontent.init(null, // KeyManager not required
|
||||
new TrustManager[] { new DummyTrustManager() },
|
||||
new java.security.SecureRandom());
|
||||
factory = sslcontent.getSocketFactory();
|
||||
}
|
||||
catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (KeyManagementException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static SocketFactory getDefault() {
|
||||
return new DummySSLSocketFactory();
|
||||
}
|
||||
|
||||
public Socket createSocket(Socket socket, String s, int i, boolean flag)
|
||||
throws IOException
|
||||
{
|
||||
return factory.createSocket(socket, s, i, flag);
|
||||
}
|
||||
|
||||
public Socket createSocket(InetAddress inaddr, int i, InetAddress inaddr2, int j)
|
||||
throws IOException
|
||||
{
|
||||
return factory.createSocket(inaddr, i, inaddr2, j);
|
||||
}
|
||||
|
||||
public Socket createSocket(InetAddress inaddr, int i) throws IOException {
|
||||
return factory.createSocket(inaddr, i);
|
||||
}
|
||||
|
||||
public Socket createSocket(String s, int i, InetAddress inaddr, int j) throws IOException {
|
||||
return factory.createSocket(s, i, inaddr, j);
|
||||
}
|
||||
|
||||
public Socket createSocket(String s, int i) throws IOException {
|
||||
return factory.createSocket(s, i);
|
||||
}
|
||||
|
||||
public String[] getDefaultCipherSuites() {
|
||||
return factory.getSupportedCipherSuites();
|
||||
}
|
||||
|
||||
public String[] getSupportedCipherSuites() {
|
||||
return factory.getSupportedCipherSuites();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Trust manager which accepts certificates without any validation
|
||||
* except date validation.
|
||||
*/
|
||||
private static class DummyTrustManager implements X509TrustManager {
|
||||
|
||||
public void checkClientTrusted(X509Certificate[] chain, String authType) {
|
||||
|
||||
}
|
||||
|
||||
public void checkServerTrusted(X509Certificate[] chain, String authType) {
|
||||
try {
|
||||
chain[0].checkValidity();
|
||||
}
|
||||
catch (CertificateExpiredException e) {
|
||||
}
|
||||
catch (CertificateNotYetValidException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return new X509Certificate[0];
|
||||
}
|
||||
}
|
||||
}
|
532
source/org/jivesoftware/smack/XMPPConnection.java
Normal file
532
source/org/jivesoftware/smack/XMPPConnection.java
Normal file
|
@ -0,0 +1,532 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack;
|
||||
|
||||
import org.jivesoftware.smack.packet.*;
|
||||
import org.jivesoftware.smack.filter.PacketIDFilter;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* Creates a connection to a XMPP (Jabber) server. A simple use of this API might
|
||||
* look like the following:
|
||||
* <pre>
|
||||
* // Create a connection to the jivesoftware.com XMPP server.
|
||||
* XMPPConnection con = new XMPPConnection("jivesoftware.com");
|
||||
* // Most servers require you to login before performing other tasks.
|
||||
* con.login("jsmith", "mypass");
|
||||
* // Start a new conversation with John Doe and send him a message.
|
||||
* Chat chat = new Chat("jdoe@jabber.org");
|
||||
* chat.sendMessage("Hey, how's it going?");
|
||||
* </pre>
|
||||
*
|
||||
* Every connection has a PacketReader and PacketWriter instance, which are used
|
||||
* to read and write XML with the server.
|
||||
*
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class XMPPConnection {
|
||||
|
||||
private static final String NEWLINE = System.getProperty ("line.separator");
|
||||
|
||||
/**
|
||||
* Value that indicates whether debugging is enabled. When enabled, a debug
|
||||
* window will apear for each new connection that will contain the following
|
||||
* information:<ul>
|
||||
* <li> Client Traffic -- raw XML traffic generated by Smack and sent to the server.
|
||||
* <li> Server Traffic -- raw XML traffic sent by the server to the client.
|
||||
* <li> Interpreted Packets -- shows XML packets from the server as parsed by Smack.
|
||||
* </ul>
|
||||
*
|
||||
* Debugging can be enabled by setting this field to true, or by setting the Java system
|
||||
* property <tt>smack.debugEnabled</tt> to true. The system property can be set on the
|
||||
* command line such as "java SomeApp -Dsmack.debugEnabled=true".
|
||||
*/
|
||||
public static boolean DEBUG_ENABLED = Boolean.getBoolean("smack.debugEnabled");
|
||||
|
||||
protected String host;
|
||||
protected int port;
|
||||
protected Socket socket;
|
||||
|
||||
String connectionID;
|
||||
private boolean connected = false;
|
||||
|
||||
private PacketWriter packetWriter;
|
||||
private PacketReader packetReader;
|
||||
|
||||
Writer writer;
|
||||
Reader reader;
|
||||
|
||||
/**
|
||||
* Constructor for use by classes extending this one.
|
||||
*/
|
||||
protected XMPPConnection() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new connection to the specified Jabber server. The default port of 5222 will
|
||||
* be used.
|
||||
*
|
||||
* @param host the name of the jabber server to connect to; e.g. <tt>jivesoftware.com</tt>.
|
||||
* @throws XMPPException if an error occurs while trying to establish a connection.
|
||||
*/
|
||||
public XMPPConnection(String host) throws XMPPException {
|
||||
this(host, 5222);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new connection to the to the specified Jabber server on the given port.
|
||||
*
|
||||
* @param host the name of the jabber server to connect to; e.g. <tt>jivesoftware.com</tt>.
|
||||
* @param port the port on the server that should be used; e.g. <tt>5222</tt>.
|
||||
* @throws XMPPException if an error occurs while trying to establish a connection.
|
||||
*/
|
||||
public XMPPConnection(String host, int port) throws XMPPException {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
try {
|
||||
this.socket = new Socket(host, port);
|
||||
}
|
||||
catch (UnknownHostException uhe) {
|
||||
throw new XMPPException("Could not connect to " + host + ":" + port + ".", uhe);
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
throw new XMPPException("Error connecting to " + host + ":" + port + ".", ioe);
|
||||
}
|
||||
init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the connection ID for this connection, which is the value set by the server
|
||||
* when opening a Jabber stream. If the server does not set a connection ID, this value
|
||||
* will be null.
|
||||
*
|
||||
* @return the ID of this connection returned from the Jabber server.
|
||||
*/
|
||||
public String getConnectionID() {
|
||||
return connectionID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the host name of the Jabber server for this connection.
|
||||
*
|
||||
* @return the host name of the Jabber server.
|
||||
*/
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the port number of the XMPP server for this connection. The default port
|
||||
* for normal connections is 5222. The default port for SSL connections is 5223.
|
||||
*
|
||||
* @return the port number of the Jabber server.
|
||||
*/
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Login to the server using the server's preferred authentication mechanism and set
|
||||
* our presence to available. If more than five seconds elapses without a response from
|
||||
* the server, or if an error occurs, a XMPPException will be thrown.<p>
|
||||
*
|
||||
* @param username the username.
|
||||
* @param password the password.
|
||||
* @throws XMPPException if an error occurs.
|
||||
*/
|
||||
public synchronized void login(String username, String password) throws XMPPException {
|
||||
if (!isConnected()) {
|
||||
throw new IllegalStateException("Not connected to server.");
|
||||
}
|
||||
Authentication auth = new Authentication(username, password, "Smack");
|
||||
packetWriter.sendPacket(auth);
|
||||
// Wait up to five seconds for a response from the server.
|
||||
PacketCollector collector = packetReader.createPacketCollector(
|
||||
new PacketIDFilter(auth.getPacketID()));
|
||||
IQ response = (IQ)collector.nextResult(5000);
|
||||
if (response == null || response.getType() == IQ.Type.ERROR) {
|
||||
throw new XMPPException("Authentication failed.");
|
||||
}
|
||||
// We're done with the collector, so explicitly cancel it.
|
||||
collector.cancel();
|
||||
// Set presence to online.
|
||||
packetWriter.sendPacket(new Presence(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new chat with the specified participant. The participant should
|
||||
* be a valid Jabber user such as <tt>jdoe@jivesoftware.com</tt> or
|
||||
* <tt>jdoe@jivesoftware.com/work</tt>.
|
||||
*
|
||||
* @param participant the person to start the conversation with.
|
||||
* @return a new Chat object.
|
||||
*/
|
||||
public Chat createChat(String participant) {
|
||||
if (!isConnected()) {
|
||||
throw new IllegalStateException("Not connected to server.");
|
||||
}
|
||||
return new Chat(this, participant);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new group chat connected to the specified room. The room name
|
||||
* should be a valid conference id, such as <tt>chatroom@jivesoftware.com</tt>.
|
||||
*
|
||||
* @param room the name of the room.
|
||||
* @return a new GroupChat object.
|
||||
*/
|
||||
public GroupChat createGroupChat(String room) {
|
||||
if (!isConnected()) {
|
||||
throw new IllegalStateException("Not connected to server.");
|
||||
}
|
||||
return new GroupChat(this, room);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if currently connected to the Jabber server.
|
||||
*
|
||||
* @return true if connected.
|
||||
*/
|
||||
public boolean isConnected() {
|
||||
return connected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set presence to unavailable then and closes the connection to the Jabber server.
|
||||
* Once a connection has been closed, it cannot be re-opened.
|
||||
*/
|
||||
public void close() {
|
||||
// Set presence to offline.
|
||||
packetWriter.sendPacket(new Presence(false));
|
||||
packetWriter.shutdown();
|
||||
packetReader.shutdown();
|
||||
try {
|
||||
socket.close();
|
||||
}
|
||||
catch (Exception e) { }
|
||||
connected = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the packet writer for this connection. This exposes the ability to directly
|
||||
* write Packet objects to the Jabber server. In general, this is only required for
|
||||
* advanced uses of the API.
|
||||
*
|
||||
* @return the packet writer for the connection.
|
||||
*/
|
||||
public PacketWriter getPacketWriter() {
|
||||
if (!isConnected()) {
|
||||
throw new IllegalStateException("Not connected to server.");
|
||||
}
|
||||
return packetWriter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the packet reader for this connection. This exposes the ability to register
|
||||
* listeners for incoming Packet objects. In general, this is only required for advanced
|
||||
* uses of the API.
|
||||
*
|
||||
* @return the packet reader for the connection.
|
||||
*/
|
||||
public PacketReader getPacketReader() {
|
||||
if (!isConnected()) {
|
||||
throw new IllegalStateException("Not connected to server.");
|
||||
}
|
||||
return packetReader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the connection by creating a packet reader and writer and opening a
|
||||
* Jabber stream to the server.
|
||||
*
|
||||
* @throws XMPPException if establishing a connection to the server fails.
|
||||
*/
|
||||
protected void init() throws XMPPException {
|
||||
try {
|
||||
reader = new InputStreamReader(socket.getInputStream(), "UTF-8");
|
||||
writer = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
throw new XMPPException("Error establishing connection with server.", ioe);
|
||||
}
|
||||
|
||||
// If debugging is enabled, we open a window and write out all network traffic.
|
||||
// The method that creates the debug GUI returns a thread that we must start after
|
||||
// the packet reader and writer are created.
|
||||
Thread allPacketListener = null;
|
||||
if (DEBUG_ENABLED) {
|
||||
allPacketListener = createDebug();
|
||||
}
|
||||
|
||||
packetWriter = new PacketWriter(this);
|
||||
packetReader = new PacketReader(this);
|
||||
|
||||
// If debugging is enabled, we should start the thread that will listen for
|
||||
// all packets and then log them.
|
||||
if (DEBUG_ENABLED) {
|
||||
allPacketListener.start();
|
||||
}
|
||||
// Start the packet writer. This will open a Jabber stream to the server
|
||||
packetWriter.start();
|
||||
// Start the packet reader. The startup() method will block until we
|
||||
// get an opening stream packet back from server.
|
||||
packetReader.startup();
|
||||
|
||||
// Make note of the fact that we're now connected.
|
||||
connected = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the debug process, which is a GUI window that displays XML traffic.
|
||||
* This method must be called before the packet reader and writer are called because
|
||||
* it wraps the reader and writer objects with special logging implementations.
|
||||
* The method returns a Thread that must be started after the packet reader and writer
|
||||
* are started.
|
||||
*
|
||||
* @return a Thread used by the debugging process that must be started after the packet
|
||||
* reader and writer are created.
|
||||
*/
|
||||
private Thread createDebug() {
|
||||
// Use the native look and feel.
|
||||
try {
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
JFrame frame = new JFrame("Smack Debug Window -- " + getHost() + ":" + getPort());
|
||||
|
||||
// We'll arrange the UI into four tabs. The first tab contains all data, the second
|
||||
// client generated XML, the third server generated XML, and the fourth is packet
|
||||
// data from the server as seen by Smack.
|
||||
JTabbedPane tabbedPane = new JTabbedPane();
|
||||
|
||||
JPanel allPane = new JPanel();
|
||||
allPane.setLayout(new GridLayout(3, 1));
|
||||
tabbedPane.add("All", allPane);
|
||||
|
||||
// Create UI elements for client generated XML traffic.
|
||||
final JTextArea sentText1 = new JTextArea();
|
||||
final JTextArea sentText2 = new JTextArea();
|
||||
sentText1.setEditable(false);
|
||||
sentText2.setEditable(false);
|
||||
sentText1.setForeground(new Color(112, 3, 3));
|
||||
sentText2.setForeground(new Color(112, 3, 3));
|
||||
allPane.add(new JScrollPane(sentText1));
|
||||
tabbedPane.add("Client", new JScrollPane(sentText2));
|
||||
|
||||
// Create UI elements for server generated XML traffic.
|
||||
final JTextArea receivedText1 = new JTextArea();
|
||||
final JTextArea receivedText2 = new JTextArea();
|
||||
receivedText1.setEditable(false);
|
||||
receivedText2.setEditable(false);
|
||||
receivedText1.setForeground(new Color(6, 76, 133));
|
||||
receivedText2.setForeground(new Color(6, 76, 133));
|
||||
allPane.add(new JScrollPane(receivedText1));
|
||||
tabbedPane.add("Server", new JScrollPane(receivedText2));
|
||||
|
||||
// Create UI elements for interpreted XML traffic.
|
||||
final JTextArea interpretedText1 = new JTextArea();
|
||||
final JTextArea interpretedText2 = new JTextArea();
|
||||
interpretedText1.setEditable(false);
|
||||
interpretedText2.setEditable(false);
|
||||
interpretedText1.setForeground(new Color(1, 94, 35));
|
||||
interpretedText2.setForeground(new Color(1, 94, 35));
|
||||
allPane.add(new JScrollPane(interpretedText1));
|
||||
tabbedPane.add("Interpreted Packets", new JScrollPane(interpretedText2));
|
||||
|
||||
frame.getContentPane().add(tabbedPane);
|
||||
|
||||
frame.setSize(550, 400);
|
||||
frame.show();
|
||||
|
||||
// Create a special Reader that wraps the main Reader and logs data to the GUI.
|
||||
Reader debugReader = new Reader() {
|
||||
|
||||
Reader myReader = reader;
|
||||
|
||||
public int read(char cbuf[], int off, int len) throws IOException {
|
||||
int count = myReader.read(cbuf, off, len);
|
||||
String str = new String(cbuf, off, count);
|
||||
receivedText1.append(str);
|
||||
receivedText2.append(str);
|
||||
if (str.endsWith(">")) {
|
||||
receivedText1.append(NEWLINE);
|
||||
receivedText2.append(NEWLINE);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
myReader.close();
|
||||
}
|
||||
|
||||
public int read() throws IOException {
|
||||
return myReader.read();
|
||||
}
|
||||
|
||||
public int read(char cbuf[]) throws IOException {
|
||||
return myReader.read(cbuf);
|
||||
}
|
||||
|
||||
public long skip(long n) throws IOException {
|
||||
return myReader.skip(n);
|
||||
}
|
||||
|
||||
public boolean ready() throws IOException {
|
||||
return myReader.ready();
|
||||
}
|
||||
|
||||
public boolean markSupported() {
|
||||
return myReader.markSupported();
|
||||
}
|
||||
|
||||
public void mark(int readAheadLimit) throws IOException {
|
||||
myReader.mark(readAheadLimit);
|
||||
}
|
||||
|
||||
public void reset() throws IOException {
|
||||
myReader.reset();
|
||||
}
|
||||
};
|
||||
|
||||
// Create a special Writer that wraps the main Writer and logs data to the GUI.
|
||||
Writer debugWriter = new Writer() {
|
||||
|
||||
Writer myWriter = writer;
|
||||
|
||||
public void write(char cbuf[], int off, int len) throws IOException {
|
||||
myWriter.write(cbuf, off, len);
|
||||
String str = new String(cbuf, off, len);
|
||||
sentText1.append(str);
|
||||
sentText2.append(str);
|
||||
if (str.endsWith(">")) {
|
||||
sentText1.append(NEWLINE);
|
||||
sentText2.append(NEWLINE);
|
||||
}
|
||||
}
|
||||
|
||||
public void flush() throws IOException {
|
||||
myWriter.flush();
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
myWriter.close();
|
||||
}
|
||||
|
||||
public void write(int c) throws IOException {
|
||||
myWriter.write(c);
|
||||
}
|
||||
|
||||
public void write(char cbuf[]) throws IOException {
|
||||
myWriter.write(cbuf);
|
||||
String str = new String(cbuf);
|
||||
sentText1.append(str);
|
||||
sentText2.append(str);
|
||||
if (str.endsWith(">")) {
|
||||
sentText1.append(NEWLINE);
|
||||
sentText2.append(NEWLINE);
|
||||
}
|
||||
}
|
||||
|
||||
public void write(String str) throws IOException {
|
||||
myWriter.write(str);
|
||||
sentText1.append(str);
|
||||
sentText2.append(str);
|
||||
if (str.endsWith(">")) {
|
||||
sentText1.append(NEWLINE);
|
||||
sentText2.append(NEWLINE);
|
||||
}
|
||||
}
|
||||
|
||||
public void write(String str, int off, int len) throws IOException {
|
||||
myWriter.write(str, off, len);
|
||||
str = str.substring(off, off + len);
|
||||
sentText1.append(str);
|
||||
sentText2.append(str);
|
||||
if (str.endsWith(">")) {
|
||||
sentText1.append(NEWLINE);
|
||||
sentText2.append(NEWLINE);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Assign the reader/writer objects to use the debug versions. The packet reader
|
||||
// and writer will use the debug versions when they are created.
|
||||
reader = debugReader;
|
||||
writer = debugWriter;
|
||||
|
||||
// Create a thread that will listen for all incoming packets and write them to
|
||||
// the GUI. This is what we call "interpreted" packet data, since it's the packet
|
||||
// data as Smack sees it and not as it's coming in as raw XML.
|
||||
Thread allPacketListener = new Thread() {
|
||||
public void run() {
|
||||
PacketCollector collector = packetReader.createPacketCollector(null);
|
||||
while (true) {
|
||||
Packet packet = collector.nextResult();
|
||||
interpretedText1.append(packet.toXML());
|
||||
interpretedText2.append(packet.toXML());
|
||||
interpretedText1.append(NEWLINE);
|
||||
interpretedText2.append(NEWLINE);
|
||||
}
|
||||
}
|
||||
};
|
||||
return allPacketListener;
|
||||
}
|
||||
}
|
73
source/org/jivesoftware/smack/XMPPException.java
Normal file
73
source/org/jivesoftware/smack/XMPPException.java
Normal file
|
@ -0,0 +1,73 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack;
|
||||
|
||||
|
||||
public class XMPPException extends Exception {
|
||||
|
||||
public XMPPException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public XMPPException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public XMPPException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public XMPPException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
85
source/org/jivesoftware/smack/filter/AndFilter.java
Normal file
85
source/org/jivesoftware/smack/filter/AndFilter.java
Normal file
|
@ -0,0 +1,85 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.filter;
|
||||
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
|
||||
/**
|
||||
* Implements the logical AND operation over two packet filters. In other words, packets
|
||||
* pass this filter if they pass <b>both</b> of the filters.
|
||||
*
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class AndFilter implements PacketFilter {
|
||||
|
||||
private PacketFilter filter1;
|
||||
private PacketFilter filter2;
|
||||
|
||||
/**
|
||||
* Creates an AND filter using the specified filters.
|
||||
*
|
||||
* @param filter1 the first packet filter.
|
||||
* @param filter2 the second packet filter.
|
||||
*/
|
||||
public AndFilter(PacketFilter filter1, PacketFilter filter2) {
|
||||
if (filter1 == null || filter2 == null) {
|
||||
throw new IllegalArgumentException("Parameters cannot be null.");
|
||||
}
|
||||
this.filter1 = filter1;
|
||||
this.filter2 = filter2;
|
||||
}
|
||||
|
||||
public boolean accept(Packet packet) {
|
||||
return filter1.accept(packet) && filter2.accept(packet);
|
||||
}
|
||||
}
|
81
source/org/jivesoftware/smack/filter/FromContainsFilter.java
Normal file
81
source/org/jivesoftware/smack/filter/FromContainsFilter.java
Normal file
|
@ -0,0 +1,81 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.filter;
|
||||
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
|
||||
/**
|
||||
* Filters for packets where the "from" field contains a specified value.
|
||||
*
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class FromContainsFilter implements PacketFilter {
|
||||
|
||||
private String from;
|
||||
|
||||
/**
|
||||
* Creates a "to" contains filter using the "to" field part.
|
||||
*
|
||||
* @param from the from field value the packet must contain.
|
||||
*/
|
||||
public FromContainsFilter(String from) {
|
||||
if (from == null) {
|
||||
throw new IllegalArgumentException("Parameter cannot be null.");
|
||||
}
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
public boolean accept(Packet packet) {
|
||||
return packet.getFrom().indexOf(from) != -1;
|
||||
}
|
||||
}
|
85
source/org/jivesoftware/smack/filter/OrFilter.java
Normal file
85
source/org/jivesoftware/smack/filter/OrFilter.java
Normal file
|
@ -0,0 +1,85 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.filter;
|
||||
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
|
||||
/**
|
||||
* Implements the logical OR operation over two packet filters. In other words, packets
|
||||
* pass this filter if they pass <b>either</b> of the filters.
|
||||
*
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class OrFilter implements PacketFilter {
|
||||
|
||||
private PacketFilter filter1;
|
||||
private PacketFilter filter2;
|
||||
|
||||
/**
|
||||
* Creates an OR filter using the specified filters.
|
||||
*
|
||||
* @param filter1 the first packet filter.
|
||||
* @param filter2 the second packet filter.
|
||||
*/
|
||||
public OrFilter(PacketFilter filter1, PacketFilter filter2) {
|
||||
if (filter1 == null || filter2 == null) {
|
||||
throw new IllegalArgumentException("Parameters cannot be null.");
|
||||
}
|
||||
this.filter1 = filter1;
|
||||
this.filter2 = filter2;
|
||||
}
|
||||
|
||||
public boolean accept(Packet packet) {
|
||||
return filter1.accept(packet) || filter2.accept(packet);
|
||||
}
|
||||
}
|
95
source/org/jivesoftware/smack/filter/PacketFilter.java
Normal file
95
source/org/jivesoftware/smack/filter/PacketFilter.java
Normal file
|
@ -0,0 +1,95 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.filter;
|
||||
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
|
||||
/**
|
||||
* Defines a way to filter packets for particular attributes. Packet filters are
|
||||
* used when constructing packet listeners or collectors -- the filter defines
|
||||
* what packets match the criteria of the collector or listener for further
|
||||
* packet processing.<p>
|
||||
*
|
||||
* Several pre-defined filters are defined. These filters can be logically combined
|
||||
* for more complex packet filtering by using the
|
||||
* {@link org.jivesoftware.smack.filter.AndFilter AndFilter} and
|
||||
* {@link org.jivesoftware.smack.filter.OrFilter OrFilter} filters. It's also possible
|
||||
* to define your own filters by implementing this interface. The code example below
|
||||
* creates a trivial filter for packets with a specific ID.
|
||||
*
|
||||
* <pre>
|
||||
* // Use an anonymous inner class to define a packet filter that returns
|
||||
* // all packets that have a packet ID of "RS145".
|
||||
* PacketFilter myFilter = new PacketFilter() {
|
||||
* public boolean accept(Packet packet) {
|
||||
* return "RS145".equals(packet.getPacketID());
|
||||
* }
|
||||
* };
|
||||
* // Create a new packet collector using the filter we created.
|
||||
* PacketCollector myCollector = packetReader.createPacketCollector(myFilter);
|
||||
* </pre>
|
||||
*
|
||||
* @see org.jivesoftware.smack.PacketCollector
|
||||
* @see org.jivesoftware.smack.PacketListener
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public interface PacketFilter {
|
||||
|
||||
/**
|
||||
* Tests whether or not the specified packet should pass the filter.
|
||||
*
|
||||
* @param packet the packet to test.
|
||||
* @return true if and only if <tt>packet</tt> passes the filter.
|
||||
*/
|
||||
public boolean accept(Packet packet);
|
||||
}
|
81
source/org/jivesoftware/smack/filter/PacketIDFilter.java
Normal file
81
source/org/jivesoftware/smack/filter/PacketIDFilter.java
Normal file
|
@ -0,0 +1,81 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.filter;
|
||||
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
|
||||
/**
|
||||
* Filters for packets with a particular packet ID.
|
||||
*
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class PacketIDFilter implements PacketFilter {
|
||||
|
||||
private String packetID;
|
||||
|
||||
/**
|
||||
* Creates a new packet ID filter using the specified packet ID.
|
||||
*
|
||||
* @param packetID the packet ID to filter for.
|
||||
*/
|
||||
public PacketIDFilter(String packetID) {
|
||||
if (packetID == null) {
|
||||
throw new IllegalArgumentException("Packet ID cannot be null.");
|
||||
}
|
||||
this.packetID = packetID;
|
||||
}
|
||||
|
||||
public boolean accept(Packet packet) {
|
||||
return packetID.equals(packet.getPacketID());
|
||||
}
|
||||
}
|
90
source/org/jivesoftware/smack/filter/PacketTypeFilter.java
Normal file
90
source/org/jivesoftware/smack/filter/PacketTypeFilter.java
Normal file
|
@ -0,0 +1,90 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.filter;
|
||||
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
|
||||
/**
|
||||
* Filters for packets of a particular type. The type is given as a Class object, so
|
||||
* example types would:
|
||||
* <ul>
|
||||
* <li><tt>Message.class</tt>
|
||||
* <li><tt>IQ.class</tt>
|
||||
* <li><tt>Presence.class</tt>
|
||||
* </ul>
|
||||
*
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class PacketTypeFilter implements PacketFilter {
|
||||
|
||||
Class packetType;
|
||||
|
||||
/**
|
||||
* Creates a new packet type filter that will filter for packets that are the
|
||||
* same type as <tt>packetType</tt>.
|
||||
*
|
||||
* @param packetType the Class type.
|
||||
*/
|
||||
public PacketTypeFilter(Class packetType) {
|
||||
// Ensure the packet type is a sub-class of Packet.
|
||||
if (!Packet.class.isAssignableFrom(packetType)) {
|
||||
throw new IllegalArgumentException("Packet type must be a sub-class of Packet.");
|
||||
}
|
||||
this.packetType = packetType;
|
||||
}
|
||||
|
||||
public boolean accept(Packet packet) {
|
||||
return packetType.isInstance(packet);
|
||||
}
|
||||
|
||||
}
|
87
source/org/jivesoftware/smack/filter/ThreadFilter.java
Normal file
87
source/org/jivesoftware/smack/filter/ThreadFilter.java
Normal file
|
@ -0,0 +1,87 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.filter;
|
||||
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
|
||||
/**
|
||||
* Filters for message packets with a particular thread value.
|
||||
*
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class ThreadFilter implements PacketFilter {
|
||||
|
||||
private String thread;
|
||||
|
||||
/**
|
||||
* Creates a new thread filter using the specified thread value.
|
||||
*
|
||||
* @param thread the thread value to filter for.
|
||||
*/
|
||||
public ThreadFilter(String thread) {
|
||||
if (thread == null) {
|
||||
throw new IllegalArgumentException("Thread cannot be null.");
|
||||
}
|
||||
this.thread = thread;
|
||||
}
|
||||
|
||||
public boolean accept(Packet packet) {
|
||||
if (packet instanceof Message) {
|
||||
return thread.equals(((Message)packet).getThread());
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
82
source/org/jivesoftware/smack/filter/ToContainsFilter.java
Normal file
82
source/org/jivesoftware/smack/filter/ToContainsFilter.java
Normal file
|
@ -0,0 +1,82 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.filter;
|
||||
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
|
||||
/**
|
||||
* Filters for packets where the "to" field contains a specified value. For example,
|
||||
* the filter could be used to listen for all packets sent to a group chat nickname.
|
||||
*
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class ToContainsFilter implements PacketFilter {
|
||||
|
||||
private String to;
|
||||
|
||||
/**
|
||||
* Creates a "to" contains filter using the "to" field part.
|
||||
*
|
||||
* @param to the to field value the packet must contain.
|
||||
*/
|
||||
public ToContainsFilter(String to) {
|
||||
if (to == null) {
|
||||
throw new IllegalArgumentException("Parameter cannot be null.");
|
||||
}
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
public boolean accept(Packet packet) {
|
||||
return packet.getTo().indexOf(to) != -1;
|
||||
}
|
||||
}
|
168
source/org/jivesoftware/smack/packet/Authentication.java
Normal file
168
source/org/jivesoftware/smack/packet/Authentication.java
Normal file
|
@ -0,0 +1,168 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.packet;
|
||||
|
||||
/**
|
||||
* Authentication packet, which can be used to login to a Jabber server as well as discover
|
||||
* login information from the server.
|
||||
*/
|
||||
public class Authentication extends IQ {
|
||||
|
||||
private IQ.Type type;
|
||||
private String username = null;
|
||||
private String password = null;
|
||||
private String resource = null;
|
||||
|
||||
/**
|
||||
* Creates a new Authentication object with the specified type.
|
||||
*
|
||||
* @param type the Type of authentication packet.
|
||||
*/
|
||||
public Authentication(IQ.Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Authentication(String username, String password, String resource) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type of the authentication packet.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the type of the authentication packet.
|
||||
*
|
||||
* @param type
|
||||
*/
|
||||
public void setType(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the username.
|
||||
*
|
||||
* @return the username.
|
||||
*/
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the username.
|
||||
*
|
||||
* @param username the username.
|
||||
*/
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the password.
|
||||
*
|
||||
* @return the password.
|
||||
*/
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the password.
|
||||
*
|
||||
* @param password the password.
|
||||
*/
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the resource.
|
||||
*
|
||||
* @return the resource.
|
||||
*/
|
||||
public String getResource() {
|
||||
return resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the resource.
|
||||
*
|
||||
* @param resource the resource.
|
||||
*/
|
||||
public void setResource(String resource) {
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
public String getQueryXML() {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("<query xmlns='jabber:iq:auth'>");
|
||||
if (username != null) {
|
||||
buf.append("<username>").append( username).append("</username>");
|
||||
}
|
||||
if (password != null) {
|
||||
buf.append("<password>").append(password).append("</password>");
|
||||
}
|
||||
if (resource != null) {
|
||||
buf.append("<resource>").append(resource).append("</resource>");
|
||||
}
|
||||
buf.append("</query>");
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
141
source/org/jivesoftware/smack/packet/Error.java
Normal file
141
source/org/jivesoftware/smack/packet/Error.java
Normal file
|
@ -0,0 +1,141 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.packet;
|
||||
|
||||
/**
|
||||
* Represents a XMPP error subpacket. Typically, a server responds to a request that has
|
||||
* problems by sending the packet back and including an error packet. Each error has a code
|
||||
* as well as as an optional text explanation. Typical error codes are as follows:
|
||||
*
|
||||
* <table border=1>
|
||||
* <tr><td><b>Code</b></td><td><b>Description</b></td></tr>
|
||||
* <tr><td> 302 </td><td> Redirect </td></tr>
|
||||
* <tr><td> 400 </td><td> Bad Request </td></tr>
|
||||
* <tr><td> 401 </td><td> Unauthorized </td></tr>
|
||||
* <tr><td> 402 </td><td> Payment Required </td></tr>
|
||||
* <tr><td> 403 </td><td> Forbidden </td></tr>
|
||||
* <tr><td> 404 </td><td> Not Found </td></tr>
|
||||
* <tr><td> 405 </td><td> Not Allowed </td></tr>
|
||||
* <tr><td> 406 </td><td> Not Acceptable </td></tr>
|
||||
* <tr><td> 407 </td><td> Registration Required </td></tr>
|
||||
* <tr><td> 408 </td><td> Request Timeout </td></tr>
|
||||
* <tr><td> 409 </td><td> Conflict </td></tr>
|
||||
* <tr><td> 500 </td><td> Internal Server Error </td></tr>
|
||||
* <tr><td> 501 </td><td> Not Implemented </td></tr>
|
||||
* <tr><td> 502 </td><td> Remote Server Error </td></tr>
|
||||
* <tr><td> 503 </td><td> Service Unavailable </td></tr>
|
||||
* <tr><td> 504 </td><td> Remote Server Timeout </td></tr>
|
||||
* </table>
|
||||
*
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class Error {
|
||||
|
||||
private int code;
|
||||
private String message;
|
||||
|
||||
/**
|
||||
* Creates a new error with the specified code and no message..
|
||||
*
|
||||
* @param code the error code.
|
||||
*/
|
||||
public Error (int code) {
|
||||
this.code = code;
|
||||
this.message = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new error with the specified code and message.
|
||||
*
|
||||
* @param code the error code.
|
||||
* @param message a message describing the error.
|
||||
*/
|
||||
public Error(int code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the error code.
|
||||
*
|
||||
* @return the error code.
|
||||
*/
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the message describing the error, or null if there is no message.
|
||||
*
|
||||
* @return the message describing the error, or null if there is no message.
|
||||
*/
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the error as XML.
|
||||
*
|
||||
* @return the error as XML.
|
||||
*/
|
||||
public String toXML() {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("<error>");
|
||||
buf.append("<code>").append(code).append("</code>");
|
||||
if (message != null) {
|
||||
buf.append("<message>").append(message).append("</message>");
|
||||
}
|
||||
buf.append("</error>");
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
164
source/org/jivesoftware/smack/packet/IQ.java
Normal file
164
source/org/jivesoftware/smack/packet/IQ.java
Normal file
|
@ -0,0 +1,164 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.packet;
|
||||
|
||||
/**
|
||||
* A generic IQ packet.
|
||||
*
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public abstract class IQ extends Packet {
|
||||
|
||||
private Type type = Type.GET;
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String toXML() {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("<iq ");
|
||||
if (getPacketID() != null) {
|
||||
buf.append("id=\"" + getPacketID() + "\" ");
|
||||
}
|
||||
if (getTo() != null) {
|
||||
buf.append("to=\"").append(getTo()).append("\" ");
|
||||
}
|
||||
if (getFrom() != null) {
|
||||
buf.append("from=\"").append(getFrom()).append("\" ");
|
||||
}
|
||||
if (type == null) {
|
||||
buf.append("type=\"get\">");
|
||||
}
|
||||
else {
|
||||
buf.append("type=\"").append(getType()).append("\">");
|
||||
}
|
||||
// Add the query section if there is one.
|
||||
String queryXML = getQueryXML();
|
||||
if (queryXML != null) {
|
||||
buf.append(queryXML);
|
||||
}
|
||||
buf.append("</iq>");
|
||||
// Add packet properties, if any are defined.
|
||||
String propertiesXML = getPropertiesXML();
|
||||
if (propertiesXML != null) {
|
||||
buf.append(propertiesXML);
|
||||
}
|
||||
// Add the error sub-packet, if there is one.
|
||||
Error error = getError();
|
||||
if (error != null) {
|
||||
buf.append(error.toXML());
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementations of this method should return the XML of the query section of
|
||||
* an IQ packet if that section should exist, or <tt>null</tt> otherwise. This lets
|
||||
* the majority of IQ XML writing be generic, with each sub-class providing just the
|
||||
* packet specific XML.
|
||||
*
|
||||
* @return the query section of the IQ XML.
|
||||
*/
|
||||
public abstract String getQueryXML();
|
||||
|
||||
/**
|
||||
* A class to represent the type of the IQ packet. Valid types are:
|
||||
*
|
||||
* <ul>
|
||||
* <li>IQ.Type.GET
|
||||
* <li>IQ.Type.SET
|
||||
* <li>IQ.Type.RESULT
|
||||
* <li>IQ.Type.ERROR
|
||||
* </ul>
|
||||
*/
|
||||
public static class Type {
|
||||
|
||||
public static final Type GET = new Type("get");
|
||||
public static final Type SET = new Type("set");
|
||||
public static final Type RESULT = new Type("result");
|
||||
public static final Type ERROR = new Type("error");
|
||||
|
||||
public static Type fromString(String type) {
|
||||
if (type.equals(GET.toString())) {
|
||||
return GET;
|
||||
}
|
||||
else if (type.equals(SET.toString())) {
|
||||
return SET;
|
||||
}
|
||||
else if (type.equals(ERROR.toString())) {
|
||||
return ERROR;
|
||||
}
|
||||
else if (type.equals(RESULT.toString())) {
|
||||
return RESULT;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String value;
|
||||
|
||||
private Type(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
320
source/org/jivesoftware/smack/packet/Message.java
Normal file
320
source/org/jivesoftware/smack/packet/Message.java
Normal file
|
@ -0,0 +1,320 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.packet;
|
||||
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Represents XMPP message packets. A message can be one of several types:
|
||||
*
|
||||
* <ul>
|
||||
* <li>NORMAL -- (Default) a normal text message used in email like interface.
|
||||
* <li>CHAT -- a typically short text message used in line-by-line chat interfaces.
|
||||
* <li>GROUP_CHAT -- a chat message sent to a groupchat server for group chats.
|
||||
* <li>HEADLINE -- a text message to be displayed in scrolling marquee displays.
|
||||
* <li>ERROR -- indicates a messaging error.
|
||||
* </ul>
|
||||
*
|
||||
* For each message type, different message fields are typically used as follows:
|
||||
*
|
||||
* <table>
|
||||
* <tr><td> </td><td><b>Message type</b></td></tr>
|
||||
* <tr><td><i>Field</i></td><td><b>Normal</b></td><td><b>Chat</b></td><td><b>Group Chat</b></td><td><b>Headline</b></td><td><b>Error</b></td></tr>
|
||||
* <tr><td><i>subject</i></td> <td>SHOULD</td><td>SHOULD NOT</td><td>SHOULD NOT</td><td>SHOULD NOT</td><td>SHOULD NOT</td></tr>
|
||||
* <tr><td><i>thread</i></td> <td>OPTIONAL</td><td>SHOULD</td><td>OPTIONAL</td><td>OPTIONAL</td><td>SHOULD NOT</td></tr>
|
||||
* <tr><td><i>body</i></td> <td>SHOULD</td><td>SHOULD</td><td>SHOULD</td><td>SHOULD</td><td>SHOULD NOT</td></tr>
|
||||
* <tr><td><i>error</i></td> <td>MUST NOT</td><td>MUST NOT</td><td>MUST NOT</td><td>MUST NOT</td><td>MUST</td></tr>
|
||||
* </table>
|
||||
*
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class Message extends Packet {
|
||||
|
||||
public static final Type NORMAL = new Type("normal");
|
||||
public static final Type CHAT = new Type("chat");
|
||||
public static final Type GROUP_CHAT = new Type("groupchat");
|
||||
public static final Type HEADLINE = new Type("headline");
|
||||
public static final Type ERROR = new Type("error");
|
||||
|
||||
private String sender = null;
|
||||
private String recipient = null;
|
||||
private Type type = NORMAL;
|
||||
private String subject = null;
|
||||
private String body = null;
|
||||
private String thread = null;
|
||||
|
||||
/**
|
||||
* Creates a new, "normal" message.
|
||||
*/
|
||||
public Message() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new "normal" message to the specified recipient.
|
||||
*
|
||||
* @param recipient the recipient of the message.
|
||||
*/
|
||||
public Message(String recipient) {
|
||||
this.recipient = recipient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new message of the specified type to a recipient.
|
||||
*
|
||||
* @param recipient the user to send the message to.
|
||||
* @param type the message type.
|
||||
*/
|
||||
public Message(String recipient, Type type) {
|
||||
if (recipient == null || type == null) {
|
||||
throw new IllegalArgumentException("Parameters cannot be null.");
|
||||
}
|
||||
this.recipient = recipient;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sender of the message, or null if the sender has not been set.
|
||||
*
|
||||
* @return the sender of the message.
|
||||
*/
|
||||
public String getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the sender of the message.
|
||||
*
|
||||
* @param sender the sender of the message.
|
||||
*/
|
||||
public void setSender(String sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the recipient of the message, or null if the recipient has not been set.
|
||||
*
|
||||
* @return the recipient of the message.
|
||||
*/
|
||||
public String getRecipient() {
|
||||
return recipient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the recipient of the message.
|
||||
*
|
||||
* @param recipient the recipient of the message.
|
||||
*/
|
||||
public void setRecipient(String recipient) {
|
||||
if (recipient == null) {
|
||||
throw new IllegalArgumentException("Recipient cannot be null.");
|
||||
}
|
||||
this.recipient = recipient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type of the message.
|
||||
*
|
||||
* @return the type of the message.
|
||||
*/
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the type of the message.
|
||||
*
|
||||
* @param type the type of the message.
|
||||
*/
|
||||
public void setType(Type type) {
|
||||
if (type == null) {
|
||||
throw new IllegalArgumentException("Type cannot be null.");
|
||||
}
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the subject of the message, or null if the subject has not been set.
|
||||
* The subject is a short description of message contents.
|
||||
*
|
||||
* @return the subject of the message.
|
||||
*/
|
||||
public String getSubject() {
|
||||
return subject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the subject of the message. The subject is a short description of
|
||||
* message contents.
|
||||
*
|
||||
* @param subject the subject of the message.
|
||||
*/
|
||||
public void setSubject(String subject) {
|
||||
this.subject = subject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the body of the message, or null if the body has not been set. The body
|
||||
* is the main message contents.
|
||||
*
|
||||
* @return the body of the message.
|
||||
*/
|
||||
public String getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the body of the message. The body is the main message contents.
|
||||
* @param body
|
||||
*/
|
||||
public void setBody(String body) {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the thread id of the message, which is a unique identifier for a sequence
|
||||
* of "chat" messages.
|
||||
*
|
||||
* @return the thread id of the message.
|
||||
*/
|
||||
public String getThread() {
|
||||
return thread;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the thread id of the message, which is a unique identifier for a sequence
|
||||
* of "chat" messages.
|
||||
*
|
||||
* @param thread the thread id of the message.
|
||||
*/
|
||||
public void setThread(String thread) {
|
||||
this.thread = thread;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the message as XML.
|
||||
*
|
||||
* @return the message as XML.
|
||||
*/
|
||||
public String toXML() {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("<message");
|
||||
buf.append(" id=\"").append(getPacketID()).append("\"");
|
||||
if (recipient != null) {
|
||||
buf.append(" to=\"").append(recipient).append("\"");
|
||||
}
|
||||
if (sender != null) {
|
||||
buf.append(" from=\"").append(sender).append("\"");
|
||||
}
|
||||
if (type != NORMAL) {
|
||||
buf.append(" type=\"").append(type).append("\"");
|
||||
}
|
||||
buf.append(">");
|
||||
if (subject != null) {
|
||||
buf.append("<subject>").append(StringUtils.escapeForXML(subject)).append("</subject>");
|
||||
}
|
||||
if (body != null) {
|
||||
buf.append("<body>").append(StringUtils.escapeForXML(body)).append("</body>");
|
||||
}
|
||||
if (thread != null) {
|
||||
buf.append("<thread>").append(thread).append("</thread>");
|
||||
}
|
||||
// Append the error subpacket if the message type is an error.
|
||||
if (type == ERROR) {
|
||||
Error error = getError();
|
||||
if (error != null) {
|
||||
buf.append(error.toXML());
|
||||
}
|
||||
}
|
||||
String properties = getPropertiesXML();
|
||||
if (properties != null) {
|
||||
buf.append(properties);
|
||||
}
|
||||
buf.append("</message>");
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the type of a message.
|
||||
*/
|
||||
public static class Type {
|
||||
|
||||
public static Type fromString(String type) {
|
||||
if (type.equals(CHAT.toString())) {
|
||||
return CHAT;
|
||||
}
|
||||
else if (type.equals(GROUP_CHAT.toString())) {
|
||||
return GROUP_CHAT;
|
||||
}
|
||||
else if (type.equals(HEADLINE.toString())) {
|
||||
return HEADLINE;
|
||||
}
|
||||
else if (type.equals(ERROR.toString())) {
|
||||
return ERROR;
|
||||
}
|
||||
else {
|
||||
return NORMAL;
|
||||
}
|
||||
}
|
||||
|
||||
private String value;
|
||||
|
||||
private Type(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
348
source/org/jivesoftware/smack/packet/Packet.java
Normal file
348
source/org/jivesoftware/smack/packet/Packet.java
Normal file
|
@ -0,0 +1,348 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.packet;
|
||||
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Base class for XMPP packets. Every packet has a unique ID (which is automatically
|
||||
* generated, but can be overriden). Optionally, the "to" and "from" fields can be set,
|
||||
* as well as an arbitrary number of properties.
|
||||
*
|
||||
* Properties provide an easy mechanism for clients to share data. Each property has a
|
||||
* String name, and a value that is a Java primitive (int, long, float, double, boolean)
|
||||
* or any Serializable object (a Java object is Serializable when it implements the
|
||||
* Serializable interface).
|
||||
*
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public abstract class Packet {
|
||||
|
||||
/**
|
||||
* A prefix helps to make sure that ID's are unique across mutliple instances.
|
||||
*/
|
||||
private static String prefix = StringUtils.randomString(3);
|
||||
|
||||
/**
|
||||
* Keeps track of the current increment, which is appended to the prefix to
|
||||
* forum a unique ID.
|
||||
*/
|
||||
private static long id = 0;
|
||||
|
||||
/**
|
||||
* Returns the next unique id. Each id made up of a short alphanumeric
|
||||
* prefix along with a unique numeric value.
|
||||
*
|
||||
* @return the next id.
|
||||
*/
|
||||
private static synchronized String nextID() {
|
||||
return prefix + Long.toString(id++);
|
||||
}
|
||||
|
||||
private String packetID = nextID();
|
||||
private String to = null;
|
||||
private String from = null;
|
||||
private Map properties = new HashMap();
|
||||
private Error error = null;
|
||||
|
||||
/**
|
||||
* Returns the unique ID of the packet.
|
||||
*
|
||||
* @return the packet's unique ID.
|
||||
*/
|
||||
public String getPacketID() {
|
||||
return packetID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the unique ID of the packet.
|
||||
*
|
||||
* @param packetID the unique ID for the packet.
|
||||
*/
|
||||
public void setPacketID(String packetID) {
|
||||
this.packetID = packetID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns who the packet is being sent "to", or <tt>null</tt> if
|
||||
* the value is not set. The XMPP protocol often makes the "to"
|
||||
* attribute optional, so it does not always need to be set.
|
||||
*
|
||||
* @return who the packet is being sent to, or <tt>null</tt> if the
|
||||
* value has not been set.
|
||||
*/
|
||||
public String getTo() {
|
||||
return to;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets who the packet is being sent "to". The XMPP protocol often makes
|
||||
* the "to" attribute optional, so it does not always need to be set.
|
||||
*
|
||||
* @param to who the packet is being sent to.
|
||||
*/
|
||||
public void setTo(String to) {
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns who the packet is being sent "from" or <tt>null</tt> if
|
||||
* the value is not set. The XMPP protocol often makes the "from"
|
||||
* attribute optional, so it does not always need to be set.
|
||||
*
|
||||
* @return who the packet is being sent from, or <tt>null</tt> if the
|
||||
* valud has not been set.
|
||||
*/
|
||||
public String getFrom() {
|
||||
return from;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets who the packet is being sent "from". The XMPP protocol often
|
||||
* makes the "from" attribute optional, so it does not always need to
|
||||
* be set.
|
||||
*
|
||||
* @param from who the packet is being sent to.
|
||||
*/
|
||||
public void setFrom(String from) {
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the error associated with this packet, or <tt>null</tt> if there are
|
||||
* no errors.
|
||||
*
|
||||
* @return the error sub-packet or <tt>null</tt> if there isn't an error.
|
||||
*/
|
||||
public Error getError() {
|
||||
return error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the error for this packet.
|
||||
*
|
||||
* @param error the error to associate with this packet.
|
||||
*/
|
||||
public void setError(Error error) {
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the packet property with the specified name or <tt>null</tt> if the
|
||||
* property doesn't exist. Property values that were orginally primitives will
|
||||
* be returned as their object equivalent. For example, an int property will be
|
||||
* returned as an Integer, a double as a Double, etc.
|
||||
*
|
||||
* @param name the name of the property.
|
||||
* @return the property, or <tt>null</tt> if the property doesn't exist.
|
||||
*/
|
||||
public synchronized Object getProperty(String name) {
|
||||
return properties.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a packet property with an int value.
|
||||
*
|
||||
* @param name the name of the property.
|
||||
* @param value the value of the property.
|
||||
*/
|
||||
public void setProperty(String name, int value) {
|
||||
setProperty(name, new Integer(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a packet property with a long value.
|
||||
*
|
||||
* @param name the name of the property.
|
||||
* @param value the value of the property.
|
||||
*/
|
||||
public void setProperty(String name, long value) {
|
||||
setProperty(name, new Long(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a packet property with a float value.
|
||||
*
|
||||
* @param name the name of the property.
|
||||
* @param value the value of the property.
|
||||
*/
|
||||
public void setProperty(String name, float value) {
|
||||
setProperty(name, new Float(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a packet property with a double value.
|
||||
*
|
||||
* @param name the name of the property.
|
||||
* @param value the value of the property.
|
||||
*/
|
||||
public void setProperty(String name, double value) {
|
||||
setProperty(name, new Double(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a packet property with a bboolean value.
|
||||
*
|
||||
* @param name the name of the property.
|
||||
* @param value the value of the property.
|
||||
*/
|
||||
public void setProperty(String name, boolean value) {
|
||||
setProperty(name, new Boolean(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a property with an Object as the value. The value must be Serializable
|
||||
* or an IllegalArgumentException will be thrown.
|
||||
*
|
||||
* @param name the name of the property.
|
||||
* @param value the value of the property.
|
||||
*/
|
||||
public synchronized void setProperty(String name, Object value) {
|
||||
if (!(value instanceof Serializable)) {
|
||||
throw new IllegalArgumentException("Value must be serialiazble");
|
||||
}
|
||||
properties.put(name, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a property.
|
||||
*
|
||||
* @param name the name of the property to delete.
|
||||
*/
|
||||
public synchronized void deleteProperty(String name) {
|
||||
properties.remove(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an Iterator for all the property names that are set.
|
||||
*
|
||||
* @return an Iterator for all property names.
|
||||
*/
|
||||
public synchronized Iterator getPropertyNames() {
|
||||
return properties.keySet().iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the packet as XML. Every concrete extension of Packet must implement
|
||||
* this method. In addition to writing out packet-specific data, each extension should
|
||||
* also write out the error and the properties data if they are defined.
|
||||
*
|
||||
* @return the XML format of the packet as a String.
|
||||
*/
|
||||
public abstract String toXML();
|
||||
|
||||
/**
|
||||
* Returns the properties portion of the packet as XML or <tt>null</tt> if there are
|
||||
* no properties.
|
||||
*
|
||||
* @return the properties data as XML or <tt>null</tt> if there are no properties.
|
||||
*/
|
||||
protected synchronized String getPropertiesXML() {
|
||||
// Return null if there are no properties.
|
||||
if (properties.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("<x xmlns=\"http://www.jivesoftware.com/xmlns/xmpp/properties\">");
|
||||
// Loop through all properties and write them out.
|
||||
for (Iterator i=getPropertyNames(); i.hasNext(); ) {
|
||||
String name = (String)i.next();
|
||||
Object value = getProperty(name);
|
||||
buf.append("<property>");
|
||||
buf.append("<name>").append(StringUtils.escapeForXML(name)).append("</name>");
|
||||
buf.append("<value type=\"");
|
||||
if (value instanceof Integer) {
|
||||
buf.append("integer\">").append(value).append("</value>");
|
||||
}
|
||||
else if (value instanceof Long) {
|
||||
buf.append("long\">").append(value).append("</value>");
|
||||
}
|
||||
else if (value instanceof Float) {
|
||||
buf.append("float\">").append(value).append("</value>");
|
||||
}
|
||||
else if (value instanceof Double) {
|
||||
buf.append("double\">").append(value).append("</value>");
|
||||
}
|
||||
else if (value instanceof Boolean) {
|
||||
buf.append("boolean\">").append(value).append("</value>");
|
||||
}
|
||||
else if (value instanceof String) {
|
||||
buf.append("string\">");
|
||||
buf.append(StringUtils.escapeForXML((String)value));
|
||||
buf.append("</value>");
|
||||
}
|
||||
// Otherwise, it's a generic Serializable object. Serialized objects are in
|
||||
// a binary format, which won't work well inside of XML. Therefore, we base-64
|
||||
// encode the binary data before adding it to the SOAP payload.
|
||||
else {
|
||||
try {
|
||||
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
|
||||
ObjectOutputStream out = new ObjectOutputStream(byteStream);
|
||||
out.writeObject(value);
|
||||
String encodedVal = StringUtils.encodeBase64(byteStream.toByteArray());
|
||||
buf.append("java-object\">");
|
||||
buf.append(encodedVal).append("</value");
|
||||
}
|
||||
catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
buf.append("</property>");
|
||||
}
|
||||
buf.append("</x>");
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
263
source/org/jivesoftware/smack/packet/Presence.java
Normal file
263
source/org/jivesoftware/smack/packet/Presence.java
Normal file
|
@ -0,0 +1,263 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.packet;
|
||||
|
||||
import org.jivesoftware.smack.*;
|
||||
|
||||
/**
|
||||
* Represents XMPP presence packets. Each packet will either be an "available" or
|
||||
* "unavailable" message. A number of attributes are optional:
|
||||
* <ul>
|
||||
* <li>Status -- free-form text describing a user's presence (i.e., gone to lunch).
|
||||
* <li>Priority -- non-negative numerical priority of a sender's resource. The
|
||||
* highest resource priority is the default recipient of packets not addressed
|
||||
* to a particular resource.
|
||||
* <li>Mode -- one of four presence modes: chat, away, xa (extended away, and
|
||||
* dnd (do not disturb).
|
||||
* </ul>
|
||||
*
|
||||
* Note: XMPP presence packets are also used to subscribe and unsubscribe users from
|
||||
* rosters. However, that functionality is controlled by the Roster object rather
|
||||
* than Presence objects.
|
||||
*
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class Presence extends Packet {
|
||||
|
||||
public static final Mode CHAT = new Mode("chat");
|
||||
public static final Mode AWAY = new Mode("away");
|
||||
public static final Mode EXTENDED_AWAY = new Mode("xa");
|
||||
public static final Mode DO_NOT_DISTURB = new Mode("dnd");
|
||||
public static final Mode INVISIBLE = new Mode("invisible");
|
||||
|
||||
private boolean available = true;
|
||||
private String status = null;
|
||||
private int priority = -1;
|
||||
private Mode mode = null;
|
||||
|
||||
/**
|
||||
* Creates a new presence update. Status, priority, and mode are left un-set.
|
||||
*
|
||||
* @param available true if this is an "available" presence packet, or false for
|
||||
* "unavailable".
|
||||
*/
|
||||
public Presence(boolean available) {
|
||||
this.available = available;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new presence update with a specified status, priority, and mode.
|
||||
*
|
||||
* @param available true if this is an "available" presence update, or false for
|
||||
* "unavailable".
|
||||
* @param status a text message describing the presence update.
|
||||
* @param priority the priority of this presence update.
|
||||
* @param mode the mode type for this presence update.
|
||||
*/
|
||||
public Presence(boolean available, String status, int priority, Mode mode) {
|
||||
this.available = available;
|
||||
this.status = status;
|
||||
this.priority = priority;
|
||||
this.mode = mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this is an "available" presence update and false if "unavailable".
|
||||
*
|
||||
* @return true if an "available" presence update, false otherwise.
|
||||
*/
|
||||
public boolean isAvailable() {
|
||||
return available;
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggles the presence update between "available" and "unavailable".
|
||||
*
|
||||
* @param available true to make this an "available" presence update, or false
|
||||
* for "unavailable".
|
||||
*/
|
||||
public void setAvailable(boolean available) {
|
||||
this.available = available;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the status message of the presence update, or <tt>null</tt> if there
|
||||
* is not status. The status is free-form text describing a user's presence
|
||||
* (i.e., "gone to lunch").
|
||||
*
|
||||
* @return the status message.
|
||||
*/
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the status message of the presence update. The status is free-form text
|
||||
* describing a user's presence (i.e., gone to lunch).
|
||||
*
|
||||
* @param status the status message.
|
||||
*/
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the priority of the presence, or -1 if no priority has been set.
|
||||
*
|
||||
* @return the priority.
|
||||
*/
|
||||
public int getPriority() {
|
||||
return priority;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the priority of the presence.
|
||||
*
|
||||
* @param priority the priority of the presence.
|
||||
*/
|
||||
public void setPriority(int priority) {
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the mode of the presence update, or <tt>null</tt> if no mode has been set.
|
||||
*
|
||||
* @return the mode.
|
||||
*/
|
||||
public Mode getMode() {
|
||||
return mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the mode of the presence update.
|
||||
*
|
||||
* @param mode the mode.
|
||||
*/
|
||||
public void setMode(Mode mode) {
|
||||
this.mode = mode;
|
||||
}
|
||||
|
||||
public String toXML() {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("<presence ");
|
||||
if (getPacketID() != null) {
|
||||
buf.append("id=\"").append(getPacketID()).append("\" ");
|
||||
}
|
||||
if (getTo() != null) {
|
||||
buf.append("to=\"").append(getTo()).append("\" ");
|
||||
}
|
||||
if (getFrom() != null) {
|
||||
buf.append("from=\"").append(getFrom()).append("\" ");
|
||||
}
|
||||
if (available) {
|
||||
buf.append("type=\"available\">");
|
||||
}
|
||||
else {
|
||||
buf.append("type=\"unavailable\">");
|
||||
}
|
||||
if (status != null) {
|
||||
buf.append("<status>").append(status).append("</status>");
|
||||
}
|
||||
if (priority != -1) {
|
||||
buf.append("<priority>").append(priority).append("</priority>");
|
||||
}
|
||||
if (mode != null) {
|
||||
buf.append("<show>").append(mode).append("</show>");
|
||||
}
|
||||
buf.append("</presence>");
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A typsafe enum class to represent the presecence mode.
|
||||
*/
|
||||
public static class Mode {
|
||||
|
||||
private String value;
|
||||
|
||||
private Mode(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Mode constant
|
||||
*/
|
||||
public static Mode fromString(String value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
else if (value.equals("chat")) {
|
||||
return CHAT;
|
||||
}
|
||||
else if (value.equals("away")) {
|
||||
return AWAY;
|
||||
}
|
||||
else if (value.equals("xa")) {
|
||||
return EXTENDED_AWAY;
|
||||
}
|
||||
else if (value.equals("dnd")) {
|
||||
return DO_NOT_DISTURB;
|
||||
}
|
||||
else if (value.equals("invisible")) {
|
||||
return INVISIBLE;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
364
source/org/jivesoftware/smack/util/StringUtils.java
Normal file
364
source/org/jivesoftware/smack/util/StringUtils.java
Normal file
|
@ -0,0 +1,364 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@coolservlets.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.util;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* A collection of utility methods for String objects.
|
||||
*/
|
||||
public class StringUtils {
|
||||
|
||||
private static final char[] QUOTE_ENCODE = """.toCharArray();
|
||||
private static final char[] AMP_ENCODE = "&".toCharArray();
|
||||
private static final char[] LT_ENCODE = "<".toCharArray();
|
||||
private static final char[] GT_ENCODE = ">".toCharArray();
|
||||
|
||||
/**
|
||||
* Escapes all necessary characters in the String so that it can be used
|
||||
* in an XML doc.
|
||||
*
|
||||
* @param string the string to escape.
|
||||
* @return the string with appropriate characters escaped.
|
||||
*/
|
||||
public static final String escapeForXML(String string) {
|
||||
if (string == null) {
|
||||
return null;
|
||||
}
|
||||
char ch;
|
||||
int i=0;
|
||||
int last=0;
|
||||
char[] input = string.toCharArray();
|
||||
int len = input.length;
|
||||
StringBuffer out = new StringBuffer((int)(len*1.3));
|
||||
for (; i < len; i++) {
|
||||
ch = input[i];
|
||||
if (ch > '>') {
|
||||
continue;
|
||||
}
|
||||
else if (ch == '<') {
|
||||
if (i > last) {
|
||||
out.append(input, last, i - last);
|
||||
}
|
||||
last = i + 1;
|
||||
out.append(LT_ENCODE);
|
||||
}
|
||||
else if (ch == '>') {
|
||||
if (i > last) {
|
||||
out.append(input, last, i - last);
|
||||
}
|
||||
last = i + 1;
|
||||
out.append(GT_ENCODE);
|
||||
}
|
||||
|
||||
else if (ch == '&') {
|
||||
if (i > last) {
|
||||
out.append(input, last, i - last);
|
||||
}
|
||||
last = i + 1;
|
||||
out.append(AMP_ENCODE);
|
||||
}
|
||||
else if (ch == '"') {
|
||||
if (i > last) {
|
||||
out.append(input, last, i - last);
|
||||
}
|
||||
last = i + 1;
|
||||
out.append(QUOTE_ENCODE);
|
||||
}
|
||||
}
|
||||
if (last == 0) {
|
||||
return string;
|
||||
}
|
||||
if (i > last) {
|
||||
out.append(input, last, i - last);
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Used by the hash method.
|
||||
*/
|
||||
private static MessageDigest digest = null;
|
||||
|
||||
/**
|
||||
* Hashes a String using the SHA-1 algorithm and returns the result as a
|
||||
* String of hexadecimal numbers. This method is synchronized to avoid
|
||||
* excessive MessageDigest object creation. If calling this method becomes
|
||||
* a bottleneck in your code, you may wish to maintain a pool of
|
||||
* MessageDigest objects instead of using this method.
|
||||
* <p>
|
||||
* A hash is a one-way function -- that is, given an
|
||||
* input, an output is easily computed. However, given the output, the
|
||||
* input is almost impossible to compute. This is useful for passwords
|
||||
* since we can store the hash and a hacker will then have a very hard time
|
||||
* determining the original password.
|
||||
*
|
||||
* @param data the String to compute the hash of.
|
||||
* @return a hashed version of the passed-in String
|
||||
*/
|
||||
public synchronized static final String hash(String data) {
|
||||
if (digest == null) {
|
||||
try {
|
||||
digest = MessageDigest.getInstance("SHA-1");
|
||||
}
|
||||
catch (NoSuchAlgorithmException nsae) {
|
||||
System.err.println("Failed to load the SHA-1 MessageDigest. " +
|
||||
"Jive will be unable to function normally.");
|
||||
}
|
||||
}
|
||||
// Now, compute hash.
|
||||
try {
|
||||
digest.update(data.getBytes("UTF-8"));
|
||||
}
|
||||
catch (UnsupportedEncodingException e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
return encodeHex(digest.digest());
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns an array of bytes into a String representing each byte as an
|
||||
* unsigned hex number.
|
||||
* <p>
|
||||
* Method by Santeri Paavolainen, Helsinki Finland 1996<br>
|
||||
* (c) Santeri Paavolainen, Helsinki Finland 1996<br>
|
||||
* Distributed under LGPL.
|
||||
*
|
||||
* @param bytes an array of bytes to convert to a hex-string
|
||||
* @return generated hex string
|
||||
*/
|
||||
public static final String encodeHex(byte[] bytes) {
|
||||
StringBuffer buf = new StringBuffer(bytes.length * 2);
|
||||
int i;
|
||||
|
||||
for (i = 0; i < bytes.length; i++) {
|
||||
if (((int) bytes[i] & 0xff) < 0x10) {
|
||||
buf.append("0");
|
||||
}
|
||||
buf.append(Long.toString((int) bytes[i] & 0xff, 16));
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
//*********************************************************************
|
||||
//* Base64 - a simple base64 encoder and decoder.
|
||||
//*
|
||||
//* Copyright (c) 1999, Bob Withers - bwit@pobox.com
|
||||
//*
|
||||
//* This code may be freely used for any purpose, either personal
|
||||
//* or commercial, provided the authors copyright notice remains
|
||||
//* intact.
|
||||
//*********************************************************************
|
||||
|
||||
private static final int fillchar = '=';
|
||||
private static final String cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
+ "0123456789+/";
|
||||
|
||||
/**
|
||||
* Encodes a String as a base64 String.
|
||||
*
|
||||
* @param data a String to encode.
|
||||
* @return a base64 encoded String.
|
||||
*/
|
||||
public static String encodeBase64(String data) {
|
||||
byte [] bytes = null;
|
||||
try {
|
||||
bytes = data.getBytes("ISO-8859-1");
|
||||
}
|
||||
catch (UnsupportedEncodingException uee) {
|
||||
uee.printStackTrace();
|
||||
}
|
||||
return encodeBase64(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes a byte array into a base64 String.
|
||||
*
|
||||
* @param data a byte array to encode.
|
||||
* @return a base64 encode String.
|
||||
*/
|
||||
public static String encodeBase64(byte[] data) {
|
||||
int c;
|
||||
int len = data.length;
|
||||
StringBuffer ret = new StringBuffer(((len / 3) + 1) * 4);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
c = (data[i] >> 2) & 0x3f;
|
||||
ret.append(cvt.charAt(c));
|
||||
c = (data[i] << 4) & 0x3f;
|
||||
if (++i < len)
|
||||
c |= (data[i] >> 4) & 0x0f;
|
||||
|
||||
ret.append(cvt.charAt(c));
|
||||
if (i < len) {
|
||||
c = (data[i] << 2) & 0x3f;
|
||||
if (++i < len)
|
||||
c |= (data[i] >> 6) & 0x03;
|
||||
|
||||
ret.append(cvt.charAt(c));
|
||||
}
|
||||
else {
|
||||
++i;
|
||||
ret.append((char) fillchar);
|
||||
}
|
||||
|
||||
if (i < len) {
|
||||
c = data[i] & 0x3f;
|
||||
ret.append(cvt.charAt(c));
|
||||
}
|
||||
else {
|
||||
ret.append((char) fillchar);
|
||||
}
|
||||
}
|
||||
return ret.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes a base64 String.
|
||||
*
|
||||
* @param data a base64 encoded String to decode.
|
||||
* @return the decoded String.
|
||||
*/
|
||||
public static String decodeBase64(String data) {
|
||||
byte [] bytes = null;
|
||||
try {
|
||||
bytes = data.getBytes("ISO-8859-1");
|
||||
}
|
||||
catch (UnsupportedEncodingException uee) {
|
||||
uee.printStackTrace();
|
||||
}
|
||||
return decodeBase64(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes a base64 aray of bytes.
|
||||
*
|
||||
* @param data a base64 encode byte array to decode.
|
||||
* @return the decoded String.
|
||||
*/
|
||||
public static String decodeBase64(byte[] data) {
|
||||
int c, c1;
|
||||
int len = data.length;
|
||||
StringBuffer ret = new StringBuffer((len * 3) / 4);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
c = cvt.indexOf(data[i]);
|
||||
++i;
|
||||
c1 = cvt.indexOf(data[i]);
|
||||
c = ((c << 2) | ((c1 >> 4) & 0x3));
|
||||
ret.append((char) c);
|
||||
if (++i < len) {
|
||||
c = data[i];
|
||||
if (fillchar == c)
|
||||
break;
|
||||
|
||||
c = cvt.indexOf(c);
|
||||
c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
|
||||
ret.append((char) c1);
|
||||
}
|
||||
|
||||
if (++i < len) {
|
||||
c1 = data[i];
|
||||
if (fillchar == c1)
|
||||
break;
|
||||
|
||||
c1 = cvt.indexOf(c1);
|
||||
c = ((c << 6) & 0xc0) | c1;
|
||||
ret.append((char) c);
|
||||
}
|
||||
}
|
||||
return ret.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Pseudo-random number generator object for use with randomString().
|
||||
* The Random class is not considered to be cryptographically secure, so
|
||||
* only use these random Strings for low to medium security applications.
|
||||
*/
|
||||
private static Random randGen = new Random();
|
||||
|
||||
/**
|
||||
* Array of numbers and letters of mixed case. Numbers appear in the list
|
||||
* twice so that there is a more equal chance that a number will be picked.
|
||||
* We can use the array to get a random number or letter by picking a random
|
||||
* array index.
|
||||
*/
|
||||
private static char[] numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" +
|
||||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
|
||||
|
||||
/**
|
||||
* Returns a random String of numbers and letters (lower and upper case)
|
||||
* of the specified length. The method uses the Random class that is
|
||||
* built-in to Java which is suitable for low to medium grade security uses.
|
||||
* This means that the output is only pseudo random, i.e., each number is
|
||||
* mathematically generated so is not truly random.<p>
|
||||
*
|
||||
* The specified length must be at least one. If not, the method will return
|
||||
* null.
|
||||
*
|
||||
* @param length the desired length of the random String to return.
|
||||
* @return a random String of numbers and letters of the specified length.
|
||||
*/
|
||||
public static final String randomString(int length) {
|
||||
if (length < 1) {
|
||||
return null;
|
||||
}
|
||||
// Create a char buffer to put random letters and numbers in.
|
||||
char [] randBuffer = new char[length];
|
||||
for (int i=0; i<randBuffer.length; i++) {
|
||||
randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
|
||||
}
|
||||
return new String(randBuffer);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue