@Override public void setTimeout(int connectTimeout, int readTimeout) { request.getFetchOptions().setDeadline( connectTimeout == 0 || readTimeout == 0 ? Double.MAX_VALUE : (connectTimeout + readTimeout) / 1000.0); }
@Override public void setTimeout(int connectTimeout, int readTimeout) { request.getFetchOptions().setDeadline(connectTimeout == 0 || readTimeout == 0 ? Double.MAX_VALUE : (connectTimeout + readTimeout) / 1000.0); }
/** * Returns a new instance of the default {@link FetchOptions} used by this requestor. This * method exists primarily since {@link FetchOptions} provides no copy methods. * * @return new instance of default fetch options used by this requestor. */ public static FetchOptions newDefaultOptions() { return FetchOptions.Builder.withDefaults() .validateCertificate() .doNotFollowRedirects() .disallowTruncate() // seconds .setDeadline((DEFAULT_CONNECT_TIMEOUT_MILLIS + DEFAULT_READ_TIMEOUT_MILLIS)/1000.0); }
@Override public Response fetchData(String url, Method method) { try { URL aUrl = new URL(url); HTTPMethod httpMethod = HTTPMethod.GET; switch (method) { case POST: httpMethod = HTTPMethod.POST; break; } HTTPRequest httpRequest = new HTTPRequest(aUrl, httpMethod, doNotValidateCertificate().setDeadline(10d)); URLFetchService service = URLFetchServiceFactory .getURLFetchService(); HTTPResponse response = service.fetch(httpRequest); return new Response(new String(response.getContent(), "UTF-8"), response.getResponseCode(), ""); } catch (MalformedURLException e) { return new Response("", 400, "Malformed URL: " + url); } catch (IOException e) { return new Response("", 500, e.getMessage()); } }
private FetchOptions createFetchOptions(HttpRequestImpl request) { FetchOptions fetchOptions = FetchOptions.Builder.withDefaults(); fetchOptions = fetchOptions.setDeadline((double) request.getTimeout() / (double) 1000); return fetchOptions = request.isFollowRedirects() ? fetchOptions.followRedirects() : fetchOptions.doNotFollowRedirects(); }
@VisibleForTesting HTTPRequest makeRequest(GcsFilename filename, @Nullable Map<String, String> queryStrings, HTTPMethod method, long timeoutMillis, byte[] payload) { HTTPRequest request = new HTTPRequest(makeUrl(filename, queryStrings), method, FetchOptions.Builder.disallowTruncate() .doNotFollowRedirects() .validateCertificate() .setDeadline(timeoutMillis / 1000.0)); for (HTTPHeader header : headers) { request.addHeader(header); } request.addHeader(USER_AGENT); if (payload != null && payload.length > 0) { request.setHeader(new HTTPHeader(CONTENT_LENGTH, String.valueOf(payload.length))); try { request.setHeader(new HTTPHeader(CONTENT_MD5, BaseEncoding.base64().encode(MessageDigest.getInstance("MD5").digest(payload)))); } catch (NoSuchAlgorithmException e) { log.severe( "Unable to get a MessageDigest instance, no Content-MD5 header sent.\n" + e.toString()); } request.setPayload(payload); } else { request.setHeader(ZERO_CONTENT_LENGTH); } return request; }
@Test public void testWithDeadline() throws Exception { FetchOptions options = buildFetchOptions(); options.setDeadline(10 * 1000.0); testOptions(options); }
if (utils.relaxHostname() || utils.trustAllCerts()) options.doNotFollowRedirects(); options.setDeadline(10.0);
if (utils.relaxHostname() || utils.trustAllCerts()) options.doNotFollowRedirects(); options.setDeadline(10.0);
@Test(expected = IOException.class) public void fetchNonExistentLocalAppThrowsException() throws Exception { URL selfURL = new URL("BOGUS-" + appUrlBase + "/"); FetchOptions fetchOptions = FetchOptions.Builder.withDefaults() .doNotFollowRedirects() .setDeadline(10.0); HTTPRequest httpRequest = new HTTPRequest(selfURL, HTTPMethod.GET, fetchOptions); URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService(); HTTPResponse httpResponse = urlFetchService.fetch(httpRequest); fail("expected exception, got " + httpResponse.getResponseCode()); }