Skip to content
Snippets Groups Projects
Commit da5b8efc authored by Martin Staehr's avatar Martin Staehr
Browse files

#63 implementing email message with test

parent 8513c113
No related branches found
No related tags found
No related merge requests found
......@@ -237,7 +237,11 @@
<artifactId>object-mappers-jackson</artifactId>
<version>1.0.01</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0-b01</version>
</dependency>
</dependencies>
......
......@@ -5,7 +5,7 @@ import unipotsdam.gf.core.management.project.Project;
import unipotsdam.gf.core.management.user.User;
import unipotsdam.gf.core.states.model.ConstraintsMessages;
import unipotsdam.gf.modules.assessment.controller.model.StudentIdentifier;
import unipotsdam.gf.modules.communication.model.Message;
import unipotsdam.gf.modules.communication.model.EMailMessage;
import unipotsdam.gf.modules.communication.model.chat.ChatMessage;
import java.util.List;
......@@ -27,7 +27,7 @@ public interface ICommunication {
List<ChatMessage> getChatHistory(String roomId);
@Deprecated
boolean sendMessageToChat(Message message, String roomId);
boolean sendMessageToChat(EMailMessage EMailMessage, String roomId);
/**
* endpoint: https://rocket.chat/docs/developer-guides/rest-api/groups/create/
......@@ -108,7 +108,7 @@ public interface ICommunication {
String getChatRoomLink(String userToken, String projectId);
// TODO implement as Email or whatever
void sendSingleMessage(Message message, User user);
void sendSingleMessage(EMailMessage EMailMessage, User user);
//added by Axel.
void informAboutMissingTasks(Map<StudentIdentifier, ConstraintsMessages> tasks, Project project);
......
package unipotsdam.gf.modules.communication.model;
public class EMailMessage {
private String subject;
private String body;
public EMailMessage() {
}
public EMailMessage(String subject, String body) {
this.subject = subject;
this.body = body;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
package unipotsdam.gf.modules.communication.model;
public class Message {
private String roomIdOrChannel;
private String message;
public Message() {}
public Message(String roomIdOrChannel, String message) {
this.roomIdOrChannel = roomIdOrChannel;
this.message = message;
}
public String getRoomIdOrChannel() {
return roomIdOrChannel;
}
public void setRoomIdOrChannel(String roomIdOrChannel) {
this.roomIdOrChannel = roomIdOrChannel;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return "Message{" +
"roomIdOrChannel='" + roomIdOrChannel + '\'' +
", message='" + message + '\'' +
'}';
}
}
......@@ -14,7 +14,7 @@ import unipotsdam.gf.core.management.user.UserDAO;
import unipotsdam.gf.core.states.model.ConstraintsMessages;
import unipotsdam.gf.interfaces.ICommunication;
import unipotsdam.gf.modules.assessment.controller.model.StudentIdentifier;
import unipotsdam.gf.modules.communication.model.Message;
import unipotsdam.gf.modules.communication.model.EMailMessage;
import unipotsdam.gf.modules.communication.model.chat.ChatMessage;
import unipotsdam.gf.modules.communication.model.rocketChat.RocketChatLoginResponse;
import unipotsdam.gf.modules.communication.model.rocketChat.RocketChatRegisterResponse;
......@@ -25,15 +25,29 @@ import javax.annotation.ManagedBean;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.ws.rs.core.Response;
import java.io.UnsupportedEncodingException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
import static unipotsdam.gf.config.GFMailConfig.SMTP_HOST;
import static unipotsdam.gf.config.GFMailConfig.SMTP_PASSWORD;
import static unipotsdam.gf.config.GFMailConfig.SMTP_PORT;
import static unipotsdam.gf.config.GFMailConfig.SMTP_USERNAME;
import static unipotsdam.gf.config.GFRocketChatConfig.ADMIN_USER;
import static unipotsdam.gf.config.GFRocketChatConfig.ROCKET_CHAT_API_LINK;
import static unipotsdam.gf.config.GFRocketChatConfig.ROCKET_CHAT_ROOM_LINK;
......@@ -71,7 +85,7 @@ public class CommunicationDummyService implements ICommunication {
}
@Override
public boolean sendMessageToChat(Message message, String roomId) {
public boolean sendMessageToChat(EMailMessage EMailMessage, String roomId) {
// TODO: not needed at the moment, possibly remove
return false;
}
......@@ -301,11 +315,35 @@ public class CommunicationDummyService implements ICommunication {
return ROCKET_CHAT_ROOM_LINK + chatRoomName + "?layout=embedded";
}
// TODO: Think about splitting email and chat communication into different
@Override
public void sendSingleMessage(Message message, User user) {
// TODO implement as email or directed message, popup after login or whatever
String message2 = "sending email with message: " + message.getMessage() + " to: " + user.getEmail();
NotImplementedLogger.logAssignment(Assignee.MARTIN, CommunicationDummyService.class, message2);
public void sendSingleMessage(EMailMessage eMailMessage, User user) {
Properties properties = new Properties();
properties.put("mail.smtp.auth", true);
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", SMTP_HOST);
properties.put("mail.smtp.port", SMTP_PORT);
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(SMTP_USERNAME, SMTP_PASSWORD);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(SMTP_USERNAME, "FLTrail Messagebot"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(user.getEmail()));
message.setSubject(eMailMessage.getSubject());
message.setText(eMailMessage.getBody());
Transport.send(message);
} catch (MessagingException | UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
@Override
......
......@@ -5,7 +5,7 @@ import org.slf4j.LoggerFactory;
import unipotsdam.gf.config.GFRocketChatConfig;
import unipotsdam.gf.core.management.user.User;
import unipotsdam.gf.interfaces.ICommunication;
import unipotsdam.gf.modules.communication.model.Message;
import unipotsdam.gf.modules.communication.model.EMailMessage;
import unipotsdam.gf.modules.communication.model.chat.ChatMessage;
import unipotsdam.gf.modules.communication.service.CommunicationDummyService;
......@@ -65,19 +65,19 @@ public class CommunicationView {
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/send/{roomId}")
public Response sendMessage(Message message, @PathParam("roomId") String roomId) {
if (isNull(message)) {
log.trace("sendMessage message object was null");
return Response.status(Response.Status.BAD_REQUEST).entity("must provide message").build();
public Response sendMessage(EMailMessage EMailMessage, @PathParam("roomId") String roomId) {
if (isNull(EMailMessage)) {
log.trace("sendMessage EMailMessage object was null");
return Response.status(Response.Status.BAD_REQUEST).entity("must provide EMailMessage").build();
}
boolean wasSend = communicationService.sendMessageToChat(message, roomId);
boolean wasSend = communicationService.sendMessageToChat(EMailMessage, roomId);
Response response;
if (wasSend) {
log.trace("response for sendMessage: {}", wasSend);
response = Response.ok(wasSend).build();
} else {
log.error("error while sending message for message: {}", message);
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("error while sending message").build();
log.error("error while sending EMailMessage for EMailMessage: {}", EMailMessage);
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("error while sending EMailMessage").build();
}
return response;
}
......
......@@ -8,6 +8,7 @@ import unipotsdam.gf.core.database.InMemoryMySqlConnect;
import unipotsdam.gf.core.management.user.User;
import unipotsdam.gf.core.management.user.UserDAO;
import unipotsdam.gf.interfaces.ICommunication;
import unipotsdam.gf.modules.communication.model.EMailMessage;
import unipotsdam.gf.modules.groupfinding.service.GroupDAO;
import java.util.ArrayList;
......@@ -154,6 +155,19 @@ public class CommunicationDummyServiceTest {
assertFalse(iCommunication.exists(chatRoomId));
}
@Test
@Ignore
public void sendSingleMessage() {
User user = new User();
// Permalink for email-address: http://www.trashmail.de/index.php?search=javatest
user.setEmail("javatest@trashmail.de");
EMailMessage eMailMessage = new EMailMessage();
eMailMessage.setSubject("Test Email");
eMailMessage.setBody("Test Body");
iCommunication.sendSingleMessage(eMailMessage, user);
}
@Test
@Ignore
public void createTestData() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment