Search in sources :

Example 21 with HttpPipeline

use of com.azure.android.core.http.HttpPipeline in project azure-sdk-for-android by Azure.

the class HttpLoggingPolicyTests method redactQueryParameters.

/**
 * Tests that a query string will be properly redacted before it is logged.
 */
@ParameterizedTest
@MethodSource("redactQueryParametersSupplier")
@ResourceLock("SYSTEM_OUT")
public void redactQueryParameters(String requestUrl, String expectedQueryString, Set<String> allowedQueryParameters) {
    HttpPipeline pipeline = new HttpPipelineBuilder().policies(new HttpLoggingPolicy(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC).setAllowedQueryParamNames(allowedQueryParameters))).httpClient(new NoOpHttpClient()).build();
    CountDownLatch latch = new CountDownLatch(1);
    // pipeline.send(new HttpRequest(HttpMethod.POST, requestUrl), CONTEXT, new HttpCallback() {..})
    pipeline.send(new HttpRequest(HttpMethod.POST, requestUrl), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {

        @Override
        public void onSuccess(HttpResponse response) {
            latch.countDown();
        }

        @Override
        public void onError(Throwable error) {
            try {
                assertTrue(false, "unexpected call to pipeline::send onError" + error.getMessage());
            } finally {
                latch.countDown();
            }
        }
    });
    awaitOnLatch(latch, "redactQueryParameters");
    assertTrue(convertOutputStreamToString(logCaptureStream).contains(expectedQueryString));
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) HttpPipeline(com.azure.android.core.http.HttpPipeline) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) HttpCallback(com.azure.android.core.http.HttpCallback) HttpResponse(com.azure.android.core.http.HttpResponse) CountDownLatch(java.util.concurrent.CountDownLatch) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 22 with HttpPipeline

use of com.azure.android.core.http.HttpPipeline in project azure-sdk-for-android by Azure.

the class ChatThreadClientBuilder method createInternalClient.

private AzureCommunicationChatServiceImpl createInternalClient() {
    if (endpoint == null) {
        throw logger.logExceptionAsError(new NullPointerException("'endpoint' is required."));
    }
    HttpPipeline pipeline;
    if (this.httpPipeline != null) {
        pipeline = this.httpPipeline;
    } else {
        if (this.communicationTokenCredential == null) {
            throw logger.logExceptionAsError(new NullPointerException("CommunicationTokenCredential is required."));
        }
        HttpPipelinePolicy authorizationPolicy = chain -> {
            final CompletableFuture<CommunicationAccessToken> tokenFuture = this.communicationTokenCredential.getToken();
            final CommunicationAccessToken token;
            try {
                token = tokenFuture.get();
            } catch (ExecutionException e) {
                chain.completedError(e);
                return;
            } catch (InterruptedException e) {
                chain.completedError(e);
                return;
            }
            HttpRequest httpRequest = chain.getRequest();
            httpRequest.getHeaders().put("Authorization", "Bearer " + token.getToken());
            chain.processNextPolicy(httpRequest);
        };
        pipeline = createHttpPipeline(this.httpClient, authorizationPolicy, this.customPolicies);
    }
    AzureCommunicationChatServiceImplBuilder clientBuilder = new AzureCommunicationChatServiceImplBuilder().apiVersion((this.serviceVersion == null) ? ChatServiceVersion.getLatest().getVersion() : this.serviceVersion.getVersion()).endpoint(this.endpoint).pipeline(pipeline);
    return clientBuilder.buildClient();
}
Also used : AzureCommunicationChatServiceImplBuilder(com.azure.android.communication.chat.implementation.AzureCommunicationChatServiceImplBuilder) AzureCommunicationChatServiceImpl(com.azure.android.communication.chat.implementation.AzureCommunicationChatServiceImpl) VERSION_NAME(com.azure.android.communication.chat.BuildConfig.VERSION_NAME) RetryPolicy(com.azure.android.core.http.policy.RetryPolicy) ServiceClientBuilder(com.azure.android.core.rest.annotation.ServiceClientBuilder) UserAgentPolicy(com.azure.android.core.http.policy.UserAgentPolicy) CompletableFuture(java9.util.concurrent.CompletableFuture) ClientLogger(com.azure.android.core.logging.ClientLogger) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) LIBRARY_PACKAGE_NAME(com.azure.android.communication.chat.BuildConfig.LIBRARY_PACKAGE_NAME) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) HttpLoggingPolicy(com.azure.android.core.http.policy.HttpLoggingPolicy) List(java.util.List) HttpLogOptions(com.azure.android.core.http.policy.HttpLogOptions) HttpPipelinePolicy(com.azure.android.core.http.HttpPipelinePolicy) HttpPipeline(com.azure.android.core.http.HttpPipeline) CookiePolicy(com.azure.android.core.http.policy.CookiePolicy) CommunicationTokenCredential(com.azure.android.communication.common.CommunicationTokenCredential) HttpClient(com.azure.android.core.http.HttpClient) HttpRequest(com.azure.android.core.http.HttpRequest) CommunicationAccessToken(com.azure.android.communication.common.CommunicationAccessToken) CommunicationAccessToken(com.azure.android.communication.common.CommunicationAccessToken) HttpRequest(com.azure.android.core.http.HttpRequest) HttpPipelinePolicy(com.azure.android.core.http.HttpPipelinePolicy) CompletableFuture(java9.util.concurrent.CompletableFuture) AzureCommunicationChatServiceImplBuilder(com.azure.android.communication.chat.implementation.AzureCommunicationChatServiceImplBuilder) HttpPipeline(com.azure.android.core.http.HttpPipeline) ExecutionException(java.util.concurrent.ExecutionException)

Example 23 with HttpPipeline

use of com.azure.android.core.http.HttpPipeline in project azure-sdk-for-android by Azure.

the class AzureCommunicationChatServiceImplBuilder method createHttpPipeline.

private HttpPipeline createHttpPipeline() {
    if (httpLogOptions == null) {
        httpLogOptions = new HttpLogOptions();
    }
    List<HttpPipelinePolicy> policies = new ArrayList<>();
    String clientName = properties.get(SDK_NAME);
    if (clientName == null) {
        clientName = "UnknownName";
    }
    String clientVersion = properties.get(SDK_VERSION);
    if (clientVersion == null) {
        clientVersion = "UnknownVersion";
    }
    policies.add(new UserAgentPolicy(null, clientName, clientVersion));
    policies.add(retryPolicy == null ? RetryPolicy.withExponentialBackoff() : retryPolicy);
    policies.add(new CookiePolicy());
    policies.addAll(this.pipelinePolicies);
    policies.add(new HttpLoggingPolicy(httpLogOptions));
    HttpPipeline httpPipeline = new HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])).httpClient(httpClient).build();
    return httpPipeline;
}
Also used : HttpPipelinePolicy(com.azure.android.core.http.HttpPipelinePolicy) UserAgentPolicy(com.azure.android.core.http.policy.UserAgentPolicy) HttpPipeline(com.azure.android.core.http.HttpPipeline) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) ArrayList(java.util.ArrayList) CookiePolicy(com.azure.android.core.http.policy.CookiePolicy) HttpLogOptions(com.azure.android.core.http.policy.HttpLogOptions) HttpLoggingPolicy(com.azure.android.core.http.policy.HttpLoggingPolicy)

Example 24 with HttpPipeline

use of com.azure.android.core.http.HttpPipeline in project azure-sdk-for-android by Azure.

the class ChatClientBuilder method buildAsyncClient.

/**
 * Create asynchronous client applying CommunicationTokenCredential, UserAgentPolicy,
 * RetryPolicy, and CookiePolicy.
 * Additional HttpPolicies specified by additionalPolicies will be applied after them
 *
 * @throws NullPointerException if endpoint or CommunicationTokenCredential is not set.
 * @return A {@link ChatAsyncClient} instance.
 */
public ChatAsyncClient buildAsyncClient() {
    if (this.endpoint == null) {
        throw logger.logExceptionAsError(new NullPointerException("Endpoint is required."));
    }
    HttpPipeline pipeline;
    if (this.httpPipeline != null) {
        pipeline = this.httpPipeline;
    } else {
        if (this.communicationTokenCredential == null) {
            throw logger.logExceptionAsError(new NullPointerException("CommunicationTokenCredential is required."));
        }
        HttpPipelinePolicy authorizationPolicy = chain -> {
            final CompletableFuture<CommunicationAccessToken> tokenFuture = this.communicationTokenCredential.getToken();
            final CommunicationAccessToken token;
            try {
                token = tokenFuture.get();
            } catch (ExecutionException e) {
                chain.completedError(e);
                return;
            } catch (InterruptedException e) {
                chain.completedError(e);
                return;
            }
            HttpRequest httpRequest = chain.getRequest();
            httpRequest.getHeaders().put("Authorization", "Bearer " + token.getToken());
            chain.processNextPolicy(httpRequest);
        };
        pipeline = createHttpPipeline(this.httpClient, authorizationPolicy, this.customPolicies);
    }
    AzureCommunicationChatServiceImplBuilder clientBuilder = new AzureCommunicationChatServiceImplBuilder().apiVersion((this.serviceVersion == null) ? ChatServiceVersion.getLatest().getVersion() : this.serviceVersion.getVersion()).endpoint(this.endpoint).pipeline(pipeline);
    return new ChatAsyncClient(clientBuilder.buildClient(), this.communicationTokenCredential);
}
Also used : AzureCommunicationChatServiceImplBuilder(com.azure.android.communication.chat.implementation.AzureCommunicationChatServiceImplBuilder) VERSION_NAME(com.azure.android.communication.chat.BuildConfig.VERSION_NAME) RetryPolicy(com.azure.android.core.http.policy.RetryPolicy) ServiceClientBuilder(com.azure.android.core.rest.annotation.ServiceClientBuilder) UserAgentPolicy(com.azure.android.core.http.policy.UserAgentPolicy) CompletableFuture(java9.util.concurrent.CompletableFuture) ClientLogger(com.azure.android.core.logging.ClientLogger) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) LIBRARY_PACKAGE_NAME(com.azure.android.communication.chat.BuildConfig.LIBRARY_PACKAGE_NAME) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) HttpLoggingPolicy(com.azure.android.core.http.policy.HttpLoggingPolicy) List(java.util.List) HttpLogOptions(com.azure.android.core.http.policy.HttpLogOptions) HttpPipelinePolicy(com.azure.android.core.http.HttpPipelinePolicy) HttpPipeline(com.azure.android.core.http.HttpPipeline) CookiePolicy(com.azure.android.core.http.policy.CookiePolicy) CommunicationTokenCredential(com.azure.android.communication.common.CommunicationTokenCredential) HttpClient(com.azure.android.core.http.HttpClient) HttpRequest(com.azure.android.core.http.HttpRequest) CommunicationAccessToken(com.azure.android.communication.common.CommunicationAccessToken) CommunicationAccessToken(com.azure.android.communication.common.CommunicationAccessToken) HttpRequest(com.azure.android.core.http.HttpRequest) HttpPipelinePolicy(com.azure.android.core.http.HttpPipelinePolicy) CompletableFuture(java9.util.concurrent.CompletableFuture) AzureCommunicationChatServiceImplBuilder(com.azure.android.communication.chat.implementation.AzureCommunicationChatServiceImplBuilder) HttpPipeline(com.azure.android.core.http.HttpPipeline) ExecutionException(java.util.concurrent.ExecutionException)

Example 25 with HttpPipeline

use of com.azure.android.core.http.HttpPipeline in project azure-sdk-for-android by Azure.

the class RestProxyXMLTests method canDeserializeXMLWithAttributes.

@Test
public void canDeserializeXMLWithAttributes() throws Exception {
    JacksonSerder serializer = new JacksonSerder();
    final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new MockXMLHTTPClient()).build();
    MyXMLServiceWithAttributes myXMLService = RestProxy.create(MyXMLServiceWithAttributes.class, pipeline, serializer);
    final SlideshowOrError slideshowOrError = new SlideshowOrError();
    CountDownLatch latch = new CountDownLatch(1);
    myXMLService.getSlideshow(new Callback<Response<Slideshow>>() {

        @Override
        public void onSuccess(Response<Slideshow> response) {
            try {
                slideshowOrError.slideshow = response.getValue();
            } finally {
                latch.countDown();
            }
        }

        @Override
        public void onFailure(Throwable error) {
            try {
                slideshowOrError.error = error;
            } finally {
                latch.countDown();
            }
        }
    });
    awaitOnLatch(latch, "canDeserializeXMLWithAttributes");
    if (slideshowOrError.error != null) {
        fail(slideshowOrError.error);
    } else {
        Slideshow slideshow = slideshowOrError.slideshow;
        assertEquals("Sample Slide Show", slideshow.title());
        assertEquals("Date of publication", slideshow.date());
        assertEquals("Yours Truly", slideshow.author());
        assertEquals(2, slideshow.slides().length);
        assertEquals("all", slideshow.slides()[0].type());
        assertEquals("Wake up to WonderWidgets!", slideshow.slides()[0].title());
        assertEquals(0, slideshow.slides()[0].items().length);
        assertEquals("all", slideshow.slides()[1].type());
        assertEquals("Overview", slideshow.slides()[1].title());
        assertEquals(3, slideshow.slides()[1].items().length);
        assertEquals("Why WonderWidgets are great", slideshow.slides()[1].items()[0]);
        assertEquals("", slideshow.slides()[1].items()[1]);
        assertEquals("Who buys WonderWidgets", slideshow.slides()[1].items()[2]);
        String xml = serializer.serialize(slideshow, SerdeEncoding.XML);
        Slideshow newSlideshow = serializer.deserialize(xml, Slideshow.class, SerdeEncoding.XML);
        String newXML = serializer.serialize(newSlideshow, SerdeEncoding.XML);
        assertEquals(xml, newXML);
    }
}
Also used : JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) HttpResponse(com.azure.android.core.http.HttpResponse) HttpPipeline(com.azure.android.core.http.HttpPipeline) Test(org.junit.jupiter.api.Test)

Aggregations

HttpPipeline (com.azure.android.core.http.HttpPipeline)25 CountDownLatch (java.util.concurrent.CountDownLatch)22 HttpPipelineBuilder (com.azure.android.core.http.HttpPipelineBuilder)21 HttpResponse (com.azure.android.core.http.HttpResponse)21 Test (org.junit.jupiter.api.Test)19 HttpCallback (com.azure.android.core.http.HttpCallback)18 HttpRequest (com.azure.android.core.http.HttpRequest)17 CancellationToken (com.azure.android.core.util.CancellationToken)13 HttpClient (com.azure.android.core.http.HttpClient)5 HttpHeaders (com.azure.android.core.http.HttpHeaders)4 HttpLogOptions (com.azure.android.core.http.policy.HttpLogOptions)4 HttpLoggingPolicy (com.azure.android.core.http.policy.HttpLoggingPolicy)4 HttpPipelinePolicy (com.azure.android.core.http.HttpPipelinePolicy)3 CookiePolicy (com.azure.android.core.http.policy.CookiePolicy)3 UserAgentPolicy (com.azure.android.core.http.policy.UserAgentPolicy)3 JacksonSerder (com.azure.android.core.serde.jackson.JacksonSerder)3 ArrayList (java.util.ArrayList)3 ResourceLock (org.junit.jupiter.api.parallel.ResourceLock)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3