Wednesday, January 26, 2011

Encoding URL delimiters (for GWT)

This is what I do to encode a parameter for my querystring:


public static String encodeUrlDelimiters(String s) {
    if (s == null) {
      return null;
    }
 
    s = s.replaceAll(";", "%2F");
    s = s.replaceAll("/", "%2F");
    s = s.replaceAll(":", "%3A");
    s = s.replaceAll("\\?", "%3F");
    s = s.replaceAll("&", "%26");
    s = s.replaceAll("\\=", "%3D");
    s = s.replaceAll("\\+", "%2B");
    s = s.replaceAll("\\$", "%24");
    s = s.replaceAll(",", "%2C");
    s = s.replaceAll("#", "%23");
 
    return s;
}

This is how I use it:
private String getGoogleLoginUrl() {
    if (cp.getAccessToken() == null && cp.getAccessToken().getThirdPartyLoginData() == null) {
      return null;
    }
    
    String domain = GWT.getHostPageBaseURL();
    String path = Window.Location.getPath();
    String qs = Window.Location.getQueryString();
    String historyToken = History.getToken();
    if (qs == null) {
      qs = "";
    }
    if (domain.matches(".*/") == true) {
      domain = domain.substring(0,domain.length()-1);
    }
    
    String url = domain + path + qs + "#" + historyToken;
    url = URL.encode(url);
    url = Global_String.encodeUrlDelimiters(url);

    String baseUrl = cp.getAccessToken().getThirdPartyLoginData().getGoogleLoginUrl();
    baseUrl = baseUrl.replaceAll("=%2F.*", "=" + url);
    
    return baseUrl;
}

No comments:

Trying out the Dart Analysis Server

I wanted to see how the Dart Analysis Server was put together and worked. I started looking to see how I could wire it up and try out the co...