Skip to content

Commit

Permalink
update name for oauth2Client
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-britton-moj committed Nov 30, 2023
1 parent 942f006 commit a68edab
Show file tree
Hide file tree
Showing 23 changed files with 62 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ import org.springframework.http.ResponseEntity
import org.springframework.http.client.ClientHttpRequestExecution
import org.springframework.http.client.ClientHttpRequestInterceptor
import org.springframework.http.client.ClientHttpResponse
import org.springframework.http.client.JdkClientHttpRequestFactory
import org.springframework.stereotype.Component
import org.springframework.web.client.RestClient
import org.springframework.web.client.RestClient.RequestHeadersSpec.ConvertibleClientHttpResponse
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody
import uk.gov.justice.digital.hmpps.exception.NotFoundException
import uk.gov.justice.digital.hmpps.security.ServiceContext
import java.net.http.HttpClient
import java.time.Duration

@Component
@ConditionalOnProperty("integrations.alfresco.url")
Expand Down Expand Up @@ -61,11 +64,16 @@ class AlfrescoClient(
class AlfrescoClientConfig(@Value("\${integrations.alfresco.url}") private val alfrescoBaseUrl: String) {
@Bean
fun alfrescoRestClient() = RestClient.builder()
.requestFactory(withTimeouts(Duration.ofSeconds(1), Duration.ofSeconds(30)))
.requestInterceptor(AlfrescoInterceptor())
.baseUrl(alfrescoBaseUrl)
.build()
}

fun withTimeouts(connection: Duration, read: Duration) =
JdkClientHttpRequestFactory(HttpClient.newBuilder().connectTimeout(connection).build())
.also { it.setReadTimeout(read) }

class AlfrescoInterceptor : ClientHttpRequestInterceptor {
override fun intercept(
request: HttpRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,31 @@ package uk.gov.justice.digital.hmpps.config.security

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.client.JdkClientHttpRequestFactory
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager
import org.springframework.web.client.RestClient
import org.springframework.web.client.RestClient.Builder
import org.springframework.web.client.support.RestClientAdapter
import org.springframework.web.service.invoker.HttpServiceProxyFactory
import java.net.http.HttpClient
import java.time.Duration

@Configuration
class HmppsAuthClientConfig(
private val restClientBuilder: Builder,
private val clientManager: OAuth2AuthorizedClientManager
) {
@Bean
fun hmppsAuthClient(): RestClient =
restClientBuilder
.requestInterceptor(HmppsAuthInterceptor(clientManager, "default"))
.build()
fun oauth2Client() = restClientBuilder
.requestFactory(withTimeouts(Duration.ofSeconds(1), Duration.ofSeconds(5)))
.requestInterceptor(HmppsAuthInterceptor(clientManager, "default"))
.build()
}

fun withTimeouts(connection: Duration, read: Duration) =
JdkClientHttpRequestFactory(HttpClient.newBuilder().connectTimeout(connection).build())
.also { it.setReadTimeout(read) }

inline fun <reified T> createClient(client: RestClient): T {
return HttpServiceProxyFactory.builderFor(RestClientAdapter.create(client)).build()
.createClient(T::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.MvcResult
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
Expand All @@ -30,6 +31,8 @@ internal class DocIntegrationTest {
mockMvc.perform(
get("/documents/A000001/uuid1").accept("application/octet-stream").withOAuth2Token(wireMockserver)
)
.andExpect(MockMvcResultMatchers.request().asyncStarted())
.andDo(MvcResult::getAsyncResult)
.andExpect(status().is2xxSuccessful)
.andExpect(header().string("Content-Type", "application/octet-stream"))
.andExpect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import uk.gov.justice.digital.hmpps.config.security.createClient
import uk.gov.justice.digital.hmpps.integrations.approvedpremises.ApprovedPremisesApiClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {
@Bean
fun approvedPremisesApiClient() = createClient<ApprovedPremisesApiClient>(hmppsAuthClient)
fun approvedPremisesApiClient() = createClient<ApprovedPremisesApiClient>(oauth2Client)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import uk.gov.justice.digital.hmpps.config.security.createClient
import uk.gov.justice.digital.hmpps.integrations.oasys.client.OasysClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {
@Bean
fun oasysClient(@Value("\${integrations.ords-oasys.url}") oasysBaseUrl: String) =
createClient<OasysClient>(hmppsAuthClient.mutate().baseUrl(oasysBaseUrl).build())
createClient<OasysClient>(oauth2Client.mutate().baseUrl(oasysBaseUrl).build())
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import uk.gov.justice.digital.hmpps.config.security.createClient
import uk.gov.justice.digital.hmpps.integrations.approvedpremesis.Cas3ApiClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {

@Bean
fun cas3ApiClient() = createClient<Cas3ApiClient>(hmppsAuthClient)
fun cas3ApiClient() = createClient<Cas3ApiClient>(oauth2Client)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import uk.gov.justice.digital.hmpps.config.security.createClient
import uk.gov.justice.digital.hmpps.integrations.courtcase.CourtCaseClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {

@Bean
fun courtCaseClient() = createClient<CourtCaseClient>(hmppsAuthClient)
fun courtCaseClient() = createClient<CourtCaseClient>(oauth2Client)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import uk.gov.justice.digital.hmpps.config.security.createClient
import uk.gov.justice.digital.hmpps.integrations.cvl.CvlClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {

@Bean
fun cvlClient() = createClient<CvlClient>(hmppsAuthClient)
fun cvlClient() = createClient<CvlClient>(oauth2Client)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import uk.gov.justice.digital.hmpps.config.security.createClient
import uk.gov.justice.digital.hmpps.integrations.prison.PrisonApiClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {

@Bean
fun prisonApiClient(@Value("\${integrations.prison-api.url}") prisonApiBaseUrl: String) =
createClient<PrisonApiClient>(
hmppsAuthClient.mutate()
oauth2Client.mutate()
.baseUrl("$prisonApiBaseUrl/api/bookings")
.build()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import uk.gov.justice.digital.hmpps.config.security.createClient
import uk.gov.justice.digital.hmpps.integrations.makerecalldecisions.MakeRecallDecisionsClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {

@Bean
fun makeRecallDecisionClient() = createClient<MakeRecallDecisionsClient>(hmppsAuthClient)
fun makeRecallDecisionClient() = createClient<MakeRecallDecisionsClient>(oauth2Client)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import uk.gov.justice.digital.hmpps.client.ManageOffencesClient
import uk.gov.justice.digital.hmpps.config.security.createClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {

@Bean
fun manageOffencesClient(@Value("\${integrations.manage-offences.url}") moBaseUrl: String) =
createClient<ManageOffencesClient>(hmppsAuthClient.mutate().baseUrl(moBaseUrl).build())
createClient<ManageOffencesClient>(oauth2Client.mutate().baseUrl(moBaseUrl).build())
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import uk.gov.justice.digital.hmpps.config.security.createClient
import uk.gov.justice.digital.hmpps.integrations.managepomcases.ManagePomCasesClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {

@Bean
fun managePomCasesClient() = createClient<ManagePomCasesClient>(hmppsAuthClient)
fun managePomCasesClient() = createClient<ManagePomCasesClient>(oauth2Client)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import uk.gov.justice.digital.hmpps.integrations.alfresco.AlfrescoUploadClient
import uk.gov.justice.digital.hmpps.integrations.psr.PsrClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {

@Bean
fun psrClient() = createClient<PsrClient>(hmppsAuthClient)
fun psrClient() = createClient<PsrClient>(oauth2Client)

@Bean
fun alfrescoUploadClient(alfrescoRestClient: RestClient) = createClient<AlfrescoUploadClient>(alfrescoRestClient)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import uk.gov.justice.digital.hmpps.config.security.createClient
import uk.gov.justice.digital.hmpps.integrations.prison.PrisonCaseNotesClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {

@Bean
fun prisonCaseNotesClient() = createClient<PrisonCaseNotesClient>(hmppsAuthClient)
fun prisonCaseNotesClient() = createClient<PrisonCaseNotesClient>(oauth2Client)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import uk.gov.justice.digital.hmpps.config.security.createClient
import uk.gov.justice.digital.hmpps.integrations.prison.PrisonApiClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {

@Bean
fun prisonApiClient(@Value("\${integrations.prison-api.url}") prisonApiBaseUrl: String) =
createClient<PrisonApiClient>(
hmppsAuthClient.mutate()
oauth2Client.mutate()
.baseUrl("$prisonApiBaseUrl/api/bookings")
.build()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import uk.gov.justice.digital.hmpps.config.security.createClient
import uk.gov.justice.digital.hmpps.integrations.example.ExampleClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {

@Bean
fun exampleClient(@Value("\${integrations.example.url}") apiBaseUrl: String): ExampleClient {
return createClient(
hmppsAuthClient.mutate()
oauth2Client.mutate()
.baseUrl(apiBaseUrl)
.build()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import uk.gov.justice.digital.hmpps.config.security.createClient
import uk.gov.justice.digital.hmpps.integrations.randm.ReferAndMonitorClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {

@Bean
fun referAndMonitorClient() = createClient<ReferAndMonitorClient>(hmppsAuthClient)
fun referAndMonitorClient() = createClient<ReferAndMonitorClient>(oauth2Client)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import uk.gov.justice.digital.hmpps.config.security.createClient
import uk.gov.justice.digital.hmpps.integrations.tier.TierClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {

@Bean
fun tierClient() = createClient<TierClient>(hmppsAuthClient)
fun tierClient() = createClient<TierClient>(oauth2Client)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import uk.gov.justice.digital.hmpps.integrations.alfresco.AlfrescoUploadClient
import uk.gov.justice.digital.hmpps.integrations.arn.ArnClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {

@Bean
fun arnClient() = createClient<ArnClient>(hmppsAuthClient)
fun arnClient() = createClient<ArnClient>(oauth2Client)

@Bean
fun alfrescoUploadClient(alfrescoRestClient: RestClient) = createClient<AlfrescoUploadClient>(alfrescoRestClient)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import uk.gov.justice.digital.hmpps.config.security.createClient
import uk.gov.justice.digital.hmpps.integrations.workforceallocations.WorkforceAllocationsClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {

@Bean
fun workforceAllocationsClient() = createClient<WorkforceAllocationsClient>(hmppsAuthClient)
fun workforceAllocationsClient() = createClient<WorkforceAllocationsClient>(oauth2Client)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import uk.gov.justice.digital.hmpps.config.security.createClient
import uk.gov.justice.digital.hmpps.integrations.example.ExampleClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {

@Bean
fun exampleClient(@Value("\${integrations.example.url}") apiBaseUrl: String): ExampleClient {
return createClient(
hmppsAuthClient.mutate()
oauth2Client.mutate()
.baseUrl(apiBaseUrl)
.build()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import uk.gov.justice.digital.hmpps.config.security.createClient
import uk.gov.justice.digital.hmpps.integrations.example.ExampleClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {

@Bean
fun exampleClient(@Value("\${integrations.example.url}") apiBaseUrl: String): ExampleClient {
return createClient(
hmppsAuthClient.mutate()
oauth2Client.mutate()
.baseUrl(apiBaseUrl)
.build()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import uk.gov.justice.digital.hmpps.config.security.createClient
import uk.gov.justice.digital.hmpps.integrations.example.ExampleClient

@Configuration
class RestClientConfig(private val hmppsAuthClient: RestClient) {
class RestClientConfig(private val oauth2Client: RestClient) {

@Bean
fun exampleClient(@Value("\${integrations.example.url}") apiBaseUrl: String): ExampleClient {
return createClient(
hmppsAuthClient.mutate()
oauth2Client.mutate()
.baseUrl(apiBaseUrl)
.build()
)
Expand Down

0 comments on commit a68edab

Please sign in to comment.