From a153b74f0fccbdeac403228d96bcfe6bf52abdb3 Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Tue, 24 May 2016 15:29:26 +0000 Subject: [PATCH] Import into svn repo git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/social-networking/social-util-library@128805 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 26 ++ .project | 23 ++ .settings/org.eclipse.core.resources.prefs | 4 + .settings/org.eclipse.jdt.core.prefs | 5 + .settings/org.eclipse.m2e.core.prefs | 4 + distro/LICENSE | 1 + distro/README | 67 +++++ distro/changelog.xml | 6 + distro/descriptor.xml | 42 +++ distro/profile.xml | 25 ++ pom.xml | 206 +++++++++++++ .../socialutillibrary/Utils.java | 276 ++++++++++++++++++ 12 files changed, 685 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 .settings/org.eclipse.m2e.core.prefs create mode 100644 distro/LICENSE create mode 100644 distro/README create mode 100644 distro/changelog.xml create mode 100644 distro/descriptor.xml create mode 100644 distro/profile.xml create mode 100644 pom.xml create mode 100644 src/main/java/org/gcube/social_networking/socialutillibrary/Utils.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..79174f7 --- /dev/null +++ b/.classpath @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..03e7ba4 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + social-util-library + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..f9fe345 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/test/java=UTF-8 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..ec4300d --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/distro/LICENSE b/distro/LICENSE new file mode 100644 index 0000000..2d9616a --- /dev/null +++ b/distro/LICENSE @@ -0,0 +1 @@ +${gcube.license} \ No newline at end of file diff --git a/distro/README b/distro/README new file mode 100644 index 0000000..6c12ffd --- /dev/null +++ b/distro/README @@ -0,0 +1,67 @@ +The gCube System - ${name} +-------------------------------------------------- + +${description} + + +${gcube.description} + +${gcube.funding} + + +Version +-------------------------------------------------- + +${version} (${buildDate}) + +Please see the file named "changelog.xml" in this directory for the release notes. + + +Authors +-------------------------------------------------- + +* Massimiliano Assante (massimiliano.assante-AT-isti.cnr.it), Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo" - CNR, Pisa (Italy). +* Costantino Perciante (costantino.perciante-AT-isti.cnr.it), Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo" - CNR, Pisa (Italy). + + +Maintainers +----------- + +* Massimiliano Assante (massimiliano.assante-AT-isti.cnr.it), Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo" - CNR, Pisa (Italy). +* Costantino Perciante (costantino.perciante-AT-isti.cnr.it), Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo" - CNR, Pisa (Italy). + +Download information +-------------------------------------------------- + +Source code is available from SVN: + ${scm.url} + +Binaries can be downloaded from the gCube website: + ${gcube.website} + + +Installation +-------------------------------------------------- + +Installation documentation is available on-line in the gCube Wiki: + ${gcube.wikiRoot}/ + + +Documentation +-------------------------------------------------- + +Documentation is available on-line in the gCube Wiki: + ${gcube.wikiRoot}/ + ${gcube.wikiRoot}/ + +Support +-------------------------------------------------- + +Bugs and support requests can be reported in the gCube issue tracking tool: + ${gcube.issueTracking} + + +Licensing +-------------------------------------------------- + +This software is licensed under the terms you may find in the file named "LICENSE" in this directory. diff --git a/distro/changelog.xml b/distro/changelog.xml new file mode 100644 index 0000000..aea611d --- /dev/null +++ b/distro/changelog.xml @@ -0,0 +1,6 @@ + + + First Release + + diff --git a/distro/descriptor.xml b/distro/descriptor.xml new file mode 100644 index 0000000..9144461 --- /dev/null +++ b/distro/descriptor.xml @@ -0,0 +1,42 @@ + + servicearchive + + tar.gz + + / + + + ${distroDirectory} + / + true + + README + LICENSE + changelog.xml + profile.xml + + 755 + true + + + target/apidocs + /${artifactId}/doc/api + true + 755 + + + + + ${distroDirectory}/profile.xml + ./ + true + + + target/${build.finalName}.jar + /${artifactId} + + + \ No newline at end of file diff --git a/distro/profile.xml b/distro/profile.xml new file mode 100644 index 0000000..4a741ef --- /dev/null +++ b/distro/profile.xml @@ -0,0 +1,25 @@ + + + + Library + + ${description} + Portal + ${artifactId} + ${version} + + + ${artifactId} + ${version} + + ${groupId} + ${artifactId} + ${version} + + + ${build.finalName}.jar + + + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..7621c10 --- /dev/null +++ b/pom.xml @@ -0,0 +1,206 @@ + + 4.0.0 + + maven-parent + org.gcube.tools + 1.0.0 + + + + org.gcube.socialnetworking + social-util-library + 1.0.0-SNAPSHOT + social-util-library + + The social-util-library contains utility functions that can be used by the social-networking portlets. + + + + scm:svn:http://svn.d4science.research-infrastructures.eu/gcube/trunk/social-networking/${project.artifactId} + scm:https://svn.d4science.research-infrastructures.eu/gcube/trunk/social-networking/${project.artifactId} + http://svn.d4science.research-infrastructures.eu/gcube/trunk/social-networking/${project.artifactId} + + + + distro + 1.7 + 1.7 + UTF-8 + UTF-8 + + + + + + org.gcube.distribution + maven-portal-bom + LATEST + pom + import + + + + + + org.gcube.portal + social-networking-library + provided + + + org.gcube.portlets.widgets + pickitem-widget + [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) + + + org.gcube.portlets.user + gcube-widgets + [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) + compile + + + org.gcube.common.portal + portal-manager + [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) + provided + + + org.gcube.dvos + usermanagement-core + [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) + + + org.gcube.common.portal + portal-manager + [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) + provided + + + org.gcube.common + home-library + provided + + + org.slf4j + slf4j-log4j12 + provided + + + org.slf4j + slf4j-api + provided + + + javax.servlet + javax.servlet-api + 3.0.1 + provided + + + org.htmlparser + htmlparser + 2.1 + + + junit + junit + 4.8 + test + + + + + + + src/main/java + + **/*.* + + + + + + maven-compiler-plugin + + 1.7 + 1.7 + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.2 + + + + test-jar + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.12 + + true + + + + org.apache.maven.plugins + maven-resources-plugin + 2.5 + + + copy-profile + install + + copy-resources + + + target + + + ${distroDirectory} + true + + profile.xml + + + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + 2.2 + + + ${distroDirectory}/descriptor.xml + + + + fully.qualified.MainClass + + + + + + + + + servicearchive + install + + single + + + + + + + diff --git a/src/main/java/org/gcube/social_networking/socialutillibrary/Utils.java b/src/main/java/org/gcube/social_networking/socialutillibrary/Utils.java new file mode 100644 index 0000000..75a0bb4 --- /dev/null +++ b/src/main/java/org/gcube/social_networking/socialutillibrary/Utils.java @@ -0,0 +1,276 @@ +package org.gcube.social_networking.socialutillibrary; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.codec.binary.Base64; +import org.gcube.common.portal.GCubePortalConstants; +import org.gcube.common.portal.PortalContext; +import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.common.scope.impl.ScopeBean.Type; +import org.gcube.portal.databook.client.GCubeSocialNetworking; +import org.gcube.portlets.widgets.pickitem.shared.ItemBean; +import org.gcube.vomanagement.usermanagement.GroupManager; +import org.gcube.vomanagement.usermanagement.UserManager; +import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager; +import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager; +import org.gcube.vomanagement.usermanagement.model.GCubeUser; +import org.htmlparser.beans.StringBean; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Utils { + + /** + * logger + */ + private static final Logger logger = LoggerFactory.getLogger(Utils.class); + + /** + * + * @param preview + * @return + */ + protected static String convertFileNameAnchorHTML(String url) { + StringBuilder sb = new StringBuilder(); + sb.append("shared ").append("a file.").append(" ").toString(); + return sb.toString(); + } + + /** + * + * @param session the Asl Session + * @param withinPortal true when is on Liferay portal + * @return the users belonging to the current organization (scope) + */ + public static ArrayList getOrganizationUsers(String scope, String currUser, boolean withinPortal) { + ArrayList portalUsers = new ArrayList(); + try { + if (withinPortal) { + UserManager um = new LiferayUserManager(); + GroupManager gm = new LiferayGroupManager(); + ScopeBean sb = new ScopeBean(scope); + List users = null; + + if (sb.is(Type.INFRASTRUCTURE)) + users = um.listUsersByGroup(gm.getRootVO().getGroupId()); + else if (sb.is(Type.VRE)) { //must be in VRE + //get the name from the scope + String orgName = scope.substring(scope.lastIndexOf("/")+1, scope.length()); + //ask the users + users = um.listUsersByGroup(gm.getGroupId(orgName)); + } + else { + logger.error("Error, you must be in SCOPE VRE OR INFRASTURCTURE, you are in VO SCOPE returning no users"); + return portalUsers; + } + for (GCubeUser user : users) { + if (user.getUsername().compareTo("test.user") != 0 && user.getUsername().compareTo(currUser) != 0) { //skip test.user & current user + portalUsers.add(new ItemBean(user.getUserId()+"", user.getUsername(), user.getFullname(), user.getUserAvatarURL())); + } + } + } + else { //test users + portalUsers.add(new ItemBean("12111", "massimiliano.assante", "Test User #1", "")); + portalUsers.add(new ItemBean("14111", "massimiliano.assante", "Test Second User #2", "")); + portalUsers.add(new ItemBean("11511", "massimiliano.assante", "Test Third User", "")); + portalUsers.add(new ItemBean("11611", "massimiliano.assante", "Test Fourth User", "")); + portalUsers.add(new ItemBean("11711", "massimiliano.assante", "Test Fifth User", "")); + portalUsers.add(new ItemBean("11811", "massimiliano.assante", "Test Sixth User", "")); + portalUsers.add(new ItemBean("15811", "massimiliano.assante", "Ninth Testing User", "")); + portalUsers.add(new ItemBean("15811", "massimiliano.assante", "Eighth Testing User", "")); + portalUsers.add(new ItemBean("11211", "giogio.giorgi", "Seventh Test User", "")); + portalUsers.add(new ItemBean("2222", "pino.pinetti", "Tenth Testing User", "")); + } + } catch (Exception e) { + logger.error("Error in server get all contacts ", e); + } + return portalUsers; + } + + /** + * utility method that extract the hashtags from a text + * @param postText + * @return the list of hashtags present in the text + */ + protected static List getHashTags(String postText) { + List hashtags = new ArrayList<>(); + Pattern MY_PATTERN = Pattern.compile("^#\\w+|\\s#\\w+"); + Matcher matcher = MY_PATTERN.matcher(postText); + while (matcher.find()) { + hashtags.add("#"+matcher.group().replace(" ", "").replace("#", "")); + } + return hashtags; + } + + /** + * utility method that extract an url ina text when you paste a link + * @param feedText + * @return the text with the clickable url in it + */ + protected static String extractURL(String feedText) { + // separate input by spaces ( URLs have no spaces ) + feedText = feedText.replaceAll("(\r\n|\n)","
"); + String [] parts = feedText.split("\\s"); + // Attempt to convert each item into an URL. + for( String item : parts ) { + String toCheck = getHttpToken(item); + if (toCheck != null) { + try { + new URL(toCheck); + return toCheck; + } catch (MalformedURLException e) { + // If there was an URL then it's not valid + logger.error("MalformedURLException returning... "); + return null; + } + } + } + return null; + } + + /** + * Escape an html string. Escaping data received from the client helps to + * prevent cross-site script vulnerabilities. + * + * @param html the html string to escape + * @return the escaped string + */ + protected static String escapeHtmlAndTransformUrl(String html) { + if (html == null) { + return null; + } + String toReturn = escapeHtml(html); + + // replace all the line breaks by
+ toReturn = toReturn.replaceAll("(\r\n|\n)","
"); + //transfrom the URL in a clickable URL + toReturn = transformUrls(toReturn); + // then replace all the double spaces by the html version   + toReturn = toReturn.replaceAll("\\s\\s","  "); + return toReturn; + } + + /** + * Escape an html string. Escaping data received from the client helps to + * prevent cross-site script vulnerabilities. + * + * @param html the html string to escape + * @return the escaped string + */ + protected static String escapeHtml(String html) { + return html.replaceAll("&", "&").replaceAll("<", "<") + .replaceAll(">", ">"); + } + + /** + * utility method that convert a url ina text in a clickable url by the browser + * and if the user has just pasted a link, converts the link in: shared a link + * @param feedText + * @return the text with the clickable url in it + */ + protected static String transformUrls(String feedText) { + StringBuilder sb = new StringBuilder(); + // separate input by spaces ( URLs have no spaces ) + String [] parts = feedText.split("\\s"); + // Attempt to convert each item into an URL. + for (int i = 0; i < parts.length; i++) { + String toCheck = getHttpToken(parts[i]); + if (toCheck != null) { + try { + URL url = new URL(toCheck); + if (i == 0 && parts.length == 1) //then he shared just a link + return sb.append("shared ").append("a link.").append(" ").toString(); + // If possible then replace with anchor... + sb.append("").append(url).append(" "); + } catch (MalformedURLException e) { + // If there was an URL then it's not valid + logger.error("MalformedURLException returning... "); + return feedText; + } + } else { + sb.append(parts[i]); + sb.append(" "); + } + } + return sb.toString(); + } + /** + * check the tokens of a pasted text and see if there's any http link in it + * @param item a text token + * @return the actual http link + */ + private static String getHttpToken(String item) { + if (item.startsWith("http") || item.startsWith("www") || item.startsWith("(www") || item.startsWith("(http")) { + if (item.startsWith("(")) + item = item.substring(1, item.length()); + if (item.endsWith(".") || item.endsWith(")")) { //sometimes people write the url and close the phrase with a . + item = item.substring(0, item.length()-1); + } + item = item.startsWith("www") ? "http://"+item : item; + logger.debug("getHttpToken returns -> " + item); + return item; + } + return null; + } + /** + * convert the mentioned people in HTML anchor and also Encode the params Base64 + * @param escapedFeedText + * @param taggedPeople + * @return + */ + protected static String convertMentionPeopleAnchorHTML(String escapedFeedText, ArrayList taggedPeople, HttpServletRequest request) { + String userProfilePageURL = ""; + userProfilePageURL = PortalContext.getConfiguration().getSiteLandingPagePath(request)+GCubePortalConstants.USER_PROFILE_FRIENDLY_URL; + for (ItemBean tagged : taggedPeople) { + String taggedHTML = ""+tagged.getAlternativeName()+" "; + escapedFeedText = escapedFeedText.replace(tagged.getAlternativeName(), taggedHTML); + } + return escapedFeedText; + } + + /** + * convert the hashtag in HTML anchor and also Encode the params Base64 + * @param escapedFeedText + * @param hashtags + * @return + */ + protected static String convertHashtagsAnchorHTML(String escapedFeedText, List hashtags) { + for (String hashtag : hashtags) { + String taggedHTML = ""+hashtag+""; + //TODO: does not work if the word is no preceeded by # + //dirty trick: double replace because \\boundaries do not accept # char + final String placeholder = "ñöñö-ñöñö"; + String first = escapedFeedText.replaceAll("(?i)\\b"+hashtag.substring(1)+"\\b", placeholder); + escapedFeedText = first.replaceAll("#"+placeholder, taggedHTML); + //this is needed if there is a word equal to an hashtagged one without '#' e.g. #job and job + escapedFeedText = escapedFeedText.replaceAll(placeholder, hashtag.substring(1)); + } + return escapedFeedText; + } + + /** + * generate the description parsing the content (Best Guess) + * @param link the link to check + * @return the description guessed + */ + private static String createDescriptionFromContent(String link) { + StringBean sb = new StringBean(); + sb.setURL(link); + sb.setLinks(false); + String description = sb.getStrings(); + description = ((description.length() > 256) ? description.substring(0, 256)+"..." : description); + return description; + } +}