|
| 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 | +} |
0 commit comments