Source:
private String getAccessToken(String scopeList) { if (consumerKey == null) { makeDCRRequest(); } URL url; HttpsURLConnection urlConn = null; try { url = new URL("https://localhost:9443/oauth2/token"); urlConn = (HttpsURLConnection) url.openConnection(); urlConn.setDoOutput(true); urlConn.setRequestMethod("POST"); urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); String clientEncoded = Base64.getEncoder().encodeToString( (consumerKey + ':' + consumerSecret).getBytes(StandardCharsets.UTF_8)); urlConn.setRequestProperty("Authorization", "Basic " + clientEncoded); String postParams = "grant_type=client_credentials"; if (!scopeList.isEmpty()) { postParams += "&scope=" + scopeList; } urlConn.setHostnameVerifier((s, sslSession) -> true); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[]{trustAll}, new SecureRandom()); urlConn.setSSLSocketFactory(sslContext.getSocketFactory()); urlConn.getOutputStream().write((postParams).getBytes("UTF-8")); int responseCode = urlConn.getResponseCode(); if (responseCode == 200) { String responseStr = getResponseString(urlConn.getInputStream()); JsonParser parser = new JsonParser(); JsonObject obj = parser.parse(responseStr).getAsJsonObject(); return obj.get("access_token").getAsString(); } else { throw new RuntimeException("Error occurred while getting token. Status code: " + responseCode); } } catch (Exception e) { String msg = "Error while creating the new token for token regeneration."; throw new RuntimeException(msg, e); } finally { if (urlConn != null) { urlConn.disconnect(); } } }

Target:
private String getAccessToken(String scopeList) { if (consumerKey == null) { makeDCRRequest(); } URL url; HttpsURLConnection urlConn = null; try { url = new URL("https://localhost:9443/oauth2/token"); urlConn = (HttpsURLConnection) url.openConnection(); urlConn.setDoOutput(true); urlConn.setRequestMethod("POST"); urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); String clientEncoded = Base64.getEncoder().encodeToString( (consumerKey + ':' + consumerSecret).getBytes(StandardCharsets.UTF_8)); urlConn.setRequestProperty("Authorization", "Basic " + clientEncoded); String postParams = "grant_type=client_credentials"; if (!scopeList.isEmpty()) { postParams += "&scope=" + scopeList; } urlConn.setHostnameVerifier((s, sslSession) -> true); SSLContext sslContext = SSLContext.getInstance(TLS_PROTOCOL); sslContext.init(null, new TrustManager[]{trustAll}, new SecureRandom()); urlConn.setSSLSocketFactory(sslContext.getSocketFactory()); urlConn.getOutputStream().write((postParams).getBytes("UTF-8")); int responseCode = urlConn.getResponseCode(); if (responseCode == 200) { String responseStr = getResponseString(urlConn.getInputStream()); JsonParser parser = new JsonParser(); JsonObject obj = parser.parse(responseStr).getAsJsonObject(); return obj.get("access_token").getAsString(); } else { throw new RuntimeException("Error occurred while getting token. Status code: " + responseCode); } } catch (Exception e) { String msg = "Error while creating the new token for token regeneration."; throw new RuntimeException(msg, e); } finally { if (urlConn != null) { urlConn.disconnect(); } } }