use of org.jivesoftware.smack.StanzaListener in project Smack by igniterealtime.
the class InBandBytestreamSessionTest method shouldNotCloseBothStreamsIfInputStreamIsClosed.
/**
* If the output stream is closed the input stream should not be closed as well.
*
* @throws Exception should not happen
*/
@Test
public void shouldNotCloseBothStreamsIfInputStreamIsClosed() throws Exception {
// acknowledgment for data packet
IQ resultIQ = IBBPacketUtils.createResultIQ(initiatorJID, targetJID);
protocol.addResponse(resultIQ);
// get IBB sessions data packet listener
InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream, initiatorJID);
InputStream inputStream = session.getInputStream();
StanzaListener listener = Whitebox.getInternalState(inputStream, StanzaListener.class);
// build data packet
String base64Data = Base64.encode("Data");
DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
Data data = new Data(dpe);
// add data packets
listener.processStanza(data);
inputStream.close();
protocol.verifyAll();
try {
while (inputStream.read() != -1) {
}
inputStream.read();
fail("should throw an exception");
} catch (IOException e) {
assertTrue(e.getMessage().contains("closed"));
}
session.getOutputStream().flush();
}
use of org.jivesoftware.smack.StanzaListener in project Smack by igniterealtime.
the class InBandBytestreamSessionTest method shouldReplyWithErrorIfAlreadyUsedSequenceIsReceived.
/**
* If the data stanza(/packet) has a sequence that is already used an 'unexpected-request' error should
* be returned. See XEP-0047 Section 2.2.
*
* @throws Exception should not happen
*/
@Test
public void shouldReplyWithErrorIfAlreadyUsedSequenceIsReceived() throws Exception {
// verify reply to first valid data packet is of type RESULT
protocol.addResponse(null, Verification.requestTypeRESULT);
// verify reply to invalid data packet is an error
protocol.addResponse(null, Verification.requestTypeERROR, new Verification<IQ, IQ>() {
@Override
public void verify(IQ request, IQ response) {
assertEquals(XMPPError.Condition.unexpected_request, request.getError().getCondition());
}
});
// get IBB sessions data packet listener
InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream, initiatorJID);
InputStream inputStream = session.getInputStream();
StanzaListener listener = Whitebox.getInternalState(inputStream, StanzaListener.class);
// build data packets
String base64Data = Base64.encode("Data");
DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
Data data1 = new Data(dpe);
Data data2 = new Data(dpe);
// notify listener
listener.processStanza(data1);
listener.processStanza(data2);
protocol.verifyAll();
}
use of org.jivesoftware.smack.StanzaListener in project Smack by igniterealtime.
the class InBandBytestreamSessionTest method shouldConfirmReceivedDataPacket.
/**
* Valid data packets should be confirmed.
*
* @throws Exception should not happen
*/
@Test
public void shouldConfirmReceivedDataPacket() throws Exception {
// verify data packet confirmation is of type RESULT
protocol.addResponse(null, Verification.requestTypeRESULT);
InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream, initiatorJID);
InputStream inputStream = session.getInputStream();
StanzaListener listener = Whitebox.getInternalState(inputStream, StanzaListener.class);
String base64Data = Base64.encode("Data");
DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
Data data = new Data(dpe);
listener.processStanza(data);
protocol.verifyAll();
}
use of org.jivesoftware.smack.StanzaListener in project Smack by igniterealtime.
the class InBandBytestreamSessionTest method shouldNotCloseBothStreamsIfOutputStreamIsClosed.
/**
* If the input stream is closed the output stream should not be closed as well.
*
* @throws Exception should not happen
*/
@Test
public void shouldNotCloseBothStreamsIfOutputStreamIsClosed() throws Exception {
InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream, initiatorJID);
OutputStream outputStream = session.getOutputStream();
outputStream.close();
// verify data packet confirmation is of type RESULT
protocol.addResponse(null, Verification.requestTypeRESULT);
// insert data to read
InputStream inputStream = session.getInputStream();
StanzaListener listener = Whitebox.getInternalState(inputStream, StanzaListener.class);
String base64Data = Base64.encode("Data");
DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
Data data = new Data(dpe);
listener.processStanza(data);
// verify no packet send
protocol.verifyAll();
try {
outputStream.flush();
fail("should throw an exception");
} catch (IOException e) {
assertTrue(e.getMessage().contains("closed"));
}
assertTrue(inputStream.read() != 0);
}
use of org.jivesoftware.smack.StanzaListener in project Smack by igniterealtime.
the class JingleSession method updatePacketListener.
/**
* Install the stanza(/packet) listener. The listener is responsible for responding
* to any stanza(/packet) that we receive...
*/
protected void updatePacketListener() {
removeAsyncPacketListener();
LOGGER.fine("UpdatePacketListener");
packetListener = new StanzaListener() {
@Override
public void processStanza(Stanza packet) {
try {
receivePacketAndRespond((IQ) packet);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "exception", e);
}
}
};
packetFilter = new StanzaFilter() {
@Override
public boolean accept(Stanza packet) {
if (packet instanceof IQ) {
IQ iq = (IQ) packet;
Jid me = getConnection().getUser();
if (!iq.getTo().equals(me)) {
return false;
}
Jid other = getResponder().equals(me) ? getInitiator() : getResponder();
if (iq.getFrom() == null || !iq.getFrom().equals(other == null ? "" : other)) {
return false;
}
if (iq instanceof Jingle) {
Jingle jin = (Jingle) iq;
String sid = jin.getSid();
if (sid == null || !sid.equals(getSid())) {
LOGGER.fine("Ignored Jingle(SID) " + sid + "|" + getSid() + " :" + iq.toXML());
return false;
}
Jid ini = jin.getInitiator();
if (!ini.equals(getInitiator())) {
LOGGER.fine("Ignored Jingle(INI): " + iq.toXML());
return false;
}
} else {
// We accept some non-Jingle IQ packets: ERRORs and ACKs
if (iq.getType().equals(IQ.Type.set)) {
LOGGER.fine("Ignored Jingle(TYPE): " + iq.toXML());
return false;
} else if (iq.getType().equals(IQ.Type.get)) {
LOGGER.fine("Ignored Jingle(TYPE): " + iq.toXML());
return false;
}
}
return true;
}
return false;
}
};
getConnection().addAsyncStanzaListener(packetListener, packetFilter);
}
Aggregations