protected HttpRequestFactory buildHttpRequestFactory ( ) { HttpRequestFactory request = this . getGloboDns ( ) . getHttpTransport ( ) . createRequestFactory ( new HttpRequestInitializer ( ) { @ Override public void initialize ( HttpRequest request ) throws IOException { request . setNumberOfRetries ( AbstractAPI . this . globoDns . getNumberOfRetries ( ) ) ; request . setConnectTimeout ( AbstractAPI . this . globoDns . getConnectionTimeout ( ) ) ; request . setReadTimeout ( AbstractAPI . this . globoDns . getReadTimeout ( ) ) ; request . setThrowExceptionOnExecuteError ( false ) ; request . setParser ( parser ) ; request . setLoggingEnabled ( true ) ; request . getHeaders ( ) . setUserAgent ( "GloboDNS-Client" ) ; request . setCurlLoggingEnabled ( true ) ; request . setUnsuccessfulResponseHandler ( new HttpUnsuccessfulResponseHandler ( ) { @ Override public boolean handleResponse ( HttpRequest request , HttpResponse response , boolean supportsRetry ) throws IOException { if ( response . getStatusCode ( ) == 401 ) { getGloboDns ( ) . clearToken ( ) ; if ( supportsRetry ) { // only if supports retry I will prepare request with a new token, and ask to retry insertAuthenticationHeaders ( request ) ; return true ; } } return false ; } } ) ; request . setResponseInterceptor ( new HttpResponseInterceptor ( ) { @ Override public void interceptResponse ( HttpResponse response ) throws IOException { AbstractAPI . this . interceptResponse ( response ) ; } } ) ; interceptRequest ( request ) ; } } ) ; return request ; }
Customize HttpRequestFactory with authentication and error handling .