001    /**
002     * Copyright (c) 2000-2013 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.HashMap;
025    import java.util.Map;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class DefaultPollerResponse implements PollerResponse {
031    
032            public DefaultPollerResponse(
033                    PollerHeader pollerHeader, String portletId, String chunkId) {
034    
035                    _pollerHeader = pollerHeader;
036                    _portletId = portletId;
037                    _chunkId = chunkId;
038            }
039    
040            @Override
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            @Override
051            public void createResponseMessage(Message message) {
052                    String responseDestinationName = message.getResponseDestinationName();
053    
054                    if (Validator.isNull(responseDestinationName)) {
055                            return;
056                    }
057    
058                    _responseMessage = MessageBusUtil.createResponseMessage(message);
059    
060                    _responseMessage.setPayload(this);
061            }
062    
063            @Override
064            public PollerHeader getPollerHeader() {
065                    return _pollerHeader;
066            }
067    
068            @Override
069            public String getPortletId() {
070                    return _portletId;
071            }
072    
073            @Override
074            public boolean isEmpty() {
075                    return _parameterMap.isEmpty();
076            }
077    
078            @Override
079            public synchronized void setParameter(String name, JSONArray value)
080                    throws PollerResponseClosedException {
081    
082                    if (_responseMessage == null) {
083                            throw new PollerResponseClosedException();
084                    }
085    
086                    _parameterMap.put(name, value);
087            }
088    
089            @Override
090            public synchronized void setParameter(String name, JSONObject value)
091                    throws PollerResponseClosedException {
092    
093                    if (_responseMessage == null) {
094                            throw new PollerResponseClosedException();
095                    }
096    
097                    _parameterMap.put(name, value);
098            }
099    
100            @Override
101            public void setParameter(String name, String value)
102                    throws PollerResponseClosedException {
103    
104                    synchronized (this) {
105                            if (_responseMessage == null) {
106                                    throw new PollerResponseClosedException();
107                            }
108    
109                            _parameterMap.put(name, value);
110                    }
111            }
112    
113            @Override
114            public JSONObject toJSONObject() {
115                    JSONObject pollerResponseJSONObject =
116                            JSONFactoryUtil.createJSONObject();
117    
118                    pollerResponseJSONObject.put("portletId", _portletId);
119    
120                    if (Validator.isNotNull(_chunkId)) {
121                            pollerResponseJSONObject.put("chunkId", _chunkId);
122                    }
123    
124                    JSONObject dataJSONObject = JSONFactoryUtil.createJSONObject();
125    
126                    for (Map.Entry<String, Object> entry : _parameterMap.entrySet()) {
127                            String name = entry.getKey();
128                            Object value = entry.getValue();
129    
130                            if (value instanceof JSONArray) {
131                                    dataJSONObject.put(name, (JSONArray)value);
132                            }
133                            else if (value instanceof JSONObject) {
134                                    dataJSONObject.put(name, (JSONObject)value);
135                            }
136                            else {
137                                    dataJSONObject.put(name, String.valueOf(value));
138                            }
139                    }
140    
141                    pollerResponseJSONObject.put("data", dataJSONObject);
142    
143                    return pollerResponseJSONObject;
144            }
145    
146            private String _chunkId;
147            private Map<String, Object> _parameterMap = new HashMap<String, Object>();
148            private PollerHeader _pollerHeader;
149            private String _portletId;
150            private Message _responseMessage;
151    
152    }