IoT CAS Frameworks/IOT_Server/Server_Admin_Framework/source/ConsumerAPI/src/java/AV_Action/AV_PublishIP.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        String id = null;
        String ipAddress = null;
        FileWriter fw = null;
        FileReader fr = null;
        try {
            System.out.println(" AV_PublishIP Started ");
            id = request.getParameter("id");
            ipAddress = request.getRemoteAddr();
            file = new File(System.getProperty("user.home") + System.getProperty("file.separator") + "IP_Address.txt");
            System.out.println("file===>" + file);
            if (!file.exists()) {
                file = new File(System.getProperty("user.home") + System.getProperty("file.separator") + "IP_Address.txt");
                file.createNewFile();
                fw = new FileWriter(file);
                bw = new BufferedWriter(fw);
                bw.write(id + "=" + ipAddress);
                bw.newLine();
                bw.flush();
                // log.info("first id " + id + "=" + ipAddress + " is added ");
                System.out.println("first id " + id + "=" + ipAddress + " is added ");
            } else {
                fr = new FileReader(file);
                br = new BufferedReader(fr);
                String oldIP = null;
                String updatedIP = null;
                String currentID = null, oldText = "";;
                while ((currentID = br.readLine()) != null) {
                    hs.put(currentID.split("=")[0].trim(), currentID.split("=")[1].trim());
                    oldText += currentID + "\r";
                    if (hs.containsKey(id)) {
                        oldIP = id + "=" + (String) hs.get(id);
                    }
                }
                br.close();
                if (hs.containsKey(id)) {
                    if (!(hs.get(id).toString().equalsIgnoreCase(ipAddress))) {
                        System.out.println(" id already exist");
                        updatedIP = oldText.replaceAll(oldIP, id + "=" + ipAddress);
                        fw = new FileWriter(file);
                        bw = new BufferedWriter(fw);
                        bw.write(updatedIP);
                        bw.newLine();
                        bw.flush();
                        //   log.info(id + "=" + ipAddress + " is updated");
                        System.out.println(id + "=" + ipAddress + " is updated");
                    }
                } else if (!hs.containsKey(id)) {
                    System.out.println("id not exist");
                    fw = new FileWriter(file, true);
                    bw = new BufferedWriter(fw);
                    bw.write(id + "=" + ipAddress);
                    bw.newLine();
                    bw.flush();
                    // log.info(id + "=" + ipAddress + " is added");
                    System.out.println(id + "=" + ipAddress + " is added");
                }
            }
            if (fr != null) {
                fr.close();
            }
            fr = null;
            if (fw != null) {
                fw.close();
            }
            fw = null;
            AV_FrontController.readFile();
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            if (fr != null) {
                fr.close();
            }
            fr = null;
            if (fw != null) {
                fw.close();
            }
            fw = null;
        }

    }
/**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
Processes requests for both HTTP
GET and
POST methods.

@param request servlet request
@param response servlet response
@throws ServletException if a servlet-specific error occurs
@throws IOException if an I/O error occurs
Processes requests for both HTTP GET and POST methods . @param request servlet request @param response servlet response @throws ServletException if a servlet - specific error occurs @throws IOException if an I / O error occurs
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String id = null;
        String ipAddress = null;
        FileWriter fw = null;
        FileReader fr = null;
        try {
            System.out.println(" AV_PublishIP Started ");
            id = request.getParameter("id");
            ipAddress = request.getRemoteAddr();
            file = new File(System.getProperty("user.home") + System.getProperty("file.separator") + "IP_Address.txt");
            System.out.println("file===>" + file);
            if (!file.exists()) {
                file = new File(System.getProperty("user.home") + System.getProperty("file.separator") + "IP_Address.txt");
                file.createNewFile();
                fw = new FileWriter(file);
                bw = new BufferedWriter(fw);
                bw.write(id + "=" + ipAddress);
                bw.newLine();
                bw.flush();
                System.out.println("first id " + id + "=" + ipAddress + " is added ");
            } else {
                fr = new FileReader(file);
                br = new BufferedReader(fr);
                String oldIP = null;
                String updatedIP = null;
                String currentID = null, oldText = "";;
                while ((currentID = br.readLine()) != null) {
                    hs.put(currentID.split("=")[0].trim(), currentID.split("=")[1].trim());
                    oldText += currentID + "\r";
                    if (hs.containsKey(id)) {
                        oldIP = id + "=" + (String) hs.get(id);
                    }
                }
                br.close();
                if (hs.containsKey(id)) {
                    if (!(hs.get(id).toString().equalsIgnoreCase(ipAddress))) {
                        System.out.println(" id already exist");
                        updatedIP = oldText.replaceAll(oldIP, id + "=" + ipAddress);
                        fw = new FileWriter(file);
                        bw = new BufferedWriter(fw);
                        bw.write(updatedIP);
                        bw.newLine();
                        bw.flush();
                        System.out.println(id + "=" + ipAddress + " is updated");
                    }
                } else if (!hs.containsKey(id)) {
                    System.out.println("id not exist");
                    fw = new FileWriter(file, true);
                    bw = new BufferedWriter(fw);
                    bw.write(id + "=" + ipAddress);
                    bw.newLine();
                    bw.flush();
                    System.out.println(id + "=" + ipAddress + " is added");
                }
            }
            if (fr != null) {
                fr.close();
            }
            fr = null;
            if (fw != null) {
                fw.close();
            }
            fw = null;
            AV_FrontController.readFile();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            if (fr != null) {
                fr.close();
            }
            fr = null;
            if (fw != null) {
                fw.close();
            }
            fw = null;
        }
    }
protected void processRequest ( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException { response . setContentType ( "text/html;charset=UTF-8" ) ; String id = null ; String ipAddress = null ; FileWriter fw = null ; FileReader fr = null ; try { System . out . println ( " AV_PublishIP Started " ) ; id = request . getParameter ( "id" ) ; ipAddress = request . getRemoteAddr ( ) ; file = new File ( System . getProperty ( "user.home" ) + System . getProperty ( "file.separator" ) + "IP_Address.txt" ) ; System . out . println ( "file===>" + file ) ; if ( ! file . exists ( ) ) { file = new File ( System . getProperty ( "user.home" ) + System . getProperty ( "file.separator" ) + "IP_Address.txt" ) ; file . createNewFile ( ) ; fw = new FileWriter ( file ) ; bw = new BufferedWriter ( fw ) ; bw . write ( id + "=" + ipAddress ) ; bw . newLine ( ) ; bw . flush ( ) ; System . out . println ( "first id " + id + "=" + ipAddress + " is added " ) ; } else { fr = new FileReader ( file ) ; br = new BufferedReader ( fr ) ; String oldIP = null ; String updatedIP = null ; String currentID = null , oldText = "" ; ; while ( ( currentID = br . readLine ( ) ) != null ) { hs . put ( currentID . split ( "=" ) [ 0 ] . trim ( ) , currentID . split ( "=" ) [ 1 ] . trim ( ) ) ; oldText += currentID + "\r" ; if ( hs . containsKey ( id ) ) { oldIP = id + "=" + ( String ) hs . get ( id ) ; } } br . close ( ) ; if ( hs . containsKey ( id ) ) { if ( ! ( hs . get ( id ) . toString ( ) . equalsIgnoreCase ( ipAddress ) ) ) { System . out . println ( " id already exist" ) ; updatedIP = oldText . replaceAll ( oldIP , id + "=" + ipAddress ) ; fw = new FileWriter ( file ) ; bw = new BufferedWriter ( fw ) ; bw . write ( updatedIP ) ; bw . newLine ( ) ; bw . flush ( ) ; System . out . println ( id + "=" + ipAddress + " is updated" ) ; } } else if ( ! hs . containsKey ( id ) ) { System . out . println ( "id not exist" ) ; fw = new FileWriter ( file , true ) ; bw = new BufferedWriter ( fw ) ; bw . write ( id + "=" + ipAddress ) ; bw . newLine ( ) ; bw . flush ( ) ; System . out . println ( id + "=" + ipAddress + " is added" ) ; } } if ( fr != null ) { fr . close ( ) ; } fr = null ; if ( fw != null ) { fw . close ( ) ; } fw = null ; AV_FrontController . readFile ( ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } finally { if ( br != null ) { try { br . close ( ) ; } catch ( IOException ex ) { ex . printStackTrace ( ) ; } } if ( bw != null ) { try { bw . close ( ) ; } catch ( IOException ex ) { ex . printStackTrace ( ) ; } } if ( fr != null ) { fr . close ( ) ; } fr = null ; if ( fw != null ) { fw . close ( ) ; } fw = null ; } }
Processes requests for both HTTP
 <code>GET</code> and
 <code>POST</code> methods.
Processes requests for both HTTP <code > GET< / code > and <code > POST< / code > methods .
// log.info("first id " + id + "=" + ipAddress + " is added "); //   log.info(id + "=" + ipAddress + " is updated"); // log.info(id + "=" + ipAddress + " is added");