Browse Source

General enhancement

- Will now able to handle request with api key correctly
- Will now ignore empty api key
master
LO Kam Tao Leo 4 years ago
parent
commit
05917529ac
  1. 24
      src/org/leolo/web/dm/servlet/BaseServlet.java

24
src/org/leolo/web/dm/servlet/BaseServlet.java

@ -9,6 +9,7 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;
import org.leolo.web.dm.Constant; import org.leolo.web.dm.Constant;
import org.leolo.web.dm.dao.APIKeyDao; import org.leolo.web.dm.dao.APIKeyDao;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -22,7 +23,8 @@ public class BaseServlet extends HttpServlet {
private static Logger log = LoggerFactory.getLogger(BaseServlet.class); private static Logger log = LoggerFactory.getLogger(BaseServlet.class);
protected String userName = null; protected String userName = null;
protected int userId = 0;//Special UID for anonymous user protected int userId = Constant.COM_DEFAULT_USER_ID;
protected boolean fatalError = false;
/** /**
* @see HttpServlet#HttpServlet() * @see HttpServlet#HttpServlet()
@ -37,19 +39,33 @@ public class BaseServlet extends HttpServlet {
*/ */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
log.info("Request - [GET] {}", request.getRequestURI()); log.info("Request - [GET] {}", request.getRequestURI());
//Reset parameters
userName = null;
userId = Constant.COM_DEFAULT_USER_ID;
fatalError = false;
if(request.getAttribute(Constant.SESSION_USER_NAME)!=null) { if(request.getAttribute(Constant.SESSION_USER_NAME)!=null) {
userName = request.getSession().getAttribute(Constant.SESSION_USER_NAME).toString(); userName = request.getSession().getAttribute(Constant.SESSION_USER_NAME).toString();
userId = (Integer) request.getSession().getAttribute(Constant.SESSION_USER_ID); userId = (Integer) request.getSession().getAttribute(Constant.SESSION_USER_ID);
} }
if(request.getParameter("key") != null) { if(request.getParameter("key") != null && !"".equals(request.getParameter("key").trim())) {
APIKeyDao apiDao = new APIKeyDao(); APIKeyDao apiDao = new APIKeyDao();
String key = request.getParameter("key").toString(); String key = request.getParameter("key");
log.info("Using API key {}", key); log.info("Using API key {}", key);
Map<String, Object> buim = apiDao.getBasicUserInfomationBuApiKey(key); Map<String, Object> buim = apiDao.getBasicUserInfomationBuApiKey(key);
if(buim!=null) { if(buim!=null) {
userName = buim.get(Constant.BUI_KEY_USER_NAME).toString(); userName = buim.get(Constant.BUI_KEY_USER_NAME).toString();
userId = (Integer) buim.get(Constant.BUI_KEY_USER_ID); userId = (Integer) buim.get(Constant.BUI_KEY_USER_ID);
apiDao.markKeyUsed(key); apiDao.markKeyUsed(key);
}else {
response.setContentType("application/json");
JSONObject obj = new JSONObject();
response.setStatus(403);
obj.put("status", "error");
obj.put("message", "Invalid API key");
obj.write(response.getWriter());
fatalError = true;
return;
} }
} }
} }
@ -58,7 +74,7 @@ public class BaseServlet extends HttpServlet {
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/ */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} }
} }

Loading…
Cancel
Save