Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/development' into 31-implementat…
Browse files Browse the repository at this point in the history
…ion-adding-fe-and-be-of-various-pages
  • Loading branch information
DDDrag0 committed Dec 31, 2023
2 parents 7ddbf38 + d5a65f9 commit 3076082
Show file tree
Hide file tree
Showing 24 changed files with 1,193 additions and 61 deletions.
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<junit.version>5.10.0</junit.version>
<sonar.organization>panuozzo77</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
</properties>

<dependencies>
Expand Down Expand Up @@ -68,6 +70,18 @@
<version>3.1.0</version>
</dependency>

<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1.4</version>
</dependency>

<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
</dependency>

</dependencies>

<build>
Expand Down
27 changes: 5 additions & 22 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,15 @@
import model.DAO.DAOUser;
import model.entity.User;
import model.service.login.Authenticator;
//import model.service.message.Conversation;
import model.service.registration.Registration;

public class Main {
public static void main(String[] args) {
Encryption encryption = new Encryption();
DAOUser db = new DAOUser();
String plainTextPassword = "123456";
EmailManager message = new EmailManager();

/*
String hashedPassword = encryption.encryptPassword(plainTextPassword);
//Conversation conv = new Conversation(9);
//System.out.println(conv.getUnreadMessagesCounter());
//System.out.println(conv.getConversations());

// Use hashed password to create new user
db.createUser("[email protected]", hashedPassword, 0);
*/

/*
User user = db.getUserByIdOrEmail("[email protected]");
System.out.println(encryption.verifyPassword(plainTextPassword, user.getPassword()));
*/


//test email
//message.sendEmail("[email protected]", "Email Test", "questo è una email di test");

//test email recupero password
Authenticator authenticator = new Authenticator();
System.out.println(authenticator.authenticate("[email protected]", "123456"));
}
}
27 changes: 27 additions & 0 deletions src/main/java/controller/CountMessagesServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package controller;

import model.service.message.MessageManager;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/CountMessages")
public class CountMessagesServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
HttpSession session = request.getSession();
int recipientId = (int) session.getAttribute("id");
MessageManager messageManager = new MessageManager();
int receivedMessageCount = messageManager.countReceivedMessages(recipientId);

response.setContentType("text/plain"); // Output is a plain text integer
response.getWriter().println(receivedMessageCount);
}

}
72 changes: 72 additions & 0 deletions src/main/java/controller/GetMessageServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package controller;

import java.io.IOException;
import java.sql.Timestamp;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.entity.Message;
import model.service.message.MessageManager;

import java.util.List;
import javax.json.*;

@WebServlet("/GetMessages")
public class GetMessageServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
int userId = (int) session.getAttribute("id");

int contactId = Integer.parseInt(request.getParameter("contact_id")); // Get the contact's ID from the request

// Retrieve the messages between the user and the contact
MessageManager messageManager = new MessageManager();
List<Message> messages = messageManager.retrieveMessages(userId, contactId);

// Now build the JSON response
JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();

for (Message message : messages) {
JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
jsonObjectBuilder.add("sender", message.getSender());
jsonObjectBuilder.add("recipient", message.getRecipient());
jsonObjectBuilder.add("body", message.getBody());
// Convert timestamp to format HH:MM
Timestamp timestamp = message.getSent();
java.util.Date date = new java.util.Date(timestamp.getTime());
java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("HH:mm");
String formattedDate = dateFormat.format(date);

jsonObjectBuilder.add("sent", formattedDate);
jsonArrayBuilder.add(jsonObjectBuilder.build());
}
messageManager.markMessagesAsRead(contactId, userId);
JsonArray jsonArray = jsonArrayBuilder.build();

response.setContentType("application/json");
response.getWriter().write(jsonArray.toString());
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException {
try {
HttpSession session = request.getSession();
int sender = (int) session.getAttribute("id");
int recipient = Integer.parseInt(request.getParameter("recipient"));
String body = request.getParameter("body");

// send the message
MessageManager messageManager = new MessageManager();
messageManager.sendMessage(sender, recipient, body);

response.getWriter().write("Message successfully sent");

} catch (NumberFormatException e) {
// The request parameter could not be parsed as an integer
response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
}
}
}
6 changes: 2 additions & 4 deletions src/main/java/controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package controller;

import model.DAO.DAOPersonalInfo;
import model.entity.PersonalInfo;
import model.entity.User;
import model.service.login.Authenticator;
import model.service.user.UserData;
import model.service.user.UserRegistry;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -28,7 +26,7 @@ public void init() {
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
throws IOException {

String email = request.getParameter("email");
String password = request.getParameter("password");
Expand All @@ -51,7 +49,7 @@ private void setSessionAttributes(int id, HttpServletRequest request){
UserData userData = new UserData();
UserRegistry userReg = new UserRegistry();

User user = userData.getUserByIdOrEmail(id);
User user = userData.getUser(id);
PersonalInfo personalInfo = userReg.getPersonalInfo(id);

session.setAttribute("id", id);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/controller/RegistrationServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private void setSessionAttributes(String email, HttpServletRequest request){
UserData userData = new UserData();
UserRegistry userReg = new UserRegistry();

User user = userData.getUserByIdOrEmail(email);
User user = userData.getUser(email);
PersonalInfo personalInfo = userReg.getPersonalInfo(user.getId());

session.setAttribute("id", user.getId());
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/controller/ResetPasswordServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,5 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
if(authenticator.resetPassword(email, password)){
response.getWriter().write("Password cambiata con successo!");
}



}
}
3 changes: 1 addition & 2 deletions src/main/java/controller/SendResetPin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package controller;

import model.entity.User;
import model.service.login.Authenticator;
import model.service.user.UserData;

Expand All @@ -18,7 +17,7 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr

response.setContentType("text/plain");
if(checker.checkIfEmailExists(email)){
String pin = new Authenticator().resetPassword(email);
String pin = new Authenticator().sendPin(email);
response.getWriter().println(pin);
}
else {
Expand Down
Loading

0 comments on commit 3076082

Please sign in to comment.