1 /* 2 * ApplicationUtils.java 3 * 4 * Copyright (C) 2021 by RStudio, PBC 5 * 6 * Unless you have received this program directly from RStudio pursuant 7 * to the terms of a commercial license agreement with RStudio, then 8 * this program is licensed to you under the terms of version 3 of the 9 * GNU Affero General Public License. This program is distributed WITHOUT 10 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, 11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the 12 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. 13 * 14 */ 15 16 package org.rstudio.studio.client.application; 17 18 import java.util.ArrayList; 19 import java.util.List; 20 import java.util.Map; 21 22 import org.rstudio.core.client.Debug; 23 import org.rstudio.core.client.dom.WindowEx; 24 import org.rstudio.core.client.regex.Pattern; 25 26 import com.google.gwt.core.client.GWT; 27 import com.google.gwt.http.client.URL; 28 import com.google.gwt.user.client.Window; 29 30 31 public class ApplicationUtils 32 { getHostPageBaseURLWithoutContext(boolean includeSlash)33 public static String getHostPageBaseURLWithoutContext(boolean includeSlash) 34 { 35 String replaceWith = includeSlash ? "/" : ""; 36 String url = GWT.getHostPageBaseURL(); 37 Pattern pattern = Pattern.create("/s/[A-Fa-f0-9]{5}[A-Fa-f0-9]{8}[A-Fa-f0-9]{8}/"); 38 return pattern.replaceAll(url, replaceWith); 39 } 40 41 // Returns: 42 // < 0 if version1 is earlier than version 2 43 // 0 if version1 and version2 are the same 44 // > 0 if version1 is later than version 2 compareVersions(String version1, String version2)45 public static int compareVersions(String version1, String version2) 46 { 47 String[] v1parts = version1.split("\\."); 48 String[] v2parts = version2.split("\\."); 49 int numParts = Math.min(v1parts.length, v2parts.length); 50 for (int i = 0; i < numParts; i++) 51 { 52 int result = Integer.parseInt(v1parts[i]) - 53 Integer.parseInt(v2parts[i]); 54 if (result != 0) 55 return result; 56 } 57 return 0; 58 } 59 getRemainingQueryString(List<String> removeKeys)60 public static String getRemainingQueryString(List<String> removeKeys) 61 { 62 Map<String, List<String>> params = Window.Location.getParameterMap(); 63 StringBuilder queryString = new StringBuilder(); 64 String prefix = ""; 65 for (Map.Entry<String, List<String>> entry : params.entrySet()) { 66 67 // skip keys we are supposed to remove 68 if (removeKeys.contains(entry.getKey())) 69 continue; 70 71 for (String val : entry.getValue()) { 72 queryString.append(prefix) 73 .append(URL.encodeQueryString(entry.getKey())) 74 .append('='); 75 if (val != null) { 76 queryString.append(URL.encodeQueryString(val)); 77 } 78 prefix = "&"; 79 } 80 } 81 82 // return string 83 return queryString.toString(); 84 } 85 removeQueryParam(String param)86 public static void removeQueryParam(String param) 87 { 88 ArrayList<String> params = new ArrayList<>(); 89 params.add(param); 90 removeQueryParams(params); 91 } 92 removeQueryParams(List<String> removeKeys)93 public static void removeQueryParams(List<String> removeKeys) 94 { 95 // determine the new URL 96 String url = GWT.getHostPageBaseURL(); 97 String queryString = getRemainingQueryString(removeKeys); 98 if (queryString.length() > 0) 99 url = url + "?" + queryString; 100 101 // replace history state (try/catch in case the browser doesn't 102 // support this or has it disabled) 103 try 104 { 105 WindowEx.get().replaceHistoryState(url); 106 } 107 catch(Exception e) 108 { 109 Debug.logException(e); 110 } 111 } 112 } 113