use of org.xmpp.packet.Packet in project Openfire by igniterealtime.
the class BaseMUCTransport method processPacket.
/**
* Handles all incoming presence stanzas.
*
* @param packet The presence packet to be processed.
* @return list of packets that will be sent back to the presence requester.
*/
private List<Packet> processPacket(Presence packet) {
Log.debug("Received presence packet: " + packet.toXML());
List<Packet> reply = new ArrayList<Packet>();
JID from = packet.getFrom();
JID to = packet.getTo();
if (packet.getType() == Presence.Type.error) {
// We don't want to do anything with this. Ignore it.
return reply;
}
try {
TransportSession<B> session = getTransport().getSessionManager().getSession(from);
if (!session.isLoggedIn()) {
Message m = new Message();
m.setError(Condition.service_unavailable);
m.setTo(from);
m.setFrom(getJID());
m.setBody(LocaleUtils.getLocalizedString("gateway.base.notloggedin", "kraken", Arrays.asList(getTransport().getType().toString().toUpperCase())));
reply.add(m);
} else if (to.getNode() == null) {
// Ignore undirected presence.
} else if (to.getResource() != null) {
// Presence to a specific resource.
if (packet.getType() == Presence.Type.unavailable) {
// Handle logout.
try {
MUCTransportSession mucSession = session.getMUCSessionManager().getSession(to.getNode());
mucSession.leaveRoom();
session.getMUCSessionManager().removeSession(to.getNode());
} catch (NotFoundException e) {
// Not found? Well then no problem.
}
} else {
// Handle login.
try {
MUCTransportSession mucSession = session.getMUCSessionManager().getSession(to.getNode());
// Active session.
mucSession.updateStatus(this.getTransport().getPresenceType(packet));
} catch (NotFoundException e) {
// No current session, lets create one.
MUCTransportSession mucSession = createRoom(session, to.getNode(), to.getResource());
session.getMUCSessionManager().storeSession(to.getNode(), mucSession);
mucSession.enterRoom();
}
}
} else {
// Presence to the room itself. Return error as per protocol.
Presence p = new Presence();
p.setError(Condition.jid_malformed);
p.setType(Presence.Type.error);
p.setTo(from);
p.setFrom(to);
reply.add(p);
}
} catch (NotFoundException e) {
Log.debug("Unable to find session.");
Message m = new Message();
m.setError(Condition.service_unavailable);
m.setTo(from);
m.setFrom(getJID());
m.setBody(LocaleUtils.getLocalizedString("gateway.base.notloggedin", "kraken", Arrays.asList(getTransport().getType().toString().toUpperCase())));
reply.add(m);
}
return reply;
}
use of org.xmpp.packet.Packet in project Openfire by igniterealtime.
the class BaseMUCTransport method handleDiscoInfo.
/**
* Handle service discovery info request.
*
* @param packet An IQ packet in the disco info namespace.
* @return A list of IQ packets to be returned to the user.
*/
private List<Packet> handleDiscoInfo(IQ packet) {
List<Packet> reply = new ArrayList<Packet>();
JID from = packet.getFrom();
JID to = packet.getTo();
if (packet.getTo().getNode() == null) {
// Requested info from transport itself.
IQ result = IQ.createResultIQ(packet);
if (from.getNode() == null || getTransport().permissionManager.hasAccess(from)) {
Element response = DocumentHelper.createElement(QName.get("query", NameSpace.DISCO_INFO));
response.addElement("identity").addAttribute("category", "conference").addAttribute("type", "text").addAttribute("name", this.getDescription());
response.addElement("feature").addAttribute("var", NameSpace.DISCO_INFO);
response.addElement("feature").addAttribute("var", NameSpace.DISCO_ITEMS);
response.addElement("feature").addAttribute("var", NameSpace.MUC);
result.setChildElement(response);
} else {
result.setError(PacketError.Condition.forbidden);
}
reply.add(result);
} else {
// Ah, a request for information about a room.
IQ result = IQ.createResultIQ(packet);
try {
TransportSession<B> session = getTransport().getSessionManager().getSession(from);
if (session.isLoggedIn()) {
storePendingRequest(packet);
session.getRoomInfo(getTransport().convertJIDToID(to));
} else {
// Not logged in? Not logged in then.
result.setError(PacketError.Condition.forbidden);
reply.add(result);
}
} catch (NotFoundException e) {
// Not found? No active session then.
result.setError(PacketError.Condition.forbidden);
reply.add(result);
}
}
return reply;
}
use of org.xmpp.packet.Packet in project Openfire by igniterealtime.
the class Reject method doAction.
@Override
public Packet doAction(Packet packet) throws PacketRejectedException {
SessionManager sessionManager = SessionManager.getInstance();
ClientSession clientSession = sessionManager.getSession(packet.getFrom());
Packet rejectPacket;
String pfFrom = JiveGlobals.getProperty("pf.From", "packetfilter");
if (packet instanceof Message) {
Message in = (Message) packet.createCopy();
if (clientSession != null && in.getBody() != null) {
in.setFrom(new JID(pfFrom));
String rejectMessage = JiveGlobals.getProperty("pf.rejectMessage", "Your message was rejected by the packet filter");
in.setBody(rejectMessage);
in.setType(Message.Type.error);
in.setTo(packet.getFrom());
String rejectSubject = JiveGlobals.getProperty("pf.rejectSubject", "Rejected");
in.setSubject(rejectSubject);
clientSession.process(in);
}
} else if (packet instanceof Presence) {
rejectPacket = new Presence();
rejectPacket.setTo(packet.getFrom());
rejectPacket.setError(PacketError.Condition.forbidden);
} else if (packet instanceof IQ) {
rejectPacket = new IQ();
rejectPacket.setTo(packet.getFrom());
rejectPacket.setError(PacketError.Condition.forbidden);
}
if (doLog()) {
Log.info("Rejecting packet from " + packet.getFrom() + " to " + packet.getTo());
}
throw new PacketRejectedException();
}
use of org.xmpp.packet.Packet in project Openfire by igniterealtime.
the class StanzaIDUtilTest method testUseOriginIdElement.
/**
* Test if {@link StanzaIDUtil#ensureUniqueAndStableStanzaID(Packet, JID)} uses a different value, if the provided
* data has an origin-id value.
*/
@Test
public void testUseOriginIdElement() throws Exception {
// Setup fixture.
final Packet input = new Message();
final JID self = new JID("foobar");
final String expected = "de305d54-75b4-431b-adb2-eb6b9e546013";
final Element toOverwrite = input.getElement().addElement("origin-id", "urn:xmpp:sid:0");
toOverwrite.addAttribute("id", expected);
// Execute system under test.
final Packet result = StanzaIDUtil.ensureUniqueAndStableStanzaID(input, self);
// Verify results.
Assert.assertNotNull(result);
final Element stanzaIDElement = result.getElement().element(QName.get("stanza-id", "urn:xmpp:sid:0"));
Assert.assertNotNull(stanzaIDElement);
try {
UUID.fromString(stanzaIDElement.attributeValue("id"));
} catch (IllegalArgumentException ex) {
Assert.fail();
}
Assert.assertNotEquals(expected, stanzaIDElement.attributeValue("id"));
assertEquals(self.toString(), stanzaIDElement.attributeValue("by"));
}
use of org.xmpp.packet.Packet in project Openfire by igniterealtime.
the class StanzaIDUtilTest method testOverwriteStanzaIDElement.
/**
* Test if {@link StanzaIDUtil#ensureUniqueAndStableStanzaID(Packet, JID)} overwrites a stanza-id
* element when another is present with the same 'by' value.
*/
@Test
public void testOverwriteStanzaIDElement() throws Exception {
// Setup fixture.
final Packet input = new Message();
final JID self = new JID("foobar");
final String notExpected = "de305d54-75b4-431b-adb2-eb6b9e546013";
final Element toOverwrite = input.getElement().addElement("stanza-id", "urn:xmpp:sid:0");
toOverwrite.addAttribute("by", self.toString());
toOverwrite.addAttribute("id", notExpected);
// Execute system under test.
final Packet result = StanzaIDUtil.ensureUniqueAndStableStanzaID(input, self);
// Verify results.
Assert.assertNotNull(result);
final Element stanzaIDElement = result.getElement().element(QName.get("stanza-id", "urn:xmpp:sid:0"));
Assert.assertNotNull(stanzaIDElement);
try {
UUID.fromString(stanzaIDElement.attributeValue("id"));
} catch (IllegalArgumentException ex) {
Assert.fail();
}
Assert.assertNotEquals(notExpected, stanzaIDElement.attributeValue("id"));
assertEquals(self.toString(), stanzaIDElement.attributeValue("by"));
}
Aggregations