tries looking for email message by id first and name as fallback
This commit is contained in:
parent
0c5a5bc110
commit
f769c3d1ce
|
@ -4,6 +4,7 @@ import static org.gcube.common.authorization.client.Constants.authorizationServi
|
|||
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
@ -39,6 +40,8 @@ import org.gcube.common.portal.PortalContext;
|
|||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.common.storagehub.client.plugins.AbstractPlugin;
|
||||
import org.gcube.common.storagehub.client.proxies.MessageManagerClient;
|
||||
import org.gcube.common.storagehub.model.exceptions.IdNotFoundException;
|
||||
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
|
||||
import org.gcube.portal.databook.server.DatabookStore;
|
||||
import org.gcube.portal.databook.shared.Comment;
|
||||
import org.gcube.portal.databook.shared.Feed;
|
||||
|
@ -270,27 +273,46 @@ public class PeriodicTask implements Runnable {
|
|||
String token = authorizationService().generateUserToken(new UserInfo(username, userRoles), scope);
|
||||
SecurityTokenProvider.instance.set(token);
|
||||
MessageManagerClient mmClient = AbstractPlugin.messages().build();
|
||||
org.gcube.common.storagehub.model.messages.Message theMessage = mmClient.get(messageId);
|
||||
String[] addresses = theMessage.getAddresses();
|
||||
for (int i = 0; i < addresses.length; i++) {
|
||||
recipientIds.add(addresses[i]);
|
||||
org.gcube.common.storagehub.model.messages.Message theMessage = null;
|
||||
try {
|
||||
theMessage = mmClient.get(messageId);
|
||||
}
|
||||
//add the sender and remove the person who is replying from the recipients
|
||||
String sender = theMessage.getSender().getUserName();
|
||||
String originalText = theMessage.getBody();
|
||||
Date originalSentTime = theMessage.getCreationTime().getTime();
|
||||
recipientIds.add(sender);
|
||||
recipientIds.remove(fakeSession.getUsername());
|
||||
|
||||
_log.debug("Message Recipients:");
|
||||
for (String rec : recipientIds) {
|
||||
_log.debug(rec);
|
||||
catch (IdNotFoundException e) {
|
||||
_log.warn("Message with id "+ messageId + " could not be found, trying by name ...");
|
||||
List<org.gcube.common.storagehub.model.messages.Message> messages = mmClient.getReceivedMessages();
|
||||
Collections.reverse(messages); //the messages are returned in chronological order, but the messages we look for is one of the newests
|
||||
_log.warn("Messages size="+messages.size());
|
||||
for(org.gcube.common.storagehub.model.messages.Message msg: messages) {
|
||||
if (msg.getName().equals(messageId)) {
|
||||
theMessage = msg;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
_log.debug("Constructing reply message");
|
||||
messageText += getReplyHeaderMessage(sender, originalSentTime, originalText);
|
||||
if (theMessage != null) {
|
||||
String[] addresses = theMessage.getAddresses();
|
||||
for (int i = 0; i < addresses.length; i++) {
|
||||
recipientIds.add(addresses[i]);
|
||||
}
|
||||
//add the sender and remove the person who is replying from the recipients
|
||||
String sender = theMessage.getSender().getUserName();
|
||||
String originalText = theMessage.getBody();
|
||||
Date originalSentTime = theMessage.getCreationTime().getTime();
|
||||
recipientIds.add(sender);
|
||||
recipientIds.remove(fakeSession.getUsername());
|
||||
|
||||
_log.debug("Trying to send message with subject: " + subject + " to: " + recipientIds.toString());
|
||||
newMessageId = mmClient.sendMessage(recipientIds, subject, messageText, new ArrayList<String>());
|
||||
_log.debug("Message Recipients:");
|
||||
for (String rec : recipientIds) {
|
||||
_log.debug(rec);
|
||||
}
|
||||
_log.debug("Constructing reply message");
|
||||
messageText += getReplyHeaderMessage(sender, originalSentTime, originalText);
|
||||
|
||||
_log.debug("Trying to send message with subject: " + subject + " to: " + recipientIds.toString());
|
||||
newMessageId = mmClient.sendMessage(recipientIds, subject, messageText, new ArrayList<String>());
|
||||
} else {
|
||||
_log.error("Message with id "+ messageId + " could not be found even iterating by name, exiting.");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
|
Loading…
Reference in New Issue