001package 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.
002
003import java.net.*;
004import java.util.regex.*;
005import votorola.g.lang.*;
006
007
008/** URI utilities.
009  *
010  *     @see <a href='http://tools.ietf.org/html/rfc2396' target='_top'>Uniform Resource
011  *       Identifiers (URI): Generic Syntax</a>
012  *     @see <a href='http://tools.ietf.org/html/rfc2732' target='_top'>Format for Literal
013  *       IPv6 Addresses in URL's</a>
014  */
015public @ThreadSafe final class URIX
016{
017
018    private URIX() {}
019
020
021
022    /** Returns an escaped version of the specified string suitable for inclusion in a
023      * URI.
024      *
025      *     @see URLEncoder
026      *     @deprecated in favour of com.sun.jersey.api.uri.UriComponent.encode().
027      */
028    public static @Deprecated String escaped( String string )
029    {
030        try{ string = URLEncoder.encode( string, "UTF-8" ); } // UTF-8 recommended by URLEncoder
031        catch( java.io.UnsupportedEncodingException x ) { throw new RuntimeException( x ); }
032        return string;
033    }
034
035
036
037    /** Returns the URI stripped of any fragment component.
038      */
039    public static String fragmentStripped( String loc )
040    {
041        final int f = loc.lastIndexOf( '#' );
042        if( f >= 0 ) loc = loc.substring( 0, f );
043        return loc;
044    }
045
046
047
048    /** Pattern to strip scheme and leading/trailing path separators '/' from an HTTP URL.
049      * It should always match provided the URL has a path.  The stripped version is
050      * returned in group 1.
051      */
052    public static final Pattern HTTP_STRIP_PATTERN = Pattern.compile( "^(?:http:)?/*(.*[^/])/*$" );
053
054
055
056    /** Returns the stripped version of the HTTP URL if it matches {@linkplain
057      * #HTTP_STRIP_PATTERN HTTP_STRIP_PATTERN}, otherwise the original URL.
058      */
059    public static final String httpStripped( final String loc )
060    {
061        final Matcher m = URIX.HTTP_STRIP_PATTERN.matcher( loc );
062        return m.matches()? m.group(1): loc;
063    }
064
065
066
067 // /** Returns the URI stripped of any query and fragment components.
068 //   */
069 // public static URI queryFragmentStripped( URI uri )
070 // {
071 //     if( uri.getRawQuery() == null && uri.getRawFragment() == null ) return uri;
072 //
073 //     try
074 //     {
075 //         if( uri.isOpaque() ) return new URI // never tested
076 //         (
077 //          // uri.getScheme(), uri.getRawSchemeSpecificPart(), /*fragment*/null
078 //          //// but the multi-arg constructors do quoting/escaping, so give them unescaped:
079 //             uri.getScheme(), uri.getSchemeSpecificPart(), /*fragment*/null
080 //         );
081 //         else return new /*hierarchical*/URI
082 //         (
083 //          // uri.getScheme(), uri.getRawAuthority(), uri.getRawPath(),
084 //          //// ditto
085 //             uri.getScheme(), uri.getAuthority(), uri.getPath(),
086 //             /*query*/null, /*fragment*/null
087 //         );
088 //     }
089 //     catch( URISyntaxException x ) { throw new RuntimeException( x ); }
090 // }
091
092
093
094}