2015-07-08 18:47:36 +02:00
package org.gcube.portal.socialmail ;
import java.security.GeneralSecurityException ;
2015-07-09 16:56:01 +02:00
import java.util.ArrayList ;
2015-07-08 18:47:36 +02:00
import java.util.Date ;
2015-10-14 17:47:59 +02:00
import java.util.List ;
2015-07-08 18:47:36 +02:00
import java.util.Properties ;
import java.util.UUID ;
import javax.mail.Address ;
import javax.mail.BodyPart ;
import javax.mail.Flags ;
import javax.mail.Folder ;
import javax.mail.Message ;
import javax.mail.MessagingException ;
import javax.mail.Multipart ;
import javax.mail.NoSuchProviderException ;
import javax.mail.Session ;
import javax.mail.Store ;
import javax.mail.internet.InternetAddress ;
import javax.mail.internet.MimeMessage.RecipientType ;
2016-05-19 00:12:38 +02:00
import javax.servlet.http.HttpServletRequest ;
2015-07-08 18:47:36 +02:00
import org.gcube.application.framework.core.session.ASLSession ;
import org.gcube.application.framework.core.session.SessionManager ;
import org.gcube.application.framework.core.util.GenderType ;
2015-07-09 16:56:01 +02:00
import org.gcube.applicationsupportlayer.social.ApplicationNotificationsManager ;
import org.gcube.applicationsupportlayer.social.NotificationsManager ;
2015-10-14 17:47:59 +02:00
import org.gcube.applicationsupportlayer.social.mailing.AppType ;
2016-05-19 00:12:38 +02:00
import org.gcube.applicationsupportlayer.social.mailing.SocialMailingUtil ;
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingSite ;
2016-02-25 18:46:38 +01:00
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingUser ;
2015-10-14 17:47:59 +02:00
import org.gcube.common.homelibrary.home.HomeLibrary ;
import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException ;
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException ;
import org.gcube.common.homelibrary.home.workspace.Workspace ;
import org.gcube.common.homelibrary.home.workspace.exceptions.ItemNotFoundException ;
import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException ;
import org.gcube.common.homelibrary.home.workspace.sharing.WorkspaceMessage ;
import org.gcube.common.homelibrary.home.workspace.sharing.WorkspaceMessageManager ;
2015-07-08 18:47:36 +02:00
import org.gcube.common.portal.PortalContext ;
import org.gcube.portal.databook.server.DatabookStore ;
import org.gcube.portal.databook.shared.Comment ;
2015-07-09 16:56:01 +02:00
import org.gcube.portal.databook.shared.Feed ;
import org.gcube.portal.databook.shared.Like ;
import org.gcube.portal.databook.shared.ex.ColumnNameNotFoundException ;
2015-07-08 18:47:36 +02:00
import org.gcube.portal.databook.shared.ex.FeedIDNotFoundException ;
2015-07-09 16:56:01 +02:00
import org.gcube.portal.databook.shared.ex.FeedTypeNotFoundException ;
import org.gcube.portal.databook.shared.ex.PrivacyLevelTypeNotFoundException ;
2015-10-14 17:47:59 +02:00
import org.gcube.portal.notifications.bean.GenericItemBean ;
2015-07-09 16:56:01 +02:00
import org.gcube.portal.notifications.thread.CommentNotificationsThread ;
import org.gcube.portal.notifications.thread.LikeNotificationsThread ;
2015-10-14 17:47:59 +02:00
import org.gcube.portal.notifications.thread.MessageNotificationsThread ;
2016-05-19 00:12:38 +02:00
import org.gcube.vomanagement.usermanagement.UserManager ;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager ;
import org.gcube.vomanagement.usermanagement.model.GCubeUser ;
2015-07-08 18:47:36 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import com.sun.mail.util.MailSSLSocketFactory ;
/ * *
*
* @author Massimiliano Assante , CNR - ISTI
*
* /
public class PeriodicTask implements Runnable {
private static final Logger _log = LoggerFactory . getLogger ( PeriodicTask . class ) ;
2016-05-19 11:32:09 +02:00
private static final String APP_ID_NEWSFEED = " org.gcube.portlets.user.newsfeed.server.NewsServiceImpl " ;
2015-07-09 16:56:01 +02:00
2015-07-08 18:47:36 +02:00
private DatabookStore socialStore ;
2016-05-19 00:12:38 +02:00
private EmailPopAccount popAccount ;
private SocialNetworkingSite site ;
2015-07-08 18:47:36 +02:00
2016-05-19 00:12:38 +02:00
public PeriodicTask ( DatabookStore store , EmailPopAccount popAccount , HttpServletRequest request ) {
2015-07-08 18:47:36 +02:00
super ( ) ;
this . socialStore = store ;
2016-05-19 00:12:38 +02:00
this . popAccount = popAccount ;
this . site = new SocialNetworkingSite ( request ) ;
String serverName = request . getServerName ( ) ;
//needed because the servlet is called via http
this . site . setSiteURL ( " https:// " + serverName ) ;
_log . debug ( " PeriodicTask instanciated, request serverName= " + serverName ) ;
2015-07-08 18:47:36 +02:00
}
@Override
public void run ( ) {
2016-05-19 00:12:38 +02:00
check ( popAccount . getPortalName ( ) , popAccount . getPop3Server ( ) , popAccount . getPop3user ( ) , popAccount . getPop3password ( ) ) ;
2015-07-08 18:47:36 +02:00
}
/ * *
* @return a fake session usuful for Notifications
* /
private ASLSession getFakeASLSession ( String emailAddress ) {
String sessionID = UUID . randomUUID ( ) . toString ( ) ;
PortalContext context = PortalContext . getConfiguration ( ) ;
String scope = " / " + context . getInfrastructureName ( ) ;
String username = " " ;
try {
2016-05-19 00:12:38 +02:00
UserManager um = new LiferayUserManager ( ) ;
GCubeUser user = um . getUserByEmail ( emailAddress ) ;
username = user . getUsername ( ) ;
2015-07-08 18:47:36 +02:00
SessionManager . getInstance ( ) . getASLSession ( sessionID , username ) . setScope ( scope ) ;
2015-07-09 16:56:01 +02:00
2015-07-08 18:47:36 +02:00
//add the social information needed by apps
String fullName = user . getFirstName ( ) + " " + user . getLastName ( ) ;
2016-05-19 00:12:38 +02:00
String email = user . getEmail ( ) ;
String thumbnailURL = user . getUserAvatarURL ( ) ;
2015-07-08 18:47:36 +02:00
boolean isMale = user . isMale ( ) ;
SessionManager . getInstance ( ) . getASLSession ( sessionID , username ) . setUserFullName ( fullName ) ;
SessionManager . getInstance ( ) . getASLSession ( sessionID , username ) . setUserEmailAddress ( email ) ;
SessionManager . getInstance ( ) . getASLSession ( sessionID , username ) . setUserAvatarId ( thumbnailURL ) ;
SessionManager . getInstance ( ) . getASLSession ( sessionID , username ) . setUserGender ( isMale ? GenderType . MALE : GenderType . FEMALE ) ;
2015-07-15 18:41:02 +02:00
2015-07-09 16:56:01 +02:00
_log . debug ( " Created fakesession for user " + username + " email= " + emailAddress ) ;
2016-05-19 00:12:38 +02:00
} catch ( Exception e ) {
2015-07-22 12:41:44 +02:00
_log . error ( " Exception while trying to get the user from email address: " + e . getMessage ( ) ) ;
2015-08-05 16:07:01 +02:00
return null ;
2015-07-08 18:47:36 +02:00
}
2015-07-09 16:56:01 +02:00
2015-07-08 18:47:36 +02:00
return SessionManager . getInstance ( ) . getASLSession ( sessionID , username ) ;
}
2015-08-05 16:07:01 +02:00
2015-07-08 18:47:36 +02:00
public void check ( String portalName , String host , String user , String password ) {
try {
MailSSLSocketFactory sf = null ;
try {
sf = new MailSSLSocketFactory ( ) ;
}
catch ( GeneralSecurityException e ) {
e . printStackTrace ( ) ;
}
sf . setTrustAllHosts ( true ) ;
Properties pop3Props = new Properties ( ) ;
pop3Props . setProperty ( " mail.pop3.ssl.enable " , " true " ) ;
pop3Props . setProperty ( " mail.protocol.ssl.trust " , host ) ;
pop3Props . put ( " mail.pop3s.ssl.socketFactory " , sf ) ;
pop3Props . setProperty ( " mail.pop3s.port " , " 995 " ) ;
Session emailSession = Session . getDefaultInstance ( pop3Props ) ;
2015-07-09 16:56:01 +02:00
emailSession . setDebug ( false ) ;
2015-07-08 18:47:36 +02:00
//create the POP3 socialStore object and connect with the pop server
Store store = emailSession . getStore ( " pop3s " ) ;
2015-07-17 12:32:12 +02:00
_log . debug ( " Trying to connect to " + host + " , user= " + user + " passwd (first 3 char)= " + password . substring ( 0 , 3 ) + " ****** " ) ;
2015-07-08 18:47:36 +02:00
store . connect ( host , user , password ) ;
//create the folder object and open it
Folder emailFolder = store . getFolder ( " INBOX " ) ;
emailFolder . open ( Folder . READ_WRITE ) ;
// retrieve the messages from the folder in an array and print it
Message [ ] messages = emailFolder . getMessages ( ) ;
_log . debug ( " Found " + messages . length + " new messages ... " ) ;
for ( int i = 0 , n = messages . length ; i < n ; i + + ) {
Message message = messages [ i ] ;
2015-07-21 16:03:05 +02:00
_log . info ( " --------------- FOUND EMAIL ------------------ " ) ;
2015-07-08 18:47:36 +02:00
String subject = message . getSubject ( ) ;
2015-08-05 16:07:01 +02:00
_log . info ( " Parsing email of " + message . getFrom ( ) [ 0 ] + " with subject: " + subject ) ;
2015-08-24 11:59:29 +02:00
if ( isValidReply ( message ) ) {
2015-10-14 17:47:59 +02:00
String subAddressField = extractSubaddress ( message ) ;
2015-08-05 16:07:01 +02:00
Address [ ] froms = message . getFrom ( ) ;
String emailAddress = froms = = null ? null : ( ( InternetAddress ) froms [ 0 ] ) . getAddress ( ) ;
ASLSession fakeSession = getFakeASLSession ( emailAddress ) ;
2015-10-14 17:47:59 +02:00
2015-08-05 16:07:01 +02:00
if ( fakeSession ! = null ) {
2015-10-14 17:47:59 +02:00
if ( subAddressField . endsWith ( AppType . POST . toString ( ) ) | | subAddressField . endsWith ( AppType . POST . toString ( ) . toLowerCase ( ) ) ) { //it is a post, a comment on a post or a mention
_log . debug ( " Looks like a post, a comment on a post or a mention to me " ) ;
String feedId = extractIdentifier ( subAddressField ) ;
handlePostReply ( portalName , feedId , message , fakeSession ) ;
}
else if ( subAddressField . endsWith ( AppType . MSG . toString ( ) ) | | subAddressField . endsWith ( AppType . MSG . toString ( ) . toLowerCase ( ) ) ) { //it is a message
_log . debug ( " Looks like a message reply to me " ) ;
String messageId = extractIdentifier ( subAddressField ) ;
handleMessageReply ( portalName , messageId , message , fakeSession ) ;
} else {
_log . warn ( " cannot identify the type of this email reply from " + message . getFrom ( ) [ 0 ] + " with subject: " + subject ) ;
2015-08-05 16:07:01 +02:00
}
2015-10-14 17:47:59 +02:00
}
else {
2015-08-05 16:07:01 +02:00
_log . warn ( " User Not Recognized, going to discard Message from emailAddress = " + emailAddress ) ;
}
2015-07-08 18:47:36 +02:00
}
2015-07-15 18:41:02 +02:00
else {
2015-08-24 11:59:29 +02:00
_log . warn ( " Message is not a valid reply, going to discard Message with subject = " + subject ) ;
2015-07-08 18:47:36 +02:00
}
2015-07-09 16:56:01 +02:00
//delete this message
2015-07-08 18:47:36 +02:00
message . setFlag ( Flags . Flag . DELETED , true ) ;
2015-07-15 18:41:02 +02:00
_log . debug ( " Marked DELETE for message: " + subject ) ;
2015-07-08 18:47:36 +02:00
}
//close the socialStore and folder objects
emailFolder . close ( true ) ;
store . close ( ) ;
} catch ( NoSuchProviderException e ) {
e . printStackTrace ( ) ;
} catch ( MessagingException e ) {
e . printStackTrace ( ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
2015-10-14 17:47:59 +02:00
/ * *
* this method manages the replies coming from message notifications
* @param portalName
* @param messageId the message identifier in the System managing the messages ( currently in the Home Library )
* @param message the javax mail Message instance
* @param fakeSession
* @throws Exception
* /
private void handleMessageReply ( String portalName , String messageId , Message message , ASLSession fakeSession ) {
String subject = " " ;
String messageText = " " ;
try {
subject = message . getSubject ( ) ;
messageText = extractText ( portalName , messageId , message ) ;
} catch ( Exception e1 ) {
e1 . printStackTrace ( ) ;
}
_log . debug ( " Found message reply, subject: " + subject , " body: " + messageText ) ;
String newMessageId = null ;
Workspace workspace ;
List < String > recipientIds = null ;
try {
workspace = getWorkspace ( fakeSession ) ;
WorkspaceMessageManager messageManager = workspace . getWorkspaceMessageManager ( ) ;
WorkspaceMessage theMessage = messageManager . getReceivedMessage ( messageId ) ;
recipientIds = theMessage . getAddresses ( ) ;
//add the sender and remove the person who is replying from the recipients
String sender = theMessage . getSender ( ) . getPortalLogin ( ) ;
2015-10-29 11:44:24 +01:00
String originalText = theMessage . getBody ( ) ;
Date originalSentTime = theMessage . getSendTime ( ) . getTime ( ) ;
2015-10-14 17:47:59 +02:00
recipientIds . add ( sender ) ;
recipientIds . remove ( fakeSession . getUsername ( ) ) ;
_log . debug ( " Message Recipients: " ) ;
for ( String rec : recipientIds ) {
_log . debug ( rec ) ;
}
2015-11-30 15:29:01 +01:00
_log . debug ( " Constructing reply message " ) ;
2015-10-29 11:44:24 +01:00
messageText + = getReplyHeaderMessage ( sender , originalSentTime , originalText ) ;
2015-10-14 17:47:59 +02:00
_log . debug ( " Trying to send message with subject: " + subject , " to: " + recipientIds . toString ( ) ) ;
newMessageId = messageManager . sendMessageToPortalLogins ( subject , messageText , new ArrayList < String > ( ) , recipientIds ) ;
} catch ( WorkspaceFolderNotFoundException | InternalErrorException | HomeNotFoundException | ItemNotFoundException e ) {
e . printStackTrace ( ) ;
}
_log . debug ( " Message with subject: " + subject , " hase been sent, returned id: " + newMessageId ) ;
if ( newMessageId ! = null ) {
2016-05-19 00:12:38 +02:00
_log . debug ( " Sending message notifications ... site server URL= " + site . getSiteURL ( ) ) ;
2015-10-14 17:47:59 +02:00
List < GenericItemBean > recipients = getUsersbyUserId ( recipientIds ) ;
2016-02-25 18:46:38 +01:00
NotificationsManager nm = new ApplicationNotificationsManager (
2016-05-19 00:12:38 +02:00
site ,
2016-02-25 18:46:38 +01:00
fakeSession . getScope ( ) ,
new SocialNetworkingUser ( fakeSession . getUsername ( ) , fakeSession . getUserEmailAddress ( ) , fakeSession . getUserFullName ( ) , fakeSession . getUserAvatarId ( ) )
) ;
2015-10-28 17:30:22 +01:00
Thread thread = new Thread ( new MessageNotificationsThread ( recipients , newMessageId , subject , messageText , nm ) ) ;
2015-10-14 17:47:59 +02:00
thread . start ( ) ;
} else {
_log . debug ( " Could not send message reply " ) ;
}
}
2015-10-29 11:44:24 +01:00
private String getReplyHeaderMessage ( String senderId , Date date , String message ) {
List < String > toPass = new ArrayList < String > ( ) ;
toPass . add ( senderId ) ;
List < GenericItemBean > theSender = getUsersbyUserId ( toPass ) ;
String senderFullName = senderId ;
if ( theSender ! = null & & ! theSender . isEmpty ( ) )
senderFullName = theSender . get ( 0 ) . getAlternativeName ( ) ;
String toReturn = " \ n \ n--- \ n on " + date + " " + senderFullName + " wrote: " ;
toReturn + = " \ n \ n " + message ;
return toReturn ;
}
2015-10-14 17:47:59 +02:00
/ * *
* return the User instance given his id
* @param recipientIds
* @return
* /
private List < GenericItemBean > getUsersbyUserId ( List < String > recipientIds ) {
List < GenericItemBean > recipients = new ArrayList < GenericItemBean > ( ) ;
for ( String userid : recipientIds ) {
2016-05-19 00:12:38 +02:00
GCubeUser user ;
2015-10-14 17:47:59 +02:00
try {
2016-05-19 00:12:38 +02:00
user = new LiferayUserManager ( ) . getUserByUsername ( userid ) ;
recipients . add ( new GenericItemBean ( " " + user . getUserId ( ) , user . getUsername ( ) , user . getFullname ( ) , " " ) ) ;
} catch ( Exception e ) {
2015-10-14 17:47:59 +02:00
e . printStackTrace ( ) ;
}
}
return recipients ;
}
/ * *
* this method manages the replies coming from post notifications
* @param portalName
* @param feedId the identifier in the System managing the feeds
* @param message the javax mail Message instance
* @param fakeSession
* @throws Exception
* /
private void handlePostReply ( String portalName , String feedId , Message message , ASLSession fakeSession ) throws Exception {
String commentText = extractText ( portalName , feedId , message ) ;
_log . info ( " Extracted id: " + feedId + " text= " + commentText ) ;
2016-05-19 00:12:38 +02:00
String escapedCommentText = org . gcube . portal . socialmail . Utils . escapeHtmlAndTransformUrl ( commentText ) ;
2015-10-14 17:47:59 +02:00
String subject = message . getSubject ( ) ;
if ( escapedCommentText . trim ( ) . compareTo ( " " ) = = 0 ) { //it is a favorite/subscription
_log . debug ( " Found favorite/subscription for feed with subject: " + subject ) ;
favoriteFeed ( feedId , fakeSession ) ;
}
else {
Comment comment = new Comment ( UUID . randomUUID ( ) . toString ( ) , fakeSession . getUsername ( ) ,
new Date ( ) , feedId , escapedCommentText , fakeSession . getUserFullName ( ) , fakeSession . getUserAvatarId ( ) ) ;
_log . debug ( " The EscapedCommentText => " + escapedCommentText ) ;
boolean commentCommitResult = false ;
try {
if ( socialStore . addComment ( comment ) )
commentCommitResult = true ;
} catch ( FeedIDNotFoundException e ) {
_log . error ( " Related post not found for this comment " + e . getMessage ( ) ) ;
e . printStackTrace ( ) ;
}
if ( commentCommitResult ) { //the notifications should start
notifyUsersInvolved ( comment , escapedCommentText , feedId , fakeSession ) ;
}
}
}
2015-07-15 18:41:02 +02:00
/ * *
* favorite the feed to subscribe to further comments
* @param feedId
* @param fakeSession
* /
private void favoriteFeed ( String feedId , ASLSession fakeSession ) {
2015-07-22 12:41:44 +02:00
if ( feedId = = null | | feedId . compareTo ( " " ) = = 0 ) {
_log . warn ( " Found email with no feedId from " + fakeSession . getUserEmailAddress ( ) + " . Going to trash it " ) ;
return ;
}
2015-08-05 16:07:01 +02:00
2015-07-15 18:41:02 +02:00
Like like = new Like ( UUID . randomUUID ( ) . toString ( ) , fakeSession . getUsername ( ) ,
new Date ( ) , feedId , fakeSession . getUserFullName ( ) , fakeSession . getUserAvatarId ( ) ) ;
2015-08-05 16:07:01 +02:00
2015-07-15 18:41:02 +02:00
boolean likeCommitResult = false ;
try {
if ( socialStore . like ( like ) ) ;
likeCommitResult = true ;
} catch ( FeedIDNotFoundException e ) {
_log . error ( " Related post not found for this like " + e . getMessage ( ) ) ;
e . printStackTrace ( ) ;
}
if ( likeCommitResult ) { //the notification should be delivered to the post owner
try {
Feed feed = socialStore . readFeed ( feedId ) ;
String feedOwnerId = feed . getEntityId ( ) ;
boolean isAppFeed = feed . isApplicationFeed ( ) ;
2016-02-25 18:46:38 +01:00
NotificationsManager nm = new ApplicationNotificationsManager (
2016-05-19 00:12:38 +02:00
site ,
2016-02-25 18:46:38 +01:00
fakeSession . getScope ( ) ,
2016-05-19 11:32:09 +02:00
new SocialNetworkingUser ( fakeSession . getUsername ( ) , fakeSession . getUserEmailAddress ( ) , fakeSession . getUserFullName ( ) , fakeSession . getUserAvatarId ( ) ) ,
APP_ID_NEWSFEED
2016-02-25 18:46:38 +01:00
) ;
2015-07-15 18:41:02 +02:00
if ( ! fakeSession . getUsername ( ) . equals ( feedOwnerId ) & & ( ! isAppFeed ) ) {
boolean result = nm . notifyLikedFeed ( feedOwnerId , feedId , " " ) ;
_log . trace ( " Like Notification to post owner added? " + result ) ;
}
}
catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
}
2015-07-09 16:56:01 +02:00
/ * *
* this method take care of notify all the users that need to be notified when someone comment
* @param comment
* @param feedId
* @param fakeSession
* @throws PrivacyLevelTypeNotFoundException
* @throws FeedTypeNotFoundException
* @throws FeedIDNotFoundException
* @throws ColumnNameNotFoundException
* /
private void notifyUsersInvolved ( Comment comment , String commentText , String feedId , ASLSession fakeSession ) throws PrivacyLevelTypeNotFoundException , FeedTypeNotFoundException , FeedIDNotFoundException , ColumnNameNotFoundException {
Feed feed = socialStore . readFeed ( feedId ) ;
String feedOwnerId = feed . getEntityId ( ) ;
boolean isAppFeed = feed . isApplicationFeed ( ) ;
//if the user who commented this post is not the user who posted it notifies the poster user (Feed owner)
2016-02-25 18:46:38 +01:00
NotificationsManager nm = new ApplicationNotificationsManager (
2016-05-19 00:12:38 +02:00
site ,
2016-05-19 11:32:09 +02:00
feed . getVreid ( ) ,
new SocialNetworkingUser ( fakeSession . getUsername ( ) , fakeSession . getUserEmailAddress ( ) , fakeSession . getUserFullName ( ) , fakeSession . getUserAvatarId ( ) ) ,
APP_ID_NEWSFEED
2016-02-25 18:46:38 +01:00
) ;
2015-07-09 16:56:01 +02:00
if ( ! fakeSession . getUsername ( ) . equals ( feedOwnerId ) & & ( ! isAppFeed ) ) {
2016-02-24 11:09:44 +01:00
boolean result = nm . notifyOwnCommentReply ( feedOwnerId , feedId , commentText , comment . getKey ( ) ) ;
2015-07-09 16:56:01 +02:00
_log . trace ( " Comment Notification to post owner added? " + result ) ;
}
//if there are users who liked this post they get notified, asynchronously with this thread
ArrayList < Like > favorites = getAllLikesByFeed ( feedId ) ;
2016-02-24 11:09:44 +01:00
Thread likesThread = new Thread ( new LikeNotificationsThread ( commentText , nm , favorites , feedOwnerId , comment . getKey ( ) ) ) ;
2015-07-09 16:56:01 +02:00
likesThread . start ( ) ;
2015-07-08 18:47:36 +02:00
2015-07-09 16:56:01 +02:00
//notify the other users who commented this post (excluding the ones above)
2016-02-24 11:09:44 +01:00
Thread commentsNotificationthread = new Thread ( new CommentNotificationsThread ( socialStore , fakeSession . getUsername ( ) , comment . getFeedid ( ) , commentText , nm , feedOwnerId , comment . getKey ( ) , favorites ) ) ;
2015-07-09 16:56:01 +02:00
commentsNotificationthread . start ( ) ;
}
/ * *
*
* @param feedid
* @return
* /
public ArrayList < Like > getAllLikesByFeed ( String feedid ) {
_log . trace ( " Asking likes for " + feedid ) ;
ArrayList < Like > toReturn = ( ArrayList < Like > ) socialStore . getAllLikesByFeed ( feedid ) ;
return toReturn ;
}
/ * *
2015-10-14 17:47:59 +02:00
* extracts the sub - address ( the part after the + and before the @ ) from the email addressee
2015-07-09 16:56:01 +02:00
* @param message
2015-10-14 17:47:59 +02:00
* @return the sub - address without + and @
2015-07-09 16:56:01 +02:00
* @throws MessagingException
* /
2015-10-14 17:47:59 +02:00
private static String extractSubaddress ( Message message ) throws MessagingException {
2015-07-08 18:47:36 +02:00
Address [ ] emails = message . getRecipients ( RecipientType . TO ) ;
String toParse = emails [ 0 ] . toString ( ) ;
int plus = toParse . indexOf ( '+' ) ;
int at = toParse . indexOf ( '@' ) ;
if ( plus = = - 1 )
return null ;
return toParse . substring ( plus + 1 , at ) ;
}
2015-10-14 17:47:59 +02:00
/ * *
* extracts the identifier ( the part before the $ ) from the Subaddress
* @param message
* @return the identifier
* @throws MessagingException
* /
private static String extractIdentifier ( String subAddress ) {
String id = subAddress ; //for backward compatibility
int at = subAddress . indexOf ( '$' ) ;
if ( at > - 1 )
id = subAddress . substring ( 0 , at ) ;
return id ;
}
2015-08-24 11:59:29 +02:00
/ * *
* the email is considered a valid reply if and only of it contains the EmailPlugin . WRITE_ABOVE_TO_REPLY text in the body
* @param message the message to check
* @return true if the email is a valid reply , false otherwise
* /
private boolean isValidReply ( Message message ) {
Object messageContent ;
try {
messageContent = message . getContent ( ) ;
String toParse = null ;
2016-05-19 00:12:38 +02:00
final String SEPARATOR = SocialMailingUtil . WRITE_ABOVE_TO_REPLY . substring ( 0 , 23 ) ;
2015-08-24 11:59:29 +02:00
// Check if content is pure text/html or in parts
if ( messageContent instanceof Multipart ) {
_log . debug ( " Checking if isValidReply, found Multipart Message, getting text part ... looking for separator " + SEPARATOR ) ;
Multipart multipart = ( Multipart ) messageContent ;
BodyPart part = multipart . getBodyPart ( 0 ) ;
part . toString ( ) ;
toParse = part . getContent ( ) . toString ( ) ;
}
else {
_log . debug ( " Found a text/plain Message, getting text ... looking for separator " + SEPARATOR ) ;
toParse = messageContent . toString ( ) ;
}
String [ ] lines = toParse . split ( System . getProperty ( " line.separator " ) ) ;
for ( int i = 0 ; i < lines . length ; i + + ) {
2015-10-14 17:47:59 +02:00
if ( lines [ i ] . contains ( SEPARATOR ) ) {
_log . debug ( " Yes, it is a valid Reply " ) ;
2015-08-24 11:59:29 +02:00
return true ;
2015-10-14 17:47:59 +02:00
}
2015-08-24 11:59:29 +02:00
}
2015-10-14 17:47:59 +02:00
_log . debug ( " NOT a valid Reply " ) ;
2015-08-24 11:59:29 +02:00
return false ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
2015-10-14 17:47:59 +02:00
_log . error ( " Exceptiom returning NOT a valid Reply " ) ;
2015-08-24 11:59:29 +02:00
return false ;
}
}
2015-07-09 16:56:01 +02:00
/ * *
*
* @param portalName
* @param subjectId
* @param message
* @return
* @throws Exception
* /
2015-07-16 16:27:04 +02:00
private static String extractText ( String portalName , String subjectId , Message message ) throws Exception {
2015-07-08 18:47:36 +02:00
Object messageContent = message . getContent ( ) ;
String toParse = null ;
2016-05-19 00:12:38 +02:00
final String SEPARATOR = SocialMailingUtil . WRITE_ABOVE_TO_REPLY . substring ( 0 , 23 ) ;
2015-07-08 18:47:36 +02:00
// Check if content is pure text/html or in parts
if ( messageContent instanceof Multipart ) {
2015-07-16 16:27:04 +02:00
_log . debug ( " Found Multipart Message, getting text part ... looking for separator " + SEPARATOR ) ;
2015-07-08 18:47:36 +02:00
Multipart multipart = ( Multipart ) messageContent ;
BodyPart part = multipart . getBodyPart ( 0 ) ;
part . toString ( ) ;
toParse = part . getContent ( ) . toString ( ) ;
}
else {
2015-10-14 17:47:59 +02:00
_log . debug ( " Found text/plain Message, getting text " ) ;
2015-07-08 18:47:36 +02:00
toParse = messageContent . toString ( ) ;
}
2015-10-14 17:47:59 +02:00
_log . debug ( " Got Message content = " + toParse ) ;
2015-07-09 16:56:01 +02:00
//cut the text below the WRITE_ABOVE_TO_REPLY text
2015-07-08 18:47:36 +02:00
String [ ] lines = toParse . split ( System . getProperty ( " line.separator " ) ) ;
int until = - 1 ;
for ( int i = 0 ; i < lines . length ; i + + ) {
2015-07-16 16:27:04 +02:00
if ( lines [ i ] . contains ( SEPARATOR ) ) {
2015-07-08 18:47:36 +02:00
until = i ;
break ;
}
}
2015-08-05 16:07:01 +02:00
2015-07-09 16:56:01 +02:00
StringBuilder sb = new StringBuilder ( ) ;
/ *
* this is needed to remove the text added by the email client like when you reply , e . g . On < day > < sender > wrote . . .
* it also handles the case where the user writes between that and the WRITE_ABOVE_TO_REPLY text
* /
2015-07-08 18:47:36 +02:00
for ( int i = 0 ; i < until ; i + + ) {
if ( ! ( lines [ i ] . contains ( portalName ) | | lines [ i ] . contains ( subjectId ) | | ( lines [ i ] . startsWith ( " > " ) & & lines [ i ] . trim ( ) . length ( ) < 2 ) ) ) {
2015-07-09 16:56:01 +02:00
sb . append ( lines [ i ] ) ;
2015-07-08 18:47:36 +02:00
}
2015-07-09 16:56:01 +02:00
if ( ! ( i = = until - 1 ) )
sb . append ( " \ n " ) ;
2015-07-08 18:47:36 +02:00
}
2015-10-14 17:47:59 +02:00
String toReturn = sb . toString ( ) . trim ( ) ;
_log . debug ( " Returning text extracted = " + toReturn ) ;
return toReturn ;
}
/ * *
*
* @return the workspace instance
* @throws InternalErrorException
* @throws HomeNotFoundException
* @throws WorkspaceFolderNotFoundException
* /
private Workspace getWorkspace ( ASLSession session ) throws InternalErrorException , HomeNotFoundException , WorkspaceFolderNotFoundException {
Workspace workspace = HomeLibrary . getUserWorkspace ( session . getUsername ( ) ) ;
return workspace ;
2015-07-08 18:47:36 +02:00
}
2015-10-14 17:47:59 +02:00
2015-07-08 18:47:36 +02:00
}