DefaultHttpRequestInitializer(GoogleCredential credential, TimeValue connectTimeout, TimeValue readTimeout) { this.credential = credential; this.connectTimeout = connectTimeout; this.readTimeout = readTimeout; this.handler = new HttpBackOffUnsuccessfulResponseHandler(newBackOff()); this.ioHandler = new HttpBackOffIOExceptionHandler(newBackOff()); }
private HttpRequestFactory createRequestFactory() { return new NetHttpTransport().createRequestFactory(request -> { request.setConnectTimeout(HttpClient.this.config.getConnectTimeout()); request.setReadTimeout(HttpClient.this.config.getReadTimeout()); request.setNumberOfRetries(HttpClient.this.config.getRetries()); request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff.Builder().build())); }); }
@Override public void initialize(HttpRequest httpRequest) throws IOException { httpRequest.setIOExceptionHandler( new HttpBackOffIOExceptionHandler(new ExponentialBackOff())); httpRequest.setUnsuccessfulResponseHandler( new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff())); }
@Override public void initialize(HttpRequest request) { // Credential must be the interceptor to fill in accessToken fields. request.setInterceptor(credential); // IOExceptions such as "socket timed out" of "insufficient bytes written" will follow a // straightforward backoff. HttpBackOffIOExceptionHandler exceptionHandler = new HttpBackOffIOExceptionHandler(new ExponentialBackOff()); if (sleeperOverride != null) { exceptionHandler.setSleeper(sleeperOverride); } // Supply a new composite handler for unsuccessful return codes. 401 Unauthorized will be // handled by the Credential, 410 Gone will be logged, and 5XX will be handled by a backoff // handler. LoggingResponseHandler loggingResponseHandler = new LoggingResponseHandler( new CredentialOrBackoffResponseHandler(), exceptionHandler, ImmutableSet.of(HttpStatus.SC_GONE, HttpStatus.SC_SERVICE_UNAVAILABLE)); request.setUnsuccessfulResponseHandler(loggingResponseHandler); request.setIOExceptionHandler(loggingResponseHandler); if (Strings.isNullOrEmpty(request.getHeaders().getUserAgent())) { LOG.debug("Request is missing a user-agent, adding default value of '{}'", defaultUserAgent); request.getHeaders().setUserAgent(defaultUserAgent); } }
@Override public void initialize(HttpRequest httpRequest) { final HttpUnsuccessfulResponseHandler backoffHandler = new HttpBackOffUnsuccessfulResponseHandler( new ExponentialBackOff.Builder() .setMaxElapsedTimeMillis(((int) maxWait.getMillis())) .build()) .setSleeper(sleeper); httpRequest.setInterceptor(wrappedCredential); httpRequest.setUnsuccessfulResponseHandler( new HttpUnsuccessfulResponseHandler() { int retry = 0; @Override public boolean handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry) throws IOException { if (wrappedCredential.handleResponse( request, response, supportsRetry)) { // If credential decides it can handle it, // the return code or message indicated // something specific to authentication, // and no backoff is desired. return true; } else if (backoffHandler.handleResponse( request, response, supportsRetry)) { // Otherwise, we defer to the judgement of // our internal backoff handler. logger.debug("Retrying [{}] times : [{}]", retry, request.getUrl()); return true; } else { return false; } } }); httpRequest.setIOExceptionHandler( new HttpBackOffIOExceptionHandler( new ExponentialBackOff.Builder() .setMaxElapsedTimeMillis(((int) maxWait.getMillis())) .build()) .setSleeper(sleeper) ); }
private HttpBackOffIOExceptionHandler getHttpBackOffIOExceptionHandler() { MockBackOff backOff = new MockBackOff(); backOff.setBackOffMillis(200); backOff.setMaxTries(5); return new HttpBackOffIOExceptionHandler(backOff); }
public static void main(String[] args) throws IOException { if (args.length != 3) { System.out.println("UploadDirs accountname folderid folder"); return; } String folderid = args[1]; String account = args[0]; String folder = args[2]; java.io.File jdrivedir = new java.io.File( new java.io.File(new java.io.File( System.getProperty("user.home")), ".gyingpan"), account); jdrivedir.mkdirs(); HttpTransport httpTransport = new NetHttpTransport(); JsonFactory jsonFactory = new JacksonFactory(); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( httpTransport, jsonFactory, GDrive.CLIENT_ID, GDrive.CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE)) .setAccessType("offline") .setApprovalPrompt("auto") .setDataStoreFactory( new FileDataStoreFactory(new java.io.File(jdrivedir, "driveauth"))).build(); Credential credential = flow.loadCredential(args[0]); Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).setHttpRequestInitializer( new HttpRequestInitializer() { @Override public void initialize(HttpRequest request) throws IOException { credential.initialize(request); request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler( new ExponentialBackOff())); request.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler( new ExponentialBackOff())); } }).build(); HashMap<String, File> gfilelist = getGDriveFileList(service, folderid); uploadDir(service, gfilelist, folderid, new java.io.File(folder).toPath()); }