Skip to content

Commit 49948fc

Browse files
authored
Bumped finagle (#108)
* Bumped finagle with custom SslClientConfiguration * Converted finagle service example from java to scala * Removed unused imports * Reformatted option * Corrected if statement * fixed name * Adjusted reference to finagle
1 parent 6bd0a7a commit 49948fc

File tree

8 files changed

+164
-187
lines changed

8 files changed

+164
-187
lines changed

README.MD

+1-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ All client examples use the same base ssl configuration created within the [SSLC
401401
* [Ktor with Okhttp engine](https://github.com/ktorio/ktor) -> [Client Configuration](https://github.com/Hakky54/mutual-tls-ssl/blob/master/client/src/main/java/nl/altindag/client/service/KtorOkHttpClientService.kt) | [Example request](https://github.com/Hakky54/mutual-tls-ssl/blob/master/client/src/main/java/nl/altindag/client/service/KtorHttpClientService.kt)
402402

403403
**Scala**
404-
* [Twitter Finagle](https://github.com/twitter/finagle) -> [Client Configuration](https://github.com/Hakky54/mutual-tls-ssl/blob/35cba2f3a2dcd73b01fa323b99eec7777f7429bb/client/src/main/java/nl/altindag/client/ClientConfig.java#L233) | [Example request](https://github.com/Hakky54/mutual-tls-ssl/blob/master/client/src/main/java/nl/altindag/client/service/FinagleHttpClientService.java)
404+
* [Twitter Finagle](https://github.com/twitter/finagle) -> [Client Configuration & Example request](https://github.com/Hakky54/mutual-tls-ssl/blob/master/client/src/main/java/nl/altindag/client/service/FinagleHttpClientService.java)
405405
* [Twitter Finagle Featherbed](https://github.com/finagle/featherbed) -> [Client Configuration & Example request](https://github.com/Hakky54/mutual-tls-ssl/blob/d78e4e81b8b775d3ff09c11b0a7c1532a741199e/client/src/main/java/nl/altindag/client/service/FeatherbedRequestService.scala#L19)
406406
* [Akka Http Client](https://github.com/akka/akka-http) -> [Client Configuration](https://github.com/Hakky54/mutual-tls-ssl/blob/35cba2f3a2dcd73b01fa323b99eec7777f7429bb/client/src/main/java/nl/altindag/client/ClientConfig.java#L253) | [Example request](https://github.com/Hakky54/mutual-tls-ssl/blob/master/client/src/main/java/nl/altindag/client/service/AkkaHttpClientService.java)
407407
* [Dispatch Reboot](https://github.com/dispatch/reboot) -> [Client Configuration & Example request](https://github.com/Hakky54/mutual-tls-ssl/blob/master/client/src/main/java/nl/altindag/client/service/DispatchRebootService.scala)

client/src/main/java/nl/altindag/client/ClientConfig.java

-17
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323
import com.google.gson.GsonBuilder;
2424
import com.sun.jersey.api.client.config.DefaultClientConfig;
2525
import com.sun.jersey.client.urlconnection.HTTPSProperties;
26-
import com.twitter.finagle.Http;
27-
import com.twitter.finagle.Service;
28-
import com.twitter.finagle.http.Request;
29-
import com.twitter.finagle.http.Response;
3026
import com.typesafe.config.ConfigFactory;
3127
import feign.Feign;
3228
import feign.googlehttpclient.GoogleHttpClient;
@@ -70,8 +66,6 @@
7066
import retrofit2.converter.gson.GsonConverterFactory;
7167

7268
import javax.net.ssl.SSLException;
73-
import java.net.URI;
74-
import java.net.URISyntaxException;
7569
import java.net.http.HttpClient;
7670

7771
@Component
@@ -243,17 +237,6 @@ public Retrofit retrofit(@Qualifier("okHttpClient") OkHttpClient okHttpClient) {
243237
.build();
244238
}
245239

246-
@Bean
247-
public Service<Request, Response> finagle(SSLFactory sslFactory) throws URISyntaxException {
248-
var uri = new URI(Constants.getServerUrl());
249-
var client = Http.client().withNoHttp2();
250-
if (uri.getScheme().equals("https")) {
251-
client = client.withTransport()
252-
.tls(sslFactory.getSslContext());
253-
}
254-
return client.newService(uri.getHost() + ":" + uri.getPort());
255-
}
256-
257240
@Bean
258241
public ActorSystem actorSystem() {
259242
return ActorSystem.create(

client/src/main/java/nl/altindag/client/service/FinagleHttpClientService.java

-58
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2018 Thunderberry.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package nl.altindag.client.service
17+
18+
import com.twitter.finagle.http.{Request, RequestBuilder, Response}
19+
import com.twitter.finagle.ssl.client.SslClientConfiguration
20+
import com.twitter.finagle.ssl.{KeyCredentials, TrustCredentials}
21+
import com.twitter.finagle.{Http, Service}
22+
import nl.altindag.client.ClientType.FINAGLE
23+
import nl.altindag.client.Constants.HEADER_KEY_CLIENT_TYPE
24+
import nl.altindag.client.model.ClientResponse
25+
import nl.altindag.client.{ClientType, Constants}
26+
import nl.altindag.ssl.SSLFactory
27+
import org.springframework.beans.factory.annotation.Qualifier
28+
import org.springframework.context.annotation.Bean
29+
import org.springframework.stereotype
30+
import org.springframework.stereotype.Component
31+
32+
import java.net.URI
33+
import java.util.concurrent.TimeUnit
34+
import scala.jdk.javaapi.OptionConverters
35+
36+
@stereotype.Service
37+
class FinagleHttpClientService(@Qualifier("finagleClient") service: Service[Request, Response]) extends RequestService {
38+
39+
private val TIMEOUT_AMOUNT_IN_SECONDS = 5
40+
41+
override def executeRequest(url: String): ClientResponse = {
42+
val request = RequestBuilder()
43+
.addHeader(HEADER_KEY_CLIENT_TYPE, getClientType.getValue)
44+
.url(url)
45+
.buildGet()
46+
47+
service.apply(request)
48+
.map(response => new ClientResponse(response.contentString, response.statusCode))
49+
.toJavaFuture
50+
.get(TIMEOUT_AMOUNT_IN_SECONDS, TimeUnit.SECONDS)
51+
}
52+
53+
override def getClientType: ClientType = FINAGLE
54+
}
55+
56+
@Component
57+
class FinagleHttpClientConfiguration {
58+
59+
@Bean(name = Array("finagleClient"))
60+
def createFinagle(sslFactory: SSLFactory): Service[Request, Response] = {
61+
val uri = new URI(Constants.getServerUrl)
62+
var client = Http.client
63+
64+
if ("https".equals(uri.getScheme)) {
65+
val sslClientConfiguration = SslClientConfiguration(
66+
keyCredentials = OptionConverters.toScala(sslFactory.getKeyManagerFactory)
67+
.map(kmf => KeyCredentials.KeyManagerFactory(kmf))
68+
.getOrElse(KeyCredentials.Unspecified),
69+
trustCredentials = OptionConverters.toScala(sslFactory.getTrustManagerFactory)
70+
.map(tmf => TrustCredentials.TrustManagerFactory(tmf))
71+
.getOrElse(TrustCredentials.Unspecified)
72+
)
73+
74+
client = client.withTransport.tls(sslClientConfiguration)
75+
}
76+
77+
client.newService(uri.getHost + ":" + uri.getPort)
78+
}
79+
80+
}

client/src/test/java/nl/altindag/client/ClientConfigShould.java

+3-33
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
import akka.http.javadsl.Http;
2020
import com.github.mizosoft.methanol.Methanol;
2121
import com.google.api.client.http.HttpTransport;
22-
import com.twitter.finagle.Service;
23-
import com.twitter.finagle.http.Request;
24-
import com.twitter.finagle.http.Response;
2522
import feign.Feign;
2623
import jakarta.ws.rs.client.Client;
2724
import kong.unirest.Unirest;
@@ -40,15 +37,16 @@
4037
import javax.net.ssl.SSLException;
4138
import java.io.IOException;
4239
import java.net.ConnectException;
43-
import java.net.URISyntaxException;
4440
import java.net.http.HttpClient;
4541

4642
import static nl.altindag.client.util.AssertJCustomConditions.GSON_CONVERTER_FACTORY;
4743
import static nl.altindag.client.util.AssertJCustomConditions.SUBSTRING_OF_HTTP_OR_HTTPS_SERVER_URL;
4844
import static nl.altindag.client.util.SSLFactoryTestHelper.createSSLFactory;
4945
import static org.assertj.core.api.Assertions.assertThat;
5046
import static org.assertj.core.api.Assertions.assertThatThrownBy;
51-
import static org.mockito.Mockito.*;
47+
import static org.mockito.Mockito.mock;
48+
import static org.mockito.Mockito.times;
49+
import static org.mockito.Mockito.verify;
5250

5351
@ExtendWith(MockitoExtension.class)
5452
class ClientConfigShould {
@@ -306,34 +304,6 @@ void createRetrofitWithProvidedOkHttpClient() {
306304
assertThat(retrofit.converterFactories()).has(GSON_CONVERTER_FACTORY);
307305
}
308306

309-
@Test
310-
void createFinagleClientWithoutSecurity() throws URISyntaxException {
311-
System.setProperty("url", TestConstants.HTTP_URL);
312-
Service<Request, Response> service = victim.finagle(null);
313-
314-
assertThat(service.isAvailable()).isTrue();
315-
assertThat(service.status()).hasToString("Open");
316-
317-
service.close();
318-
System.clearProperty("url");
319-
}
320-
321-
@Test
322-
void createFinagleClientWithSecurity() throws URISyntaxException {
323-
System.setProperty("url", TestConstants.HTTPS_URL);
324-
SSLFactory sslFactory = createSSLFactory(false, true);
325-
326-
Service<Request, Response> service = victim.finagle(sslFactory);
327-
328-
verify(sslFactory, times(1)).getSslContext();
329-
330-
assertThat(service.isAvailable()).isTrue();
331-
assertThat(service.status()).hasToString("Open");
332-
333-
service.close();
334-
System.clearProperty("url");
335-
}
336-
337307
@Test
338308
void createAkkaHttpClient() {
339309
SSLFactory sslFactory = createSSLFactory(false, true);

client/src/test/java/nl/altindag/client/service/FinagleHttpClientServiceShould.java

-77
This file was deleted.

0 commit comments

Comments
 (0)