/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (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.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */

package com.icesoft.util;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletContext;

public class ServerUtility {
    private static final Logger LOGGER = Logger.getLogger(ServerUtility.class.getName());

    public static boolean isGlassFish(final ServletContext servletContext) {
        /*
         * GlassFish' server info Strings:
         *
         * - V2     : Sun Java System Application Server 9.1
         * - V2.UR1 : Sun Java System Application Server 9.1_01
         * - V2.UR2 : Sun Java System Application Server 9.1_02
         * - V2.1   : Sun GlassFish Enterprise Server v2.1
         * - V2.1.1 : Sun GlassFish Enterprise Server v2.1.1
         * - V3     : GlassFish v3
         * - V3.0.1 : GlassFish Server Open Source Edition 3.0.1
         */
        String _serverInfo = servletContext.getServerInfo();
        return
            _serverInfo.startsWith("GlassFish Server Open Source Edition") ||
            _serverInfo.startsWith("GlassFish") ||
            _serverInfo.startsWith("Sun GlassFish Enterprise Server") ||
            _serverInfo.startsWith("Sun Java System Application Server");
    }

    public static boolean isJBoss(final ServletContext servletContext) {
        /*
         * JBoss AS' server info Strings:
         *
         * - 4.2.0.GA : JBossWeb/2.0.0.GA
         * - 4.2.1.GA : JBossWeb/2.0.0.GA
         * - 4.2.2.GA : JBossWeb/2.0.1.GA
         * - 4.2.3.GA : JBossWeb/2.0.1.GA
         * - 5.0.0.GA : JBoss Web/2.1.1.GA
         * - 5.0.1.GA : JBoss Web/2.1.2.GA
         * - 5.1.0.GA : JBoss Web/2.1.3.GA
         *
         * Notes:
         *
         * - Red Hat JBoss AS 4.0.x.GA does not support JDK 1.5
         */
        String _serverInfo = servletContext.getServerInfo();
        return
            _serverInfo.startsWith("JBossWeb") ||
            _serverInfo.startsWith("JBoss Web");
    }

    public static boolean isJBoss7(final ServletContext servletContext) {
        /*
         * JBoss AS 7's server info Strings:
         *
         * - 7.0.0.Final : JBoss Web/7.0.0.CR4
         * - 7.0.1.Final : JBoss Web/7.0.1.Final
         * - 7.0.2.Final : JBoss Web/7.0.1.Final
         * - 7.1.0.Final : JBoss Web/7.0.10.Final
         */
        String _serverInfo = servletContext.getServerInfo();
        return
            _serverInfo.startsWith("JBoss Web/7");
    }

    public static boolean isTomcat(final ServletContext servletContext) {
        /*
         * Tomcat's server info Strings:
         *
         * 6.0.29 : Apache Tomcat/6.0.29
         */
        String _serverInfo = servletContext.getServerInfo();
        return
            _serverInfo.startsWith("Apache Tomcat");
    }

    public static boolean isWebLogic(final ServletContext servletContext) {
        /*
         * WebLogic Server's server info Strings:
         *
         * - 9.2 MP3  : WebLogic Server 9.2 MP3 Mon Mar 10 08:28:41 EDT 2008 1096261
         * - 10.3.3.0 : WebLogic Server 10.3.3.0 Fri Apr 9 00:05:28 PDT 2010 1321401 Oracle WebLogic Server Module
         *              Dependencies 10.3 Tue Mar 2 15:22:37 PST 2010
         */
        String _serverInfo = servletContext.getServerInfo();
        return
            _serverInfo.startsWith("WebLogic Server");
    }

    public static boolean isWebSphere(final ServletContext servletContext) {
        /*
         * WebSphere Application Server's server info Strings:
         *
         * - 7.0 : IBM WebSphere Application Server/7.0
         */
        String _serverInfo = servletContext.getServerInfo();
        return
            _serverInfo.startsWith("IBM WebSphere Application Server");
    }

    public static String getContextPath(final ServletContext servletContext) {
        String _servletContextPath;
        if (servletContext == null) {
            _servletContextPath = null;
        } else if (
            servletContext.getMajorVersion() > 2 ||
            (servletContext.getMajorVersion() == 2 && servletContext.getMinorVersion() >= 5)) {

            _servletContextPath = servletContext.getContextPath();
        } else {
            try {
                String _path = servletContext.getResource("/WEB-INF/web.xml").getPath();
                String _serverInfo = servletContext.getServerInfo();
                if (
                    _serverInfo.startsWith("WebLogic") &&
                    (
                        _serverInfo.indexOf("9.") != -1 ||
                        _serverInfo.indexOf("10.") != -1
                    )) {

                    int _index = _path.lastIndexOf("/");
                    for (int i = 0; i < 3; i++) {
                        _index = _path.lastIndexOf("/", _index - 1);
                    }
                    _servletContextPath = _path.substring(_path.lastIndexOf("/", _index - 1) + 1, _index);
                } else {
                    int _index = _path.lastIndexOf("/", _path.lastIndexOf("/") - 1);
                    _servletContextPath = _path.substring(_path.lastIndexOf("/", _index - 1) + 1, _index);
                    if (_serverInfo.startsWith("WebLogic")) {
                        _servletContextPath = _servletContextPath.substring(0, _servletContextPath.indexOf(".war"));
                    }
                }
            } catch (MalformedURLException exception) {
                _servletContextPath = null;
            }
        }
        return _servletContextPath;
    }
    
    public static Set<InetAddress> getInetAddressSet() {
        Set<InetAddress> _localAddressSet = new HashSet<InetAddress>(); 
        try {
            Enumeration<NetworkInterface> _networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (_networkInterfaces.hasMoreElements()) {
                NetworkInterface _networkInterface = _networkInterfaces.nextElement();
                Enumeration<InetAddress> _inetAddresses = _networkInterface.getInetAddresses();
                while (_inetAddresses.hasMoreElements()) {
                    InetAddress _inetAddress = _inetAddresses.nextElement();
                    if (!_inetAddress.isLoopbackAddress() &&
                        !_inetAddress.isMulticastAddress() &&
                        _inetAddress instanceof Inet4Address) {

                        _localAddressSet.add(_inetAddress);
                    }
                }
            }
        } catch (final SocketException exception) {
            // Do nothing.
        }
        return _localAddressSet;
    }
}
