Skip to content

Commit 68e668f

Browse files
blockvotemduesterhoeft
authored andcommitted
Use Apache ContentType instead of Guava MimeType (#2)
* Use Apache ContentType instead of Guava MimeType * Use Apache ContentType instead of Guava MimeType in ProtoSerializationHandler
1 parent cdf98d1 commit 68e668f

File tree

7 files changed

+41
-25
lines changed

7 files changed

+41
-25
lines changed

router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoSerializationHandler.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package io.moia.router.proto
22

3+
import com.google.protobuf.GeneratedMessageV3
34
import io.moia.router.ResponseEntity
45
import io.moia.router.SerializationHandler
5-
import com.google.common.net.MediaType
6-
import com.google.protobuf.GeneratedMessageV3
6+
import org.apache.http.entity.ContentType
77
import java.util.Base64
88

99
class ProtoSerializationHandler : SerializationHandler {
1010

11-
private val json = MediaType.parse("application/json")
11+
private val json = ContentType.parse("application/json")
1212

13-
override fun supports(acceptHeader: MediaType, response: ResponseEntity<*>): Boolean =
13+
override fun supports(acceptHeader: ContentType, response: ResponseEntity<*>): Boolean =
1414
response.body is GeneratedMessageV3
1515

16-
override fun serialize(acceptHeader: MediaType, response: ResponseEntity<*>): String {
16+
override fun serialize(acceptHeader: ContentType, response: ResponseEntity<*>): String {
1717
val message = response.body as GeneratedMessageV3
18-
return if (acceptHeader.`is`(json)) {
18+
return if (json.mimeType == acceptHeader.mimeType) {
1919
ProtoBufUtils.toJsonWithoutWrappers(message)
2020
} else {
2121
Base64.getEncoder().encodeToString(message.toByteArray())

router/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ dependencies {
99
compile("org.slf4j:slf4j-api:1.7.26")
1010
compile("com.fasterxml.jackson.core:jackson-databind:2.9.8")
1111
compile("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.8")
12-
compile("com.google.guava:guava:23.0")
12+
compile("org.apache.httpcomponents:httpcore:4.4.11")
1313

1414
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.4.0")
1515
testImplementation("org.junit.jupiter:junit-jupiter-params:5.4.0")

router/src/main/kotlin/io/moia/router/DeserializationHandler.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package io.moia.router
33
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
44
import com.fasterxml.jackson.databind.ObjectMapper
55
import com.fasterxml.jackson.databind.type.TypeFactory
6-
import com.google.common.net.MediaType
6+
import org.apache.http.entity.ContentType
77
import kotlin.reflect.KClass
88
import kotlin.reflect.KType
99
import kotlin.reflect.full.isSubclassOf
@@ -25,12 +25,12 @@ class DeserializationHandlerChain(private val handlers: List<DeserializationHand
2525
handlers.firstOrNull { it.supports(input) }?.deserialize(input, target)
2626
}
2727

28-
class JsonDeserializationHandler(val objectMapper: ObjectMapper) : DeserializationHandler {
28+
class JsonDeserializationHandler(private val objectMapper: ObjectMapper) : DeserializationHandler {
2929

30-
private val json = MediaType.parse("application/json")
30+
private val json = ContentType.parse("application/json")
3131

3232
override fun supports(input: APIGatewayProxyRequestEvent) =
33-
input.contentType() != null && MediaType.parse(input.contentType()).`is`(json)
33+
input.contentType() != null && ContentType.parse(input.contentType()).mimeType == json.mimeType
3434

3535
override fun deserialize(input: APIGatewayProxyRequestEvent, target: KType?): Any? {
3636
val targetClass = target?.classifier as KClass<*>

router/src/main/kotlin/io/moia/router/RequestHandler.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
66
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent
77
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
88
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
9-
import com.google.common.net.MediaType
9+
import org.apache.http.entity.ContentType
1010
import org.slf4j.Logger
1111
import org.slf4j.LoggerFactory
1212
import kotlin.reflect.KClass
@@ -145,7 +145,7 @@ abstract class RequestHandler : RequestHandler<APIGatewayProxyRequestEvent, APIG
145145

146146
open fun <T> createResponse(input: APIGatewayProxyRequestEvent, response: ResponseEntity<T>): APIGatewayProxyResponseEvent {
147147
// TODO add default accept type
148-
val accept = MediaType.parse(input.acceptHeader())
148+
val accept = ContentType.parse(input.acceptHeader())
149149
return when {
150150
response.body is Unit -> APIGatewayProxyResponseEvent()
151151
.withStatusCode(204)

router/src/main/kotlin/io/moia/router/RequestPredicate.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.moia.router
22

33
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
4-
import com.google.common.net.MediaType
4+
import org.apache.http.entity.ContentType
55

66
data class RequestPredicate(
77
val method: String,
@@ -34,7 +34,7 @@ data class RequestPredicate(
3434
private fun contentTypeMatches(contentType: String?, accepted: Set<String>) =
3535
if (accepted.isEmpty() && contentType == null) true
3636
else if (contentType == null) false
37-
else accepted.any { MediaType.parse(contentType).`is`(MediaType.parse(it)) }
37+
else accepted.any { ContentType.parse(contentType).mimeType == ContentType.parse(it).mimeType }
3838

3939
companion object
4040
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
package io.moia.router
22

33
import com.fasterxml.jackson.databind.ObjectMapper
4-
import com.google.common.net.MediaType
4+
import org.apache.http.entity.ContentType
55

66
interface SerializationHandler {
77

8-
fun supports(acceptHeader: MediaType, response: ResponseEntity<*>): Boolean
8+
fun supports(acceptHeader: ContentType, response: ResponseEntity<*>): Boolean
99

10-
fun serialize(acceptHeader: MediaType, response: ResponseEntity<*>): String
10+
fun serialize(acceptHeader: ContentType, response: ResponseEntity<*>): String
1111
}
1212

1313
class SerializationHandlerChain(private val handlers: List<SerializationHandler>) :
1414
SerializationHandler {
1515

16-
override fun supports(acceptHeader: MediaType, response: ResponseEntity<*>): Boolean =
16+
override fun supports(acceptHeader: ContentType, response: ResponseEntity<*>): Boolean =
1717
handlers.any { it.supports(acceptHeader, response) }
1818

19-
override fun serialize(acceptHeader: MediaType, response: ResponseEntity<*>): String =
19+
override fun serialize(acceptHeader: ContentType, response: ResponseEntity<*>): String =
2020
handlers.first { it.supports(acceptHeader, response) }.serialize(acceptHeader, response)
2121
}
2222

23-
class JsonSerializationHandler(val objectMapper: ObjectMapper) : SerializationHandler {
23+
class JsonSerializationHandler(private val objectMapper: ObjectMapper) : SerializationHandler {
2424

25-
private val json = MediaType.parse("application/json")
25+
private val json = ContentType.parse("application/json")
2626

27-
override fun supports(acceptHeader: MediaType, response: ResponseEntity<*>): Boolean = acceptHeader.`is`(json)
27+
override fun supports(acceptHeader: ContentType, response: ResponseEntity<*>): Boolean = acceptHeader.mimeType == json.mimeType
2828

29-
override fun serialize(acceptHeader: MediaType, response: ResponseEntity<*>): String =
29+
override fun serialize(acceptHeader: ContentType, response: ResponseEntity<*>): String =
3030
objectMapper.writeValueAsString(response.body)
3131
}

router/src/test/kotlin/io/moia/router/RequestHandlerTest.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import org.junit.jupiter.api.Test
99

1010
class RequestHandlerTest {
1111

12-
val testRequestHandler = TestRequestHandler()
12+
private val testRequestHandler = TestRequestHandler()
1313

1414
@Test
1515
fun `should match request`() {
@@ -186,6 +186,22 @@ class RequestHandlerTest {
186186
assert(response.statusCode).isEqualTo(500)
187187
}
188188

189+
@Test
190+
fun `should handle request with a long accept header`() {
191+
192+
val response = testRequestHandler.handleRequest(
193+
POST("/some")
194+
.withHeaders(mapOf(
195+
"Accept" to "application/json, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8",
196+
"Content-Type" to "application/json"
197+
))
198+
.withBody("""{ "greeting": "some" }"""), mockk()
199+
)
200+
201+
assert(response.statusCode).isEqualTo(200)
202+
assert(response.body).isEqualTo("""{"greeting":"some"}""")
203+
}
204+
189205
class TestRequestHandlerWithFilter : RequestHandler() {
190206

191207
var filterInvocations = 0

0 commit comments

Comments
 (0)