use of com.amazonaws.services.s3.Headers in project MVPFrames by RockyQu.
the class LoggingInterceptor method intercept.
@Override
public Response intercept(Chain chain) throws IOException {
Level level = this.level;
Request request = chain.request();
if (level == Level.NONE) {
return chain.proceed(request);
}
RequestBody requestBody = request.body();
boolean hasRequestBody = requestBody != null;
// 请求地址
Connection connection = chain.connection();
Protocol protocol = connection != null ? connection.protocol() : Protocol.HTTP_1_1;
String requestStartMessage = "--> " + request.method() + ' ' + request.url() + ' ' + protocol;
if (hasRequestBody) {
requestStartMessage += " (" + requestBody.contentLength() + "-byte body)";
}
log(requestStartMessage);
// Content-Type
if (hasRequestBody) {
if (requestBody.contentType() != null) {
log("Content-Type: " + requestBody.contentType());
}
if (requestBody.contentLength() != -1) {
log("Content-Length: " + requestBody.contentLength());
}
}
// 拼装请求参数
Headers headers = request.headers();
for (int i = 0, count = headers.size(); i < count; i++) {
String name = headers.name(i);
if (!"Content-Type".equalsIgnoreCase(name) && !"Content-Length".equalsIgnoreCase(name)) {
log(name + ": " + headers.value(i));
}
}
// Request结束
if (!hasRequestBody) {
log("--> END " + request.method());
} else if (bodyEncoded(request.headers())) {
log("--> END " + request.method() + " (encoded body omitted)");
} else {
Buffer buffer = new Buffer();
requestBody.writeTo(buffer);
Charset charset = UTF8;
MediaType contentType = requestBody.contentType();
if (contentType != null) {
charset = contentType.charset(UTF8);
}
if (isPlaintext(buffer)) {
log(buffer.readString(charset));
log("--> END " + request.method() + " (" + requestBody.contentLength() + "-byte body)");
} else {
log("--> END " + request.method() + " (binary " + requestBody.contentLength() + "-byte body omitted)");
}
}
// Response开始
long startNs = System.nanoTime();
Response response;
try {
response = chain.proceed(request);
} catch (Exception e) {
log("<-- HTTP FAILED: " + e);
throw e;
}
long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
ResponseBody responseBody = response.body();
long contentLength = responseBody.contentLength();
String bodySize = contentLength != -1 ? contentLength + "-byte" : "unknown-length";
log("<-- " + response.code() + ' ' + response.message() + ' ' + response.request().url() + " (" + tookMs + "ms" + (", " + bodySize + " body") + ')');
headers = response.headers();
for (int i = 0, count = headers.size(); i < count; i++) {
log(headers.name(i) + ": " + headers.value(i));
}
if (!HttpHeaders.hasBody(response)) {
log("<-- END HTTP");
} else if (bodyEncoded(response.headers())) {
log("<-- END HTTP (encoded body omitted)");
} else {
BufferedSource source = responseBody.source();
// Buffer the entire body.
source.request(Long.MAX_VALUE);
Buffer buffer = source.buffer();
Charset charset = UTF8;
MediaType contentType = responseBody.contentType();
if (contentType != null) {
try {
charset = contentType.charset(UTF8);
} catch (UnsupportedCharsetException e) {
log("Couldn't decode the response body; charset is likely malformed.");
log("<-- END HTTP");
return response;
}
}
if (!isPlaintext(buffer)) {
log("<-- END HTTP (binary " + buffer.size() + "-byte body omitted)");
return response;
}
if (contentLength != 0) {
log(buffer.clone().readString(charset));
}
log("<-- END HTTP (" + buffer.size() + "-byte body)");
}
return response;
}
use of com.amazonaws.services.s3.Headers in project ProxerLibJava by proxer.
the class HeaderInterceptorTest method testCorrectHeadersForStreamServer.
@Test
public void testCorrectHeadersForStreamServer() throws IOException, InterruptedException {
server.enqueue(new MockResponse());
api.client().newCall(new Request.Builder().url("https://stream.proxer.me/test").build()).execute();
final Headers headers = server.takeRequest().getHeaders();
assertThat(headers.get("proxer-api-key")).isNull();
assertThat(headers.get("User-Agent")).startsWith("ProxerLibJava/");
}
use of com.amazonaws.services.s3.Headers in project ProxerLibJava by proxer.
the class HeaderInterceptorTest method testCorrectHeadersForCdn.
@Test
public void testCorrectHeadersForCdn() throws IOException, InterruptedException {
server.enqueue(new MockResponse());
api.client().newCall(new Request.Builder().url(ProxerUrls.cdnBase().newBuilder().addPathSegment("test").build()).build()).execute();
final Headers headers = server.takeRequest().getHeaders();
assertThat(headers.get("proxer-api-key")).isNull();
assertThat(headers.get("User-Agent")).startsWith("ProxerLibJava/");
}
use of com.amazonaws.services.s3.Headers in project dhis2-android-sdk by dhis2.
the class TrackedEntityCallUnitShould method invoke_insert_if_request_is_successful.
@Test
@SuppressWarnings("unchecked")
public void invoke_insert_if_request_is_successful() throws Exception {
Headers headers = new Headers.Builder().add("Date", lastUpdated.toString()).build();
when(payload.items()).thenReturn(Collections.singletonList(trackedEntity));
Response<Payload<TrackedEntity>> response = Response.success(payload, headers);
when(retrofitCall.execute()).thenReturn(response);
call.call();
verify(database, times(1)).beginNewTransaction();
verify(transaction, times(1)).setSuccessful();
verify(transaction, times(1)).end();
verify(trackedEntityStore, times(1)).insert(anyString(), anyString(), anyString(), anyString(), any(Date.class), any(Date.class), anyString(), anyString(), anyString(), anyString());
// TODO: after implementing the SystemInfoCall, tests..etc modify this to actually check the date:
// Right now it only checks if: (Date) null is an instance of Date.class, not a terribly useful:
verify(resourceStore, times(1)).insert(anyString(), any(Date.class));
}
use of com.amazonaws.services.s3.Headers in project autorest.java by Azure.
the class HeaderOperationsTests method responseDate.
@Test
public void responseDate() throws Exception {
lock = new CountDownLatch(1);
client.headers().responseDateWithServiceResponseAsync("valid").subscribe(new Action1<ServiceResponseWithHeaders<Void, HeaderResponseDateHeaders>>() {
@Override
public void call(ServiceResponseWithHeaders<Void, HeaderResponseDateHeaders> response) {
Headers headers = response.response().headers();
if (headers.get("value") != null) {
Assert.assertEquals("2010-01-01", headers.get("value"));
lock.countDown();
}
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
fail();
}
});
Assert.assertTrue(lock.await(1000, TimeUnit.MILLISECONDS));
lock = new CountDownLatch(1);
client.headers().responseDateWithServiceResponseAsync("min").subscribe(new Action1<ServiceResponseWithHeaders<Void, HeaderResponseDateHeaders>>() {
@Override
public void call(ServiceResponseWithHeaders<Void, HeaderResponseDateHeaders> response) {
Headers headers = response.response().headers();
if (headers.get("value") != null) {
Assert.assertEquals("0001-01-01", headers.get("value"));
lock.countDown();
}
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
fail();
}
});
Assert.assertTrue(lock.await(1000, TimeUnit.MILLISECONDS));
}
Aggregations