Avoid UnsupportedEncodingException & use const from StandardCharsets (#11948)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp
2022-01-03 16:05:08 +01:00
committed by GitHub
parent 3f54327d5a
commit 167f8ebc49
52 changed files with 180 additions and 414 deletions

View File

@@ -12,8 +12,8 @@
*/
package org.openhab.io.imperihome.internal.handler;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import javax.servlet.http.HttpServletRequest;
@@ -30,8 +30,6 @@ import org.slf4j.LoggerFactory;
*/
public class DeviceActionHandler {
private static final String CHARSET = "UTF-8";
private final Logger logger = LoggerFactory.getLogger(DeviceActionHandler.class);
private final DeviceRegistry deviceRegistry;
@@ -42,17 +40,13 @@ public class DeviceActionHandler {
public void handle(HttpServletRequest req, Matcher urlMatcher) {
String deviceId, action, value;
try {
deviceId = URLDecoder.decode(urlMatcher.group(1), CHARSET);
action = URLDecoder.decode(urlMatcher.group(2), CHARSET);
deviceId = URLDecoder.decode(urlMatcher.group(1), StandardCharsets.UTF_8);
action = URLDecoder.decode(urlMatcher.group(2), StandardCharsets.UTF_8);
if (urlMatcher.group(3) == null) {
value = null;
} else {
value = URLDecoder.decode(urlMatcher.group(3), CHARSET);
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Could not decode request params", e);
if (urlMatcher.group(3) == null) {
value = null;
} else {
value = URLDecoder.decode(urlMatcher.group(3), StandardCharsets.UTF_8);
}
logger.debug("Action request for device {}: [{}] [{}]", deviceId, action, value);

View File

@@ -12,8 +12,8 @@
*/
package org.openhab.io.imperihome.internal.handler;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
@@ -45,8 +45,6 @@ import org.slf4j.LoggerFactory;
*/
public class DeviceHistoryHandler {
private static final String CHARSET = "UTF-8";
private final Logger logger = LoggerFactory.getLogger(DeviceHistoryHandler.class);
private final DeviceRegistry deviceRegistry;
@@ -61,11 +59,11 @@ public class DeviceHistoryHandler {
String deviceId, field;
long start, end;
try {
deviceId = URLDecoder.decode(urlMatcher.group(1), CHARSET);
field = URLDecoder.decode(urlMatcher.group(2), CHARSET);
deviceId = URLDecoder.decode(urlMatcher.group(1), StandardCharsets.UTF_8);
field = URLDecoder.decode(urlMatcher.group(2), StandardCharsets.UTF_8);
start = Long.parseLong(urlMatcher.group(3));
end = Long.parseLong(urlMatcher.group(4));
} catch (UnsupportedEncodingException | NumberFormatException e) {
} catch (NumberFormatException e) {
throw new RuntimeException("Could not decode request params", e);
}