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:adams.scripting.processor.JsonProcessor.java

License:Open Source License

/**
 * Turns the command properties and payload into a single string to send.
 *
 * @param header   the header data/*from w ww  . ja  v  a 2 s.  c  o  m*/
 * @param payload   the payload
 * @return      the assembled string
 */
@Override
public String format(Properties header, byte[] payload) {
    JsonObject result;
    String data;
    JsonObject objHeader;
    Gson gson;

    if (payload.length == 0)
        data = "";
    else
        data = Base64.encodeBase64String(payload);

    objHeader = new JsonObject();
    for (String key : header.keySetAll())
        objHeader.addProperty(key, header.getProperty(key));

    result = new JsonObject();
    result.add("header", objHeader);
    result.addProperty("payload", data);

    if (m_PrettyPrinting) {
        gson = new GsonBuilder().setPrettyPrinting().create();
        return gson.toJson(result);
    } else {
        return result.toString();
    }
}

From source file:adminservlets_ads.Apr_ReviewAdsJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//from   w w w  .  j a v a 2s .com
 * @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_ReviewAds ar = new AdminClass_ReviewAds();
    AdminClass_SendMail as = new AdminClass_SendMail();
    AdminClass_Message am = new AdminClass_Message();
    String alert = null;
    AdminClass_Links al = new AdminClass_Links();

    String reciever = ar.getUserEmail(request.getParameter("to"));
    String subject = "Approval of your ad \"" + request.getParameter("subject") + "\"";
    String view_link = "<a href='" + al.getAdView()
            + "' target='blank'><p>Click here to view your advertisement</p></a>";

    String content = "Your ad \"" + request.getParameter("subject") + "\", successfully posted on Superb.lk.\n"
            + "\n" + "To view your ad, please click the following link. \n"
            + "If you have any questions, feel free to reply to the email and we will get back to you.\n";

    String inbox_content = "Hello,\n" + "\n" + "Your ad \"" + request.getParameter("subject")
            + "\", successfully posted on Superb.lk.\n" + "\n" + "Regards,\n"
            + "The support team at Superb.lk\n" + "\n";

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

    String action = request.getParameter("action");
    String item = request.getParameter("item");
    int result;
    if ("Approve".equals(action) && item != null) {

        result = ar.approveAd(item); //Approving advertiesment
        int mail_result = as.mailClass(reciever, subject, content, view_link);//sending mail to the user

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

        } else {
            alert = "<button class=\"btn btn-red\">" //returning notification of the the failure
                    + "<i  class=\"glyphicon glyphicon-remove-circle\">"
                    + "</i></button><br><strong>Failed!</strong>" + " Advertiesment number "
                    + request.getParameter("item") + " 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_ads.Load_ReviewAdsJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/* ww w  .jav a 2  s.com*/
 * @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_ReviewAds ar = new AdminClass_ReviewAds();
    ArrayList reviewAds = ar.reviewAds(); //loading ad reviews
    PrintWriter out = response.getWriter();

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

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

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

}

From source file:adminservlets_ads.Mod_ReviewAdsJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*  ww w . ja va 2  s.com*/
 * @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_ReviewAds ar = new AdminClass_ReviewAds();
    AdminClass_SendMail as = new AdminClass_SendMail();
    AdminClass_Message am = new AdminClass_Message();
    AdminClass_Links al = new AdminClass_Links();
    String alert = null;

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

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

        String content = request.getParameter("content_header") + "<br/><br/>"
                + request.getParameter("content_body");
        String itemId = request.getParameter("itemname");
        String reason = "Modified due to- " + request.getParameter("reason");

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

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

        int result = ar.modifyAds(itemId, reason); //updating advertiesment status
        int result2 = as.mailClass(reciever, subject, content, edit_link);//sending mail to the user

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

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

        }
    }
    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_ads.Rmv_ReviewAdsJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//  w  w w  . java2 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_ReviewAds ar = new AdminClass_ReviewAds();
    AdminClass_SendMail as = new AdminClass_SendMail();
    AdminClass_Message am = new AdminClass_Message();
    String alert = null;

    String reciever = ar.getUserEmail(request.getParameter("to"));
    String subject = "Refusal of your Ad \"" + request.getParameter("subject") + "\"";

    String content = "Your ad \"" + request.getParameter("subject") + "\", cannot be posted on Superb.lk.\n"
            + "\n" + "The reason why we could not approve your ad:\n" + "\n"
            + "<br/><br/>- Illegal item or service\n"
            + "<br/>Your ad features an item or service that is illegal or not suitable, which we cannot allow on our site. Please post a new ad with suitable content.\n"
            + "<br/><br/>we do not allow any content that Superb.lk considers in its complete and unfettered discretion to be- \n"
            + "<br/>(i) offensive and/or inappropriate (including, without limitation, defamatory, threatening, hateful, or pornographic content);\n"
            + "\n" + "<br/>(ii) false, fraudulent, misleading or deceptive, or incorrect;\n" + "\n"
            + "<br/>(iv) Content that discloses anothers personal, confidential or proprietary information (including content that identifies a person, or that is capable of being used to identify a person without that person's consent)."
            + "\n"
            + "<br/>If you have any questions, feel free to reply to the email and we will get back to you.\n";

    String inbox_content = "Hello,\n" + "\n" + "Your ad \"" + request.getParameter("subject")
            + "\", cannot be posted on Superb.lk.\n" + "\n" + "The reason why we could not approve your ad:\n"
            + "\n" + "- Illegal item or service\n"
            + "Your ad features an item or service that is illegal or not suitable, which we cannot allow on our site. Please post a new ad with suitable content.\n"
            + "\n" + "Regards,\n" + "The support team at Superb.lk\n" + "\n";

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

    String action = request.getParameter("action");
    String item = request.getParameter("item");

    int result;

    if (("Remove".equals(action) && item != null)) {
        result = ar.removeAd(item); //removing advertiesment
        int mail_result = as.mailClass(reciever, subject, content, "");//sending mail to the user

        if (result == 1) {
            alert = "<button class=\"btn btn-green\">" //returning notification of the success 
                    + "<i  class=\"glyphicon glyphicon-ok-sign\">"
                    + "</i></button><br><strong>Removed!</strong>" + " Advertiesment number "
                    + request.getParameter("item") + "";
        } 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 "
                    + request.getParameter("item") + " 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_Json.CheckPassJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//from   w  w  w  .  ja va 2  s.  com
 * @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_LoginMethods lm = new AdminClass_LoginMethods();

    boolean result = lm.checkPass(request.getParameter("username"), request.getParameter("password"));
    if (result == true) {
        HttpSession session = request.getSession();
        session.setAttribute("login_change", "true");
    }
    PrintWriter out = response.getWriter();

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

    myObj.addProperty("result", result);

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

From source file:adminservlets_Json.CustomDataAdsJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//  w w w . ja  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 {
    String fd = request.getParameter("fd");
    String sd = request.getParameter("sd");
    String result;
    AdminClass_Overviewstats ao = new AdminClass_Overviewstats();
    boolean checkFD = ao.isValidDate(fd);
    boolean checkSD = ao.isValidDate(sd);

    if (checkFD == true && checkSD == true) {
        ArrayList al = ao.adsCustom(fd, sd);
        DecimalFormat twoDForm = new DecimalFormat("#.#");
        if (al.get(1).equals("0")) {
            result = "Ads : " + (String) al.get(1) + " Percentage: 0%";
        } else {
            result = "Ads: " + (String) al.get(1) + " Percentage: "
                    + twoDForm.format(
                            Float.parseFloat((String) al.get(1)) / Float.parseFloat((String) al.get(0)) * 100)
                    + "%";
        }

    } else {
        result = "Incorrect entry";
    }
    PrintWriter out = response.getWriter();

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

    myObj.addProperty("result", result);

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

From source file:adminservlets_Json.CustomDataJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*from w w  w .j  a v  a 2  s. com*/
 * @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 fd = request.getParameter("fd");
    String sd = request.getParameter("sd");
    String result;
    AdminClass_Overviewstats ao = new AdminClass_Overviewstats();
    boolean checkFD = ao.isValidDate(fd);
    boolean checkSD = ao.isValidDate(sd);

    if (checkFD == true && checkSD == true) {
        ArrayList al = ao.pagevisitCustom(fd, sd);
        DecimalFormat twoDForm = new DecimalFormat("#.#");
        if (al.get(1).equals("0")) {
            result = "Site visit: " + (String) al.get(1) + " Percentage: 0%";
        } else {
            result = "Site visit: " + (String) al.get(1) + " Percentage: "
                    + twoDForm.format(
                            Float.parseFloat((String) al.get(1)) / Float.parseFloat((String) al.get(0)) * 100)
                    + "%";
        }

    } else {
        result = "Incorrect entry";
    }
    PrintWriter out = response.getWriter();

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

    myObj.addProperty("result", result);

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

From source file:adminservlets_Json.CustomDataUserJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//from   w w  w.j  a v  a2s  . 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 fd = request.getParameter("fd");
    String sd = request.getParameter("sd");
    String result;
    AdminClass_Overviewstats ao = new AdminClass_Overviewstats();
    boolean checkFD = ao.isValidDate(fd);
    boolean checkSD = ao.isValidDate(sd);

    if (checkFD == true && checkSD == true) {
        ArrayList al = ao.usersCustom(fd, sd);
        DecimalFormat twoDForm = new DecimalFormat("#.#");
        if (al.get(1).equals("0")) {
            result = "Users : " + (String) al.get(1) + " Percentage: 0%";
        } else {
            result = "Users: " + (String) al.get(1) + " Percentage: "
                    + twoDForm.format(
                            Float.parseFloat((String) al.get(1)) / Float.parseFloat((String) al.get(0)) * 100)
                    + "%";
        }

    } else {
        result = "Incorrect entry";
    }
    PrintWriter out = response.getWriter();

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

    myObj.addProperty("result", result);

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

From source file:adminservlets_Json.DataCountsJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//from  w  w w. j  a  v  a  2  s . com
 * @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_NavbarTools an = new AdminClass_NavbarTools();
    AdminClass_Overviewstats ao = new AdminClass_Overviewstats();

    AdminClass_CurrentState ac = new AdminClass_CurrentState();
    ac.updateState();

    String messageCount = String.valueOf(an.messageCount());
    if ("0".equals(messageCount)) {
        messageCount = "";
    }
    String ads = ao.reviewAdsCount();
    if ("0".equals(ads)) {
        ads = "";
    }
    String reports = ao.reportCount();
    if ("0".equals(reports)) {
        reports = "";
    }
    PrintWriter out = response.getWriter();

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

    myObj.addProperty("messageCount", messageCount);
    myObj.addProperty("ads", ads);
    myObj.addProperty("reports", reports);
    myObj.addProperty("topads", "N/A");

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