001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.portlet.LiferayRenderResponse;
026: import com.liferay.portal.kernel.portlet.LiferayWindowState;
027: import com.liferay.portal.kernel.servlet.URLEncoder;
028: import com.liferay.portal.kernel.util.ArrayUtil;
029: import com.liferay.portal.kernel.util.GetterUtil;
030: import com.liferay.portal.kernel.util.Validator;
031: import com.liferay.portal.model.Layout;
032: import com.liferay.portal.model.Portlet;
033: import com.liferay.portal.service.PortletLocalServiceUtil;
034: import com.liferay.portal.theme.PortletDisplay;
035: import com.liferay.portal.theme.ThemeDisplay;
036: import com.liferay.portal.util.PortalUtil;
037: import com.liferay.portal.util.WebKeys;
038: import com.liferay.util.CollectionFactory;
039:
040: import java.io.IOException;
041: import java.io.OutputStream;
042: import java.io.PrintWriter;
043:
044: import java.lang.reflect.Constructor;
045:
046: import java.util.Enumeration;
047: import java.util.Iterator;
048: import java.util.LinkedHashMap;
049: import java.util.Locale;
050: import java.util.Map;
051:
052: import javax.portlet.PortletModeException;
053: import javax.portlet.PortletPreferences;
054: import javax.portlet.PortletURL;
055: import javax.portlet.WindowStateException;
056:
057: import javax.servlet.http.HttpServletResponse;
058:
059: import org.apache.commons.logging.Log;
060: import org.apache.commons.logging.LogFactory;
061:
062: /**
063: * <a href="RenderResponseImpl.java.html"><b><i>View Source</i></b></a>
064: *
065: * @author Brian Wing Shun Chan
066: *
067: */
068: public class RenderResponseImpl implements LiferayRenderResponse {
069:
070: public void addProperty(String key, String value) {
071: }
072:
073: public void setProperty(String key, String value) {
074: if (_properties == null) {
075: _properties = CollectionFactory.getHashMap();
076: }
077:
078: _properties.put(key, new String[] { value });
079: }
080:
081: public PortletURL createPortletURL(boolean action) {
082: return createPortletURL(_portletName, action);
083: }
084:
085: public PortletURL createPortletURL(String portletName,
086: boolean action) {
087:
088: // Wrap portlet URL with a custom wrapper if and only if a custom
089: // wrapper for the portlet has been defined
090:
091: Portlet portlet = getPortlet();
092:
093: String portletURLClass = portlet.getPortletURLClass();
094:
095: if (portlet.getPortletId().equals(portletName)
096: && Validator.isNotNull(portletURLClass)) {
097:
098: try {
099: Class portletURLClassObj = Class
100: .forName(portletURLClass);
101:
102: Constructor constructor = portletURLClassObj
103: .getConstructor(new Class[] {
104: com.liferay.portlet.RenderResponseImpl.class,
105: boolean.class });
106:
107: return (PortletURL) constructor
108: .newInstance(new Object[] { this ,
109: Boolean.valueOf(action) });
110: } catch (Exception e) {
111: _log.error(e);
112: }
113: }
114:
115: long plid = _plid;
116:
117: try {
118: PortletPreferences portletSetup = PortletPreferencesFactoryUtil
119: .getPortletSetup(_req, _portletName, true, true);
120:
121: plid = GetterUtil.getLong(portletSetup
122: .getValue("portlet-setup-link-to-plid", String
123: .valueOf(_plid)));
124:
125: if (plid <= 0) {
126: plid = _plid;
127: }
128: } catch (PortalException e) {
129: if (_log.isWarnEnabled()) {
130: _log.warn(e);
131: }
132: } catch (SystemException e) {
133: if (_log.isWarnEnabled()) {
134: _log.warn(e);
135: }
136: }
137:
138: return new PortletURLImpl(_req, portletName, plid, action);
139: }
140:
141: public PortletURL createActionURL() {
142: return createActionURL(_portletName);
143: }
144:
145: public PortletURL createActionURL(String portletName) {
146: PortletURL portletURL = createPortletURL(portletName, true);
147:
148: try {
149: portletURL.setWindowState(_req.getWindowState());
150: } catch (WindowStateException wse) {
151: }
152:
153: try {
154: portletURL.setPortletMode(_req.getPortletMode());
155: } catch (PortletModeException pme) {
156: }
157:
158: return portletURL;
159: }
160:
161: public PortletURL createRenderURL() {
162: return createRenderURL(_portletName);
163: }
164:
165: public PortletURL createRenderURL(String portletName) {
166: PortletURL portletURL = createPortletURL(portletName, false);
167:
168: try {
169: portletURL.setWindowState(_req.getWindowState());
170: } catch (WindowStateException wse) {
171: }
172:
173: try {
174: portletURL.setPortletMode(_req.getPortletMode());
175: } catch (PortletModeException pme) {
176: }
177:
178: return portletURL;
179: }
180:
181: public String getNamespace() {
182: if (_namespace == null) {
183: _namespace = PortalUtil.getPortletNamespace(_portletName);
184: }
185:
186: return _namespace;
187: }
188:
189: public void setURLEncoder(URLEncoder urlEncoder) {
190: _urlEncoder = urlEncoder;
191: }
192:
193: public String encodeURL(String path) {
194: if ((path == null)
195: || (!path.startsWith("#") && !path.startsWith("/") && (path
196: .indexOf("://") == -1))) {
197:
198: // Allow '#' as well to workaround a bug in Oracle ADF 10.1.3
199:
200: throw new IllegalArgumentException(
201: "URL path must start with a '/' or include '://'");
202: }
203:
204: if (_urlEncoder != null) {
205: return _urlEncoder.encodeURL(_res, path);
206: } else {
207: return path;
208: }
209: }
210:
211: public String getCharacterEncoding() {
212: return _res.getCharacterEncoding();
213: }
214:
215: public String getContentType() {
216: return _contentType;
217: }
218:
219: public void setContentType(String contentType) {
220: if (Validator.isNull(contentType)) {
221: throw new IllegalArgumentException();
222: }
223:
224: Enumeration enu = _req.getResponseContentTypes();
225:
226: boolean valid = false;
227:
228: while (enu.hasMoreElements()) {
229: String resContentType = (String) enu.nextElement();
230:
231: if (contentType.startsWith(resContentType)) {
232: valid = true;
233:
234: break;
235: }
236: }
237:
238: if (_req.getWindowState().equals(LiferayWindowState.EXCLUSIVE)) {
239: valid = true;
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 = (ThemeDisplay) _req
277: .getAttribute(WebKeys.THEME_DISPLAY);
278:
279: PortletDisplay portletDisplay = themeDisplay
280: .getPortletDisplay();
281:
282: portletDisplay.setTitle(_title);
283: }
284:
285: public Boolean getUseDefaultTemplate() {
286: return _useDefaultTemplate;
287: }
288:
289: public void setUseDefaultTemplate(Boolean useDefaultTemplate) {
290: _useDefaultTemplate = useDefaultTemplate;
291: }
292:
293: public PrintWriter getWriter() throws IOException {
294: if (_calledGetPortletOutputStream) {
295: throw new IllegalStateException();
296: }
297:
298: if (_contentType == null) {
299: throw new IllegalStateException();
300: }
301:
302: _calledGetWriter = true;
303:
304: return _res.getWriter();
305: }
306:
307: public int getBufferSize() {
308: return _res.getBufferSize();
309: }
310:
311: public void setBufferSize(int size) {
312: _res.setBufferSize(size);
313: }
314:
315: public void flushBuffer() throws IOException {
316: _res.flushBuffer();
317: }
318:
319: public void resetBuffer() {
320: _res.resetBuffer();
321: }
322:
323: public boolean isCommitted() {
324: return false;
325: }
326:
327: public void reset() {
328: }
329:
330: public HttpServletResponse getHttpServletResponse() {
331: return _res;
332: }
333:
334: public Portlet getPortlet() {
335: if (_portlet == null) {
336: try {
337: _portlet = PortletLocalServiceUtil.getPortletById(
338: _companyId, _portletName);
339: } catch (Exception e) {
340: _log.error(e);
341: }
342: }
343:
344: return _portlet;
345: }
346:
347: public void addDateHeader(String name, long date) {
348: if (Validator.isNull(name)) {
349: throw new IllegalArgumentException();
350: }
351:
352: if (_headers.containsKey(name)) {
353: Long[] values = (Long[]) _headers.get(name);
354:
355: ArrayUtil.append(values, new Long(date));
356:
357: _headers.put(name, values);
358: } else {
359: setDateHeader(name, date);
360: }
361: }
362:
363: public void setDateHeader(String name, long date) {
364: if (Validator.isNull(name)) {
365: throw new IllegalArgumentException();
366: }
367:
368: if (date <= 0) {
369: _headers.remove(name);
370: } else {
371: _headers.put(name, new Long[] { new Long(date) });
372: }
373: }
374:
375: public void addHeader(String name, String value) {
376: if (Validator.isNull(name)) {
377: throw new IllegalArgumentException();
378: }
379:
380: if (_headers.containsKey(name)) {
381: String[] values = (String[]) _headers.get(name);
382:
383: ArrayUtil.append(values, value);
384:
385: _headers.put(name, values);
386: } else {
387: setHeader(name, value);
388: }
389: }
390:
391: public void setHeader(String name, String value) {
392: if (Validator.isNull(name)) {
393: throw new IllegalArgumentException();
394: }
395:
396: if (Validator.isNull(value)) {
397: _headers.remove(name);
398: } else {
399: _headers.put(name, new String[] { value });
400: }
401: }
402:
403: public void addIntHeader(String name, int value) {
404: if (Validator.isNull(name)) {
405: throw new IllegalArgumentException();
406: }
407:
408: if (_headers.containsKey(name)) {
409: Integer[] values = (Integer[]) _headers.get(name);
410:
411: ArrayUtil.append(values, new Integer(value));
412:
413: _headers.put(name, values);
414: } else {
415: setIntHeader(name, value);
416: }
417: }
418:
419: public void setIntHeader(String name, int value) {
420: if (Validator.isNull(name)) {
421: throw new IllegalArgumentException();
422: }
423:
424: if (value <= 0) {
425: _headers.remove(name);
426: } else {
427: _headers.put(name, new Integer[] { new Integer(value) });
428: }
429: }
430:
431: public String getResourceName() {
432: return _resourceName;
433: }
434:
435: public void setResourceName(String resourceName) {
436: _resourceName = resourceName;
437: }
438:
439: public void transferHeaders(HttpServletResponse res) {
440: Iterator itr = _headers.entrySet().iterator();
441:
442: while (itr.hasNext()) {
443: Map.Entry entry = (Map.Entry) itr.next();
444:
445: String name = (String) entry.getKey();
446: Object values = entry.getValue();
447:
448: if (values instanceof Integer[]) {
449: Integer[] intValues = (Integer[]) values;
450:
451: for (int i = 0; i < intValues.length; i++) {
452: if (res.containsHeader(name)) {
453: res.addIntHeader(name, intValues[i].intValue());
454: } else {
455: res.addIntHeader(name, intValues[i].intValue());
456: }
457: }
458: } else if (values instanceof Long[]) {
459: Long[] dateValues = (Long[]) values;
460:
461: for (int i = 0; i < dateValues.length; i++) {
462: if (res.containsHeader(name)) {
463: res.addDateHeader(name, dateValues[i]
464: .longValue());
465: } else {
466: res.addDateHeader(name, dateValues[i]
467: .longValue());
468: }
469: }
470: } else if (values instanceof String[]) {
471: String[] stringValues = (String[]) values;
472:
473: for (int i = 0; i < stringValues.length; i++) {
474: if (res.containsHeader(name)) {
475: res.addHeader(name, stringValues[i]);
476: } else {
477: res.addHeader(name, stringValues[i]);
478: }
479: }
480: }
481: }
482: }
483:
484: protected RenderResponseImpl() {
485: if (_log.isDebugEnabled()) {
486: _log.debug("Creating new instance " + hashCode());
487: }
488: }
489:
490: protected void init(RenderRequestImpl req, HttpServletResponse res,
491: String portletName, long companyId, long plid) {
492:
493: _req = req;
494: _res = res;
495: _portletName = portletName;
496: _companyId = companyId;
497: setPlid(plid);
498: _headers.clear();
499: }
500:
501: protected void recycle() {
502: if (_log.isDebugEnabled()) {
503: _log.debug("Recycling instance " + hashCode());
504: }
505:
506: _req = null;
507: _res = null;
508: _portletName = null;
509: _portlet = null;
510: _namespace = null;
511: _companyId = 0;
512: _plid = 0;
513: _urlEncoder = null;
514: _title = null;
515: _useDefaultTemplate = null;
516: _contentType = null;
517: _calledGetPortletOutputStream = false;
518: _calledGetWriter = false;
519: _headers = null;
520: _resourceName = null;
521: }
522:
523: protected RenderRequestImpl getReq() {
524: return _req;
525: }
526:
527: protected String getPortletName() {
528: return _portletName;
529: }
530:
531: protected long getCompanyId() {
532: return _companyId;
533: }
534:
535: protected long getPlid() {
536: return _plid;
537: }
538:
539: protected void setPlid(long plid) {
540: _plid = plid;
541:
542: if (_plid <= 0) {
543: Layout layout = (Layout) _req.getAttribute(WebKeys.LAYOUT);
544:
545: if (layout != null) {
546: _plid = layout.getPlid();
547: }
548: }
549: }
550:
551: protected Map getProperties() {
552: return _properties;
553: }
554:
555: protected URLEncoder getUrlEncoder() {
556: return _urlEncoder;
557: }
558:
559: protected boolean isCalledGetPortletOutputStream() {
560: return _calledGetPortletOutputStream;
561: }
562:
563: protected boolean isCalledGetWriter() {
564: return _calledGetWriter;
565: }
566:
567: private static Log _log = LogFactory
568: .getLog(RenderRequestImpl.class);
569:
570: private RenderRequestImpl _req;
571: private HttpServletResponse _res;
572: private String _portletName;
573: private Portlet _portlet;
574: private String _namespace;
575: private long _companyId;
576: private long _plid;
577: private Map _properties;
578: private URLEncoder _urlEncoder;
579: private String _title;
580: private Boolean _useDefaultTemplate;
581: private String _contentType;
582: private boolean _calledGetPortletOutputStream;
583: private boolean _calledGetWriter;
584: private LinkedHashMap _headers = new LinkedHashMap();
585: private String _resourceName;
586:
587: }
|