final String oldpass = new String(oldpassword.getPassword());
final String pass = new String(password.getPassword());
final String repass = new String(repassword.getPassword());
File file = new File("login");
if(file.exists()){
Json localJson= null;
String md5pass = "";
try {
md5pass = md5(oldpass);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(cLoginWindow.class.getName()).log(Level.SEVERE, "Couldn't get md5 hash of the password", ex);
}
try {
String strLine = "";
DataInputStream in = new DataInputStream(new FileInputStream(file));
BufferedReader br = new BufferedReader(new InputStreamReader(in));
try {
while ((strLine = br.readLine())!= null) {
break;
}
in.close();
localJson = Json.read(strLine);
} catch (IOException ex) {
Logger.getLogger(cLoginWindow.class.getName()).log(Level.SEVERE,"Cannot read line in the file", ex);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(cLoginWindow.class.getName()).log(Level.SEVERE,"File not found", ex);
}
final String id = localJson.at("id").asString();
if(!md5pass.equals(localJson.at("password").asString())){
noErrors = false;
error += "Old password dont match \n";
}
if(!(pass.equals(repass)) || pass.length() == 0){
noErrors = false;
error += "Both the password must match with each other \n";
}
if(noErrors){
Map<String,String> rdata = new HashMap<String, String>(){{put("id",id);put("oldpass",oldpass);put("newpass",pass);}};
bFetchURL url = new bFetchURL("http://binaydevkota.com/javabus/changepassword.php",rdata,"GET");
String returnedText = null;
try {
returnedText = url.content();
} catch (NullPointerException ex){
JOptionPane.showMessageDialog(null, "Sorry cannot connect to server, change password failed", "Error", JOptionPane.ERROR_MESSAGE);
}
catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Sorry cannot connect to server, change password failed", "Error", JOptionPane.ERROR_MESSAGE);
}
Json returnedJson = Json.read(returnedText);
if(!returnedJson.at("error").isNull()){
JOptionPane.showMessageDialog(null, returnedJson.at("error").asString(), "Error", JOptionPane.ERROR_MESSAGE);
}
if(!returnedJson.at("info").isNull()){
JOptionPane.showMessageDialog(null, returnedJson.at("info").asString(), "Info", JOptionPane.INFORMATION_MESSAGE);
}
if(!returnedJson.at("success").isNull()){
File files = new File("login");
files.delete();
new cLoginWindow();
dispose();
}