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.taglib.portletext;
022:
023: import com.liferay.portal.kernel.util.JavaConstants;
024: import com.liferay.portal.kernel.util.StringMaker;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.portal.model.Layout;
027: import com.liferay.portal.util.WebKeys;
028: import com.liferay.portlet.PortletPreferencesFactoryUtil;
029: import com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil;
030:
031: import javax.portlet.PortletRequest;
032: import javax.portlet.PortletResponse;
033: import javax.portlet.RenderRequest;
034: import javax.portlet.RenderResponse;
035:
036: import javax.servlet.ServletContext;
037: import javax.servlet.http.HttpServletRequest;
038: import javax.servlet.http.HttpServletResponse;
039: import javax.servlet.jsp.JspException;
040: import javax.servlet.jsp.PageContext;
041: import javax.servlet.jsp.tagext.TagSupport;
042:
043: import org.apache.commons.logging.Log;
044: import org.apache.commons.logging.LogFactory;
045:
046: /**
047: * <a href="RuntimeTag.java.html"><b><i>View Source</i></b></a>
048: *
049: * @author Brian Wing Shun Chan
050: *
051: */
052: public class RuntimeTag extends TagSupport {
053:
054: public static void doTag(String portletName,
055: PageContext pageContext, ServletContext ctx,
056: HttpServletRequest req, HttpServletResponse res)
057: throws Exception {
058:
059: doTag(portletName, null, pageContext, ctx, req, res);
060: }
061:
062: public static void doTag(String portletName, String queryString,
063: PageContext pageContext, ServletContext ctx,
064: HttpServletRequest req, HttpServletResponse res)
065: throws Exception {
066:
067: doTag(portletName, null, null, pageContext, ctx, req, res);
068: }
069:
070: public static void doTag(String portletName, String queryString,
071: String defaultPreferences, PageContext pageContext,
072: ServletContext ctx, HttpServletRequest req,
073: HttpServletResponse res) throws Exception {
074:
075: PortletRequest portletRequest = (PortletRequest) req
076: .getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);
077:
078: RenderRequest renderRequest = null;
079:
080: if ((portletRequest != null)
081: && (portletRequest instanceof RenderRequest)) {
082:
083: renderRequest = (RenderRequest) portletRequest;
084: }
085:
086: PortletResponse portletResponse = (PortletResponse) req
087: .getAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE);
088:
089: RenderResponse renderResponse = null;
090:
091: if ((portletResponse != null)
092: && (portletResponse instanceof RenderResponse)) {
093:
094: renderResponse = (RenderResponse) portletResponse;
095: }
096:
097: String portletId = portletName;
098:
099: StringMaker renderPortletSM = new StringMaker();
100:
101: try {
102: req.setAttribute(WebKeys.RENDER_PORTLET_RESOURCE,
103: Boolean.TRUE);
104:
105: if (Validator.isNotNull(defaultPreferences)) {
106: PortletPreferencesFactoryUtil.getPortletSetup(req,
107: portletId, true, true, defaultPreferences);
108: }
109:
110: RuntimePortletUtil.processPortlet(renderPortletSM, ctx,
111: req, res, renderRequest, renderResponse, portletId,
112: queryString);
113: } finally {
114: req.removeAttribute(WebKeys.RENDER_PORTLET_RESOURCE);
115: }
116:
117: if (pageContext != null) {
118: pageContext.getOut().print(renderPortletSM.toString());
119: } else {
120:
121: // LEP-1023
122:
123: //res.getOutputStream().print(renderPortletSM.toString());
124: res.getWriter().print(renderPortletSM.toString());
125: }
126: }
127:
128: public int doEndTag() throws JspException {
129: try {
130: HttpServletRequest req = (HttpServletRequest) pageContext
131: .getRequest();
132:
133: Layout layout = (Layout) req.getAttribute(WebKeys.LAYOUT);
134:
135: if (layout == null) {
136: return EVAL_PAGE;
137: }
138:
139: ServletContext ctx = (ServletContext) req
140: .getAttribute(WebKeys.CTX);
141:
142: HttpServletResponse res = (HttpServletResponse) pageContext
143: .getResponse();
144:
145: doTag(_portletName, _queryString, _defaultPreferences,
146: pageContext, ctx, req, res);
147:
148: return EVAL_PAGE;
149: } catch (Exception e) {
150: _log.error(e, e);
151:
152: throw new JspException(e);
153: }
154: }
155:
156: public void setPortletName(String portletName) {
157: _portletName = portletName;
158: }
159:
160: public void setQueryString(String queryString) {
161: _queryString = queryString;
162: }
163:
164: public void setDefaultPreferences(String defaultPreferences) {
165: _defaultPreferences = defaultPreferences;
166: }
167:
168: private static Log _log = LogFactory.getLog(RuntimeTag.class);
169:
170: private String _portletName;
171: private String _queryString;
172: private String _defaultPreferences;
173:
174: }
|