Search in sources :

Example 11 with HttpHeaders

use of com.azure.core.http.HttpHeaders in project ApplicationInsights-Java by microsoft.

the class QuickPulseNetworkHelper method getQuickPulseHeaderInfo.

public QuickPulseHeaderInfo getQuickPulseHeaderInfo(HttpResponse response) {
    HttpHeaders headers = response.getHeaders();
    QuickPulseStatus status = QuickPulseStatus.ERROR;
    long servicePollingIntervalHint = -1;
    String serviceEndpointRedirect = null;
    for (HttpHeader header : headers) {
        if (QPS_STATUS_HEADER.equalsIgnoreCase(header.getName())) {
            String qpStatus = header.getValue();
            if ("true".equalsIgnoreCase(qpStatus)) {
                status = QuickPulseStatus.QP_IS_ON;
            } else {
                status = QuickPulseStatus.QP_IS_OFF;
            }
        } else if (QPS_SERVICE_POLLING_INTERVAL_HINT.equalsIgnoreCase(header.getName())) {
            String servicePollingIntervalHintHeaderValue = header.getValue();
            if (!Strings.isNullOrEmpty(servicePollingIntervalHintHeaderValue)) {
                servicePollingIntervalHint = Long.parseLong(servicePollingIntervalHintHeaderValue);
            }
        } else if (QPS_SERVICE_ENDPOINT_REDIRECT.equalsIgnoreCase(header.getName())) {
            serviceEndpointRedirect = header.getValue();
        }
    }
    return new QuickPulseHeaderInfo(status, serviceEndpointRedirect, servicePollingIntervalHint);
}
Also used : HttpHeaders(com.azure.core.http.HttpHeaders) HttpHeader(com.azure.core.http.HttpHeader)

Example 12 with HttpHeaders

use of com.azure.core.http.HttpHeaders in project terra-cloud-resource-lib by DataBiosphere.

the class AzureResponseLogger method logBody.

private static void logBody(HttpHeaders headers, Flux<ByteBuffer> body, Consumer<String> consumer) {
    // Ensure we have a valid content length
    String contentLengthString = headers.getValue("Content-Length");
    if (CoreUtils.isNullOrEmpty(contentLengthString)) {
        return;
    }
    final long contentLength;
    try {
        contentLength = Long.parseLong(contentLengthString);
    } catch (NumberFormatException | NullPointerException e) {
        return;
    }
    // The body is logged if the Content-Type is not "application/octet-stream" and the
    // body isn't empty and is less than 16KB in size.
    String contentTypeHeader = headers.getValue("Content-Type");
    if (!ContentType.APPLICATION_OCTET_STREAM.equalsIgnoreCase(contentTypeHeader) && contentLength != 0 && contentLength < MAX_BODY_LOG_SIZE) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream((int) contentLength);
        WritableByteChannel bodyContentChannel = Channels.newChannel(outputStream);
        body.flatMap(byteBuffer -> {
            try {
                bodyContentChannel.write(byteBuffer.duplicate());
                return Mono.just(byteBuffer);
            } catch (IOException e) {
                return Mono.error(e);
            }
        }).doFinally(ignored -> consumer.accept(outputStream.toString(Charsets.UTF_8)));
    }
}
Also used : JsonObject(com.google.gson.JsonObject) HttpResponse(com.azure.core.http.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LoggerFactory(org.slf4j.LoggerFactory) CoreUtils(com.azure.core.util.CoreUtils) OptionalInt(java.util.OptionalInt) OperationData(bio.terra.cloudres.common.OperationData) ByteBuffer(java.nio.ByteBuffer) Duration(java.time.Duration) Charsets(com.google.common.base.Charsets) Logger(org.slf4j.Logger) HttpResponseLogger(com.azure.core.http.policy.HttpResponseLogger) ClientLogger(com.azure.core.util.logging.ClientLogger) Channels(java.nio.channels.Channels) ClientConfig(bio.terra.cloudres.common.ClientConfig) OperationAnnotator(bio.terra.cloudres.common.OperationAnnotator) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) HttpHeaders(com.azure.core.http.HttpHeaders) CLOUD_RESOURCE_REQUEST_DATA_KEY(bio.terra.cloudres.azure.resourcemanager.common.Defaults.CLOUD_RESOURCE_REQUEST_DATA_KEY) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) HttpRequest(com.azure.core.http.HttpRequest) HttpResponseLoggingContext(com.azure.core.http.policy.HttpResponseLoggingContext) ContentType(com.azure.core.http.ContentType) WritableByteChannel(java.nio.channels.WritableByteChannel) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) WritableByteChannel(java.nio.channels.WritableByteChannel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

HttpHeaders (com.azure.core.http.HttpHeaders)12 Test (org.junit.jupiter.api.Test)6 HttpRequest (com.azure.core.http.HttpRequest)4 HttpResponse (com.azure.core.http.HttpResponse)4 Mono (reactor.core.publisher.Mono)4 HttpPipeline (com.azure.core.http.HttpPipeline)3 HttpPipelineBuilder (com.azure.core.http.HttpPipelineBuilder)3 URI (java.net.URI)3 URL (java.net.URL)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 HttpHeader (com.azure.core.http.HttpHeader)2 HttpMethod (com.azure.core.http.HttpMethod)2 MockHttpResponse (com.microsoft.applicationinsights.agent.internal.MockHttpResponse)2 TelemetryClient (com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient)2 URISyntaxException (java.net.URISyntaxException)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 CLOUD_RESOURCE_REQUEST_DATA_KEY (bio.terra.cloudres.azure.resourcemanager.common.Defaults.CLOUD_RESOURCE_REQUEST_DATA_KEY)1 ClientConfig (bio.terra.cloudres.common.ClientConfig)1 OperationAnnotator (bio.terra.cloudres.common.OperationAnnotator)1