in ambari-metrics-common/src/main/java/org/apache/hadoop/metrics2/sink/timeline/AppCookieManager.java [88:152]
public String getAppCookie(String endpoint, boolean refresh)
throws IOException {
HttpUriRequest outboundRequest = new HttpGet(endpoint);
URI uri = outboundRequest.getURI();
String scheme = uri.getScheme();
String host = uri.getHost();
int port = uri.getPort();
String path = uri.getPath();
if (!refresh) {
String appCookie = endpointCookieMap.get(endpoint);
if (appCookie != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("got cached cookie");
}
return appCookie;
}
}
clearAppCookie(endpoint);
DefaultHttpClient client = new DefaultHttpClient();
SPNegoSchemeFactory spNegoSF = new SPNegoSchemeFactory(/* stripPort */true);
client.getAuthSchemes().register(AuthPolicy.SPNEGO, spNegoSF);
client.getCredentialsProvider().setCredentials(
new AuthScope(/* host */null, /* port */-1, /* realm */null),
EMPTY_JAAS_CREDENTIALS);
String hadoopAuthCookie = null;
HttpResponse httpResponse = null;
try {
HttpHost httpHost = new HttpHost(host, port, scheme);
HttpRequest httpRequest = new HttpOptions(path);
httpResponse = client.execute(httpHost, httpRequest);
Header[] headers = httpResponse.getHeaders(SET_COOKIE);
if (LOG.isDebugEnabled()) {
for (Header header : headers) {
LOG.debug(header.getName() + " : " + header.getValue());
}
}
hadoopAuthCookie = getHadoopAuthCookieValue(headers);
if (hadoopAuthCookie == null) {
int statusCode = httpResponse.getStatusLine().getStatusCode();
HttpEntity entity = httpResponse.getEntity();
String responseBody = entity != null ? EntityUtils.toString(entity) : null;
LOG.error("SPNego authentication failed with statusCode = " + statusCode + ", responseBody = " + responseBody + ", can not get hadoop.auth cookie for URL: " + endpoint);
return null;
}
} finally {
if (httpResponse != null) {
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
entity.getContent().close();
}
}
}
hadoopAuthCookie = HADOOP_AUTH_EQ + quote(hadoopAuthCookie);
setAppCookie(endpoint, hadoopAuthCookie);
if (LOG.isInfoEnabled()) {
LOG.info("Successful SPNego authentication to URL:" + uri.toString());
}
return hadoopAuthCookie;
}