diff --git a/build-tools/src/main/java/org/elasticsearch/gradle/LoggedExec.java b/build-tools/src/main/java/org/elasticsearch/gradle/LoggedExec.java index 3f1543908889c..ec7479a4867c0 100644 --- a/build-tools/src/main/java/org/elasticsearch/gradle/LoggedExec.java +++ b/build-tools/src/main/java/org/elasticsearch/gradle/LoggedExec.java @@ -26,7 +26,6 @@ import java.io.IOException; import java.io.OutputStream; import java.io.UncheckedIOException; -import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.function.Consumer; @@ -95,13 +94,7 @@ public void setSpoolOutput(boolean spoolOutput) { }; } else { out = new ByteArrayOutputStream(); - outputLogger = logger -> { - try { - logger.error(((ByteArrayOutputStream) out).toString("UTF-8")); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - }; + outputLogger = logger -> { logger.error(((ByteArrayOutputStream) out).toString(StandardCharsets.UTF_8)); }; } setStandardOutput(out); setErrorOutput(out); @@ -134,13 +127,9 @@ private static ExecResult genericExec(Function LOGGER.error("| " + s)); - } - } catch (UnsupportedEncodingException ue) { - throw new GradleException("Failed to read exec output", ue); + if (output.size() != 0) { + LOGGER.error("Exec output and error:"); + NEWLINE.splitAsStream(output.toString(StandardCharsets.UTF_8)).forEach(s -> LOGGER.error("| " + s)); } throw e; } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/URLDecodeProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/URLDecodeProcessor.java index b0a68ebe000a2..24c32d123a244 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/URLDecodeProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/URLDecodeProcessor.java @@ -8,8 +8,8 @@ package org.elasticsearch.ingest.common; -import java.io.UnsupportedEncodingException; import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.Map; /** @@ -24,11 +24,7 @@ public final class URLDecodeProcessor extends AbstractStringProcessor { } public static String apply(String value) { - try { - return URLDecoder.decode(value, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new IllegalArgumentException("Could not URL-decode value.", e); - } + return URLDecoder.decode(value, StandardCharsets.UTF_8); } @Override diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/URLDecodeProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/URLDecodeProcessorTests.java index 7e9e3ddc39eac..6284e169f44b8 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/URLDecodeProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/URLDecodeProcessorTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.ingest.common; -import java.io.UnsupportedEncodingException; import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.List; public class URLDecodeProcessorTests extends AbstractStringProcessorTestCase { @@ -25,11 +25,7 @@ protected AbstractStringProcessor newProcessor(String field, boolean ign @Override protected String expectedResult(String input) { - try { - return "Hello Günter" + URLDecoder.decode(input, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new IllegalArgumentException("invalid"); - } + return "Hello Günter" + URLDecoder.decode(input, StandardCharsets.UTF_8); } @Override diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteRequestBuilders.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteRequestBuilders.java index 351427ce31526..d6942fa87687f 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteRequestBuilders.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteRequestBuilders.java @@ -27,8 +27,8 @@ import org.elasticsearch.xcontent.json.JsonXContent; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.stream.Collectors; @@ -165,11 +165,7 @@ private static void addIndices(StringBuilder path, String[] indices) { } private static String encodeIndex(String s) { - try { - return URLEncoder.encode(s, "utf-8"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } + return URLEncoder.encode(s, StandardCharsets.UTF_8); } private static String sortToUri(SortBuilder sort) { diff --git a/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedPassageFormatter.java b/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedPassageFormatter.java index d9d75e85842a3..0ab917400c667 100644 --- a/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedPassageFormatter.java +++ b/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedPassageFormatter.java @@ -16,7 +16,6 @@ import org.elasticsearch.lucene.search.uhighlight.Snippet; import org.elasticsearch.search.fetch.subphase.highlight.HighlightUtils; -import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -119,38 +118,28 @@ public String toString() { // Merge original annotations and search hits into a single set of markups for each passage static MarkupPassage mergeAnnotations(AnnotationToken[] annotations, Passage passage) { - try { - MarkupPassage markupPassage = new MarkupPassage(); - - // Add search hits first - they take precedence over any other markup - for (int i = 0; i < passage.getNumMatches(); i++) { - int start = passage.getMatchStarts()[i]; - int end = passage.getMatchEnds()[i]; - String searchTerm = passage.getMatchTerms()[i].utf8ToString(); - Markup markup = new Markup( - start, - end, - SEARCH_HIT_TYPE + "=" + URLEncoder.encode(searchTerm, StandardCharsets.UTF_8.name()) - ); - markupPassage.addUnlessOverlapping(markup); - } + MarkupPassage markupPassage = new MarkupPassage(); + + // Add search hits first - they take precedence over any other markup + for (int i = 0; i < passage.getNumMatches(); i++) { + int start = passage.getMatchStarts()[i]; + int end = passage.getMatchEnds()[i]; + String searchTerm = passage.getMatchTerms()[i].utf8ToString(); + Markup markup = new Markup(start, end, SEARCH_HIT_TYPE + "=" + URLEncoder.encode(searchTerm, StandardCharsets.UTF_8)); + markupPassage.addUnlessOverlapping(markup); + } - // Now add original text's annotations - ignoring any that might conflict with the search hits markup. - for (AnnotationToken token : annotations) { - int start = token.offset(); - int end = token.endOffset(); - if (start >= passage.getStartOffset() && end <= passage.getEndOffset()) { - String escapedValue = URLEncoder.encode(token.value(), StandardCharsets.UTF_8.name()); - Markup markup = new Markup(start, end, escapedValue); - markupPassage.addUnlessOverlapping(markup); - } + // Now add original text's annotations - ignoring any that might conflict with the search hits markup. + for (AnnotationToken token : annotations) { + int start = token.offset(); + int end = token.endOffset(); + if (start >= passage.getStartOffset() && end <= passage.getEndOffset()) { + String escapedValue = URLEncoder.encode(token.value(), StandardCharsets.UTF_8); + Markup markup = new Markup(start, end, escapedValue); + markupPassage.addUnlessOverlapping(markup); } - return markupPassage; - - } catch (UnsupportedEncodingException e) { - // We should always have UTF-8 support - throw new IllegalStateException(e); } + return markupPassage; } @Override diff --git a/server/src/main/java/org/elasticsearch/action/DocWriteResponse.java b/server/src/main/java/org/elasticsearch/action/DocWriteResponse.java index d9ffead956d19..f2715710ed51d 100644 --- a/server/src/main/java/org/elasticsearch/action/DocWriteResponse.java +++ b/server/src/main/java/org/elasticsearch/action/DocWriteResponse.java @@ -29,8 +29,8 @@ import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.Locale; import java.util.Objects; @@ -229,19 +229,11 @@ public RestStatus status() { * @return the relative URI for the location of the document */ public String getLocation(@Nullable String routing) { - final String encodedIndex; - final String encodedType; - final String encodedId; - final String encodedRouting; - try { - // encode the path components separately otherwise the path separators will be encoded - encodedIndex = URLEncoder.encode(getIndex(), "UTF-8"); - encodedType = URLEncoder.encode(MapperService.SINGLE_MAPPING_NAME, "UTF-8"); - encodedId = URLEncoder.encode(getId(), "UTF-8"); - encodedRouting = routing == null ? null : URLEncoder.encode(routing, "UTF-8"); - } catch (final UnsupportedEncodingException e) { - throw new AssertionError(e); - } + // encode the path components separately otherwise the path separators will be encoded + final String encodedIndex = URLEncoder.encode(getIndex(), StandardCharsets.UTF_8); + final String encodedType = URLEncoder.encode(MapperService.SINGLE_MAPPING_NAME, StandardCharsets.UTF_8); + final String encodedId = URLEncoder.encode(getId(), StandardCharsets.UTF_8); + final String encodedRouting = routing == null ? null : URLEncoder.encode(routing, StandardCharsets.UTF_8); final String routingStart = "?routing="; final int bufferSizeExcludingRouting = 3 + encodedIndex.length() + encodedType.length() + encodedId.length(); final int bufferSize; diff --git a/server/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java b/server/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java index ba94dcb965f41..b947100bbc95d 100644 --- a/server/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java +++ b/server/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java @@ -45,8 +45,8 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; -import java.io.UnsupportedEncodingException; import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.security.NoSuchAlgorithmException; import java.util.Collections; @@ -387,21 +387,10 @@ static void init(final boolean foreground, final Path pidFile, final boolean qui if (e instanceof CreationException) { // guice: log the shortened exc to the log file ByteArrayOutputStream os = new ByteArrayOutputStream(); - PrintStream ps = null; - try { - ps = new PrintStream(os, false, "UTF-8"); - } catch (UnsupportedEncodingException uee) { - assert false; - e.addSuppressed(uee); - } + PrintStream ps = new PrintStream(os, false, StandardCharsets.UTF_8); new StartupException(e).printStackTrace(ps); ps.flush(); - try { - logger.error("Guice Exception: {}", os.toString("UTF-8")); - } catch (UnsupportedEncodingException uee) { - assert false; - e.addSuppressed(uee); - } + logger.error("Guice Exception: {}", os.toString(StandardCharsets.UTF_8)); } else if (e instanceof NodeValidationException) { logger.error("node validation exception\n{}", e.getMessage()); } else { diff --git a/server/src/test/java/org/elasticsearch/action/index/IndexRequestTests.java b/server/src/test/java/org/elasticsearch/action/index/IndexRequestTests.java index 8652ad00bf20f..024a168bce537 100644 --- a/server/src/test/java/org/elasticsearch/action/index/IndexRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/index/IndexRequestTests.java @@ -30,7 +30,7 @@ import org.elasticsearch.xcontent.XContentType; import java.io.IOException; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.time.Instant; import java.time.ZoneOffset; import java.time.ZonedDateTime; @@ -249,7 +249,7 @@ public void testSerializeDynamicTemplates() throws Exception { } } - public void testToStringSizeLimit() throws UnsupportedEncodingException { + public void testToStringSizeLimit() { IndexRequest request = new IndexRequest("index"); String source = "{\"name\":\"value\"}"; @@ -260,7 +260,7 @@ public void testToStringSizeLimit() throws UnsupportedEncodingException { {"name":"%s"} """.formatted(randomUnicodeOfLength(IndexRequest.MAX_SOURCE_LENGTH_IN_TOSTRING)); request.source(source, XContentType.JSON); - int actualBytes = source.getBytes("UTF-8").length; + int actualBytes = source.getBytes(StandardCharsets.UTF_8).length; assertEquals( "index {[index][null], source[n/a, actual length: [" + new ByteSizeValue(actualBytes).toString() diff --git a/server/src/test/java/org/elasticsearch/common/hashing/MurmurHash3Tests.java b/server/src/test/java/org/elasticsearch/common/hashing/MurmurHash3Tests.java index 04be4994e345f..6a6b0efb05522 100644 --- a/server/src/test/java/org/elasticsearch/common/hashing/MurmurHash3Tests.java +++ b/server/src/test/java/org/elasticsearch/common/hashing/MurmurHash3Tests.java @@ -11,11 +11,10 @@ import org.elasticsearch.common.hash.MurmurHash3; import org.elasticsearch.test.ESTestCase; -import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; public class MurmurHash3Tests extends ESTestCase { - public void testKnownValues() throws UnsupportedEncodingException { + public void testKnownValues() { assertHash(0x629942693e10f867L, 0x92db0b82baeb5347L, "hell", 0); assertHash(0xa78ddff5adae8d10L, 0x128900ef20900135L, "hello", 1); assertHash(0x8a486b23f422e826L, 0xf962a2c58947765fL, "hello ", 2); diff --git a/server/src/test/java/org/elasticsearch/index/query/WrapperQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/WrapperQueryBuilderTests.java index 6feecfe595ece..d4ab0e7b07986 100644 --- a/server/src/test/java/org/elasticsearch/index/query/WrapperQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/WrapperQueryBuilderTests.java @@ -22,7 +22,7 @@ import java.io.IOException; import java.io.UncheckedIOException; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; public class WrapperQueryBuilderTests extends AbstractQueryTestCase { @@ -97,12 +97,7 @@ public void testFromJson() throws IOException { WrapperQueryBuilder parsed = (WrapperQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); - - try { - assertEquals(json, "{}", new String(parsed.source(), "UTF-8")); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } + assertEquals(json, "{}", new String(parsed.source(), StandardCharsets.UTF_8)); } @Override diff --git a/test/framework/src/main/java/org/elasticsearch/cli/MockTerminal.java b/test/framework/src/main/java/org/elasticsearch/cli/MockTerminal.java index 357b49fb31bbd..83708a2f8c4bb 100644 --- a/test/framework/src/main/java/org/elasticsearch/cli/MockTerminal.java +++ b/test/framework/src/main/java/org/elasticsearch/cli/MockTerminal.java @@ -12,7 +12,6 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -92,8 +91,8 @@ public void addSecretInput(String input) { } /** Returns all output written to this terminal. */ - public String getOutput() throws UnsupportedEncodingException { - return stdoutBuffer.toString("UTF-8"); + public String getOutput() { + return stdoutBuffer.toString(StandardCharsets.UTF_8); } /** Returns all bytes written to this terminal. */ @@ -102,8 +101,8 @@ public byte[] getOutputBytes() { } /** Returns all output written to this terminal. */ - public String getErrorOutput() throws UnsupportedEncodingException { - return stderrBuffer.toString("UTF-8"); + public String getErrorOutput() { + return stderrBuffer.toString(StandardCharsets.UTF_8); } /** Wipes the input and output. */ diff --git a/test/framework/src/main/java/org/elasticsearch/snapshots/mockstore/MockRepository.java b/test/framework/src/main/java/org/elasticsearch/snapshots/mockstore/MockRepository.java index 628c0beaff2c0..160ef1657632a 100644 --- a/test/framework/src/main/java/org/elasticsearch/snapshots/mockstore/MockRepository.java +++ b/test/framework/src/main/java/org/elasticsearch/snapshots/mockstore/MockRepository.java @@ -43,7 +43,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -402,10 +402,10 @@ private boolean shouldFail(String blobName, double probability) { private int hashCode(String path) { try { MessageDigest digest = MessageDigest.getInstance("MD5"); - byte[] bytes = digest.digest(path.getBytes("UTF-8")); + byte[] bytes = digest.digest(path.getBytes(StandardCharsets.UTF_8)); int i = 0; return ((bytes[i++] & 0xFF) << 24) | ((bytes[i++] & 0xFF) << 16) | ((bytes[i++] & 0xFF) << 8) | (bytes[i++] & 0xFF); - } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) { + } catch (NoSuchAlgorithmException ex) { throw new ElasticsearchException("cannot calculate hashcode", ex); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/HttpResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/HttpResponse.java index fc82f8900b129..18196edbb04ab 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/HttpResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/HttpResponse.java @@ -10,7 +10,6 @@ import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentType; -import java.io.UnsupportedEncodingException; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -47,8 +46,7 @@ public HttpResponseBuilder withHttpStatus(final int httpStatus) { return this; } - public HttpResponseBuilder withResponseBody(final String responseJson) throws ElasticsearchParseException, - UnsupportedEncodingException { + public HttpResponseBuilder withResponseBody(final String responseJson) throws ElasticsearchParseException { if (responseJson == null || responseJson.trim().isEmpty()) { throw new ElasticsearchParseException( "Invalid string provided as http response body, Failed to parse content to form response body." diff --git a/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/action/SamlIdentityProviderTests.java b/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/action/SamlIdentityProviderTests.java index c7a8520b22efc..2a68eafcc1ba9 100644 --- a/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/action/SamlIdentityProviderTests.java +++ b/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/action/SamlIdentityProviderTests.java @@ -44,7 +44,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UncheckedIOException; -import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -463,8 +462,8 @@ private String base64Encode(byte[] bytes) { return Base64.getEncoder().encodeToString(bytes); } - private static String urlEncode(String param) throws UnsupportedEncodingException { - return URLEncoder.encode(param, StandardCharsets.UTF_8.name()); + private static String urlEncode(String param) { + return URLEncoder.encode(param, StandardCharsets.UTF_8); } private String deflateAndBase64Encode(SAMLObject message) throws Exception { diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/SamlAuthnRequestValidator.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/SamlAuthnRequestValidator.java index e1e07e067fbe8..22bda746b8539 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/SamlAuthnRequestValidator.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/SamlAuthnRequestValidator.java @@ -33,7 +33,6 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; @@ -369,8 +368,8 @@ private byte[] inflate(byte[] bytes) { } } - private String urlEncode(String param) throws UnsupportedEncodingException { - return URLEncoder.encode(param, StandardCharsets.UTF_8.name()); + private String urlEncode(String param) { + return URLEncoder.encode(param, StandardCharsets.UTF_8); } private void logAndRespond(String message, ActionListener listener) { @@ -406,13 +405,9 @@ private ParsedQueryString(String queryString, String samlRequest, String relaySt } public String reconstructQueryParameters() throws ElasticsearchSecurityException { - try { - return relayState == null - ? "SAMLRequest=" + urlEncode(samlRequest) + "&SigAlg=" + urlEncode(sigAlg) - : "SAMLRequest=" + urlEncode(samlRequest) + "&RelayState=" + urlEncode(relayState) + "&SigAlg=" + urlEncode(sigAlg); - } catch (UnsupportedEncodingException e) { - throw new ElasticsearchSecurityException("Cannot reconstruct query for signature verification", e); - } + return relayState == null + ? "SAMLRequest=" + urlEncode(samlRequest) + "&SigAlg=" + urlEncode(sigAlg) + : "SAMLRequest=" + urlEncode(samlRequest) + "&RelayState=" + urlEncode(relayState) + "&SigAlg=" + urlEncode(sigAlg); } } } diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/authn/SamlAuthnRequestValidatorTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/authn/SamlAuthnRequestValidatorTests.java index 9e71e7e9393d1..260a9fbc0fb90 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/authn/SamlAuthnRequestValidatorTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/authn/SamlAuthnRequestValidatorTests.java @@ -29,7 +29,6 @@ import org.opensaml.xmlsec.signature.support.SignatureConstants; import java.io.ByteArrayOutputStream; -import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -328,8 +327,8 @@ private String base64Encode(byte[] bytes) { return Base64.getEncoder().encodeToString(bytes); } - private String urlEncode(String param) throws UnsupportedEncodingException { - return URLEncoder.encode(param, StandardCharsets.UTF_8.name()); + private String urlEncode(String param) { + return URLEncoder.encode(param, StandardCharsets.UTF_8); } private String deflateAndBase64Encode(SAMLObject message) throws Exception { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthenticator.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthenticator.java index 3ea5e3ee15f66..1f2b9b39681c5 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthenticator.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthenticator.java @@ -93,7 +93,6 @@ import org.elasticsearch.xpack.core.ssl.SSLService; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URL; import java.net.URLEncoder; @@ -568,7 +567,7 @@ private void exchangeCodeForToken(AuthorizationCode code, ActionListener) () -> { @@ -593,7 +592,7 @@ public void cancelled() { }); return null; }); - } catch (AuthenticationException | UnsupportedEncodingException | JOSEException e) { + } catch (AuthenticationException | JOSEException e) { tokensListener.onFailure( new ElasticsearchSecurityException("Failed to exchange code for Id Token using the Token Endpoint.", e) ); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlRedirect.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlRedirect.java index ee97ea437396d..a883f83d1329f 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlRedirect.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlRedirect.java @@ -14,7 +14,6 @@ import org.opensaml.xmlsec.signature.support.SignatureConstants; import java.io.ByteArrayOutputStream; -import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.Base64; @@ -79,8 +78,8 @@ private String base64Encode(byte[] bytes) { return Base64.getEncoder().encodeToString(bytes); } - private String urlEncode(String param) throws UnsupportedEncodingException { - return URLEncoder.encode(param, StandardCharsets.US_ASCII.name()); + private String urlEncode(String param) { + return URLEncoder.encode(param, StandardCharsets.US_ASCII); } protected String deflateAndBase64Encode(SAMLObject message) throws Exception { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestHandlerTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestHandlerTests.java index c10581dd56d3c..77370540b22c8 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestHandlerTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestHandlerTests.java @@ -19,7 +19,6 @@ import org.opensaml.security.x509.X509Credential; import org.opensaml.xmlsec.signature.support.SignatureConstants; -import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; import java.net.URLEncoder; @@ -173,8 +172,8 @@ public void testRelayStateIsReturnedInRedirect() throws Exception { assertThat(result.getRelayState(), equalTo("Hail Hydra")); } - private String urlEncode(String str) throws UnsupportedEncodingException { - return URLEncoder.encode(str, StandardCharsets.US_ASCII.name()); + private String urlEncode(String str) { + return URLEncoder.encode(str, StandardCharsets.US_ASCII); } private String buildSignedQueryString(LogoutRequest logoutRequest) throws URISyntaxException { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlRedirectTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlRedirectTests.java index fee65587a296a..d3eb0e2264364 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlRedirectTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlRedirectTests.java @@ -13,7 +13,6 @@ import org.opensaml.saml.saml2.metadata.EntityDescriptor; import org.opensaml.security.x509.X509Credential; -import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; @@ -157,12 +156,12 @@ public void testAuthnRequestSigning() throws Exception { assertThat(validateSignature(queryParam.substring(0, queryParam.length() - 5), signature, credential), equalTo(false)); } - private String parseAndUrlDecodeParameter(String parameter) throws UnsupportedEncodingException { + private String parseAndUrlDecodeParameter(String parameter) { final String value = parameter.split("=", 2)[1]; - return URLDecoder.decode(value, "UTF-8"); + return URLDecoder.decode(value, StandardCharsets.UTF_8); } - private String validateUrlAndGetSignature(String url) throws UnsupportedEncodingException { + private String validateUrlAndGetSignature(String url) { final String params[] = url.split("\\?")[1].split("&"); assert (params.length == 3); String sigAlg = parseAndUrlDecodeParameter(params[1]); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpClient.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpClient.java index e6e6c615be1f6..2e2e181d17681 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpClient.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpClient.java @@ -65,7 +65,6 @@ import java.io.Closeable; import java.io.IOException; import java.io.InputStream; -import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; import java.net.URLDecoder; @@ -366,7 +365,7 @@ static Tuple createURI(HttpRequest request) { String part = pathParts[i]; boolean isLast = i == pathParts.length - 1; if (Strings.isEmpty(part) == false) { - unescapedPathParts.add(URLDecoder.decode(part, StandardCharsets.UTF_8.name())); + unescapedPathParts.add(URLDecoder.decode(part, StandardCharsets.UTF_8)); // if the passed URL ends with a slash, adding an empty string to the // unescaped paths will ensure the slash will be added back boolean appendSlash = isPathEndsWithSlash && isLast; @@ -385,7 +384,7 @@ static Tuple createURI(HttpRequest request) { .build(); final HttpHost httpHost = URIUtils.extractHost(uri); return new Tuple<>(httpHost, uri); - } catch (URISyntaxException | UnsupportedEncodingException e) { + } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpRequest.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpRequest.java index 0626d12907499..34989b322ad87 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpRequest.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpRequest.java @@ -26,11 +26,11 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; import java.net.URLDecoder; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -142,19 +142,11 @@ public HttpProxy proxy() { } public static String encodeUrl(String text) { - try { - return URLEncoder.encode(text, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new IllegalArgumentException("failed to URL encode text [" + text + "]", e); - } + return URLEncoder.encode(text, StandardCharsets.UTF_8); } public static String decodeUrl(String text) { - try { - return URLDecoder.decode(text, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new IllegalArgumentException("failed to URL decode text [" + text + "]", e); - } + return URLDecoder.decode(text, StandardCharsets.UTF_8); } @Override