From 46666eb169d8f1b02cf7a2f3f5b54bc83c0784ed Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Sat, 30 Jan 2021 12:22:31 +0100 Subject: [PATCH] Fix deprecations (#2163) Signed-off-by: Wouter Born --- .../core/automation/util/ReferenceResolver.java | 2 +- .../java/org/openhab/core/io/net/http/HttpUtil.java | 6 +++--- .../io/net/http/internal/WebClientFactoryImpl.java | 2 +- .../core/ui/internal/proxy/AsyncProxyServlet.java | 2 +- .../core/ui/internal/proxy/BlockingProxyServlet.java | 2 +- .../core/ui/internal/proxy/ProxyServletService.java | 5 ++--- .../ui/internal/proxy/ProxyServletServiceTest.java | 7 +++---- .../org/openhab/core/internal/items/ItemUpdater.java | 11 ++++++----- .../common/osgi/ResourceBundleClassLoaderTest.java | 2 +- .../dispatch/internal/ConfigDispatcherOSGiTest.java | 4 ++-- 10 files changed, 21 insertions(+), 22 deletions(-) diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/util/ReferenceResolver.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/util/ReferenceResolver.java index bb57d5cd0..323f22e93 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/util/ReferenceResolver.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/util/ReferenceResolver.java @@ -367,7 +367,7 @@ public class ReferenceResolver { throws NoSuchFieldException, SecurityException { try { Field f = objClass.getDeclaredField(fieldName); - if (!f.isAccessible()) { + if (!f.canAccess(bean)) { f.setAccessible(true); } return f.get(bean); diff --git a/bundles/org.openhab.core.io.net/src/main/java/org/openhab/core/io/net/http/HttpUtil.java b/bundles/org.openhab.core.io.net/src/main/java/org/openhab/core/io/net/http/HttpUtil.java index a0283f0ae..28383f6a4 100644 --- a/bundles/org.openhab.core.io.net/src/main/java/org/openhab/core/io/net/http/HttpUtil.java +++ b/bundles/org.openhab.core.io.net/src/main/java/org/openhab/core/io/net/http/HttpUtil.java @@ -24,6 +24,7 @@ import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; import java.nio.charset.StandardCharsets; +import java.util.Base64; import java.util.List; import java.util.Optional; import java.util.Properties; @@ -42,8 +43,6 @@ import org.eclipse.jetty.client.util.InputStreamContentProvider; import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpMethod; import org.eclipse.jetty.http.HttpStatus; -import org.eclipse.jetty.util.B64Code; -import org.eclipse.jetty.util.StringUtil; import org.openhab.core.library.types.RawType; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; @@ -227,7 +226,8 @@ public class HttpUtil { String user = userInfo[0]; String password = userInfo[1]; - String basicAuthentication = "Basic " + B64Code.encode(user + ":" + password, StringUtil.__ISO_8859_1); + String basicAuthentication = "Basic " + + Base64.getEncoder().encodeToString((user + ":" + password).getBytes()); request.header(HttpHeader.AUTHORIZATION, basicAuthentication); } } catch (URISyntaxException e) { diff --git a/bundles/org.openhab.core.io.net/src/main/java/org/openhab/core/io/net/http/internal/WebClientFactoryImpl.java b/bundles/org.openhab.core.io.net/src/main/java/org/openhab/core/io/net/http/internal/WebClientFactoryImpl.java index a146c2b0d..9cd6f16b1 100644 --- a/bundles/org.openhab.core.io.net/src/main/java/org/openhab/core/io/net/http/internal/WebClientFactoryImpl.java +++ b/bundles/org.openhab.core.io.net/src/main/java/org/openhab/core/io/net/http/internal/WebClientFactoryImpl.java @@ -334,7 +334,7 @@ public class WebClientFactoryImpl implements HttpClientFactory, WebSocketFactory } private SslContextFactory createSslContextFactory() { - SslContextFactory sslContextFactory = new SslContextFactory(); + SslContextFactory sslContextFactory = new SslContextFactory.Client(); sslContextFactory.setEndpointIdentificationAlgorithm("HTTPS"); try { diff --git a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/proxy/AsyncProxyServlet.java b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/proxy/AsyncProxyServlet.java index d2f0acae4..88ada6168 100644 --- a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/proxy/AsyncProxyServlet.java +++ b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/proxy/AsyncProxyServlet.java @@ -49,7 +49,7 @@ public class AsyncProxyServlet extends org.eclipse.jetty.proxy.AsyncProxyServlet */ @Override protected HttpClient newHttpClient() { - return new HttpClient(new SslContextFactory()); + return new HttpClient(new SslContextFactory.Client()); } @Override diff --git a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/proxy/BlockingProxyServlet.java b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/proxy/BlockingProxyServlet.java index 471a30793..77cc3a436 100644 --- a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/proxy/BlockingProxyServlet.java +++ b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/proxy/BlockingProxyServlet.java @@ -49,7 +49,7 @@ public class BlockingProxyServlet extends HttpServlet { private final ProxyServletService service; - private static HttpClient httpClient = new HttpClient(new SslContextFactory()); + private static HttpClient httpClient = new HttpClient(new SslContextFactory.Client()); /** Timeout for HTTP requests in ms */ private static final int TIMEOUT = 15000; diff --git a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/proxy/ProxyServletService.java b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/proxy/ProxyServletService.java index 59402b6f9..ddfec504c 100644 --- a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/proxy/ProxyServletService.java +++ b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/proxy/ProxyServletService.java @@ -17,6 +17,7 @@ import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import java.util.Base64; import java.util.Hashtable; import java.util.Map; @@ -29,8 +30,6 @@ import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.http.HttpHeader; -import org.eclipse.jetty.util.B64Code; -import org.eclipse.jetty.util.StringUtil; import org.openhab.core.library.types.StringType; import org.openhab.core.model.core.ModelRepository; import org.openhab.core.model.sitemap.sitemap.Image; @@ -317,7 +316,7 @@ public class ProxyServletService extends HttpServlet { String password = userInfo.length >= 2 ? userInfo[1] : null; String authString = password != null ? user + ":" + password : user + ":"; - String basicAuthentication = "Basic " + B64Code.encode(authString, StringUtil.__ISO_8859_1); + String basicAuthentication = "Basic " + Base64.getEncoder().encodeToString(authString.getBytes()); request.header(HttpHeader.AUTHORIZATION, basicAuthentication); } } diff --git a/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/proxy/ProxyServletServiceTest.java b/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/proxy/ProxyServletServiceTest.java index 35a2d99b1..6a552b957 100644 --- a/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/proxy/ProxyServletServiceTest.java +++ b/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/proxy/ProxyServletServiceTest.java @@ -18,13 +18,12 @@ import static org.mockito.Mockito.*; import java.net.URI; import java.net.URISyntaxException; +import java.util.Base64; import javax.servlet.http.HttpServletRequest; import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.http.HttpHeader; -import org.eclipse.jetty.util.B64Code; -import org.eclipse.jetty.util.StringUtil; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openhab.core.library.types.OnOffType; @@ -111,7 +110,7 @@ public class ProxyServletServiceTest { URI uri = new URI("http://testuser:testpassword@127.0.0.1:8080/content"); service.maybeAppendAuthHeader(uri, request); verify(request).header(HttpHeader.AUTHORIZATION, - "Basic " + B64Code.encode("testuser:testpassword", StringUtil.__ISO_8859_1)); + "Basic " + Base64.getEncoder().encodeToString("testuser:testpassword".getBytes())); } @Test @@ -120,7 +119,7 @@ public class ProxyServletServiceTest { URI uri = new URI("http://testuser@127.0.0.1:8080/content"); service.maybeAppendAuthHeader(uri, request); verify(request).header(HttpHeader.AUTHORIZATION, - "Basic " + B64Code.encode("testuser:", StringUtil.__ISO_8859_1)); + "Basic " + Base64.getEncoder().encodeToString("testuser:".getBytes())); } @Test diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/items/ItemUpdater.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/items/ItemUpdater.java index 16a5dc902..4aef6536e 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/items/ItemUpdater.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/items/ItemUpdater.java @@ -60,14 +60,15 @@ public class ItemUpdater extends AbstractItemEventSubscriber { // Look for class hierarchy for (Class state : item.getAcceptedDataTypes()) { try { - if (!state.isEnum() && state.newInstance().getClass().isAssignableFrom(newState.getClass())) { + if (!state.isEnum() && state.getDeclaredConstructor().newInstance().getClass() + .isAssignableFrom(newState.getClass())) { isAccepted = true; break; } - } catch (InstantiationException e) { - logger.warn("InstantiationException on {}", e.getMessage()); // Should never happen - } catch (IllegalAccessException e) { - logger.warn("IllegalAccessException on {}", e.getMessage()); // Should never happen + } catch (ReflectiveOperationException e) { + // Should never happen + logger.warn("{} while creating {} instance: {}", e.getClass().getSimpleName(), + state.getClass().getSimpleName(), e.getMessage()); } } } diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/common/osgi/ResourceBundleClassLoaderTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/common/osgi/ResourceBundleClassLoaderTest.java index eed70a58f..17da0c353 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/common/osgi/ResourceBundleClassLoaderTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/common/osgi/ResourceBundleClassLoaderTest.java @@ -40,7 +40,7 @@ public class ResourceBundleClassLoaderTest { File file = new File(root.toFile(), relativeFile); file.createNewFile(); - return file.toURL(); + return file.toURI().toURL(); } @Test diff --git a/itests/org.openhab.core.config.dispatch.tests/src/main/java/org/openhab/core/config/dispatch/internal/ConfigDispatcherOSGiTest.java b/itests/org.openhab.core.config.dispatch.tests/src/main/java/org/openhab/core/config/dispatch/internal/ConfigDispatcherOSGiTest.java index 259e444d0..5d167e188 100644 --- a/itests/org.openhab.core.config.dispatch.tests/src/main/java/org/openhab/core/config/dispatch/internal/ConfigDispatcherOSGiTest.java +++ b/itests/org.openhab.core.config.dispatch.tests/src/main/java/org/openhab/core/config/dispatch/internal/ConfigDispatcherOSGiTest.java @@ -35,7 +35,7 @@ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.MethodOrderer.Alphanumeric; +import org.junit.jupiter.api.MethodOrderer.MethodName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.io.TempDir; @@ -48,7 +48,7 @@ import org.osgi.service.cm.ConfigurationAdmin; /** * @author Petar Valchev - Initial contribution */ -@TestMethodOrder(Alphanumeric.class) +@TestMethodOrder(MethodName.class) public class ConfigDispatcherOSGiTest extends JavaOSGiTest { public @TempDir File tmpBaseFolder;