1010import io .jenkins .plugins .lark .notice .tools .JsonUtils ;
1111import lombok .extern .slf4j .Slf4j ;
1212
13+ import javax .net .ssl .SSLContext ;
14+ import javax .net .ssl .SSLEngine ;
15+ import javax .net .ssl .TrustManager ;
16+ import javax .net .ssl .X509ExtendedTrustManager ;
17+ import java .net .Socket ;
1318import java .net .URI ;
1419import java .net .http .HttpClient ;
1520import java .net .http .HttpRequest ;
1621import java .net .http .HttpResponse ;
22+ import java .security .SecureRandom ;
23+ import java .security .cert .X509Certificate ;
1724import java .time .Duration ;
1825import java .util .Optional ;
1926
3037@ Slf4j
3138public abstract class AbstractMessageSender implements MessageSender {
3239
40+ /**
41+ * Define a mock TrustManager to ignore certificate validation
42+ */
43+ private static final TrustManager MOCK_TRUST_MANAGER = new X509ExtendedTrustManager () {
44+ @ Override
45+ public X509Certificate [] getAcceptedIssuers () {
46+ return new X509Certificate []{};
47+ }
48+
49+ @ Override
50+ public void checkClientTrusted (X509Certificate [] chain , String authType ) {
51+ }
52+
53+ @ Override
54+ public void checkServerTrusted (X509Certificate [] chain , String authType ) {
55+ }
56+
57+ @ Override
58+ public void checkClientTrusted (X509Certificate [] chain , String authType , Socket socket ) {
59+ }
60+
61+ @ Override
62+ public void checkServerTrusted (X509Certificate [] chain , String authType , Socket socket ) {
63+ }
64+
65+ @ Override
66+ public void checkClientTrusted (X509Certificate [] chain , String authType , SSLEngine engine ) {
67+ }
68+
69+ @ Override
70+ public void checkServerTrusted (X509Certificate [] chain , String authType , SSLEngine engine ) {
71+ }
72+ };
73+
3374 /**
3475 * Retrieves the robot configuration information.
3576 *
@@ -49,8 +90,11 @@ protected SendResult sendMessage(String body, String... headers) {
4990 RobotConfigModel robotConfig = getRobotConfig ();
5091 String webhook = robotConfig .getWebhook ();
5192
52- HttpRequest .Builder builder = HttpRequest .newBuilder ().uri (URI .create (webhook ))
53- .header (CONTENT_TYPE , APPLICATION_JSON_VALUE ).timeout (Duration .ofMinutes (3 ))
93+ // Create HttpRequest.Builder
94+ HttpRequest .Builder builder = HttpRequest .newBuilder ()
95+ .uri (URI .create (webhook ))
96+ .header (CONTENT_TYPE , APPLICATION_JSON_VALUE )
97+ .timeout (Duration .ofMinutes (3 ))
5498 .POST (HttpRequest .BodyPublishers .ofString (StringUtils .defaultString (body )));
5599
56100 if (ArrayUtils .isNotEmpty (headers )) {
@@ -61,10 +105,9 @@ protected SendResult sendMessage(String body, String... headers) {
61105 builder .headers (headers );
62106 }
63107
64- HttpResponse < String > response = HttpClient . newBuilder (). version ( HttpClient . Version . HTTP_1_1 )
65- . followRedirects ( HttpClient . Redirect . NORMAL ). proxy ( robotConfig . getProxySelector ()). build ( )
108+ // Create HttpClient and send the request
109+ HttpResponse < String > response = createHttpClient ( robotConfig )
66110 .send (builder .build (), HttpResponse .BodyHandlers .ofString ());
67-
68111 sendResult = JsonUtils .readValue (response .body (), SendResult .class );
69112 } catch (Exception e ) {
70113 log .error ("Failed to send Lark message" , e );
@@ -74,4 +117,22 @@ protected SendResult sendMessage(String body, String... headers) {
74117 return sendResult ;
75118 }
76119
120+ /**
121+ * Create HttpClient.
122+ *
123+ * @param robotConfig Robot configuration information.
124+ * @return HttpClient instance.
125+ * @throws Exception Exception during HttpClient creation.
126+ */
127+ private HttpClient createHttpClient (RobotConfigModel robotConfig ) throws Exception {
128+ SSLContext sslContext = SSLContext .getInstance ("TLS" );
129+ sslContext .init (null , new TrustManager []{MOCK_TRUST_MANAGER }, new SecureRandom ());
130+ return HttpClient .newBuilder ()
131+ .version (HttpClient .Version .HTTP_1_1 )
132+ .followRedirects (HttpClient .Redirect .NORMAL )
133+ .proxy (robotConfig .getProxySelector ())
134+ .sslContext (sslContext )
135+ .build ();
136+ }
137+
77138}
0 commit comments