use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project mollyim-android by mollyim.
the class PushServiceSocket method makeCallingRequest.
public CallingResponse makeCallingRequest(long requestId, String url, String httpMethod, List<Pair<String, String>> headers, byte[] body) {
ConnectionHolder connectionHolder = getRandom(serviceClients, random);
OkHttpClient okHttpClient = connectionHolder.getClient().newBuilder().followRedirects(false).connectTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).readTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).build();
RequestBody requestBody = body != null ? RequestBody.create(null, body) : null;
Request.Builder builder = new Request.Builder().url(url).method(httpMethod, requestBody);
if (headers != null) {
for (Pair<String, String> header : headers) {
builder.addHeader(header.first(), header.second());
}
}
Request request = builder.build();
for (int i = 0; i < MAX_FOLLOW_UPS; i++) {
try (Response response = okHttpClient.newCall(request).execute()) {
int responseStatus = response.code();
if (responseStatus != 307) {
return new CallingResponse.Success(requestId, responseStatus, response.body() != null ? response.body().bytes() : new byte[0]);
}
String location = response.header("Location");
HttpUrl newUrl = location != null ? request.url().resolve(location) : null;
if (newUrl != null) {
request = request.newBuilder().url(newUrl).build();
} else {
return new CallingResponse.Error(requestId, new IOException("Received redirect without a valid Location header"));
}
} catch (IOException e) {
Log.w(TAG, "Exception during ringrtc http call.", e);
return new CallingResponse.Error(requestId, e);
}
}
Log.w(TAG, "Calling request max redirects exceeded");
return new CallingResponse.Error(requestId, new IOException("Redirect limit exceeded"));
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project mollyim-android by mollyim.
the class PushServiceSocket method getServiceConnection.
private Response getServiceConnection(String urlFragment, String method, RequestBody body, Map<String, String> headers, Optional<UnidentifiedAccess> unidentifiedAccess, boolean doNotAddAuthenticationOrUnidentifiedAccessKey) throws PushNetworkException {
try {
OkHttpClient okHttpClient = buildOkHttpClient(unidentifiedAccess.isPresent());
Call call = okHttpClient.newCall(buildServiceRequest(urlFragment, method, body, headers, unidentifiedAccess, doNotAddAuthenticationOrUnidentifiedAccessKey));
synchronized (connections) {
connections.add(call);
}
try {
return call.execute();
} finally {
synchronized (connections) {
connections.remove(call);
}
}
} catch (IOException e) {
throw new PushNetworkException(e);
}
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project mollyim-android by mollyim.
the class EmojiSearchIndexDownloadJob method onRun.
@Override
protected void onRun() throws Exception {
OkHttpClient client = ApplicationDependencies.getOkHttpClient();
Manifest manifest = downloadManifest(client);
Locale locale = DynamicLanguageContextWrapper.getUsersSelectedLocale(context);
String remoteLanguage = findMatchingLanguage(locale, manifest.getLanguages());
if (manifest.getVersion() == SignalStore.emojiValues().getSearchVersion() && remoteLanguage.equals(SignalStore.emojiValues().getSearchLanguage())) {
Log.i(TAG, "Already using the latest version of " + manifest.getVersion() + " with the correct language " + remoteLanguage);
SignalStore.emojiValues().setLastSearchIndexCheck(System.currentTimeMillis());
return;
}
Log.i(TAG, "Need to get a new search index. Downloading version: " + manifest.getVersion() + ", language: " + remoteLanguage);
List<EmojiSearchData> searchIndex = downloadSearchIndex(client, manifest.getVersion(), remoteLanguage);
SignalDatabase.emojiSearch().setSearchIndex(searchIndex);
SignalStore.emojiValues().onSearchIndexUpdated(manifest.getVersion(), remoteLanguage);
SignalStore.emojiValues().setLastSearchIndexCheck(System.currentTimeMillis());
Log.i(TAG, "Success! Now at version: " + manifest.getVersion() + ", language: " + remoteLanguage);
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project ouidb-to-json-publisher by jfisbein.
the class OUIDBDownloader method download.
public Reader download() {
Cache cache = new Cache(new File(System.getProperty("java.io.tmpdir")), TEN_MB);
OkHttpClient client = new OkHttpClient.Builder().cache(cache).build();
Reader result = null;
Iterator<String> urlIterator = Arrays.asList(ouiDbUrls).iterator();
while (result == null && urlIterator.hasNext()) {
String ouiDbUrl = urlIterator.next();
Request request = new Request.Builder().url(ouiDbUrl).build();
log.info("Trying to download info from {}", ouiDbUrl);
try {
Response response = client.newCall(request).execute();
if (response.body() != null) {
String contentTypeHeader = getContentTypeHeader(response).orElse("text/plain; charset=utf-8");
if (contentTypeHeader.equalsIgnoreCase("application/x-gzip")) {
result = new InputStreamReader(new GZIPInputStream(response.body().byteStream()));
} else if (contentTypeHeader.equalsIgnoreCase("application/x-bzip2")) {
result = new InputStreamReader(new BZip2CompressorInputStream(response.body().byteStream()));
} else {
result = response.body().charStream();
}
}
} catch (IOException e) {
log.warn("Error downloading OUIs from {} - {}: {}", ouiDbUrl, e.getClass().getSimpleName(), e.getMessage());
}
}
if (result == null) {
throw new NoRecordsFoundException("No records found");
}
return result;
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project che-server by eclipse-che.
the class OpenShiftClientFactory method buildKubernetesInterceptor.
@Override
protected Interceptor buildKubernetesInterceptor(Config config) {
final String oauthToken;
if (Utils.isNotNullOrEmpty(config.getUsername()) && Utils.isNotNullOrEmpty(config.getPassword())) {
synchronized (getHttpClient()) {
try {
OkHttpClient.Builder builder = getHttpClient().newBuilder();
builder.interceptors().clear();
OkHttpClient clone = builder.build();
String credential = Credentials.basic(config.getUsername(), config.getPassword());
URL url = new URL(URLUtils.join(config.getMasterUrl(), AUTHORIZE_PATH));
Response response = clone.newCall(new Request.Builder().get().url(url).header(AUTHORIZATION, credential).build()).execute();
// False positive warn: according to javadocs response.body() returns non-null value
// if called after Call.execute()
response.body().close();
response = response.priorResponse() != null ? response.priorResponse() : response;
response = response.networkResponse() != null ? response.networkResponse() : response;
String token = response.header(LOCATION);
if (token == null || token.isEmpty()) {
throw new KubernetesClientException("Unexpected response (" + response.code() + " " + response.message() + "), to the authorization request. Missing header:[" + LOCATION + "]!");
}
token = token.substring(token.indexOf(BEFORE_TOKEN) + BEFORE_TOKEN.length());
token = token.substring(0, token.indexOf(AFTER_TOKEN));
oauthToken = token;
} catch (Exception e) {
throw KubernetesClientException.launderThrowable(e);
}
}
} else if (Utils.isNotNullOrEmpty(config.getOauthToken())) {
oauthToken = config.getOauthToken();
} else {
oauthToken = null;
}
return chain -> {
Request request = chain.request();
if (isNotNullOrEmpty(oauthToken)) {
Request authReq = chain.request().newBuilder().addHeader("Authorization", "Bearer " + oauthToken).build();
return chain.proceed(authReq);
}
return chain.proceed(request);
};
}
Aggregations