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.layoutconfiguration.util;
022:
023: import com.liferay.portal.kernel.servlet.StringServletResponse;
024: import com.liferay.portal.kernel.util.JavaConstants;
025: import com.liferay.portal.kernel.util.MethodInvoker;
026: import com.liferay.portal.kernel.util.MethodWrapper;
027: import com.liferay.portal.kernel.util.StringMaker;
028: import com.liferay.portal.kernel.util.StringPool;
029: import com.liferay.portal.kernel.util.StringUtil;
030: import com.liferay.portal.kernel.util.Validator;
031: import com.liferay.portal.model.Portlet;
032: import com.liferay.portal.service.PortletLocalServiceUtil;
033: import com.liferay.portal.theme.PortletDisplay;
034: import com.liferay.portal.theme.PortletDisplayFactory;
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.portal.velocity.VelocityVariables;
039: import com.liferay.portlet.layoutconfiguration.util.velocity.TemplateProcessor;
040: import com.liferay.portlet.layoutconfiguration.util.xml.RuntimeLogic;
041:
042: import java.io.StringWriter;
043:
044: import java.util.Iterator;
045: import java.util.Map;
046:
047: import javax.portlet.PortletConfig;
048: import javax.portlet.RenderRequest;
049: import javax.portlet.RenderResponse;
050:
051: import javax.servlet.ServletContext;
052: import javax.servlet.http.HttpServletRequest;
053: import javax.servlet.http.HttpServletResponse;
054: import javax.servlet.jsp.PageContext;
055:
056: import org.apache.commons.logging.Log;
057: import org.apache.commons.logging.LogFactory;
058: import org.apache.velocity.VelocityContext;
059: import org.apache.velocity.app.Velocity;
060:
061: /**
062: * <a href="RuntimePortletUtil.java.html"><b><i>View Source</i></b></a>
063: *
064: * @author Brian Wing Shun Chan
065: * @author Raymond Augé
066: *
067: */
068: public class RuntimePortletUtil {
069:
070: public static void processPortlet(StringMaker sm,
071: ServletContext ctx, HttpServletRequest req,
072: HttpServletResponse res, RenderRequest renderRequest,
073: RenderResponse renderResponse, String portletId,
074: String queryString) throws Exception {
075:
076: processPortlet(sm, ctx, req, res, renderRequest,
077: renderResponse, portletId, queryString, null, null,
078: null);
079: }
080:
081: public static void processPortlet(StringMaker sm,
082: ServletContext ctx, HttpServletRequest req,
083: HttpServletResponse res, RenderRequest renderRequest,
084: RenderResponse renderResponse, String portletId,
085: String queryString, String columnId, Integer columnPos,
086: Integer columnCount) throws Exception {
087:
088: processPortlet(sm, ctx, req, res, renderRequest,
089: renderResponse, null, portletId, queryString, columnId,
090: columnPos, columnCount, null);
091: }
092:
093: public static void processPortlet(StringMaker sm,
094: ServletContext ctx, HttpServletRequest req,
095: HttpServletResponse res, Portlet portlet,
096: String queryString, String columnId, Integer columnPos,
097: Integer columnCount, String path) throws Exception {
098:
099: processPortlet(sm, ctx, req, res, null, null, portlet, portlet
100: .getPortletId(), queryString, columnId, columnPos,
101: columnCount, path);
102: }
103:
104: public static void processPortlet(StringMaker sm,
105: ServletContext ctx, HttpServletRequest req,
106: HttpServletResponse res, RenderRequest renderRequest,
107: RenderResponse renderResponse, Portlet portlet,
108: String portletId, String queryString, String columnId,
109: Integer columnPos, Integer columnCount, String path)
110: throws Exception {
111:
112: ThemeDisplay themeDisplay = (ThemeDisplay) req
113: .getAttribute(WebKeys.THEME_DISPLAY);
114:
115: if (portlet == null) {
116: portlet = PortletLocalServiceUtil.getPortletById(
117: themeDisplay.getCompanyId(), portletId);
118: }
119:
120: if ((portlet != null) && portlet.isInstanceable()) {
121: String instanceId = portlet.getInstanceId();
122:
123: if (Validator.isNotNull(instanceId)
124: && Validator.isPassword(instanceId)
125: && (instanceId.length() == 4)) {
126:
127: /*portletId +=
128: PortletImpl.INSTANCE_SEPARATOR + instanceId;
129:
130: portlet = PortletLocalServiceUtil.getPortletById(
131: themeDisplay.getCompanyId(), portletId);*/
132: } else {
133: if (_log.isDebugEnabled()) {
134: _log.debug("Portlet " + portlet.getPortletId()
135: + " is instanceable but does not have a "
136: + "valid instance id");
137: }
138:
139: portlet = null;
140: }
141: }
142:
143: if (portlet == null) {
144: return;
145: }
146:
147: // Capture the current portlet's settings to reset them once the child
148: // portlet is rendered
149:
150: PortletDisplay portletDisplay = themeDisplay
151: .getPortletDisplay();
152:
153: PortletDisplay portletDisplayClone = PortletDisplayFactory
154: .create();
155:
156: portletDisplay.copyTo(portletDisplayClone);
157:
158: PortletConfig portletConfig = (PortletConfig) req
159: .getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
160:
161: try {
162: PortalUtil
163: .renderPortlet(sm, ctx, req, res, portlet,
164: queryString, columnId, columnPos,
165: columnCount, path);
166: } finally {
167: portletDisplay.copyFrom(portletDisplayClone);
168:
169: try {
170: PortletDisplayFactory.recycle(portletDisplayClone);
171: } catch (Exception e) {
172: _log.error(e);
173: }
174:
175: _defineObjects(req, portletConfig, renderRequest,
176: renderResponse);
177: }
178: }
179:
180: public static String processTemplate(ServletContext ctx,
181: HttpServletRequest req, HttpServletResponse res,
182: PageContext pageContext, String content) throws Exception {
183:
184: return processTemplate(ctx, req, res, pageContext, null,
185: content);
186: }
187:
188: public static String processTemplate(ServletContext ctx,
189: HttpServletRequest req, HttpServletResponse res,
190: PageContext pageContext, String portletId, String content)
191: throws Exception {
192:
193: if (Validator.isNull(content)) {
194: return StringPool.BLANK;
195: }
196:
197: TemplateProcessor processor = new TemplateProcessor(ctx, req,
198: res, portletId);
199:
200: VelocityContext vc = new VelocityContext();
201:
202: vc.put("processor", processor);
203:
204: // Velocity variables
205:
206: VelocityVariables.insertVariables(vc, req);
207:
208: // liferay:include tag library
209:
210: StringServletResponse stringServletResponse = new StringServletResponse(
211: res);
212:
213: MethodWrapper methodWrapper = new MethodWrapper(
214: "com.liferay.taglib.util.VelocityTaglib", "init",
215: new Object[] { ctx, req, stringServletResponse,
216: pageContext });
217:
218: Object velocityTaglib = MethodInvoker.invoke(methodWrapper);
219:
220: vc.put("taglibLiferay", velocityTaglib);
221: vc.put("theme", velocityTaglib);
222:
223: StringWriter sw = new StringWriter();
224:
225: try {
226: Velocity.evaluate(vc, sw, RuntimePortletUtil.class
227: .getName(), content);
228: } catch (Exception e) {
229: _log.error(e, e);
230:
231: throw e;
232: }
233:
234: String output = sw.toString();
235:
236: Map columnsMap = processor.getColumnsMap();
237:
238: Iterator itr = columnsMap.entrySet().iterator();
239:
240: while (itr.hasNext()) {
241: Map.Entry entry = (Map.Entry) itr.next();
242:
243: String key = (String) entry.getKey();
244: String value = (String) entry.getValue();
245:
246: output = StringUtil.replace(output, key, value);
247: }
248:
249: Map portletsMap = processor.getPortletsMap();
250:
251: itr = portletsMap.entrySet().iterator();
252:
253: while (itr.hasNext()) {
254: Map.Entry entry = (Map.Entry) itr.next();
255:
256: Portlet portlet = (Portlet) entry.getKey();
257: Object[] value = (Object[]) entry.getValue();
258:
259: String queryString = (String) value[0];
260: String columnId = (String) value[1];
261: Integer columnPos = (Integer) value[2];
262: Integer columnCount = (Integer) value[3];
263:
264: StringMaker sm = new StringMaker();
265:
266: processPortlet(sm, ctx, req, res, portlet, queryString,
267: columnId, columnPos, columnCount, null);
268:
269: output = StringUtil.replace(output, "[$TEMPLATE_PORTLET_"
270: + portlet.getPortletId() + "$]", sm.toString());
271: }
272:
273: return output;
274: }
275:
276: public static String processXML(HttpServletRequest req,
277: String content, RuntimeLogic runtimeLogic) throws Exception {
278:
279: if (Validator.isNull(content)) {
280: return StringPool.BLANK;
281: }
282:
283: try {
284: req.setAttribute(WebKeys.RENDER_PORTLET_RESOURCE,
285: Boolean.TRUE);
286:
287: StringMaker sm = new StringMaker();
288:
289: int x = 0;
290: int y = content.indexOf(runtimeLogic.getOpenTag());
291:
292: while (y != -1) {
293: sm.append(content.substring(x, y));
294:
295: int close1 = content.indexOf(runtimeLogic
296: .getClose1Tag(), y);
297: int close2 = content.indexOf(runtimeLogic
298: .getClose2Tag(), y);
299:
300: if ((close2 == -1)
301: || ((close1 != -1) && (close1 < close2))) {
302: x = close1 + runtimeLogic.getClose1Tag().length();
303: } else {
304: x = close2 + runtimeLogic.getClose2Tag().length();
305: }
306:
307: runtimeLogic.processXML(sm, content.substring(y, x));
308:
309: y = content.indexOf(runtimeLogic.getOpenTag(), x);
310: }
311:
312: if (y == -1) {
313: sm.append(content.substring(x, content.length()));
314: }
315:
316: return sm.toString();
317: } finally {
318: req.removeAttribute(WebKeys.RENDER_PORTLET_RESOURCE);
319: }
320: }
321:
322: private static void _defineObjects(HttpServletRequest req,
323: PortletConfig portletConfig, RenderRequest renderRequest,
324: RenderResponse renderResponse) {
325:
326: if (portletConfig != null) {
327: req.setAttribute(JavaConstants.JAVAX_PORTLET_CONFIG,
328: portletConfig);
329: }
330:
331: if (renderRequest != null) {
332: req.setAttribute(JavaConstants.JAVAX_PORTLET_REQUEST,
333: renderRequest);
334: }
335:
336: if (renderResponse != null) {
337: req.setAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE,
338: renderResponse);
339: }
340: }
341:
342: private static Log _log = LogFactory
343: .getLog(RuntimePortletUtil.class);
344:
345: }
|