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();
}
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());
}
}
Aggregations