public boolean connect ( ) throws RestApiConnectionException { if ( this . autoEstablishAuthToken ) { if ( this . authTokenTimeout <= 0 ) { return false ; } else if ( ( System . currentTimeMillis ( ) - this . createdAt ) > this . authTokenTimeout ) { return false ; } } HttpEntity httpEntity = null ; try { httpEntity = getAuthentication ( ) ; if ( httpEntity != null ) { JsonElement json = GSON . fromJson ( EntityUtils . toString ( httpEntity ) , JsonObject . class ) ; if ( json == null ) { log . error ( "Http entity: " + httpEntity ) ; log . error ( "entity class: " + httpEntity . getClass ( ) . getName ( ) ) ; log . error ( "entity string size: " + EntityUtils . toString ( httpEntity ) . length ( ) ) ; log . error ( "content length: " + httpEntity . getContentLength ( ) ) ; log . error ( "content: " + IOUtils . toString ( httpEntity . getContent ( ) , Charsets . UTF_8 ) ) ; throw new RestApiConnectionException ( "JSON is NULL ! Failed on authentication with the following HTTP response received:\n" + EntityUtils . toString ( httpEntity ) ) ; } JsonObject jsonRet = json . getAsJsonObject ( ) ; log . info ( "jsonRet: " + jsonRet . toString ( ) ) ; parseAuthenticationResponse ( jsonRet ) ; } } catch ( IOException e ) { throw new RestApiConnectionException ( "Failed to get rest api connection; error - " + e . getMessage ( ) , e ) ; } finally { if ( httpEntity != null ) { try { EntityUtils . consume ( httpEntity ) ; } catch ( IOException e ) { throw new RestApiConnectionException ( "Failed to consume httpEntity; error - " + e . getMessage ( ) , e ) ; } } } return true ; }
get http connection