/src/main/java/org/exoplatform/social/client/api/util/SocialJSONDecodingSupport.java

http://github.com/exosocial/exo.social.client · Java · 178 lines · 87 code · 9 blank · 82 comment · 7 complexity · 5b399d1e16b60691c946cf7ec0c84309 MD5 · raw file

  1. /*
  2. * Copyright (C) 2003-2011 eXo Platform SAS.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Affero General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package org.exoplatform.social.client.api.util;
  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.LinkedHashMap;
  21. import java.util.LinkedList;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.apache.http.HttpEntity;
  25. import org.apache.http.HttpResponse;
  26. import org.apache.http.util.EntityUtils;
  27. import org.exoplatform.social.client.api.model.Model;
  28. import org.exoplatform.social.client.api.net.SocialHttpClientException;
  29. import org.json.simple.JSONArray;
  30. import org.json.simple.JSONValue;
  31. import org.json.simple.parser.ContainerFactory;
  32. import org.json.simple.parser.JSONParser;
  33. import org.json.simple.parser.ParseException;
  34. /**
  35. * Created by The eXo Platform SAS
  36. * Author : eXoPlatform
  37. * exo@exoplatform.com
  38. * Jun 30, 2011
  39. */
  40. public class SocialJSONDecodingSupport {
  41. /**
  42. * Parse JSON text into java Model object from the input source.
  43. * and then it's base on the class type.
  44. *
  45. * @param <T> Generic type must extend from Model.
  46. * @param clazz Class type.
  47. * @param jsonContent Json content which you need to create the Model
  48. * @throws ParseException Throw this exception if any
  49. *
  50. */
  51. public static <T extends Model> T parser(final Class<T> clazz, String jsonContent) throws ParseException {
  52. JSONParser parser = new JSONParser();
  53. ContainerFactory containerFactory = new ContainerFactory() {
  54. public List<T> creatArrayContainer() {
  55. return new LinkedList<T>();
  56. }
  57. public T createObjectContainer() {
  58. try {
  59. return clazz.newInstance();
  60. } catch (InstantiationException e) {
  61. return null;
  62. } catch (IllegalAccessException e) {
  63. return null;
  64. }
  65. }
  66. };
  67. return (T) parser.parse(jsonContent, containerFactory);
  68. }
  69. /**
  70. * Parse JSON text into java Model object from the input source.
  71. * and then it's base on the class type.
  72. *
  73. * @param <T> Generic type must extend from Model.
  74. * @param clazz Class type.
  75. * @param response HttpResponse which getting the JSONContent.
  76. * @throws ParseException Throw this exception if any
  77. * @throws IOException Throw this exception if any
  78. */
  79. public static <T extends Model> T parser(final Class<T> clazz, HttpResponse response) throws IOException, ParseException {
  80. //Read InputStream from HttpResponse to Buffered
  81. HttpEntity entity = SocialHttpClientSupport.processContent(response);
  82. //Check the content length
  83. if (entity.getContentLength() != -1) {
  84. //getting the HttpResponse content
  85. String jsonContent = EntityUtils.toString(entity);
  86. //close stream
  87. SocialHttpClientSupport.consume(entity);
  88. return parser(clazz, jsonContent);
  89. } else {
  90. return null;
  91. }
  92. }
  93. /**
  94. * Parse JSON text into java Map object from the input source.
  95. *
  96. * @param jsonContent Json content which you need to create the Model
  97. * @throws ParseException Throw this exception if any
  98. */
  99. public static Map parser(String jsonContent) throws ParseException {
  100. JSONParser parser = new JSONParser();
  101. ContainerFactory containerFactory = new ContainerFactory() {
  102. public List creatArrayContainer() {
  103. return new LinkedList();
  104. }
  105. public Map createObjectContainer() {
  106. return new LinkedHashMap();
  107. }
  108. };
  109. return (Map) parser.parse(jsonContent, containerFactory);
  110. }
  111. /**
  112. * HttpResponse text into java Map object from the input source.
  113. *
  114. * @param response HttpResponse to get the content.
  115. * @throws ParseException Throw this exception if any
  116. */
  117. public static Map parser(HttpResponse response) throws ParseException {
  118. String jsonContent = null;
  119. try {
  120. jsonContent = SocialHttpClientSupport.getContent(response);
  121. } catch (SocialHttpClientException e) {
  122. throw new ParseException(0);
  123. }
  124. return parser(jsonContent);
  125. }
  126. /**
  127. *
  128. * @author Ly Minh Phuong - http://phuonglm.net
  129. * @param <T>
  130. * @param clazz
  131. * @param response
  132. * @return
  133. * @throws IOException
  134. * @throws ParseException
  135. */
  136. public static <T extends Model > List<T> JSONArrayObjectParser(final Class<T> clazz, HttpResponse response) throws IOException, ParseException{
  137. //Read InputStream from HttpResponse to Buffered
  138. HttpEntity entity = SocialHttpClientSupport.processContent(response);
  139. //Check the content length
  140. if (entity.getContentLength() != -1) {
  141. //getting the HttpResponse content
  142. String jsonContent = EntityUtils.toString(entity);
  143. //close stream
  144. SocialHttpClientSupport.consume(entity);
  145. return JSONArrayObjectParser(clazz, jsonContent);
  146. } else {
  147. return null;
  148. }
  149. }
  150. /**
  151. *
  152. * @author Ly Minh Phuong - http://phuonglm.net
  153. * @param <T>
  154. * @param clazz
  155. * @param jsonArrayContent
  156. * @return
  157. * @throws IOException
  158. * @throws ParseException
  159. */
  160. public static <T extends Model > List<T> JSONArrayObjectParser(final Class<T> clazz, String jsonArrayContent) throws IOException, ParseException{
  161. JSONArray jsonResultArray = (JSONArray)JSONValue.parse(jsonArrayContent);
  162. List<T> result = new ArrayList<T>();
  163. for (Object jsonObject : jsonResultArray) {
  164. String jsonString = jsonObject.toString();
  165. result.add(parser(clazz,jsonString));
  166. }
  167. return result;
  168. }
  169. }