From a80cf987ae411b2e4c393cafecfbc7e1c1f2d188 Mon Sep 17 00:00:00 2001 From: Kilian Mair Date: Wed, 27 Mar 2024 08:51:09 +0100 Subject: [PATCH 1/6] IPTE-103: Sonar Issues --- .../de/doubleslash/usb_led_matrix/graph/Graph.java | 14 ++++++++++++-- .../usb_led_matrix/view/ConfigurationView.java | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/doubleslash/usb_led_matrix/graph/Graph.java b/src/main/java/de/doubleslash/usb_led_matrix/graph/Graph.java index c28eec0..2e92de9 100644 --- a/src/main/java/de/doubleslash/usb_led_matrix/graph/Graph.java +++ b/src/main/java/de/doubleslash/usb_led_matrix/graph/Graph.java @@ -44,15 +44,24 @@ public class Graph { private HttpClient client = HttpClient.newHttpClient(); public Graph() { + FileInputStream input = null; try { - final InputStream input = new FileInputStream(refreshFile); + input = new FileInputStream(refreshFile); properties.load(input); final String tenantID = properties.getProperty(TENANT_ID_PROPERTY); if (tenantID != null) { tokenURI = String.format("https://login.microsoftonline.com/%s/oauth2/v2.0/token", tenantID); } } catch (IOException e) { - LOG.error("Could not load properties from file '{}'", refreshFile.getAbsolutePath(), e); + LOG.error("Error loading properties from file '{}'", refreshFile.getAbsolutePath(), e); + } finally { + if (input != null) { + try { + input.close(); + } catch (IOException e) { + LOG.error("Error closing FileInputStream", e); + } + } } } @@ -213,6 +222,7 @@ public Color getStatusColorFromTeams() throws IOException, InterruptedException } catch (final IllegalArgumentException e) { LOG.warn("Unknown availability '{}'.", teamsStatus, e); } + assert availabilityStatus != null; return availabilityStatus.getColor(); } } diff --git a/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java b/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java index 82b5267..cdec5a9 100644 --- a/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java +++ b/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java @@ -39,7 +39,7 @@ import java.util.Optional; public class ConfigurationView { - private static final int POLLING_INTERVALL_SECONDS = 10; + private static final long POLLING_INTERVALL_SECONDS = 10; private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); From f8028caa39c3cd632a6ecf82cdd2c14c4921f7eb Mon Sep 17 00:00:00 2001 From: Kilian Mair Date: Wed, 10 Apr 2024 11:05:03 +0200 Subject: [PATCH 2/6] #103: Sonar Issues Refactoring --- .../doubleslash/usb_led_matrix/view/AuthenticationView.java | 1 + .../doubleslash/usb_led_matrix/view/ConfigurationView.java | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java b/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java index 560e7a5..c81201f 100644 --- a/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java +++ b/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java @@ -143,6 +143,7 @@ void setTeamsID(ActionEvent event) { p.setProperty("tenant_id", tenantIDTextField.getText()); p.setProperty("client_id", clientIDTextField.getText()); p.store(fw, "Sample comments"); + fw.close(); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java b/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java index cdec5a9..a2e6244 100644 --- a/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java +++ b/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java @@ -94,6 +94,7 @@ public class ConfigurationView { Thread.sleep(POLLING_INTERVALL_SECONDS * 1000); } catch (final InterruptedException e) { LOG.debug("Manual mode was interrupted.", e); + Thread.currentThread().interrupt(); return; } } @@ -111,6 +112,7 @@ public class ConfigurationView { Thread.sleep(POLLING_INTERVALL_SECONDS * 1000); } catch (final InterruptedException ie) { LOG.debug("MS Teams status polling was interrupted.", ie); + Thread.currentThread().interrupt(); return; } } @@ -124,6 +126,7 @@ public class ConfigurationView { Thread.sleep(POLLING_INTERVALL_SECONDS * 1000); } catch (final InterruptedException e) { LOG.debug("Connection check was interrupted.", e); + Thread.currentThread().interrupt(); return; } turnOffAutomaticallyIfNeeded(LocalTime.now()); @@ -137,6 +140,8 @@ public class ConfigurationView { Thread.sleep(POLLING_INTERVALL_SECONDS * 1000); } catch (final InterruptedException e) { LOG.debug("Reconnection check was interrupted.", e); + Thread.currentThread().interrupt(); + return; } turnOffAutomaticallyIfNeeded(LocalTime.now()); @@ -156,6 +161,7 @@ protected Void call() { Thread.sleep(POLLING_INTERVALL_SECONDS * 500); } catch (IOException | InterruptedException e) { LOG.error("Could not get answer from server.", e); + Thread.currentThread().interrupt(); } } if (answerBody.contains("access_token")) { From f5f6b2d453ac540d709b227dc0be019273e6596e Mon Sep 17 00:00:00 2001 From: Kilian Mair Date: Thu, 18 Apr 2024 08:55:07 +0200 Subject: [PATCH 3/6] IPTE-103: Sonar-Issues refactoring --- src/main/java/de/doubleslash/usb_led_matrix/graph/Graph.java | 1 + .../de/doubleslash/usb_led_matrix/view/AuthenticationView.java | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/main/java/de/doubleslash/usb_led_matrix/graph/Graph.java b/src/main/java/de/doubleslash/usb_led_matrix/graph/Graph.java index 2e92de9..607e700 100644 --- a/src/main/java/de/doubleslash/usb_led_matrix/graph/Graph.java +++ b/src/main/java/de/doubleslash/usb_led_matrix/graph/Graph.java @@ -179,6 +179,7 @@ private void getNewAccessToken() throws IOException, InterruptedException { } extractAndStoreAccessRefreshToken(response.body()); + input.close(); } public boolean isRefreshTokenAvailable() { diff --git a/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java b/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java index c81201f..bd825db 100644 --- a/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java +++ b/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java @@ -91,6 +91,7 @@ public void customInitialize() { clientIDTextField.setText(clientID); setTeamsIDButton.fire(); } + input.close(); } catch (IOException e) { LOG.debug("Could not read file'{}'", fileName, e); } @@ -186,6 +187,7 @@ public void setWebsiteCode() { this.deviceCode.set(deviceCode); } catch (IOException | InterruptedException e) { LOG.error("Could not retrieve device code: ", e); + Thread.currentThread().interrupt(); } } } From 0b966f28bc63190ae30b78ce7fdcd663be4d8af1 Mon Sep 17 00:00:00 2001 From: Kilian Mair Date: Thu, 25 Apr 2024 11:24:00 +0200 Subject: [PATCH 4/6] IPTE-103: Pull request --- .../usb_led_matrix/graph/Graph.java | 26 +++++++------------ .../view/AuthenticationView.java | 9 ++----- .../view/ConfigurationView.java | 4 ++- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/main/java/de/doubleslash/usb_led_matrix/graph/Graph.java b/src/main/java/de/doubleslash/usb_led_matrix/graph/Graph.java index 607e700..7fc3380 100644 --- a/src/main/java/de/doubleslash/usb_led_matrix/graph/Graph.java +++ b/src/main/java/de/doubleslash/usb_led_matrix/graph/Graph.java @@ -12,6 +12,7 @@ import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -44,9 +45,7 @@ public class Graph { private HttpClient client = HttpClient.newHttpClient(); public Graph() { - FileInputStream input = null; - try { - input = new FileInputStream(refreshFile); + try (FileInputStream input = new FileInputStream(refreshFile)) { properties.load(input); final String tenantID = properties.getProperty(TENANT_ID_PROPERTY); if (tenantID != null) { @@ -54,14 +53,6 @@ public Graph() { } } catch (IOException e) { LOG.error("Error loading properties from file '{}'", refreshFile.getAbsolutePath(), e); - } finally { - if (input != null) { - try { - input.close(); - } catch (IOException e) { - LOG.error("Error closing FileInputStream", e); - } - } } } @@ -152,8 +143,9 @@ private HttpRequest.BodyPublisher ofFormData(final Map data) { } private void getNewAccessToken() throws IOException, InterruptedException { - final InputStream input = new FileInputStream(refreshFile); - properties.load(input); + try (InputStream input = new FileInputStream(refreshFile)) { + properties.load(input); + } final Map values = new HashMap<>(); @@ -179,7 +171,6 @@ private void getNewAccessToken() throws IOException, InterruptedException { } extractAndStoreAccessRefreshToken(response.body()); - input.close(); } public boolean isRefreshTokenAvailable() { @@ -223,7 +214,10 @@ public Color getStatusColorFromTeams() throws IOException, InterruptedException } catch (final IllegalArgumentException e) { LOG.warn("Unknown availability '{}'.", teamsStatus, e); } - assert availabilityStatus != null; - return availabilityStatus.getColor(); + if (availabilityStatus != null) { + return availabilityStatus.getColor(); + } else { + return null; + } } } diff --git a/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java b/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java index bd825db..83b895f 100644 --- a/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java +++ b/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java @@ -80,8 +80,7 @@ public void customInitialize() { CommandLineOptions.Dark(scene); } final String fileName = "refreshToken.txt"; - try { - final InputStream input = new FileInputStream(new File(fileName)); + try (InputStream input = new FileInputStream(new File(fileName))) { final Properties properties = new Properties(); properties.load(input); final String tenantID = properties.getProperty("tenant_id"); @@ -91,7 +90,6 @@ public void customInitialize() { clientIDTextField.setText(clientID); setTeamsIDButton.fire(); } - input.close(); } catch (IOException e) { LOG.debug("Could not read file'{}'", fileName, e); } @@ -137,14 +135,11 @@ public void setScene(final Scene scene) { @FXML void setTeamsID(ActionEvent event) { - final FileWriter fw; - try { - fw = new FileWriter("refreshToken.txt"); + try (FileWriter fw = new FileWriter("refreshToken.txt")) { final Properties p = new Properties(); p.setProperty("tenant_id", tenantIDTextField.getText()); p.setProperty("client_id", clientIDTextField.getText()); p.store(fw, "Sample comments"); - fw.close(); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java b/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java index a2e6244..562f962 100644 --- a/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java +++ b/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java @@ -159,7 +159,9 @@ protected Void call() { answerBody = graph.pollingForToken(model.getDeviceCode()); LOG.debug("Polling for token. Answer was: '{}'.", answerBody); Thread.sleep(POLLING_INTERVALL_SECONDS * 500); - } catch (IOException | InterruptedException e) { + } catch (IOException e) { + LOG.error("Could not get answer from server.", e); + } catch (InterruptedException e) { LOG.error("Could not get answer from server.", e); Thread.currentThread().interrupt(); } From b0f6d55c4c961f936b5cf3c78edc3e88cac587f0 Mon Sep 17 00:00:00 2001 From: Kilian Mair Date: Tue, 11 Jun 2024 11:08:52 +0200 Subject: [PATCH 5/6] IPTE-103: Pull Request --- .../doubleslash/usb_led_matrix/view/AuthenticationView.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java b/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java index 83b895f..f61b1e7 100644 --- a/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java +++ b/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java @@ -180,9 +180,13 @@ public void setWebsiteCode() { initializeCopyButton(); this.deviceCode.set(deviceCode); - } catch (IOException | InterruptedException e) { + } catch (IOException e) { LOG.error("Could not retrieve device code: ", e); + ; + } catch (InterruptedException e) { Thread.currentThread().interrupt(); + LOG.error("CurrentThread was interrupt"); + } } } From 0cbdc3355f03a4f9b3326332b8df4cbdce794238 Mon Sep 17 00:00:00 2001 From: Kilian Mair Date: Thu, 27 Jun 2024 14:08:56 +0200 Subject: [PATCH 6/6] IPTE-103: Logging refactoring --- .../doubleslash/usb_led_matrix/view/AuthenticationView.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java b/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java index f61b1e7..910d530 100644 --- a/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java +++ b/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java @@ -182,11 +182,9 @@ public void setWebsiteCode() { this.deviceCode.set(deviceCode); } catch (IOException e) { LOG.error("Could not retrieve device code: ", e); - ; } catch (InterruptedException e) { + LOG.error("CurrentThread was interrupt", e); Thread.currentThread().interrupt(); - LOG.error("CurrentThread was interrupt"); - } } }