HttpRequestHandler { public JSONAware handlePostRequest(String pUri, InputStream pInputStream, String pEncoding, Map<String, String[]> pParameterMap) throws IOException { if (backendManager.isDebug()) { logHandler.debug("URI: " + pUri); } Object jsonRequest = extractJsonRequest(pInputStream,pEncoding); if (jsonRequest instanceof JSONArray) { List<JmxRequest> jmxRequests = JmxRequestFactory.createPostRequests((List) jsonRequest,getProcessingParameter(pParameterMap)); JSONArray responseList = new JSONArray(); for (JmxRequest jmxReq : jmxRequests) { if (backendManager.isDebug()) { logHandler.debug("Request: " + jmxReq.toString()); } JSONObject resp = executeRequest(jmxReq); responseList.add(resp); } return responseList; } else if (jsonRequest instanceof JSONObject) { JmxRequest jmxReq = JmxRequestFactory.createPostRequest((Map<String, ?>) jsonRequest,getProcessingParameter(pParameterMap)); return executeRequest(jmxReq); } else { throw new IllegalArgumentException("Invalid JSON Request " + jsonRequest); } } HttpRequestHandler(Configuration pConfig, BackendManager pBackendManager, LogHandler pLogHandler); JSONAware handleGetRequest(String pUri, String pPathInfo, Map<String, String[]> pParameterMap); JSONAware handlePostRequest(String pUri, InputStream pInputStream, String pEncoding, Map<String, String[]>  pParameterMap); Map<String, String> handleCorsPreflightRequest(String pOrigin, String pRequestHeaders); JSONObject handleThrowable(Throwable pThrowable); JSONObject getErrorJSON(int pErrorCode, Throwable pExp, JmxRequest pJmxReq); void checkAccess(String pHost, String pAddress, String pOrigin); String extractCorsOrigin(String pOrigin);  }
@Test public void singlePost() throws IOException, InstanceNotFoundException, ReflectionException, AttributeNotFoundException, MBeanException { JSONObject resp = new JSONObject(); expect(backend.handleRequest(isA(JmxReadRequest.class))).andReturn(resp); replay(backend); InputStream is = HttpTestUtil.createServletInputStream(HttpTestUtil.HEAP_MEMORY_POST_REQUEST); JSONObject response = (JSONObject) handler.handlePostRequest("/jolokia",is,"utf-8",null); assertTrue(response == resp); }
@Test public void doublePost() throws IOException, InstanceNotFoundException, ReflectionException, AttributeNotFoundException, MBeanException { JSONObject resp = new JSONObject(); expect(backend.handleRequest(isA(JmxReadRequest.class))).andReturn(resp).times(2); replay(backend); InputStream is = HttpTestUtil.createServletInputStream("[" + HttpTestUtil.HEAP_MEMORY_POST_REQUEST + "," + HttpTestUtil.HEAP_MEMORY_POST_REQUEST + "]"); JSONArray response = (JSONArray) handler.handlePostRequest("/jolokia", is, "utf-8", null); assertEquals(response.size(),2); assertTrue(response.get(0) == resp); assertTrue(response.get(1) == resp); }
@Test(expectedExceptions = IllegalArgumentException.class) public void invalidJson() throws IOException { replay(backend); InputStream is = HttpTestUtil.createServletInputStream("{ bla;"); handler.handlePostRequest("/jolokia",is,"utf-8",null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void invalidJson2() throws IOException { replay(backend); InputStream is = HttpTestUtil.createServletInputStream("12"); handler.handlePostRequest("/jolokia",is,"utf-8",null); }