1 package org.onap.sdc.dcae.catalog.commons; 
 
   3 import org.json.JSONArray;
 
   4 import org.json.JSONException;
 
   5 import org.json.JSONObject;
 
   6 import org.json.JSONTokener;
 
   7 import org.springframework.http.HttpHeaders;
 
   8 import org.springframework.http.HttpInputMessage;
 
   9 import org.springframework.http.HttpOutputMessage;
 
  10 import org.springframework.http.MediaType;
 
  11 import org.springframework.http.converter.AbstractHttpMessageConverter;
 
  12 import org.springframework.http.converter.HttpMessageNotReadableException;
 
  13 import org.springframework.http.converter.HttpMessageNotWritableException;
 
  16 import java.nio.charset.Charset;
 
  20 public class JSONHttpMessageConverter extends AbstractHttpMessageConverter<Object> { 
 
  22         public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); 
 
  25         public JSONHttpMessageConverter() { 
 
  26         super(new MediaType("application", "json", DEFAULT_CHARSET)); 
 
  30         public boolean canRead(Class<?> theClazz, MediaType theMediaType) { 
 
  31         return canRead(theMediaType); 
 
  35         public boolean canWrite(Class<?> theClazz, MediaType theMediaType) { 
 
  36         return canWrite(theMediaType); 
 
  40         protected boolean supports(Class<?> theClazz) {
 
  41                 return theClazz.equals(JSONObject.class) ||
 
  42                                          theClazz.equals(JSONArray.class);
 
  46         protected Object readInternal(Class<?> theClazz, HttpInputMessage theInputMessage) 
 
  47                                                                                                                                                                                                  throws IOException, HttpMessageNotReadableException { 
 
  49         Reader json = new InputStreamReader(theInputMessage.getBody(), getCharset(theInputMessage.getHeaders())); 
 
  52                         if (theClazz.equals(JSONObject.class))
 
  53                                 return new JSONObject(new JSONTokener(json));
 
  54                         if (theClazz.equals(JSONArray.class))
 
  55                                 return new JSONArray(new JSONTokener(json));
 
  57                         throw new HttpMessageNotReadableException("Could not process input, cannot handle " + theClazz); 
 
  59                 catch (JSONException jsonx) { 
 
  60                         throw new HttpMessageNotReadableException("Could not read JSON: " + jsonx.getMessage(), jsonx); 
 
  65         protected void writeInternal(Object theObject, HttpOutputMessage theOutputMessage) 
 
  66                                                                                                                                                                                                  throws IOException, HttpMessageNotWritableException { 
 
  68         Writer writer = new OutputStreamWriter(theOutputMessage.getBody(), getCharset(theOutputMessage.getHeaders())); 
 
  71                         if (theObject instanceof JSONObject) {
 
  72                                 ((JSONObject)theObject).write(writer);
 
  74                         else if (theObject instanceof JSONArray) {
 
  75                                 ((JSONArray)theObject).write(writer);
 
  80                 catch(JSONException jsonx) { 
 
  81                 throw new HttpMessageNotWritableException("Could not write JSON: " + jsonx.getMessage(), jsonx); 
 
  85         private Charset getCharset(HttpHeaders theHeaders) { 
 
  86         if (theHeaders != null &&
 
  87                                 theHeaders.getContentType() != null &&
 
  88                                 theHeaders.getContentType().getCharset() != null) {
 
  89                 return theHeaders.getContentType().getCharset();
 
  91         return DEFAULT_CHARSET;