001    package railo.runtime.functions.decision;
002    
003    import java.net.Inet6Address;
004    import java.net.InetAddress;
005    import java.net.UnknownHostException;
006    
007    import railo.commons.lang.StringUtil;
008    import railo.runtime.PageContext;
009    import railo.runtime.exp.PageException;
010    import railo.runtime.op.Caster;
011    
012    public class IsIPv6 {
013            public static boolean call(PageContext pc) throws PageException {
014                    try {
015                            InetAddress ia = InetAddress.getLocalHost();
016                            InetAddress[] ias = InetAddress.getAllByName(ia.getHostName());
017                            return _call(ias);
018                    } 
019                    catch (UnknownHostException e) {
020                            throw Caster.toPageException(e);
021                    }
022            }
023            
024            public static boolean call(PageContext pc,String hostName) throws PageException {
025                    if(StringUtil.isEmpty(hostName)) return call(pc);
026                    try {
027                            InetAddress[] ias = InetAddress.getAllByName(hostName);
028                            return _call(ias);
029                    } 
030                    catch (UnknownHostException e) {
031                            if(hostName.equalsIgnoreCase("localhost") || hostName.equals("127.0.0.1") || hostName.equalsIgnoreCase("0:0:0:0:0:0:0:1") || hostName.equalsIgnoreCase("::1"))
032                        return call(pc);
033                    throw Caster.toPageException(e);
034                    }
035            }
036            
037            
038            private static boolean _call(InetAddress[] ias) {
039                    for(int i=0;i<ias.length;i++)        {
040                if(ias[i] instanceof Inet6Address) return true;
041            }
042            return false;
043            }
044    }