Skip to content

Instantly share code, notes, and snippets.

@dirtyhenry
Created January 14, 2014 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dirtyhenry/8419599 to your computer and use it in GitHub Desktop.
Save dirtyhenry/8419599 to your computer and use it in GitHub Desktop.
Fooling around the Smack API (a unit test + a POM to get the dependencies)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SmackPOC</groupId>
<artifactId>SmackPOC</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Smack POC</name>
<description>XMPP App Using Smack</description>
<repositories>
<repository>
<id>opencast-public</id>
<url>http://repository.opencastproject.org/nexus/content/repositories/public/</url>
</repository>
</repositories>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smackx</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
</project>
package com.bootstragram.xmpp;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
import org.apache.log4j.Logger;
import org.jivesoftware.smack.AccountManager;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.ReportedData;
import org.jivesoftware.smackx.SharedGroupManager;
import org.jivesoftware.smackx.search.UserSearchManager;
/**
* @author mick
*/
public class SmackAPITest extends TestCase {
private final static String JABBER_SERVICE_URL = "chat.im";
private final static Logger LOG = Logger.getLogger(SmackAPITest.class);
private void createAccount(Connection connection) {
try {
// Create account
Map<String, String> accountCreationOptions = new HashMap<String, String>();
accountCreationOptions.put("name", "Marge Simpson");
accountCreationOptions.put("email", "marge@simpson.com");
AccountManager accountManager = connection.getAccountManager();
// If conflict, an exception is raised
accountManager.createAccount("marge", "marge", accountCreationOptions);
} catch (XMPPException e) {
LOG.error("Creation of the user failed");
}
}
private void listRoster(Connection connection) {
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
if (entries.isEmpty()) {
LOG.debug("Empty roster");
} else {
for (RosterEntry entry : entries) {
LOG.debug(entry);
}
}
}
private void listSharedGroups(Connection conn) {
try {
List<String> groups = SharedGroupManager.getSharedGroups(conn);
if (groups.isEmpty()) {
LOG.debug("Empty shared groups");
} else {
for (String group : groups) {
LOG.debug("Group " + group);
}
}
} catch (XMPPException e) {
LOG.error("Impossible to get shared groups", e);
}
}
private void addToRoster(Connection conn, String user) {
try {
if (conn.isAuthenticated()) {
Roster roster = conn.getRoster();
roster.createEntry(user + "@" + JABBER_SERVICE_URL, user, new String[] { user });
} else {
LOG.debug("Not authenticated");
}
} catch (XMPPException e) {
LOG.error("Couldn't add entry", e);
}
}
private void search(Connection conn) {
try {
final String searchService = "search." + JABBER_SERVICE_URL;
UserSearchManager search = new UserSearchManager(conn);
Form searchForm = search.getSearchForm(searchService);
LOG.info("Instructions: " + searchForm.getInstructions());
Form answerForm = searchForm.createAnswerForm();
answerForm.setAnswer("Email", true);
answerForm.setAnswer("search", "mickael@gmail.com");
ReportedData data = search.getSearchResults(answerForm, searchService);
Iterator<ReportedData.Row> iterator = data.getRows();
while (iterator.hasNext()) {
ReportedData.Row row = iterator.next();
LOG.debug("Row " + row);
Iterator<ReportedData.Column> columns = data.getColumns();
while (columns.hasNext()) {
ReportedData.Column column = columns.next();
LOG.debug("Column: " + column.getLabel() + "//" + column.getType() + "//" + column.getVariable());
Iterator<String> stringItr = row.getValues(column.getVariable());
while (stringItr.hasNext()) {
LOG.debug("Value: " + stringItr.next());
}
}
}
} catch (XMPPException e) {
LOG.error("Error during search", e);
}
}
public void testConnection() {
try {
LOG.debug("Start test");
Connection conn1 = new XMPPConnection(JABBER_SERVICE_URL);
conn1.connect();
createAccount(conn1);
listRoster(conn1);
listSharedGroups(conn1);
conn1.login("marge", "marge");
addToRoster(conn1, "mick");
addToRoster(conn1, "homer");
listRoster(conn1);
listSharedGroups(conn1);
search(conn1);
conn1.disconnect();
LOG.debug("End test");
} catch (XMPPException e) {
LOG.error("Error", e);
fail(e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment