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 1 commit
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
35 changes: 12 additions & 23 deletions impl/src/main/java/com/sun/faces/context/UrlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@

package com.sun.faces.context;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.nio.charset.Charset;
import java.util.*;
pizzi80 marked this conversation as resolved.
Show resolved Hide resolved

import com.sun.faces.util.Util;

import jakarta.faces.context.FacesContext;
import jakarta.faces.lifecycle.ClientWindow;
import jakarta.faces.render.ResponseStateManager;

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

/**
* <p>
* The <strong>UrlBuilder</strong> provides a convenient way to assemble a URL. It follows the standard Builder Pattern.
Expand All @@ -49,16 +46,16 @@ class UrlBuilder {
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";
public static final String DEFAULT_ENCODING = UTF_8.name();

private static final List<String> NULL_LIST = Arrays.asList((String) null);
private static final List<String> NULL_LIST = Collections.singletonList((String) null);

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 +65,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 Down Expand Up @@ -271,11 +268,7 @@ protected void addValuesToParameter(String name, List<String> valuesRef, boolean
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);
}
values.add(URLEncoder.encode(string, encoding));
} else {
values.add(string);
}
Expand All @@ -290,12 +283,8 @@ 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);
parameters.computeIfAbsent(name, k -> new ArrayList<>(1))
.addAll(values);
}
}

Expand Down