From c676e153fc2efa2b6b84d4bb39fdb26dc4fb6d99 Mon Sep 17 00:00:00 2001 From: Quentin Guillemin Date: Thu, 26 Sep 2024 17:59:36 +0200 Subject: [PATCH] feat: add ListUtil --- .../collabsoft/configuration/Constants.java | 7 ++-- .../configuration/bean/CorsProperties.java | 24 ++++-------- .../collabsoft/service/db/FileService.java | 2 +- .../service/db/MetadataService.java | 2 +- .../fr/recia/collabsoft/util/ListUtil.java | 38 +++++++++++++++++++ 5 files changed, 52 insertions(+), 21 deletions(-) create mode 100644 src/main/java/fr/recia/collabsoft/util/ListUtil.java diff --git a/src/main/java/fr/recia/collabsoft/configuration/Constants.java b/src/main/java/fr/recia/collabsoft/configuration/Constants.java index 25934344..ced309d1 100644 --- a/src/main/java/fr/recia/collabsoft/configuration/Constants.java +++ b/src/main/java/fr/recia/collabsoft/configuration/Constants.java @@ -18,9 +18,10 @@ public class Constants { - public static final String PROPERTIES_TO_JSON_DELIMITER = "\", \""; - public static final String PROPERTIES_TO_JSON_PREFIX = "[ \""; - public static final String PROPERTIES_TO_JSON_SUFFIX = "\" ]"; + public static final String JSON_ARRAY_DELIMITER = "\", \""; + public static final String JSON_ARRAY_PREFIX = "[ \""; + public static final String JSON_ARRAY_SUFFIX = "\" ]"; + public static final String SPRING_PROFILE_DEVELOPMENT = "dev"; public static final String SPRING_PROFILE_PRODUCTION = "prod"; public static final String SPRING_PROFILE_TEST = "test"; diff --git a/src/main/java/fr/recia/collabsoft/configuration/bean/CorsProperties.java b/src/main/java/fr/recia/collabsoft/configuration/bean/CorsProperties.java index ab058991..cb2620bd 100644 --- a/src/main/java/fr/recia/collabsoft/configuration/bean/CorsProperties.java +++ b/src/main/java/fr/recia/collabsoft/configuration/bean/CorsProperties.java @@ -16,14 +16,14 @@ package fr.recia.collabsoft.configuration.bean; +import fr.recia.collabsoft.util.ListUtil; import lombok.Data; import java.util.List; -import java.util.stream.Collectors; -import static fr.recia.collabsoft.configuration.Constants.PROPERTIES_TO_JSON_DELIMITER; -import static fr.recia.collabsoft.configuration.Constants.PROPERTIES_TO_JSON_PREFIX; -import static fr.recia.collabsoft.configuration.Constants.PROPERTIES_TO_JSON_SUFFIX; +import static fr.recia.collabsoft.configuration.Constants.JSON_ARRAY_DELIMITER; +import static fr.recia.collabsoft.configuration.Constants.JSON_ARRAY_PREFIX; +import static fr.recia.collabsoft.configuration.Constants.JSON_ARRAY_SUFFIX; @Data public class CorsProperties { @@ -40,18 +40,10 @@ public String toString() { return "\"CorsProperties\": {" + "\n\t\"enable\": " + enable + ",\n\t\"allowCredentials\": " + allowCredentials + - ",\n\t\"allowedOrigins\": " + allowedOrigins.stream() - .map(String::valueOf) - .collect(Collectors.joining(PROPERTIES_TO_JSON_DELIMITER, PROPERTIES_TO_JSON_PREFIX, PROPERTIES_TO_JSON_SUFFIX)) + - ",\n\t\"exposedHeaders\": " + exposedHeaders.stream() - .map(String::valueOf) - .collect(Collectors.joining(PROPERTIES_TO_JSON_DELIMITER, PROPERTIES_TO_JSON_PREFIX, PROPERTIES_TO_JSON_SUFFIX)) + - ",\n\t\"allowedHeaders\": " + allowedHeaders.stream() - .map(String::valueOf) - .collect(Collectors.joining(PROPERTIES_TO_JSON_DELIMITER, PROPERTIES_TO_JSON_PREFIX, PROPERTIES_TO_JSON_SUFFIX)) + - ",\n\t\"allowedMethods\": " + allowedMethods.stream() - .map(String::valueOf) - .collect(Collectors.joining(PROPERTIES_TO_JSON_DELIMITER, PROPERTIES_TO_JSON_PREFIX, PROPERTIES_TO_JSON_SUFFIX)) + + ",\n\t\"allowedOrigins\": " + ListUtil.toStringList(allowedOrigins, JSON_ARRAY_DELIMITER, JSON_ARRAY_PREFIX, JSON_ARRAY_SUFFIX) + + ",\n\t\"exposedHeaders\": " + ListUtil.toStringList(exposedHeaders, JSON_ARRAY_DELIMITER, JSON_ARRAY_PREFIX, JSON_ARRAY_SUFFIX) + + ",\n\t\"allowedHeaders\": " + ListUtil.toStringList(allowedHeaders, JSON_ARRAY_DELIMITER, JSON_ARRAY_PREFIX, JSON_ARRAY_SUFFIX) + + ",\n\t\"allowedMethods\": " + ListUtil.toStringList(allowedMethods, JSON_ARRAY_DELIMITER, JSON_ARRAY_PREFIX, JSON_ARRAY_SUFFIX) + "\n}"; } diff --git a/src/main/java/fr/recia/collabsoft/service/db/FileService.java b/src/main/java/fr/recia/collabsoft/service/db/FileService.java index 40c3d8c3..5f3ffe03 100644 --- a/src/main/java/fr/recia/collabsoft/service/db/FileService.java +++ b/src/main/java/fr/recia/collabsoft/service/db/FileService.java @@ -27,9 +27,9 @@ import fr.recia.collabsoft.db.entity.User; import fr.recia.collabsoft.db.repository.AssociatedAppRepository; import fr.recia.collabsoft.db.repository.FileRepository; -import fr.recia.collabsoft.web.interceptor.bean.SoffitHolder; import fr.recia.collabsoft.model.enums.Authority; import fr.recia.collabsoft.model.pojo.JsonFileBody; +import fr.recia.collabsoft.web.interceptor.bean.SoffitHolder; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.IteratorUtils; import org.springframework.beans.factory.annotation.Autowired; diff --git a/src/main/java/fr/recia/collabsoft/service/db/MetadataService.java b/src/main/java/fr/recia/collabsoft/service/db/MetadataService.java index 9d7f66e0..d5a0792a 100644 --- a/src/main/java/fr/recia/collabsoft/service/db/MetadataService.java +++ b/src/main/java/fr/recia/collabsoft/service/db/MetadataService.java @@ -21,9 +21,9 @@ import fr.recia.collabsoft.db.entity.QMetadata; import fr.recia.collabsoft.db.entity.User; import fr.recia.collabsoft.db.repository.MetadataRepository; -import fr.recia.collabsoft.web.interceptor.bean.SoffitHolder; import fr.recia.collabsoft.model.enums.Authority; import fr.recia.collabsoft.model.pojo.JsonMetadataBody; +import fr.recia.collabsoft.web.interceptor.bean.SoffitHolder; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; diff --git a/src/main/java/fr/recia/collabsoft/util/ListUtil.java b/src/main/java/fr/recia/collabsoft/util/ListUtil.java new file mode 100644 index 00000000..12693922 --- /dev/null +++ b/src/main/java/fr/recia/collabsoft/util/ListUtil.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2023 GIP-RECIA, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package fr.recia.collabsoft.util; + +import java.util.List; +import java.util.stream.Collectors; + +public class ListUtil { + + public static String toStringList(List list) { + return toStringList(list, ", "); + } + + public static String toStringList(List list, String delimiter) { + return toStringList(list, delimiter, "[ ", " ]"); + } + + public static String toStringList(List list, String delimiter, String prefix, String suffix) { + return list.stream() + .map(String::valueOf) + .collect(Collectors.joining(delimiter, prefix, suffix)); + } + +}