private static AzureADToken getTokenSingleCall()

in src/main/java/com/microsoft/azure/datalake/store/oauth2/AzureADAuthenticator.java [236:327]


    private static AzureADToken getTokenSingleCall(String authEndpoint, String payload, Hashtable<String, String> headers, String httpMethod)
            throws IOException {

        AzureADToken token = null;
        HttpURLConnection conn = null;
        String urlString = authEndpoint;

        httpMethod = (httpMethod == null) ? "POST" : httpMethod;
        if (httpMethod.equals("GET")) {
            urlString = urlString + "?" + payload;
        }
        long startTime = System.nanoTime();
        long totalTime = 0;

        try {
            URL url = new URL(urlString);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod(httpMethod);
            conn.setReadTimeout(30000);
            conn.setConnectTimeout(30000);

            if (headers != null && headers.size() > 0) {
                for (String name : headers.keySet()) {
                    conn.setRequestProperty(name, headers.get(name));
                }
            }

            conn.setRequestProperty("Connection", "close");

            if (httpMethod.equals("POST")) {
                conn.setDoOutput(true);
                conn.getOutputStream().write(payload.getBytes("UTF-8"));
            }

            int httpResponseCode = conn.getResponseCode();
            String requestId = conn.getHeaderField("x-ms-request-id");
            String responseContentType = conn.getHeaderField("Content-Type");
            long responseContentLength = conn.getHeaderFieldLong("Content-Length", 0);
            requestId = requestId == null ? "" : requestId;

            if (httpResponseCode == 200 && responseContentType.startsWith("application/json") && responseContentLength > 0) {
                InputStream httpResponseStream = conn.getInputStream();
                token = parseTokenFromStream(httpResponseStream);

                if (log.isDebugEnabled()) {
                    totalTime = System.nanoTime() - startTime;
                    String logMessage =
                            "AADToken: HTTP connection succeeded for getting token from AzureAD. Http response: "
                                    + httpResponseCode + " " + conn.getResponseMessage()
                                    + " Content-Type: " + responseContentType
                                    + " Content-Length: " + responseContentLength
                                    + " Request ID: " + requestId.toString()
                                    + " Client Request Id: " + headers.get("client-request-id")
                                    + " Latency(ns) : " + totalTime;
                    log.debug(logMessage);
                }
            } else {
                String responseBody = consumeInputStream(conn.getInputStream(), 1024);
                totalTime = System.nanoTime() - startTime;
                String proxies = "none";
                String httpProxy=System.getProperty("http.proxy");
                String httpsProxy=System.getProperty("https.proxy");
                if (httpProxy!=null || httpsProxy!=null) {
                    proxies = "http:" + httpProxy + ";https:" + httpsProxy;
                }
                String logMessage =
                          "AADToken: HTTP connection failed for getting token from AzureAD. Http response: "
                        + httpResponseCode + " " + conn.getResponseMessage()
                        + " Content-Type: " + responseContentType
                        + " Content-Length: " + responseContentLength
                        + " Request ID: " + requestId.toString()
                        + " Client Request Id: " + headers.get("client-request-id")
                        + " Latency(ns) : " + totalTime
                        + " Proxies: " + proxies
                        + " First 1K of Body: " + responseBody;
                log.debug(logMessage);
                throw new HttpException(httpResponseCode, requestId, logMessage);
            }
        } catch(IOException e) {
            totalTime = System.nanoTime() - startTime;
            String logMessage =
                    "AADToken: HTTP connection failed for getting token from AzureAD due to timeout. "
                    + " Client Request Id :" + headers.get("client-request-id")
                    + " Latency(ns) : " + totalTime;
            log.debug(logMessage);
            throw new IOException(logMessage, e);
        }
        finally {
            if (conn != null) conn.disconnect();
        }
        return token;
    }