Search in sources :

Example 1 with AuthEnv

use of org.apache.jena.http.auth.AuthEnv in project jena by apache.

the class HttpLib method execute.

/**
 * Execute a request, return a {@code HttpResponse<X>} which
 * can be passed to {@link #handleHttpStatusCode(HttpResponse)} which will
 * convert non-2xx status code to {@link HttpException HttpExceptions}.
 * <p>
 * This function applies the HTTP authentication challenge support
 * and will repeat the request if necessary with added authentication.
 * <p>
 * See {@link AuthEnv} for authentication registration.
 * <br/>
 * See {@link #executeJDK} to execute exactly once without challenge response handling.
 *
 * @see AuthEnv AuthEnv for authentic registration
 * @see #executeJDK executeJDK to execute exacly once.
 *
 * @param httpClient
 * @param httpRequest
 * @param bodyHandler
 * @return HttpResponse
 */
public static </*package*/
X> HttpResponse<X> execute(HttpClient httpClient, HttpRequest httpRequest, BodyHandler<X> bodyHandler) {
    // To run with no jena-supplied authentication handling.
    if (false)
        return executeJDK(httpClient, httpRequest, bodyHandler);
    URI uri = httpRequest.uri();
    URI key = null;
    AuthEnv authEnv = AuthEnv.get();
    if (uri.getUserInfo() != null) {
        String[] up = uri.getUserInfo().split(":");
        if (up.length == 2) {
            // Only if "user:password@host", not "user@host"
            key = HttpLib.endpointURI(uri);
            // The auth key will be with u:p making it specific.
            authEnv.registerUsernamePassword(key, up[0], up[1]);
        }
    }
    try {
        return AuthLib.authExecute(httpClient, httpRequest, bodyHandler);
    } finally {
        if (key != null)
            authEnv.unregisterUsernamePassword(key);
    }
}
Also used : AuthEnv(org.apache.jena.http.auth.AuthEnv) URI(java.net.URI)

Aggregations

URI (java.net.URI)1 AuthEnv (org.apache.jena.http.auth.AuthEnv)1