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
|
package org.onap.sdc.dcae.catalog.commons;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONTokener;
import org.json.JSONException;
/**
*/
public class JSONHttpMessageConverter extends AbstractHttpMessageConverter<Object> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
/** */
public JSONHttpMessageConverter() {
super(new MediaType("application", "json", DEFAULT_CHARSET));
}
/*
@Override
public boolean canRead(Class<?> theClazz, MediaType theMediaType) {
return canRead(theMediaType);
}
@Override
public boolean canWrite(Class<?> theClazz, MediaType theMediaType) {
return canWrite(theMediaType);
}
*/
@Override
protected boolean supports(Class<?> theClazz) {
return theClazz.equals(JSONObject.class) ||
theClazz.equals(JSONArray.class);
}
@Override
protected Object readInternal(Class<?> theClazz, HttpInputMessage theInputMessage)
throws IOException, HttpMessageNotReadableException {
Reader json = new InputStreamReader(theInputMessage.getBody(), getCharset(theInputMessage.getHeaders()));
try {
if (theClazz.equals(JSONObject.class))
return new JSONObject(new JSONTokener(json));
if (theClazz.equals(JSONArray.class))
return new JSONArray(new JSONTokener(json));
throw new HttpMessageNotReadableException("Could not process input, cannot handle " + theClazz);
}
catch (JSONException jsonx) {
throw new HttpMessageNotReadableException("Could not read JSON: " + jsonx.getMessage(), jsonx);
}
}
@Override
protected void writeInternal(Object theObject, HttpOutputMessage theOutputMessage)
throws IOException, HttpMessageNotWritableException {
Writer writer = new OutputStreamWriter(theOutputMessage.getBody(), getCharset(theOutputMessage.getHeaders()));
try {
if (theObject instanceof JSONObject) {
((JSONObject)theObject).write(writer);
}
else if (theObject instanceof JSONArray) {
((JSONArray)theObject).write(writer);
}
writer.close();
}
catch(JSONException jsonx) {
throw new HttpMessageNotWritableException("Could not write JSON: " + jsonx.getMessage(), jsonx);
}
}
private Charset getCharset(HttpHeaders theHeaders) {
if (theHeaders != null &&
theHeaders.getContentType() != null &&
theHeaders.getContentType().getCharSet() != null) {
return theHeaders.getContentType().getCharSet();
}
return DEFAULT_CHARSET;
}
}
|