]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.rest/src/org/simantics/scl/rest/HttpClientUtils.java
Improvements to SCL HTTP client
[simantics/platform.git] / bundles / org.simantics.scl.rest / src / org / simantics / scl / rest / HttpClientUtils.java
1 package org.simantics.scl.rest;
2
3 import java.security.KeyManagementException;
4 import java.security.NoSuchAlgorithmException;
5 import java.security.cert.X509Certificate;
6 import java.util.concurrent.Future;
7
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;
20
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;
26
27 public class HttpClientUtils {
28     
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();
33     }
34     
35     public static String statusMessageOf(Response response) {
36         return response.getStatusInfo().getReasonPhrase();
37     }
38     
39     public static Future<Response> asyncInvoke(Invocation invocation, Function1<Response, Tuple0> responseCallback, Function1<Throwable, Tuple0> failureCallback) {
40         SCLContext context = SCLContext.createDerivedContext();
41         
42         return invocation.submit(new InvocationCallback<Response>() {
43
44             @Override
45             public void completed(Response response) {
46                 SCLContext.push(context);
47                 try {
48                     responseCallback.apply(response);
49                 } finally {
50                     SCLContext.pop();
51                 }
52             }
53             
54             @Override
55             public void failed(Throwable throwable) {
56                 SCLContext.push(context);
57                 try {
58                     failureCallback.apply(throwable);
59                 } finally {
60                     SCLContext.pop();
61                 }
62                 
63             }
64         });
65     }
66     
67     public static ClientBuilder trustAllClientBuilder() throws NoSuchAlgorithmException, KeyManagementException {
68         TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
69             public java.security.cert.X509Certificate[] getAcceptedIssuers() {
70                 return null;
71             }
72             public void checkClientTrusted(X509Certificate[] certs, String authType) {
73             }
74             public void checkServerTrusted(X509Certificate[] certs, String authType) {
75             }
76         }
77         };
78
79         SSLContext sc = SSLContext.getInstance("SSL");
80         sc.init(null, trustAllCerts, new java.security.SecureRandom());
81
82         return ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier(new HostnameVerifier() {
83             @Override
84             public boolean verify(String hostname, SSLSession session) {
85                 return true;
86             }
87         });
88     }
89     
90     public static void onWriteProgress(WebTarget target, Function1<Long, Tuple0> callback) {
91         target.register(new WriteProgressInterceptor(callback));
92     }
93     
94     public static void onReadProgress(WebTarget target, Function1<Long, Tuple0> callback) {
95         target.register(new ReadProgressInterceptor(callback));
96     }
97     
98     public static Long possibleContentLengthOf(Response response) {
99         String lengthStr = response.getHeaderString("Content-Length");
100         if (lengthStr != null) {
101             try {
102                 return Long.parseLong(lengthStr);
103             } catch (NumberFormatException e) {
104                 return null;
105             }
106         } else {
107             return null;
108         }
109     }
110
111 }