1   /**
2    * Copyright (c) 2000-2007 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.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.servlet.URLEncoder;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.Layout;
31  import com.liferay.portal.model.Portlet;
32  import com.liferay.portal.service.PortletLocalServiceUtil;
33  import com.liferay.portal.theme.PortletDisplay;
34  import com.liferay.portal.theme.ThemeDisplay;
35  import com.liferay.portal.util.PortalUtil;
36  import com.liferay.portal.util.WebKeys;
37  import com.liferay.util.CollectionFactory;
38  
39  import java.io.IOException;
40  import java.io.OutputStream;
41  import java.io.PrintWriter;
42  
43  import java.lang.reflect.Constructor;
44  
45  import java.util.Enumeration;
46  import java.util.Locale;
47  import java.util.Map;
48  
49  import javax.portlet.PortletModeException;
50  import javax.portlet.PortletPreferences;
51  import javax.portlet.PortletURL;
52  import javax.portlet.RenderResponse;
53  import javax.portlet.WindowStateException;
54  
55  import javax.servlet.http.HttpServletResponse;
56  
57  import org.apache.commons.logging.Log;
58  import org.apache.commons.logging.LogFactory;
59  
60  /**
61   * <a href="RenderResponseImpl.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Brian Wing Shun Chan
64   *
65   */
66  public class RenderResponseImpl implements RenderResponse {
67  
68      public void addProperty(String key, String value) {
69      }
70  
71      public void setProperty(String key, String value) {
72          if (_properties == null) {
73              _properties = CollectionFactory.getHashMap();
74          }
75  
76          _properties.put(key, new String[] {value});
77      }
78  
79      public PortletURL createPortletURL(boolean action) {
80          return createPortletURL(_portletName, action);
81      }
82  
83      public PortletURL createPortletURL(String portletName, boolean action) {
84  
85          // Wrap portlet URL with a custom wrapper if and only if a custom
86          // wrapper for the portlet has been defined
87  
88          Portlet portlet = getPortlet();
89  
90          String portletURLClass = portlet.getPortletURLClass();
91  
92          if (portlet.getPortletId().equals(portletName) &&
93                  Validator.isNotNull(portletURLClass)) {
94  
95              try {
96                  Class portletURLClassObj = Class.forName(portletURLClass);
97  
98                  Constructor constructor = portletURLClassObj.getConstructor(
99                      new Class[] {
100                         com.liferay.portlet.RenderResponseImpl.class,
101                         boolean.class
102                     });
103 
104                 return (PortletURL)constructor.newInstance(
105                     new Object[] {this, Boolean.valueOf(action)});
106             }
107             catch (Exception e) {
108                 _log.error(e);
109             }
110         }
111 
112         long plid = _plid;
113 
114         try {
115             PortletPreferences portletSetup =
116                 PortletPreferencesFactoryUtil.getPortletSetup(
117                     _req, _portletName, true, true);
118 
119             plid = GetterUtil.getLong(portletSetup.getValue(
120                 "portlet-setup-link-to-plid", String.valueOf(_plid)));
121 
122             if (plid <= 0) {
123                 plid = _plid;
124             }
125         }
126         catch (PortalException e) {
127             if (_log.isWarnEnabled()) {
128                 _log.warn(e);
129             }
130         }
131         catch (SystemException e) {
132             if (_log.isWarnEnabled()) {
133                 _log.warn(e);
134             }
135         }
136 
137         return new PortletURLImpl(_req, portletName, plid, action);
138     }
139 
140     public PortletURL createActionURL() {
141         return createActionURL(_portletName);
142     }
143 
144     public PortletURL createActionURL(String portletName) {
145         PortletURL portletURL = createPortletURL(portletName, true);
146 
147         try {
148             portletURL.setWindowState(_req.getWindowState());
149         }
150         catch (WindowStateException wse) {
151         }
152 
153         try {
154             portletURL.setPortletMode(_req.getPortletMode());
155         }
156         catch (PortletModeException pme) {
157         }
158 
159         return portletURL;
160     }
161 
162     public PortletURL createRenderURL() {
163         return createRenderURL(_portletName);
164     }
165 
166     public PortletURL createRenderURL(String portletName) {
167         PortletURL portletURL = createPortletURL(portletName, false);
168 
169         try {
170             portletURL.setWindowState(_req.getWindowState());
171         }
172         catch (WindowStateException wse) {
173         }
174 
175         try {
176             portletURL.setPortletMode(_req.getPortletMode());
177         }
178         catch (PortletModeException pme) {
179         }
180 
181         return portletURL;
182     }
183 
184     public String getNamespace() {
185         if (_namespace == null) {
186             _namespace = PortalUtil.getPortletNamespace(_portletName);
187         }
188 
189         return _namespace;
190     }
191 
192     public void setURLEncoder(URLEncoder urlEncoder) {
193         _urlEncoder = urlEncoder;
194     }
195 
196     public String encodeURL(String path) {
197         if ((path == null) ||
198             (!path.startsWith("#") && !path.startsWith("/") &&
199                 (path.indexOf("://") == -1))) {
200 
201             // Allow '#' as well to workaround a bug in Oracle ADF 10.1.3
202 
203             throw new IllegalArgumentException(
204                 "URL path must start with a '/' or include '://'");
205         }
206 
207         if (_urlEncoder != null) {
208             return _urlEncoder.encodeURL(_res, path);
209         }
210         else {
211             return path;
212         }
213     }
214 
215     public String getCharacterEncoding() {
216         return _res.getCharacterEncoding();
217     }
218 
219     public String getContentType() {
220         return _contentType;
221     }
222 
223     public void setContentType(String contentType) {
224         if (Validator.isNull(contentType)) {
225             throw new IllegalArgumentException();
226         }
227 
228         Enumeration enu = _req.getResponseContentTypes();
229 
230         boolean valid = false;
231 
232         while (enu.hasMoreElements()) {
233             String resContentType = (String)enu.nextElement();
234 
235             if (contentType.startsWith(resContentType)) {
236                 valid = true;
237             }
238         }
239 
240         if (!valid) {
241             throw new IllegalArgumentException();
242         }
243 
244         _contentType = contentType;
245     }
246 
247     public Locale getLocale() {
248         return _req.getLocale();
249     }
250 
251     public OutputStream getPortletOutputStream() throws IOException{
252         if (_calledGetWriter) {
253             throw new IllegalStateException();
254         }
255 
256         if (_contentType == null) {
257             throw new IllegalStateException();
258         }
259 
260         _calledGetPortletOutputStream = true;
261 
262         return _res.getOutputStream();
263     }
264 
265     public String getTitle() {
266         return _title;
267     }
268 
269     public void setTitle(String title) {
270         _title = title;
271 
272         // See LEP-2188
273 
274         ThemeDisplay themeDisplay =
275             (ThemeDisplay)_req.getAttribute(WebKeys.THEME_DISPLAY);
276 
277         PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
278 
279         portletDisplay.setTitle(_title);
280     }
281 
282     public Boolean getUseDefaultTemplate() {
283         return _useDefaultTemplate;
284     }
285 
286     public void setUseDefaultTemplate(Boolean useDefaultTemplate) {
287         _useDefaultTemplate = useDefaultTemplate;
288     }
289 
290     public PrintWriter getWriter() throws IOException {
291         if (_calledGetPortletOutputStream) {
292             throw new IllegalStateException();
293         }
294 
295         if (_contentType == null) {
296             throw new IllegalStateException();
297         }
298 
299         _calledGetWriter = true;
300 
301         return _res.getWriter();
302     }
303 
304     public int getBufferSize() {
305         return _res.getBufferSize();
306     }
307 
308     public void setBufferSize(int size) {
309         _res.setBufferSize(size);
310     }
311 
312     public void flushBuffer() throws IOException {
313         _res.flushBuffer();
314     }
315 
316     public void resetBuffer() {
317         _res.resetBuffer();
318     }
319 
320     public boolean isCommitted() {
321         return false;
322     }
323 
324     public void reset() {
325     }
326 
327     public HttpServletResponse getHttpServletResponse() {
328         return _res;
329     }
330 
331     public Portlet getPortlet() {
332         if (_portlet == null) {
333             try {
334                 _portlet = PortletLocalServiceUtil.getPortletById(
335                     _companyId, _portletName);
336             }
337             catch (Exception e) {
338                 _log.error(e);
339             }
340         }
341 
342         return _portlet;
343     }
344 
345     protected RenderResponseImpl() {
346         if (_log.isDebugEnabled()) {
347             _log.debug("Creating new instance " + hashCode());
348         }
349     }
350 
351     protected void init(
352         RenderRequestImpl req, HttpServletResponse res, String portletName,
353         long companyId, long plid) {
354 
355         _req = req;
356         _res = res;
357         _portletName = portletName;
358         _companyId = companyId;
359         setPlid(plid);
360     }
361 
362     protected void recycle() {
363         if (_log.isDebugEnabled()) {
364             _log.debug("Recycling instance " + hashCode());
365         }
366 
367         _req = null;
368         _res = null;
369         _portletName = null;
370         _portlet = null;
371         _namespace = null;
372         _companyId = 0;
373         _plid = 0;
374         _urlEncoder = null;
375         _title = null;
376         _useDefaultTemplate = null;
377         _contentType = null;
378         _calledGetPortletOutputStream = false;
379         _calledGetWriter = false;
380     }
381 
382     protected RenderRequestImpl getReq() {
383         return _req;
384     }
385 
386     protected String getPortletName() {
387         return _portletName;
388     }
389 
390     protected long getCompanyId() {
391         return _companyId;
392     }
393 
394     protected long getPlid() {
395         return _plid;
396     }
397 
398     protected void setPlid(long plid) {
399         _plid = plid;
400 
401         if (_plid <= 0) {
402             Layout layout = (Layout)_req.getAttribute(WebKeys.LAYOUT);
403 
404             if (layout != null) {
405                 _plid = layout.getPlid();
406             }
407         }
408     }
409 
410     protected Map getProperties() {
411         return _properties;
412     }
413 
414     protected URLEncoder getUrlEncoder() {
415         return _urlEncoder;
416     }
417 
418     protected boolean isCalledGetPortletOutputStream() {
419         return _calledGetPortletOutputStream;
420     }
421 
422     protected boolean isCalledGetWriter() {
423         return _calledGetWriter;
424     }
425 
426     private static Log _log = LogFactory.getLog(RenderRequestImpl.class);
427 
428     private RenderRequestImpl _req;
429     private HttpServletResponse _res;
430     private String _portletName;
431     private Portlet _portlet;
432     private String _namespace;
433     private long _companyId;
434     private long _plid;
435     private Map _properties;
436     private URLEncoder _urlEncoder;
437     private String _title;
438     private Boolean _useDefaultTemplate;
439     private String _contentType;
440     private boolean _calledGetPortletOutputStream;
441     private boolean _calledGetWriter;
442 
443 }