From 7900fcb67bb6bb3926fadd73b386051a243f6bb6 Mon Sep 17 00:00:00 2001 From: Captain1653 Date: Thu, 7 Oct 2021 20:43:57 +0300 Subject: [PATCH 1/2] Using StandartCharsets and removing UnsupportedEncodingException --- .../helidon/media/common/ContentReaders.java | 11 +------ .../helidon/media/multipart/MimeParser.java | 6 ++-- .../openapi/OpenApiCdiExtension.java | 7 ++--- .../webserver/testsupport/TestRequest.java | 10 ++----- .../io/helidon/webserver/UriComponent.java | 29 ++++++++----------- 5 files changed, 20 insertions(+), 43 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..a71b5cd695c 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 @@ -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..cf85f61462c 100644 --- a/webserver/webserver/src/main/java/io/helidon/webserver/UriComponent.java +++ b/webserver/webserver/src/main/java/io/helidon/webserver/UriComponent.java @@ -16,8 +16,8 @@ package io.helidon.webserver; -import java.io.UnsupportedEncodingException; import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import io.helidon.common.http.HashParameters; import io.helidon.common.http.Parameters; @@ -86,22 +86,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), StandardCharsets.UTF_8) : param.substring(0, equals), + (decodeValues) + ? URLDecoder.decode(param.substring(equals + 1), StandardCharsets.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, StandardCharsets.UTF_8) : param, ""); } } } From b31d0d47fb897c8415e22db14f5bf964a0b289fd Mon Sep 17 00:00:00 2001 From: Captain1653 Date: Fri, 8 Oct 2021 00:39:34 +0300 Subject: [PATCH 2/2] fix checkstyle and copyright warnings --- .../java/io/helidon/media/multipart/MimeParser.java | 2 +- .../io/helidon/webserver/testsupport/TestRequest.java | 2 +- .../main/java/io/helidon/webserver/UriComponent.java | 11 ++++++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/media/multipart/src/main/java/io/helidon/media/multipart/MimeParser.java b/media/multipart/src/main/java/io/helidon/media/multipart/MimeParser.java index 85df694b511..250d0e93ff4 100644 --- a/media/multipart/src/main/java/io/helidon/media/multipart/MimeParser.java +++ b/media/multipart/src/main/java/io/helidon/media/multipart/MimeParser.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Oracle and/or its affiliates. + * Copyright (c) 2020, 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. 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 a71b5cd695c..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. 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 cf85f61462c..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. @@ -17,11 +17,12 @@ package io.helidon.webserver; import java.net.URLDecoder; -import java.nio.charset.StandardCharsets; import io.helidon.common.http.HashParameters; import io.helidon.common.http.Parameters; +import static java.nio.charset.StandardCharsets.UTF_8; + /** * Extracted from Jersey *

@@ -88,15 +89,15 @@ static Parameters decodeQuery(String query, boolean decodeNames, boolean decodeV private static void decodeQueryParam(Parameters params, String param, boolean decodeNames, boolean decodeValues) { int equals = param.indexOf('='); if (equals > 0) { - params.add((decodeNames) ? URLDecoder.decode(param.substring(0, equals), StandardCharsets.UTF_8) : param.substring(0, equals), + params.add((decodeNames) ? URLDecoder.decode(param.substring(0, equals), UTF_8) : param.substring(0, equals), (decodeValues) - ? URLDecoder.decode(param.substring(equals + 1), StandardCharsets.UTF_8) + ? 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, StandardCharsets.UTF_8) : param, ""); + params.add((decodeNames) ? URLDecoder.decode(param, UTF_8) : param, ""); } } }