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
39 public class PollerResponse {
40
41 public static final String POLLER_HINT_HIGH_CONNECTIVITY =
42 "pollerHintHighConnectivity";
43
44 public PollerResponse(String portletId, String chunkId) {
45 _portletId = portletId;
46 _chunkId = chunkId;
47 }
48
49 public void setParameter(String name, JSONArray value) {
50 _parameterMap.put(name, value);
51 }
52
53 public void setParameter(String name, JSONObject value) {
54 _parameterMap.put(name, value);
55 }
56
57 public void setParameter(String name, String value) {
58 _parameterMap.put(name, value);
59 }
60
61 public JSONObject toJSONObject() {
62 JSONObject pollerResponseJSON = JSONFactoryUtil.createJSONObject();
63
64 pollerResponseJSON.put("portletId", _portletId);
65
66 if (Validator.isNotNull(_chunkId)) {
67 pollerResponseJSON.put("chunkId", _chunkId);
68 }
69
70 JSONObject dataJSON = JSONFactoryUtil.createJSONObject();
71
72 Iterator<Map.Entry<String, Object>> itr =
73 _parameterMap.entrySet().iterator();
74
75 while (itr.hasNext()) {
76 Map.Entry<String, Object> entry = itr.next();
77
78 String name = entry.getKey();
79 Object value = entry.getValue();
80
81 if (value instanceof JSONArray) {
82 dataJSON.put(name, (JSONArray)value);
83 }
84 else if (value instanceof JSONObject) {
85 dataJSON.put(name, (JSONObject)value);
86 }
87 else {
88 dataJSON.put(name, String.valueOf(value));
89 }
90 }
91
92 pollerResponseJSON.put("data", dataJSON);
93
94 return pollerResponseJSON;
95 }
96
97 private String _chunkId;
98 private Map<String, Object> _parameterMap = new HashMap<String, Object>();
99 private String _portletId;
100
101 }