Search in sources :

Example 56 with Authenticator

use of java.net.Authenticator in project jena by apache.

the class DigestLib method getUserNameAndPassword.

/**
 * Extract user and password from a {@link HttpClient}.
 */
private static Pair<String, String> getUserNameAndPassword(HttpClient httpClient) {
    Optional<Authenticator> optAuth = httpClient.authenticator();
    if (optAuth.isEmpty())
        throw new HttpException("Username/password required but not present in HttpClient");
    // We just want the PasswordAuthentication!
    PasswordAuthentication x = optAuth.get().requestPasswordAuthenticationInstance(null, null, // port,
    -1, // protocol,
    null, // prompt,
    null, // scheme,
    null, // url,
    null, RequestorType.SERVER);
    String user = x.getUserName();
    String password = new String(x.getPassword());
    return Pair.create(user, password);
}
Also used : HttpException(org.apache.jena.atlas.web.HttpException) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 57 with Authenticator

use of java.net.Authenticator in project jena by apache.

the class ExAuth01_RDFConnectionPW method exampleConnectionAuthWithHttpClient.

// HttpClient
public static void exampleConnectionAuthWithHttpClient() {
    System.out.println();
    System.out.println("HttpClient + RDFConnectionRemote");
    // Custom HttpClient
    Authenticator authenticator = AuthLib.authenticator("u", "p");
    HttpClient httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).authenticator(authenticator).build();
    try (RDFConnection conn = RDFConnectionRemote.service(dataURL).httpClient(// Custom HttpClient
    httpClient).build()) {
        conn.update("INSERT DATA{}");
        conn.queryAsk("ASK{}");
    }
}
Also used : RDFConnection(org.apache.jena.rdfconnection.RDFConnection) HttpClient(java.net.http.HttpClient) Authenticator(java.net.Authenticator)

Example 58 with Authenticator

use of java.net.Authenticator in project jena by apache.

the class ExAuth03_UpdateExecutionPW method exampleUpdateAuthWithHttpClient.

// HttpClient
public static void exampleUpdateAuthWithHttpClient() {
    System.out.println();
    System.out.println("HttpClient + UpdateExecutionHTTP");
    Authenticator authenticator = AuthLib.authenticator("u", "p");
    HttpClient httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).authenticator(authenticator).build();
    UpdateExecutionHTTP.service(dataURL).httpClient(httpClient).update("CLEAR ALL").execute();
}
Also used : HttpClient(java.net.http.HttpClient) Authenticator(java.net.Authenticator)

Example 59 with Authenticator

use of java.net.Authenticator in project jena by apache.

the class AbstractTestWebappAuth_JDK method withAuthJDK.

public static UpdateExecutionHTTP withAuthJDK(UpdateExecutionHTTPBuilder builder, String user, String passwd) {
    Authenticator authenticator = AuthLib.authenticator(user, passwd);
    HttpClient hc = HttpClient.newBuilder().authenticator(authenticator).build();
    return builder.httpClient(hc).build();
}
Also used : HttpClient(java.net.http.HttpClient) Authenticator(java.net.Authenticator)

Example 60 with Authenticator

use of java.net.Authenticator in project jena by apache.

the class ExFuseki_09_Https_Auth method client.

private static void client(SSLContext sslContext) {
    Authenticator authenticator1 = AuthLib.authenticator(USER, PASSWORD);
    HttpClient hc = HttpClient.newBuilder().authenticator(authenticator1).connectTimeout(Duration.ofSeconds(10)).sslContext(sslContext).build();
    System.out.println("Good client set up");
    RDFConnection connSingle = RDFConnectionFuseki.create().httpClient(hc).destination("https://localhost:3443/ds").build();
    try (RDFConnection conn = connSingle) {
        QueryExecution qExec = conn.query("ASK{}");
        QueryExecUtils.executeQuery(qExec);
    }
    System.out.println("Bad client set up");
    Authenticator authenticator2 = AuthLib.authenticator(USER, "wrong-trousers-gromit");
    HttpClient hc2 = HttpClient.newBuilder().authenticator(authenticator2).connectTimeout(Duration.ofSeconds(10)).sslContext(sslContext).build();
    try (RDFConnection conn = RDFConnectionFuseki.create().httpClient(hc2).destination("https://localhost:3443/ds").build()) {
        QueryExecution qExec = conn.query("ASK{}");
        QueryExecUtils.executeQuery(qExec);
    } catch (Exception ex) {
        System.out.println(ex);
    }
}
Also used : RDFConnection(org.apache.jena.rdfconnection.RDFConnection) HttpClient(java.net.http.HttpClient) Authenticator(java.net.Authenticator) QueryExecution(org.apache.jena.query.QueryExecution)

Aggregations

Authenticator (java.net.Authenticator)80 PasswordAuthentication (java.net.PasswordAuthentication)50 URL (java.net.URL)18 Proxy (java.net.Proxy)14 InetSocketAddress (java.net.InetSocketAddress)12 HttpClient (java.net.http.HttpClient)11 Field (java.lang.reflect.Field)10 HttpURLConnection (java.net.HttpURLConnection)10 IOException (java.io.IOException)9 Test (org.junit.Test)7 Method (java.lang.reflect.Method)6 File (java.io.File)5 SocketAddress (java.net.SocketAddress)5 InputStream (java.io.InputStream)4 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ArrayList (java.util.ArrayList)3 Content (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content)3 Debug (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug)3 JCommander (com.beust.jcommander.JCommander)2