public String execute ( String url , String urlParameters , String clientId , String secret , String method ) throws Exception { HttpURLConnection connection ; String credentials = clientId + ":" + secret ; String authorizationHeader = "Basic " + Base64 . getEncoder ( ) . encodeToString ( credentials . getBytes ( ) ) ; if ( "POST" . equalsIgnoreCase ( method ) ) { connection = ( HttpURLConnection ) new URL ( url ) . openConnection ( ) ; connection . setRequestMethod ( method ) ; connection . setRequestProperty ( "Content-Type" , "application/x-www-form-urlencoded" ) ; connection . setRequestProperty ( "Authorization" , authorizationHeader ) ; connection . setDoInput ( true ) ; connection . setDoOutput ( true ) ; if ( null != urlParameters ) { try ( PrintWriter out = new PrintWriter ( connection . getOutputStream ( ) ) ) { out . print ( urlParameters ) ; } } } else { connection = ( HttpURLConnection ) new URL ( url + "?" + urlParameters ) . openConnection ( ) ; connection . setRequestMethod ( method ) ; connection . setRequestProperty ( "Authorization" , authorizationHeader ) ; } int timeout = Integer . parseInt ( System . getProperty ( "org.hawkular.accounts.http.timeout" , "5000" ) ) ; connection . setConnectTimeout ( timeout ) ; connection . setReadTimeout ( timeout ) ; StringBuilder response = new StringBuilder ( ) ; int statusCode ; try { statusCode = connection . getResponseCode ( ) ; } catch ( SocketTimeoutException timeoutException ) { throw new UsernamePasswordConversionException ( "Timed out when trying to contact the Keycloak server." ) ; } InputStream inputStream ; if ( statusCode < 300 ) { inputStream = connection . getInputStream ( ) ; } else { inputStream = connection . getErrorStream ( ) ; } try ( BufferedReader reader = new BufferedReader ( new InputStreamReader ( inputStream , "UTF-8" ) ) ) { for ( String line ; ( line = reader . readLine ( ) ) != null ; ) { response . append ( line ) ; } } return response . toString ( ) ; }
Performs an HTTP call to the Keycloak server returning the server s response as String .