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);
}
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{}");
}
}
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();
}
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();
}
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);
}
}
Aggregations