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 cayorku.eecsmail.net;
import cayorku.eecsmail.mailbox.MailMessage;
import cayorku.eecsmail.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 MyPOPServerSocket socket throws IOException
this.socket socket;
this.socketIn new BufferedReadernew InputStreamReadersocketgetInputStream;
this.socketOut new PrintWriternew OutputStreamWritersocketgetOutputStream 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 POP
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.printlnOK POP 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.splits;
String command commandPartstoUpperCase;
String arguments commandParts.length commandParts : ;
switch command
case "USER":
if argumentsisEmpty
socketOut.printlnERR User name not provided.";
else
isAuthenticated Mailbox.isValidUserarguments;
if isAuthenticated
authenticatedUsername arguments;
socketOut.printlnOK User accepted, provide password.";
else
socketOut.printlnOK;
break;
case "PASS":
if isAuthenticated
try
Mailbox mailbox new MailboxauthenticatedUsername;
mailbox.loadMessagesarguments;
socketOut.printlnOK User successfully logged in;
catch MailboxInvalidUserException Mailbox.MailboxNotAuthenticatedException e
socketOut.printlnERR Invalid username or password.";
else
socketOut.printlnERR User not authenticated.";
break;
case "STAT":
if isAuthenticated
try
Mailbox mailbox new MailboxauthenticatedUsername;
mailbox.loadMessagesauthenticatedUsername;
int messageCount mailbox.sizefalse;
long totalSize mailbox.getTotalUndeletedFileSizefalse;
socketOut.printlnOK messageCount messageCount "messages" : "message" totalSize octets;
catch MailboxMailboxNotAuthenticatedException e
socketOut.printlnERR User not authenticated.";
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
