JolokiaHttpHandler implements HttpHandler { @SuppressWarnings({"PMD.AvoidCatchingThrowable", "PMD.AvoidInstanceofChecksInCatchClause"}) public void doHandle(HttpExchange pExchange) throws IOException { if (requestHandler == null) { throw new IllegalStateException("Handler not yet started"); } JSONAware json = null; URI uri = pExchange.getRequestURI(); ParsedUri parsedUri = new ParsedUri(uri, context); try { InetSocketAddress address = pExchange.getRemoteAddress(); requestHandler.checkAccess(getHostName(address), address.getAddress().getHostAddress(), extractOriginOrReferer(pExchange)); String method = pExchange.getRequestMethod(); validateCallbackIfGiven(parsedUri); if ("GET".equalsIgnoreCase(method)) { setHeaders(pExchange); json = executeGetRequest(parsedUri); } else if ("POST".equalsIgnoreCase(method)) { setHeaders(pExchange); json = executePostRequest(pExchange, parsedUri); } else if ("OPTIONS".equalsIgnoreCase(method)) { performCorsPreflightCheck(pExchange); } else { throw new IllegalArgumentException("HTTP Method " + method + " is not supported."); } } catch (Throwable exp) { json = requestHandler.handleThrowable( exp instanceof RuntimeMBeanException ? ((RuntimeMBeanException) exp).getTargetException() : exp); } finally { sendResponse(pExchange, parsedUri, json); } } JolokiaHttpHandler(Configuration pConfig);  JolokiaHttpHandler(Configuration pConfig, LogHandler pLogHandler); void start(boolean pLazy); void start(boolean pLazy, String pUrl, boolean pSecured); void stop(); void handle(final HttpExchange pHttpExchange); @SuppressWarnings({"PMD.AvoidCatchingThrowable", "PMD.AvoidInstanceofChecksInCatchClause"}) void doHandle(HttpExchange pExchange);  }
@Test public void testCallbackGet() throws IOException, URISyntaxException { HttpExchange exchange = prepareExchange("http: expect(exchange.getRequestMethod()).andReturn("GET"); Headers header = new Headers(); ByteArrayOutputStream out = prepareResponse(handler, exchange, header); handler.doHandle(exchange); assertEquals(header.getFirst("content-type"),"text/javascript; charset=utf-8"); String result = out.toString("utf-8"); assertTrue(result.endsWith("});")); assertTrue(result.startsWith("data({")); }
@Test public void testCallbackPost() throws URISyntaxException, IOException, java.text.ParseException { HttpExchange exchange = prepareExchange("http: "Content-Type","text/plain; charset=UTF-8", "Origin","" ); prepareMemoryPostReadRequest(exchange); Headers header = new Headers(); ByteArrayOutputStream out = prepareResponse(handler, exchange, header); handler.doHandle(exchange); assertEquals(header.getFirst("content-type"),"text/javascript; charset=utf-8"); String result = out.toString("utf-8"); assertTrue(result.endsWith("});")); assertTrue(result.startsWith("data({")); assertTrue(result.contains("\"used\"")); assertEquals(header.getFirst("Cache-Control"),"no-cache"); assertEquals(header.getFirst("Pragma"),"no-cache"); SimpleDateFormat rfc1123Format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT")); String expires = header.getFirst("Expires"); String date = header.getFirst("Date"); Date parsedExpires = rfc1123Format.parse(expires); Date parsedDate = rfc1123Format.parse(date); assertTrue(parsedExpires.before(parsedDate) || parsedExpires.equals(parsedDate)); Date now = new Date(); assertTrue(parsedExpires.before(now) || parsedExpires.equals(now)); }
@Test public void invalidMethod() throws URISyntaxException, IOException, ParseException { HttpExchange exchange = prepareExchange("http: expect(exchange.getRequestMethod()).andReturn("PUT"); Headers header = new Headers(); ByteArrayOutputStream out = prepareResponse(handler, exchange, header); handler.doHandle(exchange); JSONObject resp = (JSONObject) new JSONParser().parse(out.toString()); assertTrue(resp.containsKey("error")); assertEquals(resp.get("error_type"), IllegalArgumentException.class.getName()); assertTrue(((String) resp.get("error")).contains("PUT")); }
@Test(expectedExceptions = IllegalStateException.class,expectedExceptionsMessageRegExp = ".*not.*started.*") public void handlerNotStarted() throws URISyntaxException, IOException { JolokiaHttpHandler newHandler = new JolokiaHttpHandler(getConfig()); newHandler.doHandle(prepareExchange("http: }
@Test public void simlePostRequestWithCors() throws URISyntaxException, IOException { HttpExchange exchange = prepareExchange("http: "Content-Type","text/plain; charset=UTF-8", "Origin","http: ); prepareMemoryPostReadRequest(exchange); Headers header = new Headers(); prepareResponse(handler, exchange, header); handler.doHandle(exchange); assertEquals(header.getFirst("content-type"), "text/plain; charset=utf-8"); assertEquals(header.getFirst("Access-Control-Allow-Origin"),"http: }
@Test public void preflightCheck() throws URISyntaxException, IOException { HttpExchange exchange = prepareExchange("http: "Origin","http: "Access-Control-Request-Headers","X-Bla, X-Blub"); expect(exchange.getRequestMethod()).andReturn("OPTIONS"); Headers header = new Headers(); ByteArrayOutputStream out = prepareResponse(handler, exchange, header); handler.doHandle(exchange); assertEquals(header.getFirst("Access-Control-Allow-Origin"),"http: assertEquals(header.getFirst("Access-Control-Allow-Headers"),"X-Bla, X-Blub"); assertNotNull(header.getFirst("Access-Control-Max-Age")); }