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.messaging.Message;
021 import com.liferay.portal.kernel.messaging.MessageBusUtil;
022 import com.liferay.portal.kernel.util.Validator;
023
024 import java.util.HashMap;
025 import java.util.Iterator;
026 import java.util.Map;
027
028
031 public class DefaultPollerResponse implements PollerResponse {
032
033 public DefaultPollerResponse(
034 PollerHeader pollerHeader, String portletId, String chunkId) {
035
036 _pollerHeader = pollerHeader;
037 _portletId = portletId;
038 _chunkId = chunkId;
039 }
040
041 public synchronized void close() {
042 if (Validator.isNotNull(_responseMessage)) {
043 MessageBusUtil.sendMessage(
044 _responseMessage.getDestinationName(), _responseMessage);
045
046 _responseMessage = null;
047 }
048 }
049
050 public void createResponseMessage(Message message) {
051 String responseDestinationName = message.getResponseDestinationName();
052
053 if (Validator.isNull(responseDestinationName)) {
054 return;
055 }
056
057 _responseMessage = MessageBusUtil.createResponseMessage(message);
058
059 _responseMessage.setPayload(this);
060 }
061
062 public PollerHeader getPollerHeader() {
063 return _pollerHeader;
064 }
065
066 public String getPortletId() {
067 return _portletId;
068 }
069
070 public boolean isEmpty() {
071 return _parameterMap.isEmpty();
072 }
073
074 public synchronized void setParameter(String name, JSONArray value)
075 throws PollerResponseClosedException {
076
077 if (_responseMessage == null) {
078 throw new PollerResponseClosedException();
079 }
080
081 _parameterMap.put(name, value);
082 }
083
084 public synchronized void setParameter(String name, JSONObject value)
085 throws PollerResponseClosedException {
086
087 if (_responseMessage == null) {
088 throw new PollerResponseClosedException();
089 }
090
091 _parameterMap.put(name, value);
092 }
093
094 public void setParameter(String name, String value)
095 throws PollerResponseClosedException {
096
097 synchronized (this) {
098 if (_responseMessage == null) {
099 throw new PollerResponseClosedException();
100 }
101
102 _parameterMap.put(name, value);
103 }
104 }
105
106 public JSONObject toJSONObject() {
107 JSONObject pollerResponseJSONObject =
108 JSONFactoryUtil.createJSONObject();
109
110 pollerResponseJSONObject.put("portletId", _portletId);
111
112 if (Validator.isNotNull(_chunkId)) {
113 pollerResponseJSONObject.put("chunkId", _chunkId);
114 }
115
116 JSONObject dataJSONObject = JSONFactoryUtil.createJSONObject();
117
118 Iterator<Map.Entry<String, Object>> itr =
119 _parameterMap.entrySet().iterator();
120
121 while (itr.hasNext()) {
122 Map.Entry<String, Object> entry = itr.next();
123
124 String name = entry.getKey();
125 Object value = entry.getValue();
126
127 if (value instanceof JSONArray) {
128 dataJSONObject.put(name, (JSONArray)value);
129 }
130 else if (value instanceof JSONObject) {
131 dataJSONObject.put(name, (JSONObject)value);
132 }
133 else {
134 dataJSONObject.put(name, String.valueOf(value));
135 }
136 }
137
138 pollerResponseJSONObject.put("data", dataJSONObject);
139
140 return pollerResponseJSONObject;
141 }
142
143 private String _chunkId;
144 private Map<String, Object> _parameterMap = new HashMap<String, Object>();
145 private PollerHeader _pollerHeader;
146 private String _portletId;
147 private Message _responseMessage;
148
149 }