1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-09-14 04:39:41 +02:00

Prefix subprojects with 'smack-'

instead of using the old baseName=smack appendix=project.name approach,
we are now going convention over configuration and renaming the
subprojects directories to the proper name.

Having a prefix is actually very helpful, because the resulting
libraries will be named like the subproject. And a core-4.0.0-rc1.jar is
not as explicit about what it actually *is* as a
smack-core-4.0.0-rc1.jar.

SMACK-265
This commit is contained in:
Florian Schmaus 2014-04-28 19:27:53 +02:00
parent b6fb1f3743
commit 91fd15ad86
758 changed files with 42 additions and 42 deletions

View file

@ -0,0 +1,116 @@
/**
*
* Copyright 2003-2005 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.packet.Version;
/**
* Ensure that stream compression (JEP-138) is correctly supported by Smack.
*
* @author Gaston Dombiak
*/
public class CompressionTest extends SmackTestCase {
public CompressionTest(String arg0) {
super(arg0);
}
/**
* Test that stream compression works fine. It is assumed that the server supports and has
* stream compression enabled.
*/
public void testSuccessCompression() throws XMPPException {
// Create the configuration for this new connection
ConnectionConfiguration config = new ConnectionConfiguration(getHost(), getPort());
config.setCompressionEnabled(true);
config.setSASLAuthenticationEnabled(true);
XMPPTCPConnection connection = new XMPPConnection(config);
connection.connect();
// Login with the test account
connection.login("user0", "user0");
assertTrue("XMPPConnection is not using stream compression", connection.isUsingCompression());
// Request the version of the server
Version version = new Version();
version.setType(IQ.Type.GET);
version.setTo(getServiceName());
// Create a packet collector to listen for a response.
PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(version.getPacketID()));
connection.sendPacket(version);
// Wait up to 5 seconds for a result.
IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Close the collector
collector.cancel();
assertNotNull("No reply was received from the server", result);
assertEquals("Incorrect IQ type from server", IQ.Type.RESULT, result.getType());
// Close connection
connection.disconnect();
}
protected int getMaxConnections() {
return 0;
}
/**
* Just create an account.
*/
protected void setUp() throws Exception {
super.setUp();
XMPPTCPConnection setupConnection = new XMPPConnection(getServiceName());
setupConnection.connect();
if (!setupConnection.getAccountManager().supportsAccountCreation())
fail("Server does not support account creation");
// Create the test account
try {
setupConnection.getAccountManager().createAccount("user0", "user0");
} catch (XMPPException e) {
// Do nothing if the accout already exists
if (e.getXMPPError().getCode() != 409) {
throw e;
}
}
}
/**
* Deletes the created account for the test.
*/
protected void tearDown() throws Exception {
super.tearDown();
XMPPTCPConnection setupConnection = createConnection();
setupConnection.connect();
setupConnection.login("user0", "user0");
// Delete the created account for the test
setupConnection.getAccountManager().deleteAccount();
// Close the setupConnection
setupConnection.disconnect();
}
}

View file

@ -0,0 +1,119 @@
/**
*
* Copyright 2006 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.filetransfer.*;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.concurrent.SynchronousQueue;
/**
*
*/
public class FileTransferTest extends SmackTestCase {
int receiveCount = -1;
Exception exception;
public FileTransferTest(String arg0) {
super(arg0);
}
public void testInbandFileTransfer() throws Exception {
FileTransferNegotiator.IBB_ONLY = true;
try {
testFileTransfer();
}
finally {
FileTransferNegotiator.IBB_ONLY = false;
}
}
public void testFileTransfer() throws Exception {
final byte [] testTransfer = "This is a test transfer".getBytes();
final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();
FileTransferManager manager1 = new FileTransferManager(getConnection(0));
manager1.addFileTransferListener(new FileTransferListener() {
public void fileTransferRequest(final FileTransferRequest request) {
new Thread(new Runnable() {
public void run() {
IncomingFileTransfer transfer = request.accept();
InputStream stream;
try {
stream = transfer.recieveFile();
}
catch (XMPPException e) {
exception = e;
return;
}
byte [] testRecieve = new byte[testTransfer.length];
int receiveCount = 0;
try {
while (receiveCount != -1) {
receiveCount = stream.read(testRecieve);
}
}
catch (IOException e) {
exception = e;
}
finally {
try {
stream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
try {
queue.put(testRecieve);
}
catch (InterruptedException e) {
exception = e;
}
}
}).start();
}
});
// Send the file from user1 to user0
FileTransferManager manager2 = new FileTransferManager(getConnection(1));
OutgoingFileTransfer outgoing = manager2.createOutgoingFileTransfer(getFullJID(0));
OutputStream stream =
outgoing.sendFile("test.txt", testTransfer.length, "The great work of robin hood");
stream.write(testTransfer);
stream.flush();
stream.close();
if(exception != null) {
exception.printStackTrace();
fail();
}
byte [] array = queue.take();
assertEquals("Recieved file not equal to sent file.", testTransfer, array);
}
protected int getMaxConnections() {
return 2;
}
}

View file

@ -0,0 +1,157 @@
/**
*
* Copyright 2004 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.ThreadFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.test.SmackTestCase;
/**
* Tests the DataForms extensions.
*
* @author Gaston Dombiak
*/
public class FormTest extends SmackTestCase {
public FormTest(String arg0) {
super(arg0);
}
/**
* 1. Create a form to fill out and send it to the other user
* 2. Retrieve the form to fill out, complete it and return it to the requestor
* 3. Retrieve the completed form and check that everything is OK
*/
public void testFilloutForm() {
Form formToSend = new Form(Form.TYPE_FORM);
formToSend.setInstructions(
"Fill out this form to report your case.\nThe case will be created automatically.");
formToSend.setTitle("Case configurations");
// Add a hidden variable
FormField field = new FormField("hidden_var");
field.setType(FormField.TYPE_HIDDEN);
field.addValue("Some value for the hidden variable");
formToSend.addField(field);
// Add a fixed variable
field = new FormField();
field.addValue("Section 1: Case description");
formToSend.addField(field);
// Add a text-single variable
field = new FormField("name");
field.setLabel("Enter a name for the case");
field.setType(FormField.TYPE_TEXT_SINGLE);
formToSend.addField(field);
// Add a text-multi variable
field = new FormField("description");
field.setLabel("Enter a description");
field.setType(FormField.TYPE_TEXT_MULTI);
formToSend.addField(field);
// Add a boolean variable
field = new FormField("time");
field.setLabel("Is this your first case?");
field.setType(FormField.TYPE_BOOLEAN);
formToSend.addField(field);
// Add a text variable where an int value is expected
field = new FormField("age");
field.setLabel("How old are you?");
field.setType(FormField.TYPE_TEXT_SINGLE);
formToSend.addField(field);
// Create the chats between the two participants
Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
PacketCollector collector = getConnection(0).createPacketCollector(
new ThreadFilter(chat.getThreadID()));
PacketCollector collector2 = getConnection(1).createPacketCollector(
new ThreadFilter(chat.getThreadID()));
Message msg = new Message();
msg.setBody("To enter a case please fill out this form and send it back to me");
msg.addExtension(formToSend.getDataFormToSend());
try {
// Send the message with the form to fill out
chat.sendMessage(msg);
// Get the message with the form to fill out
Message msg2 = (Message)collector2.nextResult(2000);
assertNotNull("Messge not found", msg2);
// Retrieve the form to fill out
Form formToRespond = Form.getFormFrom(msg2);
assertNotNull(formToRespond);
assertNotNull(formToRespond.getField("name"));
assertNotNull(formToRespond.getField("description"));
// Obtain the form to send with the replies
Form completedForm = formToRespond.createAnswerForm();
assertNotNull(completedForm.getField("hidden_var"));
// Check that a field of type String does not accept booleans
try {
completedForm.setAnswer("name", true);
fail("A boolean value was set to a field of type String");
}
catch (IllegalArgumentException e) {
}
completedForm.setAnswer("name", "Credit card number invalid");
completedForm.setAnswer(
"description",
"The ATM says that my credit card number is invalid. What's going on?");
completedForm.setAnswer("time", true);
completedForm.setAnswer("age", 20);
// Create a new message to send with the completed form
msg2 = new Message();
msg2.setTo(msg.getFrom());
msg2.setThread(msg.getThread());
msg2.setType(Message.Type.chat);
msg2.setBody("To enter a case please fill out this form and send it back to me");
// Add the completed form to the message
msg2.addExtension(completedForm.getDataFormToSend());
// Send the message with the completed form
getConnection(1).sendPacket(msg2);
// Get the message with the completed form
Message msg3 = (Message) collector.nextResult(2000);
assertNotNull("Messge not found", msg3);
// Retrieve the completed form
completedForm = Form.getFormFrom(msg3);
assertNotNull(completedForm);
assertNotNull(completedForm.getField("name"));
assertNotNull(completedForm.getField("description"));
assertEquals(
completedForm.getField("name").getValues().next(),
"Credit card number invalid");
assertNotNull(completedForm.getField("time"));
assertNotNull(completedForm.getField("age"));
assertEquals("The age is bad", "20", completedForm.getField("age").getValues().next());
}
catch (XMPPException ex) {
fail(ex.getMessage());
}
finally {
collector.cancel();
collector2.cancel();
}
}
protected int getMaxConnections() {
return 2;
}
}

View file

@ -0,0 +1,84 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
/**
*
*
* @author Matt Tucker
*/
public class GroupChatInvitationTest extends SmackTestCase {
private PacketCollector collector = null;
/**
* Constructor for GroupChatInvitationTest.
* @param arg0
*/
public GroupChatInvitationTest(String arg0) {
super(arg0);
}
public void testInvitation() {
try {
GroupChatInvitation invitation = new GroupChatInvitation("test@" + getChatDomain());
Message message = new Message(getBareJID(1));
message.setBody("Group chat invitation!");
message.addExtension(invitation);
getConnection(0).sendPacket(message);
Thread.sleep(250);
Message result = (Message)collector.pollResult();
assertNotNull("Message not delivered correctly.", result);
GroupChatInvitation resultInvite = (GroupChatInvitation)result.getExtension("x",
"jabber:x:conference");
assertEquals("Invitation not to correct room", "test@" + getChatDomain(),
resultInvite.getRoomAddress());
}
catch (Exception e) {
fail(e.getMessage());
}
}
protected void setUp() throws Exception {
super.setUp();
// Register listener for groupchat invitations.
PacketFilter filter = new PacketExtensionFilter("x", "jabber:x:conference");
collector = getConnection(1).createPacketCollector(filter);
}
protected void tearDown() throws Exception {
// Cancel the packet collector so that no more results are queued up
collector.cancel();
super.tearDown();
}
protected int getMaxConnections() {
return 2;
}
}

View file

@ -0,0 +1,157 @@
/**
*
* Copyright 2003-2006 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import org.jivesoftware.smack.TCPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.packet.LastActivity;
public class LastActivityManagerTest extends SmackTestCase {
/**
* This is a test to check if a LastActivity request for idle time is
* answered and correct.
*/
public void testOnline() {
TCPConnection conn0 = getConnection(0);
TCPConnection conn1 = getConnection(1);
// Send a message as the last activity action from connection 1 to
// connection 0
conn1.sendPacket(new Message(getBareJID(0)));
// Wait 1 seconds to have some idle time
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
fail("Thread sleep interrupted");
}
LastActivity lastActivity = null;
try {
lastActivity = LastActivityManager.getLastActivity(conn0, getFullJID(1));
} catch (XMPPException e) {
e.printStackTrace();
fail("An error occurred requesting the Last Activity");
}
// Asserts that the last activity packet was received
assertNotNull("No last activity packet", lastActivity);
// Asserts that there is at least a 1 second of idle time
assertTrue(
"The last activity idle time is less than expected: " + lastActivity.getIdleTime(),
lastActivity.getIdleTime() >= 1);
}
/**
* This is a test to check if a denied LastActivity response is handled correctly.
*/
public void testOnlinePermisionDenied() {
TCPConnection conn0 = getConnection(0);
TCPConnection conn2 = getConnection(2);
// Send a message as the last activity action from connection 2 to
// connection 0
conn2.sendPacket(new Message(getBareJID(0)));
// Wait 1 seconds to have some idle time
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
fail("Thread sleep interrupted");
}
try {
LastActivityManager.getLastActivity(conn0, getFullJID(2));
fail("No error was received from the server. User was able to get info of other user not in his roster.");
} catch (XMPPException e) {
assertNotNull("No error was returned from the server", e.getXMPPError());
assertEquals("Forbidden error was not returned from the server", 403,
e.getXMPPError().getCode());
}
}
/**
* This is a test to check if a LastActivity request for last logged out
* lapsed time is answered and correct
*/
public void testLastLoggedOut() {
TCPConnection conn0 = getConnection(0);
LastActivity lastActivity = null;
try {
lastActivity = LastActivityManager.getLastActivity(conn0, getBareJID(1));
} catch (XMPPException e) {
e.printStackTrace();
fail("An error occurred requesting the Last Activity");
}
assertNotNull("No last activity packet", lastActivity);
assertTrue("The last activity idle time should be 0 since the user is logged in: " +
lastActivity.getIdleTime(), lastActivity.getIdleTime() == 0);
}
/**
* This is a test to check if a LastActivity request for server uptime
* is answered and correct
*/
public void testServerUptime() {
TCPConnection conn0 = getConnection(0);
LastActivity lastActivity = null;
try {
lastActivity = LastActivityManager.getLastActivity(conn0, getHost());
} catch (XMPPException e) {
if (e.getXMPPError().getCode() == 403) {
//The test can not be done since the host do not allow this kind of request
return;
}
e.printStackTrace();
fail("An error occurred requesting the Last Activity");
}
assertNotNull("No last activity packet", lastActivity);
assertTrue("The last activity idle time should be greater than 0 : " +
lastActivity.getIdleTime(), lastActivity.getIdleTime() > 0);
}
public LastActivityManagerTest(String name) {
super(name);
}
@Override
protected int getMaxConnections() {
return 3;
}
@Override
protected void setUp() throws Exception {
super.setUp();
try {
getConnection(0).getRoster().createEntry(getBareJID(1), "User1", null);
Thread.sleep(300);
} catch (Exception e) {
fail(e.getMessage());
}
}
}

View file

@ -0,0 +1,239 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import java.util.ArrayList;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.test.SmackTestCase;
/**
*
* Test the MessageEvent extension using the high level API.
*
* @author Gaston Dombiak
*/
public class MessageEventManagerTest extends SmackTestCase {
public MessageEventManagerTest(String name) {
super(name);
}
/**
* High level API test.
* This is a simple test to use with a XMPP client and check if the client receives the
* message
* 1. User_1 will send a message to user_2 requesting to be notified when any of these events
* occurs: offline, composing, displayed or delivered
*/
public void testSendMessageEventRequest() {
// Create a chat for each connection
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
// Create the message to send with the roster
Message msg = new Message();
msg.setSubject("Any subject you want");
msg.setBody("An interesting body comes here...");
// Add to the message all the notifications requests (offline, delivered, displayed,
// composing)
MessageEventManager.addNotificationsRequests(msg, true, true, true, true);
// Send the message that contains the notifications request
try {
chat1.sendMessage(msg);
} catch (Exception e) {
fail("An error occured sending the message");
}
}
/**
* High level API test.
* This is a simple test to use with a XMPP client, check if the client receives the
* message and display in the console any notification
* 1. User_1 will send a message to user_2 requesting to be notified when any of these events
* occurs: offline, composing, displayed or delivered
* 2. User_2 will use a XMPP client (like Exodus) to display the message and compose a reply
* 3. User_1 will display any notification that receives
*/
public void testSendMessageEventRequestAndDisplayNotifications() {
// Create a chat for each connection
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
MessageEventManager messageEventManager = new MessageEventManager(getConnection(0));
messageEventManager
.addMessageEventNotificationListener(new MessageEventNotificationListener() {
public void deliveredNotification(String from, String packetID) {
System.out.println("From: " + from + " PacketID: " + packetID + "(delivered)");
}
public void displayedNotification(String from, String packetID) {
System.out.println("From: " + from + " PacketID: " + packetID + "(displayed)");
}
public void composingNotification(String from, String packetID) {
System.out.println("From: " + from + " PacketID: " + packetID + "(composing)");
}
public void offlineNotification(String from, String packetID) {
System.out.println("From: " + from + " PacketID: " + packetID + "(offline)");
}
public void cancelledNotification(String from, String packetID) {
System.out.println("From: " + from + " PacketID: " + packetID + "(cancelled)");
}
});
// Create the message to send with the roster
Message msg = new Message();
msg.setSubject("Any subject you want");
msg.setBody("An interesting body comes here...");
// Add to the message all the notifications requests (offline, delivered, displayed,
// composing)
MessageEventManager.addNotificationsRequests(msg, true, true, true, true);
// Send the message that contains the notifications request
try {
chat1.sendMessage(msg);
// Wait a few seconds so that the XMPP client can send any event
Thread.sleep(200);
} catch (Exception e) {
fail("An error occured sending the message");
}
}
/**
* High level API test.
* 1. User_1 will send a message to user_2 requesting to be notified when any of these events
* occurs: offline, composing, displayed or delivered
* 2. User_2 will receive the message
* 3. User_2 will simulate that the message was displayed
* 4. User_2 will simulate that he/she is composing a reply
* 5. User_2 will simulate that he/she has cancelled the reply
*/
public void testRequestsAndNotifications() {
final ArrayList<String> results = new ArrayList<String>();
ArrayList<String> resultsExpected = new ArrayList<String>();
resultsExpected.add("deliveredNotificationRequested");
resultsExpected.add("composingNotificationRequested");
resultsExpected.add("displayedNotificationRequested");
resultsExpected.add("offlineNotificationRequested");
resultsExpected.add("deliveredNotification");
resultsExpected.add("displayedNotification");
resultsExpected.add("composingNotification");
resultsExpected.add("cancelledNotification");
// Create a chat for each connection
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
MessageEventManager messageEventManager1 = new MessageEventManager(getConnection(0));
messageEventManager1
.addMessageEventNotificationListener(new MessageEventNotificationListener() {
public void deliveredNotification(String from, String packetID) {
results.add("deliveredNotification");
}
public void displayedNotification(String from, String packetID) {
results.add("displayedNotification");
}
public void composingNotification(String from, String packetID) {
results.add("composingNotification");
}
public void offlineNotification(String from, String packetID) {
results.add("offlineNotification");
}
public void cancelledNotification(String from, String packetID) {
results.add("cancelledNotification");
}
});
MessageEventManager messageEventManager2 = new MessageEventManager(getConnection(1));
messageEventManager2
.addMessageEventRequestListener(new DefaultMessageEventRequestListener() {
public void deliveredNotificationRequested(
String from,
String packetID,
MessageEventManager messageEventManager) {
super.deliveredNotificationRequested(from, packetID, messageEventManager);
results.add("deliveredNotificationRequested");
}
public void displayedNotificationRequested(
String from,
String packetID,
MessageEventManager messageEventManager) {
super.displayedNotificationRequested(from, packetID, messageEventManager);
results.add("displayedNotificationRequested");
}
public void composingNotificationRequested(
String from,
String packetID,
MessageEventManager messageEventManager) {
super.composingNotificationRequested(from, packetID, messageEventManager);
results.add("composingNotificationRequested");
}
public void offlineNotificationRequested(
String from,
String packetID,
MessageEventManager messageEventManager) {
super.offlineNotificationRequested(from, packetID, messageEventManager);
results.add("offlineNotificationRequested");
}
});
// Create the message to send with the roster
Message msg = new Message();
msg.setSubject("Any subject you want");
msg.setBody("An interesting body comes here...");
// Add to the message all the notifications requests (offline, delivered, displayed,
// composing)
MessageEventManager.addNotificationsRequests(msg, true, true, true, true);
// Send the message that contains the notifications request
try {
chat1.sendMessage(msg);
messageEventManager2.sendDisplayedNotification(getBareJID(0), msg.getPacketID());
messageEventManager2.sendComposingNotification(getBareJID(0), msg.getPacketID());
messageEventManager2.sendCancelledNotification(getBareJID(0), msg.getPacketID());
// Wait up to 2 seconds
long initial = System.currentTimeMillis();
while (System.currentTimeMillis() - initial < 2000 &&
(!results.containsAll(resultsExpected))) {
Thread.sleep(100);
}
assertTrue(
"Test failed due to bad results (1)" + resultsExpected,
resultsExpected.containsAll(results));
assertTrue(
"Test failed due to bad results (2)" + results,
results.containsAll(resultsExpected));
} catch (Exception e) {
fail("An error occured sending the message");
}
}
protected int getMaxConnections() {
return 2;
}
}

View file

@ -0,0 +1,41 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import org.jivesoftware.smackx.packet.MessageEventTest;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
*
* Test suite that runs all the Message Event extension tests
*
* @author Gaston Dombiak
*/
public class MessageEventTests {
public static Test suite() {
TestSuite suite = new TestSuite("High and low level API tests for message event extension");
//$JUnit-BEGIN$
suite.addTest(new TestSuite(MessageEventManagerTest.class));
suite.addTest(new TestSuite(MessageEventTest.class));
//$JUnit-END$
return suite;
}
}

View file

@ -0,0 +1,251 @@
/**
*
* Copyright 2003-2006 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.packet.MultipleAddresses;
import java.util.Arrays;
import java.util.List;
/**
* Tests that JEP-33 support in Smack is correct.
*
* @author Gaston Dombiak
*/
public class MultipleRecipientManagerTest extends SmackTestCase {
public MultipleRecipientManagerTest(String arg0) {
super(arg0);
}
/**
* Ensures that sending and receiving of packets is ok.
*/
public void testSending() throws XMPPException {
PacketCollector collector1 =
getConnection(1).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
PacketCollector collector2 =
getConnection(2).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
PacketCollector collector3 =
getConnection(3).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
Message message = new Message();
message.setBody("Hola");
List<String> to = Arrays.asList(new String[]{getBareJID(1)});
List<String> cc = Arrays.asList(new String[]{getBareJID(2)});
List<String> bcc = Arrays.asList(new String[]{getBareJID(3)});
MultipleRecipientManager.send(getConnection(0), message, to, cc, bcc);
Packet message1 = collector1.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNotNull("XMPPConnection 1 never received the message", message1);
MultipleRecipientInfo info1 = MultipleRecipientManager.getMultipleRecipientInfo(message1);
assertNotNull("Message 1 does not contain MultipleRecipientInfo", info1);
assertFalse("Message 1 should be 'replyable'", info1.shouldNotReply());
List<?> addresses1 = info1.getTOAddresses();
assertEquals("Incorrect number of TO addresses", 1, addresses1.size());
String address1 = ((MultipleAddresses.Address) addresses1.get(0)).getJid();
assertEquals("Incorrect TO address", getBareJID(1), address1);
addresses1 = info1.getCCAddresses();
assertEquals("Incorrect number of CC addresses", 1, addresses1.size());
address1 = ((MultipleAddresses.Address) addresses1.get(0)).getJid();
assertEquals("Incorrect CC address", getBareJID(2), address1);
Packet message2 = collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNotNull("XMPPConnection 2 never received the message", message2);
MultipleRecipientInfo info2 = MultipleRecipientManager.getMultipleRecipientInfo(message2);
assertNotNull("Message 2 does not contain MultipleRecipientInfo", info2);
assertFalse("Message 2 should be 'replyable'", info2.shouldNotReply());
List<MultipleAddresses.Address> addresses2 = info2.getTOAddresses();
assertEquals("Incorrect number of TO addresses", 1, addresses2.size());
String address2 = ((MultipleAddresses.Address) addresses2.get(0)).getJid();
assertEquals("Incorrect TO address", getBareJID(1), address2);
addresses2 = info2.getCCAddresses();
assertEquals("Incorrect number of CC addresses", 1, addresses2.size());
address2 = ((MultipleAddresses.Address) addresses2.get(0)).getJid();
assertEquals("Incorrect CC address", getBareJID(2), address2);
Packet message3 = collector3.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNotNull("XMPPConnection 3 never received the message", message3);
MultipleRecipientInfo info3 = MultipleRecipientManager.getMultipleRecipientInfo(message3);
assertNotNull("Message 3 does not contain MultipleRecipientInfo", info3);
assertFalse("Message 3 should be 'replyable'", info3.shouldNotReply());
List<MultipleAddresses.Address> addresses3 = info3.getTOAddresses();
assertEquals("Incorrect number of TO addresses", 1, addresses3.size());
String address3 = ((MultipleAddresses.Address) addresses3.get(0)).getJid();
assertEquals("Incorrect TO address", getBareJID(1), address3);
addresses3 = info3.getCCAddresses();
assertEquals("Incorrect number of CC addresses", 1, addresses3.size());
address3 = ((MultipleAddresses.Address) addresses3.get(0)).getJid();
assertEquals("Incorrect CC address", getBareJID(2), address3);
collector1.cancel();
collector2.cancel();
collector3.cancel();
}
/**
* Ensures that replying to packets is ok.
*/
public void testReplying() throws XMPPException {
PacketCollector collector0 =
getConnection(0).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
PacketCollector collector1 =
getConnection(1).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
PacketCollector collector2 =
getConnection(2).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
PacketCollector collector3 =
getConnection(3).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
// Send the intial message with multiple recipients
Message message = new Message();
message.setBody("Hola");
List<String> to = Arrays.asList(new String[]{getBareJID(1)});
List<String> cc = Arrays.asList(new String[]{getBareJID(2)});
List<String> bcc = Arrays.asList(new String[]{getBareJID(3)});
MultipleRecipientManager.send(getConnection(0), message, to, cc, bcc);
// Get the message and ensure it's ok
Message message1 =
(Message) collector1.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNotNull("XMPPConnection 1 never received the message", message1);
MultipleRecipientInfo info = MultipleRecipientManager.getMultipleRecipientInfo(message1);
assertNotNull("Message 1 does not contain MultipleRecipientInfo", info);
assertFalse("Message 1 should be 'replyable'", info.shouldNotReply());
assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size());
assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size());
// Prepare and send the reply
Message reply1 = new Message();
reply1.setBody("This is my reply");
MultipleRecipientManager.reply(getConnection(1), message1, reply1);
// Get the reply and ensure it's ok
reply1 = (Message) collector0.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNotNull("XMPPConnection 0 never received the reply", reply1);
info = MultipleRecipientManager.getMultipleRecipientInfo(reply1);
assertNotNull("Replied message does not contain MultipleRecipientInfo", info);
assertFalse("Replied message should be 'replyable'", info.shouldNotReply());
assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size());
assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size());
// Send a reply to the reply
Message reply2 = new Message();
reply2.setBody("This is my reply to your reply");
reply2.setFrom(getBareJID(0));
MultipleRecipientManager.reply(getConnection(0), reply1, reply2);
// Get the reply and ensure it's ok
reply2 = (Message) collector1.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNotNull("XMPPConnection 1 never received the reply", reply2);
info = MultipleRecipientManager.getMultipleRecipientInfo(reply2);
assertNotNull("Replied message does not contain MultipleRecipientInfo", info);
assertFalse("Replied message should be 'replyable'", info.shouldNotReply());
assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size());
assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size());
// Check that connection2 recevied 3 messages
message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNotNull("XMPPConnection2 didn't receive the 1 message", message1);
message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNotNull("XMPPConnection2 didn't receive the 2 message", message1);
message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNotNull("XMPPConnection2 didn't receive the 3 message", message1);
message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNull("XMPPConnection2 received 4 messages", message1);
// Check that connection3 recevied only 1 message (was BCC in the first message)
message1 = (Message) collector3.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNotNull("XMPPConnection3 didn't receive the 1 message", message1);
message1 = (Message) collector3.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNull("XMPPConnection2 received 2 messages", message1);
collector0.cancel();
collector1.cancel();
collector2.cancel();
collector3.cancel();
}
/**
* Ensures that replying is not allowed when disabled.
*/
public void testNoReply() throws XMPPException {
PacketCollector collector1 =
getConnection(1).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
PacketCollector collector2 =
getConnection(2).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
PacketCollector collector3 =
getConnection(3).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
// Send the intial message with multiple recipients
Message message = new Message();
message.setBody("Hola");
List<String> to = Arrays.asList(new String[]{getBareJID(1)});
List<String> cc = Arrays.asList(new String[]{getBareJID(2)});
List<String> bcc = Arrays.asList(new String[]{getBareJID(3)});
MultipleRecipientManager.send(getConnection(0), message, to, cc, bcc, null, null, true);
// Get the message and ensure it's ok
Message message1 =
(Message) collector1.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNotNull("XMPPConnection 1 never received the message", message1);
MultipleRecipientInfo info = MultipleRecipientManager.getMultipleRecipientInfo(message1);
assertNotNull("Message 1 does not contain MultipleRecipientInfo", info);
assertTrue("Message 1 should be not 'replyable'", info.shouldNotReply());
assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size());
assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size());
// Prepare and send the reply
Message reply1 = new Message();
reply1.setBody("This is my reply");
try {
MultipleRecipientManager.reply(getConnection(1), message1, reply1);
fail("It was possible to send a reply to a not replyable message");
}
catch (XMPPException e) {
// Exception was expected since replying was not allowed
}
// Check that connection2 recevied 1 messages
message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNotNull("XMPPConnection2 didn't receive the 1 message", message1);
message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNull("XMPPConnection2 received 2 messages", message1);
// Check that connection3 recevied only 1 message (was BCC in the first message)
message1 = (Message) collector3.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNotNull("XMPPConnection3 didn't receive the 1 message", message1);
message1 = (Message) collector3.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNull("XMPPConnection2 received 2 messages", message1);
collector1.cancel();
collector2.cancel();
collector3.cancel();
}
protected int getMaxConnections() {
return 4;
}
}

View file

@ -0,0 +1,181 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.packet.OfflineMessageInfo;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Tests handling of offline messaging using OfflineMessageManager. This server requires the
* server to support JEP-0013: Flexible Offline Message Retrieval.
*
* @author Gaston Dombiak
*/
public class OfflineMessageManagerTest extends SmackTestCase {
public OfflineMessageManagerTest(String arg0) {
super(arg0);
}
public void testDiscoverFlexibleRetrievalSupport() throws XMPPException {
OfflineMessageManager offlineManager = new OfflineMessageManager(getConnection(1));
assertTrue("Server does not support JEP-13", offlineManager.supportsFlexibleRetrieval());
}
/**
* While user2 is connected but unavailable, user1 sends 2 messages to user1. User2 then
* performs some "Flexible Offline Message Retrieval" checking the number of offline messages,
* retriving the headers, then the real messages of the headers and finally removing the
* loaded messages.
*/
public void testReadAndDelete() {
// Make user2 unavailable
getConnection(1).sendPacket(new Presence(Presence.Type.unavailable));
try {
Thread.sleep(500);
// User1 sends some messages to User2 which is not available at the moment
Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
chat.sendMessage("Test 1");
chat.sendMessage("Test 2");
Thread.sleep(500);
// User2 checks the number of offline messages
OfflineMessageManager offlineManager = new OfflineMessageManager(getConnection(1));
assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
// Check the message headers
Iterator<OfflineMessageHeader> headers = offlineManager.getHeaders();
assertTrue("No message header was found", headers.hasNext());
List<String> stamps = new ArrayList<String>();
while (headers.hasNext()) {
OfflineMessageHeader header = headers.next();
assertEquals("Incorrect sender", getFullJID(0), header.getJid());
assertNotNull("No stamp was found in the message header", header.getStamp());
stamps.add(header.getStamp());
}
assertEquals("Wrong number of headers", 2, stamps.size());
// Get the offline messages
Iterator<Message> messages = offlineManager.getMessages(stamps);
assertTrue("No message was found", messages.hasNext());
stamps = new ArrayList<String>();
while (messages.hasNext()) {
Message message = messages.next();
OfflineMessageInfo info = (OfflineMessageInfo) message.getExtension("offline",
"http://jabber.org/protocol/offline");
assertNotNull("No offline information was included in the offline message", info);
assertNotNull("No stamp was found in the message header", info.getNode());
stamps.add(info.getNode());
}
assertEquals("Wrong number of messages", 2, stamps.size());
// Check that the offline messages have not been deleted
assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
// User2 becomes available again
PacketCollector collector = getConnection(1).createPacketCollector(
new MessageTypeFilter(Message.Type.chat));
getConnection(1).sendPacket(new Presence(Presence.Type.available));
// Check that no offline messages was sent to the user
Message message = (Message) collector.nextResult(2500);
assertNull("An offline message was sent from the server", message);
// Delete the retrieved offline messages
offlineManager.deleteMessages(stamps);
// Check that there are no offline message for this user
assertEquals("Wrong number of offline messages", 0, offlineManager.getMessageCount());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
/**
* While user2 is connected but unavailable, user1 sends 2 messages to user1. User2 then
* performs some "Flexible Offline Message Retrieval" by fetching all the offline messages
* and then removing all the offline messages.
*/
public void testFetchAndPurge() {
// Make user2 unavailable
getConnection(1).sendPacket(new Presence(Presence.Type.unavailable));
try {
Thread.sleep(500);
// User1 sends some messages to User2 which is not available at the moment
Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
chat.sendMessage("Test 1");
chat.sendMessage("Test 2");
Thread.sleep(500);
// User2 checks the number of offline messages
OfflineMessageManager offlineManager = new OfflineMessageManager(getConnection(1));
assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
// Get all offline messages
Iterator<Message> messages = offlineManager.getMessages();
assertTrue("No message was found", messages.hasNext());
List<String> stamps = new ArrayList<String>();
while (messages.hasNext()) {
Message message = messages.next();
OfflineMessageInfo info = (OfflineMessageInfo) message.getExtension("offline",
"http://jabber.org/protocol/offline");
assertNotNull("No offline information was included in the offline message", info);
assertNotNull("No stamp was found in the message header", info.getNode());
stamps.add(info.getNode());
}
assertEquals("Wrong number of messages", 2, stamps.size());
// Check that the offline messages have not been deleted
assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
// User2 becomes available again
PacketCollector collector = getConnection(1).createPacketCollector(
new MessageTypeFilter(Message.Type.chat));
getConnection(1).sendPacket(new Presence(Presence.Type.available));
// Check that no offline messages was sent to the user
Message message = (Message) collector.nextResult(2500);
assertNull("An offline message was sent from the server", message);
// Delete all offline messages
offlineManager.deleteMessages();
// Check that there are no offline message for this user
assertEquals("Wrong number of offline messages", 0, offlineManager.getMessageCount());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
protected int getMaxConnections() {
return 2;
}
}

View file

@ -0,0 +1,210 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import java.util.Iterator;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.test.SmackTestCase;
/**
*
* Test the Roster Exchange extension using the high level API
*
* @author Gaston Dombiak
*/
public class RosterExchangeManagerTest extends SmackTestCase {
private int entriesSent;
private int entriesReceived;
/**
* Constructor for RosterExchangeManagerTest.
* @param name
*/
public RosterExchangeManagerTest(String name) {
super(name);
}
/**
* High level API test.
* This is a simple test to use with a XMPP client and check if the client receives user1's
* roster
* 1. User_1 will send his/her roster to user_2
*/
public void testSendRoster() {
// Send user1's roster to user2
try {
RosterExchangeManager rosterExchangeManager =
new RosterExchangeManager(getConnection(0));
rosterExchangeManager.send(getConnection(0).getRoster(), getBareJID(1));
}
catch (Exception e) {
e.printStackTrace();
fail("An error occured sending the roster");
}
}
/**
* High level API test.
* This is a simple test to use with a XMPP client and check if the client receives user1's
* roster groups
* 1. User_1 will send his/her RosterGroups to user_2
*/
public void testSendRosterGroup() {
// Send user1's RosterGroups to user2
try {
RosterExchangeManager rosterExchangeManager = new RosterExchangeManager(getConnection(0));
for (RosterGroup rosterGroup : getConnection(0).getRoster().getGroups()) {
rosterExchangeManager.send(rosterGroup, getBareJID(1));
}
}
catch (Exception e) {
e.printStackTrace();
fail("An error occured sending the roster");
}
}
/**
* High level API test.
* 1. User_1 will send his/her roster to user_2
* 2. User_2 will receive the entries and iterate over them to check if everything is fine
* 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
* something is wrong
*/
public void testSendAndReceiveRoster() {
RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(getConnection(0));
RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(getConnection(1));
// Create a RosterExchangeListener that will iterate over the received roster entries
RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() {
public void entriesReceived(String from, Iterator<RemoteRosterEntry> remoteRosterEntries) {
int received = 0;
assertNotNull("From is null", from);
assertNotNull("rosterEntries is null", remoteRosterEntries);
assertTrue("Roster without entries", remoteRosterEntries.hasNext());
while (remoteRosterEntries.hasNext()) {
received++;
RemoteRosterEntry remoteEntry = remoteRosterEntries.next();
System.out.println(remoteEntry);
}
entriesReceived = received;
}
};
rosterExchangeManager2.addRosterListener(rosterExchangeListener);
// Send user1's roster to user2
try {
entriesSent = getConnection(0).getRoster().getEntryCount();
entriesReceived = 0;
rosterExchangeManager1.send(getConnection(0).getRoster(), getBareJID(1));
// Wait up to 2 seconds
long initial = System.currentTimeMillis();
while (System.currentTimeMillis() - initial < 2000 &&
(entriesSent != entriesReceived)) {
Thread.sleep(100);
}
}
catch (Exception e) {
fail("An error occured sending the message with the roster");
}
assertEquals(
"Number of sent and received entries does not match",
entriesSent,
entriesReceived);
}
/**
* High level API test.
* 1. User_1 will send his/her roster to user_2
* 2. User_2 will automatically add the entries that receives to his/her roster in the
* corresponding group
* 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
* something is wrong
*/
public void testSendAndAcceptRoster() {
RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(getConnection(0));
RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(getConnection(1));
// Create a RosterExchangeListener that will accept all the received roster entries
RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() {
public void entriesReceived(String from, Iterator<RemoteRosterEntry> remoteRosterEntries) {
int received = 0;
assertNotNull("From is null", from);
assertNotNull("remoteRosterEntries is null", remoteRosterEntries);
assertTrue("Roster without entries", remoteRosterEntries.hasNext());
while (remoteRosterEntries.hasNext()) {
received++;
try {
RemoteRosterEntry remoteRosterEntry = remoteRosterEntries.next();
getConnection(1).getRoster().createEntry(
remoteRosterEntry.getUser(),
remoteRosterEntry.getName(),
remoteRosterEntry.getGroupArrayNames());
}
catch (Exception e) {
fail(e.toString());
}
}
entriesReceived = received;
}
};
rosterExchangeManager2.addRosterListener(rosterExchangeListener);
// Send user1's roster to user2
try {
entriesSent = getConnection(0).getRoster().getEntryCount();
entriesReceived = 0;
rosterExchangeManager1.send(getConnection(0).getRoster(), getBareJID(1));
// Wait up to 2 seconds
long initial = System.currentTimeMillis();
while (System.currentTimeMillis() - initial < 2000 &&
(entriesSent != entriesReceived)) {
Thread.sleep(100);
}
}
catch (Exception e) {
fail("An error occured sending the message with the roster");
}
assertEquals(
"Number of sent and received entries does not match",
entriesSent,
entriesReceived);
assertTrue("Roster2 has no entries", getConnection(1).getRoster().getEntryCount() > 0);
}
protected void setUp() throws Exception {
super.setUp();
try {
getConnection(0).getRoster().createEntry(
getBareJID(2),
"gato5",
new String[] { "Friends, Coworker" });
getConnection(0).getRoster().createEntry(getBareJID(3), "gato6", null);
Thread.sleep(100);
}
catch (Exception e) {
fail(e.getMessage());
}
}
protected int getMaxConnections() {
return 4;
}
}

View file

@ -0,0 +1,41 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import org.jivesoftware.smackx.packet.RosterExchangeTest;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
*
* Test suite that runs all the Roster Exchange extension tests
*
* @author Gaston Dombiak
*/
public class RosterExchangeTests {
public static Test suite() {
TestSuite suite = new TestSuite("High and low level API tests for roster exchange extension");
//$JUnit-BEGIN$
suite.addTest(new TestSuite(RosterExchangeManagerTest.class));
suite.addTest(new TestSuite(RosterExchangeTest.class));
//$JUnit-END$
return suite;
}
}

View file

@ -0,0 +1,150 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import java.util.Iterator;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.smackx.packet.DiscoverInfo.Identity;
/**
* Tests the service discovery functionality.
*
* @author Gaston Dombiak
*/
public class ServiceDiscoveryManagerTest extends SmackTestCase {
public ServiceDiscoveryManagerTest(String arg0) {
super(arg0);
}
/**
* Tests info discovery of a Smack client.
*/
public void testSmackInfo() {
ServiceDiscoveryManager discoManager = ServiceDiscoveryManager
.getInstanceFor(getConnection(0));
try {
// Discover the information of another Smack client
DiscoverInfo info = discoManager.discoverInfo(getFullJID(1));
// Check the identity of the Smack client
Iterator<Identity> identities = info.getIdentities();
assertTrue("No identities were found", identities.hasNext());
Identity identity = identities.next();
assertEquals("Name in identity is wrong", discoManager.getIdentityName(),
identity.getName());
assertEquals("Category in identity is wrong", "client", identity.getCategory());
assertEquals("Type in identity is wrong", discoManager.getIdentityType(),
identity.getType());
assertFalse("More identities were found", identities.hasNext());
}
catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
/**
* Tests that ensures that Smack answers a 404 error when the disco#info includes a node.
*/
public void testInfoWithNode() {
ServiceDiscoveryManager discoManager = ServiceDiscoveryManager
.getInstanceFor(getConnection(0));
try {
// Discover the information of another Smack client
discoManager.discoverInfo(getFullJID(1), "some node");
// Check the identity of the Smack client
fail("Unexpected identities were returned instead of a 404 error");
}
catch (XMPPException e) {
assertEquals("Incorrect error", 404, e.getXMPPError().getCode());
}
catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
/**
* Tests service discovery of XHTML support.
*/
public void testXHTMLFeature() {
// Check for local XHTML service support
// By default the XHTML service support is enabled in all the connections
assertTrue(XHTMLManager.isServiceEnabled(getConnection(0)));
assertTrue(XHTMLManager.isServiceEnabled(getConnection(1)));
// Check for XHTML support in connection1 from connection2
// Must specify a full JID and not a bare JID. Ensure that the server is working ok.
assertFalse(XHTMLManager.isServiceEnabled(getConnection(1), getBareJID(0)));
// Using a full JID check that the other client supports XHTML.
assertTrue(XHTMLManager.isServiceEnabled(getConnection(1), getFullJID(0)));
// Disable the XHTML Message support in connection1
XHTMLManager.setServiceEnabled(getConnection(0), false);
// Check for local XHTML service support
assertFalse(XHTMLManager.isServiceEnabled(getConnection(0)));
assertTrue(XHTMLManager.isServiceEnabled(getConnection(1)));
// Check for XHTML support in connection1 from connection2
assertFalse(XHTMLManager.isServiceEnabled(getConnection(1), getFullJID(0)));
}
/**
* Tests support for publishing items to another entity.
*/
public void testDiscoverPublishItemsSupport() {
try {
boolean canPublish = ServiceDiscoveryManager.getInstanceFor(getConnection(0))
.canPublishItems(getServiceName());
assertFalse("Wildfire does not support publishing...so far!!", canPublish);
}
catch (Exception e) {
fail(e.getMessage());
}
}
/**
* Tests publishing items to another entity.
*/
/*public void testPublishItems() {
DiscoverItems itemsToPublish = new DiscoverItems();
DiscoverItems.Item itemToPublish = new DiscoverItems.Item("pubsub.shakespeare.lit");
itemToPublish.setName("Avatar");
itemToPublish.setNode("romeo/avatar");
itemToPublish.setAction(DiscoverItems.Item.UPDATE_ACTION);
itemsToPublish.addItem(itemToPublish);
try {
ServiceDiscoveryManager.getInstanceFor(getConnection(0)).publishItems(getServiceName(),
itemsToPublish);
}
catch (Exception e) {
fail(e.getMessage());
}
}*/
protected int getMaxConnections() {
return 2;
}
}

View file

@ -0,0 +1,47 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smack.XMPPException;
import java.util.List;
/**
* Test cases for getting the shared groups of a user.<p>
*
* Important note: This functionality is not part of the XMPP spec and it will only work
* with Wildfire.
*
* @author Gaston Dombiak
*/
public class SharedGroupsTest extends SmackTestCase {
public SharedGroupsTest(String arg0) {
super(arg0);
}
public void testGetUserSharedGroups() throws XMPPException {
List<String> groups = SharedGroupManager.getSharedGroups(getConnection(0));
assertNotNull("User groups was null", groups);
}
protected int getMaxConnections() {
return 1;
}
}

View file

@ -0,0 +1,167 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.packet.VCard;
/**
* Created by IntelliJ IDEA.
* User: Gaston
* Date: Jun 18, 2005
* Time: 1:29:30 AM
* To change this template use File | Settings | File Templates.
*/
public class VCardTest extends SmackTestCase {
public VCardTest(String arg0) {
super(arg0);
}
public void testBigFunctional() throws XMPPException {
VCard origVCard = new VCard();
origVCard.setFirstName("kir");
origVCard.setLastName("max");
origVCard.setEmailHome("foo@fee.bar");
origVCard.setEmailWork("foo@fee.www.bar");
origVCard.setJabberId("jabber@id.org");
origVCard.setOrganization("Jetbrains, s.r.o");
origVCard.setNickName("KIR");
origVCard.setField("TITLE", "Mr");
origVCard.setAddressFieldHome("STREET", "Some street & House");
origVCard.setAddressFieldWork("STREET", "Some street work");
origVCard.setPhoneWork("FAX", "3443233");
origVCard.setPhoneHome("VOICE", "3443233");
origVCard.save(getConnection(0));
VCard loaded = new VCard();
try {
loaded.load(getConnection(0));
} catch (XMPPException e) {
e.printStackTrace();
fail(e.getMessage());
}
assertEquals("Should load own VCard successfully", origVCard, loaded);
loaded = new VCard();
try {
loaded.load(getConnection(1), getBareJID(0));
} catch (XMPPException e) {
e.printStackTrace();
fail(e.getMessage());
}
assertEquals("Should load another user's VCard successfully", origVCard, loaded);
}
public void testBinaryAvatar() throws Throwable {
VCard card = new VCard();
card.setAvatar(getAvatarBinary());
card.save(getConnection(0));
VCard loaded = new VCard();
try {
loaded.load(getConnection(0));
}
catch (XMPPException e) {
e.printStackTrace();
fail(e.getMessage());
}
byte[] initialAvatar = card.getAvatar();
byte[] loadedAvatar = loaded.getAvatar();
assertEquals("Should load own Avatar successfully", initialAvatar, loadedAvatar);
loaded = new VCard();
try {
loaded.load(getConnection(1), getBareJID(0));
}
catch (XMPPException e) {
e.printStackTrace();
fail(e.getMessage());
}
assertEquals("Should load avatar successfully", card.getAvatar(), loaded.getAvatar());
}
public static byte[] getAvatarBinary() {
return StringUtils.decodeBase64(getAvatarEncoded());
}
public static String getAvatarEncoded() {
return "/9j/4AAQSkZJRgABAQEASABIAAD/4QAWRXhpZgAATU0AKgAAAAgAAAAAAAD/2wBDAAUDBAQEAwUE\n" +
"BAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEPERETFhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7/\n" +
"2wBDAQUFBQcGBw4ICA4eFBEUHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4e\n" +
"Hh4eHh4eHh4eHh7/wAARCABQAFADASIAAhEBAxEB/8QAHAAAAgIDAQEAAAAAAAAAAAAABwgFBgID\n" +
"BAkB/8QAORAAAgEDAwIDBwIDBwUAAAAAAQIDBAURAAYSITEHE0EIFBUiMlFxYbEjUqEkQoGR0eHw\n" +
"M0NicsH/xAAZAQADAQEBAAAAAAAAAAAAAAACAwQBAAX/xAAgEQACAgMAAwADAAAAAAAAAAAAAQIR\n" +
"AxIhBBMxMmGR/9oADAMBAAIRAxEAPwDOor6ir6RqwhH0hfX9fx++t1FbGmYRUyEg4A6k5Ot9staw\n" +
"ny4FP8R+RDNkE9s6s1TR2yzW0190QVGOiq/0k/bj21Ko2/0Miv6bKSOKyW1aeAqzjq5B+pvXXKdy\n" +
"BRyYkYOqVd9xw1crSQWiCKnXIXCDl/nj9tUu80016u8dPPdKyC3ypzMMT4ZmGAUz9hkHJz3xqlTa\n" +
"4ilRk/oYJd8WunJjlr6NJT2RplB/fWUO7AwBDhhjIIPTVSsXhltF6FXlslLKGHzNLlmb9e+uC8bC\n" +
"t9muNHJa2qKeJ5eJhErFGABbA69Ppx+M6KUnR3Y/UFa17pilK8I5JSTjIIA/rqJ3TYWeve8UlH5a\n" +
"VKjzgGGCw7N+cd/wNDykNdBKI5KgD5sjI6aJW3qyueDyJI/MjIwSDlW/00vdPjMyRlVFMqoOMhjZ\n" +
"WR/5WGD/AIffUVUUoZ8EaIlDQJXVr0VTGfLlbA/8WJ6ah9zbdms1XGkh5JMnJGx9uhB/UHQShy0T\n" +
"X2iatSxSX96RXTIYRL64Oev761+L7UduTlc3ZII8BEHdjj0GrPZbRTVV5MskKJ5vE5Ax17Hr/wA9\n" +
"NUv2p57BtHbluul4q55qjzpFo7fM4Z6h1CgovqEGQWbOACO5KqdriDxy1fQSVO8DXF4LfZ3SmQdW\n" +
"diCfX0H21Xqu+Ri726oWadY3ZgyDDBBhcgEfc4z+NBi7XGqula9VVPlmJIUdFQfZR6D/AIdc8Ukk\n" +
"MqSxO0ciMGR1OCpHYg+h0aib7h69rCoa2RK7FSVGVHpqq+KNS1NV2aGeOsZ0qTxkhcqEVhxYnH5H\n" +
"X0xoXeDfjlNZsWnejz1dGSiwV0cYaSEDCkSAYLrj5uXV8g/VkYZyJbRfrRDdqCWiudG2QskTpLFK\n" +
"uSGAIJBwQR+Rps6cEGpbWAzdFpv07T8I63hEAIwwPXPc4Hr+dTnh8246CzPdUmm8mneNJ6eo+vkx\n" +
"IIH3HTP40cK+009SvvMYCiTv9gfXX21USUswWWKCcN0yy9QNI1oZJ7dIinSasus7UsL8iiuxxhQD\n" +
"+v37nXd4g2mtjstFVVlQ0s5qWV1KBRllznH7/jVlsdsaTckwY8YXwf0C46n/AC1xeLknvtdQW2PJ\n" +
"bLSOq+nLB/Yf10VtRaJH+RYLrZaSyxz1k9XFT0VPG0ss8zBI4kUFmLMegUKCST0AGvNvxs35W+JH\n" +
"iRdN0VUk3u8r+TQRSEjyaZOka8eTBSR8zBTjm7kd9Nr7fPiDd7LsW0bZs881Ku4pJxWzxS8S1PEq\n" +
"coCMZw5mXJBHRCpyHI0i2iquAXfSV2rYLnuW8xWq1QiSaTqzMcJEg7u59FGf2AySASJv3wVu1ktE\n" +
"V0sM816jBVJ6dIP46HAHNVBPJS2eg6qCPqALC5+DO2327sVLpMh9+uwWpIDdocfwh0JByCWz0Pz4\n" +
"PbRXscVQLYWqj8zDOMems7ZbHxl69m+iOa6fiFf8L+Fe/VPw/wA/3j3XzW8nzePHzOGccuPTljOO\n" +
"mmO8TPDSy7qc1dseC1Xnk7M6wgRVGcn+IB2bkf8AqDJwTkN0wud5oJrVd622VDxvNR1EkEjRklSy\n" +
"MVJGQDjI+w0TVE08cofQneylfrlafF2gt9NXSQ2+5RzR11PnMc4SGR05A+oYDBHUZIzhiC5lPV07\n" +
"SBlmHQ9j/rpV/ZB2tSXw7pu3u6SXS1rS+5yN1KLJ53mADsCQijPfGR2Jywe3qoeeUcYcdMY7aXKT\n" +
"TLfGxp47YSTc/crcayni8xuisxOPxqFo6ee43ISVEhWpq34tIf8Atqx/c6kaFTLZ5CygoHQnp07j\n" +
"UxV0kFPNNIsfFoqlXBX8jQyl0kyJKXBS/boqZrpZtk3CKCY00T1sckvA8UZxAUUnsCQjED14t9jp\n" +
"W9ej1bbrbuKxVtnvlFFWUFbmOaGQfKQT0P3BBAIIwQQCCCAdKn4kezjuayxz3Pacvx+2qSwp8BKy\n" +
"NfmOOPaXACjK4ZmPRNV5MTXUIj8Iza/jfclaODdlL8QiUn+1UyKk3949U6I390dOOAM/MdT27vaF\n" +
"5U4ptq2Tjzw0k9xHUd8qqI3/AKnkW+44+ugPV01RR1c1JVwS09RBI0csUqFXjdTgqwPUEEEEHWrS\n" +
"KH+/JVWXCbxM3nJVvULdhGWYkKtPGVUfYZUnA/Uk6gNxXu5bguJuN2mjnqigRpFgSMsB25cAMnHT\n" +
"J64AHYDVs234Q75vfkyfDIrbTy8szXCdYfLxn6kyZBkjA+X1B7ddWOP2e94StxhvO25TnrwqJiF/\n" +
"J8rWnOOWa7ZXtgeMO/djW2ntW3rnSwW2Kfz3pGoICs7Egt5j8PMbIAXPLkFAAIwMNB4d7xsW/bdS\n" +
"3iyAwVYZYq+hZ8yUrkdc/wAynB4t2IB7EMoTbeG3rjtXctbt+6iL3ujcK5ifmjggMrKfsVIIyAev\n" +
"UA5GurZ28dwbRW5fAK+Sje40vu0siMQyDkDzTrgSABlDd1DtjBIIySs7HkeN9HFvftPeGFjWp2/D\n" +
"T326SU8oV6yhghemkYYzwZpVLAHI5YwcZBIIJLuyN5WDxB2jJubbVX59FUModJFCy08gC8opFyeL\n" +
"rkZGSCCCCVIJ8vdO97EsZtfgZWS148lbjeZZ6Y8gecYSKItgHp88bjBwemexBIuKF3bCZMDTgggg\n" +
"GZSNStuhLRlyAAGP9P8AfOoKW6Udbeqe38i0kANQwHoFHrq0WpG9yp+fdkBb8nrr1GhexDbk2zaN\n" +
"x0vul8tlHcaZG8xI6qBZVVwCOYDAjOCRn9Toe1GwNsWyqBpduWihqkBaKogoo43AIwcMoBHQkaNP\n" +
"lgxYx6ai9xWb4lQfwQBURLyjP3HqupM2NfUPwZNWAi4WmvimKxvLxB6FW1O7XpK1VXzeROe7tqSq\n" +
"/PilaGWNkkU4ZWHUayo5nV8Fv8MakU2uHr+1uIvHtW+Hl5oNy1G+6fFZaK4RLO0a/NRyKixgOP5W\n" +
"4jD9snicHiWBGvTnaFtnnmSeZCsQIKgj6v8AbV5jlDS1AXsqBRqqGJyVs8bM0pcEL9mz2e7pvivi\n" +
"3BvCirLZteMLLDHKjRS3QlQyiPsRCQQTIO4PFDnLI9NBZKKgpaCjtdPDR0YaPhBGgRI1UfKiqOgA\n" +
"CgADtrKoqPLpKaXPVXUdPtnXTNUBLlTQR4xHlj+gHT/7pjw8oTsf/9k=";
}
protected int getMaxConnections() {
return 2;
}
}

View file

@ -0,0 +1,67 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.packet.Version;
/**
* Test case to ensure that Smack is able to get and parse correctly iq:version packets.
*
* @author Gaston Dombiak
*/
public class VersionTest extends SmackTestCase {
public VersionTest(String arg0) {
super(arg0);
}
/**
* Get the version of the server and make sure that all the required data is present
*
* Note: This test expects the server to answer an iq:version packet.
*/
public void testGetServerVersion() {
Version version = new Version();
version.setType(IQ.Type.GET);
version.setTo(getServiceName());
// Create a packet collector to listen for a response.
PacketCollector collector = getConnection(0).createPacketCollector(new PacketIDFilter(version.getPacketID()));
getConnection(0).sendPacket(version);
// Wait up to 5 seconds for a result.
IQ result = (IQ)collector.nextResult(5000);
// Close the collector
collector.cancel();
assertNotNull("No result from the server", result);
assertEquals("Incorrect result type", IQ.Type.RESULT, result.getType());
assertNotNull("No name specified in the result", ((Version)result).getName());
assertNotNull("No version specified in the result", ((Version)result).getVersion());
}
protected int getMaxConnections() {
return 1;
}
}

View file

@ -0,0 +1,234 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import java.util.Iterator;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.ThreadFilter;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.test.SmackTestCase;
/**
* Test the XHTML extension using the high level API
*
* @author Gaston Dombiak
*/
public class XHTMLManagerTest extends SmackTestCase {
private int bodiesSent;
private int bodiesReceived;
/**
* Constructor for XHTMLManagerTest.
* @param name
*/
public XHTMLManagerTest(String name) {
super(name);
}
/**
* High level API test.
* This is a simple test to use with a XMPP client and check if the client receives the message
* 1. User_1 will send a message with formatted text (XHTML) to user_2
*/
public void testSendSimpleXHTMLMessage() {
// User1 creates a chat with user2
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
// User1 creates a message to send to user2
Message msg = new Message();
msg.setSubject("Any subject you want");
msg.setBody("Hey John, this is my new green!!!!");
// Create an XHTMLText to send with the message
XHTMLText xhtmlText = new XHTMLText(null, null);
xhtmlText.appendOpenParagraphTag("font-size:large");
xhtmlText.append("Hey John, this is my new ");
xhtmlText.appendOpenSpanTag("color:green");
xhtmlText.append("green");
xhtmlText.appendCloseSpanTag();
xhtmlText.appendOpenEmTag();
xhtmlText.append("!!!!");
xhtmlText.appendCloseEmTag();
xhtmlText.appendCloseParagraphTag();
// Add the XHTML text to the message
XHTMLManager.addBody(msg, xhtmlText.toString());
// User1 sends the message that contains the XHTML to user2
try {
chat1.sendMessage(msg);
Thread.sleep(200);
} catch (Exception e) {
fail("An error occured sending the message with XHTML");
}
}
/**
* High level API test.
* 1. User_1 will send a message with XHTML to user_2
* 2. User_2 will receive the message and iterate over the XHTML bodies to check if everything
* is fine
* 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
* something is wrong
*/
public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() {
// Create a chat for each connection
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
final PacketCollector chat2 = getConnection(1).createPacketCollector(
new ThreadFilter(chat1.getThreadID()));
// User1 creates a message to send to user2
Message msg = new Message();
msg.setSubject("Any subject you want");
msg.setBody("Hey John, this is my new green!!!!");
// Create an XHTMLText to send with the message
XHTMLText xhtmlText = new XHTMLText(null, null);
xhtmlText.appendOpenParagraphTag("font-size:large");
xhtmlText.append("Hey John, this is my new ");
xhtmlText.appendOpenSpanTag("color:green");
xhtmlText.append("green");
xhtmlText.appendCloseSpanTag();
xhtmlText.appendOpenEmTag();
xhtmlText.append("!!!!");
xhtmlText.appendCloseEmTag();
xhtmlText.appendCloseParagraphTag();
// Add the XHTML text to the message
XHTMLManager.addBody(msg, xhtmlText.toString());
// User1 sends the message that contains the XHTML to user2
try {
chat1.sendMessage(msg);
} catch (Exception e) {
fail("An error occured sending the message with XHTML");
}
Packet packet = chat2.nextResult(2000);
Message message = (Message) packet;
assertTrue(
"The received message is not an XHTML Message",
XHTMLManager.isXHTMLMessage(message));
try {
assertTrue(
"Message without XHTML bodies",
XHTMLManager.getBodies(message).hasNext());
for (Iterator<String> it = XHTMLManager.getBodies(message); it.hasNext();) {
String body = it.next();
System.out.println(body);
}
}
catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
}
assertNotNull("No reply received", msg);
}
/**
* Low level API test. Test a message with two XHTML bodies and several XHTML tags.
* 1. User_1 will send a message with XHTML to user_2
* 2. User_2 will receive the message and iterate over the XHTML bodies to check if everything
* is fine
* 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
* something is wrong
*/
public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() {
// Create a chat for each connection
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
final PacketCollector chat2 = getConnection(1).createPacketCollector(
new ThreadFilter(chat1.getThreadID()));
// User1 creates a message to send to user2
Message msg = new Message();
msg.setSubject("Any subject you want");
msg.setBody(
"awesome! As Emerson once said: A foolish consistency is the hobgoblin of little minds.");
// Create an XHTMLText to send with the message (in Spanish)
XHTMLText xhtmlText = new XHTMLText(null, "es-ES");
xhtmlText.appendOpenHeaderTag(1, null);
xhtmlText.append("impresionante!");
xhtmlText.appendCloseHeaderTag(1);
xhtmlText.appendOpenParagraphTag(null);
xhtmlText.append("Como Emerson dijo una vez:");
xhtmlText.appendCloseParagraphTag();
xhtmlText.appendOpenBlockQuoteTag(null);
xhtmlText.appendOpenParagraphTag(null);
xhtmlText.append("Una consistencia rid&#237;cula es el espantajo de mentes peque&#241;as.");
xhtmlText.appendCloseParagraphTag();
xhtmlText.appendCloseBlockQuoteTag();
// Add the XHTML text to the message
XHTMLManager.addBody(msg, xhtmlText.toString());
// Create an XHTMLText to send with the message (in English)
xhtmlText = new XHTMLText(null, "en-US");
xhtmlText.appendOpenHeaderTag(1, null);
xhtmlText.append("awesome!");
xhtmlText.appendCloseHeaderTag(1);
xhtmlText.appendOpenParagraphTag(null);
xhtmlText.append("As Emerson once said:");
xhtmlText.appendCloseParagraphTag();
xhtmlText.appendOpenBlockQuoteTag(null);
xhtmlText.appendOpenParagraphTag(null);
xhtmlText.append("A foolish consistency is the hobgoblin of little minds.");
xhtmlText.appendCloseParagraphTag();
xhtmlText.appendCloseBlockQuoteTag();
// Add the XHTML text to the message
XHTMLManager.addBody(msg, xhtmlText.toString());
// User1 sends the message that contains the XHTML to user2
try {
bodiesSent = 2;
bodiesReceived = 0;
chat1.sendMessage(msg);
} catch (Exception e) {
fail("An error occured sending the message with XHTML");
}
Packet packet = chat2.nextResult(2000);
int received = 0;
Message message = (Message) packet;
assertTrue(
"The received message is not an XHTML Message",
XHTMLManager.isXHTMLMessage(message));
try {
assertTrue(
"Message without XHTML bodies",
XHTMLManager.getBodies(message).hasNext());
for (Iterator<String> it = XHTMLManager.getBodies(message); it.hasNext();) {
received++;
String body = it.next();
System.out.println(body);
}
bodiesReceived = received;
}
catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers" +
"is misconfigured");
}
assertEquals(
"Number of sent and received XHTMP bodies does not match",
bodiesSent,
bodiesReceived);
}
protected int getMaxConnections() {
return 2;
}
}

View file

@ -0,0 +1,40 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import org.jivesoftware.smackx.packet.XHTMLExtensionTest;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* Test suite that runs all the XHTML support tests
*
* @author Gaston Dombiak
*/
public class XHTMLSupportTests {
public static Test suite() {
TestSuite suite = new TestSuite("High and low level API tests for XHTML support");
//$JUnit-BEGIN$
suite.addTest(new TestSuite(XHTMLManagerTest.class));
suite.addTest(new TestSuite(XHTMLExtensionTest.class));
//$JUnit-END$
return suite;
}
}

View file

@ -0,0 +1,259 @@
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.ibb;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Random;
import java.util.concurrent.SynchronousQueue;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamListener;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamRequest;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager.StanzaType;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
/**
* Test for In-Band Bytestreams with real XMPP servers.
*
* @author Henning Staib
*/
public class InBandBytestreamTest extends SmackTestCase {
/* the amount of data transmitted in each test */
int dataSize = 1024000;
public InBandBytestreamTest(String arg0) {
super(arg0);
}
/**
* Target should respond with not-acceptable error if no listeners for incoming In-Band
* Bytestream requests are registered.
*
* @throws XMPPException should not happen
*/
public void testRespondWithErrorOnInBandBytestreamRequest() throws XMPPException {
XMPPConnection targetConnection = getConnection(0);
XMPPConnection initiatorConnection = getConnection(1);
Open open = new Open("sessionID", 1024);
open.setFrom(initiatorConnection.getUser());
open.setTo(targetConnection.getUser());
PacketCollector collector = initiatorConnection.createPacketCollector(new PacketIDFilter(
open.getPacketID()));
initiatorConnection.sendPacket(open);
Packet result = collector.nextResult();
assertNotNull(result.getError());
assertEquals(XMPPError.Condition.no_acceptable.toString(), result.getError().getCondition());
}
/**
* An In-Band Bytestream should be successfully established using IQ stanzas.
*
* @throws Exception should not happen
*/
public void testInBandBytestreamWithIQStanzas() throws Exception {
XMPPConnection initiatorConnection = getConnection(0);
XMPPConnection targetConnection = getConnection(1);
// test data
Random rand = new Random();
final byte[] data = new byte[dataSize];
rand.nextBytes(data);
final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();
InBandBytestreamManager targetByteStreamManager = InBandBytestreamManager.getByteStreamManager(targetConnection);
InBandBytestreamListener incomingByteStreamListener = new InBandBytestreamListener() {
public void incomingBytestreamRequest(InBandBytestreamRequest request) {
InputStream inputStream;
try {
inputStream = request.accept().getInputStream();
byte[] receivedData = new byte[dataSize];
int totalRead = 0;
while (totalRead < dataSize) {
int read = inputStream.read(receivedData, totalRead, dataSize - totalRead);
totalRead += read;
}
queue.put(receivedData);
}
catch (Exception e) {
fail(e.getMessage());
}
}
};
targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener);
InBandBytestreamManager initiatorByteStreamManager = InBandBytestreamManager.getByteStreamManager(initiatorConnection);
OutputStream outputStream = initiatorByteStreamManager.establishSession(
targetConnection.getUser()).getOutputStream();
// verify stream
outputStream.write(data);
outputStream.flush();
outputStream.close();
assertEquals("received data not equal to sent data", data, queue.take());
}
/**
* An In-Band Bytestream should be successfully established using message stanzas.
*
* @throws Exception should not happen
*/
public void testInBandBytestreamWithMessageStanzas() throws Exception {
XMPPConnection initiatorConnection = getConnection(0);
XMPPConnection targetConnection = getConnection(1);
// test data
Random rand = new Random();
final byte[] data = new byte[dataSize];
rand.nextBytes(data);
final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();
InBandBytestreamManager targetByteStreamManager = InBandBytestreamManager.getByteStreamManager(targetConnection);
InBandBytestreamListener incomingByteStreamListener = new InBandBytestreamListener() {
public void incomingBytestreamRequest(InBandBytestreamRequest request) {
InputStream inputStream;
try {
inputStream = request.accept().getInputStream();
byte[] receivedData = new byte[dataSize];
int totalRead = 0;
while (totalRead < dataSize) {
int read = inputStream.read(receivedData, totalRead, dataSize - totalRead);
totalRead += read;
}
queue.put(receivedData);
}
catch (Exception e) {
fail(e.getMessage());
}
}
};
targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener);
InBandBytestreamManager initiatorByteStreamManager = InBandBytestreamManager.getByteStreamManager(initiatorConnection);
initiatorByteStreamManager.setStanza(StanzaType.MESSAGE);
OutputStream outputStream = initiatorByteStreamManager.establishSession(
targetConnection.getUser()).getOutputStream();
// verify stream
outputStream.write(data);
outputStream.flush();
outputStream.close();
assertEquals("received data not equal to sent data", data, queue.take());
}
/**
* An In-Band Bytestream should be successfully established using IQ stanzas. The established
* session should transfer data bidirectional.
*
* @throws Exception should not happen
*/
public void testBiDirectionalInBandBytestream() throws Exception {
XMPPConnection initiatorConnection = getConnection(0);
XMPPConnection targetConnection = getConnection(1);
// test data
Random rand = new Random();
final byte[] data = new byte[dataSize];
rand.nextBytes(data);
final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();
InBandBytestreamManager targetByteStreamManager = InBandBytestreamManager.getByteStreamManager(targetConnection);
InBandBytestreamListener incomingByteStreamListener = new InBandBytestreamListener() {
public void incomingBytestreamRequest(InBandBytestreamRequest request) {
try {
InBandBytestreamSession session = request.accept();
OutputStream outputStream = session.getOutputStream();
outputStream.write(data);
outputStream.flush();
InputStream inputStream = session.getInputStream();
byte[] receivedData = new byte[dataSize];
int totalRead = 0;
while (totalRead < dataSize) {
int read = inputStream.read(receivedData, totalRead, dataSize - totalRead);
totalRead += read;
}
queue.put(receivedData);
}
catch (Exception e) {
fail(e.getMessage());
}
}
};
targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener);
InBandBytestreamManager initiatorByteStreamManager = InBandBytestreamManager.getByteStreamManager(initiatorConnection);
InBandBytestreamSession session = initiatorByteStreamManager.establishSession(targetConnection.getUser());
// verify stream
byte[] receivedData = new byte[dataSize];
InputStream inputStream = session.getInputStream();
int totalRead = 0;
while (totalRead < dataSize) {
int read = inputStream.read(receivedData, totalRead, dataSize - totalRead);
totalRead += read;
}
assertEquals("sent data not equal to received data", data, receivedData);
OutputStream outputStream = session.getOutputStream();
outputStream.write(data);
outputStream.flush();
outputStream.close();
assertEquals("received data not equal to sent data", data, queue.take());
}
@Override
protected int getMaxConnections() {
return 2;
}
}

View file

@ -0,0 +1,337 @@
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.socks5;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamListener;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamSession;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5PacketUtils;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5Proxy;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
/**
* Test for Socks5 bytestreams with real XMPP servers.
*
* @author Henning Staib
*/
public class Socks5ByteStreamTest extends SmackTestCase {
/**
* Constructor
*
* @param arg0
*/
public Socks5ByteStreamTest(String arg0) {
super(arg0);
}
/**
* Socks5 feature should be added to the service discovery on Smack startup.
*
* @throws XMPPException should not happen
*/
public void testInitializationSocks5FeaturesAndListenerOnStartup() throws XMPPException {
XMPPConnection connection = getConnection(0);
assertTrue(ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(
Socks5BytestreamManager.NAMESPACE));
}
/**
* Target should respond with not-acceptable error if no listeners for incoming Socks5
* bytestream requests are registered.
*
* @throws XMPPException should not happen
*/
public void testRespondWithErrorOnSocks5BytestreamRequest() throws XMPPException {
XMPPConnection targetConnection = getConnection(0);
XMPPConnection initiatorConnection = getConnection(1);
Bytestream bytestreamInitiation = Socks5PacketUtils.createBytestreamInitiation(
initiatorConnection.getUser(), targetConnection.getUser(), "session_id");
bytestreamInitiation.addStreamHost("proxy.localhost", "127.0.0.1", 7777);
PacketCollector collector = initiatorConnection.createPacketCollector(new PacketIDFilter(
bytestreamInitiation.getPacketID()));
initiatorConnection.sendPacket(bytestreamInitiation);
Packet result = collector.nextResult();
assertNotNull(result.getError());
assertEquals(XMPPError.Condition.no_acceptable.toString(), result.getError().getCondition());
}
/**
* Socks5 bytestream should be successfully established using the local Socks5 proxy.
*
* @throws Exception should not happen
*/
public void testSocks5BytestreamWithLocalSocks5Proxy() throws Exception {
// setup port for local socks5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(true);
SmackConfiguration.setLocalSocks5ProxyPort(7778);
Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
socks5Proxy.start();
assertTrue(socks5Proxy.isRunning());
XMPPConnection initiatorConnection = getConnection(0);
XMPPConnection targetConnection = getConnection(1);
// test data
final byte[] data = new byte[] { 1, 2, 3 };
final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();
Socks5BytestreamManager targetByteStreamManager = Socks5BytestreamManager.getBytestreamManager(targetConnection);
Socks5BytestreamListener incomingByteStreamListener = new Socks5BytestreamListener() {
public void incomingBytestreamRequest(Socks5BytestreamRequest request) {
InputStream inputStream;
try {
Socks5BytestreamSession session = request.accept();
inputStream = session.getInputStream();
byte[] receivedData = new byte[3];
inputStream.read(receivedData);
queue.put(receivedData);
}
catch (Exception e) {
fail(e.getMessage());
}
}
};
targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener);
Socks5BytestreamManager initiatorByteStreamManager = Socks5BytestreamManager.getBytestreamManager(initiatorConnection);
Socks5BytestreamSession session = initiatorByteStreamManager.establishSession(
targetConnection.getUser());
OutputStream outputStream = session.getOutputStream();
assertTrue(session.isDirect());
// verify stream
outputStream.write(data);
outputStream.flush();
outputStream.close();
assertEquals("received data not equal to sent data", data, queue.take());
// reset default configuration
SmackConfiguration.setLocalSocks5ProxyPort(7777);
}
/**
* Socks5 bytestream should be successfully established using a Socks5 proxy provided by the
* XMPP server.
* <p>
* This test will fail if the XMPP server doesn't provide any Socks5 proxies or the Socks5 proxy
* only allows Socks5 bytestreams in the context of a file transfer (like Openfire in default
* configuration, see xmpp.proxy.transfer.required flag).
*
* @throws Exception if no Socks5 proxies found or proxy is unwilling to activate Socks5
* bytestream
*/
public void testSocks5BytestreamWithRemoteSocks5Proxy() throws Exception {
// disable local socks5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false);
Socks5Proxy.getSocks5Proxy().stop();
assertFalse(Socks5Proxy.getSocks5Proxy().isRunning());
XMPPConnection initiatorConnection = getConnection(0);
XMPPConnection targetConnection = getConnection(1);
// test data
final byte[] data = new byte[] { 1, 2, 3 };
final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();
Socks5BytestreamManager targetByteStreamManager = Socks5BytestreamManager.getBytestreamManager(targetConnection);
Socks5BytestreamListener incomingByteStreamListener = new Socks5BytestreamListener() {
public void incomingBytestreamRequest(Socks5BytestreamRequest request) {
InputStream inputStream;
try {
Socks5BytestreamSession session = request.accept();
inputStream = session.getInputStream();
byte[] receivedData = new byte[3];
inputStream.read(receivedData);
queue.put(receivedData);
}
catch (Exception e) {
fail(e.getMessage());
}
}
};
targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener);
Socks5BytestreamManager initiatorByteStreamManager = Socks5BytestreamManager.getBytestreamManager(initiatorConnection);
Socks5BytestreamSession session = initiatorByteStreamManager.establishSession(
targetConnection.getUser());
OutputStream outputStream = session.getOutputStream();
assertTrue(session.isMediated());
// verify stream
outputStream.write(data);
outputStream.flush();
outputStream.close();
assertEquals("received data not equal to sent data", data, queue.take());
// reset default configuration
SmackConfiguration.setLocalSocks5ProxyEnabled(true);
Socks5Proxy.getSocks5Proxy().start();
}
/**
* Socks5 bytestream should be successfully established using a Socks5 proxy provided by the
* XMPP server. The established connection should transfer data bidirectional if the Socks5
* proxy supports it.
* <p>
* Support for bidirectional Socks5 bytestream:
* <ul>
* <li>Openfire (3.6.4 and below) - no</li>
* <li>ejabberd (2.0.5 and higher) - yes</li>
* </ul>
* <p>
* This test will fail if the XMPP server doesn't provide any Socks5 proxies or the Socks5 proxy
* only allows Socks5 bytestreams in the context of a file transfer (like Openfire in default
* configuration, see xmpp.proxy.transfer.required flag).
*
* @throws Exception if no Socks5 proxies found or proxy is unwilling to activate Socks5
* bytestream
*/
public void testBiDirectionalSocks5BytestreamWithRemoteSocks5Proxy() throws Exception {
XMPPConnection initiatorConnection = getConnection(0);
// disable local socks5 proxy
SmackConfiguration.setLocalSocks5ProxyEnabled(false);
Socks5Proxy.getSocks5Proxy().stop();
assertFalse(Socks5Proxy.getSocks5Proxy().isRunning());
XMPPConnection targetConnection = getConnection(1);
// test data
final byte[] data = new byte[] { 1, 2, 3 };
final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();
Socks5BytestreamManager targetByteStreamManager = Socks5BytestreamManager.getBytestreamManager(targetConnection);
Socks5BytestreamListener incomingByteStreamListener = new Socks5BytestreamListener() {
public void incomingBytestreamRequest(Socks5BytestreamRequest request) {
try {
Socks5BytestreamSession session = request.accept();
OutputStream outputStream = session.getOutputStream();
outputStream.write(data);
outputStream.flush();
InputStream inputStream = session.getInputStream();
byte[] receivedData = new byte[3];
inputStream.read(receivedData);
queue.put(receivedData);
session.close();
}
catch (Exception e) {
fail(e.getMessage());
}
}
};
targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener);
Socks5BytestreamManager initiatorByteStreamManager = Socks5BytestreamManager.getBytestreamManager(initiatorConnection);
Socks5BytestreamSession session = initiatorByteStreamManager.establishSession(targetConnection.getUser());
assertTrue(session.isMediated());
// verify stream
final byte[] receivedData = new byte[3];
final InputStream inputStream = session.getInputStream();
FutureTask<Integer> futureTask = new FutureTask<Integer>(new Callable<Integer>() {
public Integer call() throws Exception {
return inputStream.read(receivedData);
}
});
Thread executor = new Thread(futureTask);
executor.start();
try {
futureTask.get(2000, TimeUnit.MILLISECONDS);
}
catch (TimeoutException e) {
// reset default configuration
SmackConfiguration.setLocalSocks5ProxyEnabled(true);
Socks5Proxy.getSocks5Proxy().start();
fail("Couldn't send data from target to inititator");
}
assertEquals("sent data not equal to received data", data, receivedData);
OutputStream outputStream = session.getOutputStream();
outputStream.write(data);
outputStream.flush();
outputStream.close();
assertEquals("received data not equal to sent data", data, queue.take());
session.close();
// reset default configuration
SmackConfiguration.setLocalSocks5ProxyEnabled(true);
Socks5Proxy.getSocks5Proxy().start();
}
@Override
protected int getMaxConnections() {
return 2;
}
}

View file

@ -0,0 +1,111 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.commands;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.packet.DiscoverItems;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.FormField;
/**
* AdHocCommand tests.
*
* @author Matt Tucker
*/
public class AdHocCommandDiscoTest extends SmackTestCase {
/**
* Constructor for test.
* @param arg0 argument.
*/
public AdHocCommandDiscoTest(String arg0) {
super(arg0);
}
public void testAdHocCommands() {
try {
AdHocCommandManager manager1 = AdHocCommandManager.getAddHocCommandsManager(getConnection(0));
manager1.registerCommand("test", "test node", LocalCommand.class);
manager1.registerCommand("test2", "test node", new LocalCommandFactory() {
public LocalCommand getInstance() throws InstantiationException, IllegalAccessException {
return new LocalCommand() {
public boolean isLastStage() {
return true;
}
public boolean hasPermission(String jid) {
return true;
}
public void execute() throws XMPPException {
Form result = new Form(Form.TYPE_RESULT);
FormField resultField = new FormField("test2");
resultField.setLabel("test node");
resultField.addValue("it worked");
result.addField(resultField);
setForm(result);
}
public void next(Form response) throws XMPPException {
//
}
public void complete(Form response) throws XMPPException {
//
}
public void prev() throws XMPPException {
//
}
public void cancel() throws XMPPException {
//
}
};
}
});
AdHocCommandManager manager2 = AdHocCommandManager.getAddHocCommandsManager(getConnection(1));
DiscoverItems items = manager2.discoverCommands(getFullJID(0));
assertTrue("Disco for command test failed", items.getItems().next().getNode().equals("test"));
RemoteCommand command = manager2.getRemoteCommand(getFullJID(0), "test2");
command.execute();
assertEquals("Disco for command test failed", command.getForm().getField("test2").getValues().next(), "it worked");
}
catch (Exception e) {
fail(e.getMessage());
}
}
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
protected int getMaxConnections() {
return 2;
}
}

View file

@ -0,0 +1,147 @@
package org.jivesoftware.smackx.entitycaps;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.TCPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.packet.DiscoverInfo;
public class EntityCapsTest extends SmackTestCase {
private static final String DISCOVER_TEST_FEATURE = "entityCapsTest";
XMPPTCPConnection con0;
XMPPTCPConnection con1;
EntityCapsManager ecm0;
EntityCapsManager ecm1;
ServiceDiscoveryManager sdm0;
ServiceDiscoveryManager sdm1;
private boolean discoInfoSend = false;
public EntityCapsTest(String arg0) {
super(arg0);
}
@Override
protected int getMaxConnections() {
return 2;
}
@Override
protected void setUp() throws Exception {
super.setUp();
SmackConfiguration.setAutoEnableEntityCaps(true);
con0 = getConnection(0);
con1 = getConnection(1);
ecm0 = EntityCapsManager.getInstanceFor(getConnection(0));
ecm1 = EntityCapsManager.getInstanceFor(getConnection(1));
sdm0 = ServiceDiscoveryManager.getInstanceFor(con0);
sdm1 = ServiceDiscoveryManager.getInstanceFor(con1);
letsAllBeFriends();
}
public void testLocalEntityCaps() throws InterruptedException {
DiscoverInfo info = EntityCapsManager.getDiscoveryInfoByNodeVer(ecm1.getLocalNodeVer());
assertFalse(info.containsFeature(DISCOVER_TEST_FEATURE));
dropWholeEntityCapsCache();
// This should cause a new presence stanza from con1 with and updated
// 'ver' String
sdm1.addFeature(DISCOVER_TEST_FEATURE);
// Give the server some time to handle the stanza and send it to con0
Thread.sleep(2000);
// The presence stanza should get received by con0 and the data should
// be recorded in the map
// Note that while both connections use the same static Entity Caps
// cache,
// it's assured that *not* con1 added the data to the Entity Caps cache.
// Every time the entities features
// and identities change only a new caps 'ver' is calculated and send
// with the presence stanza
// The other connection has to receive this stanza and record the
// information in order for this test to succeed.
info = EntityCapsManager.getDiscoveryInfoByNodeVer(ecm1.getLocalNodeVer());
assertNotNull(info);
assertTrue(info.containsFeature(DISCOVER_TEST_FEATURE));
}
/**
* Test if entity caps actually prevent a disco info request and reply
*
* @throws XMPPException
*
*/
public void testPreventDiscoInfo() throws XMPPException {
con0.addPacketSendingListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
discoInfoSend = true;
}
}, new AndFilter(new PacketTypeFilter(DiscoverInfo.class), new IQTypeFilter(IQ.Type.GET)));
// add a bogus feature so that con1 ver won't match con0's
sdm1.addFeature(DISCOVER_TEST_FEATURE);
dropCapsCache();
// discover that
DiscoverInfo info = sdm0.discoverInfo(con1.getUser());
// that discovery should cause a disco#info
assertTrue(discoInfoSend);
assertTrue(info.containsFeature(DISCOVER_TEST_FEATURE));
discoInfoSend = false;
// discover that
info = sdm0.discoverInfo(con1.getUser());
// that discovery shouldn't cause a disco#info
assertFalse(discoInfoSend);
assertTrue(info.containsFeature(DISCOVER_TEST_FEATURE));
}
public void testCapsChanged() {
String nodeVerBefore = EntityCapsManager.getNodeVersionByJid(con1.getUser());
sdm1.addFeature(DISCOVER_TEST_FEATURE);
String nodeVerAfter = EntityCapsManager.getNodeVersionByJid(con1.getUser());
assertFalse(nodeVerBefore.equals(nodeVerAfter));
}
public void testEntityCaps() throws XMPPException, InterruptedException {
dropWholeEntityCapsCache();
sdm1.addFeature(DISCOVER_TEST_FEATURE);
Thread.sleep(3000);
DiscoverInfo info = sdm0.discoverInfo(con1.getUser());
assertTrue(info.containsFeature(DISCOVER_TEST_FEATURE));
String u1ver = EntityCapsManager.getNodeVersionByJid(con1.getUser());
assertNotNull(u1ver);
DiscoverInfo entityInfo = EntityCapsManager.caps.get(u1ver);
assertNotNull(entityInfo);
assertEquals(info.toXML(), entityInfo.toXML());
}
private static void dropWholeEntityCapsCache() {
EntityCapsManager.caps.clear();
EntityCapsManager.jidCaps.clear();
}
private static void dropCapsCache() {
EntityCapsManager.caps.clear();
}
}

View file

@ -0,0 +1,117 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.muc;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.FormField;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Tests creating new MUC rooms.
*
* @author Gaston Dombiak
*/
public class MultiUserChatCreationTest extends SmackTestCase {
private String room;
/**
* Constructor for MultiUserChatCreationTest.
* @param arg0
*/
public MultiUserChatCreationTest(String arg0) {
super(arg0);
}
/**
* Tests creating a new "Reserved Room".
*/
public void testCreateReservedRoom() {
MultiUserChat muc = new MultiUserChat(getConnection(0), room);
try {
// Create the room
muc.create("testbot1");
// Get the the room's configuration form
Form form = muc.getConfigurationForm();
assertNotNull("No room configuration form", form);
// Create a new form to submit based on the original form
Form submitForm = form.createAnswerForm();
// Add default answers to the form to submit
for (Iterator<FormField> fields = form.getFields(); fields.hasNext();) {
FormField field = fields.next();
if (!FormField.TYPE_HIDDEN.equals(field.getType())
&& field.getVariable() != null) {
// Sets the default value as the answer
submitForm.setDefaultAnswer(field.getVariable());
}
}
List<String> owners = new ArrayList<String>();
owners.add(getBareJID(0));
submitForm.setAnswer("muc#roomconfig_roomowners", owners);
// Update the new room's configuration
muc.sendConfigurationForm(submitForm);
// Destroy the new room
muc.destroy("The room has almost no activity...", null);
}
catch (XMPPException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
/**
* Tests creating a new "Instant Room".
*/
public void testCreateInstantRoom() {
MultiUserChat muc = new MultiUserChat(getConnection(0), room);
try {
// Create the room
muc.create("testbot");
// Send an empty room configuration form which indicates that we want
// an instant room
muc.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
// Destroy the new room
muc.destroy("The room has almost no activity...", null);
}
catch (XMPPException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
protected int getMaxConnections() {
return 2;
}
protected void setUp() throws Exception {
super.setUp();
room = "fruta124@" + getMUCDomain();
}
}

View file

@ -0,0 +1,132 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.packet;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.test.SmackTestCase;
/**
*
* Test the MessageEvent extension using the low level API
*
* @author Gaston Dombiak
*/
public class MessageEventTest extends SmackTestCase {
public MessageEventTest(String name) {
super(name);
}
/**
* Low level API test.
* This is a simple test to use with a XMPP client and check if the client receives the
* message
* 1. User_1 will send a message to user_2 requesting to be notified when any of these events
* occurs: offline, composing, displayed or delivered
*/
public void testSendMessageEventRequest() {
// Create a chat for each connection
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
// Create the message to send with the roster
Message msg = new Message();
msg.setSubject("Any subject you want");
msg.setBody("An interesting body comes here...");
// Create a MessageEvent Package and add it to the message
MessageEvent messageEvent = new MessageEvent();
messageEvent.setComposing(true);
messageEvent.setDelivered(true);
messageEvent.setDisplayed(true);
messageEvent.setOffline(true);
msg.addExtension(messageEvent);
// Send the message that contains the notifications request
try {
chat1.sendMessage(msg);
// Wait half second so that the complete test can run
Thread.sleep(200);
}
catch (Exception e) {
fail("An error occured sending the message");
}
}
/**
* Low level API test.
* This is a simple test to use with a XMPP client, check if the client receives the
* message and display in the console any notification
* 1. User_1 will send a message to user_2 requesting to be notified when any of these events
* occurs: offline, composing, displayed or delivered
* 2. User_2 will use a XMPP client (like Exodus) to display the message and compose a reply
* 3. User_1 will display any notification that receives
*/
public void testSendMessageEventRequestAndDisplayNotifications() {
// Create a chat for each connection
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
// Create a Listener that listens for Messages with the extension "jabber:x:roster"
// This listener will listen on the conn2 and answer an ACK if everything is ok
PacketFilter packetFilter = new PacketExtensionFilter("x", "jabber:x:event");
PacketListener packetListener = new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
try {
MessageEvent messageEvent =
(MessageEvent) message.getExtension("x", "jabber:x:event");
assertNotNull("Message without extension \"jabber:x:event\"", messageEvent);
assertTrue(
"Message event is a request not a notification",
!messageEvent.isMessageEventRequest());
System.out.println(messageEvent.toXML());
}
catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
}
}
};
getConnection(0).addPacketListener(packetListener, packetFilter);
// Create the message to send with the roster
Message msg = new Message();
msg.setSubject("Any subject you want");
msg.setBody("An interesting body comes here...");
// Create a MessageEvent Package and add it to the message
MessageEvent messageEvent = new MessageEvent();
messageEvent.setComposing(true);
messageEvent.setDelivered(true);
messageEvent.setDisplayed(true);
messageEvent.setOffline(true);
msg.addExtension(messageEvent);
// Send the message that contains the notifications request
try {
chat1.sendMessage(msg);
// Wait half second so that the complete test can run
Thread.sleep(200);
}
catch (Exception e) {
fail("An error occured sending the message");
}
}
protected int getMaxConnections() {
return 2;
}
}

View file

@ -0,0 +1,185 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.packet;
import java.util.Iterator;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.*;
/**
*
* Test the Roster Exchange extension using the low level API
*
* @author Gaston Dombiak
*/
public class RosterExchangeTest extends SmackTestCase {
public RosterExchangeTest(String arg0) {
super(arg0);
}
/**
* Low level API test.
* This is a simple test to use with a XMPP client and check if the client receives the message
* 1. User_1 will send his/her roster entries to user_2
*/
public void testSendRosterEntries() {
// Create a chat for each connection
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
// Create the message to send with the roster
Message msg = new Message();
msg.setSubject("Any subject you want");
msg.setBody("This message contains roster items.");
// Create a RosterExchange Package and add it to the message
assertTrue("Roster has no entries", getConnection(0).getRoster().getEntryCount() > 0);
RosterExchange rosterExchange = new RosterExchange(getConnection(0).getRoster());
msg.addExtension(rosterExchange);
// Send the message that contains the roster
try {
chat1.sendMessage(msg);
} catch (Exception e) {
fail("An error occured sending the message with the roster");
}
}
/**
* Low level API test.
* 1. User_1 will send his/her roster entries to user_2
* 2. User_2 will receive the entries and iterate over them to check if everything is fine
* 3. User_1 will wait several seconds for an ACK from user_2, if none is received then something is wrong
*/
public void testSendAndReceiveRosterEntries() {
// Create a chat for each connection
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
final PacketCollector chat2 = getConnection(1).createPacketCollector(
new ThreadFilter(chat1.getThreadID()));
// Create the message to send with the roster
Message msg = new Message();
msg.setSubject("Any subject you want");
msg.setBody("This message contains roster items.");
// Create a RosterExchange Package and add it to the message
assertTrue("Roster has no entries", getConnection(0).getRoster().getEntryCount() > 0);
RosterExchange rosterExchange = new RosterExchange(getConnection(0).getRoster());
msg.addExtension(rosterExchange);
// Send the message that contains the roster
try {
chat1.sendMessage(msg);
} catch (Exception e) {
fail("An error occured sending the message with the roster");
}
// Wait for 2 seconds for a reply
Packet packet = chat2.nextResult(2000);
Message message = (Message) packet;
assertNotNull("Body is null", message.getBody());
try {
rosterExchange =
(RosterExchange) message.getExtension("x", "jabber:x:roster");
assertNotNull("Message without extension \"jabber:x:roster\"", rosterExchange);
assertTrue(
"Roster without entries",
rosterExchange.getRosterEntries().hasNext());
}
catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
}
}
/**
* Low level API test.
* 1. User_1 will send his/her roster entries to user_2
* 2. User_2 will automatically add the entries that receives to his/her roster in the corresponding group
* 3. User_1 will wait several seconds for an ACK from user_2, if none is received then something is wrong
*/
public void testSendAndAcceptRosterEntries() {
// Create a chat for each connection
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
final PacketCollector chat2 = getConnection(1).createPacketCollector(
new ThreadFilter(chat1.getThreadID()));
// Create the message to send with the roster
Message msg = new Message();
msg.setSubject("Any subject you want");
msg.setBody("This message contains roster items.");
// Create a RosterExchange Package and add it to the message
assertTrue("Roster has no entries", getConnection(0).getRoster().getEntryCount() > 0);
RosterExchange rosterExchange = new RosterExchange(getConnection(0).getRoster());
msg.addExtension(rosterExchange);
// Send the message that contains the roster
try {
chat1.sendMessage(msg);
} catch (Exception e) {
fail("An error occured sending the message with the roster");
}
// Wait for 10 seconds for a reply
Packet packet = chat2.nextResult(5000);
Message message = (Message) packet;
assertNotNull("Body is null", message.getBody());
try {
rosterExchange =
(RosterExchange) message.getExtension("x", "jabber:x:roster");
assertNotNull("Message without extension \"jabber:x:roster\"", rosterExchange);
assertTrue(
"Roster without entries",
rosterExchange.getRosterEntries().hasNext());
// Add the roster entries to user2's roster
for (Iterator<RemoteRosterEntry> it = rosterExchange.getRosterEntries(); it.hasNext();) {
RemoteRosterEntry remoteRosterEntry = it.next();
getConnection(1).getRoster().createEntry(
remoteRosterEntry.getUser(),
remoteRosterEntry.getName(),
remoteRosterEntry.getGroupArrayNames());
}
}
catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
}
catch (Exception e) {
fail(e.toString());
}
assertTrue("Roster2 has no entries", getConnection(1).getRoster().getEntryCount() > 0);
}
protected void setUp() throws Exception {
super.setUp();
try {
getConnection(0).getRoster().createEntry(
getBareJID(2),
"gato5",
new String[] { "Friends, Coworker" });
getConnection(0).getRoster().createEntry(getBareJID(3), "gato6", null);
Thread.sleep(300);
} catch (Exception e) {
fail(e.getMessage());
}
}
protected int getMaxConnections() {
return 4;
}
}

View file

@ -0,0 +1,212 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.packet;
import java.util.Iterator;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.ThreadFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.test.SmackTestCase;
/**
* Test the XHTML extension using the low level API
*
* @author Gaston Dombiak
*/
public class XHTMLExtensionTest extends SmackTestCase {
private int bodiesSent;
private int bodiesReceived;
public XHTMLExtensionTest(String name) {
super(name);
}
/**
* Low level API test.
* This is a simple test to use with a XMPP client and check if the client receives the message
* 1. User_1 will send a message with formatted text (XHTML) to user_2
*/
public void testSendSimpleXHTMLMessage() {
// User1 creates a chat with user2
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
// User1 creates a message to send to user2
Message msg = new Message();
msg.setSubject("Any subject you want");
msg.setBody("Hey John, this is my new green!!!!");
// Create a XHTMLExtension Package and add it to the message
XHTMLExtension xhtmlExtension = new XHTMLExtension();
xhtmlExtension.addBody(
"<body><p style='font-size:large'>Hey John, this is my new <span style='color:green'>green</span><em>!!!!</em></p></body>");
msg.addExtension(xhtmlExtension);
// User1 sends the message that contains the XHTML to user2
try {
chat1.sendMessage(msg);
Thread.sleep(200);
}
catch (Exception e) {
fail("An error occured sending the message with XHTML");
}
}
/**
* Low level API test.
* 1. User_1 will send a message with XHTML to user_2
* 2. User_2 will receive the message and iterate over the XHTML bodies to check if everything
* is fine
* 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
* something is wrong
*/
public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() {
// Create a chat for each connection
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
final PacketCollector chat2 = getConnection(1).createPacketCollector(
new ThreadFilter(chat1.getThreadID()));
// User1 creates a message to send to user2
Message msg = new Message();
msg.setSubject("Any subject you want");
msg.setBody("Hey John, this is my new green!!!!");
// Create a XHTMLExtension Package and add it to the message
XHTMLExtension xhtmlExtension = new XHTMLExtension();
xhtmlExtension.addBody(
"<body><p style='font-size:large'>Hey John, this is my new <span style='color:green'>green</span><em>!!!!</em></p></body>");
msg.addExtension(xhtmlExtension);
// User1 sends the message that contains the XHTML to user2
try {
chat1.sendMessage(msg);
}
catch (Exception e) {
fail("An error occured sending the message with XHTML");
}
Packet packet = chat2.nextResult(2000);
Message message = (Message) packet;
assertNotNull("Body is null", message.getBody());
try {
xhtmlExtension =
(XHTMLExtension) message.getExtension(
"html",
"http://jabber.org/protocol/xhtml-im");
assertNotNull(
"Message without extension \"http://jabber.org/protocol/xhtml-im\"",
xhtmlExtension);
assertTrue("Message without XHTML bodies", xhtmlExtension.getBodiesCount() > 0);
for (Iterator<String> it = xhtmlExtension.getBodies(); it.hasNext();) {
String body = it.next();
System.out.println(body);
}
}
catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
}
}
/**
* Low level API test. Test a message with two XHTML bodies and several XHTML tags.
* 1. User_1 will send a message with XHTML to user_2
* 2. User_2 will receive the message and iterate over the XHTML bodies to check if everything
* is fine
* 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
* something is wrong
*/
public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() {
// Create a chat for each connection
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
final PacketCollector chat2 = getConnection(1).createPacketCollector(
new ThreadFilter(chat1.getThreadID()));
// Create a Listener that listens for Messages with the extension
//"http://jabber.org/protocol/xhtml-im"
// This listener will listen on the conn2 and answer an ACK if everything is ok
PacketFilter packetFilter =
new PacketExtensionFilter("html", "http://jabber.org/protocol/xhtml-im");
PacketListener packetListener = new PacketListener() {
@Override
public void processPacket(Packet packet) {
}
};
getConnection(1).addPacketListener(packetListener, packetFilter);
// User1 creates a message to send to user2
Message msg = new Message();
msg.setSubject("Any subject you want");
msg.setBody(
"awesome! As Emerson once said: A foolish consistency is the hobgoblin of little minds.");
// Create an XHTMLExtension and add it to the message
XHTMLExtension xhtmlExtension = new XHTMLExtension();
xhtmlExtension.addBody(
"<body xml:lang=\"es-ES\"><h1>impresionante!</h1><p>Como Emerson dijo una vez:</p><blockquote><p>Una consistencia ridicula es el espantajo de mentes pequenas.</p></blockquote></body>");
xhtmlExtension.addBody(
"<body xml:lang=\"en-US\"><h1>awesome!</h1><p>As Emerson once said:</p><blockquote><p>A foolish consistency is the hobgoblin of little minds.</p></blockquote></body>");
msg.addExtension(xhtmlExtension);
// User1 sends the message that contains the XHTML to user2
try {
bodiesSent = xhtmlExtension.getBodiesCount();
bodiesReceived = 0;
chat1.sendMessage(msg);
}
catch (Exception e) {
fail("An error occured sending the message with XHTML");
}
Packet packet = chat2.nextResult(2000);
int received = 0;
Message message = (Message) packet;
assertNotNull("Body is null", message.getBody());
try {
xhtmlExtension =
(XHTMLExtension) message.getExtension(
"html",
"http://jabber.org/protocol/xhtml-im");
assertNotNull(
"Message without extension \"http://jabber.org/protocol/xhtml-im\"",
xhtmlExtension);
assertTrue("Message without XHTML bodies", xhtmlExtension.getBodiesCount() > 0);
for (Iterator<String> it = xhtmlExtension.getBodies(); it.hasNext();) {
received++;
System.out.println(it.next());
}
bodiesReceived = received;
}
catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is " +
"misconfigured");
}
// Wait half second so that the complete test can run
assertEquals(
"Number of sent and received XHTMP bodies does not match",
bodiesSent,
bodiesReceived);
}
@Override
protected int getMaxConnections() {
return 2;
}
}

View file

@ -0,0 +1,63 @@
/**
*
* Copyright 2009 Robin Collier.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.pubsub;
import org.jivesoftware.smack.packet.PacketExtension;
/**
*
* @author Robin Collier
*
*/
class CarExtension implements PacketExtension
{
private String color;
private int numTires;
public CarExtension(String col, int num)
{
color = col;
numTires = num;
}
public String getColor()
{
return color;
}
public int getNumTires()
{
return numTires;
}
public String getElementName()
{
return "car";
}
public String getNamespace()
{
return "pubsub:test:vehicle";
}
public String toXML()
{
return "<" + getElementName() + " xmlns='" + getNamespace() + "'><paint color='" +
getColor() + "'/><tires num='" + getNumTires() + "'/></" + getElementName() + ">";
}
}

View file

@ -0,0 +1,53 @@
/**
*
* Copyright 2009 Robin Collier
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.pubsub;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.xmlpull.v1.XmlPullParser;
/**
*
* @author Robin Collier
*
*/
public class CarExtensionProvider implements PacketExtensionProvider
{
public PacketExtension parseExtension(XmlPullParser parser) throws Exception
{
String color = null;
int numTires = 0;
for (int i=0; i<2; i++)
{
while (parser.next() != XmlPullParser.START_TAG);
if (parser.getName().equals("paint"))
{
color = parser.getAttributeValue(0);
}
else
{
numTires = Integer.parseInt(parser.getAttributeValue(0));
}
}
while (parser.next() != XmlPullParser.END_TAG);
return new CarExtension(color, numTires);
}
}

View file

@ -0,0 +1,89 @@
/**
*
* Copyright 2009 Robin Collier.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.pubsub;
import java.util.Iterator;
import java.util.List;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.smackx.packet.DiscoverItems;
import org.jivesoftware.smackx.packet.DiscoverInfo.Identity;
import org.jivesoftware.smackx.pubsub.test.SingleUserTestCase;
/**
*
* @author Robin Collier
*
*/
public class EntityUseCases extends SingleUserTestCase
{
public void testDiscoverPubsubInfo() throws Exception
{
DiscoverInfo supportedFeatures = getManager().getSupportedFeatures();
assertNotNull(supportedFeatures);
}
public void testDiscoverNodeInfo() throws Exception
{
LeafNode myNode = getManager().createNode("DiscoNode" + System.currentTimeMillis());
DiscoverInfo info = myNode.discoverInfo();
assertTrue(info.getIdentities().hasNext());
Identity ident = info.getIdentities().next();
assertEquals("leaf", ident.getType());
}
public void testDiscoverNodeItems() throws Exception
{
LeafNode myNode = getRandomPubnode(getManager(), true, false);
myNode.send(new Item());
myNode.send(new Item());
myNode.send(new Item());
myNode.send(new Item());
DiscoverItems items = myNode.discoverItems();
int count = 0;
for(Iterator<DiscoverItems.Item> it = items.getItems(); it.hasNext(); it.next(),count++);
assertEquals(4, count);
}
public void testDiscoverSubscriptions() throws Exception
{
getManager().getSubscriptions();
}
public void testDiscoverNodeSubscriptions() throws Exception
{
LeafNode myNode = getRandomPubnode(getManager(), true, true);
myNode.subscribe(getConnection(0).getUser());
List<Subscription> subscriptions = myNode.getSubscriptions();
assertTrue(subscriptions.size() < 3);
for (Subscription subscription : subscriptions)
{
assertNull(subscription.getNode());
}
}
public void testRetrieveAffiliation() throws Exception
{
getManager().getAffiliations();
}
}

View file

@ -0,0 +1,82 @@
/**
*
* Copyright 2009 Robin Collier.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.pubsub;
import java.util.Collection;
import java.util.List;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smackx.pubsub.test.PubSubTestCase;
/**
*
* @author Robin Collier
*
*/
public class MultiUserSubscriptionUseCases extends PubSubTestCase
{
@Override
protected int getMaxConnections()
{
return 2;
}
public void testGetItemsWithSingleSubscription() throws XMPPException
{
LeafNode node = getRandomPubnode(getManager(0), true, false);
node.send((Item)null);
node.send((Item)null);
node.send((Item)null);
node.send((Item)null);
node.send((Item)null);
LeafNode user2Node = (LeafNode) getManager(1).getNode(node.getId());
user2Node.subscribe(getBareJID(1));
Collection<? extends Item> items = user2Node.getItems();
assertTrue(items.size() == 5);
}
public void testGetItemsWithMultiSubscription() throws XMPPException
{
LeafNode node = getRandomPubnode(getManager(0), true, false);
node.send((Item)null);
node.send((Item)null);
node.send((Item)null);
node.send((Item)null);
node.send((Item)null);
LeafNode user2Node = (LeafNode) getManager(1).getNode(node.getId());
Subscription sub1 = user2Node.subscribe(getBareJID(1));
Subscription sub2 = user2Node.subscribe(getBareJID(1));
try
{
user2Node.getItems();
}
catch (XMPPException exc)
{
assertEquals("bad-request", exc.getXMPPError().getCondition());
assertEquals(XMPPError.Type.MODIFY, exc.getXMPPError().getType());
}
List<Item> items = user2Node.getItems(sub1.getId());
assertTrue(items.size() == 5);
}
}

View file

@ -0,0 +1,148 @@
/**
*
* Copyright 2009 Robin Collier
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.pubsub;
import java.util.Collection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.pubsub.test.SingleUserTestCase;
/**
*
* @author Robin Collier
*
*/
public class OwnerUseCases extends SingleUserTestCase
{
public void testCreateInstantNode() throws Exception
{
LeafNode node = getManager().createNode();
assertNotNull(node);
assertNotNull(node.getId());
}
public void testCreateNamedNode() throws Exception
{
String id = "TestNamedNode" + System.currentTimeMillis();
LeafNode node = getManager().createNode(id);
assertEquals(id, node.getId());
}
public void testCreateConfiguredNode() throws Exception
{
// Generate reasonably unique for multiple tests
String id = "TestConfigNode" + System.currentTimeMillis();
// Create and configure a node
ConfigureForm form = new ConfigureForm(FormType.submit);
form.setAccessModel(AccessModel.open);
form.setDeliverPayloads(false);
form.setNotifyRetract(true);
form.setPersistentItems(true);
form.setPublishModel(PublishModel.open);
LeafNode node = (LeafNode)getManager().createNode(id, form);
ConfigureForm currentForm = node.getNodeConfiguration();
assertEquals(AccessModel.open, currentForm.getAccessModel());
assertFalse(currentForm.isDeliverPayloads());
assertTrue(currentForm.isNotifyRetract());
assertTrue(currentForm.isPersistItems());
assertEquals(PublishModel.open, currentForm.getPublishModel());
}
public void testCreateAndUpdateConfiguredNode() throws Exception
{
// Generate reasonably unique for multiple tests
String id = "TestConfigNode2" + System.currentTimeMillis();
// Create and configure a node
ConfigureForm form = new ConfigureForm(FormType.submit);
form.setAccessModel(AccessModel.open);
form.setDeliverPayloads(false);
form.setNotifyRetract(true);
form.setPersistentItems(true);
form.setPublishModel(PublishModel.open);
LeafNode myNode = (LeafNode)getManager().createNode(id, form);
ConfigureForm config = myNode.getNodeConfiguration();
assertEquals(AccessModel.open, config.getAccessModel());
assertFalse(config.isDeliverPayloads());
assertTrue(config.isNotifyRetract());
assertTrue(config.isPersistItems());
assertEquals(PublishModel.open, config.getPublishModel());
ConfigureForm submitForm = new ConfigureForm(config.createAnswerForm());
submitForm.setAccessModel(AccessModel.whitelist);
submitForm.setDeliverPayloads(true);
submitForm.setNotifyRetract(false);
submitForm.setPersistentItems(false);
submitForm.setPublishModel(PublishModel.publishers);
myNode.sendConfigurationForm(submitForm);
ConfigureForm newConfig = myNode.getNodeConfiguration();
assertEquals(AccessModel.whitelist, newConfig.getAccessModel());
assertTrue(newConfig.isDeliverPayloads());
assertFalse(newConfig.isNotifyRetract());
assertFalse(newConfig.isPersistItems());
assertEquals(PublishModel.publishers, newConfig.getPublishModel());
}
public void testGetDefaultConfig() throws Exception
{
ConfigureForm form = getManager().getDefaultConfiguration();
assertNotNull(form);
}
public void testDeleteNode() throws Exception
{
LeafNode myNode = getManager().createNode();
assertNotNull(getManager().getNode(myNode.getId()));
getManager(0).deleteNode(myNode.getId());
try
{
assertNull(getManager().getNode(myNode.getId()));
fail("Node should not exist");
}
catch (XMPPException e)
{
}
}
public void testPurgeItems() throws XMPPException
{
LeafNode node = getRandomPubnode(getManager(), true, false);
node.send(new Item());
node.send(new Item());
node.send(new Item());
node.send(new Item());
node.send(new Item());
Collection<? extends Item> items = node.getItems();
assertTrue(items.size() == 5);
node.deleteAllItems();
items = node.getItems();
// Pubsub service may keep the last notification (in spec), so 0 or 1 may be returned on get items.
assertTrue(items.size() < 2);
}
}

View file

@ -0,0 +1,166 @@
/**
*
* Copyright 2009 Robin Collier
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.pubsub;
import java.util.Collection;
import java.util.List;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.packet.XMPPError.Condition;
import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
import org.jivesoftware.smackx.pubsub.test.SingleUserTestCase;
/**
*
* @author Robin Collier
*
*/
public class PublisherUseCases extends SingleUserTestCase
{
public void testSendNodeTrNot() throws Exception
{
getPubnode(false, false).send();
}
public void testSendNodeTrPay_WithOutPayload() throws XMPPException
{
LeafNode node = getPubnode(false, true);
try
{
node.send(new Item());
fail("Exception should be thrown when there is no payload");
}
catch (XMPPException e) {
XMPPError err = e.getXMPPError();
assertTrue(err.getType().equals(XMPPError.Type.MODIFY));
assertTrue(err.getCondition().equals(Condition.bad_request.toString()));
assertNotNull(err.getExtension("payload-required", PubSubNamespace.ERROR.getXmlns()));
}
try
{
node.send(new Item("test" + System.currentTimeMillis()));
fail("Exception should be thrown when there is no payload");
}
catch (XMPPException e) {
XMPPError err = e.getXMPPError();
assertTrue(err.getType().equals(XMPPError.Type.MODIFY));
assertTrue(err.getCondition().equals(Condition.bad_request.toString()));
assertNotNull(err.getExtension("payload-required", PubSubNamespace.ERROR.getXmlns()));
}
}
public void testSendNodeTrPay_WithPayload() throws XMPPException
{
LeafNode node = getPubnode(false, true);
node.send(new PayloadItem<SimplePayload>(null,
new SimplePayload("book", "pubsub:test:book", "<book xmlns='pubsub:test:book'><title>Lord of the Rings</title></book>")));
node.send(new PayloadItem<SimplePayload>("test" + System.currentTimeMillis(),
new SimplePayload("book", "pubsub:test:book", "<book xmlns='pubsub:test:book'><title>Two Towers</title></book>")));
}
public void testSendNodePerNot() throws Exception
{
LeafNode node = getPubnode(true, false);
node.send(new Item());
node.send(new Item("test" + System.currentTimeMillis()));
node.send(new PayloadItem<SimplePayload>(null,
new SimplePayload("book", "pubsub:test:book", "<book xmlns='pubsub:test:book'><title>Lord of the Rings</title></book>")));
node.send(new PayloadItem<SimplePayload>("test" + System.currentTimeMillis(),
new SimplePayload("book", "pubsub:test:book", "<book xmlns='pubsub:test:book'><title>Two Towers</title></book>")));
}
public void testSendPerPay_WithPayload() throws Exception
{
LeafNode node = getPubnode(true, true);
node.send(new PayloadItem<SimplePayload>(null,
new SimplePayload("book", "pubsub:test:book", "<book xmlns='pubsub:test:book'><title>Lord of the Rings</title></book>")));
node.send(new PayloadItem<SimplePayload>("test" + System.currentTimeMillis(),
new SimplePayload("book", "pubsub:test:book", "<book xmlns='pubsub:test:book'><title>Two Towers</title></book>")));
}
public void testSendPerPay_NoPayload() throws Exception
{
LeafNode node = getPubnode(true, true);
try
{
node.send(new Item());
fail("Exception should be thrown when there is no payload");
}
catch (XMPPException e) {
XMPPError err = e.getXMPPError();
assertTrue(err.getType().equals(XMPPError.Type.MODIFY));
assertTrue(err.getCondition().equals(Condition.bad_request.toString()));
assertNotNull(err.getExtension("payload-required", PubSubNamespace.ERROR.getXmlns()));
}
try
{
node.send(new Item("test" + System.currentTimeMillis()));
fail("Exception should be thrown when there is no payload");
}
catch (XMPPException e) {
XMPPError err = e.getXMPPError();
assertTrue(err.getType().equals(XMPPError.Type.MODIFY));
assertTrue(err.getCondition().equals(Condition.bad_request.toString()));
assertNotNull(err.getExtension("payload-required", PubSubNamespace.ERROR.getXmlns()));
}
}
public void testDeleteItems() throws XMPPException
{
LeafNode node = getPubnode(true, false);
node.send(new Item("1"));
node.send(new Item("2"));
node.send(new Item("3"));
node.send(new Item("4"));
node.deleteItem("1");
Collection<? extends Item> items = node.getItems();
assertEquals(3, items.size());
}
public void testPersistItems() throws XMPPException
{
LeafNode node = getPubnode(true, false);
node.send(new Item("1"));
node.send(new Item("2"));
node.send(new Item("3"));
node.send(new Item("4"));
Collection<? extends Item> items = node.getItems();
assertTrue(items.size() == 4);
}
public void testItemOverwritten() throws XMPPException
{
LeafNode node = getPubnode(true, false);
node.send(new PayloadItem<SimplePayload>("1", new SimplePayload("test", null, "<test/>")));
node.send(new PayloadItem<SimplePayload>("1", new SimplePayload("test2", null, "<test2/>")));
List<? extends Item> items = node.getItems();
assertEquals(1, items.size());
assertEquals("1", items.get(0).getId());
}
}

View file

@ -0,0 +1,280 @@
/**
*
* Copyright 2009 Robin Collier
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.pubsub;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.FormField;
import org.jivesoftware.smackx.pubsub.test.SingleUserTestCase;
/**
*
* @author Robin Collier
*
*/
public class SubscriberUseCases extends SingleUserTestCase
{
public void testSubscribe() throws Exception
{
LeafNode node = getPubnode(false, false);
Subscription sub = node.subscribe(getJid());
assertEquals(getJid(), sub.getJid());
assertNotNull(sub.getId());
assertEquals(node.getId(), sub.getNode());
assertEquals(Subscription.State.subscribed, sub.getState());
}
public void testSubscribeBadJid() throws Exception
{
LeafNode node = getPubnode(false, false);
try
{
node.subscribe("this@over.here");
fail();
}
catch (XMPPException e)
{
}
}
public void testSubscribeWithOptions() throws Exception
{
SubscribeForm form = new SubscribeForm(FormType.submit);
form.setDeliverOn(true);
Calendar expire = Calendar.getInstance();
expire.set(2020, 1, 1);
form.setExpiry(expire.getTime());
LeafNode node = getPubnode(false, false);
node.subscribe(getJid(), form);
}
public void testSubscribeConfigRequired() throws Exception
{
ConfigureForm form = new ConfigureForm(FormType.submit);
form.setAccessModel(AccessModel.open);
// Openfire specific field - nothing in the spec yet
FormField required = new FormField("pubsub#subscription_required");
required.setType(FormField.TYPE_BOOLEAN);
form.addField(required);
form.setAnswer("pubsub#subscription_required", true);
LeafNode node = (LeafNode)getManager().createNode("Pubnode" + System.currentTimeMillis(), form);
Subscription sub = node.subscribe(getJid());
assertEquals(getJid(), sub.getJid());
assertNotNull(sub.getId());
assertEquals(node.getId(), sub.getNode());
assertEquals(true, sub.isConfigRequired());
}
public void testUnsubscribe() throws Exception
{
LeafNode node = getPubnode(false, false);
node.subscribe(getJid());
Collection<Subscription> subs = node.getSubscriptions();
node.unsubscribe(getJid());
Collection<Subscription> afterSubs = node.getSubscriptions();
assertEquals(subs.size()-1, afterSubs.size());
}
public void testUnsubscribeWithMultipleNoSubId() throws Exception
{
LeafNode node = getPubnode(false, false);
node.subscribe(getBareJID(0));
node.subscribe(getBareJID(0));
node.subscribe(getBareJID(0));
try
{
node.unsubscribe(getBareJID(0));
fail("Unsubscribe with no subid should fail");
}
catch (XMPPException e)
{
}
}
public void testUnsubscribeWithMultipleWithSubId() throws Exception
{
LeafNode node = getPubnode(false, false);
node.subscribe(getJid());
Subscription sub = node.subscribe(getJid());
node.subscribe(getJid());
node.unsubscribe(getJid(), sub.getId());
}
public void testGetOptions() throws Exception
{
LeafNode node = getPubnode(false, false);
Subscription sub = node.subscribe(getJid());
SubscribeForm form = node.getSubscriptionOptions(getJid(), sub.getId());
assertNotNull(form);
}
// public void testSubscribeWithConfig() throws Exception
// {
// LeafNode node = getPubnode(false, false);
//
// Subscription sub = node.subscribe(getBareJID(0));
//
// assertEquals(getBareJID(0), sub.getJid());
// assertNotNull(sub.getId());
// assertEquals(node.getId(), sub.getNode());
// assertEquals(true, sub.isConfigRequired());
// }
//
public void testGetItems() throws Exception
{
LeafNode node = getPubnode(true, false);
runNodeTests(node);
}
private void runNodeTests(LeafNode node) throws Exception
{
node.send((Item)null);
node.send((Item)null);
node.send((Item)null);
node.send((Item)null);
node.send((Item)null);
Collection<? extends Item> items = node.getItems();
assertTrue(items.size() == 5);
long curTime = System.currentTimeMillis();
node.send(new Item("1-" + curTime));
node.send(new Item("2-" + curTime));
node.send(new Item("3-" + curTime));
node.send(new Item("4-" + curTime));
node.send(new Item("5-" + curTime));
items = node.getItems();
assertTrue(items.size() == 10);
LeafNode payloadNode = getPubnode(true, true);
Map<String , String> idPayload = new HashMap<String, String>();
idPayload.put("6-" + curTime, "<a/>");
idPayload.put("7-" + curTime, "<a href=\"/up/here\"/>");
idPayload.put("8-" + curTime, "<entity>text<inner></inner></entity>");
idPayload.put("9-" + curTime, "<entity><inner><text></text></inner></entity>");
for (Map.Entry<String, String> payload : idPayload.entrySet())
{
payloadNode.send(new PayloadItem<SimplePayload>(payload.getKey(), new SimplePayload("a", "pubsub:test", payload.getValue())));
}
payloadNode.send(new PayloadItem<SimplePayload>("6-" + curTime, new SimplePayload("a", "pubsub:test", "<a xmlns='pubsub:test'/>")));
payloadNode.send(new PayloadItem<SimplePayload>("7-" + curTime, new SimplePayload("a", "pubsub:test", "<a xmlns='pubsub:test' href=\'/up/here\'/>")));
payloadNode.send(new PayloadItem<SimplePayload>("8-" + curTime, new SimplePayload("entity", "pubsub:test", "<entity xmlns='pubsub:test'>text<inner>a</inner></entity>")));
payloadNode.send(new PayloadItem<SimplePayload>("9-" + curTime, new SimplePayload("entity", "pubsub:test", "<entity xmlns='pubsub:test'><inner><text>b</text></inner></entity>")));
List<PayloadItem<SimplePayload>> payloadItems = payloadNode.getItems();
Map<String, PayloadItem<SimplePayload>> idMap = new HashMap<String, PayloadItem<SimplePayload>>();
for (PayloadItem<SimplePayload> payloadItem : payloadItems)
{
idMap.put(payloadItem.getId(), payloadItem);
}
assertEquals(4, payloadItems.size());
PayloadItem<SimplePayload> testItem = idMap.get("6-" + curTime);
assertNotNull(testItem);
assertXMLEqual("<a xmlns='pubsub:test'/>", testItem.getPayload().toXML());
testItem = idMap.get("7-" + curTime);
assertNotNull(testItem);
assertXMLEqual("<a xmlns='pubsub:test' href=\'/up/here\'/>", testItem.getPayload().toXML());
testItem = idMap.get("8-" + curTime);
assertNotNull(testItem);
assertXMLEqual("<entity xmlns='pubsub:test'>text<inner>a</inner></entity>", testItem.getPayload().toXML());
testItem = idMap.get("9-" + curTime);
assertNotNull(testItem);
assertXMLEqual("<entity xmlns='pubsub:test'><inner><text>b</text></inner></entity>", testItem.getPayload().toXML());
}
public void testGetSpecifiedItems() throws Exception
{
LeafNode node = getPubnode(true, true);
node.send(new PayloadItem<SimplePayload>("1", new SimplePayload("a", "pubsub:test", "<a xmlns='pubsub:test' href='1'/>")));
node.send(new PayloadItem<SimplePayload>("2", new SimplePayload("a", "pubsub:test", "<a xmlns='pubsub:test' href='2'/>")));
node.send(new PayloadItem<SimplePayload>("3", new SimplePayload("a", "pubsub:test", "<a xmlns='pubsub:test' href='3'/>")));
node.send(new PayloadItem<SimplePayload>("4", new SimplePayload("a", "pubsub:test", "<a xmlns='pubsub:test' href='4'/>")));
node.send(new PayloadItem<SimplePayload>("5", new SimplePayload("a", "pubsub:test", "<a xmlns='pubsub:test' href='5'/>")));
Collection<String> ids = new ArrayList<String>(3);
ids.add("1");
ids.add("3");
ids.add("4");
List<PayloadItem<SimplePayload>> items = node.getItems(ids);
assertEquals(3, items.size());
assertEquals("1", items.get(0).getId());
assertXMLEqual("<a xmlns='pubsub:test' href='1'/>", items.get(0).getPayload().toXML());
assertEquals( "3", items.get(1).getId());
assertXMLEqual("<a xmlns='pubsub:test' href='3'/>", items.get(1).getPayload().toXML());
assertEquals("4", items.get(2).getId());
assertXMLEqual("<a xmlns='pubsub:test' href='4'/>", items.get(2).getPayload().toXML());
}
public void testGetLastNItems() throws XMPPException
{
LeafNode node = getPubnode(true, false);
node.send(new Item("1"));
node.send(new Item("2"));
node.send(new Item("3"));
node.send(new Item("4"));
node.send(new Item("5"));
List<Item> items = node.getItems(2);
assertEquals(2, items.size());
assertTrue(listContainsId("4", items));
assertTrue(listContainsId("5", items));
}
private static boolean listContainsId(String id, List<Item> items)
{
for (Item item : items)
{
if (item.getId().equals(id))
return true;
}
return false;
}
private String getJid()
{
return getConnection(0).getUser();
}
}

View file

@ -0,0 +1,40 @@
/**
*
* Copyright 2009 Robin Collier
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.pubsub;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.pubsub.test.SingleUserTestCase;
/**
*
* @author Robin Collier
*
*/
public class TestAPI extends SingleUserTestCase
{
public void testGetNonexistentNode()
{
try
{
getManager().getNode("" + System.currentTimeMillis());
assertTrue(false);
}
catch (XMPPException e)
{
}
}
}

View file

@ -0,0 +1,598 @@
/**
*
* Copyright 2009 Robin Collier
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.pubsub;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.XMPPError.Type;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.pubsub.listener.ItemDeleteListener;
import org.jivesoftware.smackx.pubsub.listener.ItemEventListener;
import org.jivesoftware.smackx.pubsub.listener.NodeConfigListener;
/**
*
* @author Robin Collier
*
*/
public class TestEvents extends SmackTestCase
{
public TestEvents(String str)
{
super(str);
}
@Override
protected int getMaxConnections()
{
return 2;
}
private String getService()
{
return "pubsub." + getServiceName();
}
public void testCreateAndGetNode() throws Exception
{
String nodeId = "MyTestNode";
PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService());
LeafNode creatorNode = null;
try
{
creatorNode = (LeafNode)creatorMgr.getNode(nodeId);
}
catch (XMPPException e)
{
if (e.getXMPPError().getType() == Type.CANCEL && e.getXMPPError().getCondition().equals("item-not-found"))
creatorNode = creatorMgr.createNode(nodeId);
else
throw e;
}
PubSubManager subMgr = new PubSubManager(getConnection(1), getService());
LeafNode subNode = (LeafNode)subMgr.getNode(nodeId);
assertNotNull(subNode);
}
public void testConfigureAndNotify() throws Exception
{
// Setup event source
String nodeId = "TestNode" + System.currentTimeMillis();
PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService());
LeafNode creatorNode = getPubnode(creatorMgr, nodeId, false, true);
BlockingQueue<NodeConfigCoordinator> queue = new ArrayBlockingQueue<NodeConfigCoordinator>(3);
// Setup event receiver
PubSubManager subMgr = new PubSubManager(getConnection(1), getService());
LeafNode subNode = (LeafNode)subMgr.getNode(nodeId);
NodeConfigListener sub1Handler = new NodeConfigCoordinator(queue, "sub1");
subNode.subscribe(getConnection(1).getUser());
subNode.addConfigurationListener(sub1Handler);
ConfigureForm currentConfig = creatorNode.getNodeConfiguration();
ConfigureForm form = new ConfigureForm(currentConfig.createAnswerForm());
form.setPersistentItems(true);
form.setDeliverPayloads(false);
form.setNotifyConfig(true);
creatorNode.sendConfigurationForm(form);
ConfigurationEvent event = queue.poll(5, TimeUnit.SECONDS).event;
assertEquals(nodeId, event.getNode());
assertNull(event.getConfiguration());
currentConfig = creatorNode.getNodeConfiguration();
form = new ConfigureForm(currentConfig.createAnswerForm());
form.setDeliverPayloads(true);
creatorNode.sendConfigurationForm(form);
event = queue.poll(5, TimeUnit.SECONDS).event;
assertEquals(nodeId, event.getNode());
assertNotNull(event.getConfiguration());
assertTrue(event.getConfiguration().isPersistItems());
assertTrue(event.getConfiguration().isDeliverPayloads());
}
public void testSendAndReceiveNoPayload() throws Exception
{
// Setup event source
String nodeId = "TestNode" + System.currentTimeMillis();
PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService());
LeafNode creatorNode = getPubnode(creatorMgr, nodeId, true, false);
BlockingQueue<ItemEventCoordinator<Item>> queue = new ArrayBlockingQueue<ItemEventCoordinator<Item>>(3);
// Setup event receiver
PubSubManager subMgr = new PubSubManager(getConnection(1), getService());
LeafNode subNode = (LeafNode)subMgr.getNode(nodeId);
ItemEventCoordinator<Item> sub1Handler = new ItemEventCoordinator<Item>(queue, "sub1");
subNode.addItemEventListener(sub1Handler);
Subscription sub1 = subNode.subscribe(getConnection(1).getUser());
// Send event
String itemId = String.valueOf(System.currentTimeMillis());
creatorNode.send(new Item(itemId));
ItemEventCoordinator<Item> coord = queue.poll(5, TimeUnit.SECONDS);
assertEquals(1, coord.events.getItems().size());
assertEquals(itemId, coord.events.getItems().iterator().next().getId());
}
public void testPublishAndReceiveNoPayload() throws Exception
{
// Setup event source
String nodeId = "TestNode" + System.currentTimeMillis();
PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService());
LeafNode creatorNode = getPubnode(creatorMgr, nodeId, true, false);
BlockingQueue<ItemEventCoordinator<Item>> queue = new ArrayBlockingQueue<ItemEventCoordinator<Item>>(3);
// Setup event receiver
PubSubManager subMgr = new PubSubManager(getConnection(1), getService());
LeafNode subNode = (LeafNode)subMgr.getNode(nodeId);
ItemEventCoordinator<Item> sub1Handler = new ItemEventCoordinator<Item>(queue, "sub1");
subNode.addItemEventListener(sub1Handler);
Subscription sub1 = subNode.subscribe(getConnection(1).getUser());
// Send event
String itemId = String.valueOf(System.currentTimeMillis());
creatorNode.publish(new Item(itemId));
ItemEventCoordinator<Item> coord = queue.poll(5, TimeUnit.SECONDS);
assertEquals(1, coord.events.getItems().size());
assertEquals(itemId, coord.events.getItems().get(0).getId());
}
public void testSendAndReceiveSimplePayload() throws Exception
{
// Setup event source
String nodeId = "TestNode" + System.currentTimeMillis();
PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService());
LeafNode creatorNode = getPubnode(creatorMgr, nodeId, true, true);
BlockingQueue<ItemEventCoordinator<PayloadItem<SimplePayload>>> queue = new ArrayBlockingQueue<ItemEventCoordinator<PayloadItem<SimplePayload>>>(3);
// Setup event receiver
PubSubManager subMgr = new PubSubManager(getConnection(1), getService());
LeafNode subNode = (LeafNode)subMgr.getNode(nodeId);
ItemEventCoordinator<PayloadItem<SimplePayload>> sub1Handler = new ItemEventCoordinator<PayloadItem<SimplePayload>>(queue, "sub1");
subNode.addItemEventListener(sub1Handler);
Subscription sub1 = subNode.subscribe(getConnection(1).getUser());
// Send event
String itemId = String.valueOf(System.currentTimeMillis());
String payloadString = "<book xmlns=\"pubsub:test:book\"><author>Sir Arthur Conan Doyle</author></book>";
creatorNode.send(new PayloadItem<SimplePayload>(itemId, new SimplePayload("book", "pubsub:test:book", payloadString)));
ItemEventCoordinator<PayloadItem<SimplePayload>> coord = queue.poll(5, TimeUnit.SECONDS);
assertEquals(1, coord.events.getItems().size());
PayloadItem<SimplePayload> item = coord.events.getItems().get(0);
assertEquals(itemId, item.getId());
assertTrue(item.getPayload() instanceof SimplePayload);
assertEquals(payloadString, item.getPayload().toXML());
assertEquals("book", item.getPayload().getElementName());
}
/*
* For this test, the following extension needs to be added to the meta-inf/smack.providers file
*
* <extensionProvider>
* <elementName>car</elementName>
* <namespace>pubsub:test:vehicle</namespace>
* <className>org.jivesoftware.smackx.pubsub.CarExtensionProvider</className>
* </extensionProvider>
*/
/*
public void testSendAndReceiveCarPayload() throws Exception
{
// Setup event source
String nodeId = "TestNode" + System.currentTimeMillis();
PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService());
LeafNode creatorNode = getPubnode(creatorMgr, nodeId, true, true);
BlockingQueue<ItemEventCoordinator<PayloadItem<CarExtension>>> queue = new ArrayBlockingQueue<ItemEventCoordinator<PayloadItem<CarExtension>>>(3);
// Setup event receiver
PubSubManager subMgr = new PubSubManager(getConnection(1), getService());
Node subNode = subMgr.getNode(nodeId);
ItemEventCoordinator<PayloadItem<CarExtension>> sub1Handler = new ItemEventCoordinator<PayloadItem<CarExtension>>(queue, "sub1");
subNode.addItemEventListener(sub1Handler);
Subscription sub1 = subNode.subscribe(getConnection(1).getUser());
// Send event
String itemId = String.valueOf(System.currentTimeMillis());
String payloadString = "<car xmlns='pubsub:test:vehicle'><paint color='green'/><tires num='4'/></car>";
creatorNode.send(new PayloadItem(itemId, new SimplePayload("car", "pubsub:test:vehicle", payloadString)));
ItemEventCoordinator<PayloadItem<CarExtension>> coord = queue.take();
assertEquals(1, coord.events.getItems().size());
PayloadItem item = coord.events.getItems().get(0);
assertEquals(itemId, item.getId());
assertTrue(item.getPayload() instanceof CarExtension);
CarExtension car = (CarExtension)item.getPayload();
assertEquals("green", car.getColor());
assertEquals(4, car.getNumTires());
}
*/
public void testSendAndReceiveMultipleSubs() throws Exception
{
// Setup event source
String nodeId = "TestNode" + System.currentTimeMillis();
PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService());
LeafNode creatorNode = getPubnode(creatorMgr, nodeId, true, false);
BlockingQueue<ItemEventCoordinator<Item>> queue = new ArrayBlockingQueue<ItemEventCoordinator<Item>>(3);
// Setup event receiver
PubSubManager subMgr = new PubSubManager(getConnection(1), getService());
LeafNode subNode = (LeafNode)subMgr.getNode(nodeId);
ItemEventCoordinator<Item> sub1Handler = new ItemEventCoordinator<Item>(queue, "sub1");
subNode.addItemEventListener(sub1Handler);
Subscription sub1 = subNode.subscribe(getConnection(1).getUser());
ItemEventCoordinator<Item> sub2Handler = new ItemEventCoordinator<Item>(queue, "sub2");
subNode.addItemEventListener(sub2Handler);
Subscription sub2 = subNode.subscribe(getConnection(1).getUser());
// Send event
String itemId = String.valueOf(System.currentTimeMillis());
creatorNode.send(new Item(itemId));
for(int i=0; i<2; i++)
{
ItemEventCoordinator<Item> coord = queue.poll(10, TimeUnit.SECONDS);
if (coord == null)
fail();
assertEquals(1, coord.events.getItems().size());
assertEquals(itemId, coord.events.getItems().iterator().next().getId());
if (coord.id.equals("sub1") || coord.id.equals("sub2"))
{
assertEquals(2, coord.events.getSubscriptions().size());
}
}
}
public void testSendAndReceiveMultipleItems() throws Exception
{
// Setup event source
String nodeId = "TestNode" + System.currentTimeMillis();
PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService());
LeafNode creatorNode = getPubnode(creatorMgr, nodeId, true, true);
BlockingQueue<ItemEventCoordinator<PayloadItem<SimplePayload>>> queue = new ArrayBlockingQueue<ItemEventCoordinator<PayloadItem<SimplePayload>>>(3);
ItemEventCoordinator<PayloadItem<SimplePayload>> creatorHandler = new ItemEventCoordinator<PayloadItem<SimplePayload>>(queue, "creator");
creatorNode.addItemEventListener(creatorHandler);
creatorNode.subscribe(getConnection(0).getUser());
// Setup event receiver
PubSubManager subMgr = new PubSubManager(getConnection(1), getService());
LeafNode subNode = (LeafNode)subMgr.getNode(nodeId);
ItemEventCoordinator<PayloadItem<SimplePayload>> sub1Handler = new ItemEventCoordinator<PayloadItem<SimplePayload>>(queue, "sub1");
subNode.addItemEventListener(sub1Handler);
Subscription sub1 = subNode.subscribe(getConnection(1).getUser());
ItemEventCoordinator<PayloadItem<SimplePayload>> sub2Handler = new ItemEventCoordinator<PayloadItem<SimplePayload>>(queue, "sub2");
subNode.addItemEventListener(sub2Handler);
Subscription sub2 = subNode.subscribe(getConnection(1).getUser());
assertEquals(Subscription.State.subscribed, sub1.getState());
assertEquals(Subscription.State.subscribed, sub2.getState());
// Send event
String itemId = String.valueOf(System.currentTimeMillis());
Collection<PayloadItem<SimplePayload>> items = new ArrayList<PayloadItem<SimplePayload>>(3);
String ids[] = {"First-" + itemId, "Second-" + itemId, "Third-" + itemId};
items.add(new PayloadItem<SimplePayload>(ids[0], new SimplePayload("a", "pubsub:test", "<a xmlns='pubsub:test' href='1'/>")));
items.add(new PayloadItem<SimplePayload>(ids[1], new SimplePayload("a", "pubsub:test", "<a xmlns='pubsub:test' href='1'/>")));
items.add(new PayloadItem<SimplePayload>(ids[2], new SimplePayload("a", "pubsub:test", "<a xmlns='pubsub:test' href='1'/>")));
creatorNode.send(items);
for(int i=0; i<3; i++)
{
ItemEventCoordinator<PayloadItem<SimplePayload>> coord = queue.poll(5, TimeUnit.SECONDS);
if (coord == creatorHandler)
assertEquals(1, coord.events.getSubscriptions().size());
else
assertEquals(2, coord.events.getSubscriptions().size());
List<PayloadItem<SimplePayload>> itemResults = coord.events.getItems();
assertEquals(3, itemResults.size());
// assertEquals(ids[0], itemResults.get(0).getId());
// assertEquals("<a xmlns='pubsub:test' href='1'/>", itemResults.get(0).getPayload().toXML().replace('\"', '\''));
// assertEquals(ids[1], itemResults.get(1).getId());
// assertEquals("<a xmlns='pubsub:test' href='2'/>", itemResults.get(1).getPayload().toXML().replace('\"', '\''));
// assertEquals(ids[21], itemResults.get(2).getId());
// assertEquals("<a xmlns='pubsub:test' href='3'/>", itemResults.get(3).getPayload().toXML().replace('\"', '\''));
}
}
public void testSendAndReceiveDelayed() throws Exception
{
// Setup event source
String nodeId = "TestNode" + System.currentTimeMillis();
PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService());
LeafNode creatorNode = getPubnode(creatorMgr, nodeId, true, false);
// Send event
String itemId = String.valueOf("DelayId-" + System.currentTimeMillis());
String payloadString = "<book xmlns='pubsub:test:book'><author>Sir Arthur Conan Doyle</author></book>";
creatorNode.send(new PayloadItem<SimplePayload>(itemId, new SimplePayload("book", "pubsub:test:book", payloadString)));
Thread.sleep(1000);
BlockingQueue<ItemEventCoordinator<PayloadItem<SimplePayload>>> queue = new ArrayBlockingQueue<ItemEventCoordinator<PayloadItem<SimplePayload>>>(3);
// Setup event receiver
PubSubManager subMgr = new PubSubManager(getConnection(1), getService());
LeafNode subNode = (LeafNode)subMgr.getNode(nodeId);
ItemEventCoordinator<PayloadItem<SimplePayload>> sub1Handler = new ItemEventCoordinator<PayloadItem<SimplePayload>>(queue, "sub1");
subNode.addItemEventListener(sub1Handler);
Subscription sub1 = subNode.subscribe(getConnection(1).getUser());
ItemEventCoordinator<PayloadItem<SimplePayload>> coord = queue.poll(5, TimeUnit.SECONDS);
assertTrue(coord.events.isDelayed());
assertNotNull(coord.events.getPublishedDate());
}
public void testDeleteItemAndNotify() throws Exception
{
// Setup event source
String nodeId = "TestNode" + System.currentTimeMillis();
PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService());
LeafNode creatorNode = getPubnode(creatorMgr, nodeId, true, false);
BlockingQueue<ItemDeleteCoordinator> queue = new ArrayBlockingQueue<ItemDeleteCoordinator>(3);
// Setup event receiver
PubSubManager subMgr = new PubSubManager(getConnection(1), getService());
LeafNode subNode = (LeafNode)subMgr.getNode(nodeId);
ItemDeleteCoordinator sub1Handler = new ItemDeleteCoordinator(queue, "sub1");
subNode.addItemDeleteListener(sub1Handler);
subNode.subscribe(getConnection(1).getUser());
// Send event
String itemId = String.valueOf(System.currentTimeMillis());
Collection<Item> items = new ArrayList<Item>(3);
String id1 = "First-" + itemId;
String id2 = "Second-" + itemId;
String id3 = "Third-" + itemId;
items.add(new Item(id1));
items.add(new Item(id2));
items.add(new Item(id3));
creatorNode.send(items);
creatorNode.deleteItem(id1);
ItemDeleteCoordinator coord = queue.poll(5, TimeUnit.SECONDS);
assertEquals(1, coord.event.getItemIds().size());
assertEquals(id1, coord.event.getItemIds().get(0));
creatorNode.deleteItem(Arrays.asList(id2, id3));
coord = queue.poll(5, TimeUnit.SECONDS);
assertEquals(2, coord.event.getItemIds().size());
assertTrue(coord.event.getItemIds().contains(id2));
assertTrue(coord.event.getItemIds().contains(id3));
}
public void testPurgeAndNotify() throws Exception
{
// Setup event source
String nodeId = "TestNode" + System.currentTimeMillis();
PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService());
LeafNode creatorNode = getPubnode(creatorMgr, nodeId, true, false);
BlockingQueue<ItemDeleteCoordinator> queue = new ArrayBlockingQueue<ItemDeleteCoordinator>(3);
// Setup event receiver
PubSubManager subMgr = new PubSubManager(getConnection(1), getService());
LeafNode subNode = (LeafNode)subMgr.getNode(nodeId);
ItemDeleteCoordinator sub1Handler = new ItemDeleteCoordinator(queue, "sub1");
subNode.addItemDeleteListener(sub1Handler);
subNode.subscribe(getConnection(1).getUser());
// Send event
String itemId = String.valueOf(System.currentTimeMillis());
Collection<Item> items = new ArrayList<Item>(3);
String id1 = "First-" + itemId;
String id2 = "Second-" + itemId;
String id3 = "Third-" + itemId;
items.add(new Item(id1));
items.add(new Item(id2));
items.add(new Item(id3));
creatorNode.send(items);
creatorNode.deleteAllItems();
ItemDeleteCoordinator coord = queue.poll(5, TimeUnit.SECONDS);
assertNull(nodeId, coord.event);
}
public void testListenerMultipleNodes() throws Exception
{
// Setup event source
String nodeId1 = "Node-1-" + System.currentTimeMillis();
PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService());
String nodeId2 = "Node-2-" + System.currentTimeMillis();
LeafNode creatorNode1 = getPubnode(creatorMgr, nodeId1, true, false);
LeafNode creatorNode2 = getPubnode(creatorMgr, nodeId2, true, false);
BlockingQueue<ItemEventCoordinator<Item>> queue = new ArrayBlockingQueue<ItemEventCoordinator<Item>>(3);
PubSubManager subMgr = new PubSubManager(getConnection(1), getService());
LeafNode subNode1 = (LeafNode)subMgr.getNode(nodeId1);
LeafNode subNode2 = (LeafNode)subMgr.getNode(nodeId2);
subNode1.addItemEventListener(new ItemEventCoordinator<Item>(queue, "sub1"));
subNode2.addItemEventListener(new ItemEventCoordinator<Item>(queue, "sub2"));
subNode1.subscribe(getConnection(1).getUser());
subNode2.subscribe(getConnection(1).getUser());
creatorNode1.send(new Item("item1"));
creatorNode2.send(new Item("item2"));
boolean check1 = false;
boolean check2 = false;
for (int i=0; i<2; i++)
{
ItemEventCoordinator<Item> event = queue.poll(5, TimeUnit.SECONDS);
if (event.id.equals("sub1"))
{
assertEquals(event.events.getNodeId(), nodeId1);
check1 = true;
}
else
{
assertEquals(event.events.getNodeId(), nodeId2);
check2 = true;
}
}
assertTrue(check1);
assertTrue(check2);
}
class ItemEventCoordinator <T extends Item> implements ItemEventListener<T>
{
private BlockingQueue<ItemEventCoordinator<T>> theQueue;
private ItemPublishEvent<T> events;
private String id;
ItemEventCoordinator(BlockingQueue<ItemEventCoordinator<T>> queue, String id)
{
theQueue = queue;
this.id = id;
}
public void handlePublishedItems(ItemPublishEvent<T> items)
{
events = items;
theQueue.add(this);
}
@Override
public String toString()
{
return "ItemEventCoordinator: " + id;
}
}
class NodeConfigCoordinator implements NodeConfigListener
{
private BlockingQueue<NodeConfigCoordinator> theQueue;
private String id;
private ConfigurationEvent event;
NodeConfigCoordinator(BlockingQueue<NodeConfigCoordinator> queue, String id)
{
theQueue = queue;
this.id = id;
}
public void handleNodeConfiguration(ConfigurationEvent config)
{
event = config;
theQueue.add(this);
}
@Override
public String toString()
{
return "NodeConfigCoordinator: " + id;
}
}
class ItemDeleteCoordinator implements ItemDeleteListener
{
private BlockingQueue<ItemDeleteCoordinator> theQueue;
private String id;
private ItemDeleteEvent event;
ItemDeleteCoordinator(BlockingQueue<ItemDeleteCoordinator> queue, String id)
{
theQueue = queue;
this.id = id;
}
public void handleDeletedItems(ItemDeleteEvent delEvent)
{
event = delEvent;
theQueue.add(this);
}
public void handlePurge()
{
event = null;
theQueue.add(this);
}
@Override
public String toString()
{
return "ItemDeleteCoordinator: " + id;
}
}
static private LeafNode getPubnode(PubSubManager manager, String id, boolean persistItems, boolean deliverPayload)
throws XMPPException
{
ConfigureForm form = new ConfigureForm(FormType.submit);
form.setPersistentItems(persistItems);
form.setDeliverPayloads(deliverPayload);
form.setAccessModel(AccessModel.open);
return (LeafNode)manager.createNode(id, form);
}
}

View file

@ -0,0 +1,134 @@
/**
*
* Copyright 2009 Robin Collier
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.pubsub;
import junit.framework.TestCase;
/**
*
* @author Robin Collier
*
*/
public class TestMessageContent extends TestCase
{
String payloadXmlWithNS = "<book xmlns='pubsub:test:book'><author name='Stephen King'/></book>";
public void testItemWithId()
{
Item item = new Item("123");
assertEquals("<item id='123'/>", item.toXML());
assertEquals("item", item.getElementName());
assertNull(item.getNamespace());
}
public void testItemWithNoId()
{
Item item = new Item();
assertEquals("<item/>", item.toXML());
Item itemNull = new Item(null);
assertEquals("<item/>", itemNull.toXML());
}
public void testSimplePayload()
{
SimplePayload payloadNS = new SimplePayload("book", "pubsub:test:book", payloadXmlWithNS);
assertEquals(payloadXmlWithNS, payloadNS.toXML());
String payloadXmlWithNoNS = "<book><author name='Stephen King'/></book>";
SimplePayload payloadNoNS = new SimplePayload("book", null, "<book><author name='Stephen King'/></book>");
assertEquals(payloadXmlWithNoNS, payloadNoNS.toXML());
}
public void testPayloadItemWithId()
{
SimplePayload payload = new SimplePayload("book", "pubsub:test:book", payloadXmlWithNS);
PayloadItem<SimplePayload> item = new PayloadItem<SimplePayload>("123", payload);
String xml = "<item id='123'>" + payloadXmlWithNS + "</item>";
assertEquals(xml, item.toXML());
assertEquals("item", item.getElementName());
}
public void testPayloadItemWithNoId()
{
SimplePayload payload = new SimplePayload("book", "pubsub:test:book", payloadXmlWithNS);
PayloadItem<SimplePayload> item = new PayloadItem<SimplePayload>(null, payload);
String xml = "<item>" + payloadXmlWithNS + "</item>";
assertEquals(xml, item.toXML());
}
public void testPayloadItemWithIdNoPayload()
{
try
{
PayloadItem<SimplePayload> item = new PayloadItem<SimplePayload>("123", null);
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException e)
{
}
}
public void testPayloadItemWithNoIdNoPayload()
{
try
{
PayloadItem<SimplePayload> item = new PayloadItem<SimplePayload>(null, null);
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException e)
{
}
}
public void testRetractItem()
{
RetractItem item = new RetractItem("1234");
assertEquals("<retract id='1234'/>", item.toXML());
assertEquals("retract", item.getElementName());
try
{
new RetractItem(null);
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException e)
{
}
}
public void testGetItemsRequest()
{
GetItemsRequest request = new GetItemsRequest("testId");
assertEquals("<items node='testId'/>", request.toXML());
request = new GetItemsRequest("testId", 5);
assertEquals("<items node='testId' max_items='5'/>", request.toXML());
request = new GetItemsRequest("testId", "qwerty");
assertEquals("<items node='testId' subid='qwerty'/>", request.toXML());
request = new GetItemsRequest("testId", "qwerty", 5);
assertEquals("<items node='testId' subid='qwerty' max_items='5'/>", request.toXML());
}
}

View file

@ -0,0 +1,92 @@
/**
*
* Copyright 2009 Robin Collier
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.pubsub.test;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.pubsub.AccessModel;
import org.jivesoftware.smackx.pubsub.ConfigureForm;
import org.jivesoftware.smackx.pubsub.FormType;
import org.jivesoftware.smackx.pubsub.LeafNode;
import org.jivesoftware.smackx.pubsub.PubSubManager;
/**
*
* @author Robin Collier
*
*/
abstract public class PubSubTestCase extends SmackTestCase
{
private PubSubManager[] manager;
public PubSubTestCase(String arg0)
{
super(arg0);
}
public PubSubTestCase()
{
super("PubSub Test Case");
}
protected LeafNode getRandomPubnode(PubSubManager pubMgr, boolean persistItems, boolean deliverPayload) throws XMPPException
{
ConfigureForm form = new ConfigureForm(FormType.submit);
form.setPersistentItems(persistItems);
form.setDeliverPayloads(deliverPayload);
form.setAccessModel(AccessModel.open);
return (LeafNode)pubMgr.createNode("/test/Pubnode" + System.currentTimeMillis(), form);
}
protected LeafNode getPubnode(PubSubManager pubMgr, boolean persistItems, boolean deliverPayload, String nodeId) throws XMPPException
{
LeafNode node = null;
try
{
node = (LeafNode)pubMgr.getNode(nodeId);
}
catch (XMPPException e)
{
ConfigureForm form = new ConfigureForm(FormType.submit);
form.setPersistentItems(persistItems);
form.setDeliverPayloads(deliverPayload);
form.setAccessModel(AccessModel.open);
node = (LeafNode)pubMgr.createNode(nodeId, form);
}
return node;
}
protected PubSubManager getManager(int idx)
{
if (manager == null)
{
manager = new PubSubManager[getMaxConnections()];
for(int i=0; i<manager.length; i++)
{
manager[i] = new PubSubManager(getConnection(i), getService());
}
}
return manager[idx];
}
protected String getService()
{
return "pubsub." + getServiceName();
}
}

View file

@ -0,0 +1,46 @@
/**
*
* Copyright 2009 Robin Collier
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.pubsub.test;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.pubsub.LeafNode;
import org.jivesoftware.smackx.pubsub.PubSubManager;
/**
*
* @author Robin Collier
*
*/
public class SingleUserTestCase extends PubSubTestCase
{
protected PubSubManager getManager()
{
return getManager(0);
}
protected LeafNode getPubnode(boolean persistItems, boolean deliverPayload) throws XMPPException
{
return getRandomPubnode(getManager(), persistItems, deliverPayload);
}
@Override
protected int getMaxConnections()
{
return 1;
}
}

View file

@ -0,0 +1,32 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import org.jivesoftware.smack.initializer.UrlProviderFileInitializer;
/**
* Loads the default provider file for the Smack extensions on initialization.
*
* @author Robin Collier
*
*/
public class ExtensionsProviderInitializer extends UrlProviderFileInitializer {
@Override
protected String getFilePath() {
return "classpath:org.jivesoftware.smackx/extensions.providers";
}
}

View file

@ -0,0 +1,52 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx;
import java.io.InputStream;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.initializer.SmackInitializer;
import org.jivesoftware.smack.util.FileUtils;
public class ExtensionsStartupClasses implements SmackInitializer {
private static final String EXTENSIONS_XML = "classpath:org.jivesoftware.smackx/extensions.xml";
private List<Exception> exceptions = new LinkedList<Exception>();
// TODO log
@Override
public void initialize() {
InputStream is;
try {
is = FileUtils.getStreamForUrl(EXTENSIONS_XML, null);
SmackConfiguration.processConfigFile(is, exceptions);;
}
catch (Exception e) {
exceptions.add(e);
}
}
@Override
public List<Exception> getExceptions() {
return Collections.unmodifiableList(exceptions);
}
}

View file

@ -0,0 +1,95 @@
/**
*
* Copyright 2003-2006 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.address;
import org.jivesoftware.smackx.address.packet.MultipleAddresses;
import java.util.List;
/**
* MultipleRecipientInfo keeps information about the multiple recipients extension included
* in a received packet. Among the information we can find the list of TO and CC addresses.
*
* @author Gaston Dombiak
*/
public class MultipleRecipientInfo {
MultipleAddresses extension;
MultipleRecipientInfo(MultipleAddresses extension) {
this.extension = extension;
}
/**
* Returns the list of {@link org.jivesoftware.smackx.address.packet.MultipleAddresses.Address}
* that were the primary recipients of the packet.
*
* @return list of primary recipients of the packet.
*/
public List<MultipleAddresses.Address> getTOAddresses() {
return extension.getAddressesOfType(MultipleAddresses.TO);
}
/**
* Returns the list of {@link org.jivesoftware.smackx.address.packet.MultipleAddresses.Address}
* that were the secondary recipients of the packet.
*
* @return list of secondary recipients of the packet.
*/
public List<MultipleAddresses.Address> getCCAddresses() {
return extension.getAddressesOfType(MultipleAddresses.CC);
}
/**
* Returns the JID of a MUC room to which responses should be sent or <tt>null</tt> if
* no specific address was provided. When no specific address was provided then the reply
* can be sent to any or all recipients. Otherwise, the user should join the specified room
* and send the reply to the room.
*
* @return the JID of a MUC room to which responses should be sent or <tt>null</tt> if
* no specific address was provided.
*/
public String getReplyRoom() {
List<MultipleAddresses.Address> replyRoom = extension.getAddressesOfType(MultipleAddresses.REPLY_ROOM);
return replyRoom.isEmpty() ? null : ((MultipleAddresses.Address) replyRoom.get(0)).getJid();
}
/**
* Returns true if the received packet should not be replied. Use
* {@link MultipleRecipientManager#reply(org.jivesoftware.smack.XMPPConnection, org.jivesoftware.smack.packet.Message, org.jivesoftware.smack.packet.Message)}
* to send replies.
*
* @return true if the received packet should not be replied.
*/
public boolean shouldNotReply() {
return !extension.getAddressesOfType(MultipleAddresses.NO_REPLY).isEmpty();
}
/**
* Returns the address to which all replies are requested to be sent or <tt>null</tt> if
* no specific address was provided. When no specific address was provided then the reply
* can be sent to any or all recipients.
*
* @return the address to which all replies are requested to be sent or <tt>null</tt> if
* no specific address was provided.
*/
public MultipleAddresses.Address getReplyAddress() {
List<MultipleAddresses.Address> replyTo = extension.getAddressesOfType(MultipleAddresses.REPLY_TO);
return replyTo.isEmpty() ? null : (MultipleAddresses.Address) replyTo.get(0);
}
}

View file

@ -0,0 +1,357 @@
/**
*
* Copyright 2003-2006 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.address;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.FeatureNotSupportedException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.Cache;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.address.packet.MultipleAddresses;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
import org.jivesoftware.smackx.disco.packet.DiscoverItems;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* A MultipleRecipientManager allows to send packets to multiple recipients by making use of
* <a href="http://www.jabber.org/jeps/jep-0033.html">JEP-33: Extended Stanza Addressing</a>.
* It also allows to send replies to packets that were sent to multiple recipients.
*
* @author Gaston Dombiak
*/
public class MultipleRecipientManager {
/**
* Create a cache to hold the 100 most recently accessed elements for a period of
* 24 hours.
*/
private static Cache<String, String> services = new Cache<String, String>(100, 24 * 60 * 60 * 1000);
/**
* Sends the specified packet to the list of specified recipients using the
* specified connection. If the server has support for JEP-33 then only one
* packet is going to be sent to the server with the multiple recipient instructions.
* However, if JEP-33 is not supported by the server then the client is going to send
* the packet to each recipient.
*
* @param connection the connection to use to send the packet.
* @param packet the packet to send to the list of recipients.
* @param to the list of JIDs to include in the TO list or <tt>null</tt> if no TO
* list exists.
* @param cc the list of JIDs to include in the CC list or <tt>null</tt> if no CC
* list exists.
* @param bcc the list of JIDs to include in the BCC list or <tt>null</tt> if no BCC
* list exists.
* @throws FeatureNotSupportedException if special XEP-33 features where requested, but the
* server does not support them.
* @throws XMPPErrorException if server does not support JEP-33: Extended Stanza Addressing and
* some JEP-33 specific features were requested.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
*/
public static void send(XMPPConnection connection, Packet packet, List<String> to, List<String> cc, List<String> bcc) throws NoResponseException, XMPPErrorException, FeatureNotSupportedException, NotConnectedException
{
send(connection, packet, to, cc, bcc, null, null, false);
}
/**
* Sends the specified packet to the list of specified recipients using the specified
* connection. If the server has support for JEP-33 then only one packet is going to be sent to
* the server with the multiple recipient instructions. However, if JEP-33 is not supported by
* the server then the client is going to send the packet to each recipient.
*
* @param connection the connection to use to send the packet.
* @param packet the packet to send to the list of recipients.
* @param to the list of JIDs to include in the TO list or <tt>null</tt> if no TO list exists.
* @param cc the list of JIDs to include in the CC list or <tt>null</tt> if no CC list exists.
* @param bcc the list of JIDs to include in the BCC list or <tt>null</tt> if no BCC list
* exists.
* @param replyTo address to which all replies are requested to be sent or <tt>null</tt>
* indicating that they can reply to any address.
* @param replyRoom JID of a MUC room to which responses should be sent or <tt>null</tt>
* indicating that they can reply to any address.
* @param noReply true means that receivers should not reply to the message.
* @throws XMPPErrorException if server does not support JEP-33: Extended Stanza Addressing and
* some JEP-33 specific features were requested.
* @throws NoResponseException if there was no response from the server.
* @throws FeatureNotSupportedException if special XEP-33 features where requested, but the
* server does not support them.
* @throws NotConnectedException
*/
public static void send(XMPPConnection connection, Packet packet, List<String> to, List<String> cc, List<String> bcc,
String replyTo, String replyRoom, boolean noReply) throws NoResponseException, XMPPErrorException, FeatureNotSupportedException, NotConnectedException {
String serviceAddress = getMultipleRecipienServiceAddress(connection);
if (serviceAddress != null) {
// Send packet to target users using multiple recipient service provided by the server
sendThroughService(connection, packet, to, cc, bcc, replyTo, replyRoom, noReply,
serviceAddress);
}
else {
// Server does not support JEP-33 so try to send the packet to each recipient
if (noReply || (replyTo != null && replyTo.trim().length() > 0) ||
(replyRoom != null && replyRoom.trim().length() > 0)) {
// Some specified JEP-33 features were requested so throw an exception alerting
// the user that this features are not available
throw new FeatureNotSupportedException("Extended Stanza Addressing");
}
// Send the packet to each individual recipient
sendToIndividualRecipients(connection, packet, to, cc, bcc);
}
}
/**
* Sends a reply to a previously received packet that was sent to multiple recipients. Before
* attempting to send the reply message some checkings are performed. If any of those checkings
* fail then an XMPPException is going to be thrown with the specific error detail.
*
* @param connection the connection to use to send the reply.
* @param original the previously received packet that was sent to multiple recipients.
* @param reply the new message to send as a reply.
* @throws SmackException
* @throws XMPPErrorException
*/
public static void reply(XMPPConnection connection, Message original, Message reply) throws SmackException, XMPPErrorException
{
MultipleRecipientInfo info = getMultipleRecipientInfo(original);
if (info == null) {
throw new SmackException("Original message does not contain multiple recipient info");
}
if (info.shouldNotReply()) {
throw new SmackException("Original message should not be replied");
}
if (info.getReplyRoom() != null) {
throw new SmackException("Reply should be sent through a room");
}
// Any <thread/> element from the initial message MUST be copied into the reply.
if (original.getThread() != null) {
reply.setThread(original.getThread());
}
MultipleAddresses.Address replyAddress = info.getReplyAddress();
if (replyAddress != null && replyAddress.getJid() != null) {
// Send reply to the reply_to address
reply.setTo(replyAddress.getJid());
connection.sendPacket(reply);
}
else {
// Send reply to multiple recipients
List<String> to = new ArrayList<String>();
List<String> cc = new ArrayList<String>();
for (Iterator<MultipleAddresses.Address> it = info.getTOAddresses().iterator(); it.hasNext();) {
String jid = it.next().getJid();
to.add(jid);
}
for (Iterator<MultipleAddresses.Address> it = info.getCCAddresses().iterator(); it.hasNext();) {
String jid = it.next().getJid();
cc.add(jid);
}
// Add original sender as a 'to' address (if not already present)
if (!to.contains(original.getFrom()) && !cc.contains(original.getFrom())) {
to.add(original.getFrom());
}
// Remove the sender from the TO/CC list (try with bare JID too)
String from = connection.getUser();
if (!to.remove(from) && !cc.remove(from)) {
String bareJID = StringUtils.parseBareAddress(from);
to.remove(bareJID);
cc.remove(bareJID);
}
String serviceAddress = getMultipleRecipienServiceAddress(connection);
if (serviceAddress != null) {
// Send packet to target users using multiple recipient service provided by the server
sendThroughService(connection, reply, to, cc, null, null, null, false,
serviceAddress);
}
else {
// Server does not support JEP-33 so try to send the packet to each recipient
sendToIndividualRecipients(connection, reply, to, cc, null);
}
}
}
/**
* Returns the {@link MultipleRecipientInfo} contained in the specified packet or
* <tt>null</tt> if none was found. Only packets sent to multiple recipients will
* contain such information.
*
* @param packet the packet to check.
* @return the MultipleRecipientInfo contained in the specified packet or <tt>null</tt>
* if none was found.
*/
public static MultipleRecipientInfo getMultipleRecipientInfo(Packet packet) {
MultipleAddresses extension = (MultipleAddresses) packet
.getExtension("addresses", "http://jabber.org/protocol/address");
return extension == null ? null : new MultipleRecipientInfo(extension);
}
private static void sendToIndividualRecipients(XMPPConnection connection, Packet packet,
List<String> to, List<String> cc, List<String> bcc) throws NotConnectedException {
if (to != null) {
for (Iterator<String> it = to.iterator(); it.hasNext();) {
String jid = it.next();
packet.setTo(jid);
connection.sendPacket(new PacketCopy(packet.toXML()));
}
}
if (cc != null) {
for (Iterator<String> it = cc.iterator(); it.hasNext();) {
String jid = it.next();
packet.setTo(jid);
connection.sendPacket(new PacketCopy(packet.toXML()));
}
}
if (bcc != null) {
for (Iterator<String> it = bcc.iterator(); it.hasNext();) {
String jid = it.next();
packet.setTo(jid);
connection.sendPacket(new PacketCopy(packet.toXML()));
}
}
}
private static void sendThroughService(XMPPConnection connection, Packet packet, List<String> to,
List<String> cc, List<String> bcc, String replyTo, String replyRoom, boolean noReply,
String serviceAddress) throws NotConnectedException {
// Create multiple recipient extension
MultipleAddresses multipleAddresses = new MultipleAddresses();
if (to != null) {
for (Iterator<String> it = to.iterator(); it.hasNext();) {
String jid = it.next();
multipleAddresses.addAddress(MultipleAddresses.TO, jid, null, null, false, null);
}
}
if (cc != null) {
for (Iterator<String> it = cc.iterator(); it.hasNext();) {
String jid = it.next();
multipleAddresses.addAddress(MultipleAddresses.CC, jid, null, null, false, null);
}
}
if (bcc != null) {
for (Iterator<String> it = bcc.iterator(); it.hasNext();) {
String jid = it.next();
multipleAddresses.addAddress(MultipleAddresses.BCC, jid, null, null, false, null);
}
}
if (noReply) {
multipleAddresses.setNoReply();
}
else {
if (replyTo != null && replyTo.trim().length() > 0) {
multipleAddresses
.addAddress(MultipleAddresses.REPLY_TO, replyTo, null, null, false, null);
}
if (replyRoom != null && replyRoom.trim().length() > 0) {
multipleAddresses.addAddress(MultipleAddresses.REPLY_ROOM, replyRoom, null, null,
false, null);
}
}
// Set the multiple recipient service address as the target address
packet.setTo(serviceAddress);
// Add extension to packet
packet.addExtension(multipleAddresses);
// Send the packet
connection.sendPacket(packet);
}
/**
* Returns the address of the multiple recipients service. To obtain such address service
* discovery is going to be used on the connected server and if none was found then another
* attempt will be tried on the server items. The discovered information is going to be
* cached for 24 hours.
*
* @param connection the connection to use for disco. The connected server is going to be
* queried.
* @return the address of the multiple recipients service or <tt>null</tt> if none was found.
* @throws NoResponseException if there was no response from the server.
* @throws XMPPErrorException
* @throws NotConnectedException
*/
private static String getMultipleRecipienServiceAddress(XMPPConnection connection) throws NoResponseException, XMPPErrorException, NotConnectedException {
String serviceName = connection.getServiceName();
String serviceAddress = (String) services.get(serviceName);
if (serviceAddress == null) {
synchronized (services) {
serviceAddress = (String) services.get(serviceName);
if (serviceAddress == null) {
// Send the disco packet to the server itself
DiscoverInfo info = ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(
serviceName);
// Check if the server supports JEP-33
if (info.containsFeature("http://jabber.org/protocol/address")) {
serviceAddress = serviceName;
}
else {
// Get the disco items and send the disco packet to each server item
DiscoverItems items = ServiceDiscoveryManager.getInstanceFor(connection).discoverItems(
serviceName);
for (DiscoverItems.Item item : items.getItems()) {
info = ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(
item.getEntityID(), item.getNode());
if (info.containsFeature("http://jabber.org/protocol/address")) {
serviceAddress = serviceName;
break;
}
}
}
// Cache the discovered information
services.put(serviceName, serviceAddress == null ? "" : serviceAddress);
}
}
}
return "".equals(serviceAddress) ? null : serviceAddress;
}
/**
* Packet that holds the XML stanza to send. This class is useful when the same packet
* is needed to be sent to different recipients. Since using the same packet is not possible
* (i.e. cannot change the TO address of a queues packet to be sent) then this class was
* created to keep the XML stanza to send.
*/
private static class PacketCopy extends Packet {
private CharSequence text;
/**
* Create a copy of a packet with the text to send. The passed text must be a valid text to
* send to the server, no validation will be done on the passed text.
*
* @param text the whole text of the packet to send
*/
public PacketCopy(CharSequence text) {
this.text = text;
}
@Override
public CharSequence toXML() {
return text;
}
}
}

View file

@ -0,0 +1,202 @@
/**
*
* Copyright 2003-2006 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.address.packet;
import org.jivesoftware.smack.packet.PacketExtension;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Packet extension that contains the list of addresses that a packet should be sent or was sent.
*
* @author Gaston Dombiak
*/
public class MultipleAddresses implements PacketExtension {
public static final String BCC = "bcc";
public static final String CC = "cc";
public static final String NO_REPLY = "noreply";
public static final String REPLY_ROOM = "replyroom";
public static final String REPLY_TO = "replyto";
public static final String TO = "to";
private List<Address> addresses = new ArrayList<Address>();
/**
* Adds a new address to which the packet is going to be sent or was sent.
*
* @param type on of the static type (BCC, CC, NO_REPLY, REPLY_ROOM, etc.)
* @param jid the JID address of the recipient.
* @param node used to specify a sub-addressable unit at a particular JID, corresponding to
* a Service Discovery node.
* @param desc used to specify human-readable information for this address.
* @param delivered true when the packet was already delivered to this address.
* @param uri used to specify an external system address, such as a sip:, sips:, or im: URI.
*/
public void addAddress(String type, String jid, String node, String desc, boolean delivered,
String uri) {
// Create a new address with the specificed configuration
Address address = new Address(type);
address.setJid(jid);
address.setNode(node);
address.setDescription(desc);
address.setDelivered(delivered);
address.setUri(uri);
// Add the new address to the list of multiple recipients
addresses.add(address);
}
/**
* Indicate that the packet being sent should not be replied.
*/
public void setNoReply() {
// Create a new address with the specificed configuration
Address address = new Address(NO_REPLY);
// Add the new address to the list of multiple recipients
addresses.add(address);
}
/**
* Returns the list of addresses that matches the specified type. Examples of address
* type are: TO, CC, BCC, etc..
*
* @param type Examples of address type are: TO, CC, BCC, etc.
* @return the list of addresses that matches the specified type.
*/
public List<Address> getAddressesOfType(String type) {
List<Address> answer = new ArrayList<Address>(addresses.size());
for (Iterator<Address> it = addresses.iterator(); it.hasNext();) {
Address address = (Address) it.next();
if (address.getType().equals(type)) {
answer.add(address);
}
}
return answer;
}
public String getElementName() {
return "addresses";
}
public String getNamespace() {
return "http://jabber.org/protocol/address";
}
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<").append(getElementName());
buf.append(" xmlns=\"").append(getNamespace()).append("\">");
// Loop through all the addresses and append them to the string buffer
for (Iterator<Address> i = addresses.iterator(); i.hasNext();) {
Address address = (Address) i.next();
buf.append(address.toXML());
}
buf.append("</").append(getElementName()).append(">");
return buf.toString();
}
public static class Address {
private String type;
private String jid;
private String node;
private String description;
private boolean delivered;
private String uri;
private Address(String type) {
this.type = type;
}
public String getType() {
return type;
}
public String getJid() {
return jid;
}
private void setJid(String jid) {
this.jid = jid;
}
public String getNode() {
return node;
}
private void setNode(String node) {
this.node = node;
}
public String getDescription() {
return description;
}
private void setDescription(String description) {
this.description = description;
}
public boolean isDelivered() {
return delivered;
}
private void setDelivered(boolean delivered) {
this.delivered = delivered;
}
public String getUri() {
return uri;
}
private void setUri(String uri) {
this.uri = uri;
}
private String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<address type=\"");
// Append the address type (e.g. TO/CC/BCC)
buf.append(type).append("\"");
if (jid != null) {
buf.append(" jid=\"");
buf.append(jid).append("\"");
}
if (node != null) {
buf.append(" node=\"");
buf.append(node).append("\"");
}
if (description != null && description.trim().length() > 0) {
buf.append(" desc=\"");
buf.append(description).append("\"");
}
if (delivered) {
buf.append(" delivered=\"true\"");
}
if (uri != null) {
buf.append(" uri=\"");
buf.append(uri).append("\"");
}
buf.append("/>");
return buf.toString();
}
}
}

View file

@ -0,0 +1,64 @@
/**
*
* Copyright 2003-2006 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.address.provider;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.jivesoftware.smackx.address.packet.MultipleAddresses;
import org.xmlpull.v1.XmlPullParser;
/**
* The MultipleAddressesProvider parses {@link MultipleAddresses} packets.
*
* @author Gaston Dombiak
*/
public class MultipleAddressesProvider implements PacketExtensionProvider {
/**
* Creates a new MultipleAddressesProvider.
* ProviderManager requires that every PacketExtensionProvider has a public, no-argument
* constructor.
*/
public MultipleAddressesProvider() {
}
public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
boolean done = false;
MultipleAddresses multipleAddresses = new MultipleAddresses();
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("address")) {
String type = parser.getAttributeValue("", "type");
String jid = parser.getAttributeValue("", "jid");
String node = parser.getAttributeValue("", "node");
String desc = parser.getAttributeValue("", "desc");
boolean delivered = "true".equals(parser.getAttributeValue("", "delivered"));
String uri = parser.getAttributeValue("", "uri");
// Add the parsed address
multipleAddresses.addAddress(type, jid, node, desc, delivered, uri);
}
} else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals(multipleAddresses.getElementName())) {
done = true;
}
}
}
return multipleAddresses;
}
}

View file

@ -0,0 +1,90 @@
/**
*
* Copyright 2014 Vyacheslav Blinov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.amp;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smackx.amp.packet.AMPExtension;
public class AMPDeliverCondition implements AMPExtension.Condition {
public static final String NAME = "deliver";
/**
* Check if server supports deliver condition
* @param connection Smack connection instance
* @return true if deliver condition is supported.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
*/
public static boolean isSupported(XMPPConnection connection) throws NoResponseException, XMPPErrorException, NotConnectedException {
return AMPManager.isConditionSupported(connection, NAME);
}
private final Value value;
/**
* Create new amp deliver condition with value setted to one of defined by XEP-0079.
* See http://xmpp.org/extensions/xep-0079.html#conditions-def-deliver
* @param value AMPDeliveryCondition.Value instance that will be used as value parameter. Can't be null.
*/
public AMPDeliverCondition(Value value) {
if (value == null)
throw new NullPointerException("Can't create AMPDeliverCondition with null value");
this.value = value;
}
@Override
public String getName() {
return NAME;
}
@Override
public String getValue() {
return value.toString();
}
/**
* Value for amp deliver condition as defined by XEP-0079.
* See http://xmpp.org/extensions/xep-0079.html#conditions-def-deliver
*/
public static enum Value {
/**
* The message would be immediately delivered to the intended recipient or routed to the next hop.
*/
direct,
/**
* The message would be forwarded to another XMPP address or account.
*/
forward,
/**
* The message would be sent through a gateway to an address or account on a non-XMPP system.
*/
gateway,
/**
* The message would not be delivered at all (e.g., because the intended recipient is offline and message storage is not enabled).
*/
none,
/**
* The message would be stored offline for later delivery to the intended recipient.
*/
stored
}
}

View file

@ -0,0 +1,79 @@
/**
*
* Copyright 2014 Vyacheslav Blinov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.amp;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.util.XmppDateTime;
import org.jivesoftware.smackx.amp.packet.AMPExtension;
import java.util.Date;
public class AMPExpireAtCondition implements AMPExtension.Condition {
public static final String NAME = "expire-at";
/**
* Check if server supports expire-at condition
* @param connection Smack connection instance
* @return true if expire-at condition is supported.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
*/
public static boolean isSupported(XMPPConnection connection) throws NoResponseException, XMPPErrorException, NotConnectedException {
return AMPManager.isConditionSupported(connection, NAME);
}
private final String value;
/**
* Create new expire-at amp condition with value setted as XEP-0082 formatted date.
* @param utcDateTime Date instance of time
* that will be used as value parameter after formatting to XEP-0082 format. Can't be null.
*/
public AMPExpireAtCondition(Date utcDateTime) {
if (utcDateTime == null)
throw new NullPointerException("Can't create AMPExpireAtCondition with null value");
this.value = XmppDateTime.formatXEP0082Date(utcDateTime);
}
/**
* Create new expire-at amp condition with defined value.
* @param utcDateTime UTC time string that will be used as value parameter.
* Should be formatted as XEP-0082 Date format. Can't be null.
*/
public AMPExpireAtCondition(String utcDateTime) {
if (utcDateTime == null)
throw new NullPointerException("Can't create AMPExpireAtCondition with null value");
this.value = utcDateTime;
}
@Override
public String getName() {
return NAME;
}
@Override
public String getValue() {
return value;
}
}

View file

@ -0,0 +1,122 @@
/**
*
* Copyright 2014 Vyacheslav Blinov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.amp;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smackx.amp.packet.AMPExtension;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
/**
* Manages AMP stanzas within messages. A AMPManager provides a high level access to
* get and set AMP rules to messages.
*
* See http://xmpp.org/extensions/xep-0079.html for AMP extension details
*
* @author Vyacheslav Blinov
*/
public class AMPManager {
// Enable the AMP support on every established connection
// The ServiceDiscoveryManager class should have been already initialized
static {
XMPPConnection.addConnectionCreationListener(new ConnectionCreationListener() {
public void connectionCreated(XMPPConnection connection) {
AMPManager.setServiceEnabled(connection, true);
}
});
}
/**
* Enables or disables the AMP support on a given connection.<p>
*
* Before starting to send AMP messages to a user, check that the user can handle XHTML
* messages. Enable the AMP support to indicate that this client handles XHTML messages.
*
* @param connection the connection where the service will be enabled or disabled
* @param enabled indicates if the service will be enabled or disabled
*/
public synchronized static void setServiceEnabled(XMPPConnection connection, boolean enabled) {
if (isServiceEnabled(connection) == enabled)
return;
if (enabled) {
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(AMPExtension.NAMESPACE);
}
else {
ServiceDiscoveryManager.getInstanceFor(connection).removeFeature(AMPExtension.NAMESPACE);
}
}
/**
* Returns true if the AMP support is enabled for the given connection.
*
* @param connection the connection to look for AMP support
* @return a boolean indicating if the AMP support is enabled for the given connection
*/
public static boolean isServiceEnabled(XMPPConnection connection) {
connection.getServiceName();
return ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(AMPExtension.NAMESPACE);
}
/**
* Check if server supports specified action
* @param connection active xmpp connection
* @param action action to check
* @return true if this action is supported.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
*/
public static boolean isActionSupported(XMPPConnection connection, AMPExtension.Action action) throws NoResponseException, XMPPErrorException, NotConnectedException {
String featureName = AMPExtension.NAMESPACE + "?action=" + action.toString();
return isFeatureSupportedByServer(connection, featureName, AMPExtension.NAMESPACE);
}
/**
* Check if server supports specified condition
* @param connection active xmpp connection
* @param conditionName name of condition to check
* @return true if this condition is supported.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
* @see AMPDeliverCondition
* @see AMPExpireAtCondition
* @see AMPMatchResourceCondition
*/
public static boolean isConditionSupported(XMPPConnection connection, String conditionName) throws NoResponseException, XMPPErrorException, NotConnectedException {
String featureName = AMPExtension.NAMESPACE + "?condition=" + conditionName;
return isFeatureSupportedByServer(connection, featureName, AMPExtension.NAMESPACE);
}
private static boolean isFeatureSupportedByServer(XMPPConnection connection, String featureName, String node) throws NoResponseException, XMPPErrorException, NotConnectedException {
ServiceDiscoveryManager discoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
DiscoverInfo info = discoveryManager.discoverInfo(connection.getServiceName(), node);
for (DiscoverInfo.Feature feature : info.getFeatures()){
if (featureName.equals(feature.getVar())) {
return true;
}
}
return false;
}
}

View file

@ -0,0 +1,85 @@
/**
*
* Copyright 2014 Vyacheslav Blinov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.amp;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smackx.amp.packet.AMPExtension;
public class AMPMatchResourceCondition implements AMPExtension.Condition {
public static final String NAME = "match-resource";
/**
* Check if server supports match-resource condition
* @param connection Smack connection instance
* @return true if match-resource condition is supported.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
*/
public static boolean isSupported(XMPPConnection connection) throws NoResponseException, XMPPErrorException, NotConnectedException {
return AMPManager.isConditionSupported(connection, NAME);
}
private final Value value;
/**
* Create new amp match-resource condition with value setted to one of defined by XEP-0079.
* See http://xmpp.org/extensions/xep-0079.html#conditions-def-match
* @param value AMPDeliveryCondition.Value instance that will be used as value parameter. Can't be null.
*/
public AMPMatchResourceCondition(Value value) {
if (value == null)
throw new NullPointerException("Can't create AMPMatchResourceCondition with null value");
this.value = value;
}
@Override
public String getName() {
return NAME;
}
@Override
public String getValue() {
return value.toString();
}
/**
* match-resource amp condition value as defined by XEP-0079
* See http://xmpp.org/extensions/xep-0079.html#conditions-def-match
*/
public static enum Value {
/**
* Destination resource matches any value, effectively ignoring the intended resource.
* Example: "home/laptop" matches "home", "home/desktop" or "work/desktop"
*/
any,
/**
* Destination resource exactly matches the intended resource.
* Example: "home/laptop" only matches "home/laptop" and not "home/desktop" or "work/desktop"
*/
exact,
/**
* Destination resource matches any value except for the intended resource.
* Example: "home/laptop" matches "work/desktop", "home" or "home/desktop", but not "home/laptop"
*/
other
}
}

View file

@ -0,0 +1,276 @@
/**
*
* Copyright 2014 Vyacheslav Blinov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.amp.packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.amp.AMPDeliverCondition;
import org.jivesoftware.smackx.amp.AMPExpireAtCondition;
import org.jivesoftware.smackx.amp.AMPMatchResourceCondition;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
public class AMPExtension implements PacketExtension {
public static final String NAMESPACE = "http://jabber.org/protocol/amp";
public static final String ELEMENT = "amp";
private CopyOnWriteArrayList<Rule> rules = new CopyOnWriteArrayList<Rule>();
private boolean perHop = false;
private final String from;
private final String to;
private final Status status;
/**
* Create a new AMPExtension instance with defined from, to and status attributes. Used to create incoming packets.
* @param from jid that triggered this amp callback.
* @param to receiver of this amp receipt.
* @param status status of this amp receipt.
*/
public AMPExtension(String from, String to, Status status) {
this.from = from;
this.to = to;
this.status = status;
}
/**
* Create a new amp request extension to be used with outgoing message.
*/
public AMPExtension() {
this.from = null;
this.to = null;
this.status = null;
}
/**
* @return jid that triggered this amp callback.
*/
public String getFrom() {
return from;
}
/**
* @return receiver of this amp receipt.
*/
public String getTo() {
return to;
}
/**
* Status of this amp notification
* @return Status for this amp
*/
public Status getStatus() {
return status;
}
/**
* Returns a Collection of the rules in the packet.
*
* @return a Collection of the rules in the packet.
*/
public Collection<Rule> getRules() {
return Collections.unmodifiableList(new ArrayList<Rule>(rules));
}
/**
* Adds a rule to the amp element. Amp can have any number of rules.
*
* @param rule the rule to add.
*/
public void addRule(Rule rule) {
rules.add(rule);
}
/**
* Returns a count of the rules in the AMP packet.
*
* @return the number of rules in the AMP packet.
*/
public int getRulesCount() {
return rules.size();
}
/**
* Sets this amp ruleset to be "per-hop".
*
* @param enabled true if "per-hop" should be enabled
*/
public synchronized void setPerHop(boolean enabled) {
perHop = enabled;
}
/**
* Returns true is this ruleset is "per-hop".
*
* @return true is this ruleset is "per-hop".
*/
public synchronized boolean isPerHop() {
return perHop;
}
/**
* Returns the XML element name of the extension sub-packet root element.
* Always returns "amp"
*
* @return the XML element name of the packet extension.
*/
@Override
public String getElementName() {
return ELEMENT;
}
/**
* Returns the XML namespace of the extension sub-packet root element.
* According the specification the namespace is always "http://jabber.org/protocol/xhtml-im"
*
* @return the XML namespace of the packet extension.
*/
@Override
public String getNamespace() {
return NAMESPACE;
}
/**
* Returns the XML representation of a XHTML extension according the specification.
**/
@Override
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append("\"");
if (status != null) {
buf.append(" status=\"").append(status.toString()).append("\"");
}
if (to != null) {
buf.append(" to=\"").append(to).append("\"");
}
if (from != null) {
buf.append(" from=\"").append(from).append("\"");
}
if (perHop) {
buf.append(" per-hop=\"true\"");
}
buf.append(">");
// Loop through all the rules and append them to the string buffer
for (Rule rule : getRules()) {
buf.append(rule.toXML());
}
buf.append("</").append(getElementName()).append(">");
return buf.toString();
}
/**
* XEP-0079 Rule element. Defines AMP Rule parameters. Can be added to AMPExtension.
*/
public static class Rule {
public static final String ELEMENT = "rule";
private final Action action;
private final Condition condition;
public Action getAction() {
return action;
}
public Condition getCondition() {
return condition;
}
/**
* Create a new amp rule with specified action and condition. Value will be taken from condition argument
* @param action action for this rule
* @param condition condition for this rule
*/
public Rule(Action action, Condition condition) {
if (action == null)
throw new NullPointerException("Can't create Rule with null action");
if (condition == null)
throw new NullPointerException("Can't create Rule with null condition");
this.action = action;
this.condition = condition;
}
private String toXML() {
return "<" + ELEMENT + " " + Action.ATTRIBUTE_NAME + "=\"" + action.toString() + "\" " +
Condition.ATTRIBUTE_NAME + "=\"" + condition.getName() + "\" " +
"value=\"" + condition.getValue() + "\"/>";
}
}
/**
* Interface for defining XEP-0079 Conditions and their values
* @see AMPDeliverCondition
* @see AMPExpireAtCondition
* @see AMPMatchResourceCondition
**/
public static interface Condition {
String getName();
String getValue();
static final String ATTRIBUTE_NAME="condition";
}
/**
* amp action attribute
* See http://xmpp.org/extensions/xep-0079.html#actions-def
**/
public static enum Action {
/**
* The "alert" action triggers a reply <message/> stanza to the sending entity.
* This <message/> stanza MUST contain the element <amp status='alert'/>,
* which itself contains the <rule/> that triggered this action. In all other respects,
* this action behaves as "drop".
*/
alert,
/**
* The "drop" action silently discards the message from any further delivery attempts
* and ensures that it is not placed into offline storage.
* The drop MUST NOT result in other responses.
*/
drop,
/**
* The "error" action triggers a reply <message/> stanza of type "error" to the sending entity.
* The <message/> stanza's <error/> child MUST contain a
* <failed-rules xmlns='http://jabber.org/protocol/amp#errors'/> error condition,
* which itself contains the rules that triggered this action.
*/
error,
/**
* The "notify" action triggers a reply <message/> stanza to the sending entity.
* This <message/> stanza MUST contain the element <amp status='notify'/>, which itself
* contains the <rule/> that triggered this action. Unlike the other actions,
* this action does not override the default behavior for a server.
* Instead, the server then executes its default behavior after sending the notify.
*/
notify;
public static final String ATTRIBUTE_NAME="action";
}
/**
* amp notification status as defined by XEP-0079
*/
public static enum Status {
alert,
error,
notify
}
}

View file

@ -0,0 +1,132 @@
/**
*
* Copyright 2014 Vyacheslav Blinov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.amp.provider;
import java.util.logging.Logger;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.jivesoftware.smackx.amp.AMPDeliverCondition;
import org.jivesoftware.smackx.amp.AMPExpireAtCondition;
import org.jivesoftware.smackx.amp.AMPMatchResourceCondition;
import org.jivesoftware.smackx.amp.packet.AMPExtension;
import org.xmlpull.v1.XmlPullParser;
public class AMPExtensionProvider implements PacketExtensionProvider {
private static final Logger LOGGER = Logger.getLogger(AMPExtensionProvider.class.getName());
/**
* Creates a new AMPExtensionProvider.
* ProviderManager requires that every PacketExtensionProvider has a public, no-argument constructor
*/
public AMPExtensionProvider() {}
/**
* Parses a AMPExtension packet (extension sub-packet).
*
* @param parser the XML parser, positioned at the starting element of the extension.
* @return a PacketExtension.
* @throws Exception if a parsing error occurs.
*/
@Override
public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
final String from = parser.getAttributeValue(null, "from");
final String to = parser.getAttributeValue(null, "to");
final String statusString = parser.getAttributeValue(null, "status");
AMPExtension.Status status = null;
if (statusString != null) {
try {
status = AMPExtension.Status.valueOf(statusString);
} catch (IllegalArgumentException ex) {
LOGGER.severe("Found invalid amp status " + statusString);
}
}
AMPExtension ampExtension = new AMPExtension(from, to, status);
String perHopValue = parser.getAttributeValue(null, "per-hop");
if (perHopValue != null) {
boolean perHop = Boolean.parseBoolean(perHopValue);
ampExtension.setPerHop(perHop);
}
boolean done = false;
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals(AMPExtension.Rule.ELEMENT)) {
String actionString = parser.getAttributeValue(null, AMPExtension.Action.ATTRIBUTE_NAME);
String conditionName = parser.getAttributeValue(null, AMPExtension.Condition.ATTRIBUTE_NAME);
String conditionValue = parser.getAttributeValue(null, "value");
AMPExtension.Condition condition = createCondition(conditionName, conditionValue);
AMPExtension.Action action = null;
if (actionString != null) {
try {
action = AMPExtension.Action.valueOf(actionString);
} catch (IllegalArgumentException ex) {
LOGGER.severe("Found invalid rule action value " + actionString);
}
}
if (action == null || condition == null) {
LOGGER.severe("Rule is skipped because either it's action or it's condition is invalid");
} else {
AMPExtension.Rule rule = new AMPExtension.Rule(action, condition);
ampExtension.addRule(rule);
}
}
} else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals(AMPExtension.ELEMENT)) {
done = true;
}
}
}
return ampExtension;
}
private AMPExtension.Condition createCondition(String name, String value) {
if (name == null || value == null) {
LOGGER.severe("Can't create rule condition from null name and/or value");
return null;
}
if (AMPDeliverCondition.NAME.equals(name)) {
try {
return new AMPDeliverCondition(AMPDeliverCondition.Value.valueOf(value));
} catch (IllegalArgumentException ex) {
LOGGER.severe("Found invalid rule delivery condition value " + value);
return null;
}
} else if (AMPExpireAtCondition.NAME.equals(name)) {
return new AMPExpireAtCondition(value);
} else if (AMPMatchResourceCondition.NAME.equals(name)) {
try {
return new AMPMatchResourceCondition(AMPMatchResourceCondition.Value.valueOf(value));
} catch (IllegalArgumentException ex) {
LOGGER.severe("Found invalid rule match-resource condition value " + value);
return null;
}
} else {
LOGGER.severe("Found unknown rule condition name " + name);
return null;
}
}
}

View file

@ -0,0 +1,97 @@
/**
*
* Copyright 2003-2010 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.attention.packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.xmlpull.v1.XmlPullParser;
/**
* A PacketExtension that implements XEP-0224: Attention
*
* This extension is expected to be added to message stanzas of type 'headline.'
* Please refer to the XEP for more implementation guidelines.
*
* @author Guus der Kinderen, guus.der.kinderen@gmail.com
* @see <a
* href="http://xmpp.org/extensions/xep-0224.html">XEP-0224:&nbsp;Attention</a>
*/
public class AttentionExtension implements PacketExtension {
/**
* The XML element name of an 'attention' extension.
*/
public static final String ELEMENT_NAME = "attention";
/**
* The namespace that qualifies the XML element of an 'attention' extension.
*/
public static final String NAMESPACE = "urn:xmpp:attention:0";
/*
* (non-Javadoc)
*
* @see org.jivesoftware.smack.packet.PacketExtension#getElementName()
*/
public String getElementName() {
return ELEMENT_NAME;
}
/*
* (non-Javadoc)
*
* @see org.jivesoftware.smack.packet.PacketExtension#getNamespace()
*/
public String getNamespace() {
return NAMESPACE;
}
/*
* (non-Javadoc)
*
* @see org.jivesoftware.smack.packet.PacketExtension#toXML()
*/
public String toXML() {
final StringBuilder sb = new StringBuilder();
sb.append("<").append(getElementName()).append(" xmlns=\"").append(
getNamespace()).append("\"/>");
return sb.toString();
}
/**
* A {@link PacketExtensionProvider} for the {@link AttentionExtension}. As
* Attention elements have no state/information other than the element name
* and namespace, this implementation simply returns new instances of
* {@link AttentionExtension}.
*
* @author Guus der Kinderen, guus.der.kinderen@gmail.com
s */
public static class Provider implements PacketExtensionProvider {
/*
* (non-Javadoc)
*
* @see
* org.jivesoftware.smack.provider.PacketExtensionProvider#parseExtension
* (org.xmlpull.v1.XmlPullParser)
*/
public PacketExtension parseExtension(XmlPullParser arg0)
throws Exception {
return new AttentionExtension();
}
}
}

View file

@ -0,0 +1,242 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bookmarks;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smackx.iqprivate.PrivateDataManager;
/**
* Provides methods to manage bookmarks in accordance with JEP-0048. Methods for managing URLs and
* Conferences are provided.
* </p>
* It should be noted that some extensions have been made to the JEP. There is an attribute on URLs
* that marks a url as a news feed and also a sub-element can be added to either a URL or conference
* indicated that it is shared amongst all users on a server.
*
* @author Alexander Wenckus
*/
public class BookmarkManager {
private static final Map<XMPPConnection, BookmarkManager> bookmarkManagerMap = new WeakHashMap<XMPPConnection, BookmarkManager>();
static {
PrivateDataManager.addPrivateDataProvider("storage", "storage:bookmarks",
new Bookmarks.Provider());
}
/**
* Returns the <i>BookmarkManager</i> for a connection, if it doesn't exist it is created.
*
* @param connection the connection for which the manager is desired.
* @return Returns the <i>BookmarkManager</i> for a connection, if it doesn't
* exist it is created.
* @throws XMPPException
* @throws SmackException thrown has not been authenticated.
* @throws IllegalArgumentException when the connection is null.
*/
public synchronized static BookmarkManager getBookmarkManager(XMPPConnection connection)
throws XMPPException, SmackException
{
BookmarkManager manager = (BookmarkManager) bookmarkManagerMap.get(connection);
if (manager == null) {
manager = new BookmarkManager(connection);
}
return manager;
}
private PrivateDataManager privateDataManager;
private Bookmarks bookmarks;
private final Object bookmarkLock = new Object();
/**
* Default constructor. Registers the data provider with the private data manager in the
* storage:bookmarks namespace.
*
* @param connection the connection for persisting and retrieving bookmarks.
*/
private BookmarkManager(XMPPConnection connection) throws XMPPException, SmackException {
privateDataManager = PrivateDataManager.getInstanceFor(connection);
bookmarkManagerMap.put(connection, this);
}
/**
* Returns all currently bookmarked conferences.
*
* @return returns all currently bookmarked conferences
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
* @see BookmarkedConference
*/
public Collection<BookmarkedConference> getBookmarkedConferences() throws NoResponseException, XMPPErrorException, NotConnectedException {
retrieveBookmarks();
return Collections.unmodifiableCollection(bookmarks.getBookmarkedConferences());
}
/**
* Adds or updates a conference in the bookmarks.
*
* @param name the name of the conference
* @param jid the jid of the conference
* @param isAutoJoin whether or not to join this conference automatically on login
* @param nickname the nickname to use for the user when joining the conference
* @param password the password to use for the user when joining the conference
* @throws XMPPErrorException thrown when there is an issue retrieving the current bookmarks from
* the server.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
*/
public void addBookmarkedConference(String name, String jid, boolean isAutoJoin,
String nickname, String password) throws NoResponseException, XMPPErrorException, NotConnectedException
{
retrieveBookmarks();
BookmarkedConference bookmark
= new BookmarkedConference(name, jid, isAutoJoin, nickname, password);
List<BookmarkedConference> conferences = bookmarks.getBookmarkedConferences();
if(conferences.contains(bookmark)) {
BookmarkedConference oldConference = conferences.get(conferences.indexOf(bookmark));
if(oldConference.isShared()) {
throw new IllegalArgumentException("Cannot modify shared bookmark");
}
oldConference.setAutoJoin(isAutoJoin);
oldConference.setName(name);
oldConference.setNickname(nickname);
oldConference.setPassword(password);
}
else {
bookmarks.addBookmarkedConference(bookmark);
}
privateDataManager.setPrivateData(bookmarks);
}
/**
* Removes a conference from the bookmarks.
*
* @param jid the jid of the conference to be removed.
* @throws XMPPErrorException thrown when there is a problem with the connection attempting to
* retrieve the bookmarks or persist the bookmarks.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
* @throws IllegalArgumentException thrown when the conference being removed is a shared
* conference
*/
public void removeBookmarkedConference(String jid) throws NoResponseException, XMPPErrorException, NotConnectedException {
retrieveBookmarks();
Iterator<BookmarkedConference> it = bookmarks.getBookmarkedConferences().iterator();
while(it.hasNext()) {
BookmarkedConference conference = it.next();
if(conference.getJid().equalsIgnoreCase(jid)) {
if(conference.isShared()) {
throw new IllegalArgumentException("Conference is shared and can't be removed");
}
it.remove();
privateDataManager.setPrivateData(bookmarks);
return;
}
}
}
/**
* Returns an unmodifiable collection of all bookmarked urls.
*
* @return returns an unmodifiable collection of all bookmarked urls.
* @throws XMPPErrorException thrown when there is a problem retriving bookmarks from the server.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
*/
public Collection<BookmarkedURL> getBookmarkedURLs() throws NoResponseException, XMPPErrorException, NotConnectedException {
retrieveBookmarks();
return Collections.unmodifiableCollection(bookmarks.getBookmarkedURLS());
}
/**
* Adds a new url or updates an already existing url in the bookmarks.
*
* @param URL the url of the bookmark
* @param name the name of the bookmark
* @param isRSS whether or not the url is an rss feed
* @throws XMPPErrorException thrown when there is an error retriving or saving bookmarks from or to
* the server
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
*/
public void addBookmarkedURL(String URL, String name, boolean isRSS) throws NoResponseException, XMPPErrorException, NotConnectedException {
retrieveBookmarks();
BookmarkedURL bookmark = new BookmarkedURL(URL, name, isRSS);
List<BookmarkedURL> urls = bookmarks.getBookmarkedURLS();
if(urls.contains(bookmark)) {
BookmarkedURL oldURL = urls.get(urls.indexOf(bookmark));
if(oldURL.isShared()) {
throw new IllegalArgumentException("Cannot modify shared bookmarks");
}
oldURL.setName(name);
oldURL.setRss(isRSS);
}
else {
bookmarks.addBookmarkedURL(bookmark);
}
privateDataManager.setPrivateData(bookmarks);
}
/**
* Removes a url from the bookmarks.
*
* @param bookmarkURL the url of the bookmark to remove
* @throws XMPPErrorException thrown if there is an error retriving or saving bookmarks from or to
* the server.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
*/
public void removeBookmarkedURL(String bookmarkURL) throws NoResponseException, XMPPErrorException, NotConnectedException {
retrieveBookmarks();
Iterator<BookmarkedURL> it = bookmarks.getBookmarkedURLS().iterator();
while(it.hasNext()) {
BookmarkedURL bookmark = it.next();
if(bookmark.getURL().equalsIgnoreCase(bookmarkURL)) {
if(bookmark.isShared()) {
throw new IllegalArgumentException("Cannot delete a shared bookmark.");
}
it.remove();
privateDataManager.setPrivateData(bookmarks);
return;
}
}
}
private Bookmarks retrieveBookmarks() throws NoResponseException, XMPPErrorException, NotConnectedException {
synchronized(bookmarkLock) {
if(bookmarks == null) {
bookmarks = (Bookmarks) privateDataManager.getPrivateData("storage",
"storage:bookmarks");
}
return bookmarks;
}
}
}

View file

@ -0,0 +1,128 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bookmarks;
/**
* Respresents a Conference Room bookmarked on the server using JEP-0048 Bookmark Storage JEP.
*
* @author Derek DeMoro
*/
public class BookmarkedConference implements SharedBookmark {
private String name;
private boolean autoJoin;
private final String jid;
private String nickname;
private String password;
private boolean isShared;
protected BookmarkedConference(String jid) {
this.jid = jid;
}
protected BookmarkedConference(String name, String jid, boolean autoJoin, String nickname,
String password)
{
this.name = name;
this.jid = jid;
this.autoJoin = autoJoin;
this.nickname = nickname;
this.password = password;
}
/**
* Returns the display label representing the Conference room.
*
* @return the name of the conference room.
*/
public String getName() {
return name;
}
protected void setName(String name) {
this.name = name;
}
/**
* Returns true if this conference room should be auto-joined on startup.
*
* @return true if room should be joined on startup, otherwise false.
*/
public boolean isAutoJoin() {
return autoJoin;
}
protected void setAutoJoin(boolean autoJoin) {
this.autoJoin = autoJoin;
}
/**
* Returns the full JID of this conference room. (ex.dev@conference.jivesoftware.com)
*
* @return the full JID of this conference room.
*/
public String getJid() {
return jid;
}
/**
* Returns the nickname to use when joining this conference room. This is an optional
* value and may return null.
*
* @return the nickname to use when joining, null may be returned.
*/
public String getNickname() {
return nickname;
}
protected void setNickname(String nickname) {
this.nickname = nickname;
}
/**
* Returns the password to use when joining this conference room. This is an optional
* value and may return null.
*
* @return the password to use when joining this conference room, null may be returned.
*/
public String getPassword() {
return password;
}
protected void setPassword(String password) {
this.password = password;
}
public boolean equals(Object obj) {
if(obj == null || !(obj instanceof BookmarkedConference)) {
return false;
}
BookmarkedConference conference = (BookmarkedConference)obj;
return conference.getJid().equalsIgnoreCase(jid);
}
protected void setShared(boolean isShared) {
this.isShared = isShared;
}
public boolean isShared() {
return isShared;
}
}

View file

@ -0,0 +1,102 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bookmarks;
/**
* Respresents one instance of a URL defined using JEP-0048 Bookmark Storage JEP.
*
* @author Derek DeMoro
*/
public class BookmarkedURL implements SharedBookmark {
private String name;
private final String URL;
private boolean isRss;
private boolean isShared;
protected BookmarkedURL(String URL) {
this.URL = URL;
}
protected BookmarkedURL(String URL, String name, boolean isRss) {
this.URL = URL;
this.name = name;
this.isRss = isRss;
}
/**
* Returns the name representing the URL (eg. Jive Software). This can be used in as a label, or
* identifer in applications.
*
* @return the name reprenting the URL.
*/
public String getName() {
return name;
}
/**
* Sets the name representing the URL.
*
* @param name the name.
*/
protected void setName(String name) {
this.name = name;
}
/**
* Returns the URL.
*
* @return the url.
*/
public String getURL() {
return URL;
}
/**
* Set to true if this URL is an RSS or news feed.
*
* @param isRss True if the URL is a news feed and false if it is not.
*/
protected void setRss(boolean isRss) {
this.isRss = isRss;
}
/**
* Returns true if this URL is a news feed.
*
* @return Returns true if this URL is a news feed.
*/
public boolean isRss() {
return isRss;
}
public boolean equals(Object obj) {
if(!(obj instanceof BookmarkedURL)) {
return false;
}
BookmarkedURL url = (BookmarkedURL)obj;
return url.getURL().equalsIgnoreCase(URL);
}
protected void setShared(boolean shared) {
this.isShared = shared;
}
public boolean isShared() {
return isShared;
}
}

View file

@ -0,0 +1,303 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bookmarks;
import org.jivesoftware.smackx.iqprivate.packet.PrivateData;
import org.jivesoftware.smackx.iqprivate.provider.PrivateDataProvider;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Bookmarks is used for storing and retrieving URLS and Conference rooms.
* Bookmark Storage (JEP-0048) defined a protocol for the storage of bookmarks to conference rooms and other entities
* in a Jabber user's account.
* See the following code sample for saving Bookmarks:
* <p/>
* <pre>
* XMPPConnection con = new XMPPTCPConnection("jabber.org");
* con.login("john", "doe");
* Bookmarks bookmarks = new Bookmarks();
* <p/>
* // Bookmark a URL
* BookmarkedURL url = new BookmarkedURL();
* url.setName("Google");
* url.setURL("http://www.jivesoftware.com");
* bookmarks.addURL(url);
* // Bookmark a Conference room.
* BookmarkedConference conference = new BookmarkedConference();
* conference.setName("My Favorite Room");
* conference.setAutoJoin("true");
* conference.setJID("dev@conference.jivesoftware.com");
* bookmarks.addConference(conference);
* // Save Bookmarks using PrivateDataManager.
* PrivateDataManager manager = new PrivateDataManager(con);
* manager.setPrivateData(bookmarks);
* <p/>
* <p/>
* LastActivity activity = LastActivity.getLastActivity(con, "xray@jabber.org");
* </pre>
*
* @author Derek DeMoro
*/
public class Bookmarks implements PrivateData {
private List<BookmarkedURL> bookmarkedURLS;
private List<BookmarkedConference> bookmarkedConferences;
/**
* Required Empty Constructor to use Bookmarks.
*/
public Bookmarks() {
bookmarkedURLS = new ArrayList<BookmarkedURL>();
bookmarkedConferences = new ArrayList<BookmarkedConference>();
}
/**
* Adds a BookmarkedURL.
*
* @param bookmarkedURL the bookmarked bookmarkedURL.
*/
public void addBookmarkedURL(BookmarkedURL bookmarkedURL) {
bookmarkedURLS.add(bookmarkedURL);
}
/**
* Removes a bookmarked bookmarkedURL.
*
* @param bookmarkedURL the bookmarked bookmarkedURL to remove.
*/
public void removeBookmarkedURL(BookmarkedURL bookmarkedURL) {
bookmarkedURLS.remove(bookmarkedURL);
}
/**
* Removes all BookmarkedURLs from user's bookmarks.
*/
public void clearBookmarkedURLS() {
bookmarkedURLS.clear();
}
/**
* Add a BookmarkedConference to bookmarks.
*
* @param bookmarkedConference the conference to remove.
*/
public void addBookmarkedConference(BookmarkedConference bookmarkedConference) {
bookmarkedConferences.add(bookmarkedConference);
}
/**
* Removes a BookmarkedConference.
*
* @param bookmarkedConference the BookmarkedConference to remove.
*/
public void removeBookmarkedConference(BookmarkedConference bookmarkedConference) {
bookmarkedConferences.remove(bookmarkedConference);
}
/**
* Removes all BookmarkedConferences from Bookmarks.
*/
public void clearBookmarkedConferences() {
bookmarkedConferences.clear();
}
/**
* Returns a Collection of all Bookmarked URLs for this user.
*
* @return a collection of all Bookmarked URLs.
*/
public List<BookmarkedURL> getBookmarkedURLS() {
return bookmarkedURLS;
}
/**
* Returns a Collection of all Bookmarked Conference for this user.
*
* @return a collection of all Bookmarked Conferences.
*/
public List<BookmarkedConference> getBookmarkedConferences() {
return bookmarkedConferences;
}
/**
* Returns the root element name.
*
* @return the element name.
*/
public String getElementName() {
return "storage";
}
/**
* Returns the root element XML namespace.
*
* @return the namespace.
*/
public String getNamespace() {
return "storage:bookmarks";
}
/**
* Returns the XML reppresentation of the PrivateData.
*
* @return the private data as XML.
*/
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<storage xmlns=\"storage:bookmarks\">");
for (BookmarkedURL urlStorage : getBookmarkedURLS()) {
if(urlStorage.isShared()) {
continue;
}
buf.append("<url name=\"").append(urlStorage.getName()).
append("\" url=\"").append(urlStorage.getURL()).append("\"");
if(urlStorage.isRss()) {
buf.append(" rss=\"").append(true).append("\"");
}
buf.append(" />");
}
// Add Conference additions
for (BookmarkedConference conference : getBookmarkedConferences()) {
if(conference.isShared()) {
continue;
}
buf.append("<conference ");
buf.append("name=\"").append(conference.getName()).append("\" ");
buf.append("autojoin=\"").append(conference.isAutoJoin()).append("\" ");
buf.append("jid=\"").append(conference.getJid()).append("\" ");
buf.append(">");
if (conference.getNickname() != null) {
buf.append("<nick>").append(conference.getNickname()).append("</nick>");
}
if (conference.getPassword() != null) {
buf.append("<password>").append(conference.getPassword()).append("</password>");
}
buf.append("</conference>");
}
buf.append("</storage>");
return buf.toString();
}
/**
* The IQ Provider for BookmarkStorage.
*
* @author Derek DeMoro
*/
public static class Provider implements PrivateDataProvider {
/**
* Empty Constructor for PrivateDataProvider.
*/
public Provider() {
super();
}
public PrivateData parsePrivateData(XmlPullParser parser) throws Exception {
Bookmarks storage = new Bookmarks();
boolean done = false;
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG && "url".equals(parser.getName())) {
final BookmarkedURL urlStorage = getURLStorage(parser);
if (urlStorage != null) {
storage.addBookmarkedURL(urlStorage);
}
}
else if (eventType == XmlPullParser.START_TAG &&
"conference".equals(parser.getName()))
{
final BookmarkedConference conference = getConferenceStorage(parser);
storage.addBookmarkedConference(conference);
}
else if (eventType == XmlPullParser.END_TAG && "storage".equals(parser.getName()))
{
done = true;
}
}
return storage;
}
}
private static BookmarkedURL getURLStorage(XmlPullParser parser) throws IOException, XmlPullParserException {
String name = parser.getAttributeValue("", "name");
String url = parser.getAttributeValue("", "url");
String rssString = parser.getAttributeValue("", "rss");
boolean rss = rssString != null && "true".equals(rssString);
BookmarkedURL urlStore = new BookmarkedURL(url, name, rss);
boolean done = false;
while (!done) {
int eventType = parser.next();
if(eventType == XmlPullParser.START_TAG
&& "shared_bookmark".equals(parser.getName())) {
urlStore.setShared(true);
}
else if (eventType == XmlPullParser.END_TAG && "url".equals(parser.getName())) {
done = true;
}
}
return urlStore;
}
private static BookmarkedConference getConferenceStorage(XmlPullParser parser) throws Exception {
String name = parser.getAttributeValue("", "name");
String autojoin = parser.getAttributeValue("", "autojoin");
String jid = parser.getAttributeValue("", "jid");
BookmarkedConference conf = new BookmarkedConference(jid);
conf.setName(name);
conf.setAutoJoin(Boolean.valueOf(autojoin).booleanValue());
// Check for nickname
boolean done = false;
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG && "nick".equals(parser.getName())) {
conf.setNickname(parser.nextText());
}
else if (eventType == XmlPullParser.START_TAG && "password".equals(parser.getName())) {
conf.setPassword(parser.nextText());
}
else if(eventType == XmlPullParser.START_TAG
&& "shared_bookmark".equals(parser.getName())) {
conf.setShared(true);
}
else if (eventType == XmlPullParser.END_TAG && "conference".equals(parser.getName())) {
done = true;
}
}
return conf;
}
}

View file

@ -0,0 +1,33 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bookmarks;
/**
* Interface to indicate if a bookmark is shared across the server.
*
* @author Alexander Wenckus
*/
public interface SharedBookmark {
/**
* Returns true if this bookmark is shared.
*
* @return returns true if this bookmark is shared.
*/
public boolean isShared();
}

View file

@ -0,0 +1,50 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamListener;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamListener;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager;
/**
* BytestreamListener are notified if a remote user wants to initiate a bytestream. Implement this
* interface to handle incoming bytestream requests.
* <p>
* BytestreamListener can be registered at the {@link Socks5BytestreamManager} or the
* {@link InBandBytestreamManager}.
* <p>
* There are two ways to add this listener. See
* {@link BytestreamManager#addIncomingBytestreamListener(BytestreamListener)} and
* {@link BytestreamManager#addIncomingBytestreamListener(BytestreamListener, String)} for further
* details.
* <p>
* {@link Socks5BytestreamListener} or {@link InBandBytestreamListener} provide a more specific
* interface of the BytestreamListener.
*
* @author Henning Staib
*/
public interface BytestreamListener {
/**
* This listener is notified if a bytestream request from another user has been received.
*
* @param request the incoming bytestream request
*/
public void incomingBytestreamRequest(BytestreamRequest request);
}

View file

@ -0,0 +1,118 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams;
import java.io.IOException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager;
/**
* BytestreamManager provides a generic interface for bytestream managers.
* <p>
* There are two implementations of the interface. See {@link Socks5BytestreamManager} and
* {@link InBandBytestreamManager}.
*
* @author Henning Staib
*/
public interface BytestreamManager {
/**
* Adds {@link BytestreamListener} that is called for every incoming bytestream request unless
* there is a user specific {@link BytestreamListener} registered.
* <p>
* See {@link Socks5BytestreamManager#addIncomingBytestreamListener(BytestreamListener)} and
* {@link InBandBytestreamManager#addIncomingBytestreamListener(BytestreamListener)} for further
* details.
*
* @param listener the listener to register
*/
public void addIncomingBytestreamListener(BytestreamListener listener);
/**
* Removes the given listener from the list of listeners for all incoming bytestream requests.
*
* @param listener the listener to remove
*/
public void removeIncomingBytestreamListener(BytestreamListener listener);
/**
* Adds {@link BytestreamListener} that is called for every incoming bytestream request unless
* there is a user specific {@link BytestreamListener} registered.
* <p>
* Use this method if you are awaiting an incoming bytestream request from a specific user.
* <p>
* See {@link Socks5BytestreamManager#addIncomingBytestreamListener(BytestreamListener, String)}
* and {@link InBandBytestreamManager#addIncomingBytestreamListener(BytestreamListener, String)}
* for further details.
*
* @param listener the listener to register
* @param initiatorJID the JID of the user that wants to establish a bytestream
*/
public void addIncomingBytestreamListener(BytestreamListener listener, String initiatorJID);
/**
* Removes the listener for the given user.
*
* @param initiatorJID the JID of the user the listener should be removed
*/
public void removeIncomingBytestreamListener(String initiatorJID);
/**
* Establishes a bytestream with the given user and returns the session to send/receive data
* to/from the user.
* <p>
* Use this method to establish bytestreams to users accepting all incoming bytestream requests
* since this method doesn't provide a way to tell the user something about the data to be sent.
* <p>
* To establish a bytestream after negotiation the kind of data to be sent (e.g. file transfer)
* use {@link #establishSession(String, String)}.
* <p>
* See {@link Socks5BytestreamManager#establishSession(String)} and
* {@link InBandBytestreamManager#establishSession(String)} for further details.
*
* @param targetJID the JID of the user a bytestream should be established
* @return the session to send/receive data to/from the user
* @throws XMPPException if an error occurred while establishing the session
* @throws IOException if an IO error occurred while establishing the session
* @throws InterruptedException if the thread was interrupted while waiting in a blocking
* operation
*/
public BytestreamSession establishSession(String targetJID) throws XMPPException, IOException,
InterruptedException, SmackException;
/**
* Establishes a bytestream with the given user and returns the session to send/receive data
* to/from the user.
* <p>
* See {@link Socks5BytestreamManager#establishSession(String)} and
* {@link InBandBytestreamManager#establishSession(String)} for further details.
*
* @param targetJID the JID of the user a bytestream should be established
* @param sessionID the session ID for the bytestream request
* @return the session to send/receive data to/from the user
* @throws XMPPException if an error occurred while establishing the session
* @throws IOException if an IO error occurred while establishing the session
* @throws InterruptedException if the thread was interrupted while waiting in a blocking
* operation
*/
public BytestreamSession establishSession(String targetJID, String sessionID)
throws XMPPException, IOException, InterruptedException, SmackException;
}

View file

@ -0,0 +1,68 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamRequest;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest;
/**
* BytestreamRequest provides an interface to handle incoming bytestream requests.
* <p>
* There are two implementations of the interface. See {@link Socks5BytestreamRequest} and
* {@link InBandBytestreamRequest}.
*
* @author Henning Staib
*/
public interface BytestreamRequest {
/**
* Returns the sender of the bytestream open request.
*
* @return the sender of the bytestream open request
*/
public String getFrom();
/**
* Returns the session ID of the bytestream open request.
*
* @return the session ID of the bytestream open request
*/
public String getSessionID();
/**
* Accepts the bytestream open request and returns the session to send/receive data.
*
* @return the session to send/receive data
* @throws XMPPErrorException if an error occurred while accepting the bytestream request
* @throws InterruptedException if the thread was interrupted while waiting in a blocking
* operation
* @throws NoResponseException
* @throws SmackException
*/
public BytestreamSession accept() throws InterruptedException, NoResponseException, XMPPErrorException, SmackException;
/**
* Rejects the bytestream request by sending a reject error to the initiator.
* @throws NotConnectedException
*/
public void reject() throws NotConnectedException;
}

View file

@ -0,0 +1,84 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamSession;
/**
* BytestreamSession provides an interface for established bytestream sessions.
* <p>
* There are two implementations of the interface. See {@link Socks5BytestreamSession} and
* {@link InBandBytestreamSession}.
*
* @author Henning Staib
*/
public interface BytestreamSession {
/**
* Returns the InputStream associated with this session to send data.
*
* @return the InputStream associated with this session to send data
* @throws IOException if an error occurs while retrieving the input stream
*/
public InputStream getInputStream() throws IOException;
/**
* Returns the OutputStream associated with this session to receive data.
*
* @return the OutputStream associated with this session to receive data
* @throws IOException if an error occurs while retrieving the output stream
*/
public OutputStream getOutputStream() throws IOException;
/**
* Closes the bytestream session.
* <p>
* Closing the session will also close the input stream and the output stream associated to this
* session.
*
* @throws IOException if an error occurs while closing the session
*/
public void close() throws IOException;
/**
* Returns the timeout for read operations of the input stream associated with this session. 0
* returns implies that the option is disabled (i.e., timeout of infinity). Default is 0.
*
* @return the timeout for read operations
* @throws IOException if there is an error in the underlying protocol
*/
public int getReadTimeout() throws IOException;
/**
* Sets the specified timeout, in milliseconds. With this option set to a non-zero timeout, a
* read() call on the input stream associated with this session will block for only this amount
* of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the
* session is still valid. The option must be enabled prior to entering the blocking operation
* to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite
* timeout. Default is 0.
*
* @param timeout the specified timeout, in milliseconds
* @throws IOException if there is an error in the underlying protocol
*/
public void setReadTimeout(int timeout) throws IOException;
}

View file

@ -0,0 +1,79 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.ibb;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Close;
/**
* CloseListener handles all In-Band Bytestream close requests.
* <p>
* If a close request is received it looks if a stored In-Band Bytestream
* session exists and closes it. If no session with the given session ID exists
* an &lt;item-not-found/&gt; error is returned to the sender.
*
* @author Henning Staib
*/
class CloseListener implements PacketListener {
/* manager containing the listeners and the XMPP connection */
private final InBandBytestreamManager manager;
/* packet filter for all In-Band Bytestream close requests */
private final PacketFilter closeFilter = new AndFilter(new PacketTypeFilter(
Close.class), new IQTypeFilter(IQ.Type.SET));
/**
* Constructor.
*
* @param manager the In-Band Bytestream manager
*/
protected CloseListener(InBandBytestreamManager manager) {
this.manager = manager;
}
public void processPacket(Packet packet) throws NotConnectedException {
Close closeRequest = (Close) packet;
InBandBytestreamSession ibbSession = this.manager.getSessions().get(
closeRequest.getSessionID());
if (ibbSession == null) {
this.manager.replyItemNotFoundPacket(closeRequest);
}
else {
ibbSession.closeByPeer(closeRequest);
this.manager.getSessions().remove(closeRequest.getSessionID());
}
}
/**
* Returns the packet filter for In-Band Bytestream close requests.
*
* @return the packet filter for In-Band Bytestream close requests
*/
protected PacketFilter getFilter() {
return this.closeFilter;
}
}

View file

@ -0,0 +1,77 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.ibb;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Data;
/**
* DataListener handles all In-Band Bytestream IQ stanzas containing a data
* packet extension that don't belong to an existing session.
* <p>
* If a data packet is received it looks if a stored In-Band Bytestream session
* exists. If no session with the given session ID exists an
* &lt;item-not-found/&gt; error is returned to the sender.
* <p>
* Data packets belonging to a running In-Band Bytestream session are processed
* by more specific listeners registered when an {@link InBandBytestreamSession}
* is created.
*
* @author Henning Staib
*/
class DataListener implements PacketListener {
/* manager containing the listeners and the XMPP connection */
private final InBandBytestreamManager manager;
/* packet filter for all In-Band Bytestream data packets */
private final PacketFilter dataFilter = new AndFilter(
new PacketTypeFilter(Data.class));
/**
* Constructor.
*
* @param manager the In-Band Bytestream manager
*/
public DataListener(InBandBytestreamManager manager) {
this.manager = manager;
}
public void processPacket(Packet packet) throws NotConnectedException {
Data data = (Data) packet;
InBandBytestreamSession ibbSession = this.manager.getSessions().get(
data.getDataPacketExtension().getSessionID());
if (ibbSession == null) {
this.manager.replyItemNotFoundPacket(data);
}
}
/**
* Returns the packet filter for In-Band Bytestream data packets.
*
* @return the packet filter for In-Band Bytestream data packets
*/
protected PacketFilter getFilter() {
return this.dataFilter;
}
}

View file

@ -0,0 +1,49 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.ibb;
import org.jivesoftware.smackx.bytestreams.BytestreamListener;
import org.jivesoftware.smackx.bytestreams.BytestreamRequest;
/**
* InBandBytestreamListener are informed if a remote user wants to initiate an In-Band Bytestream.
* Implement this interface to handle incoming In-Band Bytestream requests.
* <p>
* There are two ways to add this listener. See
* {@link InBandBytestreamManager#addIncomingBytestreamListener(BytestreamListener)} and
* {@link InBandBytestreamManager#addIncomingBytestreamListener(BytestreamListener, String)} for
* further details.
*
* @author Henning Staib
*/
public abstract class InBandBytestreamListener implements BytestreamListener {
public void incomingBytestreamRequest(BytestreamRequest request) {
incomingBytestreamRequest((InBandBytestreamRequest) request);
}
/**
* This listener is notified if an In-Band Bytestream request from another user has been
* received.
*
* @param request the incoming In-Band Bytestream request
*/
public abstract void incomingBytestreamRequest(InBandBytestreamRequest request);
}

View file

@ -0,0 +1,570 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.ibb;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import org.jivesoftware.smack.AbstractConnectionListener;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smackx.bytestreams.BytestreamListener;
import org.jivesoftware.smackx.bytestreams.BytestreamManager;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
import org.jivesoftware.smackx.filetransfer.FileTransferManager;
/**
* The InBandBytestreamManager class handles establishing In-Band Bytestreams as specified in the <a
* href="http://xmpp.org/extensions/xep-0047.html">XEP-0047</a>.
* <p>
* The In-Band Bytestreams (IBB) enables two entities to establish a virtual bytestream over which
* they can exchange Base64-encoded chunks of data over XMPP itself. It is the fall-back mechanism
* in case the Socks5 bytestream method of transferring data is not available.
* <p>
* There are two ways to send data over an In-Band Bytestream. It could either use IQ stanzas to
* send data packets or message stanzas. If IQ stanzas are used every data packet is acknowledged by
* the receiver. This is the recommended way to avoid possible rate-limiting penalties. Message
* stanzas are not acknowledged because most XMPP server implementation don't support stanza
* flow-control method like <a href="http://xmpp.org/extensions/xep-0079.html">Advanced Message
* Processing</a>. To set the stanza that should be used invoke {@link #setStanza(StanzaType)}.
* <p>
* To establish an In-Band Bytestream invoke the {@link #establishSession(String)} method. This will
* negotiate an in-band bytestream with the given target JID and return a session.
* <p>
* If a session ID for the In-Band Bytestream was already negotiated (e.g. while negotiating a file
* transfer) invoke {@link #establishSession(String, String)}.
* <p>
* To handle incoming In-Band Bytestream requests add an {@link InBandBytestreamListener} to the
* manager. There are two ways to add this listener. If you want to be informed about incoming
* In-Band Bytestreams from a specific user add the listener by invoking
* {@link #addIncomingBytestreamListener(BytestreamListener, String)}. If the listener should
* respond to all In-Band Bytestream requests invoke
* {@link #addIncomingBytestreamListener(BytestreamListener)}.
* <p>
* Note that the registered {@link InBandBytestreamListener} will NOT be notified on incoming
* In-Band bytestream requests sent in the context of <a
* href="http://xmpp.org/extensions/xep-0096.html">XEP-0096</a> file transfer. (See
* {@link FileTransferManager})
* <p>
* If no {@link InBandBytestreamListener}s are registered, all incoming In-Band bytestream requests
* will be rejected by returning a &lt;not-acceptable/&gt; error to the initiator.
*
* @author Henning Staib
*/
public class InBandBytestreamManager implements BytestreamManager {
/**
* Stanzas that can be used to encapsulate In-Band Bytestream data packets.
*/
public enum StanzaType {
/**
* IQ stanza.
*/
IQ,
/**
* Message stanza.
*/
MESSAGE
}
/*
* create a new InBandBytestreamManager and register its shutdown listener on every established
* connection
*/
static {
XMPPConnection.addConnectionCreationListener(new ConnectionCreationListener() {
public void connectionCreated(final XMPPConnection connection) {
// create the manager for this connection
InBandBytestreamManager.getByteStreamManager(connection);
// register shutdown listener
connection.addConnectionListener(new AbstractConnectionListener() {
@Override
public void connectionClosed() {
InBandBytestreamManager.getByteStreamManager(connection).disableService();
}
@Override
public void connectionClosedOnError(Exception e) {
InBandBytestreamManager.getByteStreamManager(connection).disableService();
}
@Override
public void reconnectionSuccessful() {
// re-create the manager for this connection
InBandBytestreamManager.getByteStreamManager(connection);
}
});
}
});
}
/**
* The XMPP namespace of the In-Band Bytestream
*/
public static final String NAMESPACE = "http://jabber.org/protocol/ibb";
/**
* Maximum block size that is allowed for In-Band Bytestreams
*/
public static final int MAXIMUM_BLOCK_SIZE = 65535;
/* prefix used to generate session IDs */
private static final String SESSION_ID_PREFIX = "jibb_";
/* random generator to create session IDs */
private final static Random randomGenerator = new Random();
/* stores one InBandBytestreamManager for each XMPP connection */
private final static Map<XMPPConnection, InBandBytestreamManager> managers = new HashMap<XMPPConnection, InBandBytestreamManager>();
/* XMPP connection */
private final XMPPConnection connection;
/*
* assigns a user to a listener that is informed if an In-Band Bytestream request for this user
* is received
*/
private final Map<String, BytestreamListener> userListeners = new ConcurrentHashMap<String, BytestreamListener>();
/*
* list of listeners that respond to all In-Band Bytestream requests if there are no user
* specific listeners for that request
*/
private final List<BytestreamListener> allRequestListeners = Collections.synchronizedList(new LinkedList<BytestreamListener>());
/* listener that handles all incoming In-Band Bytestream requests */
private final InitiationListener initiationListener;
/* listener that handles all incoming In-Band Bytestream IQ data packets */
private final DataListener dataListener;
/* listener that handles all incoming In-Band Bytestream close requests */
private final CloseListener closeListener;
/* assigns a session ID to the In-Band Bytestream session */
private final Map<String, InBandBytestreamSession> sessions = new ConcurrentHashMap<String, InBandBytestreamSession>();
/* block size used for new In-Band Bytestreams */
private int defaultBlockSize = 4096;
/* maximum block size allowed for this connection */
private int maximumBlockSize = MAXIMUM_BLOCK_SIZE;
/* the stanza used to send data packets */
private StanzaType stanza = StanzaType.IQ;
/*
* list containing session IDs of In-Band Bytestream open packets that should be ignored by the
* InitiationListener
*/
private List<String> ignoredBytestreamRequests = Collections.synchronizedList(new LinkedList<String>());
/**
* Returns the InBandBytestreamManager to handle In-Band Bytestreams for a given
* {@link XMPPConnection}.
*
* @param connection the XMPP connection
* @return the InBandBytestreamManager for the given XMPP connection
*/
public static synchronized InBandBytestreamManager getByteStreamManager(XMPPConnection connection) {
if (connection == null)
return null;
InBandBytestreamManager manager = managers.get(connection);
if (manager == null) {
manager = new InBandBytestreamManager(connection);
managers.put(connection, manager);
}
return manager;
}
/**
* Constructor.
*
* @param connection the XMPP connection
*/
private InBandBytestreamManager(XMPPConnection connection) {
this.connection = connection;
// register bytestream open packet listener
this.initiationListener = new InitiationListener(this);
this.connection.addPacketListener(this.initiationListener,
this.initiationListener.getFilter());
// register bytestream data packet listener
this.dataListener = new DataListener(this);
this.connection.addPacketListener(this.dataListener, this.dataListener.getFilter());
// register bytestream close packet listener
this.closeListener = new CloseListener(this);
this.connection.addPacketListener(this.closeListener, this.closeListener.getFilter());
}
/**
* Adds InBandBytestreamListener that is called for every incoming in-band bytestream request
* unless there is a user specific InBandBytestreamListener registered.
* <p>
* If no listeners are registered all In-Band Bytestream request are rejected with a
* &lt;not-acceptable/&gt; error.
* <p>
* Note that the registered {@link InBandBytestreamListener} will NOT be notified on incoming
* Socks5 bytestream requests sent in the context of <a
* href="http://xmpp.org/extensions/xep-0096.html">XEP-0096</a> file transfer. (See
* {@link FileTransferManager})
*
* @param listener the listener to register
*/
public void addIncomingBytestreamListener(BytestreamListener listener) {
this.allRequestListeners.add(listener);
}
/**
* Removes the given listener from the list of listeners for all incoming In-Band Bytestream
* requests.
*
* @param listener the listener to remove
*/
public void removeIncomingBytestreamListener(BytestreamListener listener) {
this.allRequestListeners.remove(listener);
}
/**
* Adds InBandBytestreamListener that is called for every incoming in-band bytestream request
* from the given user.
* <p>
* Use this method if you are awaiting an incoming Socks5 bytestream request from a specific
* user.
* <p>
* If no listeners are registered all In-Band Bytestream request are rejected with a
* &lt;not-acceptable/&gt; error.
* <p>
* Note that the registered {@link InBandBytestreamListener} will NOT be notified on incoming
* Socks5 bytestream requests sent in the context of <a
* href="http://xmpp.org/extensions/xep-0096.html">XEP-0096</a> file transfer. (See
* {@link FileTransferManager})
*
* @param listener the listener to register
* @param initiatorJID the JID of the user that wants to establish an In-Band Bytestream
*/
public void addIncomingBytestreamListener(BytestreamListener listener, String initiatorJID) {
this.userListeners.put(initiatorJID, listener);
}
/**
* Removes the listener for the given user.
*
* @param initiatorJID the JID of the user the listener should be removed
*/
public void removeIncomingBytestreamListener(String initiatorJID) {
this.userListeners.remove(initiatorJID);
}
/**
* Use this method to ignore the next incoming In-Band Bytestream request containing the given
* session ID. No listeners will be notified for this request and and no error will be returned
* to the initiator.
* <p>
* This method should be used if you are awaiting an In-Band Bytestream request as a reply to
* another packet (e.g. file transfer).
*
* @param sessionID to be ignored
*/
public void ignoreBytestreamRequestOnce(String sessionID) {
this.ignoredBytestreamRequests.add(sessionID);
}
/**
* Returns the default block size that is used for all outgoing in-band bytestreams for this
* connection.
* <p>
* The recommended default block size is 4096 bytes. See <a
* href="http://xmpp.org/extensions/xep-0047.html#usage">XEP-0047</a> Section 5.
*
* @return the default block size
*/
public int getDefaultBlockSize() {
return defaultBlockSize;
}
/**
* Sets the default block size that is used for all outgoing in-band bytestreams for this
* connection.
* <p>
* The default block size must be between 1 and 65535 bytes. The recommended default block size
* is 4096 bytes. See <a href="http://xmpp.org/extensions/xep-0047.html#usage">XEP-0047</a>
* Section 5.
*
* @param defaultBlockSize the default block size to set
*/
public void setDefaultBlockSize(int defaultBlockSize) {
if (defaultBlockSize <= 0 || defaultBlockSize > MAXIMUM_BLOCK_SIZE) {
throw new IllegalArgumentException("Default block size must be between 1 and "
+ MAXIMUM_BLOCK_SIZE);
}
this.defaultBlockSize = defaultBlockSize;
}
/**
* Returns the maximum block size that is allowed for In-Band Bytestreams for this connection.
* <p>
* Incoming In-Band Bytestream open request will be rejected with an
* &lt;resource-constraint/&gt; error if the block size is greater then the maximum allowed
* block size.
* <p>
* The default maximum block size is 65535 bytes.
*
* @return the maximum block size
*/
public int getMaximumBlockSize() {
return maximumBlockSize;
}
/**
* Sets the maximum block size that is allowed for In-Band Bytestreams for this connection.
* <p>
* The maximum block size must be between 1 and 65535 bytes.
* <p>
* Incoming In-Band Bytestream open request will be rejected with an
* &lt;resource-constraint/&gt; error if the block size is greater then the maximum allowed
* block size.
*
* @param maximumBlockSize the maximum block size to set
*/
public void setMaximumBlockSize(int maximumBlockSize) {
if (maximumBlockSize <= 0 || maximumBlockSize > MAXIMUM_BLOCK_SIZE) {
throw new IllegalArgumentException("Maximum block size must be between 1 and "
+ MAXIMUM_BLOCK_SIZE);
}
this.maximumBlockSize = maximumBlockSize;
}
/**
* Returns the stanza used to send data packets.
* <p>
* Default is {@link StanzaType#IQ}. See <a
* href="http://xmpp.org/extensions/xep-0047.html#message">XEP-0047</a> Section 4.
*
* @return the stanza used to send data packets
*/
public StanzaType getStanza() {
return stanza;
}
/**
* Sets the stanza used to send data packets.
* <p>
* The use of {@link StanzaType#IQ} is recommended. See <a
* href="http://xmpp.org/extensions/xep-0047.html#message">XEP-0047</a> Section 4.
*
* @param stanza the stanza to set
*/
public void setStanza(StanzaType stanza) {
this.stanza = stanza;
}
/**
* Establishes an In-Band Bytestream with the given user and returns the session to send/receive
* data to/from the user.
* <p>
* Use this method to establish In-Band Bytestreams to users accepting all incoming In-Band
* Bytestream requests since this method doesn't provide a way to tell the user something about
* the data to be sent.
* <p>
* To establish an In-Band Bytestream after negotiation the kind of data to be sent (e.g. file
* transfer) use {@link #establishSession(String, String)}.
*
* @param targetJID the JID of the user an In-Band Bytestream should be established
* @return the session to send/receive data to/from the user
* @throws XMPPException if the user doesn't support or accept in-band bytestreams, or if the
* user prefers smaller block sizes
* @throws SmackException if there was no response from the server.
*/
public InBandBytestreamSession establishSession(String targetJID) throws XMPPException, SmackException {
String sessionID = getNextSessionID();
return establishSession(targetJID, sessionID);
}
/**
* Establishes an In-Band Bytestream with the given user using the given session ID and returns
* the session to send/receive data to/from the user.
*
* @param targetJID the JID of the user an In-Band Bytestream should be established
* @param sessionID the session ID for the In-Band Bytestream request
* @return the session to send/receive data to/from the user
* @throws XMPPErrorException if the user doesn't support or accept in-band bytestreams, or if the
* user prefers smaller block sizes
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
*/
public InBandBytestreamSession establishSession(String targetJID, String sessionID)
throws NoResponseException, XMPPErrorException, NotConnectedException {
Open byteStreamRequest = new Open(sessionID, this.defaultBlockSize, this.stanza);
byteStreamRequest.setTo(targetJID);
// sending packet will throw exception on timeout or error reply
connection.createPacketCollectorAndSend(byteStreamRequest).nextResultOrThrow();
InBandBytestreamSession inBandBytestreamSession = new InBandBytestreamSession(
this.connection, byteStreamRequest, targetJID);
this.sessions.put(sessionID, inBandBytestreamSession);
return inBandBytestreamSession;
}
/**
* Responses to the given IQ packet's sender with an XMPP error that an In-Band Bytestream is
* not accepted.
*
* @param request IQ packet that should be answered with a not-acceptable error
* @throws NotConnectedException
*/
protected void replyRejectPacket(IQ request) throws NotConnectedException {
XMPPError xmppError = new XMPPError(XMPPError.Condition.no_acceptable);
IQ error = IQ.createErrorResponse(request, xmppError);
this.connection.sendPacket(error);
}
/**
* Responses to the given IQ packet's sender with an XMPP error that an In-Band Bytestream open
* request is rejected because its block size is greater than the maximum allowed block size.
*
* @param request IQ packet that should be answered with a resource-constraint error
* @throws NotConnectedException
*/
protected void replyResourceConstraintPacket(IQ request) throws NotConnectedException {
XMPPError xmppError = new XMPPError(XMPPError.Condition.resource_constraint);
IQ error = IQ.createErrorResponse(request, xmppError);
this.connection.sendPacket(error);
}
/**
* Responses to the given IQ packet's sender with an XMPP error that an In-Band Bytestream
* session could not be found.
*
* @param request IQ packet that should be answered with a item-not-found error
* @throws NotConnectedException
*/
protected void replyItemNotFoundPacket(IQ request) throws NotConnectedException {
XMPPError xmppError = new XMPPError(XMPPError.Condition.item_not_found);
IQ error = IQ.createErrorResponse(request, xmppError);
this.connection.sendPacket(error);
}
/**
* Returns a new unique session ID.
*
* @return a new unique session ID
*/
private String getNextSessionID() {
StringBuilder buffer = new StringBuilder();
buffer.append(SESSION_ID_PREFIX);
buffer.append(Math.abs(randomGenerator.nextLong()));
return buffer.toString();
}
/**
* Returns the XMPP connection.
*
* @return the XMPP connection
*/
protected XMPPConnection getConnection() {
return this.connection;
}
/**
* Returns the {@link InBandBytestreamListener} that should be informed if a In-Band Bytestream
* request from the given initiator JID is received.
*
* @param initiator the initiator's JID
* @return the listener
*/
protected BytestreamListener getUserListener(String initiator) {
return this.userListeners.get(initiator);
}
/**
* Returns a list of {@link InBandBytestreamListener} that are informed if there are no
* listeners for a specific initiator.
*
* @return list of listeners
*/
protected List<BytestreamListener> getAllRequestListeners() {
return this.allRequestListeners;
}
/**
* Returns the sessions map.
*
* @return the sessions map
*/
protected Map<String, InBandBytestreamSession> getSessions() {
return sessions;
}
/**
* Returns the list of session IDs that should be ignored by the InitialtionListener
*
* @return list of session IDs
*/
protected List<String> getIgnoredBytestreamRequests() {
return ignoredBytestreamRequests;
}
/**
* Disables the InBandBytestreamManager by removing its packet listeners and resetting its
* internal status, which includes removing this instance from the managers map.
*/
private void disableService() {
// remove manager from static managers map
managers.remove(connection);
// remove all listeners registered by this manager
this.connection.removePacketListener(this.initiationListener);
this.connection.removePacketListener(this.dataListener);
this.connection.removePacketListener(this.closeListener);
// shutdown threads
this.initiationListener.shutdown();
// reset internal status
this.userListeners.clear();
this.allRequestListeners.clear();
this.sessions.clear();
this.ignoredBytestreamRequests.clear();
}
}

View file

@ -0,0 +1,96 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.ibb;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smackx.bytestreams.BytestreamRequest;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
/**
* InBandBytestreamRequest class handles incoming In-Band Bytestream requests.
*
* @author Henning Staib
*/
public class InBandBytestreamRequest implements BytestreamRequest {
/* the bytestream initialization request */
private final Open byteStreamRequest;
/*
* In-Band Bytestream manager containing the XMPP connection and helper
* methods
*/
private final InBandBytestreamManager manager;
protected InBandBytestreamRequest(InBandBytestreamManager manager,
Open byteStreamRequest) {
this.manager = manager;
this.byteStreamRequest = byteStreamRequest;
}
/**
* Returns the sender of the In-Band Bytestream open request.
*
* @return the sender of the In-Band Bytestream open request
*/
public String getFrom() {
return this.byteStreamRequest.getFrom();
}
/**
* Returns the session ID of the In-Band Bytestream open request.
*
* @return the session ID of the In-Band Bytestream open request
*/
public String getSessionID() {
return this.byteStreamRequest.getSessionID();
}
/**
* Accepts the In-Band Bytestream open request and returns the session to
* send/receive data.
*
* @return the session to send/receive data
* @throws NotConnectedException
*/
public InBandBytestreamSession accept() throws NotConnectedException {
XMPPConnection connection = this.manager.getConnection();
// create In-Band Bytestream session and store it
InBandBytestreamSession ibbSession = new InBandBytestreamSession(connection,
this.byteStreamRequest, this.byteStreamRequest.getFrom());
this.manager.getSessions().put(this.byteStreamRequest.getSessionID(), ibbSession);
// acknowledge request
IQ resultIQ = IQ.createResultIQ(this.byteStreamRequest);
connection.sendPacket(resultIQ);
return ibbSession;
}
/**
* Rejects the In-Band Bytestream request by sending a reject error to the
* initiator.
* @throws NotConnectedException
*/
public void reject() throws NotConnectedException {
this.manager.replyRejectPacket(this.byteStreamRequest);
}
}

View file

@ -0,0 +1,814 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.ibb;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketTimeoutException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Close;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Data;
import org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
/**
* InBandBytestreamSession class represents an In-Band Bytestream session.
* <p>
* In-band bytestreams are bidirectional and this session encapsulates the streams for both
* directions.
* <p>
* Note that closing the In-Band Bytestream session will close both streams. If both streams are
* closed individually the session will be closed automatically once the second stream is closed.
* Use the {@link #setCloseBothStreamsEnabled(boolean)} method if both streams should be closed
* automatically if one of them is closed.
*
* @author Henning Staib
*/
public class InBandBytestreamSession implements BytestreamSession {
/* XMPP connection */
private final XMPPConnection connection;
/* the In-Band Bytestream open request for this session */
private final Open byteStreamRequest;
/*
* the input stream for this session (either IQIBBInputStream or MessageIBBInputStream)
*/
private IBBInputStream inputStream;
/*
* the output stream for this session (either IQIBBOutputStream or MessageIBBOutputStream)
*/
private IBBOutputStream outputStream;
/* JID of the remote peer */
private String remoteJID;
/* flag to close both streams if one of them is closed */
private boolean closeBothStreamsEnabled = false;
/* flag to indicate if session is closed */
private boolean isClosed = false;
/**
* Constructor.
*
* @param connection the XMPP connection
* @param byteStreamRequest the In-Band Bytestream open request for this session
* @param remoteJID JID of the remote peer
*/
protected InBandBytestreamSession(XMPPConnection connection, Open byteStreamRequest,
String remoteJID) {
this.connection = connection;
this.byteStreamRequest = byteStreamRequest;
this.remoteJID = remoteJID;
// initialize streams dependent to the uses stanza type
switch (byteStreamRequest.getStanza()) {
case IQ:
this.inputStream = new IQIBBInputStream();
this.outputStream = new IQIBBOutputStream();
break;
case MESSAGE:
this.inputStream = new MessageIBBInputStream();
this.outputStream = new MessageIBBOutputStream();
break;
}
}
public InputStream getInputStream() {
return this.inputStream;
}
public OutputStream getOutputStream() {
return this.outputStream;
}
public int getReadTimeout() {
return this.inputStream.readTimeout;
}
public void setReadTimeout(int timeout) {
if (timeout < 0) {
throw new IllegalArgumentException("Timeout must be >= 0");
}
this.inputStream.readTimeout = timeout;
}
/**
* Returns whether both streams should be closed automatically if one of the streams is closed.
* Default is <code>false</code>.
*
* @return <code>true</code> if both streams will be closed if one of the streams is closed,
* <code>false</code> if both streams can be closed independently.
*/
public boolean isCloseBothStreamsEnabled() {
return closeBothStreamsEnabled;
}
/**
* Sets whether both streams should be closed automatically if one of the streams is closed.
* Default is <code>false</code>.
*
* @param closeBothStreamsEnabled <code>true</code> if both streams should be closed if one of
* the streams is closed, <code>false</code> if both streams should be closed
* independently
*/
public void setCloseBothStreamsEnabled(boolean closeBothStreamsEnabled) {
this.closeBothStreamsEnabled = closeBothStreamsEnabled;
}
public void close() throws IOException {
closeByLocal(true); // close input stream
closeByLocal(false); // close output stream
}
/**
* This method is invoked if a request to close the In-Band Bytestream has been received.
*
* @param closeRequest the close request from the remote peer
* @throws NotConnectedException
*/
protected void closeByPeer(Close closeRequest) throws NotConnectedException {
/*
* close streams without flushing them, because stream is already considered closed on the
* remote peers side
*/
this.inputStream.closeInternal();
this.inputStream.cleanup();
this.outputStream.closeInternal(false);
// acknowledge close request
IQ confirmClose = IQ.createResultIQ(closeRequest);
this.connection.sendPacket(confirmClose);
}
/**
* This method is invoked if one of the streams has been closed locally, if an error occurred
* locally or if the whole session should be closed.
*
* @throws IOException if an error occurs while sending the close request
*/
protected synchronized void closeByLocal(boolean in) throws IOException {
if (this.isClosed) {
return;
}
if (this.closeBothStreamsEnabled) {
this.inputStream.closeInternal();
this.outputStream.closeInternal(true);
}
else {
if (in) {
this.inputStream.closeInternal();
}
else {
// close stream but try to send any data left
this.outputStream.closeInternal(true);
}
}
if (this.inputStream.isClosed && this.outputStream.isClosed) {
this.isClosed = true;
// send close request
Close close = new Close(this.byteStreamRequest.getSessionID());
close.setTo(this.remoteJID);
try {
connection.createPacketCollectorAndSend(close).nextResultOrThrow();
}
catch (Exception e) {
// Sadly we are unable to use the IOException(Throwable) constructor because this
// constructor is only supported from Android API 9 on.
IOException ioException = new IOException();
ioException.initCause(e);
throw ioException;
}
this.inputStream.cleanup();
// remove session from manager
InBandBytestreamManager.getByteStreamManager(this.connection).getSessions().remove(this);
}
}
/**
* IBBInputStream class is the base implementation of an In-Band Bytestream input stream.
* Subclasses of this input stream must provide a packet listener along with a packet filter to
* collect the In-Band Bytestream data packets.
*/
private abstract class IBBInputStream extends InputStream {
/* the data packet listener to fill the data queue */
private final PacketListener dataPacketListener;
/* queue containing received In-Band Bytestream data packets */
protected final BlockingQueue<DataPacketExtension> dataQueue = new LinkedBlockingQueue<DataPacketExtension>();
/* buffer containing the data from one data packet */
private byte[] buffer;
/* pointer to the next byte to read from buffer */
private int bufferPointer = -1;
/* data packet sequence (range from 0 to 65535) */
private long seq = -1;
/* flag to indicate if input stream is closed */
private boolean isClosed = false;
/* flag to indicate if close method was invoked */
private boolean closeInvoked = false;
/* timeout for read operations */
private int readTimeout = 0;
/**
* Constructor.
*/
public IBBInputStream() {
// add data packet listener to connection
this.dataPacketListener = getDataPacketListener();
connection.addPacketListener(this.dataPacketListener, getDataPacketFilter());
}
/**
* Returns the packet listener that processes In-Band Bytestream data packets.
*
* @return the data packet listener
*/
protected abstract PacketListener getDataPacketListener();
/**
* Returns the packet filter that accepts In-Band Bytestream data packets.
*
* @return the data packet filter
*/
protected abstract PacketFilter getDataPacketFilter();
public synchronized int read() throws IOException {
checkClosed();
// if nothing read yet or whole buffer has been read fill buffer
if (bufferPointer == -1 || bufferPointer >= buffer.length) {
// if no data available and stream was closed return -1
if (!loadBuffer()) {
return -1;
}
}
// return byte and increment buffer pointer
return ((int) buffer[bufferPointer++]) & 0xff;
}
public synchronized int read(byte[] b, int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
}
else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length)
|| ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
}
else if (len == 0) {
return 0;
}
checkClosed();
// if nothing read yet or whole buffer has been read fill buffer
if (bufferPointer == -1 || bufferPointer >= buffer.length) {
// if no data available and stream was closed return -1
if (!loadBuffer()) {
return -1;
}
}
// if more bytes wanted than available return all available
int bytesAvailable = buffer.length - bufferPointer;
if (len > bytesAvailable) {
len = bytesAvailable;
}
System.arraycopy(buffer, bufferPointer, b, off, len);
bufferPointer += len;
return len;
}
public synchronized int read(byte[] b) throws IOException {
return read(b, 0, b.length);
}
/**
* This method blocks until a data packet is received, the stream is closed or the current
* thread is interrupted.
*
* @return <code>true</code> if data was received, otherwise <code>false</code>
* @throws IOException if data packets are out of sequence
*/
private synchronized boolean loadBuffer() throws IOException {
// wait until data is available or stream is closed
DataPacketExtension data = null;
try {
if (this.readTimeout == 0) {
while (data == null) {
if (isClosed && this.dataQueue.isEmpty()) {
return false;
}
data = this.dataQueue.poll(1000, TimeUnit.MILLISECONDS);
}
}
else {
data = this.dataQueue.poll(this.readTimeout, TimeUnit.MILLISECONDS);
if (data == null) {
throw new SocketTimeoutException();
}
}
}
catch (InterruptedException e) {
// Restore the interrupted status
Thread.currentThread().interrupt();
return false;
}
// handle sequence overflow
if (this.seq == 65535) {
this.seq = -1;
}
// check if data packets sequence is successor of last seen sequence
long seq = data.getSeq();
if (seq - 1 != this.seq) {
// packets out of order; close stream/session
InBandBytestreamSession.this.close();
throw new IOException("Packets out of sequence");
}
else {
this.seq = seq;
}
// set buffer to decoded data
buffer = data.getDecodedData();
bufferPointer = 0;
return true;
}
/**
* Checks if this stream is closed and throws an IOException if necessary
*
* @throws IOException if stream is closed and no data should be read anymore
*/
private void checkClosed() throws IOException {
/* throw no exception if there is data available, but not if close method was invoked */
if ((isClosed && this.dataQueue.isEmpty()) || closeInvoked) {
// clear data queue in case additional data was received after stream was closed
this.dataQueue.clear();
throw new IOException("Stream is closed");
}
}
public boolean markSupported() {
return false;
}
public void close() throws IOException {
if (isClosed) {
return;
}
this.closeInvoked = true;
InBandBytestreamSession.this.closeByLocal(true);
}
/**
* This method sets the close flag and removes the data packet listener.
*/
private void closeInternal() {
if (isClosed) {
return;
}
isClosed = true;
}
/**
* Invoked if the session is closed.
*/
private void cleanup() {
connection.removePacketListener(this.dataPacketListener);
}
}
/**
* IQIBBInputStream class implements IBBInputStream to be used with IQ stanzas encapsulating the
* data packets.
*/
private class IQIBBInputStream extends IBBInputStream {
protected PacketListener getDataPacketListener() {
return new PacketListener() {
private long lastSequence = -1;
public void processPacket(Packet packet) throws NotConnectedException {
// get data packet extension
DataPacketExtension data = (DataPacketExtension) packet.getExtension(
DataPacketExtension.ELEMENT_NAME,
InBandBytestreamManager.NAMESPACE);
/*
* check if sequence was not used already (see XEP-0047 Section 2.2)
*/
if (data.getSeq() <= this.lastSequence) {
IQ unexpectedRequest = IQ.createErrorResponse((IQ) packet, new XMPPError(
XMPPError.Condition.unexpected_request));
connection.sendPacket(unexpectedRequest);
return;
}
// check if encoded data is valid (see XEP-0047 Section 2.2)
if (data.getDecodedData() == null) {
// data is invalid; respond with bad-request error
IQ badRequest = IQ.createErrorResponse((IQ) packet, new XMPPError(
XMPPError.Condition.bad_request));
connection.sendPacket(badRequest);
return;
}
// data is valid; add to data queue
dataQueue.offer(data);
// confirm IQ
IQ confirmData = IQ.createResultIQ((IQ) packet);
connection.sendPacket(confirmData);
// set last seen sequence
this.lastSequence = data.getSeq();
if (this.lastSequence == 65535) {
this.lastSequence = -1;
}
}
};
}
protected PacketFilter getDataPacketFilter() {
/*
* filter all IQ stanzas having type 'SET' (represented by Data class), containing a
* data packet extension, matching session ID and recipient
*/
return new AndFilter(new PacketTypeFilter(Data.class), new IBBDataPacketFilter());
}
}
/**
* MessageIBBInputStream class implements IBBInputStream to be used with message stanzas
* encapsulating the data packets.
*/
private class MessageIBBInputStream extends IBBInputStream {
protected PacketListener getDataPacketListener() {
return new PacketListener() {
public void processPacket(Packet packet) {
// get data packet extension
DataPacketExtension data = (DataPacketExtension) packet.getExtension(
DataPacketExtension.ELEMENT_NAME,
InBandBytestreamManager.NAMESPACE);
// check if encoded data is valid
if (data.getDecodedData() == null) {
/*
* TODO once a majority of XMPP server implementation support XEP-0079
* Advanced Message Processing the invalid message could be answered with an
* appropriate error. For now we just ignore the packet. Subsequent packets
* with an increased sequence will cause the input stream to close the
* stream/session.
*/
return;
}
// data is valid; add to data queue
dataQueue.offer(data);
// TODO confirm packet once XMPP servers support XEP-0079
}
};
}
@Override
protected PacketFilter getDataPacketFilter() {
/*
* filter all message stanzas containing a data packet extension, matching session ID
* and recipient
*/
return new AndFilter(new PacketTypeFilter(Message.class), new IBBDataPacketFilter());
}
}
/**
* IBBDataPacketFilter class filters all packets from the remote peer of this session,
* containing an In-Band Bytestream data packet extension whose session ID matches this sessions
* ID.
*/
private class IBBDataPacketFilter implements PacketFilter {
public boolean accept(Packet packet) {
// sender equals remote peer
if (!packet.getFrom().equalsIgnoreCase(remoteJID)) {
return false;
}
// stanza contains data packet extension
PacketExtension packetExtension = packet.getExtension(DataPacketExtension.ELEMENT_NAME,
InBandBytestreamManager.NAMESPACE);
if (packetExtension == null || !(packetExtension instanceof DataPacketExtension)) {
return false;
}
// session ID equals this session ID
DataPacketExtension data = (DataPacketExtension) packetExtension;
if (!data.getSessionID().equals(byteStreamRequest.getSessionID())) {
return false;
}
return true;
}
}
/**
* IBBOutputStream class is the base implementation of an In-Band Bytestream output stream.
* Subclasses of this output stream must provide a method to send data over XMPP stream.
*/
private abstract class IBBOutputStream extends OutputStream {
/* buffer with the size of this sessions block size */
protected final byte[] buffer;
/* pointer to next byte to write to buffer */
protected int bufferPointer = 0;
/* data packet sequence (range from 0 to 65535) */
protected long seq = 0;
/* flag to indicate if output stream is closed */
protected boolean isClosed = false;
/**
* Constructor.
*/
public IBBOutputStream() {
this.buffer = new byte[byteStreamRequest.getBlockSize()];
}
/**
* Writes the given data packet to the XMPP stream.
*
* @param data the data packet
* @throws IOException if an I/O error occurred while sending or if the stream is closed
* @throws NotConnectedException
*/
protected abstract void writeToXML(DataPacketExtension data) throws IOException, NotConnectedException;
public synchronized void write(int b) throws IOException {
if (this.isClosed) {
throw new IOException("Stream is closed");
}
// if buffer is full flush buffer
if (bufferPointer >= buffer.length) {
flushBuffer();
}
buffer[bufferPointer++] = (byte) b;
}
public synchronized void write(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
}
else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length)
|| ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
}
else if (len == 0) {
return;
}
if (this.isClosed) {
throw new IOException("Stream is closed");
}
// is data to send greater than buffer size
if (len >= buffer.length) {
// "byte" off the first chunk to write out
writeOut(b, off, buffer.length);
// recursively call this method with the lesser amount
write(b, off + buffer.length, len - buffer.length);
}
else {
writeOut(b, off, len);
}
}
public synchronized void write(byte[] b) throws IOException {
write(b, 0, b.length);
}
/**
* Fills the buffer with the given data and sends it over the XMPP stream if the buffers
* capacity has been reached. This method is only called from this class so it is assured
* that the amount of data to send is <= buffer capacity
*
* @param b the data
* @param off the data
* @param len the number of bytes to write
* @throws IOException if an I/O error occurred while sending or if the stream is closed
*/
private synchronized void writeOut(byte b[], int off, int len) throws IOException {
if (this.isClosed) {
throw new IOException("Stream is closed");
}
// set to 0 in case the next 'if' block is not executed
int available = 0;
// is data to send greater that buffer space left
if (len > buffer.length - bufferPointer) {
// fill buffer to capacity and send it
available = buffer.length - bufferPointer;
System.arraycopy(b, off, buffer, bufferPointer, available);
bufferPointer += available;
flushBuffer();
}
// copy the data left to buffer
System.arraycopy(b, off + available, buffer, bufferPointer, len - available);
bufferPointer += len - available;
}
public synchronized void flush() throws IOException {
if (this.isClosed) {
throw new IOException("Stream is closed");
}
flushBuffer();
}
private synchronized void flushBuffer() throws IOException {
// do nothing if no data to send available
if (bufferPointer == 0) {
return;
}
// create data packet
String enc = StringUtils.encodeBase64(buffer, 0, bufferPointer, false);
DataPacketExtension data = new DataPacketExtension(byteStreamRequest.getSessionID(),
this.seq, enc);
// write to XMPP stream
try {
writeToXML(data);
}
catch (NotConnectedException e) {
IOException ioException = new IOException();
ioException.initCause(e);
throw ioException;
}
// reset buffer pointer
bufferPointer = 0;
// increment sequence, considering sequence overflow
this.seq = (this.seq + 1 == 65535 ? 0 : this.seq + 1);
}
public void close() throws IOException {
if (isClosed) {
return;
}
InBandBytestreamSession.this.closeByLocal(false);
}
/**
* Sets the close flag and optionally flushes the stream.
*
* @param flush if <code>true</code> flushes the stream
*/
protected void closeInternal(boolean flush) {
if (this.isClosed) {
return;
}
this.isClosed = true;
try {
if (flush) {
flushBuffer();
}
}
catch (IOException e) {
/*
* ignore, because writeToXML() will not throw an exception if stream is already
* closed
*/
}
}
}
/**
* IQIBBOutputStream class implements IBBOutputStream to be used with IQ stanzas encapsulating
* the data packets.
*/
private class IQIBBOutputStream extends IBBOutputStream {
@Override
protected synchronized void writeToXML(DataPacketExtension data) throws IOException {
// create IQ stanza containing data packet
IQ iq = new Data(data);
iq.setTo(remoteJID);
try {
connection.createPacketCollectorAndSend(iq).nextResultOrThrow();
}
catch (Exception e) {
// close session unless it is already closed
if (!this.isClosed) {
InBandBytestreamSession.this.close();
// Sadly we are unable to use the IOException(Throwable) constructor because this
// constructor is only supported from Android API 9 on.
IOException ioException = new IOException();
ioException.initCause(e);
throw ioException;
}
}
}
}
/**
* MessageIBBOutputStream class implements IBBOutputStream to be used with message stanzas
* encapsulating the data packets.
*/
private class MessageIBBOutputStream extends IBBOutputStream {
@Override
protected synchronized void writeToXML(DataPacketExtension data) throws NotConnectedException {
// create message stanza containing data packet
Message message = new Message(remoteJID);
message.addExtension(data);
connection.sendPacket(message);
}
}
}

View file

@ -0,0 +1,140 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.ibb;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.bytestreams.BytestreamListener;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
/**
* InitiationListener handles all incoming In-Band Bytestream open requests. If there are no
* listeners for a In-Band Bytestream request InitiationListener will always refuse the request and
* reply with a &lt;not-acceptable/&gt; error (<a
* href="http://xmpp.org/extensions/xep-0047.html#example-5" >XEP-0047</a> Section 2.1).
* <p>
* All In-Band Bytestream request having a block size greater than the maximum allowed block size
* for this connection are rejected with an &lt;resource-constraint/&gt; error. The maximum block
* size can be set by invoking {@link InBandBytestreamManager#setMaximumBlockSize(int)}.
*
* @author Henning Staib
*/
class InitiationListener implements PacketListener {
private static final Logger LOGGER = Logger.getLogger(InitiationListener.class.getName());
/* manager containing the listeners and the XMPP connection */
private final InBandBytestreamManager manager;
/* packet filter for all In-Band Bytestream requests */
private final PacketFilter initFilter = new AndFilter(new PacketTypeFilter(Open.class),
new IQTypeFilter(IQ.Type.SET));
/* executor service to process incoming requests concurrently */
private final ExecutorService initiationListenerExecutor;
/**
* Constructor.
*
* @param manager the In-Band Bytestream manager
*/
protected InitiationListener(InBandBytestreamManager manager) {
this.manager = manager;
initiationListenerExecutor = Executors.newCachedThreadPool();
}
public void processPacket(final Packet packet) {
initiationListenerExecutor.execute(new Runnable() {
public void run() {
try {
processRequest(packet);
}
catch (NotConnectedException e) {
LOGGER.log(Level.WARNING, "proccessRequest", e);
}
}
});
}
private void processRequest(Packet packet) throws NotConnectedException {
Open ibbRequest = (Open) packet;
// validate that block size is within allowed range
if (ibbRequest.getBlockSize() > this.manager.getMaximumBlockSize()) {
this.manager.replyResourceConstraintPacket(ibbRequest);
return;
}
// ignore request if in ignore list
if (this.manager.getIgnoredBytestreamRequests().remove(ibbRequest.getSessionID()))
return;
// build bytestream request from packet
InBandBytestreamRequest request = new InBandBytestreamRequest(this.manager, ibbRequest);
// notify listeners for bytestream initiation from a specific user
BytestreamListener userListener = this.manager.getUserListener(ibbRequest.getFrom());
if (userListener != null) {
userListener.incomingBytestreamRequest(request);
}
else if (!this.manager.getAllRequestListeners().isEmpty()) {
/*
* if there is no user specific listener inform listeners for all initiation requests
*/
for (BytestreamListener listener : this.manager.getAllRequestListeners()) {
listener.incomingBytestreamRequest(request);
}
}
else {
/*
* if there is no listener for this initiation request, reply with reject message
*/
this.manager.replyRejectPacket(ibbRequest);
}
}
/**
* Returns the packet filter for In-Band Bytestream open requests.
*
* @return the packet filter for In-Band Bytestream open requests
*/
protected PacketFilter getFilter() {
return this.initFilter;
}
/**
* Shuts down the listeners executor service.
*/
protected void shutdown() {
this.initiationListenerExecutor.shutdownNow();
}
}

View file

@ -0,0 +1,68 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.ibb.packet;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
/**
* Represents a request to close an In-Band Bytestream.
*
* @author Henning Staib
*/
public class Close extends IQ {
/* unique session ID identifying this In-Band Bytestream */
private final String sessionID;
/**
* Creates a new In-Band Bytestream close request packet.
*
* @param sessionID unique session ID identifying this In-Band Bytestream
*/
public Close(String sessionID) {
if (sessionID == null || "".equals(sessionID)) {
throw new IllegalArgumentException("Session ID must not be null or empty");
}
this.sessionID = sessionID;
setType(Type.SET);
}
/**
* Returns the unique session ID identifying this In-Band Bytestream.
*
* @return the unique session ID identifying this In-Band Bytestream
*/
public String getSessionID() {
return sessionID;
}
@Override
public String getChildElementXML() {
StringBuilder buf = new StringBuilder();
buf.append("<close ");
buf.append("xmlns=\"");
buf.append(InBandBytestreamManager.NAMESPACE);
buf.append("\" ");
buf.append("sid=\"");
buf.append(sessionID);
buf.append("\"");
buf.append("/>");
return buf.toString();
}
}

View file

@ -0,0 +1,67 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.ibb.packet;
import org.jivesoftware.smack.packet.IQ;
/**
* Represents a chunk of data sent over an In-Band Bytestream encapsulated in an
* IQ stanza.
*
* @author Henning Staib
*/
public class Data extends IQ {
/* the data packet extension */
private final DataPacketExtension dataPacketExtension;
/**
* Constructor.
*
* @param data data packet extension containing the encoded data
*/
public Data(DataPacketExtension data) {
if (data == null) {
throw new IllegalArgumentException("Data must not be null");
}
this.dataPacketExtension = data;
/*
* also set as packet extension so that data packet extension can be
* retrieved from IQ stanza and message stanza in the same way
*/
addExtension(data);
setType(IQ.Type.SET);
}
/**
* Returns the data packet extension.
* <p>
* Convenience method for <code>packet.getExtension("data",
* "http://jabber.org/protocol/ibb")</code>.
*
* @return the data packet extension
*/
public DataPacketExtension getDataPacketExtension() {
return this.dataPacketExtension;
}
public String getChildElementXML() {
return this.dataPacketExtension.toXML();
}
}

View file

@ -0,0 +1,152 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.ibb.packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
/**
* Represents a chunk of data of an In-Band Bytestream within an IQ stanza or a
* message stanza
*
* @author Henning Staib
*/
public class DataPacketExtension implements PacketExtension {
/**
* The element name of the data packet extension.
*/
public final static String ELEMENT_NAME = "data";
/* unique session ID identifying this In-Band Bytestream */
private final String sessionID;
/* sequence of this packet in regard to the other data packets */
private final long seq;
/* the data contained in this packet */
private final String data;
private byte[] decodedData;
/**
* Creates a new In-Band Bytestream data packet.
*
* @param sessionID unique session ID identifying this In-Band Bytestream
* @param seq sequence of this packet in regard to the other data packets
* @param data the base64 encoded data contained in this packet
*/
public DataPacketExtension(String sessionID, long seq, String data) {
if (sessionID == null || "".equals(sessionID)) {
throw new IllegalArgumentException("Session ID must not be null or empty");
}
if (seq < 0 || seq > 65535) {
throw new IllegalArgumentException("Sequence must not be between 0 and 65535");
}
if (data == null) {
throw new IllegalArgumentException("Data must not be null");
}
this.sessionID = sessionID;
this.seq = seq;
this.data = data;
}
/**
* Returns the unique session ID identifying this In-Band Bytestream.
*
* @return the unique session ID identifying this In-Band Bytestream
*/
public String getSessionID() {
return sessionID;
}
/**
* Returns the sequence of this packet in regard to the other data packets.
*
* @return the sequence of this packet in regard to the other data packets.
*/
public long getSeq() {
return seq;
}
/**
* Returns the data contained in this packet.
*
* @return the data contained in this packet.
*/
public String getData() {
return data;
}
/**
* Returns the decoded data or null if data could not be decoded.
* <p>
* The encoded data is invalid if it contains bad Base64 input characters or
* if it contains the pad ('=') character on a position other than the last
* character(s) of the data. See <a
* href="http://xmpp.org/extensions/xep-0047.html#sec">XEP-0047</a> Section
* 6.
*
* @return the decoded data
*/
public byte[] getDecodedData() {
// return cached decoded data
if (this.decodedData != null) {
return this.decodedData;
}
// data must not contain the pad (=) other than end of data
if (data.matches(".*={1,2}+.+")) {
return null;
}
// decodeBase64 will return null if bad characters are included
this.decodedData = StringUtils.decodeBase64(data);
return this.decodedData;
}
public String getElementName() {
return ELEMENT_NAME;
}
public String getNamespace() {
return InBandBytestreamManager.NAMESPACE;
}
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<");
buf.append(getElementName());
buf.append(" ");
buf.append("xmlns=\"");
buf.append(InBandBytestreamManager.NAMESPACE);
buf.append("\" ");
buf.append("seq=\"");
buf.append(seq);
buf.append("\" ");
buf.append("sid=\"");
buf.append(sessionID);
buf.append("\">");
buf.append(data);
buf.append("</");
buf.append(getElementName());
buf.append(">");
return buf.toString();
}
}

View file

@ -0,0 +1,131 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.ibb.packet;
import java.util.Locale;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager.StanzaType;
/**
* Represents a request to open an In-Band Bytestream.
*
* @author Henning Staib
*/
public class Open extends IQ {
/* unique session ID identifying this In-Band Bytestream */
private final String sessionID;
/* block size in which the data will be fragmented */
private final int blockSize;
/* stanza type used to encapsulate the data */
private final StanzaType stanza;
/**
* Creates a new In-Band Bytestream open request packet.
* <p>
* The data sent over this In-Band Bytestream will be fragmented in blocks
* with the given block size. The block size should not be greater than
* 65535. A recommended default value is 4096.
* <p>
* The data can be sent using IQ stanzas or message stanzas.
*
* @param sessionID unique session ID identifying this In-Band Bytestream
* @param blockSize block size in which the data will be fragmented
* @param stanza stanza type used to encapsulate the data
*/
public Open(String sessionID, int blockSize, StanzaType stanza) {
if (sessionID == null || "".equals(sessionID)) {
throw new IllegalArgumentException("Session ID must not be null or empty");
}
if (blockSize <= 0) {
throw new IllegalArgumentException("Block size must be greater than zero");
}
this.sessionID = sessionID;
this.blockSize = blockSize;
this.stanza = stanza;
setType(Type.SET);
}
/**
* Creates a new In-Band Bytestream open request packet.
* <p>
* The data sent over this In-Band Bytestream will be fragmented in blocks
* with the given block size. The block size should not be greater than
* 65535. A recommended default value is 4096.
* <p>
* The data will be sent using IQ stanzas.
*
* @param sessionID unique session ID identifying this In-Band Bytestream
* @param blockSize block size in which the data will be fragmented
*/
public Open(String sessionID, int blockSize) {
this(sessionID, blockSize, StanzaType.IQ);
}
/**
* Returns the unique session ID identifying this In-Band Bytestream.
*
* @return the unique session ID identifying this In-Band Bytestream
*/
public String getSessionID() {
return sessionID;
}
/**
* Returns the block size in which the data will be fragmented.
*
* @return the block size in which the data will be fragmented
*/
public int getBlockSize() {
return blockSize;
}
/**
* Returns the stanza type used to encapsulate the data.
*
* @return the stanza type used to encapsulate the data
*/
public StanzaType getStanza() {
return stanza;
}
@Override
public String getChildElementXML() {
StringBuilder buf = new StringBuilder();
buf.append("<open ");
buf.append("xmlns=\"");
buf.append(InBandBytestreamManager.NAMESPACE);
buf.append("\" ");
buf.append("block-size=\"");
buf.append(blockSize);
buf.append("\" ");
buf.append("sid=\"");
buf.append(sessionID);
buf.append("\" ");
buf.append("stanza=\"");
buf.append(stanza.toString().toLowerCase(Locale.US));
buf.append("\"");
buf.append("/>");
return buf.toString();
}
}

View file

@ -0,0 +1,36 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.ibb.provider;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Close;
import org.xmlpull.v1.XmlPullParser;
/**
* Parses a close In-Band Bytestream packet.
*
* @author Henning Staib
*/
public class CloseIQProvider implements IQProvider {
public IQ parseIQ(XmlPullParser parser) throws Exception {
String sid = parser.getAttributeValue("", "sid");
return new Close(sid);
}
}

View file

@ -0,0 +1,48 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.ibb.provider;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Data;
import org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension;
import org.xmlpull.v1.XmlPullParser;
/**
* Parses an In-Band Bytestream data packet which can be a packet extension of
* either an IQ stanza or a message stanza.
*
* @author Henning Staib
*/
public class DataPacketProvider implements PacketExtensionProvider, IQProvider {
public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
String sessionID = parser.getAttributeValue("", "sid");
long seq = Long.parseLong(parser.getAttributeValue("", "seq"));
String data = parser.nextText();
return new DataPacketExtension(sessionID, seq, data);
}
public IQ parseIQ(XmlPullParser parser) throws Exception {
DataPacketExtension data = (DataPacketExtension) parseExtension(parser);
IQ iq = new Data(data);
return iq;
}
}

View file

@ -0,0 +1,50 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.ibb.provider;
import java.util.Locale;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager.StanzaType;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
import org.xmlpull.v1.XmlPullParser;
/**
* Parses an In-Band Bytestream open packet.
*
* @author Henning Staib
*/
public class OpenIQProvider implements IQProvider {
public IQ parseIQ(XmlPullParser parser) throws Exception {
String sessionID = parser.getAttributeValue("", "sid");
int blockSize = Integer.parseInt(parser.getAttributeValue("", "block-size"));
String stanzaValue = parser.getAttributeValue("", "stanza");
StanzaType stanza = null;
if (stanzaValue == null) {
stanza = StanzaType.IQ;
}
else {
stanza = StanzaType.valueOf(stanzaValue.toUpperCase(Locale.US));
}
return new Open(sessionID, blockSize, stanza);
}
}

View file

@ -0,0 +1,131 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.socks5;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.bytestreams.BytestreamListener;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
/**
* InitiationListener handles all incoming SOCKS5 Bytestream initiation requests. If there are no
* listeners for a SOCKS5 bytestream request InitiationListener will always refuse the request and
* reply with a &lt;not-acceptable/&gt; error (<a
* href="http://xmpp.org/extensions/xep-0065.html#usecase-alternate">XEP-0065</a> Section 5.2.A2).
*
* @author Henning Staib
*/
final class InitiationListener implements PacketListener {
private static final Logger LOGGER = Logger.getLogger(InitiationListener.class.getName());
/* manager containing the listeners and the XMPP connection */
private final Socks5BytestreamManager manager;
/* packet filter for all SOCKS5 Bytestream requests */
private final PacketFilter initFilter = new AndFilter(new PacketTypeFilter(Bytestream.class),
new IQTypeFilter(IQ.Type.SET));
/* executor service to process incoming requests concurrently */
private final ExecutorService initiationListenerExecutor;
/**
* Constructor
*
* @param manager the SOCKS5 Bytestream manager
*/
protected InitiationListener(Socks5BytestreamManager manager) {
this.manager = manager;
initiationListenerExecutor = Executors.newCachedThreadPool();
}
public void processPacket(final Packet packet) {
initiationListenerExecutor.execute(new Runnable() {
public void run() {
try {
processRequest(packet);
}
catch (NotConnectedException e) {
LOGGER.log(Level.WARNING, "process request", e);
}
}
});
}
private void processRequest(Packet packet) throws NotConnectedException {
Bytestream byteStreamRequest = (Bytestream) packet;
// ignore request if in ignore list
if (this.manager.getIgnoredBytestreamRequests().remove(byteStreamRequest.getSessionID())) {
return;
}
// build bytestream request from packet
Socks5BytestreamRequest request = new Socks5BytestreamRequest(this.manager,
byteStreamRequest);
// notify listeners for bytestream initiation from a specific user
BytestreamListener userListener = this.manager.getUserListener(byteStreamRequest.getFrom());
if (userListener != null) {
userListener.incomingBytestreamRequest(request);
}
else if (!this.manager.getAllRequestListeners().isEmpty()) {
/*
* if there is no user specific listener inform listeners for all initiation requests
*/
for (BytestreamListener listener : this.manager.getAllRequestListeners()) {
listener.incomingBytestreamRequest(request);
}
}
else {
/*
* if there is no listener for this initiation request, reply with reject message
*/
this.manager.replyRejectPacket(byteStreamRequest);
}
}
/**
* Returns the packet filter for SOCKS5 Bytestream initialization requests.
*
* @return the packet filter for SOCKS5 Bytestream initialization requests
*/
protected PacketFilter getFilter() {
return this.initFilter;
}
/**
* Shuts down the listeners executor service.
*/
protected void shutdown() {
this.initiationListenerExecutor.shutdownNow();
}
}

View file

@ -0,0 +1,46 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.socks5;
import org.jivesoftware.smackx.bytestreams.BytestreamListener;
import org.jivesoftware.smackx.bytestreams.BytestreamRequest;
/**
* Socks5BytestreamListener are informed if a remote user wants to initiate a SOCKS5 Bytestream.
* Implement this interface to handle incoming SOCKS5 Bytestream requests.
* <p>
* There are two ways to add this listener. See
* {@link Socks5BytestreamManager#addIncomingBytestreamListener(BytestreamListener)} and
* {@link Socks5BytestreamManager#addIncomingBytestreamListener(BytestreamListener, String)} for
* further details.
*
* @author Henning Staib
*/
public abstract class Socks5BytestreamListener implements BytestreamListener {
public void incomingBytestreamRequest(BytestreamRequest request) {
incomingBytestreamRequest((Socks5BytestreamRequest) request);
}
/**
* This listener is notified if a SOCKS5 Bytestream request from another user has been received.
*
* @param request the incoming SOCKS5 Bytestream request
*/
public abstract void incomingBytestreamRequest(Socks5BytestreamRequest request);
}

View file

@ -0,0 +1,790 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.socks5;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeoutException;
import org.jivesoftware.smack.AbstractConnectionListener;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.FeatureNotSupportedException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smackx.bytestreams.BytestreamListener;
import org.jivesoftware.smackx.bytestreams.BytestreamManager;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHostUsed;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
import org.jivesoftware.smackx.disco.packet.DiscoverItems;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity;
import org.jivesoftware.smackx.disco.packet.DiscoverItems.Item;
import org.jivesoftware.smackx.filetransfer.FileTransferManager;
/**
* The Socks5BytestreamManager class handles establishing SOCKS5 Bytestreams as specified in the <a
* href="http://xmpp.org/extensions/xep-0065.html">XEP-0065</a>.
* <p>
* A SOCKS5 Bytestream is negotiated partly over the XMPP XML stream and partly over a separate
* socket. The actual transfer though takes place over a separately created socket.
* <p>
* A SOCKS5 Bytestream generally has three parties, the initiator, the target, and the stream host.
* The stream host is a specialized SOCKS5 proxy setup on a server, or, the initiator can act as the
* stream host.
* <p>
* To establish a SOCKS5 Bytestream invoke the {@link #establishSession(String)} method. This will
* negotiate a SOCKS5 Bytestream with the given target JID and return a socket.
* <p>
* If a session ID for the SOCKS5 Bytestream was already negotiated (e.g. while negotiating a file
* transfer) invoke {@link #establishSession(String, String)}.
* <p>
* To handle incoming SOCKS5 Bytestream requests add an {@link Socks5BytestreamListener} to the
* manager. There are two ways to add this listener. If you want to be informed about incoming
* SOCKS5 Bytestreams from a specific user add the listener by invoking
* {@link #addIncomingBytestreamListener(BytestreamListener, String)}. If the listener should
* respond to all SOCKS5 Bytestream requests invoke
* {@link #addIncomingBytestreamListener(BytestreamListener)}.
* <p>
* Note that the registered {@link Socks5BytestreamListener} will NOT be notified on incoming Socks5
* bytestream requests sent in the context of <a
* href="http://xmpp.org/extensions/xep-0096.html">XEP-0096</a> file transfer. (See
* {@link FileTransferManager})
* <p>
* If no {@link Socks5BytestreamListener}s are registered, all incoming SOCKS5 Bytestream requests
* will be rejected by returning a &lt;not-acceptable/&gt; error to the initiator.
*
* @author Henning Staib
*/
public final class Socks5BytestreamManager implements BytestreamManager {
/*
* create a new Socks5BytestreamManager and register a shutdown listener on every established
* connection
*/
static {
XMPPConnection.addConnectionCreationListener(new ConnectionCreationListener() {
public void connectionCreated(final XMPPConnection connection) {
// create the manager for this connection
Socks5BytestreamManager.getBytestreamManager(connection);
// register shutdown listener
connection.addConnectionListener(new AbstractConnectionListener() {
@Override
public void connectionClosed() {
Socks5BytestreamManager.getBytestreamManager(connection).disableService();
}
@Override
public void connectionClosedOnError(Exception e) {
Socks5BytestreamManager.getBytestreamManager(connection).disableService();
}
@Override
public void reconnectionSuccessful() {
// re-create the manager for this connection
Socks5BytestreamManager.getBytestreamManager(connection);
}
});
}
});
}
/**
* The XMPP namespace of the SOCKS5 Bytestream
*/
public static final String NAMESPACE = "http://jabber.org/protocol/bytestreams";
/* prefix used to generate session IDs */
private static final String SESSION_ID_PREFIX = "js5_";
/* random generator to create session IDs */
private final static Random randomGenerator = new Random();
/* stores one Socks5BytestreamManager for each XMPP connection */
private final static Map<XMPPConnection, Socks5BytestreamManager> managers = new HashMap<XMPPConnection, Socks5BytestreamManager>();
/* XMPP connection */
private final XMPPConnection connection;
/*
* assigns a user to a listener that is informed if a bytestream request for this user is
* received
*/
private final Map<String, BytestreamListener> userListeners = new ConcurrentHashMap<String, BytestreamListener>();
/*
* list of listeners that respond to all bytestream requests if there are not user specific
* listeners for that request
*/
private final List<BytestreamListener> allRequestListeners = Collections.synchronizedList(new LinkedList<BytestreamListener>());
/* listener that handles all incoming bytestream requests */
private final InitiationListener initiationListener;
/* timeout to wait for the response to the SOCKS5 Bytestream initialization request */
private int targetResponseTimeout = 10000;
/* timeout for connecting to the SOCKS5 proxy selected by the target */
private int proxyConnectionTimeout = 10000;
/* blacklist of errornous SOCKS5 proxies */
private final List<String> proxyBlacklist = Collections.synchronizedList(new LinkedList<String>());
/* remember the last proxy that worked to prioritize it */
private String lastWorkingProxy = null;
/* flag to enable/disable prioritization of last working proxy */
private boolean proxyPrioritizationEnabled = true;
/*
* list containing session IDs of SOCKS5 Bytestream initialization packets that should be
* ignored by the InitiationListener
*/
private List<String> ignoredBytestreamRequests = Collections.synchronizedList(new LinkedList<String>());
/**
* Returns the Socks5BytestreamManager to handle SOCKS5 Bytestreams for a given
* {@link XMPPConnection}.
* <p>
* If no manager exists a new is created and initialized.
*
* @param connection the XMPP connection or <code>null</code> if given connection is
* <code>null</code>
* @return the Socks5BytestreamManager for the given XMPP connection
*/
public static synchronized Socks5BytestreamManager getBytestreamManager(XMPPConnection connection) {
if (connection == null) {
return null;
}
Socks5BytestreamManager manager = managers.get(connection);
if (manager == null) {
manager = new Socks5BytestreamManager(connection);
managers.put(connection, manager);
manager.activate();
}
return manager;
}
/**
* Private constructor.
*
* @param connection the XMPP connection
*/
private Socks5BytestreamManager(XMPPConnection connection) {
this.connection = connection;
this.initiationListener = new InitiationListener(this);
}
/**
* Adds BytestreamListener that is called for every incoming SOCKS5 Bytestream request unless
* there is a user specific BytestreamListener registered.
* <p>
* If no listeners are registered all SOCKS5 Bytestream request are rejected with a
* &lt;not-acceptable/&gt; error.
* <p>
* Note that the registered {@link BytestreamListener} will NOT be notified on incoming Socks5
* bytestream requests sent in the context of <a
* href="http://xmpp.org/extensions/xep-0096.html">XEP-0096</a> file transfer. (See
* {@link FileTransferManager})
*
* @param listener the listener to register
*/
public void addIncomingBytestreamListener(BytestreamListener listener) {
this.allRequestListeners.add(listener);
}
/**
* Removes the given listener from the list of listeners for all incoming SOCKS5 Bytestream
* requests.
*
* @param listener the listener to remove
*/
public void removeIncomingBytestreamListener(BytestreamListener listener) {
this.allRequestListeners.remove(listener);
}
/**
* Adds BytestreamListener that is called for every incoming SOCKS5 Bytestream request from the
* given user.
* <p>
* Use this method if you are awaiting an incoming SOCKS5 Bytestream request from a specific
* user.
* <p>
* If no listeners are registered all SOCKS5 Bytestream request are rejected with a
* &lt;not-acceptable/&gt; error.
* <p>
* Note that the registered {@link BytestreamListener} will NOT be notified on incoming Socks5
* bytestream requests sent in the context of <a
* href="http://xmpp.org/extensions/xep-0096.html">XEP-0096</a> file transfer. (See
* {@link FileTransferManager})
*
* @param listener the listener to register
* @param initiatorJID the JID of the user that wants to establish a SOCKS5 Bytestream
*/
public void addIncomingBytestreamListener(BytestreamListener listener, String initiatorJID) {
this.userListeners.put(initiatorJID, listener);
}
/**
* Removes the listener for the given user.
*
* @param initiatorJID the JID of the user the listener should be removed
*/
public void removeIncomingBytestreamListener(String initiatorJID) {
this.userListeners.remove(initiatorJID);
}
/**
* Use this method to ignore the next incoming SOCKS5 Bytestream request containing the given
* session ID. No listeners will be notified for this request and and no error will be returned
* to the initiator.
* <p>
* This method should be used if you are awaiting a SOCKS5 Bytestream request as a reply to
* another packet (e.g. file transfer).
*
* @param sessionID to be ignored
*/
public void ignoreBytestreamRequestOnce(String sessionID) {
this.ignoredBytestreamRequests.add(sessionID);
}
/**
* Disables the SOCKS5 Bytestream manager by removing the SOCKS5 Bytestream feature from the
* service discovery, disabling the listener for SOCKS5 Bytestream initiation requests and
* resetting its internal state, which includes removing this instance from the managers map.
* <p>
* To re-enable the SOCKS5 Bytestream feature invoke {@link #getBytestreamManager(XMPPConnection)}.
* Using the file transfer API will automatically re-enable the SOCKS5 Bytestream feature.
*/
public synchronized void disableService() {
// remove initiation packet listener
this.connection.removePacketListener(this.initiationListener);
// shutdown threads
this.initiationListener.shutdown();
// clear listeners
this.allRequestListeners.clear();
this.userListeners.clear();
// reset internal state
this.lastWorkingProxy = null;
this.proxyBlacklist.clear();
this.ignoredBytestreamRequests.clear();
// remove manager from static managers map
managers.remove(this.connection);
// shutdown local SOCKS5 proxy if there are no more managers for other connections
if (managers.size() == 0) {
Socks5Proxy.getSocks5Proxy().stop();
}
// remove feature from service discovery
ServiceDiscoveryManager serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(this.connection);
// check if service discovery is not already disposed by connection shutdown
if (serviceDiscoveryManager != null) {
serviceDiscoveryManager.removeFeature(NAMESPACE);
}
}
/**
* Returns the timeout to wait for the response to the SOCKS5 Bytestream initialization request.
* Default is 10000ms.
*
* @return the timeout to wait for the response to the SOCKS5 Bytestream initialization request
*/
public int getTargetResponseTimeout() {
if (this.targetResponseTimeout <= 0) {
this.targetResponseTimeout = 10000;
}
return targetResponseTimeout;
}
/**
* Sets the timeout to wait for the response to the SOCKS5 Bytestream initialization request.
* Default is 10000ms.
*
* @param targetResponseTimeout the timeout to set
*/
public void setTargetResponseTimeout(int targetResponseTimeout) {
this.targetResponseTimeout = targetResponseTimeout;
}
/**
* Returns the timeout for connecting to the SOCKS5 proxy selected by the target. Default is
* 10000ms.
*
* @return the timeout for connecting to the SOCKS5 proxy selected by the target
*/
public int getProxyConnectionTimeout() {
if (this.proxyConnectionTimeout <= 0) {
this.proxyConnectionTimeout = 10000;
}
return proxyConnectionTimeout;
}
/**
* Sets the timeout for connecting to the SOCKS5 proxy selected by the target. Default is
* 10000ms.
*
* @param proxyConnectionTimeout the timeout to set
*/
public void setProxyConnectionTimeout(int proxyConnectionTimeout) {
this.proxyConnectionTimeout = proxyConnectionTimeout;
}
/**
* Returns if the prioritization of the last working SOCKS5 proxy on successive SOCKS5
* Bytestream connections is enabled. Default is <code>true</code>.
*
* @return <code>true</code> if prioritization is enabled, <code>false</code> otherwise
*/
public boolean isProxyPrioritizationEnabled() {
return proxyPrioritizationEnabled;
}
/**
* Enable/disable the prioritization of the last working SOCKS5 proxy on successive SOCKS5
* Bytestream connections.
*
* @param proxyPrioritizationEnabled enable/disable the prioritization of the last working
* SOCKS5 proxy
*/
public void setProxyPrioritizationEnabled(boolean proxyPrioritizationEnabled) {
this.proxyPrioritizationEnabled = proxyPrioritizationEnabled;
}
/**
* Establishes a SOCKS5 Bytestream with the given user and returns the Socket to send/receive
* data to/from the user.
* <p>
* Use this method to establish SOCKS5 Bytestreams to users accepting all incoming Socks5
* bytestream requests since this method doesn't provide a way to tell the user something about
* the data to be sent.
* <p>
* To establish a SOCKS5 Bytestream after negotiation the kind of data to be sent (e.g. file
* transfer) use {@link #establishSession(String, String)}.
*
* @param targetJID the JID of the user a SOCKS5 Bytestream should be established
* @return the Socket to send/receive data to/from the user
* @throws XMPPException if the user doesn't support or accept SOCKS5 Bytestreams, if no Socks5
* Proxy could be found, if the user couldn't connect to any of the SOCKS5 Proxies
* @throws IOException if the bytestream could not be established
* @throws InterruptedException if the current thread was interrupted while waiting
* @throws SmackException if there was no response from the server.
*/
public Socks5BytestreamSession establishSession(String targetJID) throws XMPPException,
IOException, InterruptedException, SmackException {
String sessionID = getNextSessionID();
return establishSession(targetJID, sessionID);
}
/**
* Establishes a SOCKS5 Bytestream with the given user using the given session ID and returns
* the Socket to send/receive data to/from the user.
*
* @param targetJID the JID of the user a SOCKS5 Bytestream should be established
* @param sessionID the session ID for the SOCKS5 Bytestream request
* @return the Socket to send/receive data to/from the user
* @throws IOException if the bytestream could not be established
* @throws InterruptedException if the current thread was interrupted while waiting
* @throws NoResponseException
* @throws SmackException if the target does not support SOCKS5.
* @throws XMPPException
*/
public Socks5BytestreamSession establishSession(String targetJID, String sessionID)
throws IOException, InterruptedException, NoResponseException, SmackException, XMPPException{
XMPPErrorException discoveryException = null;
// check if target supports SOCKS5 Bytestream
if (!supportsSocks5(targetJID)) {
throw new FeatureNotSupportedException("SOCKS5 Bytestream", targetJID);
}
List<String> proxies = new ArrayList<String>();
// determine SOCKS5 proxies from XMPP-server
try {
proxies.addAll(determineProxies());
} catch (XMPPErrorException e) {
// don't abort here, just remember the exception thrown by determineProxies()
// determineStreamHostInfos() will at least add the local Socks5 proxy (if enabled)
discoveryException = e;
}
// determine address and port of each proxy
List<StreamHost> streamHosts = determineStreamHostInfos(proxies);
if (streamHosts.isEmpty()) {
if (discoveryException != null) {
throw discoveryException;
} else {
throw new SmackException("no SOCKS5 proxies available");
}
}
// compute digest
String digest = Socks5Utils.createDigest(sessionID, this.connection.getUser(), targetJID);
// prioritize last working SOCKS5 proxy if exists
if (this.proxyPrioritizationEnabled && this.lastWorkingProxy != null) {
StreamHost selectedStreamHost = null;
for (StreamHost streamHost : streamHosts) {
if (streamHost.getJID().equals(this.lastWorkingProxy)) {
selectedStreamHost = streamHost;
break;
}
}
if (selectedStreamHost != null) {
streamHosts.remove(selectedStreamHost);
streamHosts.add(0, selectedStreamHost);
}
}
Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
try {
// add transfer digest to local proxy to make transfer valid
socks5Proxy.addTransfer(digest);
// create initiation packet
Bytestream initiation = createBytestreamInitiation(sessionID, targetJID, streamHosts);
// send initiation packet
Packet response = connection.createPacketCollectorAndSend(initiation).nextResultOrThrow(
getTargetResponseTimeout());
// extract used stream host from response
StreamHostUsed streamHostUsed = ((Bytestream) response).getUsedHost();
StreamHost usedStreamHost = initiation.getStreamHost(streamHostUsed.getJID());
if (usedStreamHost == null) {
throw new SmackException("Remote user responded with unknown host");
}
// build SOCKS5 client
Socks5Client socks5Client = new Socks5ClientForInitiator(usedStreamHost, digest,
this.connection, sessionID, targetJID);
// establish connection to proxy
Socket socket = socks5Client.getSocket(getProxyConnectionTimeout());
// remember last working SOCKS5 proxy to prioritize it for next request
this.lastWorkingProxy = usedStreamHost.getJID();
// negotiation successful, return the output stream
return new Socks5BytestreamSession(socket, usedStreamHost.getJID().equals(
this.connection.getUser()));
}
catch (TimeoutException e) {
throw new IOException("Timeout while connecting to SOCKS5 proxy");
}
finally {
// remove transfer digest if output stream is returned or an exception
// occurred
socks5Proxy.removeTransfer(digest);
}
}
/**
* Returns <code>true</code> if the given target JID supports feature SOCKS5 Bytestream.
*
* @param targetJID the target JID
* @return <code>true</code> if the given target JID supports feature SOCKS5 Bytestream
* otherwise <code>false</code>
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
*/
private boolean supportsSocks5(String targetJID) throws NoResponseException, XMPPErrorException, NotConnectedException {
return ServiceDiscoveryManager.getInstanceFor(connection).supportsFeature(targetJID, NAMESPACE);
}
/**
* Returns a list of JIDs of SOCKS5 proxies by querying the XMPP server. The SOCKS5 proxies are
* in the same order as returned by the XMPP server.
*
* @return list of JIDs of SOCKS5 proxies
* @throws XMPPErrorException if there was an error querying the XMPP server for SOCKS5 proxies
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
*/
private List<String> determineProxies() throws NoResponseException, XMPPErrorException, NotConnectedException {
ServiceDiscoveryManager serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(this.connection);
List<String> proxies = new ArrayList<String>();
// get all items from XMPP server
DiscoverItems discoverItems = serviceDiscoveryManager.discoverItems(this.connection.getServiceName());
// query all items if they are SOCKS5 proxies
for (Item item : discoverItems.getItems()) {
// skip blacklisted servers
if (this.proxyBlacklist.contains(item.getEntityID())) {
continue;
}
DiscoverInfo proxyInfo;
try {
proxyInfo = serviceDiscoveryManager.discoverInfo(item.getEntityID());
}
catch (NoResponseException|XMPPErrorException e) {
// blacklist errornous server
proxyBlacklist.add(item.getEntityID());
continue;
}
// item must have category "proxy" and type "bytestream"
for (Identity identity : proxyInfo.getIdentities()) {
if ("proxy".equalsIgnoreCase(identity.getCategory())
&& "bytestreams".equalsIgnoreCase(identity.getType())) {
proxies.add(item.getEntityID());
break;
}
/*
* server is not a SOCKS5 proxy, blacklist server to skip next time a Socks5
* bytestream should be established
*/
this.proxyBlacklist.add(item.getEntityID());
}
}
return proxies;
}
/**
* Returns a list of stream hosts containing the IP address an the port for the given list of
* SOCKS5 proxy JIDs. The order of the returned list is the same as the given list of JIDs
* excluding all SOCKS5 proxies who's network settings could not be determined. If a local
* SOCKS5 proxy is running it will be the first item in the list returned.
*
* @param proxies a list of SOCKS5 proxy JIDs
* @return a list of stream hosts containing the IP address an the port
*/
private List<StreamHost> determineStreamHostInfos(List<String> proxies) {
List<StreamHost> streamHosts = new ArrayList<StreamHost>();
// add local proxy on first position if exists
List<StreamHost> localProxies = getLocalStreamHost();
if (localProxies != null) {
streamHosts.addAll(localProxies);
}
// query SOCKS5 proxies for network settings
for (String proxy : proxies) {
Bytestream streamHostRequest = createStreamHostRequest(proxy);
try {
Bytestream response = (Bytestream) connection.createPacketCollectorAndSend(
streamHostRequest).nextResultOrThrow();
streamHosts.addAll(response.getStreamHosts());
}
catch (Exception e) {
// blacklist errornous proxies
this.proxyBlacklist.add(proxy);
}
}
return streamHosts;
}
/**
* Returns a IQ packet to query a SOCKS5 proxy its network settings.
*
* @param proxy the proxy to query
* @return IQ packet to query a SOCKS5 proxy its network settings
*/
private Bytestream createStreamHostRequest(String proxy) {
Bytestream request = new Bytestream();
request.setType(IQ.Type.GET);
request.setTo(proxy);
return request;
}
/**
* Returns the stream host information of the local SOCKS5 proxy containing the IP address and
* the port or null if local SOCKS5 proxy is not running.
*
* @return the stream host information of the local SOCKS5 proxy or null if local SOCKS5 proxy
* is not running
*/
private List<StreamHost> getLocalStreamHost() {
// get local proxy singleton
Socks5Proxy socks5Server = Socks5Proxy.getSocks5Proxy();
if (socks5Server.isRunning()) {
List<String> addresses = socks5Server.getLocalAddresses();
int port = socks5Server.getPort();
if (addresses.size() >= 1) {
List<StreamHost> streamHosts = new ArrayList<StreamHost>();
for (String address : addresses) {
StreamHost streamHost = new StreamHost(this.connection.getUser(), address);
streamHost.setPort(port);
streamHosts.add(streamHost);
}
return streamHosts;
}
}
// server is not running or local address could not be determined
return null;
}
/**
* Returns a SOCKS5 Bytestream initialization request packet with the given session ID
* containing the given stream hosts for the given target JID.
*
* @param sessionID the session ID for the SOCKS5 Bytestream
* @param targetJID the target JID of SOCKS5 Bytestream request
* @param streamHosts a list of SOCKS5 proxies the target should connect to
* @return a SOCKS5 Bytestream initialization request packet
*/
private Bytestream createBytestreamInitiation(String sessionID, String targetJID,
List<StreamHost> streamHosts) {
Bytestream initiation = new Bytestream(sessionID);
// add all stream hosts
for (StreamHost streamHost : streamHosts) {
initiation.addStreamHost(streamHost);
}
initiation.setType(IQ.Type.SET);
initiation.setTo(targetJID);
return initiation;
}
/**
* Responses to the given packet's sender with a XMPP error that a SOCKS5 Bytestream is not
* accepted.
*
* @param packet Packet that should be answered with a not-acceptable error
* @throws NotConnectedException
*/
protected void replyRejectPacket(IQ packet) throws NotConnectedException {
XMPPError xmppError = new XMPPError(XMPPError.Condition.no_acceptable);
IQ errorIQ = IQ.createErrorResponse(packet, xmppError);
this.connection.sendPacket(errorIQ);
}
/**
* Activates the Socks5BytestreamManager by registering the SOCKS5 Bytestream initialization
* listener and enabling the SOCKS5 Bytestream feature.
*/
private void activate() {
// register bytestream initiation packet listener
this.connection.addPacketListener(this.initiationListener,
this.initiationListener.getFilter());
// enable SOCKS5 feature
enableService();
}
/**
* Adds the SOCKS5 Bytestream feature to the service discovery.
*/
private void enableService() {
ServiceDiscoveryManager manager = ServiceDiscoveryManager.getInstanceFor(this.connection);
if (!manager.includesFeature(NAMESPACE)) {
manager.addFeature(NAMESPACE);
}
}
/**
* Returns a new unique session ID.
*
* @return a new unique session ID
*/
private String getNextSessionID() {
StringBuilder buffer = new StringBuilder();
buffer.append(SESSION_ID_PREFIX);
buffer.append(Math.abs(randomGenerator.nextLong()));
return buffer.toString();
}
/**
* Returns the XMPP connection.
*
* @return the XMPP connection
*/
protected XMPPConnection getConnection() {
return this.connection;
}
/**
* Returns the {@link BytestreamListener} that should be informed if a SOCKS5 Bytestream request
* from the given initiator JID is received.
*
* @param initiator the initiator's JID
* @return the listener
*/
protected BytestreamListener getUserListener(String initiator) {
return this.userListeners.get(initiator);
}
/**
* Returns a list of {@link BytestreamListener} that are informed if there are no listeners for
* a specific initiator.
*
* @return list of listeners
*/
protected List<BytestreamListener> getAllRequestListeners() {
return this.allRequestListeners;
}
/**
* Returns the list of session IDs that should be ignored by the InitialtionListener
*
* @return list of session IDs
*/
protected List<String> getIgnoredBytestreamRequests() {
return ignoredBytestreamRequests;
}
}

View file

@ -0,0 +1,324 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.socks5;
import java.io.IOException;
import java.net.Socket;
import java.util.Collection;
import java.util.concurrent.TimeoutException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.util.Cache;
import org.jivesoftware.smackx.bytestreams.BytestreamRequest;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost;
/**
* Socks5BytestreamRequest class handles incoming SOCKS5 Bytestream requests.
*
* @author Henning Staib
*/
public class Socks5BytestreamRequest implements BytestreamRequest {
/* lifetime of an Item in the blacklist */
private static final long BLACKLIST_LIFETIME = 60 * 1000 * 120;
/* size of the blacklist */
private static final int BLACKLIST_MAX_SIZE = 100;
/* blacklist of addresses of SOCKS5 proxies */
private static final Cache<String, Integer> ADDRESS_BLACKLIST = new Cache<String, Integer>(
BLACKLIST_MAX_SIZE, BLACKLIST_LIFETIME);
/*
* The number of connection failures it takes for a particular SOCKS5 proxy to be blacklisted.
* When a proxy is blacklisted no more connection attempts will be made to it for a period of 2
* hours.
*/
private static int CONNECTION_FAILURE_THRESHOLD = 2;
/* the bytestream initialization request */
private Bytestream bytestreamRequest;
/* SOCKS5 Bytestream manager containing the XMPP connection and helper methods */
private Socks5BytestreamManager manager;
/* timeout to connect to all SOCKS5 proxies */
private int totalConnectTimeout = 10000;
/* minimum timeout to connect to one SOCKS5 proxy */
private int minimumConnectTimeout = 2000;
/**
* Returns the number of connection failures it takes for a particular SOCKS5 proxy to be
* blacklisted. When a proxy is blacklisted no more connection attempts will be made to it for a
* period of 2 hours. Default is 2.
*
* @return the number of connection failures it takes for a particular SOCKS5 proxy to be
* blacklisted
*/
public static int getConnectFailureThreshold() {
return CONNECTION_FAILURE_THRESHOLD;
}
/**
* Sets the number of connection failures it takes for a particular SOCKS5 proxy to be
* blacklisted. When a proxy is blacklisted no more connection attempts will be made to it for a
* period of 2 hours. Default is 2.
* <p>
* Setting the connection failure threshold to zero disables the blacklisting.
*
* @param connectFailureThreshold the number of connection failures it takes for a particular
* SOCKS5 proxy to be blacklisted
*/
public static void setConnectFailureThreshold(int connectFailureThreshold) {
CONNECTION_FAILURE_THRESHOLD = connectFailureThreshold;
}
/**
* Creates a new Socks5BytestreamRequest.
*
* @param manager the SOCKS5 Bytestream manager
* @param bytestreamRequest the SOCKS5 Bytestream initialization packet
*/
protected Socks5BytestreamRequest(Socks5BytestreamManager manager, Bytestream bytestreamRequest) {
this.manager = manager;
this.bytestreamRequest = bytestreamRequest;
}
/**
* Returns the maximum timeout to connect to SOCKS5 proxies. Default is 10000ms.
* <p>
* When accepting a SOCKS5 Bytestream request Smack tries to connect to all SOCKS5 proxies given
* by the initiator until a connection is established. This timeout divided by the number of
* SOCKS5 proxies determines the timeout for every connection attempt.
* <p>
* You can set the minimum timeout for establishing a connection to one SOCKS5 proxy by invoking
* {@link #setMinimumConnectTimeout(int)}.
*
* @return the maximum timeout to connect to SOCKS5 proxies
*/
public int getTotalConnectTimeout() {
if (this.totalConnectTimeout <= 0) {
return 10000;
}
return this.totalConnectTimeout;
}
/**
* Sets the maximum timeout to connect to SOCKS5 proxies. Default is 10000ms.
* <p>
* When accepting a SOCKS5 Bytestream request Smack tries to connect to all SOCKS5 proxies given
* by the initiator until a connection is established. This timeout divided by the number of
* SOCKS5 proxies determines the timeout for every connection attempt.
* <p>
* You can set the minimum timeout for establishing a connection to one SOCKS5 proxy by invoking
* {@link #setMinimumConnectTimeout(int)}.
*
* @param totalConnectTimeout the maximum timeout to connect to SOCKS5 proxies
*/
public void setTotalConnectTimeout(int totalConnectTimeout) {
this.totalConnectTimeout = totalConnectTimeout;
}
/**
* Returns the timeout to connect to one SOCKS5 proxy while accepting the SOCKS5 Bytestream
* request. Default is 2000ms.
*
* @return the timeout to connect to one SOCKS5 proxy
*/
public int getMinimumConnectTimeout() {
if (this.minimumConnectTimeout <= 0) {
return 2000;
}
return this.minimumConnectTimeout;
}
/**
* Sets the timeout to connect to one SOCKS5 proxy while accepting the SOCKS5 Bytestream
* request. Default is 2000ms.
*
* @param minimumConnectTimeout the timeout to connect to one SOCKS5 proxy
*/
public void setMinimumConnectTimeout(int minimumConnectTimeout) {
this.minimumConnectTimeout = minimumConnectTimeout;
}
/**
* Returns the sender of the SOCKS5 Bytestream initialization request.
*
* @return the sender of the SOCKS5 Bytestream initialization request.
*/
public String getFrom() {
return this.bytestreamRequest.getFrom();
}
/**
* Returns the session ID of the SOCKS5 Bytestream initialization request.
*
* @return the session ID of the SOCKS5 Bytestream initialization request.
*/
public String getSessionID() {
return this.bytestreamRequest.getSessionID();
}
/**
* Accepts the SOCKS5 Bytestream initialization request and returns the socket to send/receive
* data.
* <p>
* Before accepting the SOCKS5 Bytestream request you can set timeouts by invoking
* {@link #setTotalConnectTimeout(int)} and {@link #setMinimumConnectTimeout(int)}.
*
* @return the socket to send/receive data
* @throws InterruptedException if the current thread was interrupted while waiting
* @throws XMPPErrorException
* @throws SmackException
*/
public Socks5BytestreamSession accept() throws InterruptedException, XMPPErrorException, SmackException {
Collection<StreamHost> streamHosts = this.bytestreamRequest.getStreamHosts();
// throw exceptions if request contains no stream hosts
if (streamHosts.size() == 0) {
cancelRequest();
}
StreamHost selectedHost = null;
Socket socket = null;
String digest = Socks5Utils.createDigest(this.bytestreamRequest.getSessionID(),
this.bytestreamRequest.getFrom(), this.manager.getConnection().getUser());
/*
* determine timeout for each connection attempt; each SOCKS5 proxy has the same amount of
* time so that the first does not consume the whole timeout
*/
int timeout = Math.max(getTotalConnectTimeout() / streamHosts.size(),
getMinimumConnectTimeout());
for (StreamHost streamHost : streamHosts) {
String address = streamHost.getAddress() + ":" + streamHost.getPort();
// check to see if this address has been blacklisted
int failures = getConnectionFailures(address);
if (CONNECTION_FAILURE_THRESHOLD > 0 && failures >= CONNECTION_FAILURE_THRESHOLD) {
continue;
}
// establish socket
try {
// build SOCKS5 client
final Socks5Client socks5Client = new Socks5Client(streamHost, digest);
// connect to SOCKS5 proxy with a timeout
socket = socks5Client.getSocket(timeout);
// set selected host
selectedHost = streamHost;
break;
}
catch (TimeoutException e) {
incrementConnectionFailures(address);
}
catch (IOException e) {
incrementConnectionFailures(address);
}
catch (XMPPException e) {
incrementConnectionFailures(address);
}
}
// throw exception if connecting to all SOCKS5 proxies failed
if (selectedHost == null || socket == null) {
cancelRequest();
}
// send used-host confirmation
Bytestream response = createUsedHostResponse(selectedHost);
this.manager.getConnection().sendPacket(response);
return new Socks5BytestreamSession(socket, selectedHost.getJID().equals(
this.bytestreamRequest.getFrom()));
}
/**
* Rejects the SOCKS5 Bytestream request by sending a reject error to the initiator.
* @throws NotConnectedException
*/
public void reject() throws NotConnectedException {
this.manager.replyRejectPacket(this.bytestreamRequest);
}
/**
* Cancels the SOCKS5 Bytestream request by sending an error to the initiator and building a
* XMPP exception.
* @throws XMPPErrorException
* @throws NotConnectedException
*/
private void cancelRequest() throws XMPPErrorException, NotConnectedException {
String errorMessage = "Could not establish socket with any provided host";
XMPPError error = new XMPPError(XMPPError.Condition.item_not_found, errorMessage);
IQ errorIQ = IQ.createErrorResponse(this.bytestreamRequest, error);
this.manager.getConnection().sendPacket(errorIQ);
throw new XMPPErrorException(errorMessage, error);
}
/**
* Returns the response to the SOCKS5 Bytestream request containing the SOCKS5 proxy used.
*
* @param selectedHost the used SOCKS5 proxy
* @return the response to the SOCKS5 Bytestream request
*/
private Bytestream createUsedHostResponse(StreamHost selectedHost) {
Bytestream response = new Bytestream(this.bytestreamRequest.getSessionID());
response.setTo(this.bytestreamRequest.getFrom());
response.setType(IQ.Type.RESULT);
response.setPacketID(this.bytestreamRequest.getPacketID());
response.setUsedHost(selectedHost.getJID());
return response;
}
/**
* Increments the connection failure counter by one for the given address.
*
* @param address the address the connection failure counter should be increased
*/
private void incrementConnectionFailures(String address) {
Integer count = ADDRESS_BLACKLIST.get(address);
ADDRESS_BLACKLIST.put(address, count == null ? 1 : count + 1);
}
/**
* Returns how often the connection to the given address failed.
*
* @param address the address
* @return number of connection failures
*/
private int getConnectionFailures(String address) {
Integer count = ADDRESS_BLACKLIST.get(address);
return count != null ? count : 0;
}
}

View file

@ -0,0 +1,97 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.socks5;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
/**
* Socks5BytestreamSession class represents a SOCKS5 Bytestream session.
*
* @author Henning Staib
*/
public class Socks5BytestreamSession implements BytestreamSession {
/* the underlying socket of the SOCKS5 Bytestream */
private final Socket socket;
/* flag to indicate if this session is a direct or mediated connection */
private final boolean isDirect;
protected Socks5BytestreamSession(Socket socket, boolean isDirect) {
this.socket = socket;
this.isDirect = isDirect;
}
/**
* Returns <code>true</code> if the session is established through a direct connection between
* the initiator and target, <code>false</code> if the session is mediated over a SOCKS proxy.
*
* @return <code>true</code> if session is a direct connection, <code>false</code> if session is
* mediated over a SOCKS5 proxy
*/
public boolean isDirect() {
return this.isDirect;
}
/**
* Returns <code>true</code> if the session is mediated over a SOCKS proxy, <code>false</code>
* if this session is established through a direct connection between the initiator and target.
*
* @return <code>true</code> if session is mediated over a SOCKS5 proxy, <code>false</code> if
* session is a direct connection
*/
public boolean isMediated() {
return !this.isDirect;
}
public InputStream getInputStream() throws IOException {
return this.socket.getInputStream();
}
public OutputStream getOutputStream() throws IOException {
return this.socket.getOutputStream();
}
public int getReadTimeout() throws IOException {
try {
return this.socket.getSoTimeout();
}
catch (SocketException e) {
throw new IOException("Error on underlying Socket");
}
}
public void setReadTimeout(int timeout) throws IOException {
try {
this.socket.setSoTimeout(timeout);
}
catch (SocketException e) {
throw new IOException("Error on underlying Socket");
}
}
public void close() throws IOException {
this.socket.close();
}
}

View file

@ -0,0 +1,215 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.socks5;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost;
/**
* The SOCKS5 client class handles establishing a connection to a SOCKS5 proxy. Connecting to a
* SOCKS5 proxy requires authentication. This implementation only supports the no-authentication
* authentication method.
*
* @author Henning Staib
*/
class Socks5Client {
/* stream host containing network settings and name of the SOCKS5 proxy */
protected StreamHost streamHost;
/* SHA-1 digest identifying the SOCKS5 stream */
protected String digest;
/**
* Constructor for a SOCKS5 client.
*
* @param streamHost containing network settings of the SOCKS5 proxy
* @param digest identifying the SOCKS5 Bytestream
*/
public Socks5Client(StreamHost streamHost, String digest) {
this.streamHost = streamHost;
this.digest = digest;
}
/**
* Returns the initialized socket that can be used to transfer data between peers via the SOCKS5
* proxy.
*
* @param timeout timeout to connect to SOCKS5 proxy in milliseconds
* @return socket the initialized socket
* @throws IOException if initializing the socket failed due to a network error
* @throws XMPPErrorException if establishing connection to SOCKS5 proxy failed
* @throws TimeoutException if connecting to SOCKS5 proxy timed out
* @throws InterruptedException if the current thread was interrupted while waiting
* @throws SmackException if the connection to the SOC
* @throws XMPPException
*/
public Socket getSocket(int timeout) throws IOException, XMPPErrorException, InterruptedException,
TimeoutException, SmackException, XMPPException {
// wrap connecting in future for timeout
FutureTask<Socket> futureTask = new FutureTask<Socket>(new Callable<Socket>() {
public Socket call() throws IOException, SmackException {
// initialize socket
Socket socket = new Socket();
SocketAddress socketAddress = new InetSocketAddress(streamHost.getAddress(),
streamHost.getPort());
socket.connect(socketAddress);
boolean res;
// initialize connection to SOCKS5 proxy
try {
res = establish(socket);
}
catch (SmackException e) {
socket.close();
throw e;
}
if (res) {
return socket;
}
else {
socket.close();
throw new SmackException("SOCKS5 negotiation failed");
}
}
});
Thread executor = new Thread(futureTask);
executor.start();
// get connection to initiator with timeout
try {
return futureTask.get(timeout, TimeUnit.MILLISECONDS);
}
catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause != null) {
// case exceptions to comply with method signature
if (cause instanceof IOException) {
throw (IOException) cause;
}
if (cause instanceof SmackException) {
throw (SmackException) cause;
}
}
// throw generic IO exception if unexpected exception was thrown
throw new IOException("Error while connection to SOCKS5 proxy");
}
}
/**
* Initializes the connection to the SOCKS5 proxy by negotiating authentication method and
* requesting a stream for the given digest. Currently only the no-authentication method is
* supported by the Socks5Client.
* <p>
* Returns <code>true</code> if a stream could be established, otherwise <code>false</code>. If
* <code>false</code> is returned the given Socket should be closed.
*
* @param socket connected to a SOCKS5 proxy
* @return <code>true</code> if if a stream could be established, otherwise <code>false</code>.
* If <code>false</code> is returned the given Socket should be closed.
* @throws SmackException
* @throws IOException
*/
protected boolean establish(Socket socket) throws SmackException, IOException {
byte[] connectionRequest;
byte[] connectionResponse;
/*
* use DataInputStream/DataOutpuStream to assure read and write is completed in a single
* statement
*/
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
// authentication negotiation
byte[] cmd = new byte[3];
cmd[0] = (byte) 0x05; // protocol version 5
cmd[1] = (byte) 0x01; // number of authentication methods supported
cmd[2] = (byte) 0x00; // authentication method: no-authentication required
out.write(cmd);
out.flush();
byte[] response = new byte[2];
in.readFully(response);
// check if server responded with correct version and no-authentication method
if (response[0] != (byte) 0x05 || response[1] != (byte) 0x00) {
return false;
}
// request SOCKS5 connection with given address/digest
connectionRequest = createSocks5ConnectRequest();
out.write(connectionRequest);
out.flush();
// receive response
connectionResponse = Socks5Utils.receiveSocks5Message(in);
// verify response
connectionRequest[1] = (byte) 0x00; // set expected return status to 0
return Arrays.equals(connectionRequest, connectionResponse);
}
/**
* Returns a SOCKS5 connection request message. It contains the command "connect", the address
* type "domain" and the digest as address.
* <p>
* (see <a href="http://tools.ietf.org/html/rfc1928">RFC1928</a>)
*
* @return SOCKS5 connection request message
*/
private byte[] createSocks5ConnectRequest() {
byte addr[] = this.digest.getBytes();
byte[] data = new byte[7 + addr.length];
data[0] = (byte) 0x05; // version (SOCKS5)
data[1] = (byte) 0x01; // command (1 - connect)
data[2] = (byte) 0x00; // reserved byte (always 0)
data[3] = (byte) 0x03; // address type (3 - domain name)
data[4] = (byte) addr.length; // address length
System.arraycopy(addr, 0, data, 5, addr.length); // address
data[data.length - 2] = (byte) 0; // address port (2 bytes always 0)
data[data.length - 1] = (byte) 0;
return data;
}
}

View file

@ -0,0 +1,131 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.socks5;
import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.TimeoutException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost;
/**
* Implementation of a SOCKS5 client used on the initiators side. This is needed because connecting
* to the local SOCKS5 proxy differs form the regular way to connect to a SOCKS5 proxy. Additionally
* a remote SOCKS5 proxy has to be activated by the initiator before data can be transferred between
* the peers.
*
* @author Henning Staib
*/
class Socks5ClientForInitiator extends Socks5Client {
/* the XMPP connection used to communicate with the SOCKS5 proxy */
private XMPPConnection connection;
/* the session ID used to activate SOCKS5 stream */
private String sessionID;
/* the target JID used to activate SOCKS5 stream */
private String target;
/**
* Creates a new SOCKS5 client for the initiators side.
*
* @param streamHost containing network settings of the SOCKS5 proxy
* @param digest identifying the SOCKS5 Bytestream
* @param connection the XMPP connection
* @param sessionID the session ID of the SOCKS5 Bytestream
* @param target the target JID of the SOCKS5 Bytestream
*/
public Socks5ClientForInitiator(StreamHost streamHost, String digest, XMPPConnection connection,
String sessionID, String target) {
super(streamHost, digest);
this.connection = connection;
this.sessionID = sessionID;
this.target = target;
}
public Socket getSocket(int timeout) throws IOException, InterruptedException,
TimeoutException, XMPPException, SmackException {
Socket socket = null;
// check if stream host is the local SOCKS5 proxy
if (this.streamHost.getJID().equals(this.connection.getUser())) {
Socks5Proxy socks5Server = Socks5Proxy.getSocks5Proxy();
socket = socks5Server.getSocket(this.digest);
if (socket == null) {
throw new SmackException("target is not connected to SOCKS5 proxy");
}
}
else {
socket = super.getSocket(timeout);
try {
activate();
}
catch (XMPPException e1) {
socket.close();
throw e1;
}
catch (NoResponseException e2) {
socket.close();
throw e2;
}
}
return socket;
}
/**
* Activates the SOCKS5 Bytestream by sending a XMPP SOCKS5 Bytestream activation packet to the
* SOCKS5 proxy.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
* @throws SmackException if there was no response from the server.
*/
private void activate() throws NoResponseException, XMPPErrorException, NotConnectedException {
Bytestream activate = createStreamHostActivation();
// if activation fails #nextResultOrThrow() throws an exception
connection.createPacketCollectorAndSend(activate).nextResultOrThrow();
}
/**
* Returns a SOCKS5 Bytestream activation packet.
*
* @return SOCKS5 Bytestream activation packet
*/
private Bytestream createStreamHostActivation() {
Bytestream activate = new Bytestream(this.sessionID);
activate.setMode(null);
activate.setType(IQ.Type.SET);
activate.setTo(this.streamHost.getJID());
activate.setToActivate(this.target);
return activate;
}
}

View file

@ -0,0 +1,463 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.socks5;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException;
/**
* The Socks5Proxy class represents a local SOCKS5 proxy server. It can be enabled/disabled by
* invoking {@link #setLocalSocks5ProxyEnabled(boolean)}. The proxy is enabled by default.
* <p>
* The port of the local SOCKS5 proxy can be configured by invoking
* {@link #setLocalSocks5ProxyPort(int)}. Default port is 7777. If you set the port to a negative
* value Smack tries to the absolute value and all following until it finds an open port.
* <p>
* If your application is running on a machine with multiple network interfaces or if you want to
* provide your public address in case you are behind a NAT router, invoke
* {@link #addLocalAddress(String)} or {@link #replaceLocalAddresses(List)} to modify the list of
* local network addresses used for outgoing SOCKS5 Bytestream requests.
* <p>
* The local SOCKS5 proxy server refuses all connections except the ones that are explicitly allowed
* in the process of establishing a SOCKS5 Bytestream (
* {@link Socks5BytestreamManager#establishSession(String)}).
* <p>
* This Implementation has the following limitations:
* <ul>
* <li>only supports the no-authentication authentication method</li>
* <li>only supports the <code>connect</code> command and will not answer correctly to other
* commands</li>
* <li>only supports requests with the domain address type and will not correctly answer to requests
* with other address types</li>
* </ul>
* (see <a href="http://tools.ietf.org/html/rfc1928">RFC 1928</a>)
*
* @author Henning Staib
*/
public class Socks5Proxy {
private static final Logger LOGGER = Logger.getLogger(Socks5Proxy.class.getName());
/* SOCKS5 proxy singleton */
private static Socks5Proxy socks5Server;
private static boolean localSocks5ProxyEnabled = true;
private static int localSocks5ProxyPort = 7777;
/* reusable implementation of a SOCKS5 proxy server process */
private Socks5ServerProcess serverProcess;
/* thread running the SOCKS5 server process */
private Thread serverThread;
/* server socket to accept SOCKS5 connections */
private ServerSocket serverSocket;
/* assigns a connection to a digest */
private final Map<String, Socket> connectionMap = new ConcurrentHashMap<String, Socket>();
/* list of digests connections should be stored */
private final List<String> allowedConnections = Collections.synchronizedList(new LinkedList<String>());
private final Set<String> localAddresses = Collections.synchronizedSet(new LinkedHashSet<String>());
/**
* Private constructor.
*/
private Socks5Proxy() {
this.serverProcess = new Socks5ServerProcess();
// add default local address
try {
this.localAddresses.add(InetAddress.getLocalHost().getHostAddress());
}
catch (UnknownHostException e) {
// do nothing
}
}
/**
* Returns true if the local Socks5 proxy should be started. Default is true.
*
* @return if the local Socks5 proxy should be started
*/
public static boolean isLocalSocks5ProxyEnabled() {
return localSocks5ProxyEnabled;
}
/**
* Sets if the local Socks5 proxy should be started. Default is true.
*
* @param localSocks5ProxyEnabled if the local Socks5 proxy should be started
*/
public static void setLocalSocks5ProxyEnabled(boolean localSocks5ProxyEnabled) {
Socks5Proxy.localSocks5ProxyEnabled = localSocks5ProxyEnabled;
}
/**
* Return the port of the local Socks5 proxy. Default is 7777.
*
* @return the port of the local Socks5 proxy
*/
public static int getLocalSocks5ProxyPort() {
return localSocks5ProxyPort;
}
/**
* Sets the port of the local Socks5 proxy. Default is 7777. If you set the port to a negative
* value Smack tries the absolute value and all following until it finds an open port.
*
* @param localSocks5ProxyPort the port of the local Socks5 proxy to set
*/
public static void setLocalSocks5ProxyPort(int localSocks5ProxyPort) {
Socks5Proxy.localSocks5ProxyPort = localSocks5ProxyPort;
}
/**
* Returns the local SOCKS5 proxy server.
*
* @return the local SOCKS5 proxy server
*/
public static synchronized Socks5Proxy getSocks5Proxy() {
if (socks5Server == null) {
socks5Server = new Socks5Proxy();
}
if (isLocalSocks5ProxyEnabled()) {
socks5Server.start();
}
return socks5Server;
}
/**
* Starts the local SOCKS5 proxy server. If it is already running, this method does nothing.
*/
public synchronized void start() {
if (isRunning()) {
return;
}
try {
if (getLocalSocks5ProxyPort() < 0) {
int port = Math.abs(getLocalSocks5ProxyPort());
for (int i = 0; i < 65535 - port; i++) {
try {
this.serverSocket = new ServerSocket(port + i);
break;
}
catch (IOException e) {
// port is used, try next one
}
}
}
else {
this.serverSocket = new ServerSocket(getLocalSocks5ProxyPort());
}
if (this.serverSocket != null) {
this.serverThread = new Thread(this.serverProcess);
this.serverThread.start();
}
}
catch (IOException e) {
// couldn't setup server
LOGGER.log(Level.SEVERE, "couldn't setup local SOCKS5 proxy on port " + getLocalSocks5ProxyPort(), e);
}
}
/**
* Stops the local SOCKS5 proxy server. If it is not running this method does nothing.
*/
public synchronized void stop() {
if (!isRunning()) {
return;
}
try {
this.serverSocket.close();
}
catch (IOException e) {
// do nothing
}
if (this.serverThread != null && this.serverThread.isAlive()) {
try {
this.serverThread.interrupt();
this.serverThread.join();
}
catch (InterruptedException e) {
// do nothing
}
}
this.serverThread = null;
this.serverSocket = null;
}
/**
* Adds the given address to the list of local network addresses.
* <p>
* Use this method if you want to provide multiple addresses in a SOCKS5 Bytestream request.
* This may be necessary if your application is running on a machine with multiple network
* interfaces or if you want to provide your public address in case you are behind a NAT router.
* <p>
* The order of the addresses used is determined by the order you add addresses.
* <p>
* Note that the list of addresses initially contains the address returned by
* <code>InetAddress.getLocalHost().getHostAddress()</code>. You can replace the list of
* addresses by invoking {@link #replaceLocalAddresses(List)}.
*
* @param address the local network address to add
*/
public void addLocalAddress(String address) {
if (address == null) {
throw new IllegalArgumentException("address may not be null");
}
this.localAddresses.add(address);
}
/**
* Removes the given address from the list of local network addresses. This address will then no
* longer be used of outgoing SOCKS5 Bytestream requests.
*
* @param address the local network address to remove
*/
public void removeLocalAddress(String address) {
this.localAddresses.remove(address);
}
/**
* Returns an unmodifiable list of the local network addresses that will be used for streamhost
* candidates of outgoing SOCKS5 Bytestream requests.
*
* @return unmodifiable list of the local network addresses
*/
public List<String> getLocalAddresses() {
return Collections.unmodifiableList(new ArrayList<String>(this.localAddresses));
}
/**
* Replaces the list of local network addresses.
* <p>
* Use this method if you want to provide multiple addresses in a SOCKS5 Bytestream request and
* want to define their order. This may be necessary if your application is running on a machine
* with multiple network interfaces or if you want to provide your public address in case you
* are behind a NAT router.
*
* @param addresses the new list of local network addresses
*/
public void replaceLocalAddresses(List<String> addresses) {
if (addresses == null) {
throw new IllegalArgumentException("list must not be null");
}
this.localAddresses.clear();
this.localAddresses.addAll(addresses);
}
/**
* Returns the port of the local SOCKS5 proxy server. If it is not running -1 will be returned.
*
* @return the port of the local SOCKS5 proxy server or -1 if proxy is not running
*/
public int getPort() {
if (!isRunning()) {
return -1;
}
return this.serverSocket.getLocalPort();
}
/**
* Returns the socket for the given digest. A socket will be returned if the given digest has
* been in the list of allowed transfers (see {@link #addTransfer(String)}) while the peer
* connected to the SOCKS5 proxy.
*
* @param digest identifying the connection
* @return socket or null if there is no socket for the given digest
*/
protected Socket getSocket(String digest) {
return this.connectionMap.get(digest);
}
/**
* Add the given digest to the list of allowed transfers. Only connections for allowed transfers
* are stored and can be retrieved by invoking {@link #getSocket(String)}. All connections to
* the local SOCKS5 proxy that don't contain an allowed digest are discarded.
*
* @param digest to be added to the list of allowed transfers
*/
protected void addTransfer(String digest) {
this.allowedConnections.add(digest);
}
/**
* Removes the given digest from the list of allowed transfers. After invoking this method
* already stored connections with the given digest will be removed.
* <p>
* The digest should be removed after establishing the SOCKS5 Bytestream is finished, an error
* occurred while establishing the connection or if the connection is not allowed anymore.
*
* @param digest to be removed from the list of allowed transfers
*/
protected void removeTransfer(String digest) {
this.allowedConnections.remove(digest);
this.connectionMap.remove(digest);
}
/**
* Returns <code>true</code> if the local SOCKS5 proxy server is running, otherwise
* <code>false</code>.
*
* @return <code>true</code> if the local SOCKS5 proxy server is running, otherwise
* <code>false</code>
*/
public boolean isRunning() {
return this.serverSocket != null;
}
/**
* Implementation of a simplified SOCKS5 proxy server.
*/
private class Socks5ServerProcess implements Runnable {
public void run() {
while (true) {
Socket socket = null;
try {
if (Socks5Proxy.this.serverSocket.isClosed()
|| Thread.currentThread().isInterrupted()) {
return;
}
// accept connection
socket = Socks5Proxy.this.serverSocket.accept();
// initialize connection
establishConnection(socket);
}
catch (SocketException e) {
/*
* do nothing, if caused by closing the server socket, thread will terminate in
* next loop
*/
}
catch (Exception e) {
try {
if (socket != null) {
socket.close();
}
}
catch (IOException e1) {
/* do nothing */
}
}
}
}
/**
* Negotiates a SOCKS5 connection and stores it on success.
*
* @param socket connection to the client
* @throws SmackException if client requests a connection in an unsupported way
* @throws IOException if a network error occurred
*/
private void establishConnection(Socket socket) throws SmackException, IOException {
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
DataInputStream in = new DataInputStream(socket.getInputStream());
// first byte is version should be 5
int b = in.read();
if (b != 5) {
throw new SmackException("Only SOCKS5 supported");
}
// second byte number of authentication methods supported
b = in.read();
// read list of supported authentication methods
byte[] auth = new byte[b];
in.readFully(auth);
byte[] authMethodSelectionResponse = new byte[2];
authMethodSelectionResponse[0] = (byte) 0x05; // protocol version
// only authentication method 0, no authentication, supported
boolean noAuthMethodFound = false;
for (int i = 0; i < auth.length; i++) {
if (auth[i] == (byte) 0x00) {
noAuthMethodFound = true;
break;
}
}
if (!noAuthMethodFound) {
authMethodSelectionResponse[1] = (byte) 0xFF; // no acceptable methods
out.write(authMethodSelectionResponse);
out.flush();
throw new SmackException("Authentication method not supported");
}
authMethodSelectionResponse[1] = (byte) 0x00; // no-authentication method
out.write(authMethodSelectionResponse);
out.flush();
// receive connection request
byte[] connectionRequest = Socks5Utils.receiveSocks5Message(in);
// extract digest
String responseDigest = new String(connectionRequest, 5, connectionRequest[4]);
// return error if digest is not allowed
if (!Socks5Proxy.this.allowedConnections.contains(responseDigest)) {
connectionRequest[1] = (byte) 0x05; // set return status to 5 (connection refused)
out.write(connectionRequest);
out.flush();
throw new SmackException("Connection is not allowed");
}
connectionRequest[1] = (byte) 0x00; // set return status to 0 (success)
out.write(connectionRequest);
out.flush();
// store connection
Socks5Proxy.this.connectionMap.put(responseDigest, socket);
}
}
}

View file

@ -0,0 +1,76 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.socks5;
import java.io.DataInputStream;
import java.io.IOException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.util.StringUtils;
/**
* A collection of utility methods for SOcKS5 messages.
*
* @author Henning Staib
*/
class Socks5Utils {
/**
* Returns a SHA-1 digest of the given parameters as specified in <a
* href="http://xmpp.org/extensions/xep-0065.html#impl-socks5">XEP-0065</a>.
*
* @param sessionID for the SOCKS5 Bytestream
* @param initiatorJID JID of the initiator of a SOCKS5 Bytestream
* @param targetJID JID of the target of a SOCKS5 Bytestream
* @return SHA-1 digest of the given parameters
*/
public static String createDigest(String sessionID, String initiatorJID, String targetJID) {
StringBuilder b = new StringBuilder();
b.append(sessionID).append(initiatorJID).append(targetJID);
return StringUtils.hash(b.toString());
}
/**
* Reads a SOCKS5 message from the given InputStream. The message can either be a SOCKS5 request
* message or a SOCKS5 response message.
* <p>
* (see <a href="http://tools.ietf.org/html/rfc1928">RFC1928</a>)
*
* @param in the DataInputStream to read the message from
* @return the SOCKS5 message
* @throws IOException if a network error occurred
* @throws SmackException if the SOCKS5 message contains an unsupported address type
*/
public static byte[] receiveSocks5Message(DataInputStream in) throws IOException, SmackException {
byte[] header = new byte[5];
in.readFully(header, 0, 5);
if (header[3] != (byte) 0x03) {
throw new SmackException("Unsupported SOCKS5 address type");
}
int addressLength = header[4];
byte[] response = new byte[7 + addressLength];
System.arraycopy(header, 0, response, 0, header.length);
in.readFully(response, header.length, addressLength + 2);
return response;
}
}

View file

@ -0,0 +1,477 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.socks5.packet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.PacketExtension;
/**
* A packet representing part of a SOCKS5 Bytestream negotiation.
*
* @author Alexander Wenckus
*/
public class Bytestream extends IQ {
private String sessionID;
private Mode mode = Mode.tcp;
private final List<StreamHost> streamHosts = new ArrayList<StreamHost>();
private StreamHostUsed usedHost;
private Activate toActivate;
/**
* The default constructor
*/
public Bytestream() {
super();
}
/**
* A constructor where the session ID can be specified.
*
* @param SID The session ID related to the negotiation.
* @see #setSessionID(String)
*/
public Bytestream(final String SID) {
super();
setSessionID(SID);
}
/**
* Set the session ID related to the bytestream. The session ID is a unique identifier used to
* differentiate between stream negotiations.
*
* @param sessionID the unique session ID that identifies the transfer.
*/
public void setSessionID(final String sessionID) {
this.sessionID = sessionID;
}
/**
* Returns the session ID related to the bytestream negotiation.
*
* @return Returns the session ID related to the bytestream negotiation.
* @see #setSessionID(String)
*/
public String getSessionID() {
return sessionID;
}
/**
* Set the transport mode. This should be put in the initiation of the interaction.
*
* @param mode the transport mode, either UDP or TCP
* @see Mode
*/
public void setMode(final Mode mode) {
this.mode = mode;
}
/**
* Returns the transport mode.
*
* @return Returns the transport mode.
* @see #setMode(Mode)
*/
public Mode getMode() {
return mode;
}
/**
* Adds a potential stream host that the remote user can connect to to receive the file.
*
* @param JID The JID of the stream host.
* @param address The internet address of the stream host.
* @return The added stream host.
*/
public StreamHost addStreamHost(final String JID, final String address) {
return addStreamHost(JID, address, 0);
}
/**
* Adds a potential stream host that the remote user can connect to to receive the file.
*
* @param JID The JID of the stream host.
* @param address The internet address of the stream host.
* @param port The port on which the remote host is seeking connections.
* @return The added stream host.
*/
public StreamHost addStreamHost(final String JID, final String address, final int port) {
StreamHost host = new StreamHost(JID, address);
host.setPort(port);
addStreamHost(host);
return host;
}
/**
* Adds a potential stream host that the remote user can transfer the file through.
*
* @param host The potential stream host.
*/
public void addStreamHost(final StreamHost host) {
streamHosts.add(host);
}
/**
* Returns the list of stream hosts contained in the packet.
*
* @return Returns the list of stream hosts contained in the packet.
*/
public Collection<StreamHost> getStreamHosts() {
return Collections.unmodifiableCollection(streamHosts);
}
/**
* Returns the stream host related to the given JID, or null if there is none.
*
* @param JID The JID of the desired stream host.
* @return Returns the stream host related to the given JID, or null if there is none.
*/
public StreamHost getStreamHost(final String JID) {
if (JID == null) {
return null;
}
for (StreamHost host : streamHosts) {
if (host.getJID().equals(JID)) {
return host;
}
}
return null;
}
/**
* Returns the count of stream hosts contained in this packet.
*
* @return Returns the count of stream hosts contained in this packet.
*/
public int countStreamHosts() {
return streamHosts.size();
}
/**
* Upon connecting to the stream host the target of the stream replies to the initiator with the
* JID of the SOCKS5 host that they used.
*
* @param JID The JID of the used host.
*/
public void setUsedHost(final String JID) {
this.usedHost = new StreamHostUsed(JID);
}
/**
* Returns the SOCKS5 host connected to by the remote user.
*
* @return Returns the SOCKS5 host connected to by the remote user.
*/
public StreamHostUsed getUsedHost() {
return usedHost;
}
/**
* Returns the activate element of the packet sent to the proxy host to verify the identity of
* the initiator and match them to the appropriate stream.
*
* @return Returns the activate element of the packet sent to the proxy host to verify the
* identity of the initiator and match them to the appropriate stream.
*/
public Activate getToActivate() {
return toActivate;
}
/**
* Upon the response from the target of the used host the activate packet is sent to the SOCKS5
* proxy. The proxy will activate the stream or return an error after verifying the identity of
* the initiator, using the activate packet.
*
* @param targetID The JID of the target of the file transfer.
*/
public void setToActivate(final String targetID) {
this.toActivate = new Activate(targetID);
}
public String getChildElementXML() {
StringBuilder buf = new StringBuilder();
buf.append("<query xmlns=\"http://jabber.org/protocol/bytestreams\"");
if (this.getType().equals(IQ.Type.SET)) {
if (getSessionID() != null) {
buf.append(" sid=\"").append(getSessionID()).append("\"");
}
if (getMode() != null) {
buf.append(" mode = \"").append(getMode()).append("\"");
}
buf.append(">");
if (getToActivate() == null) {
for (StreamHost streamHost : getStreamHosts()) {
buf.append(streamHost.toXML());
}
}
else {
buf.append(getToActivate().toXML());
}
}
else if (this.getType().equals(IQ.Type.RESULT)) {
buf.append(">");
if (getUsedHost() != null) {
buf.append(getUsedHost().toXML());
}
// A result from the server can also contain stream hosts
else if (countStreamHosts() > 0) {
for (StreamHost host : streamHosts) {
buf.append(host.toXML());
}
}
}
else if (this.getType().equals(IQ.Type.GET)) {
return buf.append("/>").toString();
}
else {
return null;
}
buf.append("</query>");
return buf.toString();
}
/**
* Packet extension that represents a potential SOCKS5 proxy for the file transfer. Stream hosts
* are forwarded to the target of the file transfer who then chooses and connects to one.
*
* @author Alexander Wenckus
*/
public static class StreamHost implements PacketExtension {
public static String NAMESPACE = "";
public static String ELEMENTNAME = "streamhost";
private final String JID;
private final String addy;
private int port = 0;
/**
* Default constructor.
*
* @param JID The JID of the stream host.
* @param address The internet address of the stream host.
*/
public StreamHost(final String JID, final String address) {
this.JID = JID;
this.addy = address;
}
/**
* Returns the JID of the stream host.
*
* @return Returns the JID of the stream host.
*/
public String getJID() {
return JID;
}
/**
* Returns the internet address of the stream host.
*
* @return Returns the internet address of the stream host.
*/
public String getAddress() {
return addy;
}
/**
* Sets the port of the stream host.
*
* @param port The port on which the potential stream host would accept the connection.
*/
public void setPort(final int port) {
this.port = port;
}
/**
* Returns the port on which the potential stream host would accept the connection.
*
* @return Returns the port on which the potential stream host would accept the connection.
*/
public int getPort() {
return port;
}
public String getNamespace() {
return NAMESPACE;
}
public String getElementName() {
return ELEMENTNAME;
}
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<").append(getElementName()).append(" ");
buf.append("jid=\"").append(getJID()).append("\" ");
buf.append("host=\"").append(getAddress()).append("\" ");
if (getPort() != 0) {
buf.append("port=\"").append(getPort()).append("\"");
}
else {
buf.append("zeroconf=\"_jabber.bytestreams\"");
}
buf.append("/>");
return buf.toString();
}
}
/**
* After selected a SOCKS5 stream host and successfully connecting, the target of the file
* transfer returns a byte stream packet with the stream host used extension.
*
* @author Alexander Wenckus
*/
public static class StreamHostUsed implements PacketExtension {
public String NAMESPACE = "";
public static String ELEMENTNAME = "streamhost-used";
private final String JID;
/**
* Default constructor.
*
* @param JID The JID of the selected stream host.
*/
public StreamHostUsed(final String JID) {
this.JID = JID;
}
/**
* Returns the JID of the selected stream host.
*
* @return Returns the JID of the selected stream host.
*/
public String getJID() {
return JID;
}
public String getNamespace() {
return NAMESPACE;
}
public String getElementName() {
return ELEMENTNAME;
}
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<").append(getElementName()).append(" ");
buf.append("jid=\"").append(getJID()).append("\" ");
buf.append("/>");
return buf.toString();
}
}
/**
* The packet sent by the stream initiator to the stream proxy to activate the connection.
*
* @author Alexander Wenckus
*/
public static class Activate implements PacketExtension {
public String NAMESPACE = "";
public static String ELEMENTNAME = "activate";
private final String target;
/**
* Default constructor specifying the target of the stream.
*
* @param target The target of the stream.
*/
public Activate(final String target) {
this.target = target;
}
/**
* Returns the target of the activation.
*
* @return Returns the target of the activation.
*/
public String getTarget() {
return target;
}
public String getNamespace() {
return NAMESPACE;
}
public String getElementName() {
return ELEMENTNAME;
}
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<").append(getElementName()).append(">");
buf.append(getTarget());
buf.append("</").append(getElementName()).append(">");
return buf.toString();
}
}
/**
* The stream can be either a TCP stream or a UDP stream.
*
* @author Alexander Wenckus
*/
public enum Mode {
/**
* A TCP based stream.
*/
tcp,
/**
* A UDP based stream.
*/
udp;
public static Mode fromName(String name) {
Mode mode;
try {
mode = Mode.valueOf(name);
}
catch (Exception ex) {
mode = tcp;
}
return mode;
}
}
}

View file

@ -0,0 +1,85 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.bytestreams.socks5.provider;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
import org.xmlpull.v1.XmlPullParser;
/**
* Parses a bytestream packet.
*
* @author Alexander Wenckus
*/
public class BytestreamsProvider implements IQProvider {
public IQ parseIQ(XmlPullParser parser) throws Exception {
boolean done = false;
Bytestream toReturn = new Bytestream();
String id = parser.getAttributeValue("", "sid");
String mode = parser.getAttributeValue("", "mode");
// streamhost
String JID = null;
String host = null;
String port = null;
int eventType;
String elementName;
while (!done) {
eventType = parser.next();
elementName = parser.getName();
if (eventType == XmlPullParser.START_TAG) {
if (elementName.equals(Bytestream.StreamHost.ELEMENTNAME)) {
JID = parser.getAttributeValue("", "jid");
host = parser.getAttributeValue("", "host");
port = parser.getAttributeValue("", "port");
}
else if (elementName.equals(Bytestream.StreamHostUsed.ELEMENTNAME)) {
toReturn.setUsedHost(parser.getAttributeValue("", "jid"));
}
else if (elementName.equals(Bytestream.Activate.ELEMENTNAME)) {
toReturn.setToActivate(parser.getAttributeValue("", "jid"));
}
}
else if (eventType == XmlPullParser.END_TAG) {
if (elementName.equals("streamhost")) {
if (port == null) {
toReturn.addStreamHost(JID, host);
}
else {
toReturn.addStreamHost(JID, host, Integer.parseInt(port));
}
JID = null;
host = null;
port = null;
}
else if (elementName.equals("query")) {
done = true;
}
}
}
toReturn.setMode((Bytestream.Mode.fromName(mode)));
toReturn.setSessionID(id);
return toReturn;
}
}

View file

@ -0,0 +1,702 @@
/**
*
* Copyright © 2009 Jonas Ådahl, 2011-2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.caps;
import org.jivesoftware.smack.AbstractConnectionListener;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.PacketInterceptor;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.filter.NotFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.util.Base64;
import org.jivesoftware.smack.util.Cache;
import org.jivesoftware.smackx.caps.cache.EntityCapsPersistentCache;
import org.jivesoftware.smackx.caps.packet.CapsExtension;
import org.jivesoftware.smackx.disco.NodeInformationProvider;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Feature;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity;
import org.jivesoftware.smackx.disco.packet.DiscoverItems.Item;
import org.jivesoftware.smackx.xdata.Form;
import org.jivesoftware.smackx.xdata.FormField;
import org.jivesoftware.smackx.xdata.packet.DataForm;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Queue;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* Keeps track of entity capabilities.
*
* @author Florian Schmaus
* @see <a href="http://www.xmpp.org/extensions/xep-0115.html">XEP-0115: Entity Capabilities</a>
*/
public class EntityCapsManager extends Manager {
private static final Logger LOGGER = Logger.getLogger(EntityCapsManager.class.getName());
public static final String NAMESPACE = "http://jabber.org/protocol/caps";
public static final String ELEMENT = "c";
private static final Map<String, MessageDigest> SUPPORTED_HASHES = new HashMap<String, MessageDigest>();
private static String DEFAULT_ENTITY_NODE = "http://www.igniterealtime.org/projects/smack";
protected static EntityCapsPersistentCache persistentCache;
private static boolean autoEnableEntityCaps = true;
private static Map<XMPPConnection, EntityCapsManager> instances = Collections
.synchronizedMap(new WeakHashMap<XMPPConnection, EntityCapsManager>());
private static final PacketFilter PRESENCES_WITH_CAPS = new AndFilter(new PacketTypeFilter(Presence.class), new PacketExtensionFilter(
ELEMENT, NAMESPACE));
private static final PacketFilter PRESENCES_WITHOUT_CAPS = new AndFilter(new PacketTypeFilter(Presence.class), new NotFilter(new PacketExtensionFilter(
ELEMENT, NAMESPACE)));
private static final PacketFilter PRESENCES = new PacketTypeFilter(Presence.class);
/**
* Map of (node + '#" + hash algorithm) to DiscoverInfo data
*/
protected static Map<String, DiscoverInfo> caps = new Cache<String, DiscoverInfo>(1000, -1);
/**
* Map of Full JID -&gt; DiscoverInfo/null. In case of c2s connection the
* key is formed as user@server/resource (resource is required) In case of
* link-local connection the key is formed as user@host (no resource) In
* case of a server or component the key is formed as domain
*/
protected static Map<String, NodeVerHash> jidCaps = new Cache<String, NodeVerHash>(10000, -1);
static {
XMPPConnection.addConnectionCreationListener(new ConnectionCreationListener() {
public void connectionCreated(XMPPConnection connection) {
getInstanceFor(connection);
}
});
try {
MessageDigest sha1MessageDigest = MessageDigest.getInstance("SHA-1");
SUPPORTED_HASHES.put("sha-1", sha1MessageDigest);
} catch (NoSuchAlgorithmException e) {
// Ignore
}
}
/**
* Set the default entity node that will be used for new EntityCapsManagers
*
* @param entityNode
*/
public static void setDefaultEntityNode(String entityNode) {
DEFAULT_ENTITY_NODE = entityNode;
}
/**
* Add DiscoverInfo to the database.
*
* @param nodeVer
* The node and verification String (e.g.
* "http://psi-im.org#q07IKJEyjvHSyhy//CH0CxmKi8w=").
* @param info
* DiscoverInfo for the specified node.
*/
public static void addDiscoverInfoByNode(String nodeVer, DiscoverInfo info) {
caps.put(nodeVer, info);
if (persistentCache != null)
persistentCache.addDiscoverInfoByNodePersistent(nodeVer, info);
}
/**
* Get the Node version (node#ver) of a JID. Returns a String or null if
* EntiyCapsManager does not have any information.
*
* @param jid
* the user (Full JID)
* @return the node version (node#ver) or null
*/
public static String getNodeVersionByJid(String jid) {
NodeVerHash nvh = jidCaps.get(jid);
if (nvh != null) {
return nvh.nodeVer;
} else {
return null;
}
}
public static NodeVerHash getNodeVerHashByJid(String jid) {
return jidCaps.get(jid);
}
/**
* Get the discover info given a user name. The discover info is returned if
* the user has a node#ver associated with it and the node#ver has a
* discover info associated with it.
*
* @param user
* user name (Full JID)
* @return the discovered info
*/
public static DiscoverInfo getDiscoverInfoByUser(String user) {
NodeVerHash nvh = jidCaps.get(user);
if (nvh == null)
return null;
return getDiscoveryInfoByNodeVer(nvh.nodeVer);
}
/**
* Retrieve DiscoverInfo for a specific node.
*
* @param nodeVer
* The node name (e.g.
* "http://psi-im.org#q07IKJEyjvHSyhy//CH0CxmKi8w=").
* @return The corresponding DiscoverInfo or null if none is known.
*/
public static DiscoverInfo getDiscoveryInfoByNodeVer(String nodeVer) {
DiscoverInfo info = caps.get(nodeVer);
if (info != null)
info = new DiscoverInfo(info);
return info;
}
/**
* Set the persistent cache implementation
*
* @param cache
* @throws IOException
*/
public static void setPersistentCache(EntityCapsPersistentCache cache) throws IOException {
if (persistentCache != null)
throw new IllegalStateException("Entity Caps Persistent Cache was already set");
persistentCache = cache;
persistentCache.replay();
}
/**
* Sets the maximum Cache size for the JID to nodeVer Cache
*
* @param maxCacheSize
*/
@SuppressWarnings("rawtypes")
public static void setJidCapsMaxCacheSize(int maxCacheSize) {
((Cache) jidCaps).setMaxCacheSize(maxCacheSize);
}
/**
* Sets the maximum Cache size for the nodeVer to DiscoverInfo Cache
*
* @param maxCacheSize
*/
@SuppressWarnings("rawtypes")
public static void setCapsMaxCacheSize(int maxCacheSize) {
((Cache) caps).setMaxCacheSize(maxCacheSize);
}
private ServiceDiscoveryManager sdm;
private boolean entityCapsEnabled;
private String currentCapsVersion;
private boolean presenceSend = false;
private Queue<String> lastLocalCapsVersions = new ConcurrentLinkedQueue<String>();
/**
* The entity node String used by this EntityCapsManager instance.
*/
private String entityNode = DEFAULT_ENTITY_NODE;
private EntityCapsManager(XMPPConnection connection) {
super(connection);
this.sdm = ServiceDiscoveryManager.getInstanceFor(connection);
instances.put(connection, this);
connection.addConnectionListener(new AbstractConnectionListener() {
@Override
public void connectionClosed() {
presenceSend = false;
}
@Override
public void connectionClosedOnError(Exception e) {
presenceSend = false;
}
});
// This calculates the local entity caps version
updateLocalEntityCaps();
if (autoEnableEntityCaps)
enableEntityCaps();
connection.addPacketListener(new PacketListener() {
// Listen for remote presence stanzas with the caps extension
// If we receive such a stanza, record the JID and nodeVer
@Override
public void processPacket(Packet packet) {
if (!entityCapsEnabled())
return;
CapsExtension ext = (CapsExtension) packet.getExtension(EntityCapsManager.ELEMENT,
EntityCapsManager.NAMESPACE);
String hash = ext.getHash().toLowerCase(Locale.US);
if (!SUPPORTED_HASHES.containsKey(hash))
return;
String from = packet.getFrom();
String node = ext.getNode();
String ver = ext.getVer();
jidCaps.put(from, new NodeVerHash(node, ver, hash));
}
}, PRESENCES_WITH_CAPS);
connection.addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
// always remove the JID from the map, even if entityCaps are
// disabled
String from = packet.getFrom();
jidCaps.remove(from);
}
}, PRESENCES_WITHOUT_CAPS);
connection.addPacketSendingListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
presenceSend = true;
}
}, PRESENCES);
// Intercept presence packages and add caps data when intended.
// XEP-0115 specifies that a client SHOULD include entity capabilities
// with every presence notification it sends.
PacketInterceptor packetInterceptor = new PacketInterceptor() {
public void interceptPacket(Packet packet) {
if (!entityCapsEnabled)
return;
CapsExtension caps = new CapsExtension(entityNode, getCapsVersion(), "sha-1");
packet.addExtension(caps);
}
};
connection.addPacketInterceptor(packetInterceptor, PRESENCES);
// It's important to do this as last action. Since it changes the
// behavior of the SDM in some ways
sdm.setEntityCapsManager(this);
}
public static synchronized EntityCapsManager getInstanceFor(XMPPConnection connection) {
if (SUPPORTED_HASHES.size() <= 0)
throw new IllegalStateException("No supported hashes for EntityCapsManager");
EntityCapsManager entityCapsManager = instances.get(connection);
if (entityCapsManager == null) {
entityCapsManager = new EntityCapsManager(connection);
}
return entityCapsManager;
}
public synchronized void enableEntityCaps() {
// Add Entity Capabilities (XEP-0115) feature node.
sdm.addFeature(NAMESPACE);
updateLocalEntityCaps();
entityCapsEnabled = true;
}
public synchronized void disableEntityCaps() {
entityCapsEnabled = false;
sdm.removeFeature(NAMESPACE);
}
public boolean entityCapsEnabled() {
return entityCapsEnabled;
}
public void setEntityNode(String entityNode) throws NotConnectedException {
this.entityNode = entityNode;
updateLocalEntityCaps();
}
/**
* Remove a record telling what entity caps node a user has.
*
* @param user
* the user (Full JID)
*/
public void removeUserCapsNode(String user) {
jidCaps.remove(user);
}
/**
* Get our own caps version. The version depends on the enabled features. A
* caps version looks like '66/0NaeaBKkwk85efJTGmU47vXI='
*
* @return our own caps version
*/
public String getCapsVersion() {
return currentCapsVersion;
}
/**
* Returns the local entity's NodeVer (e.g.
* "http://www.igniterealtime.org/projects/smack/#66/0NaeaBKkwk85efJTGmU47vXI=
* )
*
* @return the local NodeVer
*/
public String getLocalNodeVer() {
return entityNode + '#' + getCapsVersion();
}
/**
* Returns true if Entity Caps are supported by a given JID
*
* @param jid
* @return true if the entity supports Entity Capabilities.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
*/
public boolean areEntityCapsSupported(String jid) throws NoResponseException, XMPPErrorException, NotConnectedException {
return sdm.supportsFeature(jid, NAMESPACE);
}
/**
* Returns true if Entity Caps are supported by the local service/server
*
* @return true if the user's server supports Entity Capabilities.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
*/
public boolean areEntityCapsSupportedByServer() throws NoResponseException, XMPPErrorException, NotConnectedException {
return areEntityCapsSupported(connection().getServiceName());
}
/**
* Updates the local user Entity Caps information with the data provided
*
* If we are connected and there was already a presence send, another
* presence is send to inform others about your new Entity Caps node string.
*
*/
public void updateLocalEntityCaps() {
XMPPConnection connection = connection();
DiscoverInfo discoverInfo = new DiscoverInfo();
discoverInfo.setType(IQ.Type.RESULT);
discoverInfo.setNode(getLocalNodeVer());
if (connection != null)
discoverInfo.setFrom(connection.getUser());
sdm.addDiscoverInfoTo(discoverInfo);
currentCapsVersion = generateVerificationString(discoverInfo, "sha-1");
addDiscoverInfoByNode(entityNode + '#' + currentCapsVersion, discoverInfo);
if (lastLocalCapsVersions.size() > 10) {
String oldCapsVersion = lastLocalCapsVersions.poll();
sdm.removeNodeInformationProvider(entityNode + '#' + oldCapsVersion);
}
lastLocalCapsVersions.add(currentCapsVersion);
caps.put(currentCapsVersion, discoverInfo);
if (connection != null)
jidCaps.put(connection.getUser(), new NodeVerHash(entityNode, currentCapsVersion, "sha-1"));
final List<Identity> identities = new LinkedList<Identity>(ServiceDiscoveryManager.getInstanceFor(connection).getIdentities());
sdm.setNodeInformationProvider(entityNode + '#' + currentCapsVersion, new NodeInformationProvider() {
List<String> features = sdm.getFeaturesList();
List<PacketExtension> packetExtensions = sdm.getExtendedInfoAsList();
@Override
public List<Item> getNodeItems() {
return null;
}
@Override
public List<String> getNodeFeatures() {
return features;
}
@Override
public List<Identity> getNodeIdentities() {
return identities;
}
@Override
public List<PacketExtension> getNodePacketExtensions() {
return packetExtensions;
}
});
// Send an empty presence, and let the packet intercepter
// add a <c/> node to it.
// See http://xmpp.org/extensions/xep-0115.html#advertise
// We only send a presence packet if there was already one send
// to respect ConnectionConfiguration.isSendPresence()
if (connection != null && connection.isAuthenticated() && presenceSend) {
Presence presence = new Presence(Presence.Type.available);
try {
connection.sendPacket(presence);
}
catch (NotConnectedException e) {
LOGGER.log(Level.WARNING, "Could could not update presence with caps info", e);
}
}
}
/**
* Verify DisoverInfo and Caps Node as defined in XEP-0115 5.4 Processing
* Method
*
* @see <a href="http://xmpp.org/extensions/xep-0115.html#ver-proc">XEP-0115
* 5.4 Processing Method</a>
*
* @param ver
* @param hash
* @param info
* @return true if it's valid and should be cache, false if not
*/
public static boolean verifyDiscoverInfoVersion(String ver, String hash, DiscoverInfo info) {
// step 3.3 check for duplicate identities
if (info.containsDuplicateIdentities())
return false;
// step 3.4 check for duplicate features
if (info.containsDuplicateFeatures())
return false;
// step 3.5 check for well-formed packet extensions
if (verifyPacketExtensions(info))
return false;
String calculatedVer = generateVerificationString(info, hash);
if (!ver.equals(calculatedVer))
return false;
return true;
}
/**
*
* @param info
* @return true if the packet extensions is ill-formed
*/
protected static boolean verifyPacketExtensions(DiscoverInfo info) {
List<FormField> foundFormTypes = new LinkedList<FormField>();
for (PacketExtension pe : info.getExtensions()) {
if (pe.getNamespace().equals(Form.NAMESPACE)) {
DataForm df = (DataForm) pe;
for (FormField f : df.getFields()) {
if (f.getVariable().equals("FORM_TYPE")) {
for (FormField fft : foundFormTypes) {
if (f.equals(fft))
return true;
}
foundFormTypes.add(f);
}
}
}
}
return false;
}
/**
* Generates a XEP-115 Verification String
*
* @see <a href="http://xmpp.org/extensions/xep-0115.html#ver">XEP-115
* Verification String</a>
*
* @param discoverInfo
* @param hash
* the used hash function
* @return The generated verification String or null if the hash is not
* supported
*/
protected static String generateVerificationString(DiscoverInfo discoverInfo, String hash) {
MessageDigest md = SUPPORTED_HASHES.get(hash.toLowerCase(Locale.US));
if (md == null)
return null;
DataForm extendedInfo = (DataForm) discoverInfo.getExtension(Form.ELEMENT, Form.NAMESPACE);
// 1. Initialize an empty string S ('sb' in this method).
StringBuilder sb = new StringBuilder(); // Use StringBuilder as we don't
// need thread-safe StringBuffer
// 2. Sort the service discovery identities by category and then by
// type and then by xml:lang
// (if it exists), formatted as CATEGORY '/' [TYPE] '/' [LANG] '/'
// [NAME]. Note that each slash is included even if the LANG or
// NAME is not included (in accordance with XEP-0030, the category and
// type MUST be included.
SortedSet<DiscoverInfo.Identity> sortedIdentities = new TreeSet<DiscoverInfo.Identity>();
for (DiscoverInfo.Identity i : discoverInfo.getIdentities())
sortedIdentities.add(i);
// 3. For each identity, append the 'category/type/lang/name' to S,
// followed by the '<' character.
for (DiscoverInfo.Identity identity : sortedIdentities) {
sb.append(identity.getCategory());
sb.append("/");
sb.append(identity.getType());
sb.append("/");
sb.append(identity.getLanguage() == null ? "" : identity.getLanguage());
sb.append("/");
sb.append(identity.getName() == null ? "" : identity.getName());
sb.append("<");
}
// 4. Sort the supported service discovery features.
SortedSet<String> features = new TreeSet<String>();
for (Feature f : discoverInfo.getFeatures())
features.add(f.getVar());
// 5. For each feature, append the feature to S, followed by the '<'
// character
for (String f : features) {
sb.append(f);
sb.append("<");
}
// only use the data form for calculation is it has a hidden FORM_TYPE
// field
// see XEP-0115 5.4 step 3.6
if (extendedInfo != null && extendedInfo.hasHiddenFormTypeField()) {
synchronized (extendedInfo) {
// 6. If the service discovery information response includes
// XEP-0128 data forms, sort the forms by the FORM_TYPE (i.e.,
// by the XML character data of the <value/> element).
SortedSet<FormField> fs = new TreeSet<FormField>(new Comparator<FormField>() {
public int compare(FormField f1, FormField f2) {
return f1.getVariable().compareTo(f2.getVariable());
}
});
FormField ft = null;
for (FormField f : extendedInfo.getFields()) {
if (!f.getVariable().equals("FORM_TYPE")) {
fs.add(f);
} else {
ft = f;
}
}
// Add FORM_TYPE values
if (ft != null) {
formFieldValuesToCaps(ft.getValues(), sb);
}
// 7. 3. For each field other than FORM_TYPE:
// 1. Append the value of the "var" attribute, followed by the
// '<' character.
// 2. Sort values by the XML character data of the <value/>
// element.
// 3. For each <value/> element, append the XML character data,
// followed by the '<' character.
for (FormField f : fs) {
sb.append(f.getVariable());
sb.append("<");
formFieldValuesToCaps(f.getValues(), sb);
}
}
}
// 8. Ensure that S is encoded according to the UTF-8 encoding (RFC
// 3269).
// 9. Compute the verification string by hashing S using the algorithm
// specified in the 'hash' attribute (e.g., SHA-1 as defined in RFC
// 3174).
// The hashed data MUST be generated with binary output and
// encoded using Base64 as specified in Section 4 of RFC 4648
// (note: the Base64 output MUST NOT include whitespace and MUST set
// padding bits to zero).
byte[] digest = md.digest(sb.toString().getBytes());
return Base64.encodeBytes(digest);
}
private static void formFieldValuesToCaps(List<String> i, StringBuilder sb) {
SortedSet<String> fvs = new TreeSet<String>();
for (String s : i) {
fvs.add(s);
}
for (String fv : fvs) {
sb.append(fv);
sb.append("<");
}
}
public static class NodeVerHash {
private String node;
private String hash;
private String ver;
private String nodeVer;
NodeVerHash(String node, String ver, String hash) {
this.node = node;
this.ver = ver;
this.hash = hash;
nodeVer = node + "#" + ver;
}
public String getNodeVer() {
return nodeVer;
}
public String getNode() {
return node;
}
public String getHash() {
return hash;
}
public String getVer() {
return ver;
}
}
}

View file

@ -0,0 +1,41 @@
/**
*
* Copyright © 2011-2013 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.caps.cache;
import java.io.IOException;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
public interface EntityCapsPersistentCache {
/**
* Add an DiscoverInfo to the persistent Cache
*
* @param node
* @param info
*/
void addDiscoverInfoByNodePersistent(String node, DiscoverInfo info);
/**
* Replay the Caches data into EntityCapsManager
*/
void replay() throws IOException;
/**
* Empty the Cache
*/
void emptyCache();
}

View file

@ -0,0 +1,196 @@
/**
*
* Copyright © 2011 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.caps.cache;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.Base32Encoder;
import org.jivesoftware.smack.util.StringEncoder;
import org.jivesoftware.smackx.caps.EntityCapsManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
import org.jivesoftware.smackx.disco.provider.DiscoverInfoProvider;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
/**
* Simple implementation of an EntityCapsPersistentCache that uses a directory
* to store the Caps information for every known node. Every node is represented
* by a file.
*
* @author Florian Schmaus
*
*/
public class SimpleDirectoryPersistentCache implements EntityCapsPersistentCache {
private static final Logger LOGGER = Logger.getLogger(SimpleDirectoryPersistentCache.class.getName());
private File cacheDir;
private StringEncoder filenameEncoder;
/**
* Creates a new SimpleDirectoryPersistentCache Object. Make sure that the
* cacheDir exists and that it's an directory.
* <p>
* Default filename encoder {@link Base32Encoder}, as this will work on all
* file systems, both case sensitive and case insensitive. It does however
* produce longer filenames.
*
* @param cacheDir
*/
public SimpleDirectoryPersistentCache(File cacheDir) {
this(cacheDir, Base32Encoder.getInstance());
}
/**
* Creates a new SimpleDirectoryPersistentCache Object. Make sure that the
* cacheDir exists and that it's an directory.
*
* If your cacheDir is case insensitive then make sure to set the
* StringEncoder to {@link Base32Encoder} (which is the default).
*
* @param cacheDir The directory where the cache will be stored.
* @param filenameEncoder Encodes the node string into a filename.
*/
public SimpleDirectoryPersistentCache(File cacheDir, StringEncoder filenameEncoder) {
if (!cacheDir.exists())
throw new IllegalStateException("Cache directory \"" + cacheDir + "\" does not exist");
if (!cacheDir.isDirectory())
throw new IllegalStateException("Cache directory \"" + cacheDir + "\" is not a directory");
this.cacheDir = cacheDir;
this.filenameEncoder = filenameEncoder;
}
@Override
public void addDiscoverInfoByNodePersistent(String node, DiscoverInfo info) {
String filename = filenameEncoder.encode(node);
File nodeFile = new File(cacheDir, filename);
try {
if (nodeFile.createNewFile())
writeInfoToFile(nodeFile, info);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to write disco info to file", e);
}
}
@Override
public void replay() throws IOException {
File[] files = cacheDir.listFiles();
for (File f : files) {
String node = filenameEncoder.decode(f.getName());
DiscoverInfo info = restoreInfoFromFile(f);
if (info == null)
continue;
EntityCapsManager.addDiscoverInfoByNode(node, info);
}
}
public void emptyCache() {
File[] files = cacheDir.listFiles();
for (File f : files) {
f.delete();
}
}
/**
* Writes the DiscoverInfo packet to an file
*
* @param file
* @param info
* @throws IOException
*/
private static void writeInfoToFile(File file, DiscoverInfo info) throws IOException {
DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
try {
dos.writeUTF(info.toXML().toString());
} finally {
dos.close();
}
}
/**
* Tries to restore an DiscoverInfo packet from a file.
*
* @param file
* @return the restored DiscoverInfo
* @throws IOException
*/
private static DiscoverInfo restoreInfoFromFile(File file) throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream(file));
String fileContent = null;
String id;
String from;
String to;
try {
fileContent = dis.readUTF();
} finally {
dis.close();
}
if (fileContent == null)
return null;
Reader reader = new StringReader(fileContent);
XmlPullParser parser;
try {
parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(reader);
} catch (XmlPullParserException xppe) {
LOGGER.log(Level.SEVERE, "Exception initializing parser", xppe);
return null;
}
DiscoverInfo iqPacket;
IQProvider provider = new DiscoverInfoProvider();
// Parse the IQ, we only need the id
try {
parser.next();
id = parser.getAttributeValue("", "id");
from = parser.getAttributeValue("", "from");
to = parser.getAttributeValue("", "to");
parser.next();
} catch (XmlPullParserException e1) {
return null;
}
try {
iqPacket = (DiscoverInfo) provider.parseIQ(parser);
} catch (Exception e) {
return null;
}
iqPacket.setPacketID(id);
iqPacket.setFrom(from);
iqPacket.setTo(to);
iqPacket.setType(IQ.Type.RESULT);
return iqPacket;
}
}

View file

@ -0,0 +1,66 @@
/**
*
* Copyright © 2009 Jonas Ådahl, 2011-2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.caps.packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.caps.EntityCapsManager;
public class CapsExtension implements PacketExtension {
private final String node, ver, hash;
public CapsExtension(String node, String version, String hash) {
this.node = node;
this.ver = version;
this.hash = hash;
}
public String getElementName() {
return EntityCapsManager.ELEMENT;
}
public String getNamespace() {
return EntityCapsManager.NAMESPACE;
}
public String getNode() {
return node;
}
public String getVer() {
return ver;
}
public String getHash() {
return hash;
}
/*
* <c xmlns='http://jabber.org/protocol/caps'
* hash='sha-1'
* node='http://code.google.com/p/exodus'
* ver='QgayPKawpkPSDYmwT/WM94uAlu0='/>
*
*/
public CharSequence toXML() {
XmlStringBuilder xml = new XmlStringBuilder(this);
xml.attribute("hash", hash).attribute("node", node).attribute("ver", ver);
xml.closeEmptyElement();
return xml;
}
}

View file

@ -0,0 +1,58 @@
/**
*
* Copyright © 2009 Jonas Ådahl, 2011-2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.caps.provider;
import java.io.IOException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.jivesoftware.smackx.caps.EntityCapsManager;
import org.jivesoftware.smackx.caps.packet.CapsExtension;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
public class CapsExtensionProvider implements PacketExtensionProvider {
public PacketExtension parseExtension(XmlPullParser parser) throws XmlPullParserException, IOException,
SmackException {
String hash = null;
String version = null;
String node = null;
if (parser.getEventType() == XmlPullParser.START_TAG
&& parser.getName().equalsIgnoreCase(EntityCapsManager.ELEMENT)) {
hash = parser.getAttributeValue(null, "hash");
version = parser.getAttributeValue(null, "ver");
node = parser.getAttributeValue(null, "node");
} else {
throw new SmackException("Malformed Caps element");
}
parser.next();
if (!(parser.getEventType() == XmlPullParser.END_TAG
&& parser.getName().equalsIgnoreCase(EntityCapsManager.ELEMENT))) {
throw new SmackException("Malformed nested Caps element");
}
if (hash != null && version != null && node != null) {
return new CapsExtension(node, version, hash);
} else {
throw new SmackException("Caps elment with missing attributes");
}
}
}

View file

@ -0,0 +1,47 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.chatstates;
/**
* Represents the current state of a users interaction with another user. Implemented according to
* <a href="http://www.xmpp.org/extensions/xep-0085.html">XEP-0085</a>.
*
* @author Alexander Wenckus
*/
public enum ChatState {
/**
* User is actively participating in the chat session.
*/
active,
/**
* User is composing a message.
*/
composing,
/**
* User had been composing but now has stopped.
*/
paused,
/**
* User has not been actively participating in the chat session.
*/
inactive,
/**
* User has effectively ended their participation in the chat session.
*/
gone
}

View file

@ -0,0 +1,37 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.chatstates;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.MessageListener;
/**
* Events for when the state of a user in a chat changes.
*
* @author Alexander Wenckus
*/
public interface ChatStateListener extends MessageListener {
/**
* Fired when the state of a chat with another user changes.
*
* @param chat the chat in which the state has changed.
* @param state the new state of the participant.
*/
void stateChanged(Chat chat, ChatState state);
}

View file

@ -0,0 +1,190 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.chatstates;
import java.util.Map;
import java.util.WeakHashMap;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketInterceptor;
import org.jivesoftware.smack.filter.NotFilter;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.chatstates.packet.ChatStateExtension;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
/**
* Handles chat state for all chats on a particular XMPPConnection. This class manages both the
* packet extensions and the disco response necessary for compliance with
* <a href="http://www.xmpp.org/extensions/xep-0085.html">XEP-0085</a>.
*
* NOTE: {@link org.jivesoftware.smackx.chatstates.ChatStateManager#getInstance(org.jivesoftware.smack.XMPPConnection)}
* needs to be called in order for the listeners to be registered appropriately with the connection.
* If this does not occur you will not receive the update notifications.
*
* @author Alexander Wenckus
* @see org.jivesoftware.smackx.chatstates.ChatState
* @see org.jivesoftware.smackx.chatstates.packet.ChatStateExtension
*/
public class ChatStateManager extends Manager {
public static final String NAMESPACE = "http://jabber.org/protocol/chatstates";
private static final Map<XMPPConnection, ChatStateManager> INSTANCES =
new WeakHashMap<XMPPConnection, ChatStateManager>();
private static final PacketFilter filter = new NotFilter(new PacketExtensionFilter(NAMESPACE));
/**
* Returns the ChatStateManager related to the XMPPConnection and it will create one if it does
* not yet exist.
*
* @param connection the connection to return the ChatStateManager
* @return the ChatStateManager related the the connection.
*/
public static synchronized ChatStateManager getInstance(final XMPPConnection connection) {
ChatStateManager manager = INSTANCES.get(connection);
if (manager == null) {
manager = new ChatStateManager(connection);
}
return manager;
}
private final OutgoingMessageInterceptor outgoingInterceptor = new OutgoingMessageInterceptor();
private final IncomingMessageInterceptor incomingInterceptor = new IncomingMessageInterceptor();
/**
* Maps chat to last chat state.
*/
private final Map<Chat, ChatState> chatStates = new WeakHashMap<Chat, ChatState>();
private final ChatManager chatManager;
private ChatStateManager(XMPPConnection connection) {
super(connection);
chatManager = ChatManager.getInstanceFor(connection);
chatManager.addOutgoingMessageInterceptor(outgoingInterceptor, filter);
chatManager.addChatListener(incomingInterceptor);
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(NAMESPACE);
INSTANCES.put(connection, this);
}
/**
* Sets the current state of the provided chat. This method will send an empty bodied Message
* packet with the state attached as a {@link org.jivesoftware.smack.packet.PacketExtension}, if
* and only if the new chat state is different than the last state.
*
* @param newState the new state of the chat
* @param chat the chat.
* @throws NotConnectedException
*/
public void setCurrentState(ChatState newState, Chat chat) throws NotConnectedException {
if(chat == null || newState == null) {
throw new IllegalArgumentException("Arguments cannot be null.");
}
if(!updateChatState(chat, newState)) {
return;
}
Message message = new Message();
ChatStateExtension extension = new ChatStateExtension(newState);
message.addExtension(extension);
chat.sendMessage(message);
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ChatStateManager that = (ChatStateManager) o;
return connection().equals(that.connection());
}
public int hashCode() {
return connection().hashCode();
}
private synchronized boolean updateChatState(Chat chat, ChatState newState) {
ChatState lastChatState = chatStates.get(chat);
if (lastChatState != newState) {
chatStates.put(chat, newState);
return true;
}
return false;
}
private void fireNewChatState(Chat chat, ChatState state) {
for (MessageListener listener : chat.getListeners()) {
if (listener instanceof ChatStateListener) {
((ChatStateListener) listener).stateChanged(chat, state);
}
}
}
private class OutgoingMessageInterceptor implements PacketInterceptor {
public void interceptPacket(Packet packet) {
Message message = (Message) packet;
Chat chat = chatManager.getThreadChat(message.getThread());
if (chat == null) {
return;
}
if (updateChatState(chat, ChatState.active)) {
message.addExtension(new ChatStateExtension(ChatState.active));
}
}
}
private class IncomingMessageInterceptor implements ChatManagerListener, MessageListener {
public void chatCreated(final Chat chat, boolean createdLocally) {
chat.addMessageListener(this);
}
public void processMessage(Chat chat, Message message) {
PacketExtension extension = message.getExtension(NAMESPACE);
if (extension == null) {
return;
}
ChatState state;
try {
state = ChatState.valueOf(extension.getElementName());
}
catch (Exception ex) {
return;
}
fireNewChatState(chat, state);
}
}
}

View file

@ -0,0 +1,70 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.chatstates.packet;
import org.jivesoftware.smackx.chatstates.ChatState;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.xmlpull.v1.XmlPullParser;
/**
* Represents a chat state which is an extension to message packets which is used to indicate
* the current status of a chat participant.
*
* @author Alexander Wenckus
* @see org.jivesoftware.smackx.chatstates.ChatState
*/
public class ChatStateExtension implements PacketExtension {
private ChatState state;
/**
* Default constructor. The argument provided is the state that the extension will represent.
*
* @param state the state that the extension represents.
*/
public ChatStateExtension(ChatState state) {
this.state = state;
}
public String getElementName() {
return state.name();
}
public String getNamespace() {
return "http://jabber.org/protocol/chatstates";
}
public String toXML() {
return "<" + getElementName() + " xmlns=\"" + getNamespace() + "\" />";
}
public static class Provider implements PacketExtensionProvider {
public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
ChatState state;
try {
state = ChatState.valueOf(parser.getName());
}
catch (Exception ex) {
state = ChatState.active;
}
return new ChatStateExtension(state);
}
}
}

View file

@ -0,0 +1,454 @@
/**
*
* Copyright 2005-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.commands;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smackx.commands.packet.AdHocCommandData;
import org.jivesoftware.smackx.xdata.Form;
import java.util.List;
/**
* An ad-hoc command is responsible for executing the provided service and
* storing the result of the execution. Each new request will create a new
* instance of the command, allowing information related to executions to be
* stored in it. For example suppose that a command that retrieves the list of
* users on a server is implemented. When the command is executed it gets that
* list and the result is stored as a form in the command instance, i.e. the
* <code>getForm</code> method retrieves a form with all the users.
* <p>
* Each command has a <tt>node</tt> that should be unique within a given JID.
* <p>
* Commands may have zero or more stages. Each stage is usually used for
* gathering information required for the command execution. Users are able to
* move forward or backward across the different stages. Commands may not be
* cancelled while they are being executed. However, users may request the
* "cancel" action when submitting a stage response indicating that the command
* execution should be aborted. Thus, releasing any collected information.
* Commands that require user interaction (i.e. have more than one stage) will
* have to provide the data forms the user must complete in each stage and the
* allowed actions the user might perform during each stage (e.g. go to the
* previous stage or go to the next stage).
* <p>
* All the actions may throw an XMPPException if there is a problem executing
* them. The <code>XMPPError</code> of that exception may have some specific
* information about the problem. The possible extensions are:
*
* <li><i>malformed-action</i>. Extension of a <i>bad-request</i> error.</li>
* <li><i>bad-action</i>. Extension of a <i>bad-request</i> error.</li>
* <li><i>bad-locale</i>. Extension of a <i>bad-request</i> error.</li>
* <li><i>bad-payload</i>. Extension of a <i>bad-request</i> error.</li>
* <li><i>bad-sessionid</i>. Extension of a <i>bad-request</i> error.</li>
* <li><i>session-expired</i>. Extension of a <i>not-allowed</i> error.</li>
* <p>
* See the <code>SpecificErrorCondition</code> class for detailed description
* of each one.
* <p>
* Use the <code>getSpecificErrorConditionFrom</code> to obtain the specific
* information from an <code>XMPPError</code>.
*
* @author Gabriel Guardincerri
*
*/
public abstract class AdHocCommand {
// TODO: Analyze the redesign of command by having an ExecutionResponse as a
// TODO: result to the execution of every action. That result should have all the
// TODO: information related to the execution, e.g. the form to fill. Maybe this
// TODO: design is more intuitive and simpler than the current one that has all in
// TODO: one class.
private AdHocCommandData data;
public AdHocCommand() {
super();
data = new AdHocCommandData();
}
/**
* Returns the specific condition of the <code>error</code> or <tt>null</tt> if the
* error doesn't have any.
*
* @param error the error the get the specific condition from.
* @return the specific condition of this error, or null if it doesn't have
* any.
*/
public static SpecificErrorCondition getSpecificErrorCondition(XMPPError error) {
// This method is implemented to provide an easy way of getting a packet
// extension of the XMPPError.
for (SpecificErrorCondition condition : SpecificErrorCondition.values()) {
if (error.getExtension(condition.toString(),
AdHocCommandData.SpecificError.namespace) != null) {
return condition;
}
}
return null;
}
/**
* Set the the human readable name of the command, usually used for
* displaying in a UI.
*
* @param name the name.
*/
public void setName(String name) {
data.setName(name);
}
/**
* Returns the human readable name of the command.
*
* @return the human readable name of the command
*/
public String getName() {
return data.getName();
}
/**
* Sets the unique identifier of the command. This value must be unique for
* the <code>OwnerJID</code>.
*
* @param node the unique identifier of the command.
*/
public void setNode(String node) {
data.setNode(node);
}
/**
* Returns the unique identifier of the command. It is unique for the
* <code>OwnerJID</code>.
*
* @return the unique identifier of the command.
*/
public String getNode() {
return data.getNode();
}
/**
* Returns the full JID of the owner of this command. This JID is the "to" of a
* execution request.
*
* @return the owner JID.
*/
public abstract String getOwnerJID();
/**
* Returns the notes that the command has at the current stage.
*
* @return a list of notes.
*/
public List<AdHocCommandNote> getNotes() {
return data.getNotes();
}
/**
* Adds a note to the current stage. This should be used when setting a
* response to the execution of an action. All the notes added here are
* returned by the {@link #getNotes} method during the current stage.
* Once the stage changes all the notes are discarded.
*
* @param note the note.
*/
protected void addNote(AdHocCommandNote note) {
data.addNote(note);
}
public String getRaw() {
return data.getChildElementXML();
}
/**
* Returns the form of the current stage. Usually it is the form that must
* be answered to execute the next action. If that is the case it should be
* used by the requester to fill all the information that the executor needs
* to continue to the next stage. It can also be the result of the
* execution.
*
* @return the form of the current stage to fill out or the result of the
* execution.
*/
public Form getForm() {
if (data.getForm() == null) {
return null;
}
else {
return new Form(data.getForm());
}
}
/**
* Sets the form of the current stage. This should be used when setting a
* response. It could be a form to fill out the information needed to go to
* the next stage or the result of an execution.
*
* @param form the form of the current stage to fill out or the result of the
* execution.
*/
protected void setForm(Form form) {
data.setForm(form.getDataFormToSend());
}
/**
* Executes the command. This is invoked only on the first stage of the
* command. It is invoked on every command. If there is a problem executing
* the command it throws an XMPPException.
*
* @throws XMPPErrorException if there is an error executing the command.
* @throws NotConnectedException
*/
public abstract void execute() throws NoResponseException, XMPPErrorException, NotConnectedException;
/**
* Executes the next action of the command with the information provided in
* the <code>response</code>. This form must be the answer form of the
* previous stage. This method will be only invoked for commands that have one
* or more stages. If there is a problem executing the command it throws an
* XMPPException.
*
* @param response the form answer of the previous stage.
* @throws XMPPErrorException if there is a problem executing the command.
* @throws NotConnectedException
*/
public abstract void next(Form response) throws NoResponseException, XMPPErrorException, NotConnectedException;
/**
* Completes the command execution with the information provided in the
* <code>response</code>. This form must be the answer form of the
* previous stage. This method will be only invoked for commands that have one
* or more stages. If there is a problem executing the command it throws an
* XMPPException.
*
* @param response the form answer of the previous stage.
* @throws XMPPErrorException if there is a problem executing the command.
* @throws NotConnectedException
*/
public abstract void complete(Form response) throws NoResponseException, XMPPErrorException, NotConnectedException;
/**
* Goes to the previous stage. The requester is asking to re-send the
* information of the previous stage. The command must change it state to
* the previous one. If there is a problem executing the command it throws
* an XMPPException.
*
* @throws XMPPErrorException if there is a problem executing the command.
* @throws NotConnectedException
*/
public abstract void prev() throws NoResponseException, XMPPErrorException, NotConnectedException;
/**
* Cancels the execution of the command. This can be invoked on any stage of
* the execution. If there is a problem executing the command it throws an
* XMPPException.
*
* @throws XMPPErrorException if there is a problem executing the command.
* @throws NotConnectedException
*/
public abstract void cancel() throws NoResponseException, XMPPErrorException, NotConnectedException;
/**
* Returns a collection with the allowed actions based on the current stage.
* Possible actions are: {@link Action#prev prev}, {@link Action#next next} and
* {@link Action#complete complete}. This method will be only invoked for commands that
* have one or more stages.
*
* @return a collection with the allowed actions based on the current stage
* as defined in the SessionData.
*/
protected List<Action> getActions() {
return data.getActions();
}
/**
* Add an action to the current stage available actions. This should be used
* when creating a response.
*
* @param action the action.
*/
protected void addActionAvailable(Action action) {
data.addAction(action);
}
/**
* Returns the action available for the current stage which is
* considered the equivalent to "execute". When the requester sends his
* reply, if no action was defined in the command then the action will be
* assumed "execute" thus assuming the action returned by this method. This
* method will never be invoked for commands that have no stages.
*
* @return the action available for the current stage which is considered
* the equivalent to "execute".
*/
protected Action getExecuteAction() {
return data.getExecuteAction();
}
/**
* Sets which of the actions available for the current stage is
* considered the equivalent to "execute". This should be used when setting
* a response. When the requester sends his reply, if no action was defined
* in the command then the action will be assumed "execute" thus assuming
* the action returned by this method.
*
* @param action the action.
*/
protected void setExecuteAction(Action action) {
data.setExecuteAction(action);
}
/**
* Returns the status of the current stage.
*
* @return the current status.
*/
public Status getStatus() {
return data.getStatus();
}
/**
* Sets the data of the current stage. This should not used.
*
* @param data the data.
*/
void setData(AdHocCommandData data) {
this.data = data;
}
/**
* Gets the data of the current stage. This should not used.
*
* @return the data.
*/
AdHocCommandData getData() {
return data;
}
/**
* Returns true if the <code>action</code> is available in the current stage.
* The {@link Action#cancel cancel} action is always allowed. To define the
* available actions use the <code>addActionAvailable</code> method.
*
* @param action
* The action to check if it is available.
* @return True if the action is available for the current stage.
*/
protected boolean isValidAction(Action action) {
return getActions().contains(action) || Action.cancel.equals(action);
}
/**
* The status of the stage in the adhoc command.
*/
public enum Status {
/**
* The command is being executed.
*/
executing,
/**
* The command has completed. The command session has ended.
*/
completed,
/**
* The command has been canceled. The command session has ended.
*/
canceled
}
public enum Action {
/**
* The command should be executed or continue to be executed. This is
* the default value.
*/
execute,
/**
* The command should be canceled.
*/
cancel,
/**
* The command should be digress to the previous stage of execution.
*/
prev,
/**
* The command should progress to the next stage of execution.
*/
next,
/**
* The command should be completed (if possible).
*/
complete,
/**
* The action is unknow. This is used when a recieved message has an
* unknown action. It must not be used to send an execution request.
*/
unknown
}
public enum SpecificErrorCondition {
/**
* The responding JID cannot accept the specified action.
*/
badAction("bad-action"),
/**
* The responding JID does not understand the specified action.
*/
malformedAction("malformed-action"),
/**
* The responding JID cannot accept the specified language/locale.
*/
badLocale("bad-locale"),
/**
* The responding JID cannot accept the specified payload (e.g. the data
* form did not provide one or more required fields).
*/
badPayload("bad-payload"),
/**
* The responding JID cannot accept the specified sessionid.
*/
badSessionid("bad-sessionid"),
/**
* The requesting JID specified a sessionid that is no longer active
* (either because it was completed, canceled, or timed out).
*/
sessionExpired("session-expired");
private String value;
SpecificErrorCondition(String value) {
this.value = value;
}
public String toString() {
return value;
}
}
}

View file

@ -0,0 +1,707 @@
/**
*
* Copyright 2005-2008 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.commands;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.commands.AdHocCommand.Action;
import org.jivesoftware.smackx.commands.AdHocCommand.Status;
import org.jivesoftware.smackx.commands.packet.AdHocCommandData;
import org.jivesoftware.smackx.disco.NodeInformationProvider;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
import org.jivesoftware.smackx.disco.packet.DiscoverItems;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity;
import org.jivesoftware.smackx.xdata.Form;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
/**
* An AdHocCommandManager is responsible for keeping the list of available
* commands offered by a service and for processing commands requests.
*
* Pass in a XMPPConnection instance to
* {@link #getAddHocCommandsManager(org.jivesoftware.smack.XMPPConnection)} in order to
* get an instance of this class.
*
* @author Gabriel Guardincerri
*/
public class AdHocCommandManager extends Manager {
public static final String NAMESPACE = "http://jabber.org/protocol/commands";
/**
* The session time out in seconds.
*/
private static final int SESSION_TIMEOUT = 2 * 60;
/**
* Map a XMPPConnection with it AdHocCommandManager. This map have a key-value
* pair for every active connection.
*/
private static Map<XMPPConnection, AdHocCommandManager> instances =
Collections.synchronizedMap(new WeakHashMap<XMPPConnection, AdHocCommandManager>());
/**
* Register the listener for all the connection creations. When a new
* connection is created a new AdHocCommandManager is also created and
* related to that connection.
*/
static {
XMPPConnection.addConnectionCreationListener(new ConnectionCreationListener() {
public void connectionCreated(XMPPConnection connection) {
getAddHocCommandsManager(connection);
}
});
}
/**
* Returns the <code>AdHocCommandManager</code> related to the
* <code>connection</code>.
*
* @param connection the XMPP connection.
* @return the AdHocCommandManager associated with the connection.
*/
public static synchronized AdHocCommandManager getAddHocCommandsManager(XMPPConnection connection) {
AdHocCommandManager ahcm = instances.get(connection);
if (ahcm == null) ahcm = new AdHocCommandManager(connection);
return ahcm;
}
/**
* Map a command node with its AdHocCommandInfo. Note: Key=command node,
* Value=command. Command node matches the node attribute sent by command
* requesters.
*/
private final Map<String, AdHocCommandInfo> commands = new ConcurrentHashMap<String, AdHocCommandInfo>();
/**
* Map a command session ID with the instance LocalCommand. The LocalCommand
* is the an objects that has all the information of the current state of
* the command execution. Note: Key=session ID, Value=LocalCommand. Session
* ID matches the sessionid attribute sent by command responders.
*/
private final Map<String, LocalCommand> executingCommands = new ConcurrentHashMap<String, LocalCommand>();
private final ServiceDiscoveryManager serviceDiscoveryManager;
/**
* Thread that reaps stale sessions.
*/
private Thread sessionsSweeper;
private AdHocCommandManager(XMPPConnection connection) {
super(connection);
this.serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
// Register the new instance and associate it with the connection
instances.put(connection, this);
// Add the feature to the service discovery manage to show that this
// connection supports the AdHoc-Commands protocol.
// This information will be used when another client tries to
// discover whether this client supports AdHoc-Commands or not.
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(
NAMESPACE);
// Set the NodeInformationProvider that will provide information about
// which AdHoc-Commands are registered, whenever a disco request is
// received
ServiceDiscoveryManager.getInstanceFor(connection)
.setNodeInformationProvider(NAMESPACE,
new NodeInformationProvider() {
public List<DiscoverItems.Item> getNodeItems() {
List<DiscoverItems.Item> answer = new ArrayList<DiscoverItems.Item>();
Collection<AdHocCommandInfo> commandsList = getRegisteredCommands();
for (AdHocCommandInfo info : commandsList) {
DiscoverItems.Item item = new DiscoverItems.Item(
info.getOwnerJID());
item.setName(info.getName());
item.setNode(info.getNode());
answer.add(item);
}
return answer;
}
public List<String> getNodeFeatures() {
return null;
}
public List<Identity> getNodeIdentities() {
return null;
}
@Override
public List<PacketExtension> getNodePacketExtensions() {
return null;
}
});
// The packet listener and the filter for processing some AdHoc Commands
// Packets
PacketListener listener = new PacketListener() {
public void processPacket(Packet packet) {
AdHocCommandData requestData = (AdHocCommandData) packet;
try {
processAdHocCommand(requestData);
}
catch (SmackException e) {
return;
}
}
};
PacketFilter filter = new PacketTypeFilter(AdHocCommandData.class);
connection.addPacketListener(listener, filter);
sessionsSweeper = null;
}
/**
* Registers a new command with this command manager, which is related to a
* connection. The <tt>node</tt> is an unique identifier of that command for
* the connection related to this command manager. The <tt>name</tt> is the
* human readable name of the command. The <tt>class</tt> is the class of
* the command, which must extend {@link LocalCommand} and have a default
* constructor.
*
* @param node the unique identifier of the command.
* @param name the human readable name of the command.
* @param clazz the class of the command, which must extend {@link LocalCommand}.
*/
public void registerCommand(String node, String name, final Class<? extends LocalCommand> clazz) {
registerCommand(node, name, new LocalCommandFactory() {
public LocalCommand getInstance() throws InstantiationException, IllegalAccessException {
return clazz.newInstance();
}
});
}
/**
* Registers a new command with this command manager, which is related to a
* connection. The <tt>node</tt> is an unique identifier of that
* command for the connection related to this command manager. The <tt>name</tt>
* is the human readeale name of the command. The <tt>factory</tt> generates
* new instances of the command.
*
* @param node the unique identifier of the command.
* @param name the human readable name of the command.
* @param factory a factory to create new instances of the command.
*/
public void registerCommand(String node, final String name, LocalCommandFactory factory) {
AdHocCommandInfo commandInfo = new AdHocCommandInfo(node, name, connection().getUser(), factory);
commands.put(node, commandInfo);
// Set the NodeInformationProvider that will provide information about
// the added command
serviceDiscoveryManager.setNodeInformationProvider(node,
new NodeInformationProvider() {
public List<DiscoverItems.Item> getNodeItems() {
return null;
}
public List<String> getNodeFeatures() {
List<String> answer = new ArrayList<String>();
answer.add(NAMESPACE);
// TODO: check if this service is provided by the
// TODO: current connection.
answer.add("jabber:x:data");
return answer;
}
public List<DiscoverInfo.Identity> getNodeIdentities() {
List<DiscoverInfo.Identity> answer = new ArrayList<DiscoverInfo.Identity>();
DiscoverInfo.Identity identity = new DiscoverInfo.Identity(
"automation", name, "command-node");
answer.add(identity);
return answer;
}
@Override
public List<PacketExtension> getNodePacketExtensions() {
return null;
}
});
}
/**
* Discover the commands of an specific JID. The <code>jid</code> is a
* full JID.
*
* @param jid the full JID to retrieve the commands for.
* @return the discovered items.
* @throws XMPPException if the operation failed for some reason.
* @throws SmackException if there was no response from the server.
*/
public DiscoverItems discoverCommands(String jid) throws XMPPException, SmackException {
return serviceDiscoveryManager.discoverItems(jid, NAMESPACE);
}
/**
* Publish the commands to an specific JID.
*
* @param jid the full JID to publish the commands to.
* @throws XMPPException if the operation failed for some reason.
* @throws SmackException if there was no response from the server.
*/
public void publishCommands(String jid) throws XMPPException, SmackException {
// Collects the commands to publish as items
DiscoverItems discoverItems = new DiscoverItems();
Collection<AdHocCommandInfo> xCommandsList = getRegisteredCommands();
for (AdHocCommandInfo info : xCommandsList) {
DiscoverItems.Item item = new DiscoverItems.Item(info.getOwnerJID());
item.setName(info.getName());
item.setNode(info.getNode());
discoverItems.addItem(item);
}
serviceDiscoveryManager.publishItems(jid, NAMESPACE, discoverItems);
}
/**
* Returns a command that represents an instance of a command in a remote
* host. It is used to execute remote commands. The concept is similar to
* RMI. Every invocation on this command is equivalent to an invocation in
* the remote command.
*
* @param jid the full JID of the host of the remote command
* @param node the identifier of the command
* @return a local instance equivalent to the remote command.
*/
public RemoteCommand getRemoteCommand(String jid, String node) {
return new RemoteCommand(connection(), node, jid);
}
/**
* Process the AdHoc-Command packet that request the execution of some
* action of a command. If this is the first request, this method checks,
* before executing the command, if:
* <ul>
* <li>The requested command exists</li>
* <li>The requester has permissions to execute it</li>
* <li>The command has more than one stage, if so, it saves the command and
* session ID for further use</li>
* </ul>
*
* <br>
* <br>
* If this is not the first request, this method checks, before executing
* the command, if:
* <ul>
* <li>The session ID of the request was stored</li>
* <li>The session life do not exceed the time out</li>
* <li>The action to execute is one of the available actions</li>
* </ul>
*
* @param requestData
* the packet to process.
* @throws SmackException if there was no response from the server.
*/
private void processAdHocCommand(AdHocCommandData requestData) throws SmackException {
// Only process requests of type SET
if (requestData.getType() != IQ.Type.SET) {
return;
}
// Creates the response with the corresponding data
AdHocCommandData response = new AdHocCommandData();
response.setTo(requestData.getFrom());
response.setPacketID(requestData.getPacketID());
response.setNode(requestData.getNode());
response.setId(requestData.getTo());
String sessionId = requestData.getSessionID();
String commandNode = requestData.getNode();
if (sessionId == null) {
// A new execution request has been received. Check that the
// command exists
if (!commands.containsKey(commandNode)) {
// Requested command does not exist so return
// item_not_found error.
respondError(response, XMPPError.Condition.item_not_found);
return;
}
// Create new session ID
sessionId = StringUtils.randomString(15);
try {
// Create a new instance of the command with the
// corresponding sessioid
LocalCommand command = newInstanceOfCmd(commandNode, sessionId);
response.setType(IQ.Type.RESULT);
command.setData(response);
// Check that the requester has enough permission.
// Answer forbidden error if requester permissions are not
// enough to execute the requested command
if (!command.hasPermission(requestData.getFrom())) {
respondError(response, XMPPError.Condition.forbidden);
return;
}
Action action = requestData.getAction();
// If the action is unknown then respond an error.
if (action != null && action.equals(Action.unknown)) {
respondError(response, XMPPError.Condition.bad_request,
AdHocCommand.SpecificErrorCondition.malformedAction);
return;
}
// If the action is not execute, then it is an invalid action.
if (action != null && !action.equals(Action.execute)) {
respondError(response, XMPPError.Condition.bad_request,
AdHocCommand.SpecificErrorCondition.badAction);
return;
}
// Increase the state number, so the command knows in witch
// stage it is
command.incrementStage();
// Executes the command
command.execute();
if (command.isLastStage()) {
// If there is only one stage then the command is completed
response.setStatus(Status.completed);
}
else {
// Else it is still executing, and is registered to be
// available for the next call
response.setStatus(Status.executing);
executingCommands.put(sessionId, command);
// See if the session reaping thread is started. If not, start it.
if (sessionsSweeper == null) {
sessionsSweeper = new Thread(new Runnable() {
public void run() {
while (true) {
for (String sessionId : executingCommands.keySet()) {
LocalCommand command = executingCommands.get(sessionId);
// Since the command could be removed in the meanwhile
// of getting the key and getting the value - by a
// processed packet. We must check if it still in the
// map.
if (command != null) {
long creationStamp = command.getCreationDate();
// Check if the Session data has expired (default is
// 10 minutes)
// To remove it from the session list it waits for
// the double of the of time out time. This is to
// let
// the requester know why his execution request is
// not accepted. If the session is removed just
// after the time out, then whe the user request to
// continue the execution he will recieved an
// invalid session error and not a time out error.
if (System.currentTimeMillis() - creationStamp > SESSION_TIMEOUT * 1000 * 2) {
// Remove the expired session
executingCommands.remove(sessionId);
}
}
}
try {
Thread.sleep(1000);
}
catch (InterruptedException ie) {
// Ignore.
}
}
}
});
sessionsSweeper.setDaemon(true);
sessionsSweeper.start();
}
}
// Sends the response packet
connection().sendPacket(response);
}
catch (XMPPErrorException e) {
// If there is an exception caused by the next, complete,
// prev or cancel method, then that error is returned to the
// requester.
XMPPError error = e.getXMPPError();
// If the error type is cancel, then the execution is
// canceled therefore the status must show that, and the
// command be removed from the executing list.
if (XMPPError.Type.CANCEL.equals(error.getType())) {
response.setStatus(Status.canceled);
executingCommands.remove(sessionId);
}
respondError(response, error);
}
}
else {
LocalCommand command = executingCommands.get(sessionId);
// Check that a command exists for the specified sessionID
// This also handles if the command was removed in the meanwhile
// of getting the key and the value of the map.
if (command == null) {
respondError(response, XMPPError.Condition.bad_request,
AdHocCommand.SpecificErrorCondition.badSessionid);
return;
}
// Check if the Session data has expired (default is 10 minutes)
long creationStamp = command.getCreationDate();
if (System.currentTimeMillis() - creationStamp > SESSION_TIMEOUT * 1000) {
// Remove the expired session
executingCommands.remove(sessionId);
// Answer a not_allowed error (session-expired)
respondError(response, XMPPError.Condition.not_allowed,
AdHocCommand.SpecificErrorCondition.sessionExpired);
return;
}
/*
* Since the requester could send two requests for the same
* executing command i.e. the same session id, all the execution of
* the action must be synchronized to avoid inconsistencies.
*/
synchronized (command) {
Action action = requestData.getAction();
// If the action is unknown the respond an error
if (action != null && action.equals(Action.unknown)) {
respondError(response, XMPPError.Condition.bad_request,
AdHocCommand.SpecificErrorCondition.malformedAction);
return;
}
// If the user didn't specify an action or specify the execute
// action then follow the actual default execute action
if (action == null || Action.execute.equals(action)) {
action = command.getExecuteAction();
}
// Check that the specified action was previously
// offered
if (!command.isValidAction(action)) {
respondError(response, XMPPError.Condition.bad_request,
AdHocCommand.SpecificErrorCondition.badAction);
return;
}
try {
// TODO: Check that all the required fields of the form are
// TODO: filled, if not throw an exception. This will simplify the
// TODO: construction of new commands
// Since all errors were passed, the response is now a
// result
response.setType(IQ.Type.RESULT);
// Set the new data to the command.
command.setData(response);
if (Action.next.equals(action)) {
command.incrementStage();
command.next(new Form(requestData.getForm()));
if (command.isLastStage()) {
// If it is the last stage then the command is
// completed
response.setStatus(Status.completed);
}
else {
// Otherwise it is still executing
response.setStatus(Status.executing);
}
}
else if (Action.complete.equals(action)) {
command.incrementStage();
command.complete(new Form(requestData.getForm()));
response.setStatus(Status.completed);
// Remove the completed session
executingCommands.remove(sessionId);
}
else if (Action.prev.equals(action)) {
command.decrementStage();
command.prev();
}
else if (Action.cancel.equals(action)) {
command.cancel();
response.setStatus(Status.canceled);
// Remove the canceled session
executingCommands.remove(sessionId);
}
connection().sendPacket(response);
}
catch (XMPPErrorException e) {
// If there is an exception caused by the next, complete,
// prev or cancel method, then that error is returned to the
// requester.
XMPPError error = e.getXMPPError();
// If the error type is cancel, then the execution is
// canceled therefore the status must show that, and the
// command be removed from the executing list.
if (XMPPError.Type.CANCEL.equals(error.getType())) {
response.setStatus(Status.canceled);
executingCommands.remove(sessionId);
}
respondError(response, error);
}
}
}
}
/**
* Responds an error with an specific condition.
*
* @param response the response to send.
* @param condition the condition of the error.
* @throws NotConnectedException
*/
private void respondError(AdHocCommandData response,
XMPPError.Condition condition) throws NotConnectedException {
respondError(response, new XMPPError(condition));
}
/**
* Responds an error with an specific condition.
*
* @param response the response to send.
* @param condition the condition of the error.
* @param specificCondition the adhoc command error condition.
* @throws NotConnectedException
*/
private void respondError(AdHocCommandData response, XMPPError.Condition condition,
AdHocCommand.SpecificErrorCondition specificCondition) throws NotConnectedException
{
XMPPError error = new XMPPError(condition);
error.addExtension(new AdHocCommandData.SpecificError(specificCondition));
respondError(response, error);
}
/**
* Responds an error with an specific error.
*
* @param response the response to send.
* @param error the error to send.
* @throws NotConnectedException
*/
private void respondError(AdHocCommandData response, XMPPError error) throws NotConnectedException {
response.setType(IQ.Type.ERROR);
response.setError(error);
connection().sendPacket(response);
}
/**
* Creates a new instance of a command to be used by a new execution request
*
* @param commandNode the command node that identifies it.
* @param sessionID the session id of this execution.
* @return the command instance to execute.
* @throws XMPPErrorException if there is problem creating the new instance.
*/
private LocalCommand newInstanceOfCmd(String commandNode, String sessionID) throws XMPPErrorException
{
AdHocCommandInfo commandInfo = commands.get(commandNode);
LocalCommand command;
try {
command = (LocalCommand) commandInfo.getCommandInstance();
command.setSessionID(sessionID);
command.setName(commandInfo.getName());
command.setNode(commandInfo.getNode());
}
catch (InstantiationException e) {
throw new XMPPErrorException(new XMPPError(
XMPPError.Condition.internal_server_error));
}
catch (IllegalAccessException e) {
throw new XMPPErrorException(new XMPPError(
XMPPError.Condition.internal_server_error));
}
return command;
}
/**
* Returns the registered commands of this command manager, which is related
* to a connection.
*
* @return the registered commands.
*/
private Collection<AdHocCommandInfo> getRegisteredCommands() {
return commands.values();
}
/**
* Stores ad-hoc command information.
*/
private static class AdHocCommandInfo {
private String node;
private String name;
private String ownerJID;
private LocalCommandFactory factory;
public AdHocCommandInfo(String node, String name, String ownerJID,
LocalCommandFactory factory)
{
this.node = node;
this.name = name;
this.ownerJID = ownerJID;
this.factory = factory;
}
public LocalCommand getCommandInstance() throws InstantiationException,
IllegalAccessException
{
return factory.getInstance();
}
public String getName() {
return name;
}
public String getNode() {
return node;
}
public String getOwnerJID() {
return ownerJID;
}
}
}

View file

@ -0,0 +1,83 @@
/**
*
* Copyright 2005-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.commands;
/**
* Notes can be added to a command execution response. A note has a type and value.
*
* @author Gabriel Guardincerri
*/
public class AdHocCommandNote {
private Type type;
private String value;
/**
* Creates a new adhoc command note with the specified type and value.
*
* @param type the type of the note.
* @param value the value of the note.
*/
public AdHocCommandNote(Type type, String value) {
this.type = type;
this.value = value;
}
/**
* Returns the value or message of the note.
*
* @return the value or message of the note.
*/
public String getValue() {
return value;
}
/**
* Return the type of the note.
*
* @return the type of the note.
*/
public Type getType() {
return type;
}
/**
* Represents a note type.
*/
public enum Type {
/**
* The note is informational only. This is not really an exceptional
* condition.
*/
info,
/**
* The note indicates a warning. Possibly due to illogical (yet valid)
* data.
*/
warn,
/**
* The note indicates an error. The text should indicate the reason for
* the error.
*/
error
}
}

View file

@ -0,0 +1,166 @@
/**
*
* Copyright 2005-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.commands;
import org.jivesoftware.smackx.commands.packet.AdHocCommandData;
/**
* Represents a command that can be executed locally from a remote location. This
* class must be extended to implement an specific ad-hoc command. This class
* provides some useful tools:<ul>
* <li>Node</li>
* <li>Name</li>
* <li>Session ID</li>
* <li>Current Stage</li>
* <li>Available actions</li>
* <li>Default action</li>
* </ul><p/>
* To implement a new command extend this class and implement all the abstract
* methods. When implementing the actions remember that they could be invoked
* several times, and that you must use the current stage number to know what to
* do.
*
* @author Gabriel Guardincerri
*/
public abstract class LocalCommand extends AdHocCommand {
/**
* The time stamp of first invokation of the command. Used to implement the session timeout.
*/
private long creationDate;
/**
* The unique ID of the execution of the command.
*/
private String sessionID;
/**
* The full JID of the host of the command.
*/
private String ownerJID;
/**
* The number of the current stage.
*/
private int currenStage;
public LocalCommand() {
super();
this.creationDate = System.currentTimeMillis();
currenStage = -1;
}
/**
* The sessionID is an unique identifier of an execution request. This is
* automatically handled and should not be called.
*
* @param sessionID the unique session id of this execution
*/
public void setSessionID(String sessionID) {
this.sessionID = sessionID;
getData().setSessionID(sessionID);
}
/**
* Returns the session ID of this execution.
*
* @return the unique session id of this execution
*/
public String getSessionID() {
return sessionID;
}
/**
* Sets the JID of the command host. This is automatically handled and should
* not be called.
*
* @param ownerJID the JID of the owner.
*/
public void setOwnerJID(String ownerJID) {
this.ownerJID = ownerJID;
}
@Override
public String getOwnerJID() {
return ownerJID;
}
/**
* Returns the date the command was created.
*
* @return the date the command was created.
*/
public long getCreationDate() {
return creationDate;
}
/**
* Returns true if the current stage is the last one. If it is then the
* execution of some action will complete the execution of the command.
* Commands that don't have multiple stages can always return <tt>true</tt>.
*
* @return true if the command is in the last stage.
*/
public abstract boolean isLastStage();
/**
* Returns true if the specified requester has permission to execute all the
* stages of this action. This is checked when the first request is received,
* if the permission is grant then the requester will be able to execute
* all the stages of the command. It is not checked again during the
* execution.
*
* @param jid the JID to check permissions on.
* @return true if the user has permission to execute this action.
*/
public abstract boolean hasPermission(String jid);
/**
* Returns the currently executing stage number. The first stage number is
* 0. During the execution of the first action this method will answer 0.
*
* @return the current stage number.
*/
public int getCurrentStage() {
return currenStage;
}
@Override
void setData(AdHocCommandData data) {
data.setSessionID(sessionID);
super.setData(data);
}
/**
* Increase the current stage number. This is automatically handled and should
* not be called.
*
*/
void incrementStage() {
currenStage++;
}
/**
* Decrease the current stage number. This is automatically handled and should
* not be called.
*
*/
void decrementStage() {
currenStage--;
}
}

View file

@ -0,0 +1,40 @@
/**
*
* Copyright 2008 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.commands;
/**
* A factory for creating local commands. It's useful in cases where instantiation
* of a command is more complicated than just using the default constructor. For example,
* when arguments must be passed into the constructor or when using a dependency injection
* framework. When a LocalCommandFactory isn't used, you can provide the AdHocCommandManager
* a Class object instead. For more details, see
* {@link AdHocCommandManager#registerCommand(String, String, LocalCommandFactory)}.
*
* @author Matt Tucker
*/
public interface LocalCommandFactory {
/**
* Returns an instance of a LocalCommand.
*
* @return a LocalCommand instance.
* @throws InstantiationException if creating an instance failed.
* @throws IllegalAccessException if creating an instance is not allowed.
*/
public LocalCommand getInstance() throws InstantiationException, IllegalAccessException;
}

View file

@ -0,0 +1,154 @@
/**
*
* Copyright 2005-2007 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.commands;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smackx.commands.packet.AdHocCommandData;
import org.jivesoftware.smackx.xdata.Form;
/**
* Represents a command that is in a remote location. Invoking one of the
* {@link AdHocCommand.Action#execute execute}, {@link AdHocCommand.Action#next next},
* {@link AdHocCommand.Action#prev prev}, {@link AdHocCommand.Action#cancel cancel} or
* {@link AdHocCommand.Action#complete complete} actions results in executing that
* action in the remote location. In response to that action the internal state
* of the this command instance will change. For example, if the command is a
* single stage command, then invoking the execute action will execute this
* action in the remote location. After that the local instance will have a
* state of "completed" and a form or notes that applies.
*
* @author Gabriel Guardincerri
*
*/
public class RemoteCommand extends AdHocCommand {
/**
* The connection that is used to execute this command
*/
private XMPPConnection connection;
/**
* The full JID of the command host
*/
private String jid;
/**
* The session ID of this execution.
*/
private String sessionID;
/**
* Creates a new RemoteCommand that uses an specific connection to execute a
* command identified by <code>node</code> in the host identified by
* <code>jid</code>
*
* @param connection the connection to use for the execution.
* @param node the identifier of the command.
* @param jid the JID of the host.
*/
protected RemoteCommand(XMPPConnection connection, String node, String jid) {
super();
this.connection = connection;
this.jid = jid;
this.setNode(node);
}
@Override
public void cancel() throws NoResponseException, XMPPErrorException, NotConnectedException {
executeAction(Action.cancel);
}
@Override
public void complete(Form form) throws NoResponseException, XMPPErrorException, NotConnectedException {
executeAction(Action.complete, form);
}
@Override
public void execute() throws NoResponseException, XMPPErrorException, NotConnectedException {
executeAction(Action.execute);
}
/**
* Executes the default action of the command with the information provided
* in the Form. This form must be the anwser form of the previous stage. If
* there is a problem executing the command it throws an XMPPException.
*
* @param form the form anwser of the previous stage.
* @throws XMPPErrorException if an error occurs.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
*/
public void execute(Form form) throws NoResponseException, XMPPErrorException, NotConnectedException {
executeAction(Action.execute, form);
}
@Override
public void next(Form form) throws NoResponseException, XMPPErrorException, NotConnectedException {
executeAction(Action.next, form);
}
@Override
public void prev() throws NoResponseException, XMPPErrorException, NotConnectedException {
executeAction(Action.prev);
}
private void executeAction(Action action) throws NoResponseException, XMPPErrorException, NotConnectedException {
executeAction(action, null);
}
/**
* Executes the <code>action</codo> with the <code>form</code>.
* The action could be any of the available actions. The form must
* be the anwser of the previous stage. It can be <tt>null</tt> if it is the first stage.
*
* @param action the action to execute.
* @param form the form with the information.
* @throws XMPPErrorException if there is a problem executing the command.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
*/
private void executeAction(Action action, Form form) throws NoResponseException, XMPPErrorException, NotConnectedException {
// TODO: Check that all the required fields of the form were filled, if
// TODO: not throw the corresponding exeption. This will make a faster response,
// TODO: since the request is stoped before it's sent.
AdHocCommandData data = new AdHocCommandData();
data.setType(IQ.Type.SET);
data.setTo(getOwnerJID());
data.setNode(getNode());
data.setSessionID(sessionID);
data.setAction(action);
if (form != null) {
data.setForm(form.getDataFormToSend());
}
AdHocCommandData responseData = (AdHocCommandData) connection.createPacketCollectorAndSend(
data).nextResultOrThrow();
this.sessionID = responseData.getSessionID();
super.setData(responseData);
}
@Override
public String getOwnerJID() {
return jid;
}
}

Some files were not shown because too many files have changed in this diff Show more