2323
2424import java .io .FileInputStream ;
2525import java .io .IOException ;
26+ import java .net .InetAddress ;
2627import java .net .InetSocketAddress ;
2728import java .net .URI ;
2829import java .nio .charset .StandardCharsets ;
4142import io .grpc .HttpConnectProxiedSocketAddress ;
4243import io .grpc .ProxyDetector ;
4344import io .netty .handler .ssl .ClientAuth ;
45+ import io .netty .handler .ssl .JdkSslContext ;
4446import io .netty .handler .ssl .SslContext ;
4547import io .netty .handler .ssl .SslContextBuilder ;
4648import io .netty .handler .ssl .SslProvider ;
49+ import mockwebserver3 .Dispatcher ;
50+ import mockwebserver3 .MockResponse ;
51+ import mockwebserver3 .MockWebServer ;
52+ import mockwebserver3 .RecordedRequest ;
53+ import okhttp3 .Headers ;
4754import org .apache .arrow .flight .FlightServer ;
4855import org .apache .arrow .flight .Location ;
4956import org .apache .arrow .flight .NoOpFlightProducer ;
@@ -110,8 +117,11 @@ public static VectorSchemaRoot generateVectorSchemaRoot(final int fieldCount, fi
110117
111118 // fixme
112119 // Create SslContext for mTLS only
113- public static SslContext createNettySslContext (boolean isServer , String format , String password , String keyFilePath , String trustFilePath , boolean isDisableKeystore , boolean isJdkSslContext )
114- throws IOException , KeyStoreException , NoSuchAlgorithmException , CertificateException , UnrecoverableKeyException {
120+ public static SslContext createNettySslContext (final boolean isServer , final String format , final String password , final String keyFilePath ,
121+ final String trustFilePath , final boolean isDisableKeystore ,
122+ final boolean isJdkSslContext )
123+ throws IOException , KeyStoreException , NoSuchAlgorithmException , CertificateException ,
124+ UnrecoverableKeyException {
115125 KeyManagerFactory keyManagerFactory = getKeyManagerFactory (format , password , keyFilePath );
116126
117127 TrustManagerFactory trustManagerFactory = getTrustManagerFactory (format , password , trustFilePath );
@@ -136,7 +146,8 @@ public static SslContext createNettySslContext(boolean isServer, String format,
136146 return sslContextBuilder .build ();
137147 }
138148
139- public static ProxyDetector createProxyDetector (@ Nonnull final String targetUrl , @ Nonnull final String proxyUrl , @ Nullable final String username , @ Nullable final String password ) {
149+ public static ProxyDetector createProxyDetector (@ Nonnull final String targetUrl , @ Nonnull final String proxyUrl ,
150+ @ Nullable final String username , @ Nullable final String password ) {
140151 URI targetUri = URI .create (targetUrl );
141152 URI proxyUri = URI .create (proxyUrl );
142153 return (targetServerAddress ) -> {
@@ -154,17 +165,43 @@ public static ProxyDetector createProxyDetector(@Nonnull final String targetUrl,
154165 };
155166 }
156167
168+ public static MockWebServer customDispatchServer (@ NotNull final String host , @ NotNull final Integer port ,
169+ @ Nullable final MockResponse mockResponse ,
170+ @ Nullable final SslContext sslContext ,
171+ boolean requireClientAuth ) throws IOException {
172+ MockWebServer server = new MockWebServer ();
173+ server .setDispatcher (new Dispatcher () {
174+ @ NotNull
175+ @ Override
176+ public MockResponse dispatch (@ NotNull RecordedRequest recordedRequest ) {
177+ return mockResponse != null ? mockResponse : new MockResponse (200 , Headers .EMPTY , "Success" );
178+ }
179+ });
180+ if (sslContext instanceof JdkSslContext ) {
181+ server .useHttps (((JdkSslContext ) sslContext ).context ().getSocketFactory ());
182+ if (requireClientAuth ) {
183+ server .requireClientAuth ();
184+ }
185+ }
186+ server .start (InetAddress .getByName (host ), port );
187+ return server ;
188+ }
189+
157190 @ NotNull
158- private static TrustManagerFactory getTrustManagerFactory (String format , String password , String trustFilePath ) throws NoSuchAlgorithmException , KeyStoreException , IOException , CertificateException {
159- TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance (TrustManagerFactory .getDefaultAlgorithm ());
191+ private static TrustManagerFactory getTrustManagerFactory (final String format , final String password , final String trustFilePath )
192+ throws NoSuchAlgorithmException , KeyStoreException , IOException , CertificateException {
193+ TrustManagerFactory trustManagerFactory =
194+ TrustManagerFactory .getInstance (TrustManagerFactory .getDefaultAlgorithm ());
160195 KeyStore trustStore = KeyStore .getInstance (format );
161196 trustStore .load (new FileInputStream (trustFilePath ), password .toCharArray ());
162197 trustManagerFactory .init (trustStore );
163198 return trustManagerFactory ;
164199 }
165200
166201 @ NotNull
167- private static KeyManagerFactory getKeyManagerFactory (String format , String password , String keyFilePath ) throws KeyStoreException , IOException , NoSuchAlgorithmException , CertificateException , UnrecoverableKeyException {
202+ private static KeyManagerFactory getKeyManagerFactory (final String format , final String password , final String keyFilePath )
203+ throws KeyStoreException , IOException , NoSuchAlgorithmException , CertificateException ,
204+ UnrecoverableKeyException {
168205 KeyStore keyStore = KeyStore .getInstance (format );
169206 keyStore .load (new FileInputStream (keyFilePath ), password .toCharArray ());
170207 KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
0 commit comments