Example usage for com.google.gson JsonObject toString

List of usage examples for com.google.gson JsonObject toString

Introduction

In this page you can find the example usage for com.google.gson JsonObject toString.

Prototype

@Override
public String toString() 

Source Link

Document

Returns a String representation of this element.

Usage

From source file:adminservlets_Json.DetailedViewJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//from   w  w w.j  a  v a  2 s .co  m
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String id = request.getParameter("id");
    ArrayList al;
    AdminClass_DetailedView ad = new AdminClass_DetailedView();
    PrintWriter out = response.getWriter();

    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();

    if ("U".equals(String.valueOf(id.charAt(0)))) {

        al = ad.userDetailedView(id.substring(1));

        myObj.addProperty("username", (String) al.get(0));
        myObj.addProperty("email", (String) al.get(1));
        myObj.addProperty("tel", (String) al.get(2));
        myObj.addProperty("reg", (String) al.get(3));
        myObj.addProperty("status", (String) al.get(4));
        myObj.addProperty("activation", (String) al.get(5));
        myObj.addProperty("login", (String) al.get(6));
        myObj.addProperty("ads", (String) al.get(7));

    } else if ("M".equals(String.valueOf(id.charAt(0)))) {

        al = ad.messageDetailedView(id.substring(1));
        myObj.addProperty("id", (String) al.get(0));
        myObj.addProperty("sender", (String) al.get(1));
        myObj.addProperty("reciever", (String) al.get(2));
        myObj.addProperty("content", (String) al.get(3));
        myObj.addProperty("time", (String) al.get(4));

    } else if ("1".equals(String.valueOf(id.charAt(0)))) {

        al = ad.reportItemDetailedView(id.substring(1));
        myObj.addProperty("id", (String) al.get(0));
        myObj.addProperty("email", (String) al.get(1));
        myObj.addProperty("reason", (String) al.get(2));
        myObj.addProperty("message", (String) al.get(3));
        myObj.addProperty("item", (String) al.get(4));
        myObj.addProperty("title", (String) al.get(5));

    } else if ("2".equals(String.valueOf(id.charAt(0)))) {
        al = ad.reportInquiryDetailedView(id.substring(1));
        myObj.addProperty("id", (String) al.get(0));
        myObj.addProperty("item", (String) al.get(1));
        myObj.addProperty("message_to", (String) al.get(2));
        myObj.addProperty("message_from", (String) al.get(3));
        myObj.addProperty("message", (String) al.get(4));
        myObj.addProperty("message_time", (String) al.get(5));
        myObj.addProperty("response", (String) al.get(6));
        myObj.addProperty("response_time", (String) al.get(7));

    } else if ("3".equals(String.valueOf(id.charAt(0)))) {
        al = ad.reportMessageDetailedView(id.substring(1));
        myObj.addProperty("id", (String) al.get(0));
        myObj.addProperty("sender", (String) al.get(1));
        myObj.addProperty("reciever", (String) al.get(2));
        myObj.addProperty("content", (String) al.get(3));
        myObj.addProperty("message_time", (String) al.get(4));
        myObj.addProperty("reported_time", (String) al.get(5));

    } else {
        String tilte = ad.itemDetailedView(String.valueOf(id.substring(1)));
        myObj.addProperty("id", tilte);

    }
    out.write(myObj.toString());
    out.close();
}

From source file:adminservlets_Json.GetStateJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*  w ww . ja  v  a  2 s. c  om*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    AdminClass_CurrentState ac = new AdminClass_CurrentState();
    String state = String.valueOf(ac.getState());

    PrintWriter out = response.getWriter();

    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();
    myObj.addProperty("state", state);

    out.write(myObj.toString());
    out.close();
}

From source file:adminservlets_Json.Load_BlacklistUsersJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*w  w  w.  j  av a 2  s.c  o  m*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    AdminClass_BlacklistedEmails ab = new AdminClass_BlacklistedEmails();

    ArrayList blacklistedEmails = (ArrayList) ab.getBlacklistedEmails();//loading blacklisted emails
    PrintWriter out = response.getWriter();

    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();

    JsonElement x = gson.toJsonTree(blacklistedEmails);
    myObj.add("blacklistedEmails", x);

    out.write(myObj.toString());
    out.close();
}

From source file:adminservlets_Json.MsgJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*from ww  w. jav  a2 s .co m*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    AdminClass_Message am = new AdminClass_Message();
    String filter;
    if (request.getParameter("filter") == null) {
        filter = " ";
    } else {
        filter = request.getParameter("filter");
    }

    ArrayList al = am.allMessages(filter);
    Iterator itr = al.iterator();
    AdminClass_Message received = null;
    PrintWriter out = response.getWriter();

    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();

    out.write(myObj.toString());
    out.close();
    while (itr.hasNext()) {
        Object a = itr.next();
        received = (AdminClass_Message) a;
        if (request.getParameter("sid") == null || "".equals(request.getParameter("sid"))) {
            String time = received.getTimeStamp();
            if ("0".equals(received.getState())) {
                myObj.addProperty("sender", (String) received.getSender());
                myObj.addProperty("content", (String) received.getContent());
                myObj.addProperty("time", (String) time);
                myObj.addProperty("status", "style=\"background-color:#E2DEE3;\"");

            } else {
                myObj.addProperty("sender", (String) received.getSender());
                myObj.addProperty("content", (String) received.getContent());
                myObj.addProperty("time", (String) time);
                myObj.addProperty("status", "");

            }
        } else {
            if (received.getSender().contains(request.getParameter("sid"))) {
                String time = am.timeDiff(received.getTimeStamp());
                if ("0".equals(received.getState())) {
                    myObj.addProperty("sender", (String) received.getSender());
                    myObj.addProperty("content", (String) received.getContent());
                    myObj.addProperty("time", (String) time);
                    myObj.addProperty("status", "style=\"background-color:#E2DEE3;\"");

                } else {
                    myObj.addProperty("sender", (String) received.getSender());
                    myObj.addProperty("content", (String) received.getContent());
                    myObj.addProperty("time", (String) time);
                    myObj.addProperty("status", "");

                }

            }
        }

    }
}

From source file:adminservlets_Json.UpdateStateJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*  w  w w .j  a v  a2 s.  c  om*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    AdminClass_CurrentState ac = new AdminClass_CurrentState();
    String state = request.getParameter("status");
    System.out.println(state);
    if ("true".equals(state)) {
        ac.setOnline();
    } else {
        ac.setOffline();
    }
    PrintWriter out = response.getWriter();

    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();

    out.write(myObj.toString());
    out.close();
}

From source file:adminservlets_Json.UserBlacklistJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//  w  ww.j a v  a 2  s  . c  o  m
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    AdminClass_BlacklistedEmails ab = new AdminClass_BlacklistedEmails();
    AdminClass_SendMail as = new AdminClass_SendMail();
    AdminClass_Links al = new AdminClass_Links();
    String alert = null;
    if (request.getParameter("email_unblock") != null) { //checking blacklist action
        boolean exists = ab.getEnteredEmail(request.getParameter("email_unblock"));//checking exsistence

        if (exists == true) {
            int result = ab.unblockEmail(request.getParameter("email_unblock"));//unblacklist an email

            if (result == 1) { //returning notification of the success
                alert = "<button class=\"btn btn-green\"><i  class=\""
                        + "glyphicon glyphicon-ok-sign\"></i></button><br><strong>"
                        + "Unblocked !</strong> Email- " + request.getParameter("email_unblock") + "";

            } else {
                alert = "<button class=\"btn btn-red\">" //returning notification of the failure
                        + "<i  class=\"glyphicon glyphicon-remove-circle\"></i></button><br><strong>"
                        + "Failed !</strong> Email- " + request.getParameter("email_unblock") + "";

            }

        } else { //returning notification of the failure
            alert = "<button class=\"btn btn-red\"><i  class=\""
                    + "glyphicon glyphicon-remove-circle\"></i></button><br><strong>"
                    + "Failed !</strong> Email- " + request.getParameter("email_unblock") + " "
                    + "Does not exsist in the list";

        }

    } else if (request.getParameter("email_block") != null) { //checking if the email is null
        boolean exists = ab.getEnteredEmail(request.getParameter("email_block"));//checking if the email is already blacklisted

        if (exists == true) { //returning notification of  the failure
            alert = "<button class=\"btn btn-red\"><i  class=\""
                    + "glyphicon glyphicon-remove-circle\"></i></button><br><strong>"
                    + "Failed !</strong> Email- " + request.getParameter("email_block") + " "
                    + "is already blacklisted";

        } else {
            boolean exsist = ab.checkUserEmail(request.getParameter("email_block")); //check for current user
            if (exsist == true) {

                ab.RemoveUser(request.getParameter("email_block")); //remove current user content
            }
            int result = ab.blockEmail(request.getParameter("email_block")); //blacklisting an email

            String subject = "About temporarily disabeling your account";

            String content = "Your account in Superb.lk is temporarily disabled due to invalid activity or policy violations.\n\n"
                    + "Visit the link for common reasons and policy violations cause accounts to be suspended.\n";

            String link = "<a href='" + al.getPolicies()
                    + "' target='blank'><p>Click here to view our privacy ploicies</p></a>";
            int mail_result = as.mailClass(request.getParameter("email_block"), subject, content, link);//sending mail to the user

            if (result == 1) { //returning notification of the success 
                alert = "<button class=\"btn btn-green\"><i  class=\""
                        + "glyphicon glyphicon-ok-sign\"></i></button><br><strong>"
                        + "Blocked !</strong> Email- " + request.getParameter("email_block") + "";

            } else {
                alert = "<button class=\"btn btn-red\"><i  class=\""
                        + "glyphicon glyphicon-remove-circle\"></i></button><br><strong>"
                        + "Failed !</strong> Email- " + request.getParameter("email_block") + "";

            }

        }
    }

    PrintWriter out = response.getWriter();

    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();

    myObj.addProperty("result", alert);

    out.write(myObj.toString());
    out.close();
}

From source file:adminservlets_reports.BlockAdJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//from  ww  w .ja  v a 2 s.  c om
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String alert = null;

    if (request.getParameter("itemBA") != null && request.getParameter("reportBA") != null) {
        AdminClass_ReportedItems art = new AdminClass_ReportedItems();
        AdminClass_SendMail as = new AdminClass_SendMail();
        AdminClass_Message am = new AdminClass_Message();
        AdminClass_BlockedItems ab = new AdminClass_BlockedItems();
        AdminClass_Links al = new AdminClass_Links();

        String reciever = art.getUserEmail(request.getParameter("toBA"));
        String subject = request.getParameter("subjectBA");
        String edit_link = "<a href='" + al.getAdEdit()
                + "' target='blank'><p>Click here to Edit your advertisement</p></a>";

        String content = request.getParameter("contentBA_header") + "<br/><br/>"
                + request.getParameter("contentBA_body");

        String itemId = request.getParameter("itemBA");
        String reason = "Blocked due to- " + request.getParameter("reasonBA");

        String inbox_content = request.getParameter("contentBA_header") + request.getParameter("contentBA_body")
                + "\n\nPlease update your ad" + "\n" + "Regards,\n" + "The support team at Superb.lk\n" + "\n";

        int result = ab.blockItem(itemId, reason); //blocking advertiesment
        int state = art.updateViewState(request.getParameter("reportBA"));//update report status
        int inbox_result = am.sendMessage(inbox_content, request.getParameter("toBA"));
        int result2 = as.mailClass(reciever, subject, content, edit_link);//sending mail to the user

        if (result == 1 && state == 1) {

            alert = "<button class=\"btn btn-green\">" //returning notification of the success 
                    + "<i  class=\"glyphicon glyphicon-ok-sign\">"
                    + "</i></button><br><strong>Blocked !</strong>  Advertiesment number " + itemId + "  ";

        } else {

            alert = "<button class=\"btn btn-red\">" //returning notification of the failure 
                    + "<i  class=\"glyphicon glyphicon-remove-circle\">"
                    + "</i></button><br><strong>Failed!</strong> Advertiesment number " + itemId
                    + " Try again.";

        }
    }
    PrintWriter out = response.getWriter();

    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();

    myObj.addProperty("result", alert);

    out.write(myObj.toString());
    out.close();
}

From source file:adminservlets_reports.BlockAdUserJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//from   w  ww .  ja v  a  2  s  .c om
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    AdminClass_ReportedItems art = new AdminClass_ReportedItems();
    AdminClass_SendMail as = new AdminClass_SendMail();
    AdminClass_Links al = new AdminClass_Links();
    String alert = null;

    if (request.getParameter("toBU") != null && request.getParameter("reportBU") != null) {

        AdminClass_BlockedUsers ab = new AdminClass_BlockedUsers();

        String reciever = art.getUserEmail(request.getParameter("toBU"));
        String subject = "About temporarily disabeling your account";

        String content = "Your account in Superb.lk is temporarily disabled due to invalid activity or policy violations.\n\n"
                + "Visit the link for common reasons and policy violations cause accounts to be suspended.\n";

        String link = "<a href='" + al.getPolicies()
                + "' target='blank'><p>Click here to view our privacy ploicies</p></a>";

        int result1 = ab.RemoveUser(request.getParameter("toBU")); //removing user
        int result2 = ab.BlacklistUser(reciever); //Blacklisting user
        int mail_result = as.mailClass(reciever, subject, content, link);//sending mail to the user

        if (result1 == 1 && result2 == 1) {

            alert = "<button class=\"btn btn-green\">" //returning notification of the success 
                    + "<i  class=\"glyphicon glyphicon-ok-sign\">"
                    + "</i></button><br><strong>Blocked !</strong>" + "  User " + reciever + "  ";

        } else {

            alert = "<button class=\"btn btn-red\">" //returning notification of the failure 
                    + "<i  class=\"glyphicon glyphicon-remove-circle\">"
                    + "</i></button><br><strong>Failed!</strong>" + " User " + reciever + " Try again.";

        }
    }
    PrintWriter out = response.getWriter();

    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();

    myObj.addProperty("result", alert);

    out.write(myObj.toString());
    out.close();
}

From source file:adminservlets_reports.BlockInquiryJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/* w w w  .  j ava 2s  .c  o  m*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    AdminClass_ReportedItems art = new AdminClass_ReportedItems();
    AdminClass_ReportedMessages arm = new AdminClass_ReportedMessages();
    AdminClass_ReportedInquiries ari = new AdminClass_ReportedInquiries();
    AdminClass_Message am = new AdminClass_Message();
    String alert = null;

    if (request.getParameter("toBI") != null && request.getParameter("inquiryBI") != null) {

        AdminClass_BlockedInquiries ab = new AdminClass_BlockedInquiries();

        String inbox_content = "Hello,\n" + "\n" + "Your Inquiry \"" + request.getParameter("inquiry_contentBI")
                + "\" in superb.lk is  disabled due to policy violations.\n\n"
                + "Policy violations cause accounts to be suspended.\n" + "\n\n" + "Regards,\n"
                + "The support team at Superb.lk\n";

        int inbox_result = am.sendMessage(inbox_content, request.getParameter("toBI"));

        int result = ab.blockInquiries(request.getParameter("inquiryBI"));//blocking inquiry

        if (result == 1) {

            alert = "<button class=\"btn btn-green\">" //returning notification of the success 
                    + "<i  class=\"glyphicon glyphicon-ok-sign\">"
                    + "</i></button><br><strong>Blocked !</strong>" + "  Inquiry id "
                    + request.getParameter("inquiryBI") + "";

        } else {
            alert = "<button class=\"btn btn-red\">" //returning notification of the failure 
                    + "<i  class=\"glyphicon glyphicon-remove-circle\">"
                    + "</i></button><br><strong>Failed!</strong>" + " Inquiry id "
                    + request.getParameter("inquiryBI") + " Try again.";

        }

    }
    PrintWriter out = response.getWriter();

    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();

    myObj.addProperty("result", alert);

    out.write(myObj.toString());
    out.close();
}

From source file:adminservlets_reports.BlockInquiryUser.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*from  www. ja va  2 s .  co m*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    AdminClass_ReportedItems art = new AdminClass_ReportedItems();
    AdminClass_ReportedMessages arm = new AdminClass_ReportedMessages();
    AdminClass_ReportedInquiries ari = new AdminClass_ReportedInquiries();
    AdminClass_SendMail as = new AdminClass_SendMail();
    AdminClass_Links al = new AdminClass_Links();
    String alert = null;

    if (request.getParameter("toBIU") != null) {

        AdminClass_BlockedUsers ab = new AdminClass_BlockedUsers();

        String reciever = art.getUserEmail(request.getParameter("toBIU"));
        String subject = "About temporarily disabeling your account";

        String content = "Your account in Superb.lk is temporarily disabled due to invalid activity or policy violations.\n\n"
                + "Visit the link for common reasons and policy violations cause accounts to be suspended.\n";

        String link = "<a href='" + al.getPolicies()
                + "' target='blank'><p>Click here to view our privacy ploicies</p></a>";

        int mail_result = as.mailClass(reciever, subject, content, link);//sending mail to the user

        int result1 = ab.RemoveUser(request.getParameter("toBIU")); //removing user
        int result2 = ab.BlacklistUser(reciever); //blacklisting user

        if (result1 == 1 && result2 == 1) {
            alert = "<button class=\"btn btn-green\">" //returning notification of the success 
                    + "<i  class=\"glyphicon glyphicon-ok-sign\">"
                    + "</i></button><br><strong>Blocked !</strong>  User " + reciever + "";

        } else {
            alert = "<button class=\"btn btn-red\">" //returning notification of the failure 
                    + "<i class=\"glyphicon glyphicon-remove-circle\">"
                    + "</i></button><br><strong>Failed!</strong> User " + reciever + " Try again.";

        }

    }
    PrintWriter out = response.getWriter();

    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();

    myObj.addProperty("result", alert);

    out.write(myObj.toString());
    out.close();
}