2013-01-26 20:02:51 +01:00
package org.gcube.portlets.user.shareupdates.server ;
2014-01-27 19:08:15 +01:00
import java.io.File ;
2013-01-26 20:02:51 +01:00
import java.io.IOException ;
import java.net.HttpURLConnection ;
import java.net.MalformedURLException ;
import java.net.URL ;
import java.net.URLConnection ;
import java.util.ArrayList ;
2014-10-02 19:47:15 +02:00
import java.util.Collections ;
2013-01-26 20:02:51 +01:00
import java.util.Date ;
import java.util.HashMap ;
2017-01-31 15:16:51 +01:00
import java.util.HashSet ;
2016-05-25 15:05:07 +02:00
import java.util.Iterator ;
2013-01-26 20:02:51 +01:00
import java.util.List ;
2014-10-02 19:47:15 +02:00
import java.util.Map ;
2016-05-25 15:05:07 +02:00
import java.util.Map.Entry ;
2017-01-31 15:16:51 +01:00
import java.util.Set ;
2013-01-26 20:02:51 +01:00
import java.util.UUID ;
2016-01-15 18:24:52 +01:00
import javax.net.ssl.HttpsURLConnection ;
2013-01-26 20:02:51 +01:00
import org.apache.commons.validator.routines.UrlValidator ;
2013-04-18 11:44:10 +02:00
import org.gcube.applicationsupportlayer.social.ApplicationNotificationsManager ;
import org.gcube.applicationsupportlayer.social.NotificationsManager ;
2016-05-19 00:28:26 +02:00
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingSite ;
2016-02-25 19:00:58 +01:00
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingUser ;
2016-04-27 14:35:28 +02:00
import org.gcube.common.portal.GCubePortalConstants ;
2014-01-21 18:43:03 +01:00
import org.gcube.common.portal.PortalContext ;
import org.gcube.common.scope.api.ScopeProvider ;
2013-09-26 15:44:21 +02:00
import org.gcube.common.scope.impl.ScopeBean ;
import org.gcube.common.scope.impl.ScopeBean.Type ;
2014-01-21 18:43:03 +01:00
import org.gcube.contentmanagement.blobstorage.service.IClient ;
import org.gcube.contentmanager.storageclient.wrapper.AccessType ;
import org.gcube.contentmanager.storageclient.wrapper.MemoryType ;
import org.gcube.contentmanager.storageclient.wrapper.StorageClient ;
2013-01-26 20:02:51 +01:00
import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl ;
import org.gcube.portal.databook.server.DatabookStore ;
2016-01-15 18:24:52 +01:00
import org.gcube.portal.databook.shared.Attachment ;
2013-01-26 20:02:51 +01:00
import org.gcube.portal.databook.shared.ClientFeed ;
import org.gcube.portal.databook.shared.Feed ;
import org.gcube.portal.databook.shared.FeedType ;
import org.gcube.portal.databook.shared.PrivacyLevel ;
import org.gcube.portal.databook.shared.UserInfo ;
import org.gcube.portal.databook.shared.ex.FeedIDNotFoundException ;
2015-12-21 18:33:23 +01:00
import org.gcube.portal.notifications.bean.GenericItemBean ;
import org.gcube.portal.notifications.thread.MentionNotificationsThread ;
import org.gcube.portal.notifications.thread.PostNotificationsThread ;
2013-01-26 20:02:51 +01:00
import org.gcube.portlets.user.shareupdates.client.ShareUpdateService ;
2014-01-30 16:39:42 +01:00
import org.gcube.portlets.user.shareupdates.client.view.ShareUpdateForm ;
2013-01-26 20:02:51 +01:00
import org.gcube.portlets.user.shareupdates.server.opengraph.OpenGraph ;
2014-10-02 19:47:15 +02:00
import org.gcube.portlets.user.shareupdates.shared.HashTagAndOccurrence ;
2013-01-26 20:02:51 +01:00
import org.gcube.portlets.user.shareupdates.shared.LinkPreview ;
2018-03-18 17:49:46 +01:00
import org.gcube.portlets.user.shareupdates.shared.MentionedDTO ;
2016-01-15 18:24:52 +01:00
import org.gcube.portlets.user.shareupdates.shared.UploadedFile ;
2013-04-11 18:46:58 +02:00
import org.gcube.portlets.user.shareupdates.shared.UserSettings ;
2014-10-02 19:47:15 +02:00
import org.gcube.portlets.widgets.pickitem.shared.ItemBean ;
2016-05-25 15:05:07 +02:00
import org.gcube.social_networking.socialutillibrary.Utils ;
2019-01-24 11:55:50 +01:00
import org.gcube.socialnetworking.socialtoken.SocialMessageParser ;
import org.gcube.socialnetworking.socialtoken.URLToken ;
2013-01-26 20:02:51 +01:00
import org.gcube.vomanagement.usermanagement.GroupManager ;
2017-01-31 15:16:51 +01:00
import org.gcube.vomanagement.usermanagement.RoleManager ;
2013-04-16 23:49:16 +02:00
import org.gcube.vomanagement.usermanagement.UserManager ;
2016-04-27 14:35:28 +02:00
import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault ;
2016-05-25 15:05:07 +02:00
import org.gcube.vomanagement.usermanagement.exception.TeamRetrievalFault ;
2016-04-27 14:35:28 +02:00
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException ;
2016-05-25 15:05:07 +02:00
import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault ;
2016-04-27 14:35:28 +02:00
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager ;
import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager ;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager ;
import org.gcube.vomanagement.usermanagement.model.CustomAttributeKeys ;
import org.gcube.vomanagement.usermanagement.model.GCubeGroup ;
2018-03-18 17:49:46 +01:00
import org.gcube.vomanagement.usermanagement.model.GCubeTeam ;
2016-04-27 14:35:28 +02:00
import org.gcube.vomanagement.usermanagement.model.GCubeUser ;
2013-09-26 15:44:21 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2013-01-26 20:02:51 +01:00
import com.google.gwt.user.server.rpc.RemoteServiceServlet ;
import com.liferay.portal.kernel.exception.PortalException ;
import com.liferay.portal.kernel.exception.SystemException ;
import com.liferay.portal.service.UserLocalServiceUtil ;
/ * *
* The server side implementation of the RPC service .
* /
@SuppressWarnings ( " serial " )
public class ShareUpdateServiceImpl extends RemoteServiceServlet implements ShareUpdateService {
2014-05-09 17:05:42 +02:00
public static final String TEST_USER = " test.user " ;
2014-01-21 18:43:03 +01:00
private static final String STORAGE_OWNER = " gCubeSocialFramework " ;
public static final String UPLOAD_DIR = " /social-framework-uploads " ;
2014-03-05 00:38:20 +01:00
private static final String NEWS_FEED_PORTLET_CLASSNAME = " org.gcube.portlets.user.newsfeed.server.NewsServiceImpl " ;
2016-11-18 13:03:13 +01:00
private static final Logger _log = LoggerFactory . getLogger ( ShareUpdateServiceImpl . class ) ;
2013-01-26 20:02:51 +01:00
private DatabookStore store ;
2016-11-15 18:48:54 +01:00
2013-01-26 20:02:51 +01:00
/ * *
2017-01-31 15:16:51 +01:00
* Connect to cassandra at startup
2013-01-26 20:02:51 +01:00
* /
public void init ( ) {
2014-01-27 19:08:15 +01:00
store = new DBCassandraAstyanaxImpl ( ) ;
2013-01-26 20:02:51 +01:00
}
2017-01-31 15:16:51 +01:00
/ * *
* Close connection pool on close
* /
2013-01-26 20:02:51 +01:00
public void destroy ( ) {
store . closeConnection ( ) ;
}
2014-05-09 17:05:42 +02:00
/ * *
*
* @return true if you ' re running into the portal , false if in development
* /
2016-11-21 12:22:18 +01:00
public static boolean isWithinPortal ( ) {
2014-05-09 17:05:42 +02:00
try {
UserLocalServiceUtil . getService ( ) ;
return true ;
}
2016-11-14 18:03:55 +01:00
catch ( Exception ex ) {
2014-05-09 17:05:42 +02:00
_log . trace ( " Development Mode ON " ) ;
return false ;
}
}
2016-11-15 18:48:54 +01:00
2013-04-17 17:29:44 +02:00
/ * *
2016-01-15 18:24:52 +01:00
* Share post that could contain a link preview .
2013-04-17 17:29:44 +02:00
* /
2016-01-15 18:24:52 +01:00
@Override
public ClientFeed sharePostWithLinkPreview ( String postText , FeedType feedType , PrivacyLevel pLevel ,
2018-03-18 17:49:46 +01:00
Long vreOrgId , LinkPreview preview , String urlThumbnail , ArrayList < MentionedDTO > mentionedItems , boolean notifyGroup ) {
2013-10-03 12:16:57 +02:00
2019-01-23 17:06:35 +01:00
/ *
2016-01-15 18:24:52 +01:00
// escape text
2016-05-25 15:05:07 +02:00
String escapedFeedText = Utils . escapeHtmlAndTransformUrl ( postText ) ;
2015-04-27 16:52:29 +02:00
2016-01-15 18:24:52 +01:00
// get hashtags
2016-06-15 17:15:17 +02:00
List < String > hashtags = Utils . getHashTags ( escapedFeedText ) ;
2014-10-06 14:16:44 +02:00
if ( hashtags ! = null & & ! hashtags . isEmpty ( ) )
2016-05-25 15:05:07 +02:00
escapedFeedText = Utils . convertHashtagsAnchorHTML ( escapedFeedText , hashtags ) ;
2015-04-27 16:52:29 +02:00
2016-01-15 18:24:52 +01:00
// retrieve mentioned users
2014-10-02 19:47:15 +02:00
ArrayList < ItemBean > mentionedUsers = null ;
2018-03-18 17:49:46 +01:00
if ( mentionedItems ! = null & & ! mentionedItems . isEmpty ( ) ) {
mentionedUsers = getMentionsBean ( mentionedItems ) ;
2016-05-25 15:05:07 +02:00
escapedFeedText = Utils . convertMentionPeopleAnchorHTML ( escapedFeedText , mentionedUsers , getThreadLocalRequest ( ) ) ;
2013-07-10 17:39:14 +02:00
}
2019-01-23 17:06:35 +01:00
* /
2019-01-24 10:02:45 +01:00
ArrayList < ItemBean > mentionedUsers = new ArrayList < > ( ) ;
2019-01-23 17:06:35 +01:00
if ( mentionedItems ! = null & & ! mentionedItems . isEmpty ( ) ) {
mentionedUsers = getMentionsBean ( mentionedItems ) ;
}
2019-01-24 11:55:50 +01:00
SocialMessageParser messageParser = new SocialMessageParser ( postText ) ;
2019-01-23 17:06:35 +01:00
String siteLandingPagePath = PortalContext . getConfiguration ( ) . getSiteLandingPagePath ( getThreadLocalRequest ( ) ) ;
String escapedFeedText = messageParser . getParsedMessage ( mentionedUsers , siteLandingPagePath ) ;
List < String > hashtags = messageParser . getHashtags ( ) ;
2016-11-14 18:03:55 +01:00
PortalContext context = PortalContext . getConfiguration ( ) ;
GCubeUser currUser = context . getCurrentUser ( getThreadLocalRequest ( ) ) ;
2014-03-22 17:32:46 +01:00
2016-11-14 18:03:55 +01:00
String username = currUser . getUsername ( ) ;
String email = currUser . getEmail ( ) ;
String fullName = currUser . getFullname ( ) ;
2016-11-18 13:03:13 +01:00
String thumbnailAvatarURL = currUser . getUserAvatarURL ( ) ;
2016-11-15 18:48:54 +01:00
2016-01-15 18:24:52 +01:00
// get data from the preview of the link
2014-01-30 16:39:42 +01:00
String linkTitle = preview . getTitle ( ) ;
String linkDesc = preview . getDescription ( ) ;
String host = preview . getHost ( ) ;
String url = preview . getUrl ( ) ;
2014-04-04 16:08:04 +02:00
if ( urlThumbnail = = null )
urlThumbnail = " null " ;
2016-11-15 18:48:54 +01:00
else
2017-01-16 18:22:39 +01:00
urlThumbnail = FilePreviewer . saveThumbnailOnFTPAndGetUrl ( urlThumbnail , context . getCurrentScope ( getThreadLocalRequest ( ) ) ) ;
2015-04-27 16:52:29 +02:00
2013-04-09 17:12:10 +02:00
Date feedDate = new Date ( ) ;
2018-03-18 17:49:46 +01:00
2016-01-15 18:24:52 +01:00
//get the VRE scope if single channel post
String vreScope2Set = " " ;
2016-02-25 19:00:58 +01:00
if ( pLevel = = PrivacyLevel . SINGLE_VRE & & vreOrgId ! = null ) {
2016-11-14 18:03:55 +01:00
vreScope2Set = context . getCurrentScope ( getThreadLocalRequest ( ) ) ;
2016-01-15 18:24:52 +01:00
}
// build the feed to share (and save on cassandra)
Feed toShare = new Feed ( UUID . randomUUID ( ) . toString ( ) , feedType , username , feedDate ,
vreScope2Set , url , urlThumbnail , escapedFeedText , pLevel , fullName , email , thumbnailAvatarURL , linkTitle , linkDesc , host ) ;
_log . info ( " Attempting to save Feed with text: " + escapedFeedText + " Level: " + pLevel + " Timeline= " + vreScope2Set ) ;
boolean result = store . saveUserFeed ( toShare ) ;
//need to put the feed into VRES Timeline too
if ( pLevel = = PrivacyLevel . VRES ) {
_log . trace ( " PrivacyLevel was set to VRES attempting to write onto User's VRES Timelines " ) ;
2016-04-27 14:35:28 +02:00
for ( GCubeGroup vre : getUserVREs ( username ) ) {
String vreScope = " " ;
2016-01-15 18:24:52 +01:00
try {
2016-04-27 14:35:28 +02:00
vreScope = new LiferayGroupManager ( ) . getInfrastructureScope ( vre . getGroupId ( ) ) ;
_log . trace ( " Attempting to write onto " + vreScope ) ;
2016-01-15 18:24:52 +01:00
store . saveFeedToVRETimeline ( toShare . getKey ( ) , vreScope ) ;
} catch ( FeedIDNotFoundException e ) {
_log . error ( " Error writing onto VRES Time Line " + vreScope ) ;
2016-04-27 14:35:28 +02:00
}
catch ( Exception e ) {
_log . error ( " Error retrieving user VRES " ) ;
} //save the feed
2016-01-15 18:24:52 +01:00
_log . trace ( " Success writing onto " + vreScope ) ;
}
}
//share on a single VRE Timeline
//receives a VreId(groupId) get the scope from the groupId
2016-02-25 19:00:58 +01:00
else if ( pLevel = = PrivacyLevel . SINGLE_VRE & & vreOrgId ! = null ) {
2016-01-15 18:24:52 +01:00
_log . trace ( " Attempting to write onto " + vreScope2Set ) ;
try {
store . saveFeedToVRETimeline ( toShare . getKey ( ) , vreScope2Set ) ;
if ( hashtags ! = null & & ! hashtags . isEmpty ( ) )
store . saveHashTags ( toShare . getKey ( ) , vreScope2Set , hashtags ) ;
} catch ( FeedIDNotFoundException e ) {
_log . error ( " Error writing onto VRES Time Line " + vreScope2Set ) ;
} //save the feed
_log . trace ( " Success writing onto " + vreScope2Set ) ;
}
2016-05-25 15:05:07 +02:00
if ( ! result )
return null ;
2016-01-15 18:24:52 +01:00
//everything went fine
ClientFeed cf = new ClientFeed ( toShare . getKey ( ) , toShare . getType ( ) . toString ( ) , username , feedDate , toShare . getUri ( ) ,
2017-05-18 12:58:56 +02:00
toShare . getDescription ( ) , fullName , email , thumbnailAvatarURL , toShare . getLinkTitle ( ) , toShare . getLinkDescription ( ) ,
2016-01-18 10:26:36 +01:00
toShare . getUriThumbnail ( ) , toShare . getLinkHost ( ) , null ) ;
2016-01-15 18:24:52 +01:00
2016-05-25 15:05:07 +02:00
// check if is needed to notify people in the vre
2017-01-31 15:16:51 +01:00
notifyPeopleGroup ( pLevel , vreOrgId , notifyGroup , username , email , fullName , thumbnailAvatarURL , toShare , hashtags , vreScope2Set , escapedFeedText , mentionedUsers ) ;
2016-01-15 18:24:52 +01:00
//send the notification to the mentioned users
2016-05-25 15:05:07 +02:00
if ( mentionedUsers ! = null & & mentionedUsers . size ( ) > 0 )
notifyMentionedUsers ( vreScope2Set , mentionedUsers , username , email , fullName , thumbnailAvatarURL , toShare , escapedFeedText ) ;
2016-01-15 18:24:52 +01:00
return cf ;
}
2016-05-25 15:05:07 +02:00
2016-01-15 18:24:52 +01:00
/ * *
* Share a post with at least one attachment .
* /
@Override
public ClientFeed sharePostWithAttachments ( String feedText , FeedType feedType ,
2016-02-25 19:00:58 +01:00
PrivacyLevel pLevel , Long vreOrgId , ArrayList < UploadedFile > uploadedFiles ,
2018-03-18 17:49:46 +01:00
ArrayList < MentionedDTO > mentionedItems , boolean notifyGroup , boolean saveCopyWokspace ) {
2016-01-15 18:24:52 +01:00
2019-01-23 17:06:35 +01:00
/ *
2016-01-15 18:24:52 +01:00
// escape text
2016-05-25 15:05:07 +02:00
String escapedFeedText = Utils . escapeHtmlAndTransformUrl ( feedText ) ;
2016-01-15 18:24:52 +01:00
// get the list of hashtags
2016-06-15 17:15:17 +02:00
List < String > hashtags = Utils . getHashTags ( escapedFeedText ) ;
2016-01-15 18:24:52 +01:00
if ( hashtags ! = null & & ! hashtags . isEmpty ( ) )
2016-05-25 15:05:07 +02:00
escapedFeedText = Utils . convertHashtagsAnchorHTML ( escapedFeedText , hashtags ) ;
2016-01-15 18:24:52 +01:00
// get the list of mentioned users
ArrayList < ItemBean > mentionedUsers = null ;
2018-03-18 17:49:46 +01:00
if ( mentionedItems ! = null & & ! mentionedItems . isEmpty ( ) ) {
mentionedUsers = getMentionsBean ( mentionedItems ) ;
2016-05-25 15:05:07 +02:00
escapedFeedText = Utils . convertMentionPeopleAnchorHTML ( escapedFeedText , mentionedUsers , getThreadLocalRequest ( ) ) ;
2016-01-15 18:24:52 +01:00
}
2019-01-23 17:06:35 +01:00
* /
2019-01-24 10:02:45 +01:00
ArrayList < ItemBean > mentionedUsers = new ArrayList < > ( ) ; ;
2019-01-23 17:06:35 +01:00
if ( mentionedItems ! = null & & ! mentionedItems . isEmpty ( ) ) {
mentionedUsers = getMentionsBean ( mentionedItems ) ;
}
2019-01-24 11:55:50 +01:00
SocialMessageParser messageParser = new SocialMessageParser ( feedText ) ;
2019-01-23 17:06:35 +01:00
String siteLandingPagePath = PortalContext . getConfiguration ( ) . getSiteLandingPagePath ( getThreadLocalRequest ( ) ) ;
String escapedFeedText = messageParser . getParsedMessage ( mentionedUsers , siteLandingPagePath ) ;
List < String > hashtags = messageParser . getHashtags ( ) ;
2016-01-15 18:24:52 +01:00
2016-11-14 18:03:55 +01:00
PortalContext context = PortalContext . getConfiguration ( ) ;
GCubeUser currUser = context . getCurrentUser ( getThreadLocalRequest ( ) ) ;
2016-01-15 18:24:52 +01:00
2016-11-14 18:03:55 +01:00
String username = currUser . getUsername ( ) ;
String email = currUser . getEmail ( ) ;
String fullName = currUser . getFullname ( ) ;
2016-11-18 13:03:13 +01:00
String thumbnailAvatarURL = currUser . getUserAvatarURL ( ) ;
2016-01-15 18:24:52 +01:00
2016-01-20 10:36:24 +01:00
// Managing attachments: the first one will use the same fields of a link preview.
2016-01-15 18:24:52 +01:00
// If more than one attachments are present, they will be saved as Attachment objects.
2016-01-29 12:39:02 +01:00
// In this way we can handle backward-compatibility.
2016-01-15 18:24:52 +01:00
List < Attachment > attachments = null ;
String firstAttachmentName = " " ,
firstAttachmentDescription = " " ,
firstAttachmentFormat = " " ,
firstAttachmentDownloadUrl = " " ,
2016-01-20 17:54:20 +01:00
firstAttachmenturlThumbnail = " " ;
2016-01-15 18:24:52 +01:00
if ( uploadedFiles . size ( ) > 0 ) {
2016-01-20 10:36:24 +01:00
// retrieve the first element (and remove it)
2016-01-15 18:24:52 +01:00
UploadedFile firstAttachment = uploadedFiles . get ( 0 ) ;
firstAttachmentName = firstAttachment . getFileName ( ) ;
firstAttachmentDescription = firstAttachment . getDescription ( ) ;
firstAttachmentFormat = firstAttachment . getFormat ( ) ;
firstAttachmentDownloadUrl = firstAttachment . getDownloadUrl ( ) ;
2016-05-25 15:05:07 +02:00
firstAttachmenturlThumbnail = firstAttachment . getThumbnailUrl ( ) ! = null ? firstAttachment . getThumbnailUrl ( ) : firstAttachmenturlThumbnail ;
// check if there are more files
if ( uploadedFiles . size ( ) > 1 ) {
attachments = new ArrayList < > ( ) ;
// starting from 1
for ( int i = 1 ; i < uploadedFiles . size ( ) ; i + + ) {
UploadedFile file = uploadedFiles . get ( i ) ;
attachments . add ( new Attachment (
UUID . randomUUID ( ) . toString ( ) ,
file . getDownloadUrl ( ) ,
file . getFileName ( ) ,
file . getDescription ( ) ,
file . getThumbnailUrl ( ) ,
file . getFormat ( ) )
) ;
}
}
2016-01-15 18:24:52 +01:00
}
// evaluate the date (this will be the date of the post)
Date feedDate = new Date ( ) ;
2014-01-30 16:39:42 +01:00
String textToPost = " " ;
2016-01-29 12:39:02 +01:00
//this means the user has shared a file without text in it.
2014-05-15 18:01:32 +02:00
if ( escapedFeedText . trim ( ) . compareTo ( ShareUpdateForm . NO_TEXT_FILE_SHARE ) = = 0 ) {
2016-01-29 12:39:02 +01:00
if ( uploadedFiles . size ( ) < = 1 ) {
2016-05-25 15:05:07 +02:00
textToPost = Utils . convertFileNameAnchorHTML ( firstAttachmentDownloadUrl ) ;
2016-01-29 12:39:02 +01:00
}
else {
StringBuilder sb = new StringBuilder ( ) ;
textToPost = sb . append ( " <span style= \" color:gray; font-size:12px; \" >shared a set of files.</span> " ) . toString ( ) ;
}
2014-01-30 16:39:42 +01:00
} else {
2014-03-31 19:02:52 +02:00
textToPost = escapedFeedText ;
2014-01-30 16:39:42 +01:00
}
2015-04-27 16:52:29 +02:00
2014-05-09 17:05:42 +02:00
//get the VRE scope if single channel post
String vreScope2Set = " " ;
2016-02-25 19:00:58 +01:00
if ( pLevel = = PrivacyLevel . SINGLE_VRE & & vreOrgId ! = null ) {
2016-11-14 18:03:55 +01:00
vreScope2Set = context . getCurrentScope ( getThreadLocalRequest ( ) ) ;
2014-05-09 17:05:42 +02:00
}
2014-03-22 17:32:46 +01:00
2016-01-15 18:24:52 +01:00
Feed toShare = null ;
boolean result ;
if ( uploadedFiles . size ( ) < = 1 ) {
toShare = new Feed (
UUID . randomUUID ( ) . toString ( ) , feedType , username , feedDate ,
vreScope2Set , firstAttachmentDownloadUrl , firstAttachmenturlThumbnail ,
textToPost , pLevel , fullName , email , thumbnailAvatarURL ,
firstAttachmentName , firstAttachmentDescription , firstAttachmentFormat ) ;
// save the feed itself
result = store . saveUserFeed ( toShare ) ;
}
else {
2013-01-26 20:02:51 +01:00
2016-01-15 18:24:52 +01:00
toShare = new Feed (
UUID . randomUUID ( ) . toString ( ) , feedType , username , feedDate ,
vreScope2Set , firstAttachmentDownloadUrl , firstAttachmenturlThumbnail ,
textToPost , pLevel , fullName , email , thumbnailAvatarURL ,
firstAttachmentName , firstAttachmentDescription , firstAttachmentFormat ) ;
2014-01-30 16:39:42 +01:00
2016-01-15 18:24:52 +01:00
// set the field multiFileUpload to true
toShare . setMultiFileUpload ( true ) ;
// save the feed itself plus the attachments
result = store . saveUserFeed ( toShare , attachments ) ;
}
_log . info ( " Attempting to save Feed with text: " + textToPost + " Level: " + pLevel + " Timeline= " + vreScope2Set ) ;
2015-04-27 16:52:29 +02:00
2013-01-26 20:02:51 +01:00
//need to put the feed into VRES Timeline too
if ( pLevel = = PrivacyLevel . VRES ) {
_log . trace ( " PrivacyLevel was set to VRES attempting to write onto User's VRES Timelines " ) ;
2016-04-27 14:35:28 +02:00
for ( GCubeGroup vre : getUserVREs ( username ) ) {
String vreScope = getScopeByGroupId ( vre . getGroupId ( ) ) ;
2013-01-26 20:02:51 +01:00
_log . trace ( " Attempting to write onto " + vreScope ) ;
try {
store . saveFeedToVRETimeline ( toShare . getKey ( ) , vreScope ) ;
} catch ( FeedIDNotFoundException e ) {
_log . error ( " Error writing onto VRES Time Line " + vreScope ) ;
} //save the feed
_log . trace ( " Success writing onto " + vreScope ) ;
}
} //share on a single VRE Timeline
//receives a VreId(groupId) get the scope from the groupId
2016-02-25 19:00:58 +01:00
else if ( pLevel = = PrivacyLevel . SINGLE_VRE & & vreOrgId ! = null ) {
2014-05-09 17:05:42 +02:00
_log . trace ( " Attempting to write onto " + vreScope2Set ) ;
2013-01-26 20:02:51 +01:00
try {
2014-05-09 17:05:42 +02:00
store . saveFeedToVRETimeline ( toShare . getKey ( ) , vreScope2Set ) ;
2014-10-17 16:29:23 +02:00
if ( hashtags ! = null & & ! hashtags . isEmpty ( ) )
store . saveHashTags ( toShare . getKey ( ) , vreScope2Set , hashtags ) ;
2013-01-26 20:02:51 +01:00
} catch ( FeedIDNotFoundException e ) {
2014-05-09 17:05:42 +02:00
_log . error ( " Error writing onto VRES Time Line " + vreScope2Set ) ;
2013-01-26 20:02:51 +01:00
} //save the feed
2014-05-09 17:05:42 +02:00
_log . trace ( " Success writing onto " + vreScope2Set ) ;
2013-01-26 20:02:51 +01:00
}
if ( ! result ) return null ;
2013-04-18 11:44:10 +02:00
//everything went fine
2013-04-09 17:12:10 +02:00
ClientFeed cf = new ClientFeed ( toShare . getKey ( ) , toShare . getType ( ) . toString ( ) , username , feedDate , toShare . getUri ( ) ,
2017-05-18 12:58:56 +02:00
toShare . getDescription ( ) , fullName , email , thumbnailAvatarURL , toShare . getLinkTitle ( ) , toShare . getLinkDescription ( ) ,
2016-01-18 10:26:36 +01:00
toShare . getUriThumbnail ( ) , toShare . getLinkHost ( ) , attachments ) ;
2014-03-22 17:32:46 +01:00
2016-05-25 15:05:07 +02:00
// check if is needed to notify people in the vre
2017-01-31 15:16:51 +01:00
notifyPeopleGroup ( pLevel , vreOrgId , notifyGroup , username , email , fullName , thumbnailAvatarURL , toShare , hashtags , vreScope2Set , textToPost , mentionedUsers ) ;
2016-05-25 15:05:07 +02:00
//send the notification to the mentioned users
if ( mentionedUsers ! = null & & mentionedUsers . size ( ) > 0 )
notifyMentionedUsers ( vreScope2Set , mentionedUsers , username , email , fullName , thumbnailAvatarURL , toShare , textToPost ) ;
//it means I also should upload a copy of the files on the user's Workspace root folder
if ( saveCopyWokspace )
saveCopyIntoWorkSpace ( fullName , username , uploadedFiles ) ;
return cf ;
}
/ * *
* Check if vre notification must be performed and does it .
* @param pLevel
* @param vreOrgId
* @param notifyGroup
* @param username
* @param email
* @param fullName
* @param thumbnailAvatarURL
* @param toShare
* @param hashtags
* @param vreScope2Set
* @param postText
* /
private void notifyPeopleGroup ( PrivacyLevel pLevel , Long vreOrgId ,
boolean notifyGroup , String username , String email ,
String fullName , String thumbnailAvatarURL , Feed toShare ,
2017-01-31 15:16:51 +01:00
List < String > hashtags , String vreScope2Set , String postText , ArrayList < ItemBean > mentionedUsers ) {
2016-05-25 15:05:07 +02:00
2014-03-05 00:38:20 +01:00
//send the notification about this posts to everyone in the group if notifyGroup is true
2016-02-25 19:00:58 +01:00
if ( pLevel = = PrivacyLevel . SINGLE_VRE & & vreOrgId ! = null & & notifyGroup ) {
2016-05-19 00:28:26 +02:00
NotificationsManager nm = new ApplicationNotificationsManager (
new SocialNetworkingSite ( getThreadLocalRequest ( ) ) ,
2016-02-25 19:00:58 +01:00
vreScope2Set ,
new SocialNetworkingUser ( username , email , fullName , thumbnailAvatarURL ) ,
NEWS_FEED_PORTLET_CLASSNAME ) ;
2017-01-31 15:16:51 +01:00
Thread thread = new Thread ( new PostNotificationsThread ( new LiferayUserManager ( ) , toShare . getKey ( ) , postText , " " + vreOrgId , nm , new HashSet < String > ( hashtags ) , getVreGroupsNames ( mentionedUsers ) ) ) ;
2014-03-05 00:38:20 +01:00
thread . start ( ) ;
2014-03-22 17:32:46 +01:00
2014-03-05 00:38:20 +01:00
}
2016-05-25 15:05:07 +02:00
}
/ * *
* Save copy of the file ( s ) into the workspace
* @param fullName
* @param username
* @param uploadedFiles
* /
private void saveCopyIntoWorkSpace ( String fullName , String username , ArrayList < UploadedFile > uploadedFiles ) {
for ( UploadedFile file : uploadedFiles ) {
new Thread (
new UploadToWorkspaceThread (
fullName ,
username ,
file . getFileName ( ) ,
file . getFileAbsolutePathOnServer ( ) ) )
. start ( ) ;
2013-04-18 11:44:10 +02:00
}
2016-05-25 15:05:07 +02:00
}
2017-03-16 18:14:23 +01:00
2017-01-31 15:16:51 +01:00
/ * *
* Extract mentioned VRE ' s group names
* @return Set < String > of vre groups ' names
* /
private static Set < String > getVreGroupsNames ( ArrayList < ItemBean > mentionedUsers ) {
2017-03-16 18:14:23 +01:00
2017-01-31 15:16:51 +01:00
Set < String > toReturn = new HashSet < String > ( ) ;
RoleManager rManager = new LiferayRoleManager ( ) ;
2017-03-16 18:14:23 +01:00
if ( mentionedUsers ! = null & & ! mentionedUsers . isEmpty ( ) ) {
for ( ItemBean itemBean : mentionedUsers ) {
if ( itemBean . isItemGroup ( ) )
try {
toReturn . add ( rManager . getTeam ( Long . parseLong ( itemBean . getId ( ) ) ) . getTeamName ( ) ) ;
} catch ( NumberFormatException | UserManagementSystemException
| TeamRetrievalFault e ) {
_log . warn ( " Unable to add this team " , e ) ;
}
}
2017-01-31 15:16:51 +01:00
}
2017-03-16 18:14:23 +01:00
2017-01-31 15:16:51 +01:00
return toReturn ;
}
2016-05-25 15:05:07 +02:00
/ * *
* Common method to notify users .
* @param vreScope2Set
* @param mentionedUsers
* @param username
* @param email
* @param fullName
* @param thumbnailAvatarURL
* /
private void notifyMentionedUsers ( String vreScope2Set , ArrayList < ItemBean > mentionedUsers , String username , String email , String fullName , String thumbnailAvatarURL ,
Feed toShare , String escapedFeedText ) {
NotificationsManager nm = new ApplicationNotificationsManager (
new SocialNetworkingSite ( getThreadLocalRequest ( ) ) ,
vreScope2Set ,
new SocialNetworkingUser ( username , email , fullName , thumbnailAvatarURL ) ,
NEWS_FEED_PORTLET_CLASSNAME ) ;
ArrayList < GenericItemBean > toPass = new ArrayList < GenericItemBean > ( ) ;
// among the mentionedUsers there could be groups of people
Map < String , ItemBean > uniqueUsersToNotify = new HashMap < > ( ) ;
UserManager um = new LiferayUserManager ( ) ;
for ( ItemBean bean : mentionedUsers ) {
if ( bean . isItemGroup ( ) ) {
// retrieve the users of this group
try {
List < GCubeUser > teamUsers = um . listUsersByTeam ( Long . parseLong ( bean . getId ( ) ) ) ;
for ( GCubeUser userTeam : teamUsers ) {
if ( ! uniqueUsersToNotify . containsKey ( userTeam . getUsername ( ) ) )
uniqueUsersToNotify . put ( userTeam . getUsername ( ) , new ItemBean ( userTeam . getUserId ( ) + " " ,
userTeam . getUsername ( ) , userTeam . getFullname ( ) , userTeam . getUserAvatarURL ( ) ) ) ;
}
} catch ( NumberFormatException
| UserManagementSystemException
| TeamRetrievalFault | UserRetrievalFault e ) {
_log . error ( " Unable to retrieve team information " , e ) ;
}
} else {
// it is a user, just add to the hashmap
if ( ! uniqueUsersToNotify . containsKey ( bean . getName ( ) ) )
uniqueUsersToNotify . put ( bean . getName ( ) , bean ) ;
2014-03-22 17:32:46 +01:00
2016-01-15 18:24:52 +01:00
}
2014-01-30 16:39:42 +01:00
}
2013-10-03 12:16:57 +02:00
2016-05-25 15:05:07 +02:00
// iterate over the hashmap
Iterator < Entry < String , ItemBean > > userMapIterator = uniqueUsersToNotify . entrySet ( ) . iterator ( ) ;
while ( userMapIterator . hasNext ( ) ) {
Map . Entry < String , ItemBean > user = ( Map . Entry < String , ItemBean > ) userMapIterator
. next ( ) ;
ItemBean userBean = user . getValue ( ) ;
toPass . add ( new GenericItemBean ( userBean . getId ( ) , userBean . getName ( ) , userBean . getAlternativeName ( ) , userBean . getThumbnailURL ( ) ) ) ;
}
2013-04-17 17:29:44 +02:00
2016-05-25 15:05:07 +02:00
Thread thread = new Thread ( new MentionNotificationsThread ( toShare . getKey ( ) , escapedFeedText , nm , null , toPass ) ) ;
thread . start ( ) ;
2013-01-26 20:02:51 +01:00
}
2016-01-15 18:24:52 +01:00
2014-01-30 16:39:42 +01:00
@Override
2013-04-11 18:46:58 +02:00
public UserSettings getUserSettings ( ) {
2013-01-26 20:02:51 +01:00
try {
2016-11-14 18:03:55 +01:00
PortalContext context = PortalContext . getConfiguration ( ) ;
GCubeUser currUser = context . getCurrentUser ( getThreadLocalRequest ( ) ) ;
String username = currUser . getUsername ( ) ;
2016-04-27 14:35:28 +02:00
_log . debug ( " getUserSettings() for " + username ) ;
2016-05-25 15:05:07 +02:00
2016-11-14 18:03:55 +01:00
String thumbnailURL = currUser . getUserAvatarURL ( ) ;
String fullName = currUser . getFullname ( ) ;
String email = currUser . getEmail ( ) ;
final String profilePageURL =
GCubePortalConstants . PREFIX_GROUP_URL +
PortalContext . getConfiguration ( ) . getSiteLandingPagePath ( getThreadLocalRequest ( ) ) +
GCubePortalConstants . USER_PROFILE_FRIENDLY_URL ;
String accountURL = profilePageURL ;
try {
accountURL = " " ;
} catch ( NullPointerException e ) {
e . printStackTrace ( ) ;
2013-01-26 20:02:51 +01:00
}
2016-11-14 18:03:55 +01:00
HashMap < String , String > vreNames = getUserVreNames ( username ) ;
UserInfo userInfo = new UserInfo ( username , fullName , thumbnailURL , email , accountURL , true , isAdmin ( ) , vreNames ) ;
UserSettings toReturn = new UserSettings ( userInfo , 0 , context . getCurrentScope ( getThreadLocalRequest ( ) ) , isInfrastructureScope ( ) , isNotificationViaEmailEnabled ( ) ) ;
_log . debug ( " getUserSettings() return " + toReturn ) ;
return toReturn ;
2013-04-17 17:29:44 +02:00
2013-01-26 20:02:51 +01:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2013-04-11 18:46:58 +02:00
return new UserSettings ( ) ;
2013-01-26 20:02:51 +01:00
}
2016-01-15 18:24:52 +01:00
2016-11-14 18:03:55 +01:00
private boolean isNotificationViaEmailEnabled ( ) throws UserManagementSystemException , GroupRetrievalFault {
if ( ! isWithinPortal ( ) )
return false ;
PortalContext context = PortalContext . getConfiguration ( ) ;
2016-04-27 14:35:28 +02:00
GroupManager gm = new LiferayGroupManager ( ) ;
2016-11-14 18:03:55 +01:00
return ( Boolean ) gm . readCustomAttr ( context . getCurrentGroupId ( getThreadLocalRequest ( ) ) , CustomAttributeKeys . POST_NOTIFICATION . getKeyName ( ) ) ;
2016-04-27 14:35:28 +02:00
}
/ * *
* tell if the user is a portal administrator or not
* @param username
* @return true if is admin
* @throws SystemException
* @throws PortalException
* /
private boolean isAdmin ( ) throws PortalException , SystemException {
if ( ! isWithinPortal ( ) )
return false ;
try {
2016-11-14 18:03:55 +01:00
PortalContext context = PortalContext . getConfiguration ( ) ;
GCubeUser currUser = context . getCurrentUser ( getThreadLocalRequest ( ) ) ;
return new LiferayRoleManager ( ) . isAdmin ( currUser . getUserId ( ) ) ;
2016-04-27 14:35:28 +02:00
}
catch ( Exception e ) {
_log . error ( " Could not check if the user is an Administrator, returning false " ) ;
return false ;
}
2015-07-06 15:11:58 +02:00
}
2017-03-16 18:14:23 +01:00
2014-01-17 19:21:35 +01:00
/ * *
* generate a preview of the file , upload the file on the storage and shorts the link
* /
@Override
public LinkPreview checkUploadedFile ( String fileName , String fileabsolutePathOnServer ) {
2016-11-15 18:48:54 +01:00
2016-11-21 12:22:18 +01:00
PortalContext context = PortalContext . getConfiguration ( ) ;
2014-01-21 18:43:03 +01:00
LinkPreview toReturn = null ;
String randomUploadFolderName = UUID . randomUUID ( ) . toString ( ) ;
String remoteFilePath = UPLOAD_DIR + " / " + randomUploadFolderName + " / " + fileName ;
2014-01-27 19:08:15 +01:00
//get the Storage Client
2016-11-21 12:22:18 +01:00
String currScope = ScopeProvider . instance . get ( ) ;
ScopeProvider . instance . set ( context . getCurrentScope ( getThreadLocalRequest ( ) ) ) ;
IClient storageClient = new StorageClient ( STORAGE_OWNER , AccessType . SHARED , MemoryType . PERSISTENT ) . getClient ( ) ;
ScopeProvider . instance . set ( currScope ) ;
2014-03-22 17:32:46 +01:00
2014-01-21 18:43:03 +01:00
String httpURL = " " ;
2015-04-27 16:52:29 +02:00
String smpURI = " " ;
2016-01-20 17:54:20 +01:00
String mimeType = null ;
2016-01-18 10:26:36 +01:00
if ( isWithinPortal ( ) ) {
//get the url to show, before actually uploading it
2016-01-20 17:54:20 +01:00
//smpURI = storageClient.getUrl(true).RFile(remoteFilePath); //"http://ciccio.com";
smpURI = storageClient . getHttpUrl ( true ) . RFile ( remoteFilePath ) ;
2016-01-18 10:26:36 +01:00
2016-11-15 18:48:54 +01:00
//The storage uploader Thread starts here asyncronously
2016-01-20 17:54:20 +01:00
try {
mimeType = FilePreviewer . getMimeType ( new File ( fileabsolutePathOnServer ) , fileName ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
Thread thread = new Thread ( new UploadToStorageThread ( storageClient , fileName , fileabsolutePathOnServer , remoteFilePath , mimeType ) ) ;
2016-01-18 10:26:36 +01:00
thread . start ( ) ;
2016-11-21 12:22:18 +01:00
}
2014-03-22 17:32:46 +01:00
2016-11-21 12:22:18 +01:00
try {
//get the url to show (though it could not be ready for download at this stage)
httpURL = smpURI ;
switch ( mimeType ) {
case " application/pdf " :
2017-01-16 18:22:39 +01:00
toReturn = FilePreviewer . getPdfPreview ( fileName , fileabsolutePathOnServer , httpURL , mimeType , context . getCurrentScope ( getThreadLocalRequest ( ) ) ) ;
2016-11-21 12:22:18 +01:00
break ;
case " application/vnd.openxmlformats-officedocument.wordprocessingml.document " :
mimeType = " application/wordprocessor " ;
2017-01-16 18:22:39 +01:00
return FilePreviewer . getUnhandledTypePreview ( fileName , fileabsolutePathOnServer , httpURL , mimeType , context . getCurrentScope ( getThreadLocalRequest ( ) ) ) ;
2016-11-21 12:22:18 +01:00
case " application/vnd.openxmlformats-officedocument.spreadsheetml.sheet " :
mimeType = " application/spreadsheet " ;
2017-01-16 18:22:39 +01:00
return FilePreviewer . getUnhandledTypePreview ( fileName , fileabsolutePathOnServer , httpURL , mimeType , context . getCurrentScope ( getThreadLocalRequest ( ) ) ) ;
2016-11-21 12:22:18 +01:00
case " application/vnd.openxmlformats-officedocument.presentationml.presentation " :
mimeType = " application/presentation " ;
2017-01-16 18:22:39 +01:00
return FilePreviewer . getUnhandledTypePreview ( fileName , fileabsolutePathOnServer , httpURL , mimeType , context . getCurrentScope ( getThreadLocalRequest ( ) ) ) ;
2016-11-21 12:22:18 +01:00
case " image/png " :
case " image/gif " :
case " image/tiff " :
case " image/jpg " :
case " image/jpeg " :
case " image/bmp " :
2017-01-16 18:22:39 +01:00
toReturn = FilePreviewer . getImagePreview ( fileName , fileabsolutePathOnServer , httpURL , mimeType , context . getCurrentScope ( getThreadLocalRequest ( ) ) ) ;
2016-11-21 12:22:18 +01:00
break ;
default :
2017-01-16 18:22:39 +01:00
return FilePreviewer . getUnhandledTypePreview ( fileName , fileabsolutePathOnServer , httpURL , mimeType , context . getCurrentScope ( getThreadLocalRequest ( ) ) ) ;
2016-11-21 12:22:18 +01:00
}
} catch ( Exception e ) {
_log . error ( " Error while resolving or previewing file " ) ;
e . printStackTrace ( ) ;
2015-04-27 16:52:29 +02:00
try {
2017-01-16 18:22:39 +01:00
return FilePreviewer . getUnhandledTypePreview ( fileName , fileabsolutePathOnServer , httpURL , " Error During upload on Server! " , context . getCurrentScope ( getThreadLocalRequest ( ) ) ) ;
2016-11-21 12:22:18 +01:00
} catch ( Exception e1 ) {
e1 . printStackTrace ( ) ;
}
}
2014-01-21 18:43:03 +01:00
_log . debug ( " smpURI= " + smpURI ) ;
_log . debug ( " Returning httpURL= " + httpURL ) ;
return toReturn ;
2014-01-20 19:35:57 +01:00
}
2017-03-16 18:14:23 +01:00
2019-01-25 11:01:29 +01:00
protected String findFirstLink ( String message ) {
try {
SocialMessageParser messageParser = new SocialMessageParser ( message ) ;
List < URLToken > urlTokens = messageParser . getURLTokens ( ) ;
return urlTokens . get ( 0 ) . getExtractedURL ( ) . toString ( ) ;
} catch ( Exception e ) {
return null ;
}
}
2013-01-26 20:02:51 +01:00
/ * *
* tries the following in the indicated order for Populating the Link preview
* Open Graph protocol
* Meta " title " and " description " tags
* Best guess from page content ( not recommended )
* /
2016-05-25 15:05:07 +02:00
@SuppressWarnings ( " restriction " )
2014-04-01 15:00:37 +02:00
@Override
2013-01-26 20:02:51 +01:00
public LinkPreview checkLink ( String linkToCheck ) {
LinkPreview toReturn = null ;
_log . info ( " to check " + linkToCheck ) ;
//look for a url in text
2019-01-25 11:01:29 +01:00
linkToCheck = findFirstLink ( linkToCheck ) ;
2019-01-25 10:11:52 +01:00
if ( linkToCheck = = null )
2013-01-26 20:02:51 +01:00
return null ; //no url
2019-01-25 10:11:52 +01:00
2013-01-26 20:02:51 +01:00
String [ ] schemes = { " http " , " https " } ;
UrlValidator urlValidator = new UrlValidator ( schemes ) ;
if ( ! urlValidator . isValid ( linkToCheck ) ) {
_log . warn ( " url is NOT valid, returning nothing " ) ;
return null ;
}
_log . debug ( " url is valid " ) ;
URL pageURL ;
URLConnection siteConnection = null ;
try {
2016-04-27 14:35:28 +02:00
pageURL = new URL ( null , linkToCheck , new sun . net . www . protocol . https . Handler ( ) ) ;
2013-01-26 20:02:51 +01:00
if ( pageURL . getProtocol ( ) . equalsIgnoreCase ( " https " ) ) {
System . setProperty ( " java.protocol.handler.pkgs " , " com.sun.net.ssl.internal.www.protocol " ) ;
java . security . Security . addProvider ( new com . sun . net . ssl . internal . ssl . Provider ( ) ) ;
2014-04-01 15:00:37 +02:00
TextTransfromUtils . trustAllHTTPSConnections ( ) ;
2013-01-26 20:02:51 +01:00
siteConnection = ( HttpsURLConnection ) pageURL . openConnection ( ) ;
}
2016-04-27 14:35:28 +02:00
else {
pageURL = new URL ( linkToCheck ) ;
2013-01-26 20:02:51 +01:00
siteConnection = ( HttpURLConnection ) pageURL . openConnection ( ) ;
2016-04-27 14:35:28 +02:00
}
2013-01-26 20:02:51 +01:00
} catch ( MalformedURLException e ) {
_log . error ( " url is not valid " ) ;
return null ;
} catch ( IOException e ) {
_log . error ( " url is not reachable " ) ;
return null ;
}
2014-03-22 17:32:46 +01:00
//pretend you're a browser (make my request from Java more “browsery-like”.)
siteConnection . addRequestProperty ( " User-Agent " , " Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11 " ) ;
2013-01-26 20:02:51 +01:00
String title ;
String description ;
ArrayList < String > imageUrls = new ArrayList < String > ( ) ;
//get the host from the url
String host = pageURL . getHost ( ) . replaceAll ( " www. " , " " ) ;
//try openGraph First
OpenGraph ogLink = null ;
try {
ogLink = new OpenGraph ( linkToCheck , true , siteConnection ) ;
if ( ogLink = = null | | ogLink . getContent ( " title " ) = = null ) {
//there is no OpenGraph for this link
_log . info ( " No OpenGraph Found, going Best guess from page content " ) ;
2014-04-01 15:00:37 +02:00
toReturn = TextTransfromUtils . getInfoFromHTML ( siteConnection , pageURL , linkToCheck , host ) ;
2013-01-26 20:02:51 +01:00
} else {
//there is OpenGraph
2014-03-31 11:18:22 +02:00
_log . info ( " OpenGraph Found " ) ;
2013-01-26 20:02:51 +01:00
title = ogLink . getContent ( " title " ) ;
description = ( ogLink . getContent ( " description " ) ! = null ) ? ogLink . getContent ( " description " ) : " " ;
description = ( ( description . length ( ) > 256 ) ? description . substring ( 0 , 256 ) + " ... " : description ) ;
2013-11-06 16:09:30 +01:00
//look for the image ask the guesser if not present
2014-03-31 11:18:22 +02:00
if ( ogLink . getContent ( " image " ) ! = null ) {
2016-10-06 14:49:55 +02:00
String imageUrl = TextTransfromUtils . getImageUrlFromSrcAttribute ( ogLink . getRealURL ( ) , ogLink . getContent ( " image " ) ) ;
2014-03-31 11:18:22 +02:00
imageUrls . add ( imageUrl ) ;
_log . trace ( " OpenGraph getImage = " + imageUrl ) ;
}
2013-01-26 20:02:51 +01:00
else {
2014-03-31 11:18:22 +02:00
_log . trace ( " OpenGraph No Image, trying manuale parsing " ) ;
2014-04-01 15:00:37 +02:00
ArrayList < String > images = TextTransfromUtils . getImagesWithCleaner ( pageURL ) ;
2013-01-26 20:02:51 +01:00
if ( ! images . isEmpty ( ) )
imageUrls = images ;
}
toReturn = new LinkPreview ( title , description , linkToCheck , host , imageUrls ) ;
return toReturn ;
}
} catch ( IOException e ) {
e . printStackTrace ( ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
return toReturn ;
}
2015-04-27 16:52:29 +02:00
2013-01-26 20:02:51 +01:00
/ * *
2014-04-01 15:00:37 +02:00
* return the id as key and the names as value of the vre a user is subscribed to
* @param username
* @return the id as key and the names as value of the vre a user is subscribed to
2013-01-26 20:02:51 +01:00
* /
2014-04-01 15:00:37 +02:00
private HashMap < String , String > getUserVreNames ( String username ) {
2017-04-13 17:08:32 +02:00
HashMap < String , String > toReturn = new HashMap < String , String > ( ) ;
2016-11-14 18:03:55 +01:00
PortalContext context = PortalContext . getConfiguration ( ) ;
String groupName = context . getCurrentGroupName ( getThreadLocalRequest ( ) ) ;
2018-03-18 17:49:46 +01:00
2017-04-13 17:08:32 +02:00
//for dev
if ( ! isWithinPortal ( ) ) {
toReturn . put ( " " + context . getCurrentGroupId ( getThreadLocalRequest ( ) ) , groupName ) ;
return toReturn ;
}
2018-03-18 17:49:46 +01:00
2016-04-27 14:35:28 +02:00
for ( GCubeGroup vre : getUserVREs ( username ) ) {
2016-11-14 18:03:55 +01:00
if ( vre . getGroupName ( ) . compareTo ( groupName ) = = 0 )
2016-04-27 14:35:28 +02:00
toReturn . put ( vre . getGroupId ( ) + " " , vre . getGroupName ( ) ) ;
2015-04-27 16:52:29 +02:00
}
2014-03-22 17:32:46 +01:00
return toReturn ;
}
2013-01-26 20:02:51 +01:00
/ * *
2014-04-01 15:00:37 +02:00
*
* @param username
* @return
2013-01-26 20:02:51 +01:00
* /
2016-04-27 14:35:28 +02:00
private ArrayList < GCubeGroup > getUserVREs ( String username ) {
ArrayList < GCubeGroup > toReturn = new ArrayList < GCubeGroup > ( ) ;
GCubeUser currUser ;
2014-04-01 15:00:37 +02:00
try {
GroupManager gm = new LiferayGroupManager ( ) ;
2016-04-27 14:35:28 +02:00
currUser = new LiferayUserManager ( ) . getUserByUsername ( username ) ;
for ( GCubeGroup group : gm . listGroupsByUser ( currUser . getUserId ( ) ) )
if ( gm . isVRE ( group . getGroupId ( ) ) ) {
toReturn . add ( group ) ;
2014-04-01 15:00:37 +02:00
}
} catch ( Exception e ) {
_log . error ( " Failed reading User VREs for : " + username ) ;
e . printStackTrace ( ) ;
return toReturn ;
}
return toReturn ;
2013-01-26 20:02:51 +01:00
}
2014-04-01 15:00:37 +02:00
2016-04-27 14:35:28 +02:00
private String getScopeByGroupId ( Long vreGroupId ) {
2014-04-01 15:00:37 +02:00
try {
2016-04-27 14:35:28 +02:00
return new LiferayGroupManager ( ) . getInfrastructureScope ( vreGroupId ) ;
2014-04-01 15:00:37 +02:00
} catch ( Exception e ) {
2016-04-27 14:35:28 +02:00
_log . error ( " Could not find a scope for this vreGroupId: " + vreGroupId ) ;
2014-04-01 15:00:37 +02:00
return null ;
}
2013-01-26 20:02:51 +01:00
}
2014-04-01 15:00:37 +02:00
2013-04-11 18:46:58 +02:00
/ * *
* Indicates whether the scope is the whole infrastructure .
* @return < code > true < / code > if it is , < code > false < / code > otherwise .
* /
private boolean isInfrastructureScope ( ) {
2016-11-14 18:03:55 +01:00
PortalContext context = PortalContext . getConfiguration ( ) ;
ScopeBean scope = new ScopeBean ( context . getCurrentScope ( getThreadLocalRequest ( ) ) ) ;
2013-09-26 15:44:21 +02:00
return scope . is ( Type . INFRASTRUCTURE ) ;
2013-04-11 18:46:58 +02:00
}
2013-04-16 23:49:16 +02:00
2018-03-18 17:49:46 +01:00
public ArrayList < ItemBean > getMentionsBean ( ArrayList < MentionedDTO > mentions ) {
ArrayList < ItemBean > toReturn = new ArrayList < ItemBean > ( ) ;
if ( mentions = = null )
return toReturn ;
UserManager um = new LiferayUserManager ( ) ;
RoleManager rm = new LiferayRoleManager ( ) ;
try {
for ( MentionedDTO mentioned : mentions ) {
2019-01-04 17:41:49 +01:00
if ( mentioned . id ! = null & & mentioned . id . compareTo ( " " ) ! = 0 ) { //patch temporarely incident #13096
if ( mentioned . getType ( ) . equalsIgnoreCase ( " user " ) ) {
long userId = Long . parseLong ( mentioned . id ) ;
GCubeUser user = um . getUserById ( userId ) ;
toReturn . add ( new ItemBean ( user . getUserId ( ) + " " , user . getUsername ( ) , user . getFullname ( ) , user . getUserAvatarURL ( ) ) ) ;
}
else { //is a team
long teamId = Long . parseLong ( mentioned . id ) ;
GCubeTeam gCubeTeam = rm . getTeam ( teamId ) ;
toReturn . add ( new ItemBean ( gCubeTeam . getTeamId ( ) + " " , gCubeTeam . getTeamName ( ) ) ) ;
}
} else {
_log . warn ( " getMentionsBean found empty mentioned.id for " + mentioned . toString ( ) ) ;
2018-03-18 17:49:46 +01:00
}
}
} catch ( Exception e ) {
_log . error ( " getMentionsBean Error: " , e ) ;
}
return toReturn ;
}
2014-10-02 19:47:15 +02:00
public ArrayList < ItemBean > getSelectedUserIds ( ArrayList < String > fullNames ) {
2013-04-17 17:29:44 +02:00
if ( fullNames = = null )
2014-10-02 19:47:15 +02:00
return new ArrayList < ItemBean > ( ) ;
2013-04-17 17:29:44 +02:00
else {
2016-05-25 15:05:07 +02:00
ArrayList < ItemBean > allbeans = getPortalItemBeans ( ) ;
2014-10-02 19:47:15 +02:00
ArrayList < ItemBean > toReturn = new ArrayList < ItemBean > ( ) ;
2013-04-17 17:29:44 +02:00
for ( String fullName : fullNames )
2016-05-25 15:05:07 +02:00
for ( ItemBean puser : allbeans ) {
2014-10-02 19:47:15 +02:00
if ( puser . getAlternativeName ( ) . compareTo ( fullName ) = = 0 ) {
2013-07-10 17:39:14 +02:00
toReturn . add ( puser ) ;
2013-04-17 17:29:44 +02:00
break ;
}
}
return toReturn ;
}
}
2013-04-16 23:49:16 +02:00
@Override
2016-05-25 15:05:07 +02:00
public ArrayList < ItemBean > getPortalItemBeans ( ) {
2016-11-14 18:03:55 +01:00
PortalContext context = PortalContext . getConfiguration ( ) ;
context . getCurrentScope ( getThreadLocalRequest ( ) ) ;
String scope = context . getCurrentScope ( getThreadLocalRequest ( ) ) ;
String username = context . getCurrentUser ( getThreadLocalRequest ( ) ) . getUsername ( ) ;
2014-05-09 17:05:42 +02:00
boolean withinPortal = false ;
2016-11-14 18:03:55 +01:00
if ( isWithinPortal ( ) & & username . compareTo ( TEST_USER ) ! = 0 ) {
2014-05-09 17:05:42 +02:00
withinPortal = true ;
}
2016-05-25 15:05:07 +02:00
// retrieve user and group beans
2016-11-14 18:03:55 +01:00
return Utils . getDisplayableItemBeans ( scope , username , withinPortal ) ;
2014-04-10 17:56:29 +02:00
}
2015-04-27 16:52:29 +02:00
2014-10-02 19:47:15 +02:00
@Override
public ArrayList < ItemBean > getHashtags ( ) {
2016-11-14 18:03:55 +01:00
PortalContext context = PortalContext . getConfiguration ( ) ;
context . getCurrentScope ( getThreadLocalRequest ( ) ) ;
String scope = context . getCurrentScope ( getThreadLocalRequest ( ) ) ;
2014-10-02 19:47:15 +02:00
_log . error ( " getting hashtags for " + scope ) ;
Map < String , Integer > map = store . getVREHashtagsWithOccurrence ( scope ) ;
ArrayList < HashTagAndOccurrence > toSort = new ArrayList < HashTagAndOccurrence > ( ) ;
_log . trace ( " Got " + map . keySet ( ) . size ( ) + " hashtags " ) ;
for ( String hashtag : map . keySet ( ) ) {
toSort . add ( new HashTagAndOccurrence ( hashtag , map . get ( hashtag ) ) ) ;
}
Collections . sort ( toSort , Collections . reverseOrder ( ) ) ;
ArrayList < ItemBean > toReturn = new ArrayList < > ( ) ;
for ( HashTagAndOccurrence wrapper : toSort ) {
2014-10-06 14:16:44 +02:00
String hashtag = wrapper . getHashtag ( ) ;
2014-10-02 19:47:15 +02:00
toReturn . add ( new ItemBean ( hashtag , hashtag , hashtag , null ) ) ;
}
return toReturn ;
}
2013-01-26 20:02:51 +01:00
}