1
22
23 package com.liferay.portal.kernel.poller;
24
25 import com.liferay.portal.kernel.json.JSONArray;
26 import com.liferay.portal.kernel.json.JSONFactoryUtil;
27 import com.liferay.portal.kernel.json.JSONObject;
28 import com.liferay.portal.kernel.util.Validator;
29
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.Map;
33
34
40 public class PollerResponse {
41
42 public static final String POLLER_HINT_HIGH_CONNECTIVITY =
43 "pollerHintHighConnectivity";
44
45 public PollerResponse(String portletId, String chunkId) {
46 _portletId = portletId;
47 _chunkId = chunkId;
48 }
49
50 public void setParameter(String name, JSONArray value) {
51 _parameterMap.put(name, value);
52 }
53
54 public void setParameter(String name, JSONObject value) {
55 _parameterMap.put(name, value);
56 }
57
58 public void setParameter(String name, String value) {
59 _parameterMap.put(name, value);
60 }
61
62 public JSONObject toJSONObject() {
63 JSONObject pollerResponseJSON = JSONFactoryUtil.createJSONObject();
64
65 pollerResponseJSON.put("portletId", _portletId);
66
67 if (Validator.isNotNull(_chunkId)) {
68 pollerResponseJSON.put("chunkId", _chunkId);
69 }
70
71 JSONObject dataJSON = JSONFactoryUtil.createJSONObject();
72
73 Iterator<Map.Entry<String, Object>> itr =
74 _parameterMap.entrySet().iterator();
75
76 while (itr.hasNext()) {
77 Map.Entry<String, Object> entry = itr.next();
78
79 String name = entry.getKey();
80 Object value = entry.getValue();
81
82 if (value instanceof JSONArray) {
83 dataJSON.put(name, (JSONArray)value);
84 }
85 else if (value instanceof JSONObject) {
86 dataJSON.put(name, (JSONObject)value);
87 }
88 else {
89 dataJSON.put(name, String.valueOf(value));
90 }
91 }
92
93 pollerResponseJSON.put("data", dataJSON);
94
95 return pollerResponseJSON;
96 }
97
98 private String _chunkId;
99 private Map<String, Object> _parameterMap = new HashMap<String, Object>();
100 private String _portletId;
101
102 }