@Test public void testRemoveParameterFromUrl() { assertEquals("http: assertEquals("http: assertEquals( "http: UrlUtil.removeParameterFromUrl("http: ); assertEquals("http: assertEquals( "http: UrlUtil.removeParameterFromUrl( "http: ) ); assertEquals( "http: UrlUtil.removeParameterFromUrl( "http: ) ); }
@SuppressWarnings("IfStatementWithIdenticalBranches") public static String removeParameterFromUrl(@Nonnull String url, @Nullable String parameterName) { if (!isValidUri(url) || StringUtil.isBlank(parameterName)) { return url; } int questionSignPos = url.indexOf('?'); int sharpPos = url.indexOf('#'); if (questionSignPos == -1 && sharpPos == -1) { return url; } else if (questionSignPos == -1 || sharpPos != -1 && questionSignPos > sharpPos) { return url; } else { StringBuilder resultUrl = new StringBuilder(url.substring(0, questionSignPos)); if (url.length() > questionSignPos + 1) { String query = sharpPos == -1 ? url.substring(questionSignPos + 1) : url.substring(questionSignPos + 1, sharpPos); query = removeParameterFromQuery(query, parameterName); if (!StringUtil.isBlank(query)) { resultUrl.append('?').append(query); } if (sharpPos != -1) { resultUrl.append(url.substring(sharpPos)); } } return resultUrl.toString(); } }
UrlUtil { @SuppressWarnings("IfStatementWithIdenticalBranches") public static String removeParameterFromUrl(@Nonnull String url, @Nullable String parameterName) { if (!isValidUri(url) || StringUtil.isBlank(parameterName)) { return url; } int questionSignPos = url.indexOf('?'); int sharpPos = url.indexOf('#'); if (questionSignPos == -1 && sharpPos == -1) { return url; } else if (questionSignPos == -1 || sharpPos != -1 && questionSignPos > sharpPos) { return url; } else { StringBuilder resultUrl = new StringBuilder(url.substring(0, questionSignPos)); if (url.length() > questionSignPos + 1) { String query = sharpPos == -1 ? url.substring(questionSignPos + 1) : url.substring(questionSignPos + 1, sharpPos); query = removeParameterFromQuery(query, parameterName); if (!StringUtil.isBlank(query)) { resultUrl.append('?').append(query); } if (sharpPos != -1) { resultUrl.append(url.substring(sharpPos)); } } return resultUrl.toString(); } } }
UrlUtil { @SuppressWarnings("IfStatementWithIdenticalBranches") public static String removeParameterFromUrl(@Nonnull String url, @Nullable String parameterName) { if (!isValidUri(url) || StringUtil.isBlank(parameterName)) { return url; } int questionSignPos = url.indexOf('?'); int sharpPos = url.indexOf('#'); if (questionSignPos == -1 && sharpPos == -1) { return url; } else if (questionSignPos == -1 || sharpPos != -1 && questionSignPos > sharpPos) { return url; } else { StringBuilder resultUrl = new StringBuilder(url.substring(0, questionSignPos)); if (url.length() > questionSignPos + 1) { String query = sharpPos == -1 ? url.substring(questionSignPos + 1) : url.substring(questionSignPos + 1, sharpPos); query = removeParameterFromQuery(query, parameterName); if (!StringUtil.isBlank(query)) { resultUrl.append('?').append(query); } if (sharpPos != -1) { resultUrl.append(url.substring(sharpPos)); } } return resultUrl.toString(); } } private  UrlUtil(); }
UrlUtil { @SuppressWarnings("IfStatementWithIdenticalBranches") public static String removeParameterFromUrl(@Nonnull String url, @Nullable String parameterName) { if (!isValidUri(url) || StringUtil.isBlank(parameterName)) { return url; } int questionSignPos = url.indexOf('?'); int sharpPos = url.indexOf('#'); if (questionSignPos == -1 && sharpPos == -1) { return url; } else if (questionSignPos == -1 || sharpPos != -1 && questionSignPos > sharpPos) { return url; } else { StringBuilder resultUrl = new StringBuilder(url.substring(0, questionSignPos)); if (url.length() > questionSignPos + 1) { String query = sharpPos == -1 ? url.substring(questionSignPos + 1) : url.substring(questionSignPos + 1, sharpPos); query = removeParameterFromQuery(query, parameterName); if (!StringUtil.isBlank(query)) { resultUrl.append('?').append(query); } if (sharpPos != -1) { resultUrl.append(url.substring(sharpPos)); } } return resultUrl.toString(); } } private  UrlUtil(); @SuppressWarnings("unused") static Set<String> getParameterNames(@Nonnull String url); @SuppressWarnings("IfStatementWithIdenticalBranches") static String removeParameterFromUrl(@Nonnull String url, @Nullable String parameterName); static String replaceParameterInUrl(
            @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue); static String appendParameterToUrl(
            @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue); @SuppressWarnings("OverlyComplexMethod") static String appendParametersToUrl(@Nonnull String url, @Nonnull String... parameterParts); @SuppressWarnings("unused") static String appendRelativePathToUrl(@Nonnull String url, @Nullable String relativePath); @SuppressWarnings("BooleanMethodIsAlwaysInverted") @Contract("null -> false") static boolean isValidUrl(@Nullable String url); @Contract("null, _ -> false") static boolean isValidUrl(@Nullable String url, String[] allowedSchemes); @SuppressWarnings({"ResultOfMethodCallIgnored", "BooleanMethodIsAlwaysInverted"}) @Contract("null -> false") static boolean isValidUri(@Nullable String uri); @SuppressWarnings("unused") @Contract("null -> null") @Nullable static String extractFileName(@Nullable String url); }
UrlUtil { @SuppressWarnings("IfStatementWithIdenticalBranches") public static String removeParameterFromUrl(@Nonnull String url, @Nullable String parameterName) { if (!isValidUri(url) || StringUtil.isBlank(parameterName)) { return url; } int questionSignPos = url.indexOf('?'); int sharpPos = url.indexOf('#'); if (questionSignPos == -1 && sharpPos == -1) { return url; } else if (questionSignPos == -1 || sharpPos != -1 && questionSignPos > sharpPos) { return url; } else { StringBuilder resultUrl = new StringBuilder(url.substring(0, questionSignPos)); if (url.length() > questionSignPos + 1) { String query = sharpPos == -1 ? url.substring(questionSignPos + 1) : url.substring(questionSignPos + 1, sharpPos); query = removeParameterFromQuery(query, parameterName); if (!StringUtil.isBlank(query)) { resultUrl.append('?').append(query); } if (sharpPos != -1) { resultUrl.append(url.substring(sharpPos)); } } return resultUrl.toString(); } } private  UrlUtil(); @SuppressWarnings("unused") static Set<String> getParameterNames(@Nonnull String url); @SuppressWarnings("IfStatementWithIdenticalBranches") static String removeParameterFromUrl(@Nonnull String url, @Nullable String parameterName); static String replaceParameterInUrl(
            @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue); static String appendParameterToUrl(
            @Nonnull String url, @Nullable String parameterName, @Nullable String parameterValue); @SuppressWarnings("OverlyComplexMethod") static String appendParametersToUrl(@Nonnull String url, @Nonnull String... parameterParts); @SuppressWarnings("unused") static String appendRelativePathToUrl(@Nonnull String url, @Nullable String relativePath); @SuppressWarnings("BooleanMethodIsAlwaysInverted") @Contract("null -> false") static boolean isValidUrl(@Nullable String url); @Contract("null, _ -> false") static boolean isValidUrl(@Nullable String url, String[] allowedSchemes); @SuppressWarnings({"ResultOfMethodCallIgnored", "BooleanMethodIsAlwaysInverted"}) @Contract("null -> false") static boolean isValidUri(@Nullable String uri); @SuppressWarnings("unused") @Contract("null -> null") @Nullable static String extractFileName(@Nullable String url);  }