Search in sources :

Example 1 with Session

use of org.jivesoftware.openfire.session.Session in project Openfire by igniterealtime.

the class DiscoIQRegisteredProcessor method process.

@Override
public void process(Packet packet, final String subdomain, String to, String from) throws PacketRejectedException {
    Log.debug("Processing packet in DiscoIQRegisteredProcessor for " + subdomain);
    // Check if the jabber:iq:register is enabled in admin panel
    boolean isFeatureEnabled = JiveGlobals.getBooleanProperty("plugin.remoteroster.sparkDiscoInfo", false);
    if (!isFeatureEnabled) {
        Log.debug("Spark extension is deactivated. Won't change the disco#info");
        return;
    }
    final InterceptorManager interceptorManager = InterceptorManager.getInstance();
    final PacketInterceptor interceptor = new PacketInterceptor() {

        public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) throws PacketRejectedException {
            if (!processed && incoming) {
                if (packet instanceof IQ) {
                    IQ iqPacket = (IQ) packet;
                    Element packetElement = iqPacket.getChildElement();
                    if (packetElement == null)
                        return;
                    String ns = iqPacket.getChildElement().getNamespace().getURI();
                    if (iqPacket.getType().equals(IQ.Type.result) && ns.equals("jabber:iq:register") && iqPacket.getFrom().toString().equals(subdomain)) {
                        // Check if we are already registered
                        setRegistered(iqPacket.toString().contains("<registered/>"));
                        throw new PacketRejectedException();
                    } else if (iqPacket.getType().equals(IQ.Type.result) && ns.equals("http://jabber.org/protocol/disco#info") && iqPacket.getFrom().toString().equals(subdomain)) {
                        /*
							 * This is the answer of the disco#info from spark
							 * to our component. add the jabber:iq:register
							 * feature if we are registered
							 */
                        if (isRegistered()) {
                            Log.debug("Modifying disco#info packge to send registered iq feature to Spark user " + iqPacket.getTo().toString());
                            Attribute attribut = new DefaultAttribute("var", "jabber:iq:registered");
                            iqPacket.getChildElement().addElement("feature").add(attribut);
                        }
                    }
                }
            }
        }
    };
    Log.debug("Creating my own listener for jabber:iq:register result to external component " + subdomain);
    interceptorManager.addInterceptor(interceptor);
    IQ askComponent = new IQ();
    askComponent.setTo(to);
    askComponent.setFrom(from);
    askComponent.setType(IQ.Type.get);
    Element query = new DefaultElement(QName.get("query", "jabber:iq:register"));
    askComponent.setChildElement(query);
    // Remove the package intercepter in 1sec
    TimerTask removeInterceptorTask = new TimerTask() {

        @Override
        public void run() {
            Log.debug("Removing my created listener for jabber:iq:register. Component " + subdomain);
            interceptorManager.removeInterceptor(interceptor);
        }
    };
    Timer timer = new Timer();
    timer.schedule(removeInterceptorTask, 1000);
    // Send the register query to component
    dispatchPacket(askComponent);
}
Also used : Packet(org.xmpp.packet.Packet) DefaultAttribute(org.dom4j.tree.DefaultAttribute) Attribute(org.dom4j.Attribute) DefaultElement(org.dom4j.tree.DefaultElement) InterceptorManager(org.jivesoftware.openfire.interceptor.InterceptorManager) DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) PacketInterceptor(org.jivesoftware.openfire.interceptor.PacketInterceptor) TimerTask(java.util.TimerTask) Timer(java.util.Timer) PacketRejectedException(org.jivesoftware.openfire.interceptor.PacketRejectedException) DefaultAttribute(org.dom4j.tree.DefaultAttribute) Session(org.jivesoftware.openfire.session.Session)

Example 2 with Session

use of org.jivesoftware.openfire.session.Session in project Openfire by igniterealtime.

the class SessionManager method restoreCacheContent.

private void restoreCacheContent() {
    // Add external component sessions hosted locally to the cache (using new nodeID)
    for (Session session : localSessionManager.getComponentsSessions()) {
        componentSessionsCache.put(session.getAddress().toString(), server.getNodeID().toByteArray());
    }
    // Add connection multiplexer sessions hosted locally to the cache (using new nodeID)
    for (String address : localSessionManager.getConnnectionManagerSessions().keySet()) {
        multiplexerSessionsCache.put(address, server.getNodeID().toByteArray());
    }
    // Add incoming server sessions hosted locally to the cache (using new nodeID)
    for (LocalIncomingServerSession session : localSessionManager.getIncomingServerSessions()) {
        StreamID streamID = session.getStreamID();
        incomingServerSessionsCache.put(streamID, server.getNodeID().toByteArray());
        for (String hostname : session.getValidatedDomains()) {
            // Update list of sockets/sessions coming from the same remote hostname
            Lock lock = CacheFactory.getLock(hostname, hostnameSessionsCache);
            try {
                lock.lock();
                List<StreamID> streamIDs = hostnameSessionsCache.get(hostname);
                if (streamIDs == null) {
                    streamIDs = new ArrayList<>();
                }
                streamIDs.add(streamID);
                hostnameSessionsCache.put(hostname, streamIDs);
            } finally {
                lock.unlock();
            }
            // Add to clustered cache
            lock = CacheFactory.getLock(streamID, validatedDomainsCache);
            try {
                lock.lock();
                Set<String> validatedDomains = validatedDomainsCache.get(streamID);
                if (validatedDomains == null) {
                    validatedDomains = new HashSet<>();
                }
                boolean added = validatedDomains.add(hostname);
                if (added) {
                    validatedDomainsCache.put(streamID, validatedDomains);
                }
            } finally {
                lock.unlock();
            }
        }
    }
}
Also used : LocalIncomingServerSession(org.jivesoftware.openfire.session.LocalIncomingServerSession) LocalClientSession(org.jivesoftware.openfire.session.LocalClientSession) ClientSession(org.jivesoftware.openfire.session.ClientSession) LocalComponentSession(org.jivesoftware.openfire.session.LocalComponentSession) OutgoingServerSession(org.jivesoftware.openfire.session.OutgoingServerSession) ComponentSession(org.jivesoftware.openfire.session.ComponentSession) HttpSession(org.jivesoftware.openfire.http.HttpSession) LocalConnectionMultiplexerSession(org.jivesoftware.openfire.session.LocalConnectionMultiplexerSession) LocalOutgoingServerSession(org.jivesoftware.openfire.session.LocalOutgoingServerSession) Session(org.jivesoftware.openfire.session.Session) IncomingServerSession(org.jivesoftware.openfire.session.IncomingServerSession) LocalIncomingServerSession(org.jivesoftware.openfire.session.LocalIncomingServerSession) ConnectionMultiplexerSession(org.jivesoftware.openfire.session.ConnectionMultiplexerSession) Lock(java.util.concurrent.locks.Lock)

Example 3 with Session

use of org.jivesoftware.openfire.session.Session in project Openfire by igniterealtime.

the class PacketCounter method start.

/**
     * Resets all counters, and starts counting.
     */
public void start() {
    // Register a packet listener so that we can track packet traffic.
    interceptor = new PacketInterceptor() {

        public void interceptPacket(final Packet packet, final Session session, final boolean incoming, final boolean processed) {
            if (!processed) {
                // don't count packets twice!
                return;
            }
            stanza.incrementAndGet();
            if (packet instanceof Message) {
                message.incrementAndGet();
            }
            if (packet instanceof Presence) {
                presence.incrementAndGet();
            }
            if (packet instanceof IQ) {
                iq.incrementAndGet();
                switch(((IQ) packet).getType()) {
                    case get:
                        iqGet.incrementAndGet();
                        break;
                    case set:
                        iqSet.incrementAndGet();
                        break;
                    case result:
                        iqResult.incrementAndGet();
                        break;
                    case error:
                        iqError.incrementAndGet();
                        break;
                }
            }
        }
    };
    // reset counters
    stanza.set(0);
    message.set(0);
    presence.set(0);
    iq.set(0);
    iqGet.set(0);
    iqSet.set(0);
    iqResult.set(0);
    iqError.set(0);
    // register listener
    InterceptorManager.getInstance().addInterceptor(interceptor);
}
Also used : Packet(org.xmpp.packet.Packet) Message(org.xmpp.packet.Message) IQ(org.xmpp.packet.IQ) Presence(org.xmpp.packet.Presence) PacketInterceptor(org.jivesoftware.openfire.interceptor.PacketInterceptor) Session(org.jivesoftware.openfire.session.Session)

Example 4 with Session

use of org.jivesoftware.openfire.session.Session in project Openfire by igniterealtime.

the class RemoteSession method doSynchronousClusterTask.

/**
     * Invokes a task on the remote cluster member synchronously and returns the result of
     * the remote operation.
     *
     * @param task        the ClusterTask object to be invoked on a given cluster member.
     * @return result of remote operation.
     */
protected Object doSynchronousClusterTask(ClusterTask task) {
    ClusterNodeInfo info = CacheFactory.getClusterNodeInfo(nodeID);
    Object result = null;
    if (info == null && task instanceof RemoteSessionTask) {
        // clean up invalid session
        Session remoteSession = ((RemoteSessionTask) task).getSession();
        if (remoteSession instanceof ClientSession) {
            SessionManager.getInstance().removeSession(null, remoteSession.getAddress(), false, false);
        }
    } else {
        result = (info == null) ? null : CacheFactory.doSynchronousClusterTask(task, nodeID);
    }
    return result;
}
Also used : ClientSession(org.jivesoftware.openfire.session.ClientSession) ClusterNodeInfo(org.jivesoftware.openfire.cluster.ClusterNodeInfo) ClientSession(org.jivesoftware.openfire.session.ClientSession) Session(org.jivesoftware.openfire.session.Session)

Example 5 with Session

use of org.jivesoftware.openfire.session.Session 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) {
                try {
                    VCardManager.getInstance().setVCard(user.getUsername(), vcard);
                } catch (UnsupportedOperationException e) {
                    Log.debug("Entity '{}' tried to set VCard, but the configured VCard provider is read-only. An IQ error will be returned to sender.", packet.getFrom());
                    // VCards can include binary data. Let's not echo that back in the error.
                    // result.setChildElement( packet.getChildElement().createCopy() );
                    result.setError(PacketError.Condition.not_allowed);
                    // default to server locale.
                    Locale locale = JiveGlobals.getLocale();
                    final Session session = SessionManager.getInstance().getSession(result.getTo());
                    if (session != null && session.getLanguage() != null) {
                        // use client locale if one is available.
                        locale = session.getLanguage();
                    }
                    result.getError().setText(LocaleUtils.getLocalizedString("vcard.read_only", locale), locale.getLanguage());
                }
            }
        } catch (UserNotFoundException e) {
            // VCards can include binary data. Let's not echo that back in the error.
            // 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;
}
Also used : Locale(java.util.Locale) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) User(org.jivesoftware.openfire.user.User) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) Iterator(java.util.Iterator) VCardManager(org.jivesoftware.openfire.vcard.VCardManager) PacketException(org.jivesoftware.openfire.PacketException) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) Session(org.jivesoftware.openfire.session.Session)

Aggregations

Session (org.jivesoftware.openfire.session.Session)18 Element (org.dom4j.Element)5 DomainPair (org.jivesoftware.openfire.session.DomainPair)5 IQ (org.xmpp.packet.IQ)5 PacketInterceptor (org.jivesoftware.openfire.interceptor.PacketInterceptor)4 ClientSession (org.jivesoftware.openfire.session.ClientSession)4 PacketRejectedException (org.jivesoftware.openfire.interceptor.PacketRejectedException)3 LocalClientSession (org.jivesoftware.openfire.session.LocalClientSession)3 OutgoingServerSession (org.jivesoftware.openfire.session.OutgoingServerSession)3 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)3 Packet (org.xmpp.packet.Packet)3 Iterator (java.util.Iterator)2 Locale (java.util.Locale)2 Lock (java.util.concurrent.locks.Lock)2 DefaultElement (org.dom4j.tree.DefaultElement)2 PacketException (org.jivesoftware.openfire.PacketException)2 SessionManager (org.jivesoftware.openfire.SessionManager)2 MUCRoom (org.jivesoftware.openfire.muc.MUCRoom)2 ComponentSession (org.jivesoftware.openfire.session.ComponentSession)2 ConnectionMultiplexerSession (org.jivesoftware.openfire.session.ConnectionMultiplexerSession)2