|
@ -0,0 +1,59 @@ |
|
|
|
|
|
package com.jesuspinar.webserver; |
|
|
|
|
|
|
|
|
|
|
|
public class UrlParser { |
|
|
|
|
|
|
|
|
|
|
|
private String url; |
|
|
|
|
|
private StringBuilder sb ; |
|
|
|
|
|
|
|
|
|
|
|
public UrlParser(String url) { |
|
|
|
|
|
sb = new StringBuilder(); |
|
|
|
|
|
this.url = url; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String getUrl(){ |
|
|
|
|
|
return url; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private String splitString(String charStart, String charEnd , boolean reverse){ |
|
|
|
|
|
sb.setLength(0); |
|
|
|
|
|
sb.append(url); |
|
|
|
|
|
int start; |
|
|
|
|
|
int end; |
|
|
|
|
|
|
|
|
|
|
|
start = url.indexOf(charStart); |
|
|
|
|
|
end = url.indexOf(charEnd); |
|
|
|
|
|
|
|
|
|
|
|
if (reverse){ |
|
|
|
|
|
start = url.lastIndexOf(charStart)+1; |
|
|
|
|
|
end = url.lastIndexOf(charEnd); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return sb.substring(start, end); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String getProtocol(){ |
|
|
|
|
|
return splitString("h",":", false); |
|
|
|
|
|
} |
|
|
|
|
|
public String getDomain(){ |
|
|
|
|
|
int i = url.indexOf("/")+2; |
|
|
|
|
|
return splitString(String.valueOf(url.charAt(i)),".", false); |
|
|
|
|
|
} |
|
|
|
|
|
public String getDomainName(){ |
|
|
|
|
|
sb.setLength(0); |
|
|
|
|
|
sb.append(url); |
|
|
|
|
|
int i = url.indexOf(".")+1; |
|
|
|
|
|
sb.delete(0,i); |
|
|
|
|
|
i = sb.toString().indexOf("."); |
|
|
|
|
|
sb.delete(i,sb.length()); |
|
|
|
|
|
return sb.toString(); |
|
|
|
|
|
} |
|
|
|
|
|
public String getTld(){ |
|
|
|
|
|
return splitString(".","/" , true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String getPort(){ |
|
|
|
|
|
return splitString("/","" , true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |