/**
 * 
 */
package com.github.jerryxia.devhelperservletgaesample.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Administrator
 *
 */
@Slf4j
public class HomeServlet extends HttpServlet {
    private static final long serialVersionUID = -7201198165652905457L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestFirst = "requestFirst";
        HttpSession session = req.getSession();
        // session test
        if (session.getAttribute(requestFirst) == null) {
            String now = DateTime.now(DateTimeZone.UTC).toString("yyyy-MM-dd HH:mm:ss");
            log.info("first request time: {}", now);
            log.info("session: {}", session.getClass().toGenericString());
            session.setAttribute(requestFirst, now);
        } else {
            log.info("session storage data: {}", session.getAttribute(requestFirst));
        }

        String appName = query();

        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        resp.getWriter().print("<html>");
        resp.getWriter().print("<head>");
        resp.getWriter().print("<title>devhelper-servlet-gae-sample</title>");
        resp.getWriter().print("</head>");
        resp.getWriter().print("<h1>devhelper-servlet-gae-sample</h1>");
        resp.getWriter().print("<p>first request: ");
        resp.getWriter().print(session.getAttribute(requestFirst));
        resp.getWriter().print("</p>");

        resp.getWriter().print("<p>appName: ");
        resp.getWriter().print(appName);
        resp.getWriter().print("</p>");

        resp.getWriter().print("<p>now time: <span id=\"now\"></span></p>");
        resp.getWriter().print("<p><a href=\"/requestcapture/\" target=\"_blank\">requestcapture</a></p>");
        resp.getWriter()
                .print("<iframe src=\"/requestcapture/\" style=\"border:0px;width:100%;height:500px;\"></iframe>");
        resp.getWriter()
                .print("<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\"></script>");

        resp.getWriter().print("<script>");
        resp.getWriter()
                .print("var now = function(){ $.post('/now.json', {}, function(res){ $('#now').html(res.t); });   };");
        resp.getWriter().print("setTimeout(now, 1234);");
        resp.getWriter().print("</script>");
        resp.getWriter().print("</body></html>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json");
        resp.getWriter().print("{\"t\":\"" + DateTime.now(DateTimeZone.UTC).toString("yyyy-MM-dd HH:mm:ss") + "\"}");
    }

    // @Cacheable("time")
    public String query() {
        String appName = "devhelper-servlet-gae-sample";
        // Const.AppProperties.getProperty("app.name");
        log.debug("appName: {}, now: {}", appName, DateTime.now(DateTimeZone.UTC).toString("yyyy-MM-dd HH:mm:ss"));
        return appName;
    }

}

@@ -1323,22 +1323,162 @@
 pName = 
-query(
+%22devhelper-servlet-gae-sample%22;%0D%0A        log.debug(%22appName: %7B%7D, now: %7B%7D%22, appName, DateTime.now(DateTimeZone.UTC).toString(%22yyyy-MM-dd HH:mm:ss%22)
 );%0D%0A%0D%0A  
@@ -2351,32 +2351,39 @@
 resp.getWriter()
+.print(
 %0D%0A              
@@ -2384,23 +2384,16 @@
         
-.print(
 %22%3Ciframe
@@ -2415,16 +2415,31 @@
 capture/
+apirecords.html
 %5C%22 style
@@ -2701,32 +2701,39 @@
 resp.getWriter()
+.print(
 %0D%0A              
@@ -2734,23 +2734,16 @@
         
-.print(
 %22var now
@@ -2778,16 +2778,57 @@
 json', %7B
+ r: Math.floor(Math.random() * 10000000) 
 %7D, funct
@@ -3276,380 +3276,294 @@
     
-resp.getWriter().print(%22%7B%5C%22t%5C%22:%5C%22%22 + DateTime.now(DateTimeZone.UTC).toString(%22yyyy-MM-dd HH:mm:ss%22) + %22%5C%22%7D%22);%0D%0A    %7D%0D%0A%0D%0A    // @Cacheable(%22time%22)%0D%0A    public String query() %7B%0D%0A        String appName = %22devhelper-servlet-gae-sample%22;%0D%0A        // Const.AppProperties.getProperty(%22app.name%22);%0D%0A        log.debug(%22appName:
+DateTime now = DateTime.now(DateTimeZone.UTC);%0D%0A        if (now.getSecondOfMinute() %25 2 == 0) %7B%0D%0A            log.warn(%22random warn message, %7B%7D%22, now);%0D%0A        %7D else %7B%0D%0A            log.error(%22random error message,
  %7B%7D
+%22
 , now
-: %7B%7D%22, appName, DateTime.now(DateTimeZone.UTC)
+);%0D%0A        %7D%0D%0A        resp.getWriter().print(%22%7B%5C%22t%5C%22:%5C%22%22 + now
 .toS
@@ -3594,34 +3594,17 @@
 ss%22)
-);%0D%0A        return appName
+ + %22%5C%22%7D%22)
 ;%0D%0A 

update gae-sample