ovirt-engine-sdk-java icon indicating copy to clipboard operation
ovirt-engine-sdk-java copied to clipboard

request new token ,release previous request

Open huangtaoz opened this issue 1 year ago • 0 comments

method: HttpConnection.send(HttpUriRequest request, boolean failedAuth) old:

private HttpResponse send(HttpUriRequest request, boolean failedAuth) {
        try {
            injectHeaders(request);
            HttpResponse response = client.execute(request);

            /**
             * If the request failed because of authentication, and it
             * wasn't a request to the SSO service, then the most likely
             * cause is an expired SSO token. In this case we need to
             * request a new token, and try the original request again, but
             * only once. It if fails again, we just return the failed
             * response.
             */
            if (response.getStatusLine().getStatusCode() == 401 && !failedAuth) {
                ssoToken = null;
                authenticate();
                response = send(request, true);
            }

            if (response.getFirstHeader("content-type") != null) {
                checkContentType(XML_CONTENT_TYPE_RE, "XML", response.getFirstHeader("content-type").getValue());
            }
            return response;
        }
        catch (Exception e) {
            throw new Error("Failed to send request", e);
        }
    }


private HttpResponse send(HttpUriRequest request, boolean failedAuth) {
    try {
        injectHeaders(request);
        HttpResponse response = client.execute(request);

        /**
         * If the request failed because of authentication, and it
         * wasn't a request to the SSO service, then the most likely
         * cause is an expired SSO token. In this case we need to
         * request a new token, and try the original request again, but
         * only once. It if fails again, we just return the failed
         * response.
         */
        if (response.getStatusLine().getStatusCode() == 401 && !failedAuth) {
            HttpEntity entity = response.getEntity();
            if (null != entity) {
                entity.getContent().close();
            }
            ssoToken = null;
            authenticate();
            response = send(request, true);
        }

        if (response.getFirstHeader("content-type") != null) {
            checkContentType(XML_CONTENT_TYPE_RE, "XML", response.getFirstHeader("content-type").getValue());
        }
        return response;
    }
    catch (Exception e) {
        throw new Error("Failed to send request", e);
    }
}

huangtaoz avatar Aug 07 '23 10:08 huangtaoz