@Override protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { final long startNanoTime = System.nanoTime(); final String operation = checkProtVerAndGetOperation(PROTOCOL_VERSION_1, request, response); if (operation == null) return; final String apiKey = request.getParameter(PARAM_API_KEY); if (apiKey == null || apiKey.isEmpty()) { LOGGER.warning("Missing API key!"); response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing API key!"); return; } long opsCharged = 0; ApiAccount apiAccount = null; PersistenceManager pm = null; ResponseWrapper responseWrapper = null; boolean denied = false; try { pm = PMF.get().getPersistenceManager(); // Check API key final List<ApiAccount> apiAccountList = new JQBuilder<>(pm, ApiAccount.class).filter("apiKey==p1", "String p1").get(apiKey); if (apiAccountList.isEmpty()) { LOGGER.warning("Unauthorized access, invalid API Key: " + apiKey); response.sendError( HttpServletResponse.SC_FORBIDDEN, "Unauthorized access, invalid API Key!"); return; } apiAccount = apiAccountList.get(0); responseWrapper = new ResponseWrapper(response); // Check Ops quota final List<ApiCallStat> totalApiCallStatList = new JQBuilder<>(pm, ApiCallStat.class) .filter("ownerKey==p1 && day==p2", "KEY p1, String p2") .get(apiAccount.getKey(), ApiCallStat.DAY_TOTAL); final long totalUsedOps = totalApiCallStatList.isEmpty() ? 0 : totalApiCallStatList.get(0).getUsedOps(); if (!OPERATION_INFO.equals(operation) && totalUsedOps >= apiAccount.getPaidOps()) { denied = true; LOGGER.warning( "Ops quota have been exceeded, serving denied! (API account: " + apiAccount.getUser().getEmail() + ")"); responseWrapper.sendError( HttpServletResponse.SC_PAYMENT_REQUIRED, "Ops quota have been exceeded, serving denied!"); return; } switch (operation) { case OPERATION_INFO: opsCharged = infoOp(request, responseWrapper, pm, apiAccount); break; case OPERATION_MAP_INFO: opsCharged = mapInfoOp(request, responseWrapper, pm, apiAccount); break; case OPERATION_PARSE_REPLAY: opsCharged = parseRepOp(request, responseWrapper, pm, apiAccount); break; case OPERATION_PROFILE_INFO: opsCharged = profInfoOp(request, responseWrapper, pm, apiAccount); break; default: LOGGER.warning( "Invalid Operation! (API account: " + apiAccount.getUser().getEmail() + ")"); responseWrapper.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid Operation!"); return; } // Notification available Ops will be checked in the task servlet, update API call stat task } finally { if (apiAccount != null) TaskServlet.register_updateApiCallStat( apiAccount.getKey(), apiAccount.getPaidOps(), apiAccount.getNotificationAvailOps(), opsCharged, (System.nanoTime() - startNanoTime) / 1000000l, denied, responseWrapper == null ? true : responseWrapper.isError(), operation); if (pm != null) pm.close(); } }