-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58e0ff8
commit 4a67e9c
Showing
8 changed files
with
338 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.ServletException; | ||
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.Conversation; | ||
import java.util.List; | ||
import javax.json.*; | ||
|
||
@WebServlet("/GetMessages") | ||
public class GetMessageServlet extends HttpServlet { | ||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, 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 | ||
Conversation conversation = new Conversation(); | ||
List<Message> messages = conversation.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()); | ||
} | ||
conversation.markMessagesAsRead(contactId, userId); | ||
JsonArray jsonArray = jsonArrayBuilder.build(); | ||
|
||
response.setContentType("application/json"); | ||
response.getWriter().write(jsonArray.toString()); | ||
} | ||
|
||
protected void doPost(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, 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 | ||
Conversation conversation = new Conversation(); | ||
conversation.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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.