Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UrlBuilder: Charset instead of String #5251

Merged
merged 2 commits into from
Jul 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 34 additions & 49 deletions impl/src/main/java/com/sun/faces/context/UrlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package com.sun.faces.context;

import java.io.UnsupportedEncodingException;
import static java.nio.charset.StandardCharsets.UTF_8;

import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -46,19 +46,19 @@
*/
class UrlBuilder {
public static final String QUERY_STRING_SEPARATOR = "?";
public static final char QUERY_STRING_SEPARATOR_CHAR = QUERY_STRING_SEPARATOR.charAt(0);
public static final String PARAMETER_PAIR_SEPARATOR = "&";
public static final String PARAMETER_NAME_VALUE_SEPARATOR = "=";
public static final String FRAGMENT_SEPARATOR = "#";
public static final String DEFAULT_ENCODING = "UTF-8";

private static final List<String> NULL_LIST = Arrays.asList((String) null);
public static final char FRAGMENT_SEPARATOR_CHAR = FRAGMENT_SEPARATOR.charAt(0);
public static final String DEFAULT_ENCODING = UTF_8.name();

private StringBuilder url;
private final StringBuilder url;
private String path;
private String queryString;
private String fragment;
private Map<String, List<String>> parameters;
private String encoding;
private final Charset encoding;

// ------------------------------------------------------------ Constructors

Expand All @@ -68,7 +68,7 @@ public UrlBuilder(String url, String encoding) {
}
this.url = new StringBuilder(url.length() * 2);
extractSegments(url);
this.encoding = encoding;
this.encoding = encoding != null ? Charset.forName(encoding) : null;
// PERF TL lookup per-instance
}

Expand All @@ -79,7 +79,7 @@ public UrlBuilder(String url) {
// ---------------------------------------------------------- Public Methods

public UrlBuilder addParameters(String name, List<String> values) {
if (name == null || name.trim().length() == 0) {
if ( name == null || name.isBlank() ) {
throw new IllegalArgumentException("Parameter name cannot be empty");
}
addValuesToParameter(name.trim(), values, true);
Expand All @@ -94,8 +94,7 @@ public UrlBuilder addParameters(Map<String, List<String>> params) {
throw new IllegalArgumentException("Parameter name cannot be empty");
}
List<String> values = entry.getValue();
List<String> retValues = values;
addValuesToParameter(entry.getKey().trim(), retValues, true);
addValuesToParameter(entry.getKey().trim(), values, true);
}
}

Expand Down Expand Up @@ -187,7 +186,7 @@ protected void appendQueryString() {
nextSeparatorChar = QUERY_STRING_SEPARATOR;
} else {
nextSeparatorChar = PARAMETER_PAIR_SEPARATOR;
url.append(QUERY_STRING_SEPARATOR).append(queryString);
url.append(QUERY_STRING_SEPARATOR_CHAR).append(queryString);
}

for (Map.Entry<String, List<String>> param : parameters.entrySet()) {
Expand Down Expand Up @@ -240,14 +239,14 @@ protected void appendFragment() {
}

protected void extractSegments(String url) {
int fragmentIndex = url.indexOf(FRAGMENT_SEPARATOR);
int fragmentIndex = url.indexOf(FRAGMENT_SEPARATOR_CHAR);
if (fragmentIndex != -1) {
fragment = url.substring(fragmentIndex + 1);
cleanFragment();
url = url.substring(0, fragmentIndex);
}

int queryStringIndex = url.indexOf(QUERY_STRING_SEPARATOR);
int queryStringIndex = url.indexOf(QUERY_STRING_SEPARATOR_CHAR);
if (queryStringIndex != -1) {
queryString = url.substring(queryStringIndex + 1);
cleanQueryString();
Expand All @@ -268,19 +267,11 @@ protected void addValueToParameter(String name, String value, boolean replace) {
protected void addValuesToParameter(String name, List<String> valuesRef, boolean replace) {
List<String> values = new ArrayList<>();
if (valuesRef != null) {
for (Iterator<String> it = valuesRef.iterator(); it.hasNext();) {
String string = it.next();
if (encoding != null) {
try {
values.add(URLEncoder.encode(string, encoding));
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex);
}
} else {
values.add(string);
for (String string : valuesRef) {
if (string != null) {
values.add(encoding != null ? URLEncoder.encode(string, encoding) : string);
}
}
values.removeAll(NULL_LIST);
}

if (parameters == null) {
Expand All @@ -290,45 +281,39 @@ protected void addValuesToParameter(String name, List<String> valuesRef, boolean
if (replace) {
parameters.put(name, values);
} else {
List<String> currentValues = parameters.get(name);
if (currentValues == null) {
currentValues = new ArrayList<>(1);
parameters.put(name, currentValues);
}
currentValues.addAll(values);
List<String> oldValues = parameters.get(name);
// add if exists
if ( oldValues != null ) oldValues.addAll(values);
// put old+new or put only new values
parameters.put( name , oldValues != null ? oldValues : values );
}
}

// --------------------------------------------------------- Private Methods

private void cleanFragment() {
if (fragment != null) {
String f = fragment;
f = f.trim();
if (f.startsWith(FRAGMENT_SEPARATOR)) {
// trim
String f = fragment.trim();
// remove '#'
if ( f.charAt(0) == FRAGMENT_SEPARATOR_CHAR ) {
f = f.substring(1);
}

if (f.length() == 0) {
f = null;
}

fragment = f;
// null if empty
fragment = f.isEmpty() ? null : f;
}
}

private void cleanQueryString() {
if (queryString != null) {
String q = queryString;
q = q.trim();
if (q.startsWith(QUERY_STRING_SEPARATOR)) {
// trim
String q = queryString.trim();
// remove '?'
if ( q.charAt(0) == QUERY_STRING_SEPARATOR_CHAR ) {
q = q.substring(1);
}

if (q.length() == 0) {
q = null;
}
queryString = q;
// null if empty
queryString = q.isEmpty() ? null : q;
}
}

Expand Down