-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthIIOPClient.java
147 lines (138 loc) · 4.72 KB
/
AuthIIOPClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package server;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.MarshalledObject;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
* Classe client che sfruttano il protocollo IIOP.
*
* @author Pietro Musoni
* @author Carlo Tacchella
*/
public class AuthIIOPClient implements Runnable {
private AuthServer as;
private String serverIP;
private String codebase;
private String username;
private char[] password;
@SuppressWarnings("unchecked")
public void run() {
try {
System.setProperty("javax.net.ssl.trustStore", "mySrvKeystore");
System.setProperty("javax.net.ssl.trustStorePassword", "123456");
System.out.println("Sono nel codice di AuthIIOPClient.");
System.setSecurityManager(new RMISecurityManager());
serverIP = System.getenv("SERVERIP");
codebase = "http://" + serverIP + ":8000/";
Properties pr = new Properties();
pr.put("java.naming.factory.initial",
"com.sun.jndi.cosnaming.CNCtxFactory");
pr.put(Context.PROVIDER_URL, "iiop://" + serverIP + ":5555");
InitialContext ic = new InitialContext(pr);
Object objRef = ic.lookup("AuthServer");
System.out.println("Ho recuperato lo stub dell'AuthServer.");
as = (AuthServer) PortableRemoteObject.narrow(objRef,
AuthServer.class);
} catch (ClassCastException e) {
e.printStackTrace();
} catch (NamingException e) {
System.out.println("Problema con Naming.");
}
String scelta = "";
boolean login = true;
while (login) {
try {
username=null;
password=null;
URL icon = new URL(codebase + "res/UserHelp.png");
scelta = (String) JOptionPane.showInputDialog(null,
"MENU:\n1) Login\n2) Registra \n3) Esci",
"Menu Utente", JOptionPane.INFORMATION_MESSAGE,
new ImageIcon(icon), null, null);
int s = Integer.parseInt(scelta);
Object[] message = new Object[4];
message[0] = "Nome utente:";
message[1] = new JTextField("");
message[2] = "Password:";
message[3] = new JPasswordField("");
String[] options = { "OK" };
switch (s) {
case 1: {
// Visualizzazione della schermata di Login
icon = new URL(codebase + "res/Login.png");
while (username == null || username.equals("")
|| password == null || password.equals("")) {
JOptionPane.showOptionDialog(null, message,
"Autenticazione.", JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE, new ImageIcon(
icon), options, options[0]);
username = ((JTextField) message[1]).getText();
password = ((JPasswordField) message[3]).getPassword();
}
MarshalledObject<IIOPClient> mo = null;
mo = (MarshalledObject<IIOPClient>) as.login(username, password);
if (mo != null) {
IIOPClient ai = mo.get();
ai.act();
login = false;
} else {
icon = new URL(codebase + "res/UserFailed.png");
JOptionPane.showMessageDialog(null, "Login non valido",
"Errore.", JOptionPane.ERROR_MESSAGE,
new ImageIcon(icon));
}
break;
}
case 2: {
// Visualizzazione della schermata di Registrazione
icon = new URL(codebase + "res/UserAdd.png");
while (username == null || username.equals("")
|| password == null || password.equals("")) {
JOptionPane.showOptionDialog(null, message,
"Registrazione.", JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE, new ImageIcon(
icon), options, options[0]);
username = ((JTextField) message[1]).getText();
password = ((JPasswordField) message[3]).getPassword();
}
if(!as.registerUser(username, password, 0)){
icon = new URL(codebase + "res/UserFailed.png");
JOptionPane.showMessageDialog(null, "Registrazione fallita.",
"Errore.", JOptionPane.ERROR_MESSAGE,
new ImageIcon(icon));
}
break;
}
case 3: {
System.exit(0);
}
}
} catch (MalformedURLException mue) {
System.out.println("Indirizzo non valido.");
} catch (NumberFormatException ne) {
System.out.println("Scelta non valida.");
} catch (RemoteException re) {
System.out.println("Server non raggiungibile. Esco.");
login=false;
} catch (ClassNotFoundException cnfe) {
System.out.println("Problema codebase. Esco.");
login=false;
} catch (IOException ioe) {
System.out.println("Problema di IO. Esco.");
ioe.printStackTrace();
login=false;
}
}
}
}