1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
35   * <a href="PollerResponse.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
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 }