use of org.jivesoftware.smack.packet.Packet in project camel by apache.
the class XmppEndpoint method createConnection.
public synchronized XMPPConnection createConnection() throws XMPPException, SmackException, IOException {
if (connection != null && connection.isConnected()) {
// use existing working connection
return connection;
}
// prepare for creating new connection
connection = null;
LOG.trace("Creating new connection ...");
XMPPConnection newConnection = createConnectionInternal();
newConnection.connect();
newConnection.addPacketListener(new XmppLogger("INBOUND"), new PacketFilter() {
public boolean accept(Packet packet) {
return true;
}
});
newConnection.addPacketSendingListener(new XmppLogger("OUTBOUND"), new PacketFilter() {
public boolean accept(Packet packet) {
return true;
}
});
if (!newConnection.isAuthenticated()) {
if (user != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Logging in to XMPP as user: {} on connection: {}", user, getConnectionMessage(newConnection));
}
if (password == null) {
LOG.warn("No password configured for user: {} on connection: {}", user, getConnectionMessage(newConnection));
}
if (createAccount) {
AccountManager accountManager = AccountManager.getInstance(newConnection);
accountManager.createAccount(user, password);
}
if (login) {
if (resource != null) {
newConnection.login(user, password, resource);
} else {
newConnection.login(user, password);
}
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Logging in anonymously to XMPP on connection: {}", getConnectionMessage(newConnection));
}
newConnection.loginAnonymously();
}
// presence is not needed to be sent after login
}
// okay new connection was created successfully so assign it as the connection
LOG.debug("Created new connection successfully: {}", newConnection);
connection = newConnection;
return connection;
}
use of org.jivesoftware.smack.packet.Packet in project Smack by igniterealtime.
the class JingleManagerTest method testInitJingleSessionRequestListeners.
/**
* Test for the session request detection. Here, we use the same filter we
* use in the JingleManager...
*/
public void testInitJingleSessionRequestListeners() {
resetCounter();
ProviderManager.getInstance().addIQProvider("jingle", "http://jabber.org/protocol/jingle", new JingleProvider());
PacketFilter initRequestFilter = new PacketFilter() {
// Return true if we accept this packet
public boolean accept(Packet pin) {
if (pin instanceof IQ) {
System.out.println("packet: " + pin.toXML());
IQ iq = (IQ) pin;
if (iq.getType().equals(IQ.Type.set)) {
System.out.println("packet");
if (iq instanceof Jingle) {
Jingle jin = (Jingle) pin;
if (jin.getAction().equals(JingleActionEnum.SESSION_INITIATE)) {
System.out.println("Session initiation packet accepted... ");
return true;
}
}
}
}
return false;
}
};
// Start a packet listener for session initiation requests
getConnection(0).addAsyncPacketListener(new PacketListener() {
public void processStanza(final Packet packet) {
System.out.println("Packet detected... ");
incCounter();
}
}, initRequestFilter);
// Create a dummy packet for testing...
IQfake iqSent = new IQfake(" <jingle xmlns='http://jabber.org/protocol/jingle'" + " initiator=\"user1@thiago\"" + " responder=\"user0@thiago\"" + " action=\"session-initiate\" sid=\"08666555\">" + "</jingle>");
iqSent.setTo(getFullJID(0));
iqSent.setFrom(getFullJID(0));
iqSent.setType(IQ.Type.set);
System.out.println("Sending packet and waiting... ");
getConnection(1).sendStanza(iqSent);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
System.out.println("Awake... " + valCounter());
assertTrue(valCounter() > 0);
}
use of org.jivesoftware.smack.packet.Packet in project Smack by igniterealtime.
the class InBandBytestreamTest method testRespondWithErrorOnInBandBytestreamRequest.
/**
* 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());
StanzaCollector collector = initiatorConnection.createStanzaCollector(new PacketIDFilter(open.getStanzaId()));
initiatorConnection.sendStanza(open);
Packet result = collector.nextResult();
assertNotNull(result.getError());
assertEquals(XMPPError.Condition.no_acceptable.toString(), result.getError().getCondition());
}
use of org.jivesoftware.smack.packet.Packet in project Smack by igniterealtime.
the class Socks5ByteStreamTest method testRespondWithErrorOnSocks5BytestreamRequest.
/**
* 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);
StanzaCollector collector = initiatorConnection.createStanzaCollector(new PacketIDFilter(bytestreamInitiation.getStanzaId()));
initiatorConnection.sendStanza(bytestreamInitiation);
Packet result = collector.nextResult();
assertNotNull(result.getError());
assertEquals(XMPPError.Condition.no_acceptable.toString(), result.getError().getCondition());
}
use of org.jivesoftware.smack.packet.Packet in project Smack by igniterealtime.
the class XHTMLExtensionTest method testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage.
/**
* 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 StanzaCollector chat2 = getConnection(1).createStanzaCollector(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 StanzaExtensionFilter("html", "http://jabber.org/protocol/xhtml-im");
PacketListener packetListener = new PacketListener() {
@Override
public void processStanza(Packet packet) {
}
};
getConnection(1).addAsyncPacketListener(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);
}
Aggregations