From 85a4ee6abd7955c180289abbe8d849b7df017176 Mon Sep 17 00:00:00 2001 From: Andrei Arlou Date: Fri, 8 Oct 2021 07:14:19 +0300 Subject: [PATCH] Using StandartCharsets and removing UnsupportedEncodingException (#3479) * Using StandartCharsets and removing UnsupportedEncodingException * fix checkstyle and copyright warnings --- .../helidon/media/common/ContentReaders.java | 11 +------ .../helidon/media/multipart/MimeParser.java | 8 ++--- .../openapi/OpenApiCdiExtension.java | 7 ++-- .../webserver/testsupport/TestRequest.java | 12 ++----- .../io/helidon/webserver/UriComponent.java | 32 ++++++++----------- 5 files changed, 24 insertions(+), 46 deletions(-) diff --git a/media/common/src/main/java/io/helidon/media/common/ContentReaders.java b/media/common/src/main/java/io/helidon/media/common/ContentReaders.java index 14b58f634c0..a5ab4a340c1 100644 --- a/media/common/src/main/java/io/helidon/media/common/ContentReaders.java +++ b/media/common/src/main/java/io/helidon/media/common/ContentReaders.java @@ -19,7 +19,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.nio.ByteBuffer; import java.nio.charset.Charset; @@ -168,15 +167,7 @@ private static final class StringToDecodedString implements Mapper findIndexFiles(String... indexPaths) throws IOException { return result; } - private static void dumpIndex(Level level, Index index) throws UnsupportedEncodingException { + private static void dumpIndex(Level level, Index index) { if (LOGGER.isLoggable(level)) { LOGGER.log(level, "Dump of internal Jandex index:"); PrintStream oldStdout = System.out; ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (PrintStream newPS = new PrintStream(baos, true, Charset.defaultCharset().name())) { + try (PrintStream newPS = new PrintStream(baos, true, Charset.defaultCharset())) { System.setOut(newPS); index.printAnnotations(); index.printSubclasses(); - LOGGER.log(level, baos.toString(Charset.defaultCharset().name())); + LOGGER.log(level, baos.toString(Charset.defaultCharset())); } finally { System.setOut(oldStdout); } diff --git a/webserver/test-support/src/main/java/io/helidon/webserver/testsupport/TestRequest.java b/webserver/test-support/src/main/java/io/helidon/webserver/testsupport/TestRequest.java index e87b562bf4c..6b17157a436 100644 --- a/webserver/test-support/src/main/java/io/helidon/webserver/testsupport/TestRequest.java +++ b/webserver/test-support/src/main/java/io/helidon/webserver/testsupport/TestRequest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2020 Oracle and/or its affiliates. + * Copyright (c) 2017, 2021 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,6 @@ package io.helidon.webserver.testsupport; -import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -96,7 +95,7 @@ public TestRequest queryParameter(String name, String value) { public TestRequest header(String name, String value) { Objects.requireNonNull(name, "Parameter 'name' is null!"); Objects.requireNonNull(name, "Parameter 'value' is null!"); - headers.computeIfAbsent(name, k -> new ArrayList()).add(value); + headers.computeIfAbsent(name, k -> new ArrayList<>()).add(value); return this; } @@ -310,11 +309,6 @@ public TestResponse call(Http.RequestMethod method) throws InterruptedException, } private String encode(String str) { - try { - return URLEncoder.encode(str, StandardCharsets.UTF_8.toString()); - } catch (UnsupportedEncodingException e) { - // Rare case. Should never happen in java - throw new IllegalStateException("UTF-8 is not supported!", e); - } + return URLEncoder.encode(str, StandardCharsets.UTF_8); } } diff --git a/webserver/webserver/src/main/java/io/helidon/webserver/UriComponent.java b/webserver/webserver/src/main/java/io/helidon/webserver/UriComponent.java index 960bbb8b1f8..f8be1df7c40 100644 --- a/webserver/webserver/src/main/java/io/helidon/webserver/UriComponent.java +++ b/webserver/webserver/src/main/java/io/helidon/webserver/UriComponent.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2020 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2021 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,12 +16,13 @@ package io.helidon.webserver; -import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import io.helidon.common.http.HashParameters; import io.helidon.common.http.Parameters; +import static java.nio.charset.StandardCharsets.UTF_8; + /** * Extracted from Jersey *

@@ -86,22 +87,17 @@ static Parameters decodeQuery(String query, boolean decodeNames, boolean decodeV } private static void decodeQueryParam(Parameters params, String param, boolean decodeNames, boolean decodeValues) { - try { - int equals = param.indexOf('='); - if (equals > 0) { - params.add((decodeNames) ? URLDecoder.decode(param.substring(0, equals), "UTF-8") : param.substring(0, equals), - (decodeValues) - ? URLDecoder.decode(param.substring(equals + 1), "UTF-8") - : param.substring(equals + 1)); - } else if (equals == 0) { - // no key declared, ignore - return; - } else if (!param.isEmpty()) { - params.add((decodeNames) ? URLDecoder.decode(param, "UTF-8") : param, ""); - } - } catch (UnsupportedEncodingException ex) { - // This should never occur - throw new IllegalArgumentException("Should never occur!", ex); + int equals = param.indexOf('='); + if (equals > 0) { + params.add((decodeNames) ? URLDecoder.decode(param.substring(0, equals), UTF_8) : param.substring(0, equals), + (decodeValues) + ? URLDecoder.decode(param.substring(equals + 1), UTF_8) + : param.substring(equals + 1)); + } else if (equals == 0) { + // no key declared, ignore + return; + } else if (!param.isEmpty()) { + params.add((decodeNames) ? URLDecoder.decode(param, UTF_8) : param, ""); } } }