@Test public void testInvalidRequestBody() throws Exception { ArgumentCaptor<FullHttpResponse> argument = ArgumentCaptor.forClass(FullHttpResponse.class); handler.handle(context, createRequest(HttpMethod.POST, "", "{\"xxx\": \"yyy\"}")); verify(searchIO, never()).insert(anyString(), anyMap()); verify(channel).write(argument.capture()); String errorResponseBody = argument.getValue().content().toString(Charset.defaultCharset()); ErrorResponse errorResponse = getErrorResponse(errorResponseBody); assertEquals("Number of errors invalid", 1, errorResponse.getErrors().size()); assertEquals("Invalid tenant", TENANT, errorResponse.getErrors().get(0).getTenantId()); assertEquals("Invalid status", HttpResponseStatus.BAD_REQUEST, argument.getValue().getStatus()); }
@Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { final String tenantId = request.headers().get(Event.FieldLabels.tenantId.name()); String response = ""; ObjectMapper objectMapper = new ObjectMapper(); final Timer.Context httpEventsIngestTimerContext = httpEventsIngestTimer.time(); try { String body = request.content().toString(0, request.content().writerIndex(), CharsetUtil.UTF_8); Event event = objectMapper.readValue(body, Event.class); Iterator<Event> iterator = objectMapper.reader(Event.class).readValues(body); if (iterator.hasNext()) { iterator.next(); if (iterator.hasNext()) { throw new InvalidDataException("Only one event is allowed per request"); } } Set<ConstraintViolation<Event>> constraintViolations = validator.validate(event); List<ErrorResponse.ErrorData> validationErrors = new ArrayList<ErrorResponse.ErrorData>(); for (ConstraintViolation<Event> constraintViolation : constraintViolations) { validationErrors.add( new ErrorResponse.ErrorData(tenantId, "", constraintViolation.getPropertyPath().toString(), constraintViolation.getMessage(), event.getWhen())); } if (!validationErrors.isEmpty()) { DefaultHandler.sendErrorResponse(ctx, request, validationErrors, HttpResponseStatus.BAD_REQUEST); return; } searchIO.insert(tenantId, event.toMap()); DefaultHandler.sendResponse(ctx, request, response, HttpResponseStatus.OK); } catch (JsonMappingException e) { log.debug(String.format("Exception %s", e.toString())); response = String.format("Invalid Data: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.BAD_REQUEST); } catch (JsonParseException e){ log.debug(String.format("Exception %s", e.toString())); response = String.format("Invalid Data: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.BAD_REQUEST); } catch (InvalidDataException e) { log.debug(String.format("Exception %s", e.toString())); response = String.format("Invalid Data: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.BAD_REQUEST); } catch (Exception e) { log.error(String.format("Exception %s", e.toString())); response = String.format("Error: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpEventsIngestTimerContext.stop(); } }
HttpEventsIngestionHandler implements HttpRequestHandler { @Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { final String tenantId = request.headers().get(Event.FieldLabels.tenantId.name()); String response = ""; ObjectMapper objectMapper = new ObjectMapper(); final Timer.Context httpEventsIngestTimerContext = httpEventsIngestTimer.time(); try { String body = request.content().toString(0, request.content().writerIndex(), CharsetUtil.UTF_8); Event event = objectMapper.readValue(body, Event.class); Iterator<Event> iterator = objectMapper.reader(Event.class).readValues(body); if (iterator.hasNext()) { iterator.next(); if (iterator.hasNext()) { throw new InvalidDataException("Only one event is allowed per request"); } } Set<ConstraintViolation<Event>> constraintViolations = validator.validate(event); List<ErrorResponse.ErrorData> validationErrors = new ArrayList<ErrorResponse.ErrorData>(); for (ConstraintViolation<Event> constraintViolation : constraintViolations) { validationErrors.add( new ErrorResponse.ErrorData(tenantId, "", constraintViolation.getPropertyPath().toString(), constraintViolation.getMessage(), event.getWhen())); } if (!validationErrors.isEmpty()) { DefaultHandler.sendErrorResponse(ctx, request, validationErrors, HttpResponseStatus.BAD_REQUEST); return; } searchIO.insert(tenantId, event.toMap()); DefaultHandler.sendResponse(ctx, request, response, HttpResponseStatus.OK); } catch (JsonMappingException e) { log.debug(String.format("Exception %s", e.toString())); response = String.format("Invalid Data: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.BAD_REQUEST); } catch (JsonParseException e){ log.debug(String.format("Exception %s", e.toString())); response = String.format("Invalid Data: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.BAD_REQUEST); } catch (InvalidDataException e) { log.debug(String.format("Exception %s", e.toString())); response = String.format("Invalid Data: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.BAD_REQUEST); } catch (Exception e) { log.error(String.format("Exception %s", e.toString())); response = String.format("Error: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpEventsIngestTimerContext.stop(); } } }
HttpEventsIngestionHandler implements HttpRequestHandler { @Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { final String tenantId = request.headers().get(Event.FieldLabels.tenantId.name()); String response = ""; ObjectMapper objectMapper = new ObjectMapper(); final Timer.Context httpEventsIngestTimerContext = httpEventsIngestTimer.time(); try { String body = request.content().toString(0, request.content().writerIndex(), CharsetUtil.UTF_8); Event event = objectMapper.readValue(body, Event.class); Iterator<Event> iterator = objectMapper.reader(Event.class).readValues(body); if (iterator.hasNext()) { iterator.next(); if (iterator.hasNext()) { throw new InvalidDataException("Only one event is allowed per request"); } } Set<ConstraintViolation<Event>> constraintViolations = validator.validate(event); List<ErrorResponse.ErrorData> validationErrors = new ArrayList<ErrorResponse.ErrorData>(); for (ConstraintViolation<Event> constraintViolation : constraintViolations) { validationErrors.add( new ErrorResponse.ErrorData(tenantId, "", constraintViolation.getPropertyPath().toString(), constraintViolation.getMessage(), event.getWhen())); } if (!validationErrors.isEmpty()) { DefaultHandler.sendErrorResponse(ctx, request, validationErrors, HttpResponseStatus.BAD_REQUEST); return; } searchIO.insert(tenantId, event.toMap()); DefaultHandler.sendResponse(ctx, request, response, HttpResponseStatus.OK); } catch (JsonMappingException e) { log.debug(String.format("Exception %s", e.toString())); response = String.format("Invalid Data: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.BAD_REQUEST); } catch (JsonParseException e){ log.debug(String.format("Exception %s", e.toString())); response = String.format("Invalid Data: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.BAD_REQUEST); } catch (InvalidDataException e) { log.debug(String.format("Exception %s", e.toString())); response = String.format("Invalid Data: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.BAD_REQUEST); } catch (Exception e) { log.error(String.format("Exception %s", e.toString())); response = String.format("Error: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpEventsIngestTimerContext.stop(); } } HttpEventsIngestionHandler(EventsIO searchIO); }
HttpEventsIngestionHandler implements HttpRequestHandler { @Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { final String tenantId = request.headers().get(Event.FieldLabels.tenantId.name()); String response = ""; ObjectMapper objectMapper = new ObjectMapper(); final Timer.Context httpEventsIngestTimerContext = httpEventsIngestTimer.time(); try { String body = request.content().toString(0, request.content().writerIndex(), CharsetUtil.UTF_8); Event event = objectMapper.readValue(body, Event.class); Iterator<Event> iterator = objectMapper.reader(Event.class).readValues(body); if (iterator.hasNext()) { iterator.next(); if (iterator.hasNext()) { throw new InvalidDataException("Only one event is allowed per request"); } } Set<ConstraintViolation<Event>> constraintViolations = validator.validate(event); List<ErrorResponse.ErrorData> validationErrors = new ArrayList<ErrorResponse.ErrorData>(); for (ConstraintViolation<Event> constraintViolation : constraintViolations) { validationErrors.add( new ErrorResponse.ErrorData(tenantId, "", constraintViolation.getPropertyPath().toString(), constraintViolation.getMessage(), event.getWhen())); } if (!validationErrors.isEmpty()) { DefaultHandler.sendErrorResponse(ctx, request, validationErrors, HttpResponseStatus.BAD_REQUEST); return; } searchIO.insert(tenantId, event.toMap()); DefaultHandler.sendResponse(ctx, request, response, HttpResponseStatus.OK); } catch (JsonMappingException e) { log.debug(String.format("Exception %s", e.toString())); response = String.format("Invalid Data: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.BAD_REQUEST); } catch (JsonParseException e){ log.debug(String.format("Exception %s", e.toString())); response = String.format("Invalid Data: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.BAD_REQUEST); } catch (InvalidDataException e) { log.debug(String.format("Exception %s", e.toString())); response = String.format("Invalid Data: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.BAD_REQUEST); } catch (Exception e) { log.error(String.format("Exception %s", e.toString())); response = String.format("Error: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpEventsIngestTimerContext.stop(); } } HttpEventsIngestionHandler(EventsIO searchIO); @Override void handle(ChannelHandlerContext ctx, FullHttpRequest request); }
HttpEventsIngestionHandler implements HttpRequestHandler { @Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { final String tenantId = request.headers().get(Event.FieldLabels.tenantId.name()); String response = ""; ObjectMapper objectMapper = new ObjectMapper(); final Timer.Context httpEventsIngestTimerContext = httpEventsIngestTimer.time(); try { String body = request.content().toString(0, request.content().writerIndex(), CharsetUtil.UTF_8); Event event = objectMapper.readValue(body, Event.class); Iterator<Event> iterator = objectMapper.reader(Event.class).readValues(body); if (iterator.hasNext()) { iterator.next(); if (iterator.hasNext()) { throw new InvalidDataException("Only one event is allowed per request"); } } Set<ConstraintViolation<Event>> constraintViolations = validator.validate(event); List<ErrorResponse.ErrorData> validationErrors = new ArrayList<ErrorResponse.ErrorData>(); for (ConstraintViolation<Event> constraintViolation : constraintViolations) { validationErrors.add( new ErrorResponse.ErrorData(tenantId, "", constraintViolation.getPropertyPath().toString(), constraintViolation.getMessage(), event.getWhen())); } if (!validationErrors.isEmpty()) { DefaultHandler.sendErrorResponse(ctx, request, validationErrors, HttpResponseStatus.BAD_REQUEST); return; } searchIO.insert(tenantId, event.toMap()); DefaultHandler.sendResponse(ctx, request, response, HttpResponseStatus.OK); } catch (JsonMappingException e) { log.debug(String.format("Exception %s", e.toString())); response = String.format("Invalid Data: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.BAD_REQUEST); } catch (JsonParseException e){ log.debug(String.format("Exception %s", e.toString())); response = String.format("Invalid Data: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.BAD_REQUEST); } catch (InvalidDataException e) { log.debug(String.format("Exception %s", e.toString())); response = String.format("Invalid Data: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.BAD_REQUEST); } catch (Exception e) { log.error(String.format("Exception %s", e.toString())); response = String.format("Error: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpEventsIngestTimerContext.stop(); } } HttpEventsIngestionHandler(EventsIO searchIO); @Override void handle(ChannelHandlerContext ctx, FullHttpRequest request);  }