Skip to content

Commit

Permalink
Avoid UnsupportedEncodingException by design, replacing magic string …
Browse files Browse the repository at this point in the history
…"UTF-8" with StandardCharsets.UTF_8 (#18851)

* Replace magic string "UTF-8" with StandardCharsets.UTF_8

This avoids an UnsupportedEncodingException by design.

* Remove unused UnsupportedCharsetException import
  • Loading branch information
Philzen committed Jun 8, 2024
1 parent dc81339 commit 5adf1ff
Show file tree
Hide file tree
Showing 38 changed files with 52 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -40,11 +40,11 @@ public static List<AuthorizationValue> parse(String urlEncodedAuthStr) {
for (String part : parts) {
String[] kvPair = part.split(":");
if (kvPair.length == 2) {
try {
auths.add(new AuthorizationValue(URLDecoder.decode(kvPair[0], "UTF-8"), URLDecoder.decode(kvPair[1], "UTF-8"), "header"));
} catch (UnsupportedEncodingException e) {
LOGGER.warn(e.getMessage());
}
auths.add(new AuthorizationValue(
URLDecoder.decode(kvPair[0], StandardCharsets.UTF_8),
URLDecoder.decode(kvPair[1], StandardCharsets.UTF_8),
"header"
));
}
}
}
Expand All @@ -55,17 +55,11 @@ public static String reconstruct(List<AuthorizationValue> authorizationValueList
if (authorizationValueList != null) {
StringBuilder b = new StringBuilder();
for (AuthorizationValue v : authorizationValueList) {
try {
if (b.toString().length() > 0) {
b.append(",");
}
b.append(URLEncoder.encode(v.getKeyName(), "UTF-8"))
.append(":")
.append(URLEncoder.encode(v.getValue(), "UTF-8"));
} catch (Exception e) {
// continue
LOGGER.error(e.getMessage(), e);
if (b.toString().length() > 0) {
b.append(",");
}
b.append(URLEncoder.encode(v.getKeyName(), StandardCharsets.UTF_8))
.append(":").append(URLEncoder.encode(v.getValue(), StandardCharsets.UTF_8));
}
return b.toString();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,35 @@

package org.openapitools.codegen.languages;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.fasterxml.jackson.core.JsonPointer;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.mifmif.common.regex.Generex;
import io.swagger.v3.oas.models.media.Schema;
import lombok.Setter;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.SupportingFile;
import org.apache.commons.text.StringEscapeUtils;
import org.openapitools.codegen.*;
import org.openapitools.codegen.languages.features.CXFExtServerFeatures;
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.ModelsMap;
import org.openapitools.codegen.model.OperationMap;
import org.openapitools.codegen.model.OperationsMap;
import org.openapitools.codegen.utils.JsonCache;
import org.openapitools.codegen.utils.ModelUtils;
import org.openapitools.codegen.utils.JsonCache.CacheException;
import org.openapitools.codegen.utils.JsonCache.Root.MergePolicy;
import org.openapitools.codegen.utils.ModelUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonPointer;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.mifmif.common.regex.Generex;

import io.swagger.v3.oas.models.media.Schema;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* An Apache CXF-based JAX-RS server with extended capabilities.
Expand Down Expand Up @@ -1289,24 +1282,24 @@ public Map<String, Object> postProcessSupportingFileData(Map<String, Object> obj
if (testDataCache.root().isDirty()) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
testDataCache.root().flush(out);
String testDataJson = out.toString("UTF-8");
String testDataJson = out.toString(StandardCharsets.UTF_8);
objs.put("test-data.json", testDataJson);
supportingFiles.add(new SupportingFile("testData.mustache", testDataFile.getAbsolutePath()));
}
} catch (CacheException | UnsupportedEncodingException e) {
} catch (CacheException e) {
LOGGER.error("Error writing JSON test data file " + testDataFile, e);
}

try {
if (testDataControlCache.root().isDirty()) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
testDataControlCache.root().flush(out);
String testDataControlJson = out.toString("UTF-8");
String testDataControlJson = out.toString(StandardCharsets.UTF_8);
objs.put("test-data-control.json", testDataControlJson);
supportingFiles
.add(new SupportingFile("testDataControl.mustache", testDataControlFile.getAbsolutePath()));
}
} catch (CacheException | UnsupportedEncodingException e) {
} catch (CacheException e) {
LOGGER.error("Error writing JSON test data control file " + testDataControlFile, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.servers.Server;
import lombok.Getter;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.SupportingFile;
import org.apache.commons.text.StringEscapeUtils;
import org.openapitools.codegen.*;
import org.openapitools.codegen.meta.GeneratorMetadata;
import org.openapitools.codegen.meta.Stability;
import org.openapitools.codegen.meta.features.*;
Expand All @@ -37,9 +33,11 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
import java.nio.charset.StandardCharsets;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import static org.openapitools.codegen.utils.StringUtils.camelize;

Expand Down Expand Up @@ -349,23 +347,17 @@ public String encodePath(String input) {
// from AbstractPhpCodegen.java
// Trim the string to avoid leading and trailing spaces.
input = input.trim();
try {

input = URLEncoder.encode(input, "UTF-8")
.replaceAll("\\+", "%20")
.replaceAll("\\%2F", "/")
.replaceAll("\\%7B", "{") // keep { part of complex placeholders
.replaceAll("\\%7D", "}") // } part
.replaceAll("\\%5B", "[") // [ part
.replaceAll("\\%5D", "]") // ] part
.replaceAll("\\%3A", ":") // : part
.replaceAll("\\%2B", "+") // + part
.replaceAll("\\%5C\\%5Cd", "\\\\d"); // \d part
} catch (UnsupportedEncodingException e) {
// continue
LOGGER.error(e.getMessage(), e);
}
return input;

return URLEncoder.encode(input, StandardCharsets.UTF_8)
.replaceAll("\\+", "%20")
.replaceAll("\\%2F", "/")
.replaceAll("\\%7B", "{") // keep { part of complex placeholders
.replaceAll("\\%7D", "}") // } part
.replaceAll("\\%5B", "[") // [ part
.replaceAll("\\%5D", "]") // ] part
.replaceAll("\\%3A", ":") // : part
.replaceAll("\\%2B", "+") // + part
.replaceAll("\\%5C\\%5Cd", "\\\\d"); // \d part
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -395,11 +395,7 @@ public static String getSimpleRef(String ref) {
return null;
}

try {
ref = URLDecoder.decode(ref, "UTF-8");
} catch (UnsupportedEncodingException ignored) {
once(LOGGER).warn("Found UnsupportedEncodingException: {}", ref);
}
ref = URLDecoder.decode(ref, StandardCharsets.UTF_8);

// see https://tools.ietf.org/html/rfc6901#section-3
// Because the characters '~' (%x7E) and '/' (%x2F) have special meanings in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import java.net.URI;
import java.util.Map;
import java.util.List;

import java.io.UnsupportedEncodingException;

public class HttpBasicAuth implements Authentication {
private String username;
private String password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package {{invokerPackage}}.auth;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package {{invokerPackage}}.auth;

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

import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.*;

Expand Down Expand Up @@ -51,12 +52,7 @@ private static GenerationState compileModule(String moduleName, List<String> sou
CompilerConfiguration configuration = new CompilerConfiguration();
configuration.put(CommonConfigurationKeys.MODULE_NAME, moduleName);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = null;
try {
ps = new PrintStream(baos, true, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
PrintStream ps = new PrintStream(baos, true, StandardCharsets.UTF_8);
configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, new PrintingMessageCollector(ps, MessageRenderer.PLAIN_FULL_PATHS, true));
configuration.put(JVMConfigurationKeys.OUTPUT_DIRECTORY, saveClassesDir);
// configuration.put(JVMConfigurationKeys.RETAIN_OUTPUT_IN_MEMORY, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -205,7 +204,7 @@ public String toString() {
private JsonCache.Root root;
private JsonCache cache;

private void reload() throws CacheException, UnsupportedEncodingException {
private void reload() throws CacheException {
root.unload();
root.load(new ByteArrayInputStream(JSON.getBytes(StandardCharsets.UTF_8)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.Map;
import java.util.List;

import java.io.UnsupportedEncodingException;

public class HttpBasicAuth implements Authentication {
private String username;
private String password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.Map;
import java.util.List;

import java.io.UnsupportedEncodingException;

public class HttpBasicAuth implements Authentication {
private String username;
private String password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.Map;
import java.util.List;

import java.io.UnsupportedEncodingException;

public class HttpBasicAuth implements Authentication {
private String username;
private String password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.Map;
import java.util.List;

import java.io.UnsupportedEncodingException;

public class HttpBasicAuth implements Authentication {
private String username;
private String password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.openapitools.client.auth;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.openapitools.client.auth;

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

import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.Map;
import java.util.List;

import java.io.UnsupportedEncodingException;

public class HttpBasicAuth implements Authentication {
private String username;
private String password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.Map;
import java.util.List;

import java.io.UnsupportedEncodingException;

public class HttpBasicAuth implements Authentication {
private String username;
private String password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.Map;
import java.util.List;

import java.io.UnsupportedEncodingException;

public class HttpBasicAuth implements Authentication {
private String username;
private String password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.Map;
import java.util.List;

import java.io.UnsupportedEncodingException;

public class HttpBasicAuth implements Authentication {
private String username;
private String password;
Expand Down
Loading

0 comments on commit 5adf1ff

Please sign in to comment.