a2ad7d3edab889936da9d9c606ccfde9886ef59f
[sdc/dcae-d/dt-be-main.git] /
1 package org.onap.sdc.dcae.catalog.commons; 
2  
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;
14
15 import java.io.*;
16 import java.nio.charset.Charset;
17  
18 /**
19  */ 
20 public class JSONHttpMessageConverter extends AbstractHttpMessageConverter<Object> { 
21  
22         public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); 
23
24         /** */ 
25         public JSONHttpMessageConverter() { 
26         super(new MediaType("application", "json", DEFAULT_CHARSET)); 
27         } 
28  /* 
29         @Override 
30         public boolean canRead(Class<?> theClazz, MediaType theMediaType) { 
31         return canRead(theMediaType); 
32         } 
33  
34         @Override 
35         public boolean canWrite(Class<?> theClazz, MediaType theMediaType) { 
36         return canWrite(theMediaType); 
37         } 
38  */
39         @Override 
40         protected boolean supports(Class<?> theClazz) {
41                 return theClazz.equals(JSONObject.class) ||
42                                          theClazz.equals(JSONArray.class);
43         } 
44  
45         @Override 
46         protected Object readInternal(Class<?> theClazz, HttpInputMessage theInputMessage) 
47                                                                                                                                                                                                  throws IOException, HttpMessageNotReadableException { 
48    
49         Reader json = new InputStreamReader(theInputMessage.getBody(), getCharset(theInputMessage.getHeaders())); 
50    
51                 try {
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));
56                         
57                         throw new HttpMessageNotReadableException("Could not process input, cannot handle " + theClazz); 
58                 }
59                 catch (JSONException jsonx) { 
60                         throw new HttpMessageNotReadableException("Could not read JSON: " + jsonx.getMessage(), jsonx); 
61                 } 
62         } 
63  
64         @Override 
65         protected void writeInternal(Object theObject, HttpOutputMessage theOutputMessage) 
66                                                                                                                                                                                                  throws IOException, HttpMessageNotWritableException { 
67    
68         Writer writer = new OutputStreamWriter(theOutputMessage.getBody(), getCharset(theOutputMessage.getHeaders())); 
69  
70                 try {
71                         if (theObject instanceof JSONObject) {
72                                 ((JSONObject)theObject).write(writer);
73                         }
74                         else if (theObject instanceof JSONArray) {
75                                 ((JSONArray)theObject).write(writer);
76                         }
77
78                         writer.close();
79         }
80                 catch(JSONException jsonx) { 
81                 throw new HttpMessageNotWritableException("Could not write JSON: " + jsonx.getMessage(), jsonx); 
82         }  
83         } 
84   
85         private Charset getCharset(HttpHeaders theHeaders) { 
86         if (theHeaders != null &&
87                                 theHeaders.getContentType() != null &&
88                                 theHeaders.getContentType().getCharset() != null) {
89                 return theHeaders.getContentType().getCharset();
90         } 
91         return DEFAULT_CHARSET; 
92         } 
93  
94 }