001// Generated by delombok at Sat Feb 25 17:58:34 UTC 2023 002package io.cui.tools.net; 003 004import static io.cui.tools.string.MoreStrings.isEmpty; 005import java.net.URI; 006import java.net.URISyntaxException; 007import java.util.Collections; 008import java.util.List; 009import java.util.Optional; 010import io.cui.tools.logging.CuiLogger; 011import io.cui.tools.string.MoreStrings; 012import io.cui.tools.string.Splitter; 013 014/** 015 * Provides some utility methods for url / path related data 016 * 017 * @author Oliver Wolff 018 */ 019public final class UrlHelper { 020 private static final CuiLogger LOGGER = new CuiLogger(UrlHelper.class); 021 022 /** 023 * @param url to be adapted 024 * @return the given url if it already suffixed with '/' or null / empty. The given url suffixed 025 * with '/' otherwise 026 */ 027 public static String addTrailingSlashToUrl(String url) { 028 if (isEmpty(url) || url.endsWith("/")) { 029 return url; 030 } 031 return url + "/"; 032 } 033 034 /** 035 * @param url to be adapted 036 * @return the given url if it is not suffixed with '/' or null / empty. The given url 037 * '/' removed otherwise. Multiple trailing slashes will be removed as well 038 */ 039 public static String removeTrailingSlashesFromUrl(String url) { 040 if (isEmpty(url) || !url.endsWith("/")) { 041 return url; 042 } 043 return removeTrailingSlashesFromUrl(url.substring(0, url.length() - 1)); 044 } 045 046 /** 047 * @param path to be adapted 048 * @return the given path if it already prefixed with '/'. The given path 049 * prefixed with '/' otherwise. In case of being null or empty it will return "/" 050 */ 051 public static String addPrecedingSlashToPath(String path) { 052 if (isEmpty(path)) { 053 return "/"; 054 } 055 if (path.startsWith("/")) { 056 return path; 057 } 058 return "/" + path; 059 } 060 061 /** 062 * @param path 063 * @return the given path if it is not suffixed with '/' or null / empty. The given path 064 * '/' removed otherwise. Multiple preceding slashes will be removed as well 065 */ 066 public static String removePrecedingSlashFromPath(String path) { 067 if (isEmpty(path) || !path.startsWith("/")) { 068 return path; 069 } 070 return removePrecedingSlashFromPath(path.substring(1)); 071 } 072 073 /** 074 * Split the given path String in a unified view with a list of {@link String}. The individual 075 * path-elements are implicitly trimmed 076 * 077 * @param pathString if it is null or empty an empty List will be returned 078 * @return the list view on the path elements. never null but may be empty 079 */ 080 public static List<String> splitPath(String pathString) { 081 if (isEmpty(pathString)) { 082 return Collections.emptyList(); 083 } 084 return Splitter.on("/").trimResults().omitEmptyStrings().splitToList(pathString); 085 } 086 087 /** 088 * @param uri value to be verified if it is a valid URI. 089 * @return URI object, if the given value is a valid URI. 090 */ 091 public static Optional<URI> tryParseUri(final String uri) { 092 if (!MoreStrings.isEmpty(uri)) { 093 try { 094 return Optional.of(new URI(uri)); 095 } catch (URISyntaxException e) { 096 LOGGER.trace(e, "Invalid URI"); 097 } 098 } 099 return Optional.empty(); 100 } 101 102 /** 103 * @param uri value to be verified if it is a valid URI. 104 * @return true, if the given value is a valid URI. False otherwise. 105 */ 106 public static boolean isValidUri(final String uri) { 107 if (!MoreStrings.isEmpty(uri)) { 108 try { 109 new URI(uri); 110 } catch (URISyntaxException e) { 111 return false; 112 } 113 } 114 return true; 115 } 116 117 @java.lang.SuppressWarnings("all") 118 @lombok.Generated 119 private UrlHelper() { 120 throw new java.lang.UnsupportedOperationException("This is a utility class and cannot be instantiated"); 121 } 122}