use of org.jivesoftware.openfire.handler.IQHandler in project Openfire by igniterealtime.
the class IQRouter method getHandler.
private IQHandler getHandler(String namespace) {
IQHandler handler = namespace2Handlers.get(namespace);
if (handler == null) {
for (IQHandler handlerCandidate : iqHandlers) {
IQHandlerInfo handlerInfo = handlerCandidate.getInfo();
if (handlerInfo != null && namespace.equalsIgnoreCase(handlerInfo.getNamespace())) {
handler = handlerCandidate;
namespace2Handlers.put(namespace, handler);
break;
}
}
}
return handler;
}
use of org.jivesoftware.openfire.handler.IQHandler in project Openfire by igniterealtime.
the class MultiUserChatServiceImpl method process.
/**
* Returns true if the IQ packet was processed. This method should only process disco packets
* as well as jabber:iq:register packets sent to the MUC service.
*
* @param iq the IQ packet to process.
* @return true if the IQ packet was processed.
*/
private boolean process(final IQ iq) {
final Element childElement = iq.getChildElement();
String namespace = null;
// Ignore IQs of type ERROR
if (IQ.Type.error == iq.getType()) {
return false;
}
if (iq.getTo().getResource() != null) {
// Ignore IQ packets sent to room occupants
return false;
}
if (childElement != null) {
namespace = childElement.getNamespaceURI();
}
if ("jabber:iq:register".equals(namespace)) {
final IQ reply = registerHandler.handleIQ(iq);
XMPPServer.getInstance().getPacketRouter().route(reply);
} else if ("jabber:iq:search".equals(namespace)) {
final IQ reply = searchHandler.handleIQ(iq);
XMPPServer.getInstance().getPacketRouter().route(reply);
} else if (IQMuclumbusSearchHandler.NAMESPACE.equals(namespace)) {
final IQ reply = muclumbusSearchHandler.handleIQ(iq);
XMPPServer.getInstance().getPacketRouter().route(reply);
} else if (IQMUCvCardHandler.NAMESPACE.equals(namespace)) {
final IQ reply = mucVCardHandler.handleIQ(iq);
XMPPServer.getInstance().getPacketRouter().route(reply);
} else if ("http://jabber.org/protocol/disco#info".equals(namespace)) {
// TODO MUC should have an IQDiscoInfoHandler of its own when MUC becomes
// a component
final IQ reply = XMPPServer.getInstance().getIQDiscoInfoHandler().handleIQ(iq);
XMPPServer.getInstance().getPacketRouter().route(reply);
} else if ("http://jabber.org/protocol/disco#items".equals(namespace)) {
// TODO MUC should have an IQDiscoItemsHandler of its own when MUC becomes
// a component
final IQ reply = XMPPServer.getInstance().getIQDiscoItemsHandler().handleIQ(iq);
XMPPServer.getInstance().getPacketRouter().route(reply);
} else if ("urn:xmpp:ping".equals(namespace)) {
XMPPServer.getInstance().getPacketRouter().route(IQ.createResultIQ(iq));
} else if (this.iqHandlers != null) {
final IQHandler h = this.iqHandlers.get(namespace);
if (h != null) {
try {
final IQ reply = h.handleIQ(iq);
if (reply != null) {
XMPPServer.getInstance().getPacketRouter().route(reply);
}
} catch (final UnauthorizedException e) {
final IQ reply = IQ.createResultIQ(iq);
reply.setType(IQ.Type.error);
reply.setError(PacketError.Condition.service_unavailable);
XMPPServer.getInstance().getPacketRouter().route(reply);
}
return true;
}
return false;
} else {
return false;
}
return true;
}
use of org.jivesoftware.openfire.handler.IQHandler in project Openfire by igniterealtime.
the class AbstractXepSupport method start.
public void start() {
for (IQHandler iqHandler : iqHandlers) {
try {
iqHandler.initialize(server);
iqHandler.start();
} catch (Exception e) {
Log.error("Unable to initialize and start " + iqHandler.getClass());
continue;
}
element2Handlers.put(iqHandler.getInfo().getName(), iqHandler);
if (iqHandler instanceof ServerFeaturesProvider) {
for (Iterator<String> i = ((ServerFeaturesProvider) iqHandler).getFeatures(); i.hasNext(); ) {
server.getIQDiscoInfoHandler().addServerFeature(i.next());
}
}
if (muc) {
MultiUserChatManager manager = server.getMultiUserChatManager();
for (MultiUserChatService mucService : manager.getMultiUserChatServices()) {
mucService.addIQHandler(iqHandler);
mucService.addExtraFeature(namespace);
}
}
}
server.getIQDiscoInfoHandler().addServerFeature(namespace);
server.getIQRouter().addHandler(iqDispatcher);
}
use of org.jivesoftware.openfire.handler.IQHandler in project Openfire by igniterealtime.
the class AbstractXepSupport method stop.
public void stop() {
IQRouter iqRouter = server.getIQRouter();
IQDiscoInfoHandler iqDiscoInfoHandler = server.getIQDiscoInfoHandler();
for (IQHandler iqHandler : iqHandlers) {
element2Handlers.remove(iqHandler.getInfo().getName());
try {
iqHandler.stop();
iqHandler.destroy();
} catch (Exception e) {
Log.warn("Unable to stop and destroy " + iqHandler.getClass());
}
if (iqHandler instanceof ServerFeaturesProvider) {
for (Iterator<String> i = ((ServerFeaturesProvider) iqHandler).getFeatures(); i.hasNext(); ) {
if (iqDiscoInfoHandler != null) {
iqDiscoInfoHandler.removeServerFeature(i.next());
}
}
}
if (muc) {
MultiUserChatManager manager = server.getMultiUserChatManager();
for (MultiUserChatService mucService : manager.getMultiUserChatServices()) {
mucService.removeIQHandler(iqHandler);
mucService.removeExtraFeature(namespace);
}
}
}
if (iqRouter != null) {
iqRouter.removeHandler(iqDispatcher);
}
}
use of org.jivesoftware.openfire.handler.IQHandler in project Openfire by igniterealtime.
the class IQRouter method handle.
private void handle(IQ packet) {
JID recipientJID = packet.getTo();
// Check if the packet was sent to the server hostname
if (recipientJID != null && recipientJID.getNode() == null && recipientJID.getResource() == null && serverName.equals(recipientJID.getDomain())) {
Element childElement = packet.getChildElement();
if (childElement != null && childElement.element("addresses") != null) {
// Packet includes multicast processing instructions. Ask the multicastRouter
// to route this packet
multicastRouter.route(packet);
return;
}
}
if (packet.getID() != null && (IQ.Type.result == packet.getType() || IQ.Type.error == packet.getType())) {
// The server got an answer to an IQ packet that was sent from the server
// If there's a listener for this result at all, then it's likely that that listener had been registered
// on this cluster node. For efficiency, try the local cluster node before triggering tasks in the rest
// of the cluster.
IQResultListener iqResultListener = resultListeners.remove(packet.getID());
if (iqResultListener != null) {
resultTimeout.remove(packet.getID());
resultPending.remove(packet.getID());
try {
iqResultListener.receivedAnswer(packet);
} catch (Exception e) {
Log.error("Error processing answer of remote entity. Answer: " + packet.toXML(), e);
}
return;
} else if (ClusterManager.isClusteringStarted()) {
// Only do lookups in the cluster, after it's determined that the local node cannot process the result.
// remove it, to reduce the risk of this packet being sent back and forth.
final NodeID nodeID = resultPending.remove(packet.getID());
if (nodeID != null && !XMPPServer.getInstance().getNodeID().equals(nodeID)) {
CacheFactory.doClusterTask(new IQResultListenerTask(packet), nodeID.toByteArray());
return;
}
}
}
try {
// they are not allowed in s2s traffic.
if (packet.getFrom() == null && !XMPPServer.getInstance().isLocal(recipientJID)) {
// Stanzas that originate from clients _always_ have a 'from' attribute (as that attribute value is set/
// overwritten by Openfire upon receiving the stanza, to prevent abuse where a user tries to impersonate
// someone else). That means that, if we're processing a stanza without a 'from' attribute, that the
// stanza is very likely to originate from Openfire's code. If we have code that generates a stanza
// without a 'from' address but addressed to a remote domain, this simply is a bug that we should very
// verbosely warn about.
Log.error("Unable to process a stanza that has no 'from' attribute, addressed to a remote entity. Stanza is being dropped: {}", packet.toXML());
return;
}
if (recipientJID != null && (routingTable.hasComponentRoute(recipientJID) || (packet.getFrom() != null && routingTable.hasServerRoute(new DomainPair(packet.getFrom().getDomain(), recipientJID.getDomain()))))) {
// A component/service/remote server was found that can handle the Packet
routingTable.routePacket(recipientJID, packet, false);
return;
}
if (isLocalServer(recipientJID)) {
// Let the server handle the Packet
Element childElement = packet.getChildElement();
String namespace = null;
if (childElement != null) {
namespace = childElement.getNamespaceURI();
}
if (namespace == null) {
if (packet.getType() != IQ.Type.result && packet.getType() != IQ.Type.error) {
// Do nothing. We can't handle queries outside of a valid namespace
Log.warn("Unknown packet " + packet.toXML());
}
} else {
// Check if communication to local users is allowed
if (recipientJID != null && userManager.isRegisteredUser(recipientJID, false)) {
PrivacyList list = PrivacyListManager.getInstance().getDefaultPrivacyList(recipientJID.getNode());
if (list != null && list.shouldBlockPacket(packet)) {
// Communication is blocked
if (IQ.Type.set == packet.getType() || IQ.Type.get == packet.getType()) {
// Answer that the service is unavailable
sendErrorPacket(packet, PacketError.Condition.service_unavailable);
}
return;
}
}
IQHandler handler = getHandler(namespace);
if (handler == null) {
if (recipientJID == null) {
// Answer an error since the server can't handle the requested namespace
sendErrorPacket(packet, PacketError.Condition.service_unavailable);
} else if (recipientJID.getNode() == null || "".equals(recipientJID.getNode())) {
// Answer an error if JID is of the form <domain>
sendErrorPacket(packet, PacketError.Condition.feature_not_implemented);
} else {
// JID is of the form <node@domain>
// Answer an error since the server can't handle packets sent to a node
sendErrorPacket(packet, PacketError.Condition.service_unavailable);
}
} else {
handler.process(packet);
}
}
} else {
// If the user account identified by the 'to' attribute does not exist, how the stanza is processed depends on the stanza type.
if (recipientJID != null && recipientJID.getNode() != null && serverName.equals(recipientJID.getDomain()) && !userManager.isRegisteredUser(recipientJID, false) && sessionManager.getSession(recipientJID) == null && (IQ.Type.set == packet.getType() || IQ.Type.get == packet.getType())) {
// For an IQ stanza, the server MUST return a <service-unavailable/> stanza error to the sender.
sendErrorPacket(packet, PacketError.Condition.service_unavailable);
return;
}
ClientSession session = sessionManager.getSession(packet.getFrom());
boolean isAcceptable = true;
if (session instanceof LocalClientSession) {
// Check if we could process IQ stanzas from the recipient.
// If not, return a not-acceptable error as per XEP-0016:
// If the user attempts to send an outbound stanza to a contact and that stanza type is blocked, the user's server MUST NOT route the stanza to the contact but instead MUST return a <not-acceptable/> error
IQ dummyIQ = packet.createCopy();
dummyIQ.setFrom(packet.getTo());
dummyIQ.setTo(packet.getFrom());
if (!((LocalClientSession) session).canProcess(dummyIQ)) {
packet.setTo(session.getAddress());
packet.setFrom((JID) null);
packet.setError(PacketError.Condition.not_acceptable);
session.process(packet);
isAcceptable = false;
}
}
if (isAcceptable) {
// JID is of the form <node@domain/resource> or belongs to a remote server
// or to an uninstalled component
routingTable.routePacket(recipientJID, packet, false);
}
}
} catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error.routing"), e);
Session session = sessionManager.getSession(packet.getFrom());
if (session != null) {
IQ reply = IQ.createResultIQ(packet);
reply.setError(PacketError.Condition.internal_server_error);
session.process(reply);
}
}
}
Aggregations