com.tomtom.http.ResponseParser.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of goji-http-client Show documentation
Show all versions of goji-http-client Show documentation
A wrapper around Apache HttpClient and Jackson Databind libraries with lean Groovy syntax.
/*
* Copyright (C) 2017. TomTom International BV (http://tomtom.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tomtom.http
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.JavaType
import com.fasterxml.jackson.databind.ObjectMapper
import com.tomtom.http.response.Response
import groovy.transform.PackageScope
import org.apache.http.HttpEntity
import org.apache.http.HttpResponse
import org.apache.http.util.EntityUtils
@PackageScope
class ResponseParser {
private mapper = new ObjectMapper()
Response parse(HttpResponse response, Class type, Class subtype) {
new Response(
statusCode: response.statusLine?.statusCode,
headers: headersOf(response) as Map>,
body: bodyOf(response, type, subtype))
}
private bodyOf(HttpResponse response, Class type, Class subtype) {
def entity = response.entity as HttpEntity
entity ? readEntity(entity, type, subtype) : null
}
private readEntity(HttpEntity entity, Class rawType, Class subtype) {
def content = EntityUtils.toString entity
if (rawType) {
def type = subtype ?
typeOf(rawType, subtype) : typeOf(rawType)
try {
return mapper.readValue(content, type)
} catch (Exception e) {
println "Failed to deserialize $content to $rawType due to $e.message, defaulting to string"
}
}
content
}
private JavaType typeOf(Class type, Class subtype) {
mapper.typeFactory
.constructParametricType type, subtype
}
private JavaType typeOf(Class type) {
mapper.typeFactory
.constructType(new TypeReference
© 2015 - 2025 Weber Informatics LLC | Privacy Policy