Search in sources :

Example 1 with JsonReader

use of com.squareup.moshi.JsonReader in project zipkin by openzipkin.

the class AWSSignatureVersion4 method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Request input = chain.request();
    Request signed = sign(input);
    Response response = chain.proceed(signed);
    if (response.code() == 403) {
        try (ResponseBody body = response.body()) {
            JsonReader message = enterPath(JsonReader.of(body.source()), "message");
            if (message != null)
                throw new IllegalStateException(message.nextString());
        }
        throw new IllegalStateException(response.toString());
    }
    return response;
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) JsonReader(com.squareup.moshi.JsonReader) ResponseBody(okhttp3.ResponseBody)

Example 2 with JsonReader

use of com.squareup.moshi.JsonReader in project zipkin by openzipkin.

the class ElasticsearchDomainEndpoint method get.

@Override
public List<String> get() {
    try (Response response = client.newCall(describeElasticsearchDomain).execute()) {
        if (!response.isSuccessful()) {
            throw new IllegalStateException(response.body().string());
        }
        JsonReader endpointReader = enterPath(JsonReader.of(response.body().source()), "DomainStatus", "Endpoint");
        checkArgument(endpointReader != null, "DomainStatus.Endpoint wasn't present in response");
        String endpoint = endpointReader.nextString();
        if (!endpoint.startsWith("https://")) {
            endpoint = "https://" + endpoint;
        }
        log.fine("using endpoint " + endpoint);
        return Collections.singletonList(endpoint);
    } catch (IOException e) {
        throw new IllegalStateException("couldn't lookup domain endpoint", e);
    }
}
Also used : Response(okhttp3.Response) JsonReader(com.squareup.moshi.JsonReader) IOException(java.io.IOException)

Example 3 with JsonReader

use of com.squareup.moshi.JsonReader in project zipkin by openzipkin.

the class SearchResultConverter method convert.

@Override
public List<T> convert(BufferedSource content) throws IOException {
    JsonReader hits = enterPath(JsonReader.of(content), "hits", "hits");
    if (hits == null || hits.peek() != JsonReader.Token.BEGIN_ARRAY)
        return defaultValue;
    List<T> result = new ArrayList<>();
    hits.beginArray();
    while (hits.hasNext()) {
        JsonReader source = enterPath(hits, "_source");
        if (source != null) {
            result.add(adapter.fromJson(source));
        }
        hits.endObject();
    }
    hits.endArray();
    return result.isEmpty() ? defaultValue : result;
}
Also used : ArrayList(java.util.ArrayList) JsonReader(com.squareup.moshi.JsonReader)

Example 4 with JsonReader

use of com.squareup.moshi.JsonReader in project zipkin by openzipkin.

the class ElasticsearchHttpStorage method ensureClusterReady.

CheckResult ensureClusterReady(String index) {
    Request request = new Request.Builder().url(http().baseUrl.resolve("/_cluster/health/" + index)).tag("get-cluster-health").build();
    try {
        return http().execute(request, b -> {
            b.request(Long.MAX_VALUE);
            Buffer body = b.buffer();
            JsonReader status = enterPath(JsonReader.of(body.clone()), "status");
            if (status == null) {
                throw new IllegalStateException("Health status couldn't be read " + body.readUtf8());
            }
            if ("RED".equalsIgnoreCase(status.nextString())) {
                throw new IllegalStateException("Health status is RED");
            }
            return CheckResult.OK;
        });
    } catch (RuntimeException e) {
        return CheckResult.failed(e);
    }
}
Also used : Buffer(okio.Buffer) Request(okhttp3.Request) JsonReader(com.squareup.moshi.JsonReader)

Aggregations

JsonReader (com.squareup.moshi.JsonReader)4 Request (okhttp3.Request)2 Response (okhttp3.Response)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ResponseBody (okhttp3.ResponseBody)1 Buffer (okio.Buffer)1