From 5853e529c7bba778ac60058a86f84d23a223f570 Mon Sep 17 00:00:00 2001 From: nipuni Date: Tue, 29 Oct 2024 21:46:03 +0530 Subject: [PATCH] Fix throwing NullPointerException when getting a fileName with a query param. --- .../impl/FileDownloaderServiceProvider.java | 53 +++++++++++++------ .../util/FileTransferServiceHelperUtil.java | 10 +++- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/FileDownloaderServiceProvider.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/FileDownloaderServiceProvider.java index f8fe6f2f94..7c8ad99533 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/FileDownloaderServiceProvider.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/FileDownloaderServiceProvider.java @@ -132,9 +132,21 @@ public class FileDownloaderServiceProvider { } /** - * Extracting file name segments by parsing the URL - * @param url Remote URL to extract file name segments - * @return Array containing file name segments or null when failed to extract + * Extracts file name segments (name and extension) by parsing the given URL. + * This method handles two types of URL formats: + * - If the URL includes a query parameter in the format `?fileName=`, the file name + * is extracted from this query parameter (ex: when referencing an existing + * screenshot or icon from the main release) + * - If the URL does not have the `fileName` query parameter, the method attempts to + * extract the file name from the URL path. (ex: this applies to cases where new files are + * uploaded, and only a path-based URL is provided) + * After locating the file name (from either the query parameter or path), the method + * splits the name into segments based on the last dot (`.`), returning the base name and + * extension as a two-element array. If file name cannot be extracted, `null` is returned. + * + * @param url Remote URL to extract file name segments from, which may contain a file name + * as either a query parameter (`fileName=...`) or in the path. + * @return An array containing the file name and extension segments, or null if extraction fails. */ public static String[] extractFileNameSegmentsFromUrl(URL url) { if (url == null) { @@ -143,24 +155,35 @@ public class FileDownloaderServiceProvider { } return null; } - - String []urlSegments = url.toString().split("/"); - if (urlSegments.length < 1) { - if (log.isDebugEnabled()) { - log.debug("Cannot determine the file name for the remote file"); + String fullQualifiedName = null; + String query = url.getQuery(); + if (query != null && query.startsWith("fileName=")) { + String[] queryParts = query.split("=", 2); + if (queryParts.length > 1 && !queryParts[1].isEmpty()) { + fullQualifiedName = queryParts[1]; } - return null; } - - String fullQualifiedName = urlSegments[urlSegments.length - 1]; - String []fileNameSegments = fullQualifiedName.split("\\.(?=[^.]+$)"); - if (fileNameSegments.length != 2) { + if (fullQualifiedName == null) { + String[] urlSegments = url.getPath().split("/"); + if (urlSegments.length > 0) { + fullQualifiedName = urlSegments[urlSegments.length - 1]; + } + } + if (fullQualifiedName != null) { + String[] fileNameSegments = fullQualifiedName.split("\\.(?=[^.]+$)"); + if (fileNameSegments.length == 2) { + return fileNameSegments; + } else { + if (log.isDebugEnabled()) { + log.debug("Error encountered when constructing file name"); + } + } + } else { if (log.isDebugEnabled()) { log.debug("Error encountered when constructing file name"); } - return null; } - return fileNameSegments; + return null; } /** diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/FileTransferServiceHelperUtil.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/FileTransferServiceHelperUtil.java index 856faf4944..0542aba5a6 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/FileTransferServiceHelperUtil.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/FileTransferServiceHelperUtil.java @@ -183,6 +183,15 @@ public class FileTransferServiceHelperUtil { return fileDescriptorResolvedFromRelease; } + String file = urlPathSegments[urlPathSegments.length - 1]; + String query = downloadUrl.getQuery(); + if (query != null && query.startsWith("fileName=")) { + String[] queryParts = query.split("=", 2); + if (queryParts.length > 1 && !queryParts[1].isEmpty()) { + file = queryParts[1]; + } + } + if (urlPathSegments.length < 2) { if (log.isDebugEnabled()) { log.debug("URL patch segments contain less than 2 segments"); @@ -190,7 +199,6 @@ public class FileTransferServiceHelperUtil { return null; } - String file = urlPathSegments[urlPathSegments.length - 1]; String artifactHolder = urlPathSegments[urlPathSegments.length - 2]; try { FileDescriptor fileDescriptor = new FileDescriptor();