From 03ec89187f95d12a344f400bd66bf89ae9bf6637 Mon Sep 17 00:00:00 2001 From: mcmics Date: Mon, 24 Apr 2023 07:26:33 +0200 Subject: [PATCH] fixes #469 Index out of bounds --- CHANGELOG.md | 1 + .../tools/jenkins/security/DefaultSecurityClient.java | 3 ++- .../codinjutsu/tools/jenkins/util/CollectionUtil.java | 9 +++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38e61d55..c523ab73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ## [[Unreleased]] +- \#469 Index out of bounds ## [0.13.17] - \#440 java.lang.IllegalArgumentException: URL is malformed diff --git a/src/main/java/org/codinjutsu/tools/jenkins/security/DefaultSecurityClient.java b/src/main/java/org/codinjutsu/tools/jenkins/security/DefaultSecurityClient.java index 57f6b417..cb65f720 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/security/DefaultSecurityClient.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/security/DefaultSecurityClient.java @@ -44,6 +44,7 @@ import org.codinjutsu.tools.jenkins.exception.ConfigurationException; import org.codinjutsu.tools.jenkins.logic.JenkinsNotifier; import org.codinjutsu.tools.jenkins.model.RequestData; +import org.codinjutsu.tools.jenkins.util.CollectionUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -185,7 +186,7 @@ protected final HttpHost getLastRedirectionHost(HttpHost host) { var targetHost = host; if (HttpURLConnection.HTTP_OK == statusCode) { final var locations = context.getRedirectLocations(); - if (locations != null) { + if (CollectionUtil.isNotEmpty(locations)) { final var lastHost = locations.get(locations.size() - 1).toURL(); targetHost = new HttpHost(lastHost.getHost(), lastHost.getPort(), lastHost.getProtocol()); } diff --git a/src/main/java/org/codinjutsu/tools/jenkins/util/CollectionUtil.java b/src/main/java/org/codinjutsu/tools/jenkins/util/CollectionUtil.java index 3f3858ed..3bd262dd 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/util/CollectionUtil.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/util/CollectionUtil.java @@ -3,6 +3,7 @@ import lombok.experimental.UtilityClass; import org.codinjutsu.tools.jenkins.model.Job; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.stream.Collectors; @@ -11,6 +12,14 @@ @UtilityClass public class CollectionUtil { + public boolean isEmpty(@Nullable List list) { + return list == null || list.isEmpty(); + } + + public boolean isNotEmpty(@Nullable List list) { + return !isEmpty(list); + } + @NotNull public List flattenedJobs(@NotNull List jobs) { return jobs.stream().flatMap(CollectionUtil::flattenedJobs).collect(Collectors.toList());