-
Notifications
You must be signed in to change notification settings - Fork 0
/
JerseyClient2.java
82 lines (71 loc) · 2.76 KB
/
JerseyClient2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.mycompany.restclient;
import com.mycompany.restclientcommon.EmployeeBean;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.ws.rs.ClientErrorException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.jaxb.internal.JaxbStringReaderProvider;
/**
* Jersey REST client generated for REST resource:GenericResource [generic]<br>
* USAGE:
* <pre>
* NewJerseyClient client = new NewJerseyClient();
* Object response = client.XXX(...);
* // do whatever with response
* client.close();
* </pre>
*
* @author atsushi
*/
public class JerseyClient2 {
private WebTarget webTarget;
private Client client;
private static final String BASE_URI = "https://localhost:8181/RestClientExam/webresources/generic";
public JerseyClient2() {
ClientConfig config = new ClientConfig();
config.register(JaxbStringReaderProvider.class);
client = javax.ws.rs.client.ClientBuilder.newBuilder().sslContext(getSSLContext()).build();
webTarget = client.target(BASE_URI);
}
public EmployeeBean search(final long id) throws ClientErrorException {
WebTarget resource = webTarget.path("search").queryParam("id", id);
return resource.request(javax.ws.rs.core.MediaType.APPLICATION_XML).get(EmployeeBean.class);
}
public void close() {
client.close();
}
private HostnameVerifier getHostnameVerifier() {
return new HostnameVerifier() {
@Override
public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
return true;
}
};
}
private SSLContext getSSLContext() {
// for alternative implementation checkout org.glassfish.jersey.SslConfigurator
javax.net.ssl.TrustManager x509 = new javax.net.ssl.X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException {
return;
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException {
return;
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
};
SSLContext ctx = null;
try {
ctx = SSLContext.getInstance("SSL");
ctx.init(null, new javax.net.ssl.TrustManager[]{x509}, null);
} catch (java.security.GeneralSecurityException ex) {
}
return ctx;
}
}