1   /**
2    * Copyright (c) 2000-2008 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.portlet;
24  
25  import com.liferay.portal.kernel.portlet.LiferayRenderResponse;
26  import com.liferay.portal.kernel.util.ArrayUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.theme.PortletDisplay;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portal.util.WebKeys;
31  
32  import java.util.Collection;
33  import java.util.LinkedHashMap;
34  import java.util.Map;
35  
36  import javax.portlet.PortletMode;
37  import javax.portlet.PortletRequest;
38  
39  import javax.servlet.http.HttpServletResponse;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  /**
45   * <a href="RenderResponseImpl.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class RenderResponseImpl
51      extends MimeResponseImpl implements LiferayRenderResponse {
52  
53      public void addDateHeader(String name, long date) {
54          if (Validator.isNull(name)) {
55              throw new IllegalArgumentException();
56          }
57  
58          if (_headers.containsKey(name)) {
59              Long[] values = (Long[])_headers.get(name);
60  
61              ArrayUtil.append(values, new Long(date));
62  
63              _headers.put(name, values);
64          }
65          else {
66              setDateHeader(name, date);
67          }
68      }
69  
70      public void addHeader(String name, String value) {
71          if (Validator.isNull(name)) {
72              throw new IllegalArgumentException();
73          }
74  
75          if (_headers.containsKey(name)) {
76              String[] values = (String[])_headers.get(name);
77  
78              ArrayUtil.append(values, value);
79  
80              _headers.put(name, values);
81          }
82          else {
83              setHeader(name, value);
84          }
85      }
86  
87      public void addIntHeader(String name, int value) {
88          if (Validator.isNull(name)) {
89              throw new IllegalArgumentException();
90          }
91  
92          if (_headers.containsKey(name)) {
93              Integer[] values = (Integer[])_headers.get(name);
94  
95              ArrayUtil.append(values, new Integer(value));
96  
97              _headers.put(name, values);
98          }
99          else {
100             setIntHeader(name, value);
101         }
102     }
103 
104     public String getLifecycle() {
105         return PortletRequest.RENDER_PHASE;
106     }
107 
108     public String getResourceName() {
109         return _resourceName;
110     }
111 
112     public String getTitle() {
113         return _title;
114     }
115 
116     public Boolean getUseDefaultTemplate() {
117         return _useDefaultTemplate;
118     }
119 
120     public void setDateHeader(String name, long date) {
121         if (Validator.isNull(name)) {
122             throw new IllegalArgumentException();
123         }
124 
125         if (date <= 0) {
126             _headers.remove(name);
127         }
128         else {
129             _headers.put(name, new Long[] {new Long(date)});
130         }
131     }
132 
133     public void setHeader(String name, String value) {
134         if (Validator.isNull(name)) {
135             throw new IllegalArgumentException();
136         }
137 
138         if (Validator.isNull(value)) {
139             _headers.remove(name);
140         }
141         else {
142             _headers.put(name, new String[] {value});
143         }
144     }
145 
146     public void setIntHeader(String name, int value) {
147         if (Validator.isNull(name)) {
148             throw new IllegalArgumentException();
149         }
150 
151         if (value <= 0) {
152             _headers.remove(name);
153         }
154         else {
155             _headers.put(name, new Integer[] {new Integer(value)});
156         }
157     }
158 
159     public void setResourceName(String resourceName) {
160         _resourceName = resourceName;
161     }
162 
163     public void setNextPossiblePortletModes(
164         Collection<PortletMode> portletModes) {
165     }
166 
167     public void setTitle(String title) {
168         _title = title;
169 
170         // See LEP-2188
171 
172         ThemeDisplay themeDisplay =
173             (ThemeDisplay)_req.getAttribute(WebKeys.THEME_DISPLAY);
174 
175         PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
176 
177         portletDisplay.setTitle(_title);
178     }
179 
180     public void setUseDefaultTemplate(Boolean useDefaultTemplate) {
181         _useDefaultTemplate = useDefaultTemplate;
182     }
183 
184     public void transferHeaders(HttpServletResponse res) {
185         for (Map.Entry<String, Object> entry : _headers.entrySet()) {
186             String name = entry.getKey();
187             Object values = entry.getValue();
188 
189             if (values instanceof Integer[]) {
190                 Integer[] intValues = (Integer[])values;
191 
192                 for (int i = 0; i < intValues.length; i++) {
193                     if (res.containsHeader(name)) {
194                         res.addIntHeader(name, intValues[i].intValue());
195                     }
196                     else {
197                         res.addIntHeader(name, intValues[i].intValue());
198                     }
199                 }
200             }
201             else if (values instanceof Long[]) {
202                 Long[] dateValues = (Long[])values;
203 
204                 for (int i = 0; i < dateValues.length; i++) {
205                     if (res.containsHeader(name)) {
206                         res.addDateHeader(name, dateValues[i].longValue());
207                     }
208                     else {
209                         res.addDateHeader(name, dateValues[i].longValue());
210                     }
211                 }
212             }
213             else if (values instanceof String[]) {
214                 String[] stringValues = (String[])values;
215 
216                 for (int i = 0; i < stringValues.length; i++) {
217                     if (res.containsHeader(name)) {
218                         res.addHeader(name, stringValues[i]);
219                     }
220                     else {
221                         res.addHeader(name, stringValues[i]);
222                     }
223                 }
224             }
225         }
226     }
227 
228     protected RenderResponseImpl() {
229         if (_log.isDebugEnabled()) {
230             _log.debug("Creating new instance " + hashCode());
231         }
232     }
233 
234     protected void init(
235         PortletRequestImpl req, HttpServletResponse res, String portletName,
236         long companyId, long plid) {
237 
238         super.init(req, res, portletName, companyId, plid);
239 
240         _req = req;
241     }
242 
243     protected void recycle() {
244         if (_log.isDebugEnabled()) {
245             _log.debug("Recycling instance " + hashCode());
246         }
247 
248         super.recycle();
249 
250         _req = null;
251         _title = null;
252         _useDefaultTemplate = null;
253         _resourceName = null;
254         _headers.clear();
255     }
256 
257     private static Log _log = LogFactory.getLog(RenderResponseImpl.class);
258 
259     private PortletRequestImpl _req;
260     private String _title;
261     private Boolean _useDefaultTemplate;
262     private String _resourceName;
263     private LinkedHashMap<String, Object> _headers =
264         new LinkedHashMap<String, Object>();
265 
266 }