Package mjson

Examples of mjson.Json


    /**
     * Initialized the JFrame and Tabbed Panel on it<br />
     * Fetches the search token from reittiopas site<br />
     */
    public eMainWindow() {
        Json AllData  = null;
        String strLine = "";
        BufferedReader br = null;
        try { // read all name of the places from file LocationData saved in JSON format
            DataInputStream in = new DataInputStream(getClass().getResource("LocationData").openStream());
            br = new BufferedReader(new InputStreamReader(in));
            while ((strLine = br.readLine())!= null)   {
                break;
            }
            AllData = Json.read(strLine); //convert text to JSON object
            in.close();
        } catch (IOException ex) {
            Logger.getLogger(cLoginWindow.class.getName()).log(Level.SEVERE,"Cannot read the file with data", ex);
        }
       
        // Reittiopas uses token to verify the requests which comes in the following link(http://reittiopas.turku.fi/fi/config.js_b8d9ad995abbbe6251c6c1c2b1017cd0e71e286b.php), which is normal javascript file
        String tokenText=null;
       
        //request for the token
        bFetchURL requestForToken = new bFetchURL("http://reittiopas.turku.fi/fi/config.js_b8d9ad995abbbe6251c6c1c2b1017cd0e71e286b.php", new HashMap<String, String>(){{put("","");}}, "GET");
        try {
            tokenText = requestForToken.content();
        }
        catch (NullPointerException ex){
            JOptionPane.showMessageDialog(null, "Sorry couldn't read the URL for search token, exiting the application", "Fatal error", JOptionPane.ERROR_MESSAGE);
            System.exit(-1);
        }
        catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "Sorry couldn't read the URL for token, exiting the application", "Fatal error", JOptionPane.ERROR_MESSAGE);
            System.exit(-1);
        }
        try{ //parse the token, if we got the response.
            token = Json.read(tokenText.substring(11, tokenText.length())).at("token").asString();
        }
        catch(NullPointerException ex){ //if token couldn't be parsed application cannot initialize i.e. there is no use of application
            JOptionPane.showMessageDialog(null, "Sorry couldn't parse the search token, exiting the application");
            System.exit(-1);
        }
        Datas = AllData.asJsonMap(); //convert all the location's object into Map<String, Json>
        List a = new ArrayList();
        a.add("");
        for(Entry<String, Json> entry : Datas.entrySet()) { //break each item from Map and make it to a List
            String key = entry.getKey();
            a.add(key);
View Full Code Here


                        break;
                    }
                }
                for(Entry<String, Json> entry : Datas.entrySet()) { //get which place is selected in combobox and then get x,y point from locationdata
                    String key = entry.getKey();
                    Json value = entry.getValue();
                    if(key==getFrom){
                        sendFromX  = value.at("x").asString();
                        sendFromY = value.at("y").asString();
                        break;
                    }
                }
                for(Entry<String, Json> entry : Datas.entrySet()) {
                    String key = entry.getKey();
                    Json value = entry.getValue();
                    if(key==getTo){
                        sendToX = value.at("x").asString();
                        sendToY = value.at("y").asString();
                        break;
                    }
                }
                DateFormat formatter = new SimpleDateFormat("yyyy:M:d:H:m"); // set a date format, which we suppose to get from user input
                Date date = null;
View Full Code Here

                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();
                            }
View Full Code Here

                             JOptionPane.showMessageDialog(null, "Cannot find login server", "Fatal error", JOptionPane.ERROR_MESSAGE);
                             System.exit(-1);
                        }
                       
                        // parse returned content as JSON using msjon library(msjon.jar), more info about msjon.jar is on main.java page
                        Json returnedJson = Json.read(returnedText);
                        System.out.print(returnedText);
                        if(!returnedJson.at("error").isNull()){ //check if server has sent any error or not, if there is display the error
                            JOptionPane.showMessageDialog(null, returnedJson.at("error").asString(), "Error", JOptionPane.ERROR_MESSAGE);
                        }
                        if(!returnedJson.at("info").isNull()){//check if server has sent any info or not, if there is display the info
                            JOptionPane.showMessageDialog(null, returnedJson.at("info").asString(), "Info", JOptionPane.INFORMATION_MESSAGE);
                        }
                        if(!returnedJson.at("success").isNull()){ //if response has success message then
                            Writer output = null;
                            try {
                                 if(!file.exists()){ // create a new login file
                                     file.createNewFile();
                                 }
                                output = new BufferedWriter(new FileWriter(file,false));
                                output.write(returnedJson.at("user").toString()); //put the JSON response of user information into that file
                                output.close();
                            } catch (IOException ex) {
                                Logger.getLogger(cLoginWindow.class.getName()).log(Level.SEVERE,"Cannot create/write to the file", ex);
                            }
                            dispose(); //close this window since login is success
View Full Code Here

TOP

Related Classes of mjson.Json

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.