@Test public void testDoHTTPPost() { logger.info("DoHTTPPost"); InetSocketAddress address = new InetSocketAddress(Help.PORT); HttpServer httpServer = null; try { httpServer = HttpServer.create(address, 0); HttpHandler handler = new HttpHandler() { @Override public void handle(HttpExchange exchange) throws IOException { byte[] response = "OK\n".getBytes(); exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length); exchange.getResponseBody().write(response); exchange.close(); } }; httpServer.createContext("/path", handler); httpServer.start(); boolean result = Utils.DoHTTPPost( String.format("http: assertEquals(true, result); result = Utils.DoHTTPPost( String.format("http: assertEquals(false, result); result = Utils.DoHTTPPost("<<this is an invalid URL>>", "xxxxx"); assertEquals(false, result); } catch (IOException ex) { logger.error(ex.toString()); fail(ex.toString()); } finally { if (httpServer != null) { httpServer.stop(0); } } }
public static boolean DoHTTPPost(String urlStr, String content) { try { URL url = new URL(urlStr); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); urlConn.setDoOutput(true); urlConn.setRequestProperty("Content-Type", "application/json; charset=utf-8"); urlConn.setRequestProperty(Constants.CORRELATOR_HEADER, MDC.get(Constants.CORRELATOR_ID)); urlConn.setRequestProperty(Constants.SERVICE_HEADER, MDC.get(Constants.SERVICE_FIELD)); urlConn.setRequestProperty(Constants.SUBSERVICE_HEADER, MDC.get(Constants.SUBSERVICE_FIELD)); urlConn.setRequestProperty(Constants.REALIP_HEADER, MDC.get(Constants.REALIP_FIELD)); OutputStreamWriter printout = new OutputStreamWriter(urlConn.getOutputStream(), Charset.forName("UTF-8")); printout.write(content); printout.flush(); printout.close(); int code = urlConn.getResponseCode(); String message = urlConn.getResponseMessage(); logger.debug(String.format("action http response %s %s",code,message)); if (code / 100 == 2) { InputStream input = urlConn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); for (String line; (line = reader.readLine()) != null;) { logger.info(String.format("action response body: %s", line)); } input.close(); return true; } else { logger.error(String.format("action response is not OK: %s %s",code,message)); InputStream error = urlConn.getErrorStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(error)); for (String line; (line = reader.readLine()) != null;) { logger.error(String.format("action error response body: %s", line)); } error.close(); return false; } } catch (MalformedURLException me) { logger.error(String.format("exception MalformedURLException: %s",me)); return false; } catch (IOException ioe) { logger.error(String.format("exception IOException: %s",ioe)); return false; } }
Utils { public static boolean DoHTTPPost(String urlStr, String content) { try { URL url = new URL(urlStr); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); urlConn.setDoOutput(true); urlConn.setRequestProperty("Content-Type", "application/json; charset=utf-8"); urlConn.setRequestProperty(Constants.CORRELATOR_HEADER, MDC.get(Constants.CORRELATOR_ID)); urlConn.setRequestProperty(Constants.SERVICE_HEADER, MDC.get(Constants.SERVICE_FIELD)); urlConn.setRequestProperty(Constants.SUBSERVICE_HEADER, MDC.get(Constants.SUBSERVICE_FIELD)); urlConn.setRequestProperty(Constants.REALIP_HEADER, MDC.get(Constants.REALIP_FIELD)); OutputStreamWriter printout = new OutputStreamWriter(urlConn.getOutputStream(), Charset.forName("UTF-8")); printout.write(content); printout.flush(); printout.close(); int code = urlConn.getResponseCode(); String message = urlConn.getResponseMessage(); logger.debug(String.format("action http response %s %s",code,message)); if (code / 100 == 2) { InputStream input = urlConn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); for (String line; (line = reader.readLine()) != null;) { logger.info(String.format("action response body: %s", line)); } input.close(); return true; } else { logger.error(String.format("action response is not OK: %s %s",code,message)); InputStream error = urlConn.getErrorStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(error)); for (String line; (line = reader.readLine()) != null;) { logger.error(String.format("action error response body: %s", line)); } error.close(); return false; } } catch (MalformedURLException me) { logger.error(String.format("exception MalformedURLException: %s",me)); return false; } catch (IOException ioe) { logger.error(String.format("exception IOException: %s",ioe)); return false; } } }
Utils { public static boolean DoHTTPPost(String urlStr, String content) { try { URL url = new URL(urlStr); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); urlConn.setDoOutput(true); urlConn.setRequestProperty("Content-Type", "application/json; charset=utf-8"); urlConn.setRequestProperty(Constants.CORRELATOR_HEADER, MDC.get(Constants.CORRELATOR_ID)); urlConn.setRequestProperty(Constants.SERVICE_HEADER, MDC.get(Constants.SERVICE_FIELD)); urlConn.setRequestProperty(Constants.SUBSERVICE_HEADER, MDC.get(Constants.SUBSERVICE_FIELD)); urlConn.setRequestProperty(Constants.REALIP_HEADER, MDC.get(Constants.REALIP_FIELD)); OutputStreamWriter printout = new OutputStreamWriter(urlConn.getOutputStream(), Charset.forName("UTF-8")); printout.write(content); printout.flush(); printout.close(); int code = urlConn.getResponseCode(); String message = urlConn.getResponseMessage(); logger.debug(String.format("action http response %s %s",code,message)); if (code / 100 == 2) { InputStream input = urlConn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); for (String line; (line = reader.readLine()) != null;) { logger.info(String.format("action response body: %s", line)); } input.close(); return true; } else { logger.error(String.format("action response is not OK: %s %s",code,message)); InputStream error = urlConn.getErrorStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(error)); for (String line; (line = reader.readLine()) != null;) { logger.error(String.format("action error response body: %s", line)); } error.close(); return false; } } catch (MalformedURLException me) { logger.error(String.format("exception MalformedURLException: %s",me)); return false; } catch (IOException ioe) { logger.error(String.format("exception IOException: %s",ioe)); return false; } }  }
Utils { public static boolean DoHTTPPost(String urlStr, String content) { try { URL url = new URL(urlStr); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); urlConn.setDoOutput(true); urlConn.setRequestProperty("Content-Type", "application/json; charset=utf-8"); urlConn.setRequestProperty(Constants.CORRELATOR_HEADER, MDC.get(Constants.CORRELATOR_ID)); urlConn.setRequestProperty(Constants.SERVICE_HEADER, MDC.get(Constants.SERVICE_FIELD)); urlConn.setRequestProperty(Constants.SUBSERVICE_HEADER, MDC.get(Constants.SUBSERVICE_FIELD)); urlConn.setRequestProperty(Constants.REALIP_HEADER, MDC.get(Constants.REALIP_FIELD)); OutputStreamWriter printout = new OutputStreamWriter(urlConn.getOutputStream(), Charset.forName("UTF-8")); printout.write(content); printout.flush(); printout.close(); int code = urlConn.getResponseCode(); String message = urlConn.getResponseMessage(); logger.debug(String.format("action http response %s %s",code,message)); if (code / 100 == 2) { InputStream input = urlConn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); for (String line; (line = reader.readLine()) != null;) { logger.info(String.format("action response body: %s", line)); } input.close(); return true; } else { logger.error(String.format("action response is not OK: %s %s",code,message)); InputStream error = urlConn.getErrorStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(error)); for (String line; (line = reader.readLine()) != null;) { logger.error(String.format("action error response body: %s", line)); } error.close(); return false; } } catch (MalformedURLException me) { logger.error(String.format("exception MalformedURLException: %s",me)); return false; } catch (IOException ioe) { logger.error(String.format("exception IOException: %s",ioe)); return false; } }  static synchronized EPServiceProvider initEPService(ServletContext sc); static synchronized void destroyEPService(ServletContext sc); static Map<String, Object> JSONObject2Map(JSONObject jo); static JSONObject Event2JSONObject(EventBean event); static JSONObject Statement2JSONObject(EPStatement st); static boolean DoHTTPPost(String urlStr, String content); static void putCorrelatorAndTrans(HttpServletRequest req); static String getBodyAsString(HttpServletRequest request); static void setTimerRuleHeaders(JSONObject rule); static Boolean isValidURL(String url); }
Utils { public static boolean DoHTTPPost(String urlStr, String content) { try { URL url = new URL(urlStr); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); urlConn.setDoOutput(true); urlConn.setRequestProperty("Content-Type", "application/json; charset=utf-8"); urlConn.setRequestProperty(Constants.CORRELATOR_HEADER, MDC.get(Constants.CORRELATOR_ID)); urlConn.setRequestProperty(Constants.SERVICE_HEADER, MDC.get(Constants.SERVICE_FIELD)); urlConn.setRequestProperty(Constants.SUBSERVICE_HEADER, MDC.get(Constants.SUBSERVICE_FIELD)); urlConn.setRequestProperty(Constants.REALIP_HEADER, MDC.get(Constants.REALIP_FIELD)); OutputStreamWriter printout = new OutputStreamWriter(urlConn.getOutputStream(), Charset.forName("UTF-8")); printout.write(content); printout.flush(); printout.close(); int code = urlConn.getResponseCode(); String message = urlConn.getResponseMessage(); logger.debug(String.format("action http response %s %s",code,message)); if (code / 100 == 2) { InputStream input = urlConn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); for (String line; (line = reader.readLine()) != null;) { logger.info(String.format("action response body: %s", line)); } input.close(); return true; } else { logger.error(String.format("action response is not OK: %s %s",code,message)); InputStream error = urlConn.getErrorStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(error)); for (String line; (line = reader.readLine()) != null;) { logger.error(String.format("action error response body: %s", line)); } error.close(); return false; } } catch (MalformedURLException me) { logger.error(String.format("exception MalformedURLException: %s",me)); return false; } catch (IOException ioe) { logger.error(String.format("exception IOException: %s",ioe)); return false; } }  static synchronized EPServiceProvider initEPService(ServletContext sc); static synchronized void destroyEPService(ServletContext sc); static Map<String, Object> JSONObject2Map(JSONObject jo); static JSONObject Event2JSONObject(EventBean event); static JSONObject Statement2JSONObject(EPStatement st); static boolean DoHTTPPost(String urlStr, String content); static void putCorrelatorAndTrans(HttpServletRequest req); static String getBodyAsString(HttpServletRequest request); static void setTimerRuleHeaders(JSONObject rule); static Boolean isValidURL(String url);  }