forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 3
/
URLUtils.java
158 lines (136 loc) · 4.87 KB
/
URLUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* Copyright (C) 2015 Red Hat, 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 io.fabric8.kubernetes.client.utils;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public class URLUtils {
public static String join(String... parts) {
StringBuilder sb = new StringBuilder();
String urlQueryParams = "";
if (parts.length > 0) {
String urlWithoutQuery = parts[0];
try {
URI uri = new URI(parts[0]);
if (containsQueryParam(uri)) {
urlQueryParams = uri.getQuery();
urlWithoutQuery = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), null, uri.getFragment())
.toString();
}
} catch (URISyntaxException e) {
// Not all first parameters are URL
}
sb.append(urlWithoutQuery).append("/");
}
StringBuilder queryParams = new StringBuilder();
for (int i = 1; i < parts.length; i++) {
try {
URI partUri = new URI(parts[i]);
if (containsQueryParam(partUri)) {
queryParams = getQueryParams(partUri, parts, i+1);
// If we start detecting query params then everything will be query params part
break;
}
sb.append(parts[i]);
} catch (URISyntaxException e) {
sb.append(parts[i]);
}
if (i < parts.length - 1) {
sb.append("/");
}
}
appendQueryParametersFromOriginalUrl(sb, urlQueryParams, queryParams);
String joined = sb.toString();
// And normalize it...
return joined
.replaceAll("/+", "/")
.replaceAll("/\\?", "?")
.replaceAll("/#", "#")
.replaceAll(":/", "://");
}
private static void appendQueryParametersFromOriginalUrl(StringBuilder sb, String urlQueryParams, StringBuilder queryParams) {
if (!urlQueryParams.isEmpty()) {
if (queryParams.length() == 0) {
queryParams.append("?");
} else {
queryParams.append("&");
}
queryParams.append(urlQueryParams);
}
sb.append(queryParams);
}
private static StringBuilder getQueryParams(URI firstPart, String[] parts, int index) {
StringBuilder queryParams = new StringBuilder();
queryParams.append(firstPart.toString());
for (int i = index; i < parts.length; i++) {
String param = parts[i];
if (!param.startsWith("&")) {
queryParams.append("&");
}
queryParams.append((param));
}
return queryParams;
}
private static boolean containsQueryParam(URI uri) {
return uri.getQuery() != null;
}
/**
* Joins all the given strings, ignoring nulls so that they form a URL with / between the paths without a // if the previous path ends with / and the next path starts with / unless a path item is blank
*
* @param strings A list of strings which you need to concatenate.
* @return the strings concatenated together with / while avoiding a double // between non blank strings.
*/
public static String pathJoin(String... strings) {
StringBuilder buffer = new StringBuilder();
for (String string : strings) {
if (string == null) {
continue;
}
if (buffer.length() > 0) {
boolean bufferEndsWithSeparator = buffer.toString().endsWith("/");
boolean stringStartsWithSeparator = string.startsWith("/");
if (bufferEndsWithSeparator) {
if (stringStartsWithSeparator) {
string = string.substring(1);
}
} else {
if (!stringStartsWithSeparator) {
buffer.append("/");
}
}
}
buffer.append(string);
}
return buffer.toString();
}
public static boolean isValidURL(String url) {
if (url != null) {
try {
URI u = new URI(url);
} catch (URISyntaxException exception) {
return false;
}
return url.contains("null") ? false : true;
} else {
return false;
}
}
public static String encodeToUTF(String url) throws UnsupportedEncodingException {
return URLEncoder.encode(url, StandardCharsets.UTF_8.displayName());
}
}