Skip to content

Commit

Permalink
chore: use internal endpoint in harvest-admin
Browse files Browse the repository at this point in the history
  • Loading branch information
NilsOveTen committed Jun 4, 2024
1 parent f3fdcf7 commit 2e05f03
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import java.io.BufferedReader
import java.net.HttpURLConnection
import java.net.URI
import java.net.URL

private const val TEN_MINUTES = 600000
Expand All @@ -14,7 +15,7 @@ private const val TEN_MINUTES = 600000
class DataServiceAdapter {

fun getDataServices(source: HarvestDataSource): String {
val connection = URL(source.url).openConnection() as HttpURLConnection
val connection = URI(source.url).toURL().openConnection() as HttpURLConnection
connection.setRequestProperty("Accept", source.acceptHeaderValue)
connection.connectTimeout = TEN_MINUTES
connection.readTimeout = TEN_MINUTES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.springframework.http.*
import org.springframework.stereotype.Service
import java.io.BufferedReader
import java.net.HttpURLConnection
import java.net.URI
import java.net.URL

private val logger = LoggerFactory.getLogger(HarvestAdminAdapter::class.java)
Expand All @@ -19,8 +20,8 @@ class HarvestAdminAdapter(private val applicationProperties: ApplicationProperti

fun urlWithParameters(params: HarvestAdminParameters): URL {
val pathString: String = when {
params.publisherId.isNullOrBlank() -> "/datasources"
else -> "/organizations/${params.publisherId}/datasources"
params.publisherId.isNullOrBlank() -> "/internal/datasources"
else -> "/internal/organizations/${params.publisherId}/datasources"
}

val paramString: String = when {
Expand All @@ -32,12 +33,12 @@ class HarvestAdminAdapter(private val applicationProperties: ApplicationProperti
else -> ""
}

return URL("${applicationProperties.harvestAdminRootUrl}$pathString$paramString")
return URI("${applicationProperties.harvestAdminRootUrl}$pathString$paramString").toURL()
}

private fun urlForSingleDataSource(params: HarvestAdminParameters): URL {
val path = "/organizations/${params.publisherId}/datasources/${params.dataSourceId}"
return URL("${applicationProperties.harvestAdminRootUrl}$path")
val path = "/internal/organizations/${params.publisherId}/datasources/${params.dataSourceId}"
return URI("${applicationProperties.harvestAdminRootUrl}$path").toURL()
}

fun getDataSources(params: HarvestAdminParameters): List<HarvestDataSource> =
Expand All @@ -57,6 +58,7 @@ class HarvestAdminAdapter(private val applicationProperties: ApplicationProperti
try {
setRequestProperty(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON.toString())
setRequestProperty(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON.toString())
setRequestProperty("X-API-KEY", applicationProperties.harvestAdminApiKey)

if (HttpStatus.valueOf(responseCode).is2xxSuccessful) {
return inputStream.bufferedReader().use(BufferedReader::readText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import org.springframework.boot.context.properties.ConfigurationProperties
data class ApplicationProperties(
val dataserviceUri: String,
val catalogUri: String,
val harvestAdminRootUrl: String
val harvestAdminRootUrl: String,
val harvestAdminApiKey: String
)
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ application:
dataserviceUri: ${FDK_DATASERVICE_HARVESTER_URI:https://dataservices.staging.fellesdatakatalog.digdir.no}/dataservices
catalogUri: ${FDK_DATASERVICE_HARVESTER_URI:https://dataservices.staging.fellesdatakatalog.digdir.no}/catalogs
harvestAdminRootUrl: ${HARVEST_ADMIN_ROOT_URL:http://new-harvest-admin:8080}
harvestAdminApiKey: ${ADMIN_API_KEY}
spring:
security.oauth2.resourceserver.jwt:
jwk-set-uri: ${SSO_HOST:https://sso.staging.fellesdatakatalog.digdir.no}/auth/realms/fdk/protocol/openid-connect/certs
Expand Down Expand Up @@ -44,6 +45,7 @@ application:
dataserviceUri: https://dataservices.staging.fellesdatakatalog.digdir.no/dataservices
catalogUri: https://dataservices.staging.fellesdatakatalog.digdir.no/catalogs
harvestAdminRootUrl: https://admin-api.staging.fellesdatakatalog.digdir.no
harvestAdminApiKey: test-key

---
spring:
Expand All @@ -60,4 +62,5 @@ application:
dataserviceUri: http://localhost:5050/dataservices
catalogUri: http://localhost:5050/catalogs
harvestAdminRootUrl: http://localhost:5050
harvestAdminApiKey: test-key
server.port: 5555
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class HarvestAdminAdapterTest {

val url = adapter.urlWithParameters(HarvestAdminParameters(null, null, null, null))

assertEquals(URL("http://www.example.com/datasources"), url)
assertEquals(URL("http://www.example.com/internal/datasources"), url)
}

@Test
Expand All @@ -36,7 +36,7 @@ class HarvestAdminAdapterTest {

val url = adapter.urlWithParameters(HarvestAdminParameters("", "", "", ""))

assertEquals(URL("http://www.example.com/datasources"), url)
assertEquals(URL("http://www.example.com/internal/datasources"), url)
}

@Test
Expand All @@ -46,7 +46,7 @@ class HarvestAdminAdapterTest {

val url = adapter.urlWithParameters(HarvestAdminParameters(null, null, null))

assertEquals(URL("http://www.example.com/datasources?dataType=dataservice"), url)
assertEquals(URL("http://www.example.com/internal/datasources?dataType=dataservice"), url)
}

@Test
Expand All @@ -63,7 +63,7 @@ class HarvestAdminAdapterTest {
)
)

assertEquals(URL("http://www.example.com/organizations/123456789/datasources?dataType=dataservice&dataSourceType=DCAT-AP-NO"), url)
assertEquals(URL("http://www.example.com/internal/organizations/123456789/datasources?dataType=dataservice&dataSourceType=DCAT-AP-NO"), url)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fun startMockServer() {
.willReturn(aResponse()
.withStatus(200))
)
mockserver.stubFor(get(urlEqualTo("/datasources?dataType=dataservice"))
mockserver.stubFor(get(urlEqualTo("/internal/datasources?dataType=dataservice"))
.willReturn(okJson(jacksonObjectMapper().writeValueAsString(listOf(TEST_HARVEST_SOURCE, ERROR_HARVEST_SOURCE))))
)
mockserver.stubFor(get(urlMatching("/harvest"))
Expand Down

0 comments on commit 2e05f03

Please sign in to comment.