001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.Map;
025    import java.util.concurrent.ConcurrentHashMap;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class DefaultPollerResponse implements PollerResponse {
031    
032            @Override
033            public void close(
034                    Message message, PollerHeader pollerHeader, String portletId,
035                    String chunkId) {
036    
037                    _closed = true;
038    
039                    _pollerHeader = pollerHeader;
040                    _portletId = portletId;
041                    _chunkId = chunkId;
042    
043                    String responseDestinationName = message.getResponseDestinationName();
044    
045                    if (Validator.isNull(responseDestinationName)) {
046                            return;
047                    }
048    
049                    Message responseMessage = MessageBusUtil.createResponseMessage(message);
050    
051                    responseMessage.setPayload(this);
052    
053                    MessageBusUtil.sendMessage(responseDestinationName, responseMessage);
054            }
055    
056            @Override
057            public PollerHeader getPollerHeader() {
058                    return _pollerHeader;
059            }
060    
061            @Override
062            public String getPortletId() {
063                    return _portletId;
064            }
065    
066            @Override
067            public boolean isEmpty() {
068                    return _parameterMap.isEmpty();
069            }
070    
071            @Override
072            public void setParameter(String name, JSONArray value)
073                    throws PollerResponseClosedException {
074    
075                    if (_closed) {
076                            throw new PollerResponseClosedException();
077                    }
078    
079                    _parameterMap.put(name, value);
080            }
081    
082            @Override
083            public void setParameter(String name, JSONObject value)
084                    throws PollerResponseClosedException {
085    
086                    if (_closed) {
087                            throw new PollerResponseClosedException();
088                    }
089    
090                    _parameterMap.put(name, value);
091            }
092    
093            @Override
094            public void setParameter(String name, String value)
095                    throws PollerResponseClosedException {
096    
097                    if (_closed) {
098                            throw new PollerResponseClosedException();
099                    }
100    
101                    _parameterMap.put(name, value);
102            }
103    
104            @Override
105            public JSONObject toJSONObject() {
106                    JSONObject pollerResponseJSONObject =
107                            JSONFactoryUtil.createJSONObject();
108    
109                    pollerResponseJSONObject.put("portletId", _portletId);
110    
111                    if (Validator.isNotNull(_chunkId)) {
112                            pollerResponseJSONObject.put("chunkId", _chunkId);
113                    }
114    
115                    JSONObject dataJSONObject = JSONFactoryUtil.createJSONObject();
116    
117                    for (Map.Entry<String, Object> entry : _parameterMap.entrySet()) {
118                            String name = entry.getKey();
119                            Object value = entry.getValue();
120    
121                            if (value instanceof JSONArray) {
122                                    dataJSONObject.put(name, (JSONArray)value);
123                            }
124                            else if (value instanceof JSONObject) {
125                                    dataJSONObject.put(name, (JSONObject)value);
126                            }
127                            else {
128                                    dataJSONObject.put(name, String.valueOf(value));
129                            }
130                    }
131    
132                    pollerResponseJSONObject.put("data", dataJSONObject);
133    
134                    return pollerResponseJSONObject;
135            }
136    
137            private String _chunkId;
138            private volatile boolean _closed;
139            private final Map<String, Object> _parameterMap = new ConcurrentHashMap<>();
140            private PollerHeader _pollerHeader;
141            private String _portletId;
142    
143    }