diff --git a/src/main/java/org/gcube/socialnetworking/socialtoken/SanitizedURL.java b/src/main/java/org/gcube/socialnetworking/socialtoken/SanitizedURL.java index b9a35b6..358f81d 100644 --- a/src/main/java/org/gcube/socialnetworking/socialtoken/SanitizedURL.java +++ b/src/main/java/org/gcube/socialnetworking/socialtoken/SanitizedURL.java @@ -10,7 +10,7 @@ import java.util.Objects; */ public class SanitizedURL { - private static String CHARACTERS_TO_REMOVE = "[\\.\\,\\;\\(\\)\\:\\\"\\'\\“\\”\\‘\\’\\«\\»]"; + private static String CHARACTERS_TO_REMOVE = "[\\{\\}\\[\\]\\.\\,\\;\\(\\)\\:\\\"\\'\\“\\”\\‘\\’\\«\\»]"; protected String prefix; protected String postfix; @@ -21,13 +21,10 @@ public class SanitizedURL { throw new MalformedURLException(); } - prefix = urlString.substring(0,1); - if(prefix.matches(CHARACTERS_TO_REMOVE)) { - prefix = urlString.substring(0, 1); - urlString = urlString.substring(1); - }else { - prefix = ""; - } + prefix = ""; + postfix = ""; + + urlString = stripPrefix(urlString); if(urlString.startsWith("www.")) { urlString = "http://" + urlString; @@ -36,12 +33,33 @@ public class SanitizedURL { postfix = ""; urlString = stripPostfix(urlString); + + url = new URL(urlString); } - private String stripPostfix(String urlString) { - String testPostFix = urlString.substring(urlString.length()-1); - if(testPostFix.matches(CHARACTERS_TO_REMOVE)) { + private String stripPrefix(String urlString) throws MalformedURLException { + if(Objects.isNull(urlString) || urlString.isEmpty() || urlString.length()<2) { + throw new MalformedURLException(); + } + + String testPrefix = urlString.substring(0,1); + if(testPrefix.matches(CHARACTERS_TO_REMOVE)) { + prefix = prefix + urlString.substring(0, 1); + urlString = urlString.substring(1); + urlString = stripPrefix(urlString); + } + + return urlString; + } + + private String stripPostfix(String urlString) throws MalformedURLException { + if(Objects.isNull(urlString) || urlString.isEmpty() || urlString.length()<2) { + throw new MalformedURLException(); + } + + String testPostfix = urlString.substring(urlString.length()-1); + if(testPostfix.matches(CHARACTERS_TO_REMOVE)) { postfix = urlString.substring(urlString.length()-1) + postfix; urlString = urlString.substring(0, urlString.length()-1); diff --git a/src/test/java/org/gcube/social_networking/socialutillibrary/MessageParserTest.java b/src/test/java/org/gcube/social_networking/socialutillibrary/MessageParserTest.java index 8bd5521..298ac82 100644 --- a/src/test/java/org/gcube/social_networking/socialutillibrary/MessageParserTest.java +++ b/src/test/java/org/gcube/social_networking/socialutillibrary/MessageParserTest.java @@ -60,7 +60,7 @@ public class MessageParserTest { + "\n" + "Just a kind reminder that we look forward to welcoming all of you during the Project’s upcoming Blue-Cloud “Service Exploitation & Sustainability Plan (SE&SP) and Roadmap to 2030” Workshop next Thursday, January 21st (9.30am to 1.30pm). Everyone's welcome for an interactive and productive discussion.\n" + "\n" - + "Please find here a link to the workshop Agenda (https://data.d4science.net/tfXA), including connection details.\n" + + "Please find here a link to the workshop Agenda, (https://data.d4science.net/tfXA), including connection details.\n" + "\n" + "As discussed, the workshop will provide an opportunity to share the high-level results of the initial phase of stakeholder consultations towards the B-C Roadmap and to work within the Partnership to discuss and align some of the underlying, strategic concepts and ideas that will set the direction for the B-C SE&SP and B-C Roadmap to 2030.\n" + "\n" @@ -71,6 +71,20 @@ public class MessageParserTest { + "Kind regards,\n" + "Julia"; + public static final String TEST_BUG_18356_2 = "Dear B-C colleagues,\n" + + "\n" + + "Just a kind reminder that we look forward to welcoming all of you during the Project’s upcoming Blue-Cloud “Service Exploitation & Sustainability Plan (SE&SP) and Roadmap to 2030” Workshop next Thursday, January 21st (9.30am to 1.30pm). Everyone's welcome for an interactive and productive discussion.\n" + + "\n" + + "Please find here a link to the workshop Agenda [(https://data.d4science.net/tfXA)], including connection details.\n" + + "\n" + + "As discussed, the workshop will provide an opportunity to share the high-level results of the initial phase of stakeholder consultations towards the B-C Roadmap and to work within the Partnership to discuss and align some of the underlying, strategic concepts and ideas that will set the direction for the B-C SE&SP and B-C Roadmap to 2030.\n" + + "\n" + + "To inform the discussions to be held during the workshop, we have produced a report bringing together all the feedback, messages and recommendations gathered during the initial phase of stakeholder consultations (available here - https://data.d4science.net/JEm7). It is a long document, but we have included a one-page Executive Summary with high-level, key messages & recommendations, in case useful.\n" + + "\n" + + "We look forward to a productive discussion with you next Thursday!\n" + + "\n" + + "Kind regards,\n" + + "Julia"; @Test public void test13() { @@ -123,6 +137,16 @@ public class MessageParserTest { logger.debug("{}", messageParser.getParsedMessage()); messageParser = new SocialMessageParser(TEST_BUG_18356); logger.debug("{}", messageParser.getParsedMessage()); + messageParser = new SocialMessageParser(TEST_BUG_18356_2); + logger.debug("{}", messageParser.getParsedMessage()); + messageParser = new SocialMessageParser(":)"); + logger.debug("{}", messageParser.getParsedMessage()); + } + + @Test + public void parseNonURL() { + SocialMessageParser messageParser = new SocialMessageParser(":)"); + logger.debug("{}", messageParser.getParsedMessage()); } @Test(expected=MalformedURLException.class)