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