2019-01-24 11:47:03 +01:00
|
|
|
|
package org.gcube.socialnetworking.socialtoken;
|
2019-01-24 11:38:34 +01:00
|
|
|
|
|
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
|
import java.net.URL;
|
2019-01-31 15:34:02 +01:00
|
|
|
|
import java.util.Objects;
|
2019-01-24 11:38:34 +01:00
|
|
|
|
|
|
|
|
|
public class SanitizedURL {
|
|
|
|
|
|
2019-03-14 12:58:03 +01:00
|
|
|
|
private static String CHARACTERS_TO_REMOVE = "[\\.\\,\\;\\)\\:\\\"\\'\\”\\‘]";
|
2019-01-24 11:38:34 +01:00
|
|
|
|
|
|
|
|
|
protected String prefix;
|
|
|
|
|
protected String postfix;
|
|
|
|
|
protected final URL url;
|
|
|
|
|
|
|
|
|
|
public SanitizedURL(String urlString) throws MalformedURLException {
|
2019-01-31 15:34:02 +01:00
|
|
|
|
if(Objects.isNull(urlString) || urlString.isEmpty()) {
|
2019-01-24 12:07:06 +01:00
|
|
|
|
throw new MalformedURLException();
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-14 12:58:03 +01:00
|
|
|
|
prefix = urlString.substring(0,1);;
|
|
|
|
|
if(prefix.matches(CHARACTERS_TO_REMOVE)) {
|
2019-01-24 11:38:34 +01:00
|
|
|
|
prefix = urlString.substring(0, 1);
|
|
|
|
|
urlString = urlString.substring(1);
|
2019-03-14 12:58:03 +01:00
|
|
|
|
}else {
|
|
|
|
|
prefix = "";
|
2019-01-24 11:38:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(urlString.startsWith("www.")) {
|
|
|
|
|
urlString = "http://" + urlString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
postfix = urlString.substring(urlString.length()-1);
|
2019-03-14 12:58:03 +01:00
|
|
|
|
if(postfix.matches(CHARACTERS_TO_REMOVE)) {
|
2019-01-24 11:38:34 +01:00
|
|
|
|
urlString = urlString.substring(0, urlString.length()-1);
|
|
|
|
|
}else {
|
|
|
|
|
postfix = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
url = new URL(urlString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getPrefix() {
|
|
|
|
|
return prefix;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getPostfix() {
|
|
|
|
|
return postfix;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public URL getURL() {
|
|
|
|
|
return url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|