private void handleArrayOfJsonRequests( PeekableInputStream inputStream, HttpServletResponse response) throws IOException { try { JsonRpcRequestModel[] jsonRequests; String streamString = IOUtils.toString(inputStream, "UTF-8"); ServletRequestInfo info = getServletRequestInfo(); /*If first character is { then it is a single request. We add it to the array jsonRequests and continue*/ if (streamString.charAt(0) == '{') { // TODO:CHeck parse error for this JsonRpcRequestModel req = (new Gson()).fromJson(streamString, JsonRpcRequestModel.class); jsonRequests = new JsonRpcRequestModel[] {req}; info.isBatchRequest = false; } else { jsonRequests = (new Gson()).fromJson(streamString, JsonRpcRequestModel[].class); info.isBatchRequest = true; } /* we loop through each request, get results or check error and add repsonses to an array*/ for (int i = 0; i < jsonRequests.length; i++) { info.currentJsonRequest = jsonRequests[i]; /* Check to see if JSON-RPC protocol is 2.0*/ if (info.currentJsonRequest.jsonrpc == null || !info.currentJsonRequest.jsonrpc.equals(JSONRPC_VERSION)) { sendJsonError(JSON_RPC_PROTOCOL_ERROR_MESSAGE, info.currentJsonRequest.jsonrpc); continue; } /*Check if ID is a number and if so it has not fractional numbers*/ else if (info.currentJsonRequest.id instanceof Number) { Number number = (Number) info.currentJsonRequest.id; if (number.intValue() != number.doubleValue()) { sendJsonError(JSON_RPC_ID_ERROR_MESSAGE, info.currentJsonRequest.id); continue; } info.currentJsonRequest.id = number.intValue(); } /*Check if Method exists*/ if (!methodMap.containsKey(info.currentJsonRequest.method)) { sendJsonError(JSON_RPC_METHOD_ERROR_MESSAGE, info.currentJsonRequest.method); continue; } invokeMethod(info.currentJsonRequest.method, info.currentJsonRequest.params); } } catch (JsonParseException e) { sendError(e, JSON_RPC_PARSE_ERROR_MESSAGE); } }
@SuppressWarnings("unchecked") private void handleServletRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { try { ServletRequestInfo info = setServletRequestInfo(request, response); if (request.getMethod().equals("GET")) { List<String> urlParamNames = Collections.list(request.getParameterNames()); HashMap<String, String> params = new HashMap<String, String>(); for (String paramName : urlParamNames) params.put(paramName, request.getParameter(paramName)); JsonRpcRequestModel json = new JsonRpcRequestModel(); json.jsonrpc = JSONRPC_VERSION; json.id = ""; json.method = params.remove(METHOD); json.params = params; info.currentJsonRequest = json; invokeMethod(json.method, params); } else // post { try { String methodName; Object methodParams; if (info.inputStream.peek() == '[' || info.inputStream.peek() == '{') // json { handleArrayOfJsonRequests(info.inputStream, response); } else // AMF3 { ASObject obj = (ASObject) deserializeAmf3(info.inputStream); methodName = (String) obj.get(METHOD); methodParams = obj.get(PARAMS); info.streamParameterIndex = (Number) obj.get(STREAM_PARAMETER_INDEX); invokeMethod(methodName, methodParams); } } catch (IOException e) { sendError(e, null); } catch (Exception e) { sendError(e, null); } } handleJsonResponses(); } finally { removeServletRequestInfo(); } }