Search in sources :

Example 61 with Builder

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

the class BatchCredentialsInterceptor method signHeader.

private Request signHeader(Request request) throws IOException {
    Request.Builder builder = request.newBuilder();
    // Set Headers
    if (request.headers().get("ocp-date") == null) {
        DateTimeRfc1123 rfcDate = new DateTimeRfc1123(new DateTime());
        builder.header("ocp-date", rfcDate.toString());
        request = builder.build();
    }
    String signature = request.method() + "\n";
    signature = signature + headerValue(request, "Content-Encoding") + "\n";
    signature = signature + headerValue(request, "Content-Language") + "\n";
    // Special handle content length
    long length = -1;
    if (request.body() != null) {
        length = request.body().contentLength();
    }
    signature = signature + (length >= 0 ? Long.valueOf(length) : "") + "\n";
    signature = signature + headerValue(request, "Content-MD5") + "\n";
    // Special handle content type header
    String contentType = request.header("Content-Type");
    if (contentType == null) {
        contentType = "";
        if (request.body() != null) {
            MediaType mediaType = request.body().contentType();
            if (mediaType != null) {
                contentType = mediaType.toString();
            }
        }
    }
    signature = signature + contentType + "\n";
    signature = signature + headerValue(request, "Date") + "\n";
    signature = signature + headerValue(request, "If-Modified-Since") + "\n";
    signature = signature + headerValue(request, "If-Match") + "\n";
    signature = signature + headerValue(request, "If-None-Match") + "\n";
    signature = signature + headerValue(request, "If-Unmodified-Since") + "\n";
    signature = signature + headerValue(request, "Range") + "\n";
    ArrayList<String> customHeaders = new ArrayList<String>();
    for (String name : request.headers().names()) {
        if (name.toLowerCase().startsWith("ocp-")) {
            customHeaders.add(name.toLowerCase());
        }
    }
    Collections.sort(customHeaders);
    for (String canonicalHeader : customHeaders) {
        String value = request.header(canonicalHeader);
        value = value.replace('\n', ' ').replace('\r', ' ').replaceAll("^[ ]+", "");
        signature = signature + canonicalHeader + ":" + value + "\n";
    }
    signature = signature + "/" + credentials.accountName().toLowerCase() + "/" + request.url().uri().getPath().replaceAll("^[/]+", "");
    // We temporary change client side auth code generator to bypass server
    // bug 4092533
    signature = signature.replace("%5C", "/").replace("%2F", "/");
    String query = request.url().query();
    if (query != null) {
        Map<String, String> queryComponents = new TreeMap<String, String>();
        String[] pairs = query.split("&");
        for (String pair : pairs) {
            int idx = pair.indexOf("=");
            String key = URLDecoder.decode(pair.substring(0, idx), "UTF-8").toLowerCase(Locale.US);
            queryComponents.put(key, key + ":" + URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
        }
        for (Map.Entry<String, String> entry : queryComponents.entrySet()) {
            signature = signature + "\n" + entry.getValue();
        }
    }
    String signedSignature = sign(credentials.keyValue(), signature);
    String authorization = "SharedKey " + credentials.accountName() + ":" + signedSignature;
    builder.header("Authorization", authorization);
    return builder.build();
}
Also used : Request(okhttp3.Request) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) DateTime(org.joda.time.DateTime) DateTimeRfc1123(com.microsoft.rest.DateTimeRfc1123) MediaType(okhttp3.MediaType) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 62 with Builder

use of okhttp3.OkHttpClient.Builder in project keywhiz by square.

the class ClientResourceTest method createClient_duplicate.

@Test
public void createClient_duplicate() throws Exception {
    CreateClientRequestV2 request = CreateClientRequestV2.builder().name("client2").build();
    // Initial request OK
    create(request);
    // Duplicate request fails
    Response httpResponse = create(request);
    assertThat(httpResponse.code()).isEqualTo(409);
}
Also used : Response(okhttp3.Response) CreateClientRequestV2(keywhiz.api.automation.v2.CreateClientRequestV2) Test(org.junit.Test)

Example 63 with Builder

use of okhttp3.OkHttpClient.Builder in project keywhiz by square.

the class ClientResourceTest method createClient_success.

@Test
public void createClient_success() throws Exception {
    Response httpResponse = create(CreateClientRequestV2.builder().name("client1").build());
    assertThat(httpResponse.code()).isEqualTo(201);
    URI location = URI.create(httpResponse.header(LOCATION));
    assertThat(location.getPath()).isEqualTo("/automation/v2/clients/client1");
}
Also used : Response(okhttp3.Response) URI(java.net.URI) Test(org.junit.Test)

Example 64 with Builder

use of okhttp3.OkHttpClient.Builder in project keywhiz by square.

the class GroupResourceTest method deleteGroup_success.

@Test
public void deleteGroup_success() throws Exception {
    create(CreateGroupRequestV2.builder().name("group6").build());
    Response httpResponse = delete("group6");
    assertThat(httpResponse.code()).isEqualTo(204);
}
Also used : Response(okhttp3.Response) Test(org.junit.Test)

Example 65 with Builder

use of okhttp3.OkHttpClient.Builder in project keywhiz by square.

the class GroupResourceTest method createGroup_duplicate.

@Test
public void createGroup_duplicate() throws Exception {
    CreateGroupRequestV2 request = CreateGroupRequestV2.builder().name("group2").build();
    // Initial request OK
    create(request);
    // Duplicate request fails
    Response httpResponse = create(request);
    assertThat(httpResponse.code()).isEqualTo(409);
}
Also used : Response(okhttp3.Response) CreateGroupRequestV2(keywhiz.api.automation.v2.CreateGroupRequestV2) Test(org.junit.Test)

Aggregations

Request (okhttp3.Request)47 Response (okhttp3.Response)44 OkHttpClient (okhttp3.OkHttpClient)31 IOException (java.io.IOException)28 RequestBody (okhttp3.RequestBody)27 Test (org.junit.Test)17 Interceptor (okhttp3.Interceptor)11 File (java.io.File)9 Provides (dagger.Provides)8 X509TrustManager (javax.net.ssl.X509TrustManager)8 Retrofit (retrofit2.Retrofit)8 URI (java.net.URI)7 Map (java.util.Map)7 MultipartBody (okhttp3.MultipartBody)7 Singleton (javax.inject.Singleton)6 HttpUrl (okhttp3.HttpUrl)6 ResponseBody (okhttp3.ResponseBody)6 SSLContext (javax.net.ssl.SSLContext)5 TrustManager (javax.net.ssl.TrustManager)5 TestClients.clientRequest (keywhiz.TestClients.clientRequest)5