1 package org.simantics.scl.rest;
3 import java.security.KeyManagementException;
4 import java.security.NoSuchAlgorithmException;
5 import java.security.cert.X509Certificate;
6 import java.util.concurrent.Future;
8 import javax.net.ssl.HostnameVerifier;
9 import javax.net.ssl.SSLContext;
10 import javax.net.ssl.SSLSession;
11 import javax.net.ssl.TrustManager;
12 import javax.net.ssl.X509TrustManager;
13 import javax.ws.rs.client.Client;
14 import javax.ws.rs.client.ClientBuilder;
15 import javax.ws.rs.client.Invocation;
16 import javax.ws.rs.client.InvocationCallback;
17 import javax.ws.rs.client.WebTarget;
18 import javax.ws.rs.core.Configuration;
19 import javax.ws.rs.core.Response;
21 import org.glassfish.jersey.client.ClientConfig;
22 import org.glassfish.jersey.media.multipart.MultiPartFeature;
23 import org.simantics.scl.runtime.SCLContext;
24 import org.simantics.scl.runtime.function.Function1;
25 import org.simantics.scl.runtime.tuple.Tuple0;
27 public class HttpClientUtils {
29 public static Client buildClient(ClientBuilder clientBuilder) {
30 final ClientConfig clientConfig = new ClientConfig();
31 clientConfig.register(MultiPartFeature.class);
32 return clientBuilder.withConfig((Configuration) clientConfig).build();
35 public static String statusMessageOf(Response response) {
36 return response.getStatusInfo().getReasonPhrase();
39 public static Future<Response> asyncInvoke(Invocation invocation, Function1<Response, Tuple0> responseCallback, Function1<Throwable, Tuple0> failureCallback) {
40 SCLContext context = SCLContext.createDerivedContext();
42 return invocation.submit(new InvocationCallback<Response>() {
45 public void completed(Response response) {
46 SCLContext.push(context);
48 responseCallback.apply(response);
55 public void failed(Throwable throwable) {
56 SCLContext.push(context);
58 failureCallback.apply(throwable);
67 public static ClientBuilder trustAllClientBuilder() throws NoSuchAlgorithmException, KeyManagementException {
68 TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
69 public java.security.cert.X509Certificate[] getAcceptedIssuers() {
72 public void checkClientTrusted(X509Certificate[] certs, String authType) {
74 public void checkServerTrusted(X509Certificate[] certs, String authType) {
79 SSLContext sc = SSLContext.getInstance("SSL");
80 sc.init(null, trustAllCerts, new java.security.SecureRandom());
82 return ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier(new HostnameVerifier() {
84 public boolean verify(String hostname, SSLSession session) {
90 public static void onWriteProgress(WebTarget target, Function1<Long, Tuple0> callback) {
91 target.register(new WriteProgressInterceptor(callback));
94 public static void onReadProgress(WebTarget target, Function1<Long, Tuple0> callback) {
95 target.register(new ReadProgressInterceptor(callback));
98 public static Long possibleContentLengthOf(Response response) {
99 String lengthStr = response.getHeaderString("Content-Length");
100 if (lengthStr != null) {
102 return Long.parseLong(lengthStr);
103 } catch (NumberFormatException e) {