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