package votorola.g.net; // Copyright 2007-2008, 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.net.*; import java.util.regex.*; import votorola.g.lang.*; /** URI utilities. * * @see Uniform Resource * Identifiers (URI): Generic Syntax * @see Format for Literal * IPv6 Addresses in URL's */ public @ThreadSafe final class URIX { private URIX() {} /** Returns an escaped version of the specified string suitable for inclusion in a * URI. * * @see URLEncoder * @deprecated in favour of com.sun.jersey.api.uri.UriComponent.encode(). */ public static @Deprecated String escaped( String string ) { try{ string = URLEncoder.encode( string, "UTF-8" ); } // UTF-8 recommended by URLEncoder catch( java.io.UnsupportedEncodingException x ) { throw new RuntimeException( x ); } return string; } /** Returns the URI stripped of any fragment component. */ public static String fragmentStripped( String loc ) { final int f = loc.lastIndexOf( '#' ); if( f >= 0 ) loc = loc.substring( 0, f ); return loc; } /** Pattern to strip scheme and leading/trailing path separators '/' from an HTTP URL. * It should always match provided the URL has a path. The stripped version is * returned in group 1. */ public static final Pattern HTTP_STRIP_PATTERN = Pattern.compile( "^(?:http:)?/*(.*[^/])/*$" ); /** Returns the stripped version of the HTTP URL if it matches {@linkplain * #HTTP_STRIP_PATTERN HTTP_STRIP_PATTERN}, otherwise the original URL. */ public static final String httpStripped( final String loc ) { final Matcher m = URIX.HTTP_STRIP_PATTERN.matcher( loc ); return m.matches()? m.group(1): loc; } // /** Returns the URI stripped of any query and fragment components. // */ // public static URI queryFragmentStripped( URI uri ) // { // if( uri.getRawQuery() == null && uri.getRawFragment() == null ) return uri; // // try // { // if( uri.isOpaque() ) return new URI // never tested // ( // // uri.getScheme(), uri.getRawSchemeSpecificPart(), /*fragment*/null // //// but the multi-arg constructors do quoting/escaping, so give them unescaped: // uri.getScheme(), uri.getSchemeSpecificPart(), /*fragment*/null // ); // else return new /*hierarchical*/URI // ( // // uri.getScheme(), uri.getRawAuthority(), uri.getRawPath(), // //// ditto // uri.getScheme(), uri.getAuthority(), uri.getPath(), // /*query*/null, /*fragment*/null // ); // } // catch( URISyntaxException x ) { throw new RuntimeException( x ); } // } }