use of org.openclinica.ns.response.v31.Response 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 org.openclinica.ns.response.v31.Response 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 org.openclinica.ns.response.v31.Response in project okhttp by square.
the class HttpOverHttp2Test method receiveResponseCookies.
@Test
public void receiveResponseCookies() throws Exception {
RecordingCookieJar cookieJar = new RecordingCookieJar();
client = client.newBuilder().cookieJar(cookieJar).build();
server.enqueue(new MockResponse().addHeader("set-cookie: a=b"));
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
assertEquals("", response.body().string());
cookieJar.assertResponseCookies("a=b; path=/");
}
use of org.openclinica.ns.response.v31.Response in project okhttp by square.
the class HttpOverHttp2Test method connectionTimeout.
@Test
public void connectionTimeout() throws Exception {
server.enqueue(new MockResponse().setBody("A").setBodyDelay(1, SECONDS));
OkHttpClient client1 = client.newBuilder().readTimeout(2000, MILLISECONDS).build();
Call call1 = client1.newCall(new Request.Builder().url(server.url("/")).build());
OkHttpClient client2 = client.newBuilder().readTimeout(200, MILLISECONDS).build();
Call call2 = client2.newCall(new Request.Builder().url(server.url("/")).build());
Response response1 = call1.execute();
assertEquals("A", response1.body().string());
try {
call2.execute();
fail();
} catch (IOException expected) {
}
// Confirm that the connection was reused.
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals(1, server.takeRequest().getSequenceNumber());
}
use of org.openclinica.ns.response.v31.Response in project okhttp by square.
the class HttpOverHttp2Test method redirect.
@Test
public void redirect() throws Exception {
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: /foo").setBody("This page has moved!"));
server.enqueue(new MockResponse().setBody("This is the new location!"));
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
assertEquals("This is the new location!", response.body().string());
RecordedRequest request1 = server.takeRequest();
assertEquals("/", request1.getPath());
RecordedRequest request2 = server.takeRequest();
assertEquals("/foo", request2.getPath());
}
Aggregations