package com.game.cloudymon;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;
import javax.servlet.http.*;
import com.game.cloudymon.bean.PlayBean;
import com.game.cloudymon.entity.RouteMap;
import com.game.util.Encryption;
import com.game.util.ResourceReader;
import com.google.appengine.repackaged.org.json.JSONObject;
@SuppressWarnings("serial")
public class PlayServlet extends HttpServlet
{
public static Logger logger = Logger.getLogger("PlayServlet");
public void doGet(HttpServletRequest req, HttpServletResponse resp)
{
try
{
doProcess(req, resp);
} catch (Exception e)
{
logger.severe(e.getMessage());
}
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
{
try
{
doProcess(req, resp);
} catch (Exception e)
{
logger.severe(e.getMessage());
}
}
public void doProcess(HttpServletRequest req, HttpServletResponse resp)
{
try
{
String param = req.getParameter("param").toLowerCase();
if (param == null || param.isEmpty())
{
logger.severe("Invalid params.");
return;
}
param = param.replace(' ', '+');
String jsonParam = Encryption.decrypt(param);
JSONObject jsonObj = new JSONObject(jsonParam);
String action = jsonObj.getString("action");
switch (action)
{
case "getroutemap":
// http://localhost:8888/play?param={"action":"getroutemap","lat":"10.75918","lng":"106.662498"}
doGetRouteMap(req, resp, jsonObj);
break;
default:
break;
}
} catch (Exception e)
{
logger.severe(e.getMessage());
}
}
private void returnVal(HttpServletResponse resp, String jsonStr) throws Exception
{
resp.setContentType("text/plain");
resp.getWriter().write(Encryption.encrypt(jsonStr));
}
private void doGetRouteMap(HttpServletRequest req, HttpServletResponse resp, JSONObject jsonObj)
{
try
{
Double lat = jsonObj.getDouble("lat");
Double lng = jsonObj.getDouble("lng");
RouteMap routeMap = PlayBean.getRouteMapByLatLng(lat, lng);
returnVal(resp, routeMap.toJSON());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}