Java Code Examples for retrofit.mime.TypedInput#in()

The following examples show how to use retrofit.mime.TypedInput#in() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SignalFxConverter.java    From kayenta with Apache License 2.0 6 votes vote down vote up
private SignalFlowExecutionResult getSignalFlowExecutionResultFromBody(TypedInput body)
    throws ConversionException {
  List<ChannelMessage> messages = new LinkedList<>();
  try (ServerSentEventsTransport.TransportEventStreamParser parser =
      new ServerSentEventsTransport.TransportEventStreamParser(body.in())) {

    while (parser.hasNext()) {
      StreamMessage streamMessage = parser.next();
      ChannelMessage channelMessage = ChannelMessage.decodeStreamMessage(streamMessage);
      messages.add(channelMessage);
    }
  } catch (Exception e) {
    throw new ConversionException("There was an issue parsing the SignalFlow response", e);
  }
  return new SignalFlowExecutionResult(messages);
}
 
Example 2
Source File: ConfigBinResponseConverter.java    From kayenta with Apache License 2.0 6 votes vote down vote up
@Override
public String fromBody(TypedInput body, Type type) throws ConversionException {
  try {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead;
    byte[] data = new byte[1024];
    InputStream inputStream = body.in();
    while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
      buffer.write(data, 0, nRead);
    }
    buffer.flush();
    byte[] byteArray = buffer.toByteArray();
    return new String(byteArray, StandardCharsets.UTF_8);
  } catch (IOException e) {
    log.error("Unable to read response body or convert it to a UTF-8 string", e);
    return null;
  }
}
 
Example 3
Source File: GoogleAnalytics.java    From foam with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Object fromBody(TypedInput typedInput, Type type) throws ConversionException {
    String text = null;
    try {
        typedInput.in();
        BufferedReader reader = new BufferedReader(new InputStreamReader(typedInput.in()));
        StringBuilder out = new StringBuilder();
        String newLine = System.getProperty("line.separator");
        String line;
        while ((line = reader.readLine()) != null) {
            out.append(line);
            out.append(newLine);
        }
        text = out.toString();
    } catch (IOException ignored) {/*NOP*/ }
    return text;
}
 
Example 4
Source File: WebViewFragment.java    From dhis2-android-dashboard with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
static String readInputStream(TypedInput in) {
    StringBuilder builder = new StringBuilder();
    try {
        BufferedReader bufferedStream
                = new BufferedReader(new InputStreamReader(in.in()));
        try {
            String line;
            while ((line = bufferedStream.readLine()) != null) {
                builder.append(line);
                builder.append('\n');
            }
            return builder.toString();
        } finally {
            bufferedStream.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return builder.toString();
}
 
Example 5
Source File: JacksonConverter.java    From android-skeleton-project with MIT License 6 votes vote down vote up
@Override
public Object fromBody(TypedInput typedInput, Type type) throws ConversionException {
    JavaType javaType = objectMapper.getTypeFactory().constructType(type);

    try {
        InputStream in = typedInput.in();
        try {
            return objectMapper.readValue(in, javaType);
        } finally {
             if (in != null) {
                 in.close();
             }
        }


    } catch (IOException e) {
        throw new ConversionException(e);
    }
}
 
Example 6
Source File: AtlasSSEConverter.java    From kayenta with Apache License 2.0 5 votes vote down vote up
@Override
public List<AtlasResults> fromBody(TypedInput body, Type type) throws ConversionException {
  try (BufferedReader reader = new BufferedReader(new InputStreamReader(body.in()))) {
    return processInput(reader);
  } catch (IOException e) {
    log.error("Cannot process Atlas results", e);
  }
  return null;
}
 
Example 7
Source File: PrometheusResponseConverter.java    From kayenta with Apache License 2.0 4 votes vote down vote up
@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException {
  if (type == PrometheusMetricDescriptorsResponse.class) {
    return new JacksonConverter(kayentaObjectMapper).fromBody(body, type);
  } else {
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(body.in()))) {
      String json = reader.readLine();
      Map responseMap = kayentaObjectMapper.readValue(json, Map.class);
      Map data = (Map) responseMap.get("data");
      List<Map> resultList = (List<Map>) data.get("result");
      List<PrometheusResults> prometheusResultsList =
          new ArrayList<PrometheusResults>(resultList.size());

      if (CollectionUtils.isEmpty(resultList)) {
        log.warn("Received no data from Prometheus.");
        return null;
      }

      for (Map elem : resultList) {
        Map<String, String> tags = (Map<String, String>) elem.get("metric");
        String id = tags.remove("__name__");
        List<List> values = (List<List>) elem.get("values");
        List<Double> dataValues = new ArrayList<Double>(values.size());

        for (List tuple : values) {
          String val = (String) tuple.get(1);
          if (val != null) {
            switch (val) {
              case "+Inf":
                dataValues.add(Double.POSITIVE_INFINITY);
                break;
              case "-Inf":
                dataValues.add(Double.NEGATIVE_INFINITY);
                break;
              case "NaN":
                dataValues.add(Double.NaN);
                break;
              default:
                dataValues.add(Double.valueOf(val));
            }
          }
        }

        long startTimeMillis =
            doubleTimestampSecsToLongTimestampMillis(values.get(0).get(0) + "");
        // If there aren't at least two data points, consider the step size to be zero.
        long stepSecs =
            values.size() > 1
                ? TimeUnit.MILLISECONDS.toSeconds(
                    doubleTimestampSecsToLongTimestampMillis(values.get(1).get(0) + "")
                        - startTimeMillis)
                : 0;
        long endTimeMillis = startTimeMillis + values.size() * stepSecs * 1000;

        prometheusResultsList.add(
            new PrometheusResults(
                id, startTimeMillis, stepSecs, endTimeMillis, tags, dataValues));
      }

      return prometheusResultsList;
    } catch (IOException e) {
      e.printStackTrace();
    }

    return null;
  }
}
 
Example 8
Source File: InfluxDbResponseConverter.java    From kayenta with Apache License 2.0 4 votes vote down vote up
@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException {

  try (BufferedReader reader = new BufferedReader(new InputStreamReader(body.in()))) {
    String json = reader.readLine();
    log.debug("Converting response from influxDb: {}", json);

    Map result = getResultObject(json);
    List<Map> seriesList = (List<Map>) result.get("series");

    if (CollectionUtils.isEmpty(seriesList)) {
      log.warn("Received no data from Influxdb.");
      return null;
    }

    Map series = seriesList.get(0);
    List<String> seriesColumns = (List<String>) series.get("columns");
    List<List> seriesValues = (List<List>) series.get("values");
    List<InfluxDbResult> influxDbResultsList = new ArrayList<InfluxDbResult>(seriesValues.size());

    // TODO(joerajeev): if returning tags (other than the field names) we will need to skip tags
    // from this loop,
    // and to extract and set the tag values to the influxDb result.
    for (int i = 1;
        i < seriesColumns.size();
        i++) { // Starting from index 1 to skip 'time' column

      String id = seriesColumns.get(i);
      long firstTimeMillis = extractTimeInMillis(seriesValues, 0);
      long stepMillis = calculateStep(seriesValues, firstTimeMillis);
      List<Double> values = new ArrayList<>(seriesValues.size());
      for (List<Object> valueRow : seriesValues) {
        if (valueRow.get(i) != null) {
          values.add(Double.valueOf((Integer) valueRow.get(i)));
        }
      }
      influxDbResultsList.add(new InfluxDbResult(id, firstTimeMillis, stepMillis, null, values));
    }

    log.debug("Converted response: {} ", influxDbResultsList);
    return influxDbResultsList;
  } catch (IOException e) {
    e.printStackTrace();
  }

  return null;
}
 
Example 9
Source File: ExceptionCatchingTypedInput.java    From android-discourse with Apache License 2.0 4 votes vote down vote up
ExceptionCatchingTypedInput(TypedInput delegate) throws IOException {
    this.delegate = delegate;
    this.delegateStream = new ExceptionCatchingInputStream(delegate.in());
}