|
10 | 10 |
|
11 | 11 | package org.eclipse.sw360.common.utils; |
12 | 12 |
|
13 | | -import java.io.IOException; |
14 | 13 | import java.net.*; |
15 | 14 | import java.util.*; |
16 | 15 | import java.util.stream.Collectors; |
17 | 16 |
|
18 | 17 | import org.apache.logging.log4j.LogManager; |
19 | 18 | import org.apache.logging.log4j.Logger; |
20 | 19 | import org.eclipse.sw360.datahandler.common.CommonUtils; |
21 | | -import org.eclipse.sw360.datahandler.common.SW360Constants; |
22 | 20 | import org.eclipse.sw360.datahandler.common.SW360Utils; |
23 | 21 |
|
24 | 22 | import static org.eclipse.sw360.datahandler.common.SW360ConfigKeys.VCS_HOSTS; |
25 | | -import static org.eclipse.sw360.datahandler.common.SW360ConfigKeys.VCS_REDIRECTION_LIMIT; |
26 | | -import static org.eclipse.sw360.datahandler.common.SW360ConfigKeys.VCS_REDIRECTION_TIMEOUT_LIMIT; |
27 | 23 |
|
28 | 24 | public class RepositoryURL { |
29 | | - private String url; |
30 | 25 | private static final Logger log = LogManager.getLogger(RepositoryURL.class); |
31 | 26 | private static final String SCHEMA_PATTERN = ".+://(\\w*(?:[\\-@.\\\\s,_:/][/(.\\-)A-Za-z0-9]+)*)"; |
32 | 27 | private static final String VCS_HOSTS_STRING = SW360Utils.readConfig(VCS_HOSTS,""); |
33 | 28 | private static final Map<String, String> KNOWN_VCS_HOSTS = parseVCSHosts(VCS_HOSTS_STRING); |
34 | | - private Set<String> redirectedUrls = new HashSet<>(); |
35 | | - private static int redirectionTimeout = Integer.parseInt(SW360Utils.readConfig(VCS_REDIRECTION_TIMEOUT_LIMIT, SW360Constants.VCS_REDIRECTION_TIMEOUT_LIMIT)); |
36 | | - private static int redirectionLimit = Integer.parseInt(SW360Utils.readConfig(VCS_REDIRECTION_LIMIT, SW360Constants.VCS_REDIRECTION_LIMIT)); |
37 | | - |
38 | | - public RepositoryURL(){} |
39 | | - |
40 | | - public RepositoryURL(String url) { |
41 | | - if (url == null || url.isEmpty()) { |
42 | | - throw new IllegalArgumentException("URL cannot be null or empty"); |
43 | | - } |
44 | | - this.url = processURL(url); |
45 | | - } |
46 | 29 |
|
47 | 30 | public String processURL(String url) { |
48 | | - String sanitized = sanitizeVCS(url); |
49 | | - return handleURLRedirection(sanitized); |
| 31 | + return sanitizeVCS(url); |
50 | 32 | } |
51 | 33 |
|
52 | | - |
53 | 34 | private static String formatVCSUrl(String host, String[] urlParts) { |
54 | 35 | String formatString = KNOWN_VCS_HOSTS.get(host); |
55 | 36 |
|
@@ -111,74 +92,6 @@ public static String getComponentNameFromVCS(String vcsUrl, boolean isGetVendora |
111 | 92 | return isGetVendorandName ? String.join("/", pathParts) : pathParts[pathParts.length - 1]; |
112 | 93 | } |
113 | 94 |
|
114 | | - public String handleURLRedirection(String urlString) { |
115 | | - URL url; |
116 | | - HttpURLConnection connection = null; |
117 | | - try { |
118 | | - url = new URI(urlString).toURL(); |
119 | | - } catch (MalformedURLException | URISyntaxException | IllegalArgumentException e) { |
120 | | - log.error("Invalid URL format: {}", e.getMessage()); |
121 | | - return urlString; |
122 | | - } |
123 | | - |
124 | | - int redirectCount = 0; |
125 | | - |
126 | | - while (redirectCount < redirectionLimit) { |
127 | | - try { |
128 | | - connection = openConnection(url); |
129 | | - int status = connection.getResponseCode(); |
130 | | - |
131 | | - if (status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_MOVED_TEMP || status == 308) { |
132 | | - String newUrl = connection.getHeaderField("Location"); |
133 | | - connection.disconnect(); |
134 | | - |
135 | | - // Resolve relative URLs |
136 | | - url = url.toURI().resolve(newUrl).toURL(); |
137 | | - |
138 | | - if (!"https".equalsIgnoreCase(url.getProtocol())) { |
139 | | - log.error("Insecure redirection to non-HTTPS URL: {}", url); |
140 | | - return urlString; |
141 | | - } |
142 | | - |
143 | | - redirectCount++; |
144 | | - redirectedUrls.add(urlString); |
145 | | - } else { |
146 | | - connection.disconnect(); |
147 | | - break; |
148 | | - } |
149 | | - } catch (IOException | URISyntaxException | IllegalArgumentException e) { |
150 | | - log.error("Error during redirection handling: {}", e.getMessage()); |
151 | | - return urlString; |
152 | | - } |
153 | | - finally { |
154 | | - if (connection != null) { |
155 | | - connection.disconnect(); |
156 | | - } |
157 | | - } |
158 | | - |
159 | | - } |
160 | | - |
161 | | - if (redirectCount == 0 || redirectCount >= redirectionLimit) { |
162 | | - if (redirectCount >= redirectionLimit) { |
163 | | - log.error("Exceeded maximum redirect limit. Returning original URL."); |
164 | | - } |
165 | | - return urlString; |
166 | | - } |
167 | | - return sanitizeVCS(url.toString()); |
168 | | - } |
169 | | - |
170 | | - private static HttpURLConnection openConnection(URL url) throws IOException{ |
171 | | - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
172 | | - connection.setInstanceFollowRedirects(false); |
173 | | - connection.setConnectTimeout(redirectionTimeout); |
174 | | - connection.setReadTimeout(redirectionTimeout); |
175 | | - return connection; |
176 | | - } |
177 | | - |
178 | | - public Set<String> getRedirectedUrls(){ |
179 | | - return redirectedUrls; |
180 | | - } |
181 | | - |
182 | 95 | private static Map<String, String> parseVCSHosts(String propertyValue) { |
183 | 96 | if (propertyValue == null || propertyValue.isEmpty()) { |
184 | 97 | log.error("VCS_HOSTS property is empty"); |
|
0 commit comments