Search in sources :

Example 36 with Dns

use of okhttp3.Dns in project zipkin by openzipkin.

the class PseudoAddressRecordSet method create.

static Dns create(List<String> urls, Dns actualDns) {
    Set<String> schemes = new LinkedHashSet<>();
    Set<String> hosts = new LinkedHashSet<>();
    Set<InetAddress> ipAddresses = new LinkedHashSet<>();
    Set<Integer> ports = new LinkedHashSet<>();
    for (String url : urls) {
        HttpUrl httpUrl = HttpUrl.parse(url);
        schemes.add(httpUrl.scheme());
        // Kick out if we can't cheaply read the address
        byte[] addressBytes = null;
        try {
            addressBytes = ipStringToBytes(httpUrl.host());
        } catch (RuntimeException e) {
        }
        if (addressBytes != null) {
            try {
                ipAddresses.add(InetAddress.getByAddress(addressBytes));
            } catch (UnknownHostException e) {
                hosts.add(httpUrl.host());
            }
        } else {
            hosts.add(httpUrl.host());
        }
        ports.add(httpUrl.port());
    }
    checkArgument(ports.size() == 1, "Only one port supported with multiple hosts %s", urls);
    checkArgument(schemes.size() == 1 && schemes.iterator().next().equals("http"), "Only http supported with multiple hosts %s", urls);
    if (hosts.isEmpty())
        return new StaticDns(ipAddresses);
    return new ConcatenatingDns(ipAddresses, hosts, actualDns);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) UnknownHostException(java.net.UnknownHostException) HttpUrl(okhttp3.HttpUrl) InetAddress(java.net.InetAddress)

Example 37 with Dns

use of okhttp3.Dns in project zipkin by openzipkin.

the class PseudoAddressRecordSetTest method concatenatesMixedIpLengths.

@Test
public void concatenatesMixedIpLengths() throws UnknownHostException {
    Dns result = PseudoAddressRecordSet.create(asList("http://1.1.1.1:9200", "http://[2001:db8::c001]:9200"), underlying);
    assertThat(result).isInstanceOf(PseudoAddressRecordSet.StaticDns.class);
    assertThat(result.lookup("foo")).containsExactly(InetAddresses.forString("1.1.1.1"), InetAddresses.forString("2001:db8::c001"));
}
Also used : Dns(okhttp3.Dns) Test(org.junit.Test)

Example 38 with Dns

use of okhttp3.Dns in project zipkin by openzipkin.

the class ElasticsearchHttpStorage method http.

// hosts resolution might imply a network call, and we might make a new okhttp instance
@Memoized
HttpCall.Factory http() {
    List<String> hosts = hostsSupplier().get();
    if (hosts.isEmpty())
        throw new IllegalArgumentException("no hosts configured");
    OkHttpClient ok = hosts.size() == 1 ? client() : client().newBuilder().dns(PseudoAddressRecordSet.create(hosts, client().dns())).build();
    ok.dispatcher().setMaxRequests(maxRequests());
    ok.dispatcher().setMaxRequestsPerHost(maxRequests());
    return new HttpCall.Factory(ok, HttpUrl.parse(hosts.get(0)));
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Memoized(com.google.auto.value.extension.memoized.Memoized)

Example 39 with Dns

use of okhttp3.Dns in project azure-sdk-for-java by Azure.

the class EndpointsInner method validateCustomDomainWithServiceResponseAsync.

/**
     * Validates the custom domain mapping to ensure it maps to the correct CDN endpoint in DNS.
     *
     * @param resourceGroupName Name of the Resource group within the Azure subscription.
     * @param profileName Name of the CDN profile which is unique within the resource group.
     * @param endpointName Name of the endpoint under the profile which is unique globally.
     * @param hostName The host name of the custom domain. Must be a domain name.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the ValidateCustomDomainOutputInner object
     */
public Observable<ServiceResponse<ValidateCustomDomainOutputInner>> validateCustomDomainWithServiceResponseAsync(String resourceGroupName, String profileName, String endpointName, String hostName) {
    if (resourceGroupName == null) {
        throw new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null.");
    }
    if (profileName == null) {
        throw new IllegalArgumentException("Parameter profileName is required and cannot be null.");
    }
    if (endpointName == null) {
        throw new IllegalArgumentException("Parameter endpointName is required and cannot be null.");
    }
    if (this.client.subscriptionId() == null) {
        throw new IllegalArgumentException("Parameter this.client.subscriptionId() is required and cannot be null.");
    }
    if (this.client.apiVersion() == null) {
        throw new IllegalArgumentException("Parameter this.client.apiVersion() is required and cannot be null.");
    }
    if (hostName == null) {
        throw new IllegalArgumentException("Parameter hostName is required and cannot be null.");
    }
    ValidateCustomDomainInput customDomainProperties = new ValidateCustomDomainInput();
    customDomainProperties.withHostName(hostName);
    return service.validateCustomDomain(resourceGroupName, profileName, endpointName, this.client.subscriptionId(), this.client.apiVersion(), this.client.acceptLanguage(), customDomainProperties, this.client.userAgent()).flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<ValidateCustomDomainOutputInner>>>() {

        @Override
        public Observable<ServiceResponse<ValidateCustomDomainOutputInner>> call(Response<ResponseBody> response) {
            try {
                ServiceResponse<ValidateCustomDomainOutputInner> clientResponse = validateCustomDomainDelegate(response);
                return Observable.just(clientResponse);
            } catch (Throwable t) {
                return Observable.error(t);
            }
        }
    });
}
Also used : Response(retrofit2.Response) ServiceResponse(com.microsoft.rest.ServiceResponse) ServiceResponse(com.microsoft.rest.ServiceResponse) ValidateCustomDomainInput(com.microsoft.azure.management.cdn.ValidateCustomDomainInput) Observable(rx.Observable) ResponseBody(okhttp3.ResponseBody)

Example 40 with Dns

use of okhttp3.Dns in project Signal-Android by WhisperSystems.

the class SubmitDebugLogRepository method uploadContent.

@WorkerThread
@NonNull
private String uploadContent(@NonNull String contentType, @NonNull RequestBody requestBody) throws IOException {
    try {
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new StandardUserAgentInterceptor()).dns(SignalServiceNetworkAccess.DNS).build();
        Response response = client.newCall(new Request.Builder().url(API_ENDPOINT).get().build()).execute();
        ResponseBody body = response.body();
        if (!response.isSuccessful() || body == null) {
            throw new IOException("Unsuccessful response: " + response);
        }
        JSONObject json = new JSONObject(body.string());
        String url = json.getString("url");
        JSONObject fields = json.getJSONObject("fields");
        String item = fields.getString("key");
        MultipartBody.Builder post = new MultipartBody.Builder();
        Iterator<String> keys = fields.keys();
        post.addFormDataPart("Content-Type", contentType);
        while (keys.hasNext()) {
            String key = keys.next();
            post.addFormDataPart(key, fields.getString(key));
        }
        post.addFormDataPart("file", "file", requestBody);
        Response postResponse = client.newCall(new Request.Builder().url(url).post(post.build()).build()).execute();
        if (!postResponse.isSuccessful()) {
            throw new IOException("Bad response: " + postResponse);
        }
        return API_ENDPOINT + "/" + item;
    } catch (JSONException e) {
        Log.w(TAG, "Error during upload.", e);
        throw new IOException(e);
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) JSONException(org.json.JSONException) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) JSONObject(org.json.JSONObject) StandardUserAgentInterceptor(org.thoughtcrime.securesms.net.StandardUserAgentInterceptor) MultipartBody(okhttp3.MultipartBody) WorkerThread(androidx.annotation.WorkerThread) NonNull(androidx.annotation.NonNull)

Aggregations

Test (org.junit.Test)23 MockResponse (okhttp3.mockwebserver.MockResponse)13 OkHttpClient (okhttp3.OkHttpClient)12 DoubleInetAddressDns (okhttp3.internal.DoubleInetAddressDns)10 Test (org.junit.jupiter.api.Test)9 IOException (java.io.IOException)8 Request (okhttp3.Request)8 SingleInetAddressDns (okhttp3.internal.SingleInetAddressDns)8 InetAddress (java.net.InetAddress)7 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)7 MockResponse (mockwebserver3.MockResponse)6 Response (okhttp3.Response)6 HttpURLConnection (java.net.HttpURLConnection)5 InetSocketAddress (java.net.InetSocketAddress)5 UnknownHostException (java.net.UnknownHostException)5 List (java.util.List)4 X509TrustManager (javax.net.ssl.X509TrustManager)4 Dns (okhttp3.Dns)4 Interceptor (okhttp3.Interceptor)4 HandshakeCertificates (okhttp3.tls.HandshakeCertificates)4