Question: Can you correct my run method and write the new one package ca . yorku.eecs 3 2 1 4 . mail.net; import ca . yorku.eecs

Can you correct my run method and write the new one
package ca.yorku.eecs3214.mail.net;
import ca.yorku.eecs3214.mail.mailbox.MailMessage;
import ca.yorku.eecs3214.mail.mailbox.Mailbox;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
public class MyPOPServer extends Thread {
private final Socket socket;
private final BufferedReader socketIn;
private final PrintWriter socketOut;
// TODO Additional properties, if needed
private boolean isAuthenticated = false;
private String authenticatedUsername = null;
/**
* Initializes an object responsible for a connection to an individual client.
*
* @param socket The socket associated to the accepted connection.
* @throws IOException If there is an error attempting to retrieve the socket's
* information.
*/
public MyPOPServer(Socket socket) throws IOException {
this.socket = socket;
this.socketIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.socketOut = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
}
/**
* Handles the communication with an individual client. Must send the
* initial welcome message, and then repeatedly read requests, process the
* individual operation, and return a response, according to the POP3
* protocol. Empty request lines should be ignored. Only returns if the
* connection is terminated or if the QUIT command is issued. Must close the
* socket connection before returning.
*/
@Override
public void run(){
try {
// Send initial welcome message
socketOut.println("+OK POP3 server ready");
// Variables to keep track of session state
boolean isAuthenticated = false;
String authenticatedUsername = null;
boolean isQuitRequested = false;
// Loop to handle client requests
while (!isQuitRequested){
// Read client command
String clientCommand = socketIn.readLine();
if (clientCommand == null){
// Client has closed the connection unexpectedly
break;
}
// Split client command into command and arguments
String[] commandParts = clientCommand.split("\\s+",2);
String command = commandParts[0].toUpperCase();
String arguments = commandParts.length >1? commandParts[1] : "";
switch (command){
case "USER":
if (arguments.isEmpty()){
socketOut.println("-ERR User name not provided.");
} else {
isAuthenticated = Mailbox.isValidUser(arguments);
if (isAuthenticated){
authenticatedUsername = arguments;
socketOut.println("+OK User accepted, provide password.");
} else {
socketOut.println("+OK");
}
}
break;
case "PASS":
if (isAuthenticated){
try {
Mailbox mailbox = new Mailbox(authenticatedUsername);
mailbox.loadMessages(arguments);
socketOut.println("+OK User successfully logged in.");
} catch (Mailbox.InvalidUserException | Mailbox.MailboxNotAuthenticatedException e){
socketOut.println("-ERR Invalid username or password.");
}
} else {
socketOut.println("-ERR User not authenticated.");
}
break;
case "STAT":
if (isAuthenticated){
try {
Mailbox mailbox = new Mailbox(authenticatedUsername);
mailbox.loadMessages(authenticatedUsername);
int messageCount = mailbox.size(false);
long totalSize = mailbox.getTotalUndeletedFileSize(false);
socketOut.println("+OK "+ messageCount +""+(messageCount >1? "messages" : "message")+"("+ totalSize +" octets)");
} catch (Mailbox.MailboxNotAuthenticatedException e){
socketOut.println("-ERR User not authenticated.");

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!