package 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. import com.google.gwt.http.client.URL; /** {@linkplain URL URL} utilities. */ public final class URLX // cannot extend URL, it's final (2.3) { private URLX() {} /** Encodes the path such that all reserved characters are escaped. * * @see URL#encodePathSegment(String) * @see Uniform Resource Identifiers (URI): Generic Syntax */ public static String encodePath( final String decodedPath ) { // return URL.encodePathSegment(decodedPath).replace( "%2F", "/" ); /// but it erroneously escapes non-reserved ':' and maybe others, so roll our own: final StringBuilder b = GWTX.stringBuilderClear(); for( int c = 0, cN = decodedPath.length(); c < cN; ++c ) { char ch = decodedPath.charAt( c ); if( ch == ';' ) b.append( "%3B" ); else if( ch == '=' ) b.append( "%3D" ); else if( ch == '?' ) b.append( "%3F" ); else b.append( ch ); } return b.toString(); } /** Returns an arbitrary value for adding to a request URL in order to defeat caching * on the client side. The return value is not suitable for use in cryptography; it * is based on a non-secure random number which is merely incremented on each call. * It will contain no illegal characters and therefore need not be encoded for use in * a query parameter or elsewhere in a URL. */ public static String serialNonce() { if( serial == -1 ) serial = Math.abs( com.google.gwt.user.client.Random.nextInt() ); else serial = GWTX.wrapMaxInteger( ++serial, 0 ); return Integer.toString( serial, /*radix*/36 ); } private static int serial = -1; }