Commit d37ef9f7 authored by j.hoerdt's avatar j.hoerdt
Browse files

in the middle of exchanging httpclient

parent bb753be5
......@@ -16,11 +16,11 @@ dependencies {
}
mainClassName = 'sensor2graph.handle.Session'
mainClassName = 'sensor2graph.Main'
jar {
manifest {
attributes 'Main-Class': 'sensor2graph.handle.Session'
attributes 'Main-Class': 'sensor2graph.Main'
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
......
package sensor2graph.handle;
package sensor2graph;
import java.io.*;
import java.time.Duration;
......@@ -9,7 +9,9 @@ import java.security.cert.*;
import okhttp3.*;
public class Session {
public class Handle {
public static OkHttpClient http_client;
private static TrustManager[] get_trust_managers() throws Exception {
var server_self_signed_certificate = CertificateFactory.getInstance("X.509")
.generateCertificate(new FileInputStream("server_cert"));
......@@ -31,21 +33,12 @@ public class Session {
return key_manager_factory.getKeyManagers();
}
public static HostnameVerifier allowAllHostNames() {
private static HostnameVerifier allowAllHostNames() {
return (hostname, sslSession) -> true;
}
public static void main(String[] args) throws Exception {
var trust_managers = get_trust_managers();
var trust_self_signed_cert_and_provide_client_cert = SSLContext.getInstance("TLS");
trust_self_signed_cert_and_provide_client_cert.init(get_key_managers(), trust_managers, null);
var client = new OkHttpClient.Builder()
.sslSocketFactory(trust_self_signed_cert_and_provide_client_cert.getSocketFactory(), (X509TrustManager) trust_managers[0])
.hostnameVerifier(allowAllHostNames())
.connectTimeout(Duration.ofSeconds(20))
.build();
Request request = new Request.Builder()
.url("https://vm13.pid.gwdg.de:8000/api/handles?prefix=21.11138")
.post(RequestBody.create(null, new byte[0]))
......@@ -53,7 +46,7 @@ public class Session {
.header("Authorization", "Handle clientCert=\"true\"")
.build();
try (Response response = client.newCall(request).execute()) {
try (Response response = http_client.newCall(request).execute()) {
System.out.println(response.body().string() + response.code());
}
......@@ -67,4 +60,17 @@ public class Session {
// new Gson().fromJson(response_body, JsonObject.class).get("epic-pid").getAsString();
}
public static OkHttpClient get_client() throws Exception, NoSuchAlgorithmException, KeyManagementException {
var trust_managers = get_trust_managers();
var trust_self_signed_cert_and_provide_client_cert = SSLContext.getInstance("TLS");
trust_self_signed_cert_and_provide_client_cert.init(get_key_managers(), trust_managers, null);
var client = new OkHttpClient.Builder()
.sslSocketFactory(trust_self_signed_cert_and_provide_client_cert.getSocketFactory(), (X509TrustManager) trust_managers[0])
.hostnameVerifier(allowAllHostNames())
.connectTimeout(Duration.ofSeconds(20))
.build();
return client;
}
}
......@@ -161,7 +161,10 @@ public class Main {
Main.logger.fine("initializing neo4j driver.");
initialize_driver();
try {
Handle.http_client = Handle.get_client();
test_auth();
run();
} catch (org.neo4j.driver.exceptions.AuthenticationException e) {
......
......@@ -5,6 +5,8 @@ import sensor2graph.georev.*;
import org.neo4j.driver.*;
import org.neo4j.driver.Record;
import okhttp3.MediaType;
import com.google.gson.*;
import java.net.*;
......@@ -18,13 +20,6 @@ import java.util.logging.*;
import java.util.regex.*;
class Sensor {
private static HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(20))
.authenticator(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(System.getenv("SENSOR2GRAPH_PIDREG_USER"), System.getenv("SENSOR2GRAPH_PIDREG_PASS").toCharArray());
}
}).build();
private Map<String, Object> properties = new HashMap<String, Object>();
public void process(Session session, TransactionConfig transaction_config) throws Exception {
......@@ -76,6 +71,19 @@ class Sensor {
);
Main.glogger.fine("sent body: " + body);
Request request = new Request.Builder()
.url("https://vm13.pid.gwdg.de:8000/api/handles?prefix=21.11138")
.post(RequestBody.create(MediaType.parse("application/json", body.toString())))
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.header("Authorization", "Handle clientCert=\"true\"")
.build();
try (Response response = http_client.newCall(request).execute()) {
System.out.println(response.body().string() + response.code());
}
HttpRequest request = HttpRequest.newBuilder(URI.create(Main.pid_registry_uri + target_uri))
.header("Content-Type", "application/json")
.header("Accept", "application/json")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment