package votorola.g.net; // Copyright 2011-2012, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import java.util.regex.*; import votorola.g.lang.*; /** Networking utilities. */ public @ThreadSafe final class Net { private Net() {} // changing? change also in g/web/gwt/super/ /** Converts a country code from ISO 3166-1 alpha-2 to top-level domain (ccTLD). * * @param cc the ISO 3166-1 alpha-2 country code in upper case. * @return the country code top-level domain (ccTLD) in lower case. * * @see ccTLD * @see ISO 3166-1 alpha-2 */ public static final String ccTLD( final String cc ) { if( cc.equals( "GB" )) return "uk"; return cc.toLowerCase(); } /** Matches the specified string against the pattern of a dotted decimal IPV4 address * such as "127.0.0.1". Sets goups 1 through 4 to the numeric components. * * @return the match, or null if there is none. */ public static MatchResult matchIPAddress( final String s ) { // exposing a MatchResult rather than Pattern for sake of GWT final Matcher m = IP_ADDRESS_PATTERN.matcher( s ); return m.matches()? m: null; } private static final Pattern IP_ADDRESS_PATTERN = Pattern.compile( "^([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})$" ); /** Returns the widest possible cookie domain (e.g. ".mydomain.dom") for the host, or * null if no domain can be determined from the host specification. * * @param host the host specification. This is normally the fully qualified * domain name of the page host, as for example "hostname.mydomain.dom" or * "mydomain.dom". It can also be an Internet address ("206.248.142.181") or * "localhost". The value can also be null, in which case the result is null. */ public static String widestCookieDomain( final String host ) { String domain = null; // till proven otherwise domain: if( host != null ) { final int hN = host.length(); if( hN == 0 ) break domain; if( matchIPAddress(host) != null ) { domain = host; break domain; } int c = host.lastIndexOf( '.' ); // host.domain[.]tld | domain[.]tld if( c < 0 ) // host | localhost { domain = ".local"; // per http://www.ietf.org/rfc/rfc2965.txt break domain; // not expected } c = host.lastIndexOf( '.', c - 1 ); // host[.]domain.tld | []domain.tld if( c >= 0 ) // host[.]domain.tld { domain = host.substring( c ); // .domain.tld, meaning all hosts of domain break domain; } // else []domain.tld domain = "." + host; // again .domain.tld } return domain; } }