String authHeaderValue = connection.getHeaderField("WWW-Authenticate");
if (authHeaderValue != null) {
Map<String, String> headerEntries = StringSplitUtils.splitEachArrayElementAndCreateMap(StringSplitUtils.splitIgnoringQuotes(authHeaderValue, ','), "=", "\"");
String requiredRealm = headerEntries.get("realm");
if ((requiredRealm != null) && (!requiredRealm.equals(realm))) {
throw new InvalidOAuthRealmException(String.format("Invalid OAuth realm. Provider expects \"%s\", when the resource details specify \"%s\".", requiredRealm, realm), requiredRealm);
}
}
throw new OAuthRequestFailedException("OAuth authentication failed: " + responseMessage);
}