Skip to content

Commit faddfd6

Browse files
committed
Added URLUtils and URLBuilder
1 parent c1dd91d commit faddfd6

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package org.javawebstack.framework.util;
2+
3+
import org.javawebstack.abstractdata.util.QueryString;
4+
5+
import java.io.UnsupportedEncodingException;
6+
import java.net.URLDecoder;
7+
import java.net.URLEncoder;
8+
import java.nio.charset.StandardCharsets;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
public class URLUtils {
13+
public String urlEncode(String content){
14+
try {
15+
return URLEncoder.encode(content, "UTF-8");
16+
} catch (UnsupportedEncodingException e) {
17+
e.printStackTrace();
18+
}
19+
return null;
20+
}
21+
22+
public String urlDecode(String content){
23+
try {
24+
return URLDecoder.decode(content, "UTF-8");
25+
} catch (UnsupportedEncodingException e) {
26+
e.printStackTrace();
27+
}
28+
return null;
29+
}
30+
31+
public static class URLBuilder {
32+
private String protocol = "http";
33+
private String domain = "localhost";
34+
private String path = "";
35+
private QueryString queryParameters = new QueryString();
36+
37+
public URLBuilder(){
38+
39+
}
40+
41+
public String build(){
42+
String url = protocol+"://"+domain;
43+
44+
url += getFullPath();
45+
46+
return url;
47+
}
48+
49+
public URLBuilder setDomain(String domain) {
50+
this.domain = domain;
51+
return this;
52+
}
53+
54+
public URLBuilder setProtocol(String protocol) {
55+
this.protocol = protocol;
56+
return this;
57+
}
58+
59+
public String getDomain() {
60+
return domain;
61+
}
62+
63+
public String getProtocol() {
64+
return protocol;
65+
}
66+
67+
public String getPath() {
68+
return path;
69+
}
70+
71+
public String getFullPath() {
72+
String fullPath = "";
73+
if (!path.equals(""))
74+
fullPath += (path.startsWith("/") ? "" : "/") + path;
75+
76+
if (queryParameters.size() > 0)
77+
fullPath += (fullPath.contains("/") ? "" : "/") + "?"+queryParameters.toString();
78+
79+
return fullPath;
80+
}
81+
82+
public QueryString getQueryParameters() {
83+
return queryParameters;
84+
}
85+
86+
public String getQueryParameter(String key) {
87+
return queryParameters.get(key);
88+
}
89+
90+
public URLBuilder setPath(String path) {
91+
this.path = path;
92+
return this;
93+
}
94+
95+
public URLBuilder setQueryParameters(QueryString queryParameters) {
96+
this.queryParameters = queryParameters;
97+
return this;
98+
}
99+
100+
public URLBuilder setQueryParameter(String key, String value){
101+
queryParameters.set(key, value);
102+
return this;
103+
}
104+
105+
public String toString() {
106+
return build();
107+
}
108+
109+
public static URLBuilder from(String url){
110+
URLBuilder urlBuilder = new URLBuilder();
111+
String[] protocolAndUrl = url.split("://", 2);
112+
urlBuilder.protocol = protocolAndUrl[0];
113+
if (protocolAndUrl.length > 1) {
114+
String[] domainAndPath = protocolAndUrl[1].split("/", 2);
115+
urlBuilder.domain = domainAndPath[0];
116+
if (domainAndPath.length > 1) {
117+
String[] pathAndQueryParameters = domainAndPath[1].split("\\?", 2);
118+
urlBuilder.path = pathAndQueryParameters[0];
119+
if (pathAndQueryParameters.length > 1) {
120+
String[] queryParameters = pathAndQueryParameters[1].split("&");
121+
for (String queryParameter : queryParameters) {
122+
String[] keyValue = queryParameter.split("=", 2);
123+
urlBuilder.queryParameters.set(keyValue[0], keyValue.length > 1 ? keyValue[1] : "");
124+
}
125+
}
126+
}
127+
}
128+
return urlBuilder;
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)