Skip to content

Commit 60b0c7a

Browse files
authored
Add string extension for decoding URLs (#89)
* Add string extension for decoding URLs * Refactor Person controller to use decodeUrl string extension * Rename decodeUrl to decodeUrlCharacters * Add test to confirm returns string back when no encoded URL characters
1 parent 3c382b7 commit 60b0c7a

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/controllers/PersonController.kt

+6-7
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ import org.springframework.web.bind.annotation.RequestMapping
88
import org.springframework.web.bind.annotation.RequestParam
99
import org.springframework.web.bind.annotation.RestController
1010
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.exception.EntityNotFoundException
11+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.decodeUrlCharacters
1112
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.Address
1213
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.ImageMetadata
1314
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.Person
1415
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetAddressesForPersonService
1516
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetImageMetadataForPersonService
1617
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetPersonService
1718
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetPersonsService
18-
import java.net.URLDecoder
19-
import java.nio.charset.StandardCharsets
2019

2120
@RestController
2221
@RequestMapping("/persons")
@@ -43,7 +42,7 @@ class PersonController(
4342

4443
@GetMapping("{encodedPncId}")
4544
fun getPerson(@PathVariable encodedPncId: String): Map<String, Person?> {
46-
val pncId = URLDecoder.decode(encodedPncId, StandardCharsets.UTF_8)
45+
val pncId = encodedPncId.decodeUrlCharacters()
4746
val result = getPersonService.execute(pncId)
4847

4948
if (result.isNullOrEmpty()) {
@@ -55,17 +54,17 @@ class PersonController(
5554

5655
@GetMapping("{encodedPncId}/images")
5756
fun getPersonImages(@PathVariable encodedPncId: String): Map<String, List<ImageMetadata>> {
58-
val pncId = URLDecoder.decode(encodedPncId, StandardCharsets.UTF_8)
57+
val pncId = encodedPncId.decodeUrlCharacters()
5958
val images = getImageMetadataForPersonService.execute(pncId)
6059

6160
return mapOf("images" to images)
6261
}
6362

6463
@GetMapping("{encodedPncId}/addresses")
6564
fun getPersonAddresses(@PathVariable encodedPncId: String): Map<String, List<Address>> {
66-
val pncId = URLDecoder.decode(encodedPncId, StandardCharsets.UTF_8)
67-
val addresses =
68-
getAddressesForPersonService.execute(pncId) ?: throw EntityNotFoundException("Could not find person with id: $pncId")
65+
val pncId = encodedPncId.decodeUrlCharacters()
66+
val addresses = getAddressesForPersonService.execute(pncId)
67+
?: throw EntityNotFoundException("Could not find person with id: $pncId")
6968

7069
return mapOf("addresses" to addresses)
7170
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions
22

3+
import java.net.URLDecoder
4+
import java.nio.charset.StandardCharsets
5+
36
fun String.removeWhitespaceAndNewlines(): String = this.replace("(\"[^\"]*\")|\\s".toRegex(), "\$1")
7+
8+
fun String.decodeUrlCharacters(): String = URLDecoder.decode(this, StandardCharsets.UTF_8)

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/extensions/StringExtensionTest.kt

+10
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,14 @@ class StringExtensionTest : DescribeSpec({
2323
""".removeWhitespaceAndNewlines().shouldBe("{\"cat\":\"meow meow\"}")
2424
}
2525
}
26+
27+
describe("#decodeUrlCharacters") {
28+
it("decodes URL encoded string") {
29+
"never%21gonna%26give%2Fyou%23up".decodeUrlCharacters().shouldBe("never!gonna&give/you#up")
30+
}
31+
32+
it("returns same string when no URL encoded characters") {
33+
"never/gonna/give/you/up".decodeUrlCharacters().shouldBe("never/gonna/give/you/up")
34+
}
35+
}
2636
},)

0 commit comments

Comments
 (0)