use of org.jivesoftware.openfire.PacketException in project Openfire by igniterealtime.
the class IQRosterHandler method handleIQ.
/**
* Handles all roster queries. There are two major types of queries:
*
* <ul>
* <li>Roster remove - A forced removal of items from a roster. Roster
* removals are the only roster queries allowed to
* directly affect the roster from another user.
* </li>
* <li>Roster management - A local user looking up or updating their
* roster.
* </li>
* </ul>
*
* @param packet The update packet
* @return The reply or null if no reply
*/
@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException, PacketException {
try {
IQ returnPacket;
org.xmpp.packet.Roster roster = (org.xmpp.packet.Roster) packet;
JID recipientJID = packet.getTo();
// The packet is bound for the server and must be roster management
if (recipientJID == null || recipientJID.equals(packet.getFrom().asBareJID())) {
returnPacket = manageRoster(roster);
} else {
returnPacket = IQ.createResultIQ(packet);
// The server MUST return a <forbidden/> stanza error to the client if the sender of the roster set is not authorized to update the roster
// (where typically only an authenticated resource of the account itself is authorized).
returnPacket.setError(PacketError.Condition.forbidden);
}
return returnPacket;
} catch (SharedGroupException e) {
IQ result = IQ.createResultIQ(packet);
result.setChildElement(packet.getChildElement().createCopy());
result.setError(PacketError.Condition.not_acceptable);
return result;
} catch (Exception e) {
if (e.getCause() instanceof IDNAException || e.getCause() instanceof IllegalArgumentException) {
Log.warn(LocaleUtils.getLocalizedString("admin.error") + e.getMessage());
IQ result = IQ.createResultIQ(packet);
result.setChildElement(packet.getChildElement().createCopy());
result.setError(PacketError.Condition.jid_malformed);
return result;
} else {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
IQ result = IQ.createResultIQ(packet);
result.setChildElement(packet.getChildElement().createCopy());
result.setError(PacketError.Condition.internal_server_error);
return result;
}
}
}
use of org.jivesoftware.openfire.PacketException in project Openfire by igniterealtime.
the class IQvCardHandler method handleIQ.
@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException, PacketException {
IQ result = IQ.createResultIQ(packet);
IQ.Type type = packet.getType();
if (type.equals(IQ.Type.set)) {
try {
User user = userManager.getUser(packet.getFrom().getNode());
Element vcard = packet.getChildElement();
if (vcard != null) {
VCardManager.getInstance().setVCard(user.getUsername(), vcard);
}
} catch (UserNotFoundException e) {
result = IQ.createResultIQ(packet);
result.setChildElement(packet.getChildElement().createCopy());
result.setError(PacketError.Condition.item_not_found);
} catch (Exception e) {
Log.error(e.getMessage(), e);
result.setError(PacketError.Condition.internal_server_error);
}
} else if (type.equals(IQ.Type.get)) {
JID recipient = packet.getTo();
// If no TO was specified then get the vCard of the sender of the packet
if (recipient == null) {
recipient = packet.getFrom();
}
// By default return an empty vCard
result.setChildElement("vCard", "vcard-temp");
// Only try to get the vCard values of non-anonymous users
if (recipient != null) {
if (recipient.getNode() != null && server.isLocal(recipient)) {
VCardManager vManager = VCardManager.getInstance();
Element userVCard = vManager.getVCard(recipient.getNode());
if (userVCard != null) {
// Check if the requester wants to ignore some vCard's fields
Element filter = packet.getChildElement().element(QName.get("filter", "vcard-temp-filter"));
if (filter != null) {
// Create a copy so we don't modify the original vCard
userVCard = userVCard.createCopy();
// Ignore fields requested by the user
for (Iterator toFilter = filter.elementIterator(); toFilter.hasNext(); ) {
Element field = (Element) toFilter.next();
Element fieldToRemove = userVCard.element(field.getName());
if (fieldToRemove != null) {
fieldToRemove.detach();
}
}
}
result.setChildElement(userVCard);
}
} else {
result = IQ.createResultIQ(packet);
result.setChildElement(packet.getChildElement().createCopy());
result.setError(PacketError.Condition.item_not_found);
}
} else {
result = IQ.createResultIQ(packet);
result.setChildElement(packet.getChildElement().createCopy());
result.setError(PacketError.Condition.item_not_found);
}
} else {
result.setChildElement(packet.getChildElement().createCopy());
result.setError(PacketError.Condition.not_acceptable);
}
return result;
}
use of org.jivesoftware.openfire.PacketException in project Openfire by igniterealtime.
the class PresenceSubscribeHandler method process.
@Override
public void process(Presence presence) throws PacketException {
if (presence == null) {
throw new IllegalArgumentException("Argument 'presence' cannot be null.");
}
final Presence.Type type = presence.getType();
if (type != Presence.Type.subscribe && type != Presence.Type.unsubscribe && type != Presence.Type.subscribed && type != Presence.Type.unsubscribed) {
throw new IllegalArgumentException("Packet processed by PresenceSubscribeHandler is " + "not of a subscription-related type, but: " + type);
}
// RFC-6121 paragraph 3: "When a server processes or generates an outbound presence stanza
// of type "subscribe", "subscribed", "unsubscribe", or "unsubscribed", the server MUST stamp
// the outgoing presence stanza with the bare JID <localpart@domainpart> of the sending entity,
// not the full JID <localpart@domainpart/resourcepart>."
presence.setFrom(presence.getFrom().toBareJID());
// JID and modify the 'to' address accordingly.
if (presence.getTo() != null) {
presence.setTo(presence.getTo().toBareJID());
}
final JID senderJID = presence.getFrom();
final JID recipientJID = presence.getTo();
try {
// Reject presence subscription requests sent to the local server itself.
if (recipientJID == null || recipientJID.toString().equals(serverName)) {
if (type == Presence.Type.subscribe) {
Presence reply = new Presence();
reply.setTo(senderJID);
reply.setFrom(recipientJID);
reply.setType(Presence.Type.unsubscribed);
deliverer.deliver(reply);
}
return;
}
try {
Roster senderRoster = getRoster(senderJID);
if (senderRoster != null) {
manageSub(recipientJID, true, type, senderRoster);
}
Roster recipientRoster = getRoster(recipientJID);
boolean recipientSubChanged = false;
if (recipientRoster != null) {
recipientSubChanged = manageSub(senderJID, false, type, recipientRoster);
}
// and the recipient user has not changed its subscription state.
if (!(type == Presence.Type.subscribed && recipientRoster != null && !recipientSubChanged)) {
// See http://tools.ietf.org/html/rfc3921#section-7 and/or OF-38
if (type == Presence.Type.subscribe && recipientRoster != null && !recipientSubChanged) {
try {
RosterItem.SubType subType = recipientRoster.getRosterItem(senderJID).getSubStatus();
if (subType == RosterItem.SUB_FROM || subType == RosterItem.SUB_BOTH) {
return;
}
} catch (UserNotFoundException e) {
// Weird case: Roster item does not exist. Should never happen
Log.error("User does not exist while trying to update roster item. " + "This should never happen (this indicates a programming " + "logic error). Processing stanza: " + presence.toString(), e);
}
}
// Try to obtain a handler for the packet based on the routes. If the handler is
// a module, the module will be able to handle the packet. If the handler is a
// Session the packet will be routed to the client. If a route cannot be found
// then the packet will be delivered based on its recipient and sender.
List<JID> jids = routingTable.getRoutes(recipientJID, null);
if (!jids.isEmpty()) {
for (JID jid : jids) {
Presence presenteToSend = presence.createCopy();
// Stamp the presence with the user's bare JID as the 'from' address,
// as required by section 8.2.5 of RFC 3921
presenteToSend.setFrom(senderJID.toBareJID());
routingTable.routePacket(jid, presenteToSend, false);
}
} else {
deliverer.deliver(presence.createCopy());
}
if (type == Presence.Type.subscribed) {
// Send the presence of the local user to the remote user. The remote user
// subscribed to the presence of the local user and the local user accepted
JID prober = localServer.isLocal(recipientJID) ? recipientJID.asBareJID() : recipientJID;
if (presenceManager.canProbePresence(prober, senderJID.getNode())) {
presenceManager.probePresence(prober, senderJID);
PresenceEventDispatcher.subscribedToPresence(recipientJID, senderJID);
} else {
Presence nonProbablePresence = new Presence();
nonProbablePresence.setStatus("unavailable");
nonProbablePresence.setFrom(senderJID);
nonProbablePresence.setTo(recipientJID);
presenceManager.handleProbe(nonProbablePresence);
}
}
}
if (type == Presence.Type.unsubscribed) {
// Send unavailable presence from all of the local user's available resources
// to the remote user
presenceManager.sendUnavailableFromSessions(recipientJID, senderJID);
PresenceEventDispatcher.unsubscribedToPresence(senderJID, recipientJID);
}
} catch (SharedGroupException e) {
Presence result = presence.createCopy();
JID sender = result.getFrom();
result.setFrom(presence.getTo());
result.setTo(sender);
result.setError(PacketError.Condition.not_acceptable);
deliverer.deliver(result);
}
} catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
}
use of org.jivesoftware.openfire.PacketException in project Openfire by igniterealtime.
the class TransportHandler method process.
@Override
public void process(Packet packet) throws UnauthorizedException, PacketException {
boolean handled = false;
String host = packet.getTo().getDomain();
for (Channel<Packet> channel : transports.values()) {
if (channel.getName().equalsIgnoreCase(host)) {
channel.add(packet);
handled = true;
}
}
if (!handled) {
JID recipient = packet.getTo();
JID sender = packet.getFrom();
packet.setError(PacketError.Condition.remote_server_timeout);
packet.setFrom(recipient);
packet.setTo(sender);
try {
deliverer.deliver(packet);
} catch (PacketException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
}
}
use of org.jivesoftware.openfire.PacketException in project Openfire by igniterealtime.
the class InternalComponentManager method process.
/**
* Processes packets that were sent to this service. Currently only packets that were sent from
* registered components are being processed. In the future, we may also process packet of
* trusted clients. Trusted clients may be able to execute ad-hoc commands such as adding or
* removing components.
*
* @param packet the packet to process.
*/
@Override
public void process(Packet packet) throws PacketException {
List<Component> components = getComponents(packet.getFrom());
// Only process packets that were sent by registered components
if (!components.isEmpty()) {
if (packet instanceof IQ && IQ.Type.result == ((IQ) packet).getType()) {
IQ iq = (IQ) packet;
Element childElement = iq.getChildElement();
if (childElement != null) {
String namespace = childElement.getNamespaceURI();
if ("http://jabber.org/protocol/disco#info".equals(namespace)) {
// Add a disco item to the server for the component that supports disco
Element identity = childElement.element("identity");
if (identity == null) {
// Do nothing since there are no identities in the disco#info packet
return;
}
try {
XMPPServer.getInstance().getIQDiscoItemsHandler().addComponentItem(packet.getFrom().toBareJID(), identity.attributeValue("name"));
for (Component component : components) {
if (component instanceof ComponentSession.ExternalComponent) {
ComponentSession.ExternalComponent externalComponent = (ComponentSession.ExternalComponent) component;
externalComponent.setName(identity.attributeValue("name"));
externalComponent.setType(identity.attributeValue("type"));
externalComponent.setCategory(identity.attributeValue("category"));
}
}
} catch (Exception e) {
Log.error("Error processing disco packet of components: " + components + " - " + packet.toXML(), e);
}
// Store the IQ disco#info returned by the component
addComponentInfo(iq);
// Notify listeners that a component answered the disco#info request
notifyComponentInfo(iq);
// Alert other cluster nodes
CacheFactory.doClusterTask(new NotifyComponentInfo(iq));
}
}
}
}
}
Aggregations