001package votorola.g.web.gwt; // Copyright 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 com.google.gwt.http.client.URL;
004
005
006/** {@linkplain URL URL} utilities.
007  */
008public final class URLX // cannot extend URL, it's final (2.3)
009{
010
011
012    private URLX() {}
013
014
015
016    /** Encodes the path such that all reserved characters are escaped.
017      *
018      *     @see URL#encodePathSegment(String)
019      *     @see <a href='http://tools.ietf.org/html/rfc2396#section-3.3'
020      *       target='_top'>Uniform Resource Identifiers (URI): Generic Syntax</a>
021      */
022    public static String encodePath( final String decodedPath )
023    {
024     // return URL.encodePathSegment(decodedPath).replace( "%2F", "/" );
025     /// but it erroneously escapes non-reserved ':' and maybe others, so roll our own:
026        final StringBuilder b = GWTX.stringBuilderClear();
027        for( int c = 0, cN = decodedPath.length(); c < cN; ++c )
028        {
029            char ch = decodedPath.charAt( c );
030            if( ch == ';' ) b.append( "%3B" );
031            else if( ch == '=' ) b.append( "%3D" );
032            else if( ch == '?' ) b.append( "%3F" );
033            else b.append( ch );
034        }
035        return b.toString();
036    }
037
038
039
040    /** Returns an arbitrary value for adding to a request URL in order to defeat caching
041      * on the client side.  The return value is not suitable for use in cryptography; it
042      * is based on a non-secure random number which is merely incremented on each call.
043      * It will contain no illegal characters and therefore need not be encoded for use in
044      * a query parameter or elsewhere in a URL.
045      */
046    public static String serialNonce()
047    {
048        if( serial == -1 ) serial = Math.abs( com.google.gwt.user.client.Random.nextInt() );
049        else serial = GWTX.wrapMaxInteger( ++serial, 0 );
050        return Integer.toString( serial, /*radix*/36 );
051    }
052
053
054        private static int serial = -1;
055
056
057}