use of okhttp3.ResponseBody in project okhttp by square.
the class JavaApiConverterTest method createJavaCacheResponse_httpsPost.
@Test
public void createJavaCacheResponse_httpsPost() throws Exception {
Request okRequest = createArbitraryOkRequest().newBuilder().url("https://secure/request").post(createRequestBody("RequestBody")).build();
ResponseBody responseBody = createResponseBody("ResponseBody");
Handshake handshake = Handshake.get(null, CipherSuite.TLS_RSA_WITH_NULL_MD5, Arrays.<Certificate>asList(SERVER_CERT), Arrays.<Certificate>asList(LOCAL_CERT));
Response okResponse = createArbitraryOkResponse(okRequest).newBuilder().protocol(Protocol.HTTP_1_1).code(200).message("Fantastic").addHeader("key1", "value1_1").addHeader("key2", "value2").addHeader("key1", "value1_2").body(responseBody).handshake(handshake).build();
SecureCacheResponse javaCacheResponse = (SecureCacheResponse) JavaApiConverter.createJavaCacheResponse(okResponse);
Map<String, List<String>> javaHeaders = javaCacheResponse.getHeaders();
assertEquals(Arrays.asList("value1_1", "value1_2"), javaHeaders.get("key1"));
assertEquals(Arrays.asList("HTTP/1.1 200 Fantastic"), javaHeaders.get(null));
assertEquals("ResponseBody", readAll(javaCacheResponse.getBody()));
assertEquals(handshake.cipherSuite().javaName(), javaCacheResponse.getCipherSuite());
assertEquals(handshake.localCertificates(), javaCacheResponse.getLocalCertificateChain());
assertEquals(handshake.peerCertificates(), javaCacheResponse.getServerCertificateChain());
assertEquals(handshake.localPrincipal(), javaCacheResponse.getLocalPrincipal());
assertEquals(handshake.peerPrincipal(), javaCacheResponse.getPeerPrincipal());
}
use of okhttp3.ResponseBody in project okhttp by square.
the class OkHttpAsync method prepare.
@Override
public void prepare(final Benchmark benchmark) {
concurrencyLevel = benchmark.concurrencyLevel;
targetBacklog = benchmark.targetBacklog;
client = new OkHttpClient.Builder().protocols(benchmark.protocols).dispatcher(new Dispatcher(new ThreadPoolExecutor(benchmark.concurrencyLevel, benchmark.concurrencyLevel, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()))).build();
if (benchmark.tls) {
SslClient sslClient = SslClient.localhost();
SSLSocketFactory socketFactory = sslClient.socketFactory;
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession session) {
return true;
}
};
client = client.newBuilder().sslSocketFactory(socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build();
}
callback = new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println("Failed: " + e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
ResponseBody body = response.body();
long total = SynchronousHttpClient.readAllAndClose(body.byteStream());
long finish = System.nanoTime();
if (VERBOSE) {
long start = (Long) response.request().tag();
System.out.printf("Transferred % 8d bytes in %4d ms%n", total, TimeUnit.NANOSECONDS.toMillis(finish - start));
}
requestsInFlight.decrementAndGet();
}
};
}
use of okhttp3.ResponseBody in project okhttp by square.
the class JavaApiConverter method createOkResponseForCacheGet.
/**
* Creates an OkHttp {@link Response} using the supplied {@link Request} and {@link CacheResponse}
* to supply the data.
*/
static Response createOkResponseForCacheGet(Request request, CacheResponse javaResponse) throws IOException {
// Build a cache request for the response to use.
Headers responseHeaders = createHeaders(javaResponse.getHeaders());
Headers varyHeaders;
if (HttpHeaders.hasVaryAll(responseHeaders)) {
// "*" means that this will be treated as uncacheable anyway.
varyHeaders = new Headers.Builder().build();
} else {
varyHeaders = HttpHeaders.varyHeaders(request.headers(), responseHeaders);
}
Request cacheRequest = new Request.Builder().url(request.url()).method(request.method(), null).headers(varyHeaders).build();
Response.Builder okResponseBuilder = new Response.Builder();
// Request: Use the cacheRequest we built.
okResponseBuilder.request(cacheRequest);
// Status line: Java has this as one of the headers.
StatusLine statusLine = StatusLine.parse(extractStatusLine(javaResponse));
okResponseBuilder.protocol(statusLine.protocol);
okResponseBuilder.code(statusLine.code);
okResponseBuilder.message(statusLine.message);
// Response headers
Headers okHeaders = extractOkHeaders(javaResponse, okResponseBuilder);
okResponseBuilder.headers(okHeaders);
// Response body
ResponseBody okBody = createOkBody(okHeaders, javaResponse);
okResponseBuilder.body(okBody);
// Handle SSL handshake information as needed.
if (javaResponse instanceof SecureCacheResponse) {
SecureCacheResponse javaSecureCacheResponse = (SecureCacheResponse) javaResponse;
// Handshake doesn't support null lists.
List<Certificate> peerCertificates;
try {
peerCertificates = javaSecureCacheResponse.getServerCertificateChain();
} catch (SSLPeerUnverifiedException e) {
peerCertificates = Collections.emptyList();
}
List<Certificate> localCertificates = javaSecureCacheResponse.getLocalCertificateChain();
if (localCertificates == null) {
localCertificates = Collections.emptyList();
}
String cipherSuiteString = javaSecureCacheResponse.getCipherSuite();
CipherSuite cipherSuite = CipherSuite.forJavaName(cipherSuiteString);
Handshake handshake = Handshake.get(null, cipherSuite, peerCertificates, localCertificates);
okResponseBuilder.handshake(handshake);
}
return okResponseBuilder.build();
}
use of okhttp3.ResponseBody in project okhttp by square.
the class JavaApiConverter method createJavaCacheResponse.
/**
* Creates a {@link java.net.CacheResponse} of the correct (sub)type using information gathered
* from the supplied {@link Response}.
*/
public static CacheResponse createJavaCacheResponse(final Response response) {
final Headers headers = withSyntheticHeaders(response);
final ResponseBody body = response.body();
if (response.request().isHttps()) {
final Handshake handshake = response.handshake();
return new SecureCacheResponse() {
@Override
public String getCipherSuite() {
return handshake != null ? handshake.cipherSuite().javaName() : null;
}
@Override
public List<Certificate> getLocalCertificateChain() {
if (handshake == null)
return null;
// Java requires null, not an empty list here.
List<Certificate> certificates = handshake.localCertificates();
return certificates.size() > 0 ? certificates : null;
}
@Override
public List<Certificate> getServerCertificateChain() throws SSLPeerUnverifiedException {
if (handshake == null)
return null;
// Java requires null, not an empty list here.
List<Certificate> certificates = handshake.peerCertificates();
return certificates.size() > 0 ? certificates : null;
}
@Override
public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
if (handshake == null)
return null;
return handshake.peerPrincipal();
}
@Override
public Principal getLocalPrincipal() {
if (handshake == null)
return null;
return handshake.localPrincipal();
}
@Override
public Map<String, List<String>> getHeaders() throws IOException {
// Java requires that the entry with a null key be the status line.
return JavaNetHeaders.toMultimap(headers, StatusLine.get(response).toString());
}
@Override
public InputStream getBody() throws IOException {
if (body == null)
return null;
return body.byteStream();
}
};
} else {
return new CacheResponse() {
@Override
public Map<String, List<String>> getHeaders() throws IOException {
// Java requires that the entry with a null key be the status line.
return JavaNetHeaders.toMultimap(headers, StatusLine.get(response).toString());
}
@Override
public InputStream getBody() throws IOException {
if (body == null)
return null;
return body.byteStream();
}
};
}
}
use of okhttp3.ResponseBody in project okhttp by square.
the class ThreadInterruptTest method interruptReadingResponseBody.
@Test
public void interruptReadingResponseBody() throws Exception {
// 2 MiB
int responseBodySize = 2 * 1024 * 1024;
server.enqueue(new MockResponse().setBody(new Buffer().write(new byte[responseBodySize])).throttleBody(64 * 1024, 125, // 500 Kbps
TimeUnit.MILLISECONDS));
server.start();
interruptLater(500);
HttpURLConnection connection = new OkUrlFactory(client).open(server.url("/").url());
InputStream responseBody = connection.getInputStream();
byte[] buffer = new byte[1024];
try {
while (responseBody.read(buffer) != -1) {
}
fail("Expected thread to be interrupted");
} catch (InterruptedIOException expected) {
}
responseBody.close();
}
Aggregations