[CT414]: Assignment 1 error handling and testing
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
package client;
|
||||
|
||||
import exceptions.InvalidCredentialsException;
|
||||
import exceptions.InvalidSessionIDException;
|
||||
import interfaces.ApplicationForm;
|
||||
import interfaces.ApplicationHandler;
|
||||
|
||||
@ -21,14 +23,22 @@ public class ApplicationClient {
|
||||
System.out.println("ApplicationHandler found in registry");
|
||||
|
||||
// login
|
||||
System.out.printf("Enter your username\n> ");
|
||||
String username = scanner.nextLine();
|
||||
long sessionID;
|
||||
while (true) {
|
||||
try {
|
||||
System.out.printf("Enter your username\n> ");
|
||||
String username = scanner.nextLine();
|
||||
|
||||
System.out.printf("Enter your password\n> ");
|
||||
String password = scanner.nextLine();
|
||||
System.out.printf("Enter your password\n> ");
|
||||
String password = scanner.nextLine();
|
||||
|
||||
long sessionID = handler.login(username, password);
|
||||
System.out.println("Successfully logged in with session ID: " + sessionID);
|
||||
sessionID = handler.login(username, password);
|
||||
System.out.println("Successfully logged in with session ID: " + sessionID);
|
||||
break;
|
||||
} catch (Exception e) {
|
||||
System.out.println("Username or password incorrect, try again.");
|
||||
}
|
||||
}
|
||||
|
||||
// download application form
|
||||
ApplicationForm form = handler.downloadApplicationForm(sessionID);
|
||||
@ -52,6 +62,8 @@ public class ApplicationClient {
|
||||
handler.submitApplicationForm(sessionID, form);
|
||||
System.out.println("Successfully submitted application form.");
|
||||
|
||||
} catch (InvalidSessionIDException e) {
|
||||
System.out.println("Invalid session ID, exiting...");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
|
@ -2,7 +2,7 @@ package exceptions;
|
||||
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
public class InvalidCredentialsException extends RemoteException {
|
||||
public class InvalidCredentialsException extends Exception {
|
||||
public InvalidCredentialsException() {
|
||||
super("Invalid username or password.");
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package exceptions;
|
||||
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
public class InvalidSessionIDException extends RemoteException {
|
||||
public class InvalidSessionIDException extends Exception {
|
||||
public InvalidSessionIDException() {
|
||||
super("Invalid or expired session ID.");
|
||||
}
|
||||
|
@ -10,6 +10,13 @@ import java.io.*;
|
||||
public class ApplicationHandlerImpl implements ApplicationHandler {
|
||||
private ArrayList<Long> sessions = new ArrayList<Long>();
|
||||
|
||||
// private method to validate session ids
|
||||
private boolean isSessionIDValid(long sessionID) {
|
||||
// check that session is valid and has not expired
|
||||
// letting session ids expire after 60 seconds for easier testing, a more practical number would be something like 10 minutes
|
||||
return sessions.contains(sessionID) && ((System.currentTimeMillis()) - sessionID < 6000);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long login(String username, String password) throws RemoteException, InvalidCredentialsException {
|
||||
// hardcoded username and password (great practice for security)
|
||||
@ -21,13 +28,15 @@ public class ApplicationHandlerImpl implements ApplicationHandler {
|
||||
|
||||
return sessionId;
|
||||
} else {
|
||||
System.out.println("Login failed with user " + username + " and password " + password);
|
||||
throw new InvalidCredentialsException("Invalid username or password.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApplicationForm downloadApplicationForm(long sessionID) throws RemoteException, InvalidSessionIDException {
|
||||
if (sessions.contains(sessionID)) {
|
||||
// check that session is valid and has not expired
|
||||
if (isSessionIDValid(sessionID)) {
|
||||
System.out.println("Returning application form for session ID: " + sessionID);
|
||||
return new ApplicationFormV1();
|
||||
} else {
|
||||
@ -37,7 +46,7 @@ public class ApplicationHandlerImpl implements ApplicationHandler {
|
||||
|
||||
@Override
|
||||
public void submitApplicationForm(long sessionID, ApplicationForm applicationForm) throws RemoteException, InvalidSessionIDException {
|
||||
if (sessions.contains(sessionID)) {
|
||||
if (isSessionIDValid(sessionID)) {
|
||||
String filename = applicationForm.getName().replaceAll("\\s", "_") + ".txt";
|
||||
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
|
||||
|
@ -14,4 +14,4 @@ $TERM -e java -cp $src_path -Djava.rmi.server.codebase=file:$src_path/ server.Ap
|
||||
sleep 1
|
||||
|
||||
# start client (in a new terminal)
|
||||
$TERM -e java -cp $src_path -Djava.rmi.server.codebase=file:$src_path/ client.ApplicationClient
|
||||
$TERM -e bash -c "java -cp $src_path -Djava.rmi.server.codebase=file:$src_path/ client.ApplicationClient ; zsh"
|
||||
|
Reference in New Issue
Block a user