@Test public void testDoPost() throws Exception { logger.info("doPost"); Server server = Help.getServer(EventsServlet.class); server.start(); try { String url = String.format("http: Help.Res r = Help.sendPost(url, Help.ExampleNotices()[0]); assertEquals(200, r.getCode()); r = Help.sendPost(url, "<<this is invalid JSON>>"); assertEquals(400, r.getCode()); } finally { server.stop(); } }
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Utils.putCorrelatorAndTrans(request); logger.debug("events doPost"); response.setCharacterEncoding("UTF-8"); response.setContentType("application/json;charset=UTF-8"); try { String eventText = Utils.getBodyAsString(request); logger.info(String.format("incoming event: %s", eventText)); org.json.JSONObject jo = new JSONObject(eventText); logger.debug(String.format("event as JSONObject: %s", jo)); Map<String, Object> eventMap = Utils.JSONObject2Map(jo); logger.debug(String.format("event as map: %s" , eventMap)); epService.getEPRuntime().sendEvent(eventMap, Constants.IOT_EVENT); logger.debug(String.format("event was sent: %s", eventMap)); } catch (Exception je) { try { logger.error(String.format("error: %s" ,je)); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.getOutputStream().print(String.format("{\"error\":\"%s\"}%n", Encode.forHtmlContent(je.getMessage()))); } catch (IOException exception) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); } } }
EventsServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Utils.putCorrelatorAndTrans(request); logger.debug("events doPost"); response.setCharacterEncoding("UTF-8"); response.setContentType("application/json;charset=UTF-8"); try { String eventText = Utils.getBodyAsString(request); logger.info(String.format("incoming event: %s", eventText)); org.json.JSONObject jo = new JSONObject(eventText); logger.debug(String.format("event as JSONObject: %s", jo)); Map<String, Object> eventMap = Utils.JSONObject2Map(jo); logger.debug(String.format("event as map: %s" , eventMap)); epService.getEPRuntime().sendEvent(eventMap, Constants.IOT_EVENT); logger.debug(String.format("event was sent: %s", eventMap)); } catch (Exception je) { try { logger.error(String.format("error: %s" ,je)); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.getOutputStream().print(String.format("{\"error\":\"%s\"}%n", Encode.forHtmlContent(je.getMessage()))); } catch (IOException exception) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); } } } }
EventsServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Utils.putCorrelatorAndTrans(request); logger.debug("events doPost"); response.setCharacterEncoding("UTF-8"); response.setContentType("application/json;charset=UTF-8"); try { String eventText = Utils.getBodyAsString(request); logger.info(String.format("incoming event: %s", eventText)); org.json.JSONObject jo = new JSONObject(eventText); logger.debug(String.format("event as JSONObject: %s", jo)); Map<String, Object> eventMap = Utils.JSONObject2Map(jo); logger.debug(String.format("event as map: %s" , eventMap)); epService.getEPRuntime().sendEvent(eventMap, Constants.IOT_EVENT); logger.debug(String.format("event was sent: %s", eventMap)); } catch (Exception je) { try { logger.error(String.format("error: %s" ,je)); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.getOutputStream().print(String.format("{\"error\":\"%s\"}%n", Encode.forHtmlContent(je.getMessage()))); } catch (IOException exception) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); } } }  }
EventsServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Utils.putCorrelatorAndTrans(request); logger.debug("events doPost"); response.setCharacterEncoding("UTF-8"); response.setContentType("application/json;charset=UTF-8"); try { String eventText = Utils.getBodyAsString(request); logger.info(String.format("incoming event: %s", eventText)); org.json.JSONObject jo = new JSONObject(eventText); logger.debug(String.format("event as JSONObject: %s", jo)); Map<String, Object> eventMap = Utils.JSONObject2Map(jo); logger.debug(String.format("event as map: %s" , eventMap)); epService.getEPRuntime().sendEvent(eventMap, Constants.IOT_EVENT); logger.debug(String.format("event was sent: %s", eventMap)); } catch (Exception je) { try { logger.error(String.format("error: %s" ,je)); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.getOutputStream().print(String.format("{\"error\":\"%s\"}%n", Encode.forHtmlContent(je.getMessage()))); } catch (IOException exception) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); } } }  @Override void init(); @Override void destroy(); @Override String getServletInfo(); }
EventsServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Utils.putCorrelatorAndTrans(request); logger.debug("events doPost"); response.setCharacterEncoding("UTF-8"); response.setContentType("application/json;charset=UTF-8"); try { String eventText = Utils.getBodyAsString(request); logger.info(String.format("incoming event: %s", eventText)); org.json.JSONObject jo = new JSONObject(eventText); logger.debug(String.format("event as JSONObject: %s", jo)); Map<String, Object> eventMap = Utils.JSONObject2Map(jo); logger.debug(String.format("event as map: %s" , eventMap)); epService.getEPRuntime().sendEvent(eventMap, Constants.IOT_EVENT); logger.debug(String.format("event was sent: %s", eventMap)); } catch (Exception je) { try { logger.error(String.format("error: %s" ,je)); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.getOutputStream().print(String.format("{\"error\":\"%s\"}%n", Encode.forHtmlContent(je.getMessage()))); } catch (IOException exception) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); } } }  @Override void init(); @Override void destroy(); @Override String getServletInfo();  }