@@ -6,57 +6,53 @@ import okhttp3.logging.HttpLoggingInterceptor.Level
6
6
import org.slf4j.LoggerFactory
7
7
import org.springframework.beans.factory.annotation.Autowired
8
8
import org.springframework.beans.factory.annotation.Value
9
+ import org.springframework.context.annotation.Bean
9
10
import org.springframework.context.annotation.Configuration
10
11
import retrofit2.Retrofit
11
12
import retrofit2.converter.jaxb.JaxbConverterFactory
13
+ import uk.gov.justice.digital.hmpps.learnerrecordsapi.interfaces.LRSApiInterface
12
14
import java.util.concurrent.TimeUnit
13
15
14
16
@Configuration
15
- class HttpClientConfiguration (
16
- @Value(" \$ {lrs.pfx-path}" ) val pfxFilePath : String ,
17
- @Value(" \$ {lrs.base-url}" ) val baseUrl : String ,
17
+ class HttpClientConfiguration {
18
+
19
+ @Value(" \$ {lrs.pfx-path}" )
20
+ lateinit var pfxFilePath: String
21
+
22
+ @Value(" \$ {lrs.base-url}" )
23
+ lateinit var baseUrl: String
24
+
18
25
@Autowired
19
- private val appConfig : AppConfig ,
20
- ) {
21
- fun buildSSLHttpClient (): OkHttpClient {
26
+ lateinit var appConfig: AppConfig
27
+
28
+ @Bean
29
+ fun lrsClient (): LRSApiInterface = Retrofit .Builder ()
30
+ .baseUrl(baseUrl)
31
+ .client(sslHttpClient())
32
+ .addConverterFactory(JaxbConverterFactory .create())
33
+ .build().create(LRSApiInterface ::class .java)
34
+
35
+ companion object {
36
+ private val log = LoggerFactory .getLogger(this ::class .java)
37
+ }
38
+
39
+ fun sslHttpClient (): OkHttpClient {
22
40
log.info(" Building HTTP client with SSL" )
23
41
val loggingInterceptor = HttpLoggingInterceptor ()
24
42
loggingInterceptor.level = Level .BODY
25
43
26
- try {
27
- val sslContextConfiguration = SSLContextConfiguration (pfxFilePath)
28
- val sslContext = sslContextConfiguration.createSSLContext()
29
- val trustManager = sslContextConfiguration.getTrustManager()
30
-
31
- val httpClientBuilder = OkHttpClient .Builder ()
32
- .connectTimeout(appConfig.lrsConnectTimeout(), TimeUnit .SECONDS )
33
- .writeTimeout(appConfig.lrsWriteTimeout(), TimeUnit .SECONDS )
34
- .readTimeout(appConfig.lrsReadTimeout(), TimeUnit .SECONDS )
35
- .sslSocketFactory(sslContext.socketFactory, trustManager)
36
- .addInterceptor(loggingInterceptor)
37
-
38
- log.info(" HTTP client with SSL built successfully!" )
39
- return httpClientBuilder.build()
40
- } catch (e: Exception ) {
41
- log.info(e.message + " Falling back to HTTP client without SSL" )
42
- val httpClientBuilder = OkHttpClient .Builder ()
43
- .addInterceptor(loggingInterceptor)
44
-
45
- return httpClientBuilder.build()
46
- }
47
- }
44
+ val sslContextConfiguration = SSLContextConfiguration (pfxFilePath)
45
+ val sslContext = sslContextConfiguration.createSSLContext()
46
+ val trustManager = sslContextConfiguration.getTrustManager()
48
47
49
- fun retrofit (): Retrofit {
50
- log.info(" Retrofit Client" )
48
+ val httpClientBuilder = OkHttpClient .Builder ()
49
+ .connectTimeout(appConfig.lrsConnectTimeout(), TimeUnit .SECONDS )
50
+ .writeTimeout(appConfig.lrsWriteTimeout(), TimeUnit .SECONDS )
51
+ .readTimeout(appConfig.lrsReadTimeout(), TimeUnit .SECONDS )
52
+ .sslSocketFactory(sslContext.socketFactory, trustManager)
53
+ .addInterceptor(loggingInterceptor)
51
54
52
- return Retrofit .Builder ()
53
- .baseUrl(baseUrl)
54
- .client(buildSSLHttpClient())
55
- .addConverterFactory(JaxbConverterFactory .create())
56
- .build()
57
- }
58
-
59
- companion object {
60
- private val log = LoggerFactory .getLogger(this ::class .java)
55
+ log.info(" HTTP client with SSL built successfully!" )
56
+ return httpClientBuilder.build()
61
57
}
62
58
}
0 commit comments