2016-05-24 17:29:26 +02:00
|
|
|
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;
|
2016-05-25 12:37:32 +02:00
|
|
|
import org.gcube.vomanagement.usermanagement.RoleManager;
|
2016-05-24 17:29:26 +02:00
|
|
|
import org.gcube.vomanagement.usermanagement.UserManager;
|
|
|
|
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
|
2016-05-25 12:37:32 +02:00
|
|
|
import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager;
|
2016-05-24 17:29:26 +02:00
|
|
|
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
|
2016-05-25 12:37:32 +02:00
|
|
|
import org.gcube.vomanagement.usermanagement.model.GCubeGroup;
|
|
|
|
import org.gcube.vomanagement.usermanagement.model.GCubeTeam;
|
2016-05-24 17:29:26 +02:00
|
|
|
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);
|
2016-10-04 17:05:18 +02:00
|
|
|
|
2016-09-08 14:45:08 +02:00
|
|
|
/**
|
|
|
|
* Hashtag regex enhanced for ticket #4937
|
|
|
|
*/
|
2016-10-04 17:05:18 +02:00
|
|
|
private static final String HASHTAG_REGEX = "^#\\w+([.]?\\w+)*|\\s#\\w+([.]?\\w+)*";
|
2016-05-25 12:37:32 +02:00
|
|
|
|
2016-05-24 17:29:26 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param preview
|
|
|
|
* @return
|
|
|
|
*/
|
2016-05-24 17:33:10 +02:00
|
|
|
public static String convertFileNameAnchorHTML(String url) {
|
2016-05-24 17:29:26 +02:00
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.append("<span style=\"color:gray; font-size:12px;\">shared </span><a class=\"link\" href=\"").append(url).append("\" target=\"_blank\">").append("a file.").append("</a> ").toString();
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param session the Asl Session
|
|
|
|
* @param withinPortal true when is on Liferay portal
|
2016-05-25 12:37:32 +02:00
|
|
|
* @return the users plus the groups belonging to the current organization (scope)
|
2016-05-24 17:29:26 +02:00
|
|
|
*/
|
2016-05-25 12:37:32 +02:00
|
|
|
public static ArrayList<ItemBean> getDisplayableItemBeans(String scope, String currUser, boolean withinPortal) {
|
|
|
|
ArrayList<ItemBean> portalBeans = new ArrayList<ItemBean>();
|
2016-05-24 17:29:26 +02:00
|
|
|
try {
|
|
|
|
if (withinPortal) {
|
|
|
|
UserManager um = new LiferayUserManager();
|
|
|
|
GroupManager gm = new LiferayGroupManager();
|
2016-05-25 12:37:32 +02:00
|
|
|
RoleManager rm = new LiferayRoleManager();
|
2016-05-24 17:29:26 +02:00
|
|
|
ScopeBean sb = new ScopeBean(scope);
|
|
|
|
List<GCubeUser> users = null;
|
2016-05-25 12:37:32 +02:00
|
|
|
List<GCubeTeam> teams = null;
|
2016-05-24 17:29:26 +02:00
|
|
|
|
2016-05-25 12:37:32 +02:00
|
|
|
if (sb.is(Type.INFRASTRUCTURE)){
|
2016-05-24 17:29:26 +02:00
|
|
|
users = um.listUsersByGroup(gm.getRootVO().getGroupId());
|
2016-05-25 12:37:32 +02:00
|
|
|
|
|
|
|
// we need to retrieve vres to whom the current user belongs and then retrieve their teams
|
|
|
|
List<GCubeGroup> groupsOfCurrentUser = gm.listGroupsByUser(um.getUserId(currUser));
|
|
|
|
|
|
|
|
teams = new ArrayList<GCubeTeam>();
|
|
|
|
|
|
|
|
// retrieve the teams of each group
|
|
|
|
for (GCubeGroup gCubeGroup : groupsOfCurrentUser) {
|
|
|
|
List<GCubeTeam> partialTeamList = rm.listTeamsByGroup(gCubeGroup.getGroupId());
|
|
|
|
if(partialTeamList != null && !partialTeamList.isEmpty())
|
|
|
|
teams.addAll(partialTeamList);
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug("Teams retrieved are " + teams);
|
|
|
|
|
|
|
|
}
|
2016-05-24 17:29:26 +02:00
|
|
|
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));
|
2016-05-25 12:37:32 +02:00
|
|
|
// ask the teams
|
|
|
|
teams = rm.listTeamsByGroup(gm.getGroupId(orgName));
|
|
|
|
logger.debug("Teams retrieved are " + teams);
|
2016-05-24 17:29:26 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
logger.error("Error, you must be in SCOPE VRE OR INFRASTURCTURE, you are in VO SCOPE returning no users");
|
2016-05-25 12:37:32 +02:00
|
|
|
return portalBeans;
|
2016-05-24 17:29:26 +02:00
|
|
|
}
|
|
|
|
for (GCubeUser user : users) {
|
|
|
|
if (user.getUsername().compareTo("test.user") != 0 && user.getUsername().compareTo(currUser) != 0) { //skip test.user & current user
|
2016-05-25 12:37:32 +02:00
|
|
|
portalBeans.add(new ItemBean(user.getUserId()+"", user.getUsername(), user.getFullname(), user.getUserAvatarURL()));
|
2016-05-24 17:29:26 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-25 12:37:32 +02:00
|
|
|
for (GCubeTeam gCubeTeam : teams) {
|
|
|
|
portalBeans.add(new ItemBean(gCubeTeam.getTeamId()+"", gCubeTeam.getTeamName()));
|
|
|
|
}
|
2016-05-24 17:29:26 +02:00
|
|
|
}
|
|
|
|
else { //test users
|
2016-05-25 12:37:32 +02:00
|
|
|
portalBeans.add(new ItemBean("12111", "massimiliano.assante", "Test User #1", ""));
|
|
|
|
portalBeans.add(new ItemBean("14111", "massimiliano.assante", "Test Second User #2", ""));
|
|
|
|
portalBeans.add(new ItemBean("11511", "massimiliano.assante", "Test Third User", ""));
|
|
|
|
portalBeans.add(new ItemBean("11611", "massimiliano.assante", "Test Fourth User", ""));
|
|
|
|
portalBeans.add(new ItemBean("11711", "massimiliano.assante", "Test Fifth User", ""));
|
|
|
|
portalBeans.add(new ItemBean("11811", "massimiliano.assante", "Test Sixth User", ""));
|
|
|
|
portalBeans.add(new ItemBean("15811", "massimiliano.assante", "Ninth Testing User", ""));
|
|
|
|
portalBeans.add(new ItemBean("15811", "massimiliano.assante", "Eighth Testing User", ""));
|
|
|
|
portalBeans.add(new ItemBean("11211", "giogio.giorgi", "Seventh Test User", ""));
|
|
|
|
portalBeans.add(new ItemBean("2222", "pino.pinetti", "Tenth Testing User", ""));
|
2016-05-24 17:29:26 +02:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("Error in server get all contacts ", e);
|
|
|
|
}
|
2016-05-25 12:37:32 +02:00
|
|
|
return portalBeans;
|
2016-05-24 17:29:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* utility method that extract the hashtags from a text
|
|
|
|
* @param postText
|
|
|
|
* @return the list of hashtags present in the text
|
|
|
|
*/
|
2016-05-24 17:33:10 +02:00
|
|
|
public static List<String> getHashTags(String postText) {
|
2016-05-24 17:29:26 +02:00
|
|
|
List<String> hashtags = new ArrayList<>();
|
2016-09-08 14:45:08 +02:00
|
|
|
Pattern MY_PATTERN = Pattern.compile(HASHTAG_REGEX);//Pattern.compile("^#\\w+|\\s#\\w+");
|
2016-05-24 17:29:26 +02:00
|
|
|
Matcher matcher = MY_PATTERN.matcher(postText);
|
|
|
|
while (matcher.find()) {
|
|
|
|
hashtags.add("#"+matcher.group().replace(" ", "").replace("#", ""));
|
|
|
|
}
|
|
|
|
return hashtags;
|
|
|
|
}
|
2016-05-25 12:37:32 +02:00
|
|
|
|
2016-05-24 17:29:26 +02:00
|
|
|
/**
|
|
|
|
* utility method that extract an url ina text when you paste a link
|
|
|
|
* @param feedText
|
|
|
|
* @return the text with the clickable url in it
|
|
|
|
*/
|
2016-05-24 17:33:10 +02:00
|
|
|
public static String extractURL(String feedText) {
|
2016-05-24 17:29:26 +02:00
|
|
|
// separate input by spaces ( URLs have no spaces )
|
|
|
|
feedText = feedText.replaceAll("(\r\n|\n)"," <br/> ");
|
|
|
|
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
|
|
|
|
*/
|
2016-05-24 17:33:10 +02:00
|
|
|
public static String escapeHtmlAndTransformUrl(String html) {
|
2016-05-24 17:29:26 +02:00
|
|
|
if (html == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String toReturn = escapeHtml(html);
|
|
|
|
|
|
|
|
// replace all the line breaks by <br/>
|
|
|
|
toReturn = toReturn.replaceAll("(\r\n|\n)"," <br/> ");
|
|
|
|
//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;
|
|
|
|
}
|
2016-05-25 12:37:32 +02:00
|
|
|
|
2016-05-24 17:29:26 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2016-05-24 17:33:10 +02:00
|
|
|
public static String escapeHtml(String html) {
|
2016-05-24 17:29:26 +02:00
|
|
|
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
|
|
|
|
*/
|
2016-05-24 17:33:10 +02:00
|
|
|
public static String transformUrls(String feedText) {
|
2016-05-24 17:29:26 +02:00
|
|
|
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("<span style=\"color:gray; font-size:12px;\">shared </span><a class=\"link\" href=\"").append(url).append("\" target=\"_blank\">").append("a link.").append("</a> ").toString();
|
|
|
|
// If possible then replace with anchor...
|
|
|
|
sb.append("<a class=\"link\" style=\"font-size:14px;\" href=\"").append(url).append("\" target=\"_blank\">").append(url).append("</a> ");
|
|
|
|
} 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
|
|
|
|
*/
|
2016-05-24 17:33:10 +02:00
|
|
|
public static String getHttpToken(String item) {
|
2016-05-24 17:29:26 +02:00
|
|
|
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;
|
|
|
|
}
|
2016-10-04 17:05:18 +02:00
|
|
|
|
|
|
|
/**
|
2016-05-24 17:29:26 +02:00
|
|
|
* convert the mentioned people in HTML anchor and also Encode the params Base64
|
|
|
|
* @param escapedFeedText
|
|
|
|
* @param taggedPeople
|
|
|
|
* @return
|
|
|
|
*/
|
2016-05-24 17:33:10 +02:00
|
|
|
public static String convertMentionPeopleAnchorHTML(String escapedFeedText, ArrayList<ItemBean> taggedPeople, HttpServletRequest request) {
|
2016-05-25 12:37:32 +02:00
|
|
|
String pageToRedirectURL = "";
|
|
|
|
String httpGETAttrName = "";
|
|
|
|
String httpGETAttrValue ="";
|
2016-10-04 17:05:18 +02:00
|
|
|
|
2016-05-24 17:29:26 +02:00
|
|
|
for (ItemBean tagged : taggedPeople) {
|
2016-05-25 12:37:32 +02:00
|
|
|
if (! tagged.isItemGroup()) {
|
|
|
|
pageToRedirectURL = PortalContext.getConfiguration().getSiteLandingPagePath(request)+GCubePortalConstants.USER_PROFILE_FRIENDLY_URL;
|
|
|
|
httpGETAttrName = GCubeSocialNetworking.USER_PROFILE_OID;
|
|
|
|
httpGETAttrValue = tagged.getName();
|
|
|
|
} else {
|
|
|
|
long teamId = Long.parseLong(tagged.getId());
|
|
|
|
try {
|
|
|
|
GCubeTeam theTeam = new LiferayRoleManager().getTeam(teamId);
|
2016-05-25 23:17:02 +02:00
|
|
|
//returns the VRE url e.g. /devVRE
|
2016-05-25 12:37:32 +02:00
|
|
|
String vreURL = new LiferayGroupManager().getGroup(theTeam.getGroupId()).getFriendlyURL();
|
|
|
|
//append the members url
|
2016-05-25 23:17:02 +02:00
|
|
|
pageToRedirectURL= GCubePortalConstants.PREFIX_GROUP_URL + vreURL + GCubePortalConstants.GROUP_MEMBERS_FRIENDLY_URL;
|
2016-05-25 12:37:32 +02:00
|
|
|
httpGETAttrName = GCubeSocialNetworking.GROUP_MEMBERS_OID;
|
|
|
|
httpGETAttrValue = tagged.getId();
|
|
|
|
} catch (Exception e) {
|
2016-05-25 14:56:28 +02:00
|
|
|
logger.error("Error while retrieving team", e);
|
2016-05-25 12:37:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
String taggedHTML = "<a class=\"link\" href=\""+pageToRedirectURL
|
2016-05-24 17:29:26 +02:00
|
|
|
+"?"+
|
2016-05-25 12:37:32 +02:00
|
|
|
new String(Base64.encodeBase64(httpGETAttrName.getBytes()))+"="+
|
|
|
|
new String(Base64.encodeBase64(httpGETAttrValue.getBytes()))+"\">"+tagged.getAlternativeName()+"</a> ";
|
2016-05-24 17:29:26 +02:00
|
|
|
escapedFeedText = escapedFeedText.replace(tagged.getAlternativeName(), taggedHTML);
|
|
|
|
}
|
|
|
|
return escapedFeedText;
|
|
|
|
}
|
2016-05-25 12:37:32 +02:00
|
|
|
|
2016-05-24 17:29:26 +02:00
|
|
|
/**
|
|
|
|
* convert the hashtag in HTML anchor and also Encode the params Base64
|
|
|
|
* @param escapedFeedText
|
|
|
|
* @param hashtags
|
|
|
|
* @return
|
|
|
|
*/
|
2016-05-24 17:33:10 +02:00
|
|
|
public static String convertHashtagsAnchorHTML(String escapedFeedText, List<String> hashtags) {
|
2016-05-24 17:29:26 +02:00
|
|
|
for (String hashtag : hashtags) {
|
|
|
|
String taggedHTML = "<a class=\"link\" style=\"font-size:14px;\" href=\"?"+
|
|
|
|
new String(Base64.encodeBase64(GCubeSocialNetworking.HASHTAG_OID.getBytes()))+"="+
|
|
|
|
new String(Base64.encodeBase64(hashtag.getBytes()))+"\">"+hashtag+"</a>";
|
|
|
|
//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;
|
|
|
|
}
|
2016-05-25 12:37:32 +02:00
|
|
|
|
2016-05-24 17:29:26 +02:00
|
|
|
/**
|
|
|
|
* generate the description parsing the content (Best Guess)
|
|
|
|
* @param link the link to check
|
|
|
|
* @return the description guessed
|
|
|
|
*/
|
2016-05-24 17:33:10 +02:00
|
|
|
public static String createDescriptionFromContent(String link) {
|
2016-05-24 17:29:26 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|