PageRenderTime 42ms CodeModel.GetById 33ms app.highlight 7ms RepoModel.GetById 0ms app.codeStats 0ms

/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
Possible License(s): AGPL-3.0
  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 */
 17package org.exoplatform.social.client.api.util;
 18
 19import java.io.IOException;
 20import java.util.ArrayList;
 21import java.util.LinkedHashMap;
 22import java.util.LinkedList;
 23import java.util.List;
 24import java.util.Map;
 25import org.apache.http.HttpEntity;
 26import org.apache.http.HttpResponse;
 27import org.apache.http.util.EntityUtils;
 28import org.exoplatform.social.client.api.model.Model;
 29import org.exoplatform.social.client.api.net.SocialHttpClientException;
 30import org.json.simple.JSONArray;
 31import org.json.simple.JSONValue;
 32import org.json.simple.parser.ContainerFactory;
 33import org.json.simple.parser.JSONParser;
 34import org.json.simple.parser.ParseException;
 35
 36/**
 37 * Created by The eXo Platform SAS
 38 * Author : eXoPlatform
 39 *          exo@exoplatform.com
 40 * Jun 30, 2011  
 41 */
 42public class SocialJSONDecodingSupport {
 43  /**
 44   * Parse JSON text into java Model object from the input source.
 45   * and then it's base on the class type.
 46   * 
 47   * @param <T> Generic type must extend from Model.
 48   * @param clazz Class type.
 49   * @param jsonContent Json content which you need to create the Model
 50   * @throws ParseException Throw this exception if any
 51   * 
 52   */
 53  public static <T extends Model> T parser(final Class<T> clazz, String jsonContent) throws ParseException {
 54    JSONParser parser = new JSONParser();
 55    ContainerFactory containerFactory = new ContainerFactory() {
 56      public List<T> creatArrayContainer() {
 57        return new LinkedList<T>();
 58      }
 59
 60      public T createObjectContainer() {
 61        try {
 62          return clazz.newInstance();
 63        } catch (InstantiationException e) {
 64          return null;
 65        } catch (IllegalAccessException e) {
 66          return null;
 67        }
 68      }
 69    };
 70    return (T) parser.parse(jsonContent, containerFactory);
 71  }
 72  
 73  /**
 74   * Parse JSON text into java Model object from the input source.
 75   * and then it's base on the class type.
 76   * 
 77   * @param <T> Generic type must extend from Model.
 78   * @param clazz Class type.
 79   * @param response HttpResponse which getting the JSONContent.
 80   * @throws ParseException Throw this exception if any
 81   * @throws IOException Throw this exception if any
 82   */
 83  public static <T extends Model> T parser(final Class<T> clazz, HttpResponse response) throws IOException, ParseException {
 84    //Read InputStream from HttpResponse to Buffered
 85    HttpEntity entity = SocialHttpClientSupport.processContent(response);
 86    //Check the content length
 87    if (entity.getContentLength() != -1) {
 88      //getting the HttpResponse content
 89      String jsonContent = EntityUtils.toString(entity);
 90      //close stream
 91      SocialHttpClientSupport.consume(entity);
 92      return parser(clazz, jsonContent);
 93    } else {
 94      return null;
 95    }
 96  }
 97  
 98  /**
 99   * Parse JSON text into java Map object from the input source.
100   *  
101   * @param jsonContent Json content which you need to create the Model
102   * @throws ParseException Throw this exception if any
103   */
104  public static Map parser(String jsonContent) throws ParseException {
105    JSONParser parser = new JSONParser();
106    ContainerFactory containerFactory = new ContainerFactory() {
107      public List creatArrayContainer() {
108        return new LinkedList();
109      }
110
111      public Map createObjectContainer() {
112          return new LinkedHashMap();
113      }
114    };
115    return (Map) parser.parse(jsonContent, containerFactory);
116  }
117
118  /**
119   * HttpResponse text into java Map object from the input source.
120   *  
121   * @param response HttpResponse to get the content.
122   * @throws ParseException Throw this exception if any
123   */
124  public static Map parser(HttpResponse response) throws ParseException {
125    String jsonContent = null;
126    try {
127      jsonContent = SocialHttpClientSupport.getContent(response);
128    } catch (SocialHttpClientException e) {
129      throw new ParseException(0);
130    }
131    return parser(jsonContent);
132  }
133
134  /**
135   *  
136   * @author Ly Minh Phuong - http://phuonglm.net
137   * @param <T>
138   * @param clazz
139   * @param response
140   * @return
141   * @throws IOException
142   * @throws ParseException
143   */
144  public static <T extends Model > List<T> JSONArrayObjectParser(final Class<T> clazz, HttpResponse response) throws IOException, ParseException{
145    //Read InputStream from HttpResponse to Buffered
146    HttpEntity entity = SocialHttpClientSupport.processContent(response);
147    //Check the content length
148    if (entity.getContentLength() != -1) {
149      //getting the HttpResponse content
150      String jsonContent = EntityUtils.toString(entity);
151      //close stream
152      SocialHttpClientSupport.consume(entity);
153      return JSONArrayObjectParser(clazz, jsonContent);
154    } else {
155      return null;
156    }
157  }
158  
159  /**
160   * 
161   * @author Ly Minh Phuong - http://phuonglm.net
162   * @param <T>
163   * @param clazz
164   * @param jsonArrayContent
165   * @return
166   * @throws IOException
167   * @throws ParseException
168   */
169  public static <T extends Model > List<T> JSONArrayObjectParser(final Class<T> clazz, String jsonArrayContent) throws IOException, ParseException{
170    JSONArray jsonResultArray = (JSONArray)JSONValue.parse(jsonArrayContent);
171    List<T> result = new ArrayList<T>();
172    for (Object jsonObject : jsonResultArray) {
173      String jsonString = jsonObject.toString();
174      result.add(parser(clazz,jsonString));
175    }
176    return result;
177  }
178}