private static FullHttpResponse newHttpResponseWithStackTrace( Exception e, HttpResponseStatus status, @Nullable String simplifiedMessage) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); StringBuilder sb = new StringBuilder(); try { JsonGenerator jg = jsonFactory.createGenerator(CharStreams.asWriter(sb)); jg.writeStartObject(); String message; if (simplifiedMessage == null) { Throwable cause = e; Throwable childCause = cause.getCause(); while (childCause != null) { cause = childCause; childCause = cause.getCause(); } message = cause.getMessage(); } else { message = simplifiedMessage; } jg.writeStringField("message", message); jg.writeStringField("stackTrace", sw.toString()); jg.writeEndObject(); jg.close(); return HttpServices.createJsonResponse(sb.toString(), status); } catch (IOException f) { logger.error(f.getMessage(), f); return new DefaultFullHttpResponse(HTTP_1_1, INTERNAL_SERVER_ERROR); } }
@GET("/backend/aggregate/points") String getPoints(String content) throws IOException { logger.debug("getPoints(): content={}", content); PointsRequest request = ObjectMappers.readRequiredValue(mapper, content, PointsRequest.class); List<AggregatePoint> points = aggregateDao.readAggregates(request.getFrom(), request.getTo()); StringBuilder sb = new StringBuilder(); JsonGenerator jg = mapper.getFactory().createGenerator(CharStreams.asWriter(sb)); jg.writeStartObject(); jg.writeArrayFieldStart("points"); for (AggregatePoint point : points) { jg.writeStartArray(); jg.writeNumber(point.getCaptureTime()); long durationAverage; if (point.getTraceCount() == 0) { durationAverage = 0; } else { durationAverage = point.getDurationTotal() / point.getTraceCount(); } jg.writeNumber(durationAverage / 1000000000.0); jg.writeNumber(point.getTraceCount()); jg.writeEndArray(); } jg.writeEndArray(); jg.writeNumberField("fixedAggregateIntervalSeconds", fixedAggregateIntervalSeconds); jg.writeEndObject(); jg.close(); return sb.toString(); }
private static FullHttpResponse newHttpResponseWithMessage( HttpResponseStatus status, @Nullable String message) { // this is an "expected" exception, no need to send back stack trace StringBuilder sb = new StringBuilder(); try { JsonGenerator jg = jsonFactory.createGenerator(CharStreams.asWriter(sb)); jg.writeStartObject(); jg.writeStringField("message", message); jg.writeEndObject(); jg.close(); return HttpServices.createJsonResponse(sb.toString(), status); } catch (IOException f) { logger.error(f.getMessage(), f); return new DefaultFullHttpResponse(HTTP_1_1, INTERNAL_SERVER_ERROR); } }