Search in sources :

Example 11 with ZulipClientException

use of com.github.jamesnetherton.zulip.client.exception.ZulipClientException in project zulip-java-client by jamesnetherton.

the class ZulipCommonsHttpClient method configure.

/**
 * Configures the HTTP client.
 *
 * @throws ZulipClientException if configuration fails
 */
public void configure() throws ZulipClientException {
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(configuration.getEmail(), configuration.getApiKey());
    CredentialsProvider provider = new BasicCredentialsProvider();
    provider.setCredentials(AuthScope.ANY, credentials);
    HttpClientBuilder builder = HttpClientBuilder.create().setDefaultCredentialsProvider(provider);
    URL zulipUrl = configuration.getZulipUrl();
    HttpHost targetHost = new HttpHost(zulipUrl.getHost(), zulipUrl.getPort(), zulipUrl.getProtocol());
    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);
    URL proxyUrl = configuration.getProxyUrl();
    if (proxyUrl != null) {
        HttpHost proxyHost = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort(), proxyUrl.getProtocol());
        builder.setProxy(proxyHost);
        String proxyUsername = configuration.getProxyUsername();
        String proxyPassword = configuration.getProxyPassword();
        if (proxyUsername != null && !proxyUsername.isEmpty() && proxyPassword != null && !proxyPassword.isEmpty()) {
            provider.setCredentials(new AuthScope(proxyHost.getHostName(), proxyHost.getPort()), new UsernamePasswordCredentials(proxyUsername, proxyPassword));
        }
    }
    context = HttpClientContext.create();
    context.setCredentialsProvider(provider);
    context.setAuthCache(authCache);
    if (configuration.isInsecure()) {
        try {
            SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
            sslContextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
            SSLContext sslContext = sslContextBuilder.build();
            SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
            builder.setSSLSocketFactory(sslConnectionSocketFactory);
        } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
            throw new ZulipClientException(e);
        }
    }
    this.client = builder.build();
}
Also used : ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) URL(java.net.URL) KeyManagementException(java.security.KeyManagementException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy)

Example 12 with ZulipClientException

use of com.github.jamesnetherton.zulip.client.exception.ZulipClientException in project zulip-java-client by jamesnetherton.

the class ZulipCommonsHttpClientTest method invalidRateLimitReset.

@Test
public void invalidRateLimitReset() throws Exception {
    server.stubFor(request("GET", urlPathEqualTo("/api/v1/")).willReturn(aResponse().withStatus(429).withHeader("x-ratelimit-reset", "").withBody((String) null)));
    URL zulipUrl = new URL(server.baseUrl());
    ZulipConfiguration configuration = new ZulipConfiguration(zulipUrl, "test@test.com", "abc123");
    ZulipCommonsHttpClient client = new ZulipCommonsHttpClient(configuration);
    try {
        client.get("/", Collections.emptyMap(), ZulipApiResponse.class);
    } catch (ZulipClientException e) {
        ZulipRateLimitExceededException cause = (ZulipRateLimitExceededException) e.getCause();
        assertEquals(0, cause.getReteLimitReset());
    }
}
Also used : ZulipConfiguration(com.github.jamesnetherton.zulip.client.http.ZulipConfiguration) ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) ZulipRateLimitExceededException(com.github.jamesnetherton.zulip.client.exception.ZulipRateLimitExceededException) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Aggregations

ZulipClientException (com.github.jamesnetherton.zulip.client.exception.ZulipClientException)12 List (java.util.List)4 Test (org.junit.jupiter.api.Test)4 ZulipIntegrationTestBase (com.github.jamesnetherton.zulip.client.api.integration.ZulipIntegrationTestBase)3 Message (com.github.jamesnetherton.zulip.client.api.message.Message)3 Stream (com.github.jamesnetherton.zulip.client.api.stream.Stream)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 UUID (java.util.UUID)3 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 EventPoller (com.github.jamesnetherton.zulip.client.api.event.EventPoller)2 MessageEventListener (com.github.jamesnetherton.zulip.client.api.event.MessageEventListener)2 MessageService (com.github.jamesnetherton.zulip.client.api.message.MessageService)2 Narrow (com.github.jamesnetherton.zulip.client.api.narrow.Narrow)2 ProfileField (com.github.jamesnetherton.zulip.client.api.server.ProfileField)2 StreamService (com.github.jamesnetherton.zulip.client.api.stream.StreamService)2 StreamSubscriptionRequest (com.github.jamesnetherton.zulip.client.api.stream.StreamSubscriptionRequest)2 ZulipRateLimitExceededException (com.github.jamesnetherton.zulip.client.exception.ZulipRateLimitExceededException)2 File (java.io.File)2