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.portal.velocity;
022:
023: import com.liferay.portal.kernel.language.LanguageUtil;
024: import com.liferay.portal.kernel.language.UnicodeLanguageUtil;
025: import com.liferay.portal.kernel.servlet.BrowserSniffer_IW;
026: import com.liferay.portal.kernel.servlet.ImageServletTokenUtil;
027: import com.liferay.portal.kernel.util.ArrayUtil;
028: import com.liferay.portal.kernel.util.ArrayUtil_IW;
029: import com.liferay.portal.kernel.util.DateUtil_IW;
030: import com.liferay.portal.kernel.util.GetterUtil;
031: import com.liferay.portal.kernel.util.GetterUtil_IW;
032: import com.liferay.portal.kernel.util.JavaConstants;
033: import com.liferay.portal.kernel.util.ParamUtil_IW;
034: import com.liferay.portal.kernel.util.Randomizer_IW;
035: import com.liferay.portal.kernel.util.StaticFieldGetter;
036: import com.liferay.portal.kernel.util.StringUtil_IW;
037: import com.liferay.portal.kernel.util.UnicodeFormatter_IW;
038: import com.liferay.portal.kernel.util.Validator;
039: import com.liferay.portal.kernel.util.Validator_IW;
040: import com.liferay.portal.model.Layout;
041: import com.liferay.portal.model.Theme;
042: import com.liferay.portal.service.permission.AccountPermissionUtil;
043: import com.liferay.portal.service.permission.CommonPermissionUtil;
044: import com.liferay.portal.service.permission.GroupPermissionUtil;
045: import com.liferay.portal.service.permission.LayoutPermissionUtil;
046: import com.liferay.portal.service.permission.OrganizationPermissionUtil;
047: import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil;
048: import com.liferay.portal.service.permission.PortalPermissionUtil;
049: import com.liferay.portal.service.permission.PortletPermissionUtil;
050: import com.liferay.portal.service.permission.RolePermissionUtil;
051: import com.liferay.portal.service.permission.UserGroupPermissionUtil;
052: import com.liferay.portal.service.permission.UserPermissionUtil;
053: import com.liferay.portal.theme.NavItem;
054: import com.liferay.portal.theme.RequestVars;
055: import com.liferay.portal.theme.ThemeDisplay;
056: import com.liferay.portal.util.DateFormats_IW;
057: import com.liferay.portal.util.PortalUtil_IW;
058: import com.liferay.portal.util.PrefsPropsUtil_IW;
059: import com.liferay.portal.util.PropsUtil_IW;
060: import com.liferay.portal.util.ServiceLocator;
061: import com.liferay.portal.util.SessionClicks_IW;
062: import com.liferay.portal.util.WebKeys;
063: import com.liferay.portlet.PortletConfigImpl;
064: import com.liferay.portlet.PortletURLFactory;
065: import com.liferay.util.Html_IW;
066: import com.liferay.util.HttpUtil_IW;
067:
068: import java.util.Iterator;
069: import java.util.List;
070: import java.util.Map;
071:
072: import javax.portlet.PortletRequest;
073: import javax.portlet.PortletResponse;
074: import javax.portlet.RenderRequest;
075: import javax.portlet.RenderResponse;
076:
077: import javax.servlet.http.HttpServletRequest;
078:
079: import org.apache.struts.taglib.tiles.ComponentConstants;
080: import org.apache.struts.tiles.ComponentContext;
081: import org.apache.velocity.VelocityContext;
082: import org.apache.velocity.tools.generic.DateTool;
083: import org.apache.velocity.tools.generic.EscapeTool;
084: import org.apache.velocity.tools.generic.IteratorTool;
085: import org.apache.velocity.tools.generic.ListTool;
086: import org.apache.velocity.tools.generic.MathTool;
087: import org.apache.velocity.tools.generic.NumberTool;
088: import org.apache.velocity.tools.generic.SortTool;
089:
090: /**
091: * <a href="VelocityVariables.java.html"><b><i>View Source</i></b></a>
092: *
093: * @author Brian Wing Shun Chan
094: *
095: */
096: public class VelocityVariables {
097:
098: public static void insertHelperUtilities(VelocityContext vc,
099: String[] restrictedVariables) {
100:
101: // Array util
102:
103: vc.put("arrayUtil", ArrayUtil_IW.getInstance());
104:
105: // Browser sniffer
106:
107: vc.put("browserSniffer", BrowserSniffer_IW.getInstance());
108:
109: // Date formats
110:
111: vc.put("dateFormats", DateFormats_IW.getInstance());
112:
113: // Date tool
114:
115: vc.put("dateTool", new DateTool());
116:
117: // Date util
118:
119: vc.put("dateUtil", DateUtil_IW.getInstance());
120:
121: // Escape tool
122:
123: vc.put("escapeTool", new EscapeTool());
124:
125: // Getter util
126:
127: vc.put("getterUtil", GetterUtil_IW.getInstance());
128:
129: // Html util
130:
131: vc.put("htmlUtil", Html_IW.getInstance());
132:
133: // Http util
134:
135: vc.put("httpUtil", HttpUtil_IW.getInstance());
136:
137: // ImageServletToken
138:
139: vc.put("imageToken", ImageServletTokenUtil
140: .getImageServletToken());
141:
142: // Iterator tool
143:
144: vc.put("iteratorTool", new IteratorTool());
145:
146: // Language util
147:
148: vc.put("languageUtil", LanguageUtil.getLanguage());
149: vc.put("unicodeLanguageUtil", UnicodeLanguageUtil
150: .getUnicodeLanguage());
151:
152: // List tool
153:
154: vc.put("listTool", new ListTool());
155:
156: // Math tool
157:
158: vc.put("mathTool", new MathTool());
159:
160: // Number tool
161:
162: vc.put("numberTool", new NumberTool());
163:
164: // Param util
165:
166: vc.put("paramUtil", ParamUtil_IW.getInstance());
167:
168: // Portal util
169:
170: _insertHelperUtility(vc, restrictedVariables, "portalUtil",
171: PortalUtil_IW.getInstance());
172: _insertHelperUtility(vc, restrictedVariables, "portal",
173: PortalUtil_IW.getInstance());
174:
175: // Prefs props util
176:
177: _insertHelperUtility(vc, restrictedVariables, "prefsPropsUtil",
178: PrefsPropsUtil_IW.getInstance());
179:
180: // Props util
181:
182: _insertHelperUtility(vc, restrictedVariables, "propsUtil",
183: PropsUtil_IW.getInstance());
184:
185: // Portlet URL factory
186:
187: vc.put("portletURLFactory", PortletURLFactory.getInstance());
188:
189: // Portlet preferences
190:
191: _insertHelperUtility(vc, restrictedVariables,
192: "velocityPortletPreferences",
193: new VelocityPortletPreferences());
194:
195: // Randomizer
196:
197: vc.put("randomizer", Randomizer_IW.getInstance()
198: .getWrappedInstance());
199:
200: // Service locator
201:
202: _insertHelperUtility(vc, restrictedVariables, "serviceLocator",
203: ServiceLocator.getInstance());
204:
205: // Session clicks
206:
207: _insertHelperUtility(vc, restrictedVariables, "sessionClicks",
208: SessionClicks_IW.getInstance());
209:
210: // Sort tool
211:
212: vc.put("sortTool", new SortTool());
213:
214: // Static field getter
215:
216: vc.put("staticFieldGetter", StaticFieldGetter.getInstance());
217:
218: // String util
219:
220: vc.put("stringUtil", StringUtil_IW.getInstance());
221:
222: // Unicode formatter
223:
224: vc.put("unicodeFormatter", UnicodeFormatter_IW.getInstance());
225:
226: // Validator
227:
228: vc.put("validator", Validator_IW.getInstance());
229:
230: // Permissions
231:
232: vc.put("accountPermission", AccountPermissionUtil
233: .getAccountPermission());
234: vc.put("commonPermission", CommonPermissionUtil
235: .getCommonPermission());
236: vc.put("groupPermission", GroupPermissionUtil
237: .getGroupPermission());
238: vc.put("layoutPermission", LayoutPermissionUtil
239: .getLayoutPermission());
240: vc.put("organizationPermission", OrganizationPermissionUtil
241: .getOrganizationPermission());
242: vc.put("passwordPolicyPermission", PasswordPolicyPermissionUtil
243: .getPasswordPolicyPermission());
244: vc.put("portalPermission", PortalPermissionUtil
245: .getPortalPermission());
246: vc.put("portletPermission", PortletPermissionUtil
247: .getPortletPermission());
248: vc
249: .put("rolePermission", RolePermissionUtil
250: .getRolePermission());
251: vc.put("userGroupPermission", UserGroupPermissionUtil
252: .getUserGroupPermission());
253: vc
254: .put("userPermission", UserPermissionUtil
255: .getUserPermission());
256:
257: // Deprecated permissions
258:
259: vc.put("locationPermission", OrganizationPermissionUtil
260: .getOrganizationPermission());
261: }
262:
263: public static void insertVariables(VelocityContext vc,
264: HttpServletRequest req) {
265:
266: // Request
267:
268: vc.put("request", req);
269:
270: // Portlet config
271:
272: PortletConfigImpl portletConfig = (PortletConfigImpl) req
273: .getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
274:
275: if (portletConfig != null) {
276: vc.put("portletConfig", portletConfig);
277: }
278:
279: // Render request
280:
281: PortletRequest portletRequest = (PortletRequest) req
282: .getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);
283:
284: if (portletRequest != null) {
285: if (portletRequest instanceof RenderRequest) {
286: vc.put("renderRequest", portletRequest);
287: }
288: }
289:
290: // Render response
291:
292: PortletResponse portletResponse = (PortletResponse) req
293: .getAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE);
294:
295: if (portletResponse != null) {
296: if (portletResponse instanceof RenderResponse) {
297: vc.put("renderResponse", portletResponse);
298: }
299: }
300:
301: // Theme display
302:
303: ThemeDisplay themeDisplay = (ThemeDisplay) req
304: .getAttribute(WebKeys.THEME_DISPLAY);
305:
306: if (themeDisplay != null) {
307: Theme theme = themeDisplay.getTheme();
308:
309: Layout layout = themeDisplay.getLayout();
310: List layouts = themeDisplay.getLayouts();
311:
312: vc.put("themeDisplay", themeDisplay);
313: vc.put("company", themeDisplay.getCompany());
314: vc.put("user", themeDisplay.getUser());
315: vc.put("realUser", themeDisplay.getRealUser());
316: vc.put("layout", layout);
317: vc.put("layouts", layouts);
318: vc.put("plid", String.valueOf(themeDisplay.getPlid()));
319: vc.put("layoutTypePortlet", themeDisplay
320: .getLayoutTypePortlet());
321: vc.put("portletGroupId", new Long(themeDisplay
322: .getPortletGroupId()));
323: vc.put("permissionChecker", themeDisplay
324: .getPermissionChecker());
325: vc.put("locale", themeDisplay.getLocale());
326: vc.put("timeZone", themeDisplay.getTimeZone());
327: vc.put("theme", theme);
328: vc.put("colorScheme", themeDisplay.getColorScheme());
329: vc.put("portletDisplay", themeDisplay.getPortletDisplay());
330:
331: // Navigation items
332:
333: if (layout != null) {
334: RequestVars requestVars = new RequestVars(req,
335: themeDisplay, layout.getAncestorPlid(), layout
336: .getAncestorLayoutId());
337:
338: List navItems = NavItem.fromLayouts(requestVars,
339: layouts);
340:
341: vc.put("navItems", navItems);
342: }
343:
344: // Full css and templates path
345:
346: String ctxName = GetterUtil.getString(theme
347: .getServletContextName());
348:
349: vc.put("fullCssPath", ctxName
350: + theme.getVelocityResourceListener()
351: + theme.getCssPath());
352:
353: vc.put("fullTemplatesPath", ctxName
354: + theme.getVelocityResourceListener()
355: + theme.getTemplatesPath());
356:
357: // Init
358:
359: vc.put("init", themeDisplay.getPathContext()
360: + VelocityResourceListener.SERVLET_SEPARATOR
361: + "/html/themes/_unstyled/templates/init.vm");
362: }
363:
364: // Tiles attributes
365:
366: String tilesTitle = _insertTilesVariables(vc, req,
367: "tilesTitle", "title");
368: String tilesContent = _insertTilesVariables(vc, req,
369: "tilesContent", "content");
370: boolean tilesSelectable = GetterUtil
371: .getBoolean(_insertTilesVariables(vc, req,
372: "tilesSelectable", "selectable"));
373:
374: if (themeDisplay != null) {
375: themeDisplay.setTilesTitle(tilesTitle);
376: themeDisplay.setTilesContent(tilesContent);
377: themeDisplay.setTilesSelectable(tilesSelectable);
378: }
379:
380: // Page title and subtitle
381:
382: vc.put("pageTitle", req.getAttribute(WebKeys.PAGE_TITLE));
383: vc.put("pageSubtitle", req.getAttribute(WebKeys.PAGE_SUBTITLE));
384:
385: // Helper utilities
386:
387: insertHelperUtilities(vc, null);
388:
389: // Insert custom vm variables
390:
391: Map vmVariables = (Map) req.getAttribute(WebKeys.VM_VARIABLES);
392:
393: if (vmVariables != null) {
394: Iterator itr = vmVariables.entrySet().iterator();
395:
396: while (itr.hasNext()) {
397: Map.Entry entry = (Map.Entry) itr.next();
398:
399: String key = (String) entry.getKey();
400: Object value = entry.getValue();
401:
402: if (Validator.isNotNull(key)) {
403: vc.put(key, value);
404: }
405: }
406: }
407: }
408:
409: private static void _insertHelperUtility(VelocityContext vc,
410: String[] restrictedVariables, String key, Object value) {
411:
412: if (!ArrayUtil.contains(restrictedVariables, key)) {
413: vc.put(key, value);
414: }
415: }
416:
417: private static String _insertTilesVariables(VelocityContext vc,
418: HttpServletRequest req, String attributeId,
419: String attributeName) {
420:
421: ComponentContext componentContext = (ComponentContext) req
422: .getAttribute(ComponentConstants.COMPONENT_CONTEXT);
423:
424: String value = null;
425:
426: if (componentContext != null) {
427: value = (String) componentContext
428: .getAttribute(attributeName);
429:
430: if (value != null) {
431: vc.put(attributeId, value);
432: }
433: }
434:
435: return value;
436: }
437:
438: }
|