|
| 1 | +package de.devofvictory.schoolapi.objects; |
| 2 | + |
| 3 | +import de.devofvictory.schoolapi.objects.exercises.ExerciseManager; |
| 4 | +import de.devofvictory.schoolapi.objects.mail.MailManager; |
| 5 | +import de.devofvictory.schoolapi.utils.IservUtil; |
| 6 | + |
| 7 | +public class IservSession { |
| 8 | + |
| 9 | + private long connectedSince; |
| 10 | + private String[] sessionCookies; |
| 11 | + private String iservAddress; |
| 12 | + private String username; |
| 13 | + private String password; |
| 14 | + private boolean isConnected; |
| 15 | + |
| 16 | + private ExerciseManager exerciseManager; |
| 17 | + private MailManager mailManager; |
| 18 | + |
| 19 | + public IservSession(String iservAddress, String username, String password) { |
| 20 | + this.iservAddress = iservAddress; |
| 21 | + this.username = username; |
| 22 | + this.password = password; |
| 23 | + this.isConnected = false; |
| 24 | + } |
| 25 | + |
| 26 | + public long getConnectedSince() { |
| 27 | + return connectedSince; |
| 28 | + } |
| 29 | + |
| 30 | + public String getUsername() { |
| 31 | + return username; |
| 32 | + } |
| 33 | + |
| 34 | + public String getPassword() { |
| 35 | + return password; |
| 36 | + } |
| 37 | + |
| 38 | + public boolean isConnected() { |
| 39 | + return isConnected; |
| 40 | + } |
| 41 | + |
| 42 | + public String[] getSessionCookies() { |
| 43 | + return sessionCookies; |
| 44 | + } |
| 45 | + |
| 46 | + public String getIservAddress() { |
| 47 | + return iservAddress; |
| 48 | + } |
| 49 | + |
| 50 | + public ExerciseManager getExerciseManager() { |
| 51 | + if (isConnected) { |
| 52 | + return exerciseManager; |
| 53 | + }else { |
| 54 | + System.out.println("Not connected"); |
| 55 | + return null; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public MailManager getMailManager() { |
| 60 | + if (isConnected) { |
| 61 | + return mailManager; |
| 62 | + }else { |
| 63 | + System.out.println("Not connected"); |
| 64 | + return null; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public String connect() { |
| 69 | + |
| 70 | + try { |
| 71 | + String[] cookies = IservUtil.getCookiesFromCredentials(iservAddress, username, password); |
| 72 | + |
| 73 | + if (cookies.length == 3) { |
| 74 | + |
| 75 | + this.sessionCookies = cookies; |
| 76 | + this.connectedSince = System.currentTimeMillis(); |
| 77 | + this.isConnected = true; |
| 78 | + |
| 79 | + this.exerciseManager = new ExerciseManager(iservAddress, cookies); |
| 80 | + this.mailManager = new MailManager(iservAddress, username, password); |
| 81 | + this.mailManager.login(); |
| 82 | + |
| 83 | + return "SUCCESS"; |
| 84 | + } else if (cookies.length == 1) { |
| 85 | + return cookies[0]; |
| 86 | + }else { |
| 87 | + return "Unknown error occured"; |
| 88 | + } |
| 89 | + } catch (Exception ex) { |
| 90 | + return "Couldn't connect to mail servers"; |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | +} |
0 commit comments