/** * Returns the {@link CacheResponse} from the delegate by converting the OkHttp {@link Request} * into the arguments required by the {@link ResponseCache}. */ private CacheResponse getJavaCachedResponse(Request request) throws IOException { Map<String, List<String>> headers = JavaApiConverter.extractJavaHeaders(request); return delegate.get(request.url().uri(), request.method(), headers); } }
@Override public CacheRequest put(Response response) throws IOException { URI uri = response.request().url().uri(); HttpURLConnection connection = JavaApiConverter.createJavaUrlConnectionForCachePut(response); final java.net.CacheRequest request = delegate.put(uri, connection); if (request == null) { return null; } return new CacheRequest() { @Override public Sink body() throws IOException { OutputStream body = request.getBody(); return body != null ? Okio.sink(body) : null; } @Override public void abort() { request.abort(); } }; }
@Override public List<Cookie> loadForRequest(HttpUrl url) { // The RI passes all headers. We don't have 'em, so we don't pass 'em! Map<String, List<String>> headers = Collections.emptyMap(); Map<String, List<String>> cookieHeaders; try { cookieHeaders = cookieHandler.get(url.uri(), headers); } catch (IOException e) { Platform.get().log(WARN, "Loading cookies failed for " + url.resolve("/..."), e); return Collections.emptyList(); } List<Cookie> cookies = null; for (Map.Entry<String, List<String>> entry : cookieHeaders.entrySet()) { String key = entry.getKey(); if (("Cookie".equalsIgnoreCase(key) || "Cookie2".equalsIgnoreCase(key)) && !entry.getValue().isEmpty()) { for (String header : entry.getValue()) { if (cookies == null) cookies = new ArrayList<>(); cookies.addAll(decodeHeaderAsJavaNetCookies(url, header)); } } } return cookies != null ? Collections.unmodifiableList(cookies) : Collections.emptyList(); }
/** Prepares the proxy servers to try. */ private void resetNextProxy(HttpUrl url, Proxy proxy) { if (proxy != null) { // If the user specifies a proxy, try that and only that. proxies = Collections.singletonList(proxy); } else { // Try each of the ProxySelector choices until one connection succeeds. List<Proxy> proxiesOrNull = address.proxySelector().select(url.uri()); proxies = proxiesOrNull != null && !proxiesOrNull.isEmpty() ? Util.immutableList(proxiesOrNull) : Util.immutableList(Proxy.NO_PROXY); } nextProxyIndex = 0; }
@Override public void saveFromResponse(HttpUrl url, List<Cookie> cookies) { if (cookieHandler != null) { List<String> cookieStrings = new ArrayList<>(); for (Cookie cookie : cookies) { cookieStrings.add(cookie.toString(true)); } Map<String, List<String>> multimap = Collections.singletonMap("Set-Cookie", cookieStrings); try { cookieHandler.put(url.uri(), multimap); } catch (IOException e) { Platform.get().log(WARN, "Saving cookies failed for " + url.resolve("/..."), e); } } }
private Connection createConnection() throws SQLException { String url = format("jdbc:presto://%s", server.url("/").uri().getAuthority()); return DriverManager.getConnection(url, "test", null); }
/** Prepares the proxy servers to try. */ private void resetNextProxy(HttpUrl url, Proxy proxy) { if (proxy != null) { // If the user specifies a proxy, try that and only that. proxies = Collections.singletonList(proxy); } else { // Try each of the ProxySelector choices until one connection succeeds. List<Proxy> proxiesOrNull = address.proxySelector().select(url.uri()); proxies = proxiesOrNull != null && !proxiesOrNull.isEmpty() ? Util.immutableList(proxiesOrNull) : Util.immutableList(Proxy.NO_PROXY); } nextProxyIndex = 0; }
private String newQueryResults(Integer partialCancelId, Integer nextUriId, List<Column> responseColumns, List<List<Object>> data, String state) { String queryId = "20160128_214710_00012_rk68b"; QueryResults queryResults = new QueryResults( queryId, server.url("/query.html?" + queryId).uri(), partialCancelId == null ? null : server.url(format("/v1/stage/%s.%s", queryId, partialCancelId)).uri(), nextUriId == null ? null : server.url(format("/v1/statement/%s/%s", queryId, nextUriId)).uri(), responseColumns, data, new StatementStats(state, state.equals("QUEUED"), true, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null), null, ImmutableList.of(), null, null); return QUERY_RESULTS_CODEC.toJson(queryResults); }
/** * Clients should invoke this method when they encounter a connectivity failure on a connection * returned by this route selector. */ public void connectFailed(Route failedRoute, IOException failure) { if (failedRoute.proxy().type() != Proxy.Type.DIRECT && address.proxySelector() != null) { // Tell the proxy selector when we fail to connect on a fresh connection. address.proxySelector().connectFailed( address.url().uri(), failedRoute.proxy().address(), failure); } routeDatabase.failed(failedRoute); }
private String createResults() { QueryResults queryResults = new QueryResults( "20160128_214710_00012_rk68b", server.url("/query.html?20160128_214710_00012_rk68b").uri(), null, null, ImmutableList.of(new Column("_col0", BigintType.BIGINT)), ImmutableList.of(ImmutableList.of(123)), StatementStats.builder().setState("FINISHED").build(), //new StatementStats("FINISHED", false, true, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null), null, ImmutableList.of(), null, null); return QUERY_RESULTS_CODEC.toJson(queryResults); }
@Test public void testGetServerInfo() throws Exception { ServerInfo expected = new ServerInfo(UNKNOWN, "test", true, false, Optional.of(Duration.valueOf("2m"))); server.enqueue(new MockResponse() .addHeader(CONTENT_TYPE, "application/json") .setBody(SERVER_INFO_CODEC.toJson(expected))); QueryExecutor executor = new QueryExecutor(new OkHttpClient()); ServerInfo actual = executor.getServerInfo(server.url("/v1/info").uri()); assertEquals(actual.getEnvironment(), "test"); assertEquals(actual.getUptime(), Optional.of(Duration.valueOf("2m"))); assertEquals(server.getRequestCount(), 1); assertEquals(server.takeRequest().getPath(), "/v1/info"); } }
/** * Clients should invoke this method when they encounter a connectivity failure on a connection * returned by this route selector. */ public void connectFailed(Route failedRoute, IOException failure) { if (failedRoute.proxy().type() != Proxy.Type.DIRECT && address.proxySelector() != null) { // Tell the proxy selector when we fail to connect on a fresh connection. address.proxySelector().connectFailed( address.url().uri(), failedRoute.proxy().address(), failure); } routeDatabase.failed(failedRoute); }
@Before public void setup() throws Exception { this.server = new MockWebServer(); this.server.start(); this.clientRegistration = TestClientRegistrations .clientCredentials() .tokenUri(this.server.url("/oauth2/token").uri().toASCIIString()); }
private static HttpResponse doCall(ArmeriaCallFactory callFactory, Request request) { final HttpUrl httpUrl = request.url(); final URI uri = httpUrl.uri(); final HttpClient httpClient = callFactory.getHttpClient(uri.getAuthority(), uri.getScheme()); final String uriString;
server.url("/").uri(), "user", "source",
public void receiveHeaders(Headers headers) throws IOException { CookieHandler cookieHandler = client.getCookieHandler(); if (cookieHandler != null) { cookieHandler.put(userRequest.url().uri(), OkHeaders.toMultimap(headers, null)); } }
public void receiveHeaders(Headers headers) throws IOException { CookieHandler cookieHandler = client.getCookieHandler(); if (cookieHandler != null) { cookieHandler.put(userRequest.url().uri(), OkHeaders.toMultimap(headers, null)); } }
/** * Return the request url including query parameters. * * @return the string */ private String toUrl() { final HttpUrl.Builder builder = httpUrl.newBuilder(); for (final NameValue param : queryParams) { builder.addQueryParameter(param.getName(), param.getValue()); } return builder.build().uri().toString(); }
/** * Return the request url including query parameters. * * @return the string */ private String toUrl() { final HttpUrl.Builder builder = httpUrl.newBuilder(); for (final NameValue param : queryParams) { builder.addQueryParameter(param.getName(), param.getValue()); } return builder.build().uri().toString(); }
private Connection createConnection() throws SQLException { String url = format("jdbc:presto://%s", server.url("/").uri().getAuthority()); return DriverManager.getConnection(url, "test", null); }