[CT414]: Assignment 1 error handling and testing
This commit is contained in:
@ -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))) {
|
||||
|
Reference in New Issue
Block a user