001
014
015 package com.liferay.portal.kernel.poller;
016
017 import com.liferay.portal.kernel.json.JSONArray;
018 import com.liferay.portal.kernel.json.JSONFactoryUtil;
019 import com.liferay.portal.kernel.json.JSONObject;
020 import com.liferay.portal.kernel.util.Validator;
021
022 import java.util.HashMap;
023 import java.util.Iterator;
024 import java.util.Map;
025
026
029 public class PollerResponse {
030
031 public static final String POLLER_HINT_HIGH_CONNECTIVITY =
032 "pollerHintHighConnectivity";
033
034 public PollerResponse(String portletId, String chunkId) {
035 _portletId = portletId;
036 _chunkId = chunkId;
037 }
038
039 public void setParameter(String name, JSONArray value) {
040 _parameterMap.put(name, value);
041 }
042
043 public void setParameter(String name, JSONObject value) {
044 _parameterMap.put(name, value);
045 }
046
047 public void setParameter(String name, String value) {
048 _parameterMap.put(name, value);
049 }
050
051 public JSONObject toJSONObject() {
052 JSONObject pollerResponseJSON = JSONFactoryUtil.createJSONObject();
053
054 pollerResponseJSON.put("portletId", _portletId);
055
056 if (Validator.isNotNull(_chunkId)) {
057 pollerResponseJSON.put("chunkId", _chunkId);
058 }
059
060 JSONObject dataJSON = JSONFactoryUtil.createJSONObject();
061
062 Iterator<Map.Entry<String, Object>> itr =
063 _parameterMap.entrySet().iterator();
064
065 while (itr.hasNext()) {
066 Map.Entry<String, Object> entry = itr.next();
067
068 String name = entry.getKey();
069 Object value = entry.getValue();
070
071 if (value instanceof JSONArray) {
072 dataJSON.put(name, (JSONArray)value);
073 }
074 else if (value instanceof JSONObject) {
075 dataJSON.put(name, (JSONObject)value);
076 }
077 else {
078 dataJSON.put(name, String.valueOf(value));
079 }
080 }
081
082 pollerResponseJSON.put("data", dataJSON);
083
084 return pollerResponseJSON;
085 }
086
087 private String _chunkId;
088 private Map<String, Object> _parameterMap = new HashMap<String, Object>();
089 private String _portletId;
090
091 }