-
Notifications
You must be signed in to change notification settings - Fork 161
/
UrlUtil.java
105 lines (97 loc) · 3.63 KB
/
UrlUtil.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
/*
* Copyright 2000-2021 Vaadin Ltd.
*
* 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 com.vaadin.flow.internal;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
/**
* Internal utility class for URL handling.
* <p>
* For internal use only. May be renamed or removed in a future release.
*
* @author Vaadin Ltd
* @since 2.0
*/
public class UrlUtil {
private UrlUtil() {
}
/**
* checks if the given url is an external URL (e.g. staring with http:// or
* https://) or not.
*
* @param url
* is the url to be checked.
* @return true if the url is external otherwise false.
*/
public static boolean isExternal(String url) {
if (url.startsWith("//")) {
return true;
}
return url.contains("://");
}
/**
* Encodes a full URI.
* <p>
* Corresponds to encodeURI in JavaScript
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI
* <p>
* The path can contain {@code /} and other special URL characters as these
* will not be encoded. See {@link #encodeURIComponent(String)} if you want
* to encode all special characters.
* <p>
* The following characters are not escaped:
* {@literal A-Za-z0-9;,/?:@&=+$-_.!~*'()#}
*
* @param uri
* the uri to encode
*/
public static String encodeURI(String uri) {
try {
return URLEncoder.encode(uri, StandardCharsets.UTF_8.name())
.replace("+", "%20").replace("%2F", "/").replace("%40", "@")
.replace("%3B", ";").replace("%2C", ",").replace("%3F", "?")
.replace("%3A", ":").replace("%26", "&").replace("%3D", "=")
.replace("%2B", "+").replace("%24", "$").replace("%21", "!")
.replace("%7E", "~").replace("%27", "'").replace("%28", "(")
.replace("%29", ")").replace("%23", "#");
} catch (UnsupportedEncodingException e) {
// Runtime exception as this doesn't really happen
throw new RuntimeException("Encoding the URI failed", e); // NOSONAR
}
}
/**
* Encodes a path segment of a URI.
* <p>
* Corresponds to encodeURIComponent in JavaScript
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
* <p>
* The following characters are not escaped: {@literal A-Za-z0-9-_.!~*'()}
*
* @param path
* the path to encode
*/
public static String encodeURIComponent(String path) {
try {
return URLEncoder.encode(path, StandardCharsets.UTF_8.name())
.replace("+", "%20").replace("%21", "!").replace("%7E", "~")
.replace("%27", "'").replace("%28", "(")
.replace("%29", ")");
} catch (UnsupportedEncodingException e) {
// Runtime exception as this doesn't really happen
throw new RuntimeException("Encoding the URI failed", e); // NOSONAR
}
}
}