Source:
private String getManagementRealm(String url) throws IOException { CloseableHttpClient defaultHttpClient = HttpClients.custom() .setConnectionManager(new PoolingHttpClientConnectionManager(registry)) .setDefaultRequestConfig(requestConfig) .build(); HttpPost post = new HttpPost(url); post.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType()); CloseableHttpResponse response = null; try { response = defaultHttpClient.execute(post); response.close(); } finally { defaultHttpClient.close(); } if (response.getStatusLine().getStatusCode() != HttpStatus.SC_UNAUTHORIZED) { throw new IllegalStateException(String.format("Authentication phase failed. Server responded %d instead of " + "%d. Isn't server authentication turned off while username and password set?", response.getStatusLine().getStatusCode(), HttpStatus.SC_UNAUTHORIZED)); } if (!response.containsHeader(HttpHeaders.WWW_AUTHENTICATE)) { throw new IllegalStateException("Authentication phase failed. Missing WWW-Authenticate header in server " + "response after first request and management realm needed for digest authentication is unable" + "to be obtained."); } for (HeaderElement el : response.getHeaders(HttpHeaders.WWW_AUTHENTICATE)[0].getElements()) { if (el.getName().equals("Digest realm")) { return el.getValue(); } } throw new IllegalStateException("Authentication phase failed. Digest realm not found in WWW-Authenticate " + "header."); }

Target:
private String getManagementRealm(String url) throws IOException { CloseableHttpClient defaultHttpClient = HttpClients.custom() .setConnectionManager(new PoolingHttpClientConnectionManager(registry)) .setDefaultRequestConfig(requestConfig) .build(); HttpPost post = new HttpPost(url); post.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType()); post.setEntity(new StringEntity("[]")); CloseableHttpResponse response = null; String content; try { response = defaultHttpClient.execute(post); content = EntityUtils.toString(response.getEntity()); } finally { if (response != null) { response.close(); } defaultHttpClient.close(); } if (response.getStatusLine().getStatusCode() == HttpStatus.SC_INTERNAL_SERVER_ERROR) { throw new IllegalStateException(String.format("Failed to obtain management realm name. Server responded %d instead of %d. Isn't server authentication turned off while username and password set? Content: %s", HttpStatus.SC_INTERNAL_SERVER_ERROR, HttpStatus.SC_UNAUTHORIZED, content)); } if (response.getStatusLine().getStatusCode() != HttpStatus.SC_UNAUTHORIZED) { throw new IllegalStateException(String.format("Failed to obtain management realm name. Server responded %d instead of %d. Content: %s", response.getStatusLine().getStatusCode(), HttpStatus.SC_UNAUTHORIZED, content)); } if (!response.containsHeader(HttpHeaders.WWW_AUTHENTICATE)) { throw new IllegalStateException("Failed to obtain management realm name. Missing WWW-Authenticate header in server response."); } for (HeaderElement el : response.getHeaders(HttpHeaders.WWW_AUTHENTICATE)[0].getElements()) { if (el.getName().equals("Digest realm")) { return el.getValue(); } } throw new IllegalStateException("Failed to obtain management realm name. Digest realm not found in WWW-Authenticate header."); }