@Test public void testMissingRequiredQueryParams() throws IOException { FullHttpRequest request = createQueryRequest("?from=111111", createRequestBody(1)); ArgumentCaptor<FullHttpResponse> argument = ArgumentCaptor.forClass(FullHttpResponse.class); handler.handle(context, request); 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 error message", "Either 'points' or 'resolution' is required.", errorResponse.getErrors().get(0).getMessage()); 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) { Tracker.getInstance().track(request); final String tenantId = request.headers().get("tenantId"); if (!(request instanceof HttpRequestWithDecodedQueryParams)) { DefaultHandler.sendErrorResponse(ctx, request, "Missing query params: from, to, points", HttpResponseStatus.BAD_REQUEST); return; } final String body = request.content().toString(Constants.DEFAULT_CHARSET); if (body == null || body.isEmpty()) { DefaultHandler.sendErrorResponse(ctx, request, "Invalid body. Expected JSON array of metrics.", HttpResponseStatus.BAD_REQUEST); return; } List<String> locators = new ArrayList<String>(); try { locators.addAll(getLocatorsFromJSONBody(tenantId, body)); } catch (Exception ex) { log.debug(ex.getMessage(), ex); sendResponse(ctx, request, ex.getMessage(), HttpResponseStatus.BAD_REQUEST); return; } if (locators.size() > maxMetricsPerRequest) { DefaultHandler.sendErrorResponse(ctx, request, "Too many metrics fetch in a single call. Max limit is " + maxMetricsPerRequest + ".", HttpResponseStatus.BAD_REQUEST); return; } HttpRequestWithDecodedQueryParams requestWithParams = (HttpRequestWithDecodedQueryParams) request; final Timer.Context httpBatchMetricsFetchTimerContext = httpBatchMetricsFetchTimer.time(); try { RollupsQueryParams params = PlotRequestParser.parseParams(requestWithParams.getQueryParams()); Map<Locator, MetricData> results = getRollupByGranularity(tenantId, locators, params.getRange().getStart(), params.getRange().getStop(), params.getGranularity(tenantId)); JSONObject metrics = serializer.transformRollupData(results, params.getStats()); final JsonElement element = parser.parse(metrics.toString()); final String jsonStringRep = gson.toJson(element); sendResponse(ctx, request, jsonStringRep, HttpResponseStatus.OK); } catch (InvalidRequestException e) { log.debug(e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.BAD_REQUEST); } catch (SerializationException e) { log.debug(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } catch (Exception e) { log.error(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpBatchMetricsFetchTimerContext.stop(); } }
HttpMultiRollupsQueryHandler extends RollupHandler implements HttpRequestHandler { @Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { Tracker.getInstance().track(request); final String tenantId = request.headers().get("tenantId"); if (!(request instanceof HttpRequestWithDecodedQueryParams)) { DefaultHandler.sendErrorResponse(ctx, request, "Missing query params: from, to, points", HttpResponseStatus.BAD_REQUEST); return; } final String body = request.content().toString(Constants.DEFAULT_CHARSET); if (body == null || body.isEmpty()) { DefaultHandler.sendErrorResponse(ctx, request, "Invalid body. Expected JSON array of metrics.", HttpResponseStatus.BAD_REQUEST); return; } List<String> locators = new ArrayList<String>(); try { locators.addAll(getLocatorsFromJSONBody(tenantId, body)); } catch (Exception ex) { log.debug(ex.getMessage(), ex); sendResponse(ctx, request, ex.getMessage(), HttpResponseStatus.BAD_REQUEST); return; } if (locators.size() > maxMetricsPerRequest) { DefaultHandler.sendErrorResponse(ctx, request, "Too many metrics fetch in a single call. Max limit is " + maxMetricsPerRequest + ".", HttpResponseStatus.BAD_REQUEST); return; } HttpRequestWithDecodedQueryParams requestWithParams = (HttpRequestWithDecodedQueryParams) request; final Timer.Context httpBatchMetricsFetchTimerContext = httpBatchMetricsFetchTimer.time(); try { RollupsQueryParams params = PlotRequestParser.parseParams(requestWithParams.getQueryParams()); Map<Locator, MetricData> results = getRollupByGranularity(tenantId, locators, params.getRange().getStart(), params.getRange().getStop(), params.getGranularity(tenantId)); JSONObject metrics = serializer.transformRollupData(results, params.getStats()); final JsonElement element = parser.parse(metrics.toString()); final String jsonStringRep = gson.toJson(element); sendResponse(ctx, request, jsonStringRep, HttpResponseStatus.OK); } catch (InvalidRequestException e) { log.debug(e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.BAD_REQUEST); } catch (SerializationException e) { log.debug(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } catch (Exception e) { log.error(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpBatchMetricsFetchTimerContext.stop(); } } }
HttpMultiRollupsQueryHandler extends RollupHandler implements HttpRequestHandler { @Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { Tracker.getInstance().track(request); final String tenantId = request.headers().get("tenantId"); if (!(request instanceof HttpRequestWithDecodedQueryParams)) { DefaultHandler.sendErrorResponse(ctx, request, "Missing query params: from, to, points", HttpResponseStatus.BAD_REQUEST); return; } final String body = request.content().toString(Constants.DEFAULT_CHARSET); if (body == null || body.isEmpty()) { DefaultHandler.sendErrorResponse(ctx, request, "Invalid body. Expected JSON array of metrics.", HttpResponseStatus.BAD_REQUEST); return; } List<String> locators = new ArrayList<String>(); try { locators.addAll(getLocatorsFromJSONBody(tenantId, body)); } catch (Exception ex) { log.debug(ex.getMessage(), ex); sendResponse(ctx, request, ex.getMessage(), HttpResponseStatus.BAD_REQUEST); return; } if (locators.size() > maxMetricsPerRequest) { DefaultHandler.sendErrorResponse(ctx, request, "Too many metrics fetch in a single call. Max limit is " + maxMetricsPerRequest + ".", HttpResponseStatus.BAD_REQUEST); return; } HttpRequestWithDecodedQueryParams requestWithParams = (HttpRequestWithDecodedQueryParams) request; final Timer.Context httpBatchMetricsFetchTimerContext = httpBatchMetricsFetchTimer.time(); try { RollupsQueryParams params = PlotRequestParser.parseParams(requestWithParams.getQueryParams()); Map<Locator, MetricData> results = getRollupByGranularity(tenantId, locators, params.getRange().getStart(), params.getRange().getStop(), params.getGranularity(tenantId)); JSONObject metrics = serializer.transformRollupData(results, params.getStats()); final JsonElement element = parser.parse(metrics.toString()); final String jsonStringRep = gson.toJson(element); sendResponse(ctx, request, jsonStringRep, HttpResponseStatus.OK); } catch (InvalidRequestException e) { log.debug(e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.BAD_REQUEST); } catch (SerializationException e) { log.debug(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } catch (Exception e) { log.error(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpBatchMetricsFetchTimerContext.stop(); } } HttpMultiRollupsQueryHandler(); @VisibleForTesting  HttpMultiRollupsQueryHandler(BatchedMetricsOutputSerializer<JSONObject> serializer); }
HttpMultiRollupsQueryHandler extends RollupHandler implements HttpRequestHandler { @Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { Tracker.getInstance().track(request); final String tenantId = request.headers().get("tenantId"); if (!(request instanceof HttpRequestWithDecodedQueryParams)) { DefaultHandler.sendErrorResponse(ctx, request, "Missing query params: from, to, points", HttpResponseStatus.BAD_REQUEST); return; } final String body = request.content().toString(Constants.DEFAULT_CHARSET); if (body == null || body.isEmpty()) { DefaultHandler.sendErrorResponse(ctx, request, "Invalid body. Expected JSON array of metrics.", HttpResponseStatus.BAD_REQUEST); return; } List<String> locators = new ArrayList<String>(); try { locators.addAll(getLocatorsFromJSONBody(tenantId, body)); } catch (Exception ex) { log.debug(ex.getMessage(), ex); sendResponse(ctx, request, ex.getMessage(), HttpResponseStatus.BAD_REQUEST); return; } if (locators.size() > maxMetricsPerRequest) { DefaultHandler.sendErrorResponse(ctx, request, "Too many metrics fetch in a single call. Max limit is " + maxMetricsPerRequest + ".", HttpResponseStatus.BAD_REQUEST); return; } HttpRequestWithDecodedQueryParams requestWithParams = (HttpRequestWithDecodedQueryParams) request; final Timer.Context httpBatchMetricsFetchTimerContext = httpBatchMetricsFetchTimer.time(); try { RollupsQueryParams params = PlotRequestParser.parseParams(requestWithParams.getQueryParams()); Map<Locator, MetricData> results = getRollupByGranularity(tenantId, locators, params.getRange().getStart(), params.getRange().getStop(), params.getGranularity(tenantId)); JSONObject metrics = serializer.transformRollupData(results, params.getStats()); final JsonElement element = parser.parse(metrics.toString()); final String jsonStringRep = gson.toJson(element); sendResponse(ctx, request, jsonStringRep, HttpResponseStatus.OK); } catch (InvalidRequestException e) { log.debug(e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.BAD_REQUEST); } catch (SerializationException e) { log.debug(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } catch (Exception e) { log.error(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpBatchMetricsFetchTimerContext.stop(); } } HttpMultiRollupsQueryHandler(); @VisibleForTesting  HttpMultiRollupsQueryHandler(BatchedMetricsOutputSerializer<JSONObject> serializer); @Override void handle(ChannelHandlerContext ctx, FullHttpRequest request); }
HttpMultiRollupsQueryHandler extends RollupHandler implements HttpRequestHandler { @Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { Tracker.getInstance().track(request); final String tenantId = request.headers().get("tenantId"); if (!(request instanceof HttpRequestWithDecodedQueryParams)) { DefaultHandler.sendErrorResponse(ctx, request, "Missing query params: from, to, points", HttpResponseStatus.BAD_REQUEST); return; } final String body = request.content().toString(Constants.DEFAULT_CHARSET); if (body == null || body.isEmpty()) { DefaultHandler.sendErrorResponse(ctx, request, "Invalid body. Expected JSON array of metrics.", HttpResponseStatus.BAD_REQUEST); return; } List<String> locators = new ArrayList<String>(); try { locators.addAll(getLocatorsFromJSONBody(tenantId, body)); } catch (Exception ex) { log.debug(ex.getMessage(), ex); sendResponse(ctx, request, ex.getMessage(), HttpResponseStatus.BAD_REQUEST); return; } if (locators.size() > maxMetricsPerRequest) { DefaultHandler.sendErrorResponse(ctx, request, "Too many metrics fetch in a single call. Max limit is " + maxMetricsPerRequest + ".", HttpResponseStatus.BAD_REQUEST); return; } HttpRequestWithDecodedQueryParams requestWithParams = (HttpRequestWithDecodedQueryParams) request; final Timer.Context httpBatchMetricsFetchTimerContext = httpBatchMetricsFetchTimer.time(); try { RollupsQueryParams params = PlotRequestParser.parseParams(requestWithParams.getQueryParams()); Map<Locator, MetricData> results = getRollupByGranularity(tenantId, locators, params.getRange().getStart(), params.getRange().getStop(), params.getGranularity(tenantId)); JSONObject metrics = serializer.transformRollupData(results, params.getStats()); final JsonElement element = parser.parse(metrics.toString()); final String jsonStringRep = gson.toJson(element); sendResponse(ctx, request, jsonStringRep, HttpResponseStatus.OK); } catch (InvalidRequestException e) { log.debug(e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.BAD_REQUEST); } catch (SerializationException e) { log.debug(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } catch (Exception e) { log.error(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpBatchMetricsFetchTimerContext.stop(); } } HttpMultiRollupsQueryHandler(); @VisibleForTesting  HttpMultiRollupsQueryHandler(BatchedMetricsOutputSerializer<JSONObject> serializer); @Override void handle(ChannelHandlerContext ctx, FullHttpRequest request);  }