/*
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jivesoftware.openfire.handler;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.QName;
import org.jivesoftware.admin.AdminConsole;
import org.jivesoftware.openfire.IQHandlerInfo;
import org.jivesoftware.openfire.PacketException;
import org.jivesoftware.openfire.disco.ServerFeaturesProvider;
import org.xmpp.packet.IQ;
import org.xmpp.packet.PacketError;

import java.util.Collections;
import java.util.Iterator;

/**
 * Implements the TYPE_IQ jabber:iq:version protocol (version info). Allows
 * XMPP entities to query each other's application versions.  The server
 * will respond with its current version info.
 *
 * @author Iain Shigeoka
 */
public class IQVersionHandler extends IQHandler implements ServerFeaturesProvider {

    private static Element bodyElement;
    private IQHandlerInfo info;

    public IQVersionHandler() {
        super("XMPP Server Version Handler");
        info = new IQHandlerInfo("query", "jabber:iq:version");
        if (bodyElement == null) {
            bodyElement = DocumentHelper.createElement(QName.get("query", "jabber:iq:version"));
            bodyElement.addElement("name").setText(AdminConsole.getAppName());
            bodyElement.addElement("version").setText(AdminConsole.getVersionString());
        }
    }

    @Override
    public IQ handleIQ(IQ packet) throws PacketException {
        if (IQ.Type.get == packet.getType()) {
            // Could cache this information for every server we see
            Element answerElement = bodyElement.createCopy();
            try {
                // Try to retrieve this for every request - security settings
                // might be changed runtime!
                final String os = System.getProperty("os.name") + ' ' 
                        + System.getProperty("os.version") + " ("
                        + System.getProperty("os.arch") + ')';
                final String java = "Java " + System.getProperty("java.version");
                answerElement.addElement("os").setText(os + " - " + java);
            } catch (SecurityException ex) {
                // Security settings don't allow the OS to be read. We'll honor
                // this and simply not report it.
            }
            IQ result = IQ.createResultIQ(packet);
            result.setChildElement(answerElement);
            return result;
        } else if (IQ.Type.set == packet.getType()) {
            // Answer an not-acceptable error since IQ should be of type GET
            IQ result = IQ.createResultIQ(packet);
            result.setError(PacketError.Condition.not_acceptable);
            return result;
        }
        // Ignore any other type of packet
        return null;
    }

    @Override
    public IQHandlerInfo getInfo() {
        return info;
    }

    @Override
    public Iterator<String> getFeatures() {
        return Collections.singleton("jabber:iq:version").iterator();
    }
}

@@ -928,36 +928,193 @@
 ire.
-disco.ServerFeaturesProvider
+XMPPServer;%0Aimport org.jivesoftware.openfire.disco.ServerFeaturesProvider;%0Aimport org.jivesoftware.openfire.session.LocalSession;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
 ;%0Aim
@@ -1231,16 +1231,39 @@
 terator;
+%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -1646,16 +1646,104 @@
  info;%0A%0A
+    private static final Logger Log = LoggerFactory.getLogger(IQVersionHandler.class);%0A%0A
     publ
@@ -3507,16 +3507,16 @@
 table);%0A
-
         
@@ -3526,32 +3526,1456 @@
  return result;%0A
+        %7D else if (IQ.Type.result == packet.getType()) %7B%0A            /* handle results coming through BOSH Connections,%0A             * other results are processed in org.jivesoftware.openfire.net.SocketRead.java - getIQ()%0A             */%0A            LocalSession localSession = (LocalSession) XMPPServer.getInstance().getSessionManager().getSession(packet.getFrom());%0A%0A            Element query = packet.getChildElement();%0A            List%3CElement%3E elements = query.elements();%0A            if (elements.size() %3E 0) %7B%0A                for (Element element : elements) %7B%0A                    if (element.getName() != null && element.getStringValue() != null) %7B%0A                        if (localSession!=null)%0A                        %7B%0A                            localSession.setSoftwareVersionData(element.getName(), element.getStringValue());%0A                        %7D%0A                        else%0A                        %7B%0A                            /*%0A                              The result comes from a server 2 server connection, so we write the information%0A                              only to the debug log, because we dont need it at this point.%0A                            */%0A                            Log.debug(%22XEP-0092 Packet from=%7B%7D %7B%7D=%7B%7D%22,packet.getFrom(),element.getName(),element.getStringValue());%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D%0A%0A            return null;%0A
         %7D%0A      

handle result packets in IQVersionHandler (#1792)