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                 break;
239             }
240         }
241 
242         if (!valid) {
243             throw new IllegalArgumentException();
244         }
245 
246         _contentType = contentType;
247     }
248 
249     public Locale getLocale() {
250         return _req.getLocale();
251     }
252 
253     public OutputStream getPortletOutputStream() throws IOException{
254         if (_calledGetWriter) {
255             throw new IllegalStateException();
256         }
257 
258         if (_contentType == null) {
259             throw new IllegalStateException();
260         }
261 
262         _calledGetPortletOutputStream = true;
263 
264         return _res.getOutputStream();
265     }
266 
267     public String getTitle() {
268         return _title;
269     }
270 
271     public void setTitle(String title) {
272         _title = title;
273 
274         // See LEP-2188
275 
276         ThemeDisplay themeDisplay =
277             (ThemeDisplay)_req.getAttribute(WebKeys.THEME_DISPLAY);
278 
279         PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
280 
281         portletDisplay.setTitle(_title);
282     }
283 
284     public Boolean getUseDefaultTemplate() {
285         return _useDefaultTemplate;
286     }
287 
288     public void setUseDefaultTemplate(Boolean useDefaultTemplate) {
289         _useDefaultTemplate = useDefaultTemplate;
290     }
291 
292     public PrintWriter getWriter() throws IOException {
293         if (_calledGetPortletOutputStream) {
294             throw new IllegalStateException();
295         }
296 
297         if (_contentType == null) {
298             throw new IllegalStateException();
299         }
300 
301         _calledGetWriter = true;
302 
303         return _res.getWriter();
304     }
305 
306     public int getBufferSize() {
307         return _res.getBufferSize();
308     }
309 
310     public void setBufferSize(int size) {
311         _res.setBufferSize(size);
312     }
313 
314     public void flushBuffer() throws IOException {
315         _res.flushBuffer();
316     }
317 
318     public void resetBuffer() {
319         _res.resetBuffer();
320     }
321 
322     public boolean isCommitted() {
323         return false;
324     }
325 
326     public void reset() {
327     }
328 
329     public HttpServletResponse getHttpServletResponse() {
330         return _res;
331     }
332 
333     public Portlet getPortlet() {
334         if (_portlet == null) {
335             try {
336                 _portlet = PortletLocalServiceUtil.getPortletById(
337                     _companyId, _portletName);
338             }
339             catch (Exception e) {
340                 _log.error(e);
341             }
342         }
343 
344         return _portlet;
345     }
346 
347     protected RenderResponseImpl() {
348         if (_log.isDebugEnabled()) {
349             _log.debug("Creating new instance " + hashCode());
350         }
351     }
352 
353     protected void init(
354         RenderRequestImpl req, HttpServletResponse res, String portletName,
355         long companyId, long plid) {
356 
357         _req = req;
358         _res = res;
359         _portletName = portletName;
360         _companyId = companyId;
361         setPlid(plid);
362     }
363 
364     protected void recycle() {
365         if (_log.isDebugEnabled()) {
366             _log.debug("Recycling instance " + hashCode());
367         }
368 
369         _req = null;
370         _res = null;
371         _portletName = null;
372         _portlet = null;
373         _namespace = null;
374         _companyId = 0;
375         _plid = 0;
376         _urlEncoder = null;
377         _title = null;
378         _useDefaultTemplate = null;
379         _contentType = null;
380         _calledGetPortletOutputStream = false;
381         _calledGetWriter = false;
382     }
383 
384     protected RenderRequestImpl getReq() {
385         return _req;
386     }
387 
388     protected String getPortletName() {
389         return _portletName;
390     }
391 
392     protected long getCompanyId() {
393         return _companyId;
394     }
395 
396     protected long getPlid() {
397         return _plid;
398     }
399 
400     protected void setPlid(long plid) {
401         _plid = plid;
402 
403         if (_plid <= 0) {
404             Layout layout = (Layout)_req.getAttribute(WebKeys.LAYOUT);
405 
406             if (layout != null) {
407                 _plid = layout.getPlid();
408             }
409         }
410     }
411 
412     protected Map getProperties() {
413         return _properties;
414     }
415 
416     protected URLEncoder getUrlEncoder() {
417         return _urlEncoder;
418     }
419 
420     protected boolean isCalledGetPortletOutputStream() {
421         return _calledGetPortletOutputStream;
422     }
423 
424     protected boolean isCalledGetWriter() {
425         return _calledGetWriter;
426     }
427 
428     private static Log _log = LogFactory.getLog(RenderRequestImpl.class);
429 
430     private RenderRequestImpl _req;
431     private HttpServletResponse _res;
432     private String _portletName;
433     private Portlet _portlet;
434     private String _namespace;
435     private long _companyId;
436     private long _plid;
437     private Map _properties;
438     private URLEncoder _urlEncoder;
439     private String _title;
440     private Boolean _useDefaultTemplate;
441     private String _contentType;
442     private boolean _calledGetPortletOutputStream;
443     private boolean _calledGetWriter;
444 
445 }