forked from kairosdb/kairosdb-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefaultJsonResponseHandler.java
135 lines (120 loc) · 3.93 KB
/
DefaultJsonResponseHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package org.kairosdb.client.response;
import com.google.common.collect.ImmutableSet;
import com.google.common.net.MediaType;
import com.google.common.reflect.TypeToken;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import com.proofpoint.http.client.Request;
import com.proofpoint.http.client.Response;
import com.proofpoint.http.client.UnexpectedResponseException;
import org.kairosdb.client.DataPointTypeRegistry;
import org.kairosdb.client.JsonMapper;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.Set;
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
import static com.proofpoint.http.client.ResponseHandlerUtils.propagate;
import static org.weakref.jmx.internal.guava.base.Preconditions.checkNotNull;
public class DefaultJsonResponseHandler<T> implements JsonResponseHandler
{
private static final MediaType MEDIA_TYPE_JSON = MediaType.create("application", "json");
private final Set<Integer> successfulResponseCodes;
private final JsonMapper mapper;
private final Type type;
@SuppressWarnings({"unused", "WeakerAccess"})
public DefaultJsonResponseHandler(Class<T> clazz)
{
this(clazz, new DataPointTypeRegistry());
}
@SuppressWarnings({"WeakerAccess", "unused"})
public DefaultJsonResponseHandler(Type type)
{
this(type, new DataPointTypeRegistry());
}
public DefaultJsonResponseHandler(Class<T> clazz, DataPointTypeRegistry typeRegistry)
{
this(TypeToken.of(clazz).getType(), typeRegistry);
}
public DefaultJsonResponseHandler(Type type, DataPointTypeRegistry typeRegistry)
{
checkNotNull(typeRegistry, "typeRegistry must not be null");
mapper = new JsonMapper(typeRegistry);
successfulResponseCodes = ImmutableSet.of(200, 204);
this.type = checkNotNull(type, "type must not be null");
}
@Override
public T handleException(Request request, Exception e)
{
throw propagate(request, e);
}
@Override
public T handle(Request request, Response response)
{
if (!successfulResponseCodes.contains(response.getStatusCode()) && response.getStatusCode() != 400)
{
throw new UnexpectedResponseException(
String.format("Expected response code to be %s, but was %d: %s", successfulResponseCodes, response.getStatusCode(), response.getStatusMessage()),
request,
response);
}
if (response.getStatusCode() == 204) {
// Apparently some proxies/gateways return 204 but with content
return null;
}
String contentType = response.getHeader(CONTENT_TYPE);
if (contentType == null)
{
throw new UnexpectedResponseException("Content-Type is not set for response", request, response);
}
if (!MediaType.parse(contentType).is(MEDIA_TYPE_JSON))
{
throw new UnexpectedResponseException("Expected application/json response from server but got " + contentType, request, response);
}
Reader reader = null;
try{
if (response.getInputStream() == null)
{
return null;
}
reader = new InputStreamReader(response.getInputStream(), StandardCharsets.UTF_8);
if (response.getStatusCode() == 400)
{
ErrorResponse error = mapper.fromJson(reader, ErrorResponse.class);
throw new UnexpectedResponseException(
String.format("Expected response code to be %s, but was %d: %s", successfulResponseCodes, response.getStatusCode(), error.toString()),
request,
response);
}
else
{
return mapper.fromJson(reader, type);
}
}
catch (IOException e)
{
throw new RuntimeException("Error reading JSON response from server", e);
}
catch (JsonIOException | JsonSyntaxException e)
{
throw new IllegalArgumentException("Unable to create parse JSON response:\n", e);
}
finally
{
if (reader != null)
{
try
{
reader.close();
}
catch (IOException e)
{
//noinspection ThrowFromFinallyBlock
throw new RuntimeException("Error closing reader ", e);
}
}
}
}
}