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.portal.velocity;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.language.UnicodeLanguageUtil;
27  import com.liferay.portal.kernel.servlet.BrowserSniffer_IW;
28  import com.liferay.portal.kernel.servlet.ImageServletTokenUtil;
29  import com.liferay.portal.kernel.util.ArrayUtil_IW;
30  import com.liferay.portal.kernel.util.DateUtil_IW;
31  import com.liferay.portal.kernel.util.GetterUtil;
32  import com.liferay.portal.kernel.util.GetterUtil_IW;
33  import com.liferay.portal.kernel.util.JavaConstants;
34  import com.liferay.portal.kernel.util.ParamUtil_IW;
35  import com.liferay.portal.kernel.util.Randomizer_IW;
36  import com.liferay.portal.kernel.util.StaticFieldGetter;
37  import com.liferay.portal.kernel.util.StringUtil_IW;
38  import com.liferay.portal.kernel.util.UnicodeFormatter_IW;
39  import com.liferay.portal.kernel.util.Validator;
40  import com.liferay.portal.model.Layout;
41  import com.liferay.portal.model.Theme;
42  import com.liferay.portal.service.permission.AccountPermissionUtil;
43  import com.liferay.portal.service.permission.CommonPermissionUtil;
44  import com.liferay.portal.service.permission.GroupPermissionUtil;
45  import com.liferay.portal.service.permission.LayoutPermissionUtil;
46  import com.liferay.portal.service.permission.LocationPermissionUtil;
47  import com.liferay.portal.service.permission.OrganizationPermissionUtil;
48  import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil;
49  import com.liferay.portal.service.permission.PortalPermissionUtil;
50  import com.liferay.portal.service.permission.PortletPermissionUtil;
51  import com.liferay.portal.service.permission.RolePermissionUtil;
52  import com.liferay.portal.service.permission.UserGroupPermissionUtil;
53  import com.liferay.portal.service.permission.UserPermissionUtil;
54  import com.liferay.portal.theme.NavItem;
55  import com.liferay.portal.theme.RequestVars;
56  import com.liferay.portal.theme.ThemeDisplay;
57  import com.liferay.portal.util.DateFormats_IW;
58  import com.liferay.portal.util.PortalUtil_IW;
59  import com.liferay.portal.util.PrefsPropsUtil_IW;
60  import com.liferay.portal.util.PropsUtil_IW;
61  import com.liferay.portal.util.ServiceLocator;
62  import com.liferay.portal.util.SessionClicks_IW;
63  import com.liferay.portal.util.WebKeys;
64  import com.liferay.portlet.PortletConfigImpl;
65  import com.liferay.portlet.PortletURLFactory;
66  import com.liferay.util.Html_IW;
67  import com.liferay.util.HttpUtil_IW;
68  
69  import java.util.Iterator;
70  import java.util.List;
71  import java.util.Map;
72  
73  import javax.portlet.PortletRequest;
74  import javax.portlet.PortletResponse;
75  import javax.portlet.RenderRequest;
76  import javax.portlet.RenderResponse;
77  
78  import javax.servlet.http.HttpServletRequest;
79  
80  import org.apache.struts.taglib.tiles.ComponentConstants;
81  import org.apache.struts.tiles.ComponentContext;
82  import org.apache.velocity.VelocityContext;
83  
84  /**
85   * <a href="VelocityVariables.java.html"><b><i>View Source</i></b></a>
86   *
87   * @author Brian Wing Shun Chan
88   *
89   */
90  public class VelocityVariables {
91  
92      public static void insertHelperUtilities(VelocityContext vc) {
93  
94          // Array util
95  
96          vc.put("arrayUtil", ArrayUtil_IW.getInstance());
97  
98          // Browser sniffer
99  
100         vc.put("browserSniffer", BrowserSniffer_IW.getInstance());
101 
102         // Date formats
103 
104         vc.put("dateFormats", DateFormats_IW.getInstance());
105 
106         // Date util
107 
108         vc.put("dateUtil", DateUtil_IW.getInstance());
109 
110         // Getter util
111 
112         vc.put("getterUtil", GetterUtil_IW.getInstance());
113 
114         // Html util
115 
116         vc.put("htmlUtil", Html_IW.getInstance());
117 
118         // Http util
119 
120         vc.put("httpUtil", HttpUtil_IW.getInstance());
121 
122         // ImageServletToken
123 
124         vc.put("imageToken", ImageServletTokenUtil.getImageServletToken());
125 
126         // Language util
127 
128         vc.put("languageUtil", LanguageUtil.getLanguage());
129         vc.put("unicodeLanguageUtil", UnicodeLanguageUtil.getUnicodeLanguage());
130 
131         // Param util
132 
133         vc.put("paramUtil", ParamUtil_IW.getInstance());
134 
135         // Portal util
136 
137         vc.put("portalUtil", PortalUtil_IW.getInstance());
138         vc.put("portal", PortalUtil_IW.getInstance());
139 
140         // Prefs props util
141 
142         vc.put("prefsPropsUtil", PrefsPropsUtil_IW.getInstance());
143 
144         // Props util
145 
146         vc.put("propsUtil", PropsUtil_IW.getInstance());
147 
148         // Portlet URL factory
149 
150         vc.put("portletURLFactory", PortletURLFactory.getInstance());
151 
152         // Portlet preferences
153 
154         vc.put("velocityPortletPreferences", new VelocityPortletPreferences());
155 
156         // Randomizer
157 
158         vc.put("randomizer", Randomizer_IW.getInstance().getWrappedInstance());
159 
160         // Service locator
161 
162         vc.put("serviceLocator", ServiceLocator.getInstance());
163 
164         // Session clicks
165 
166         vc.put("sessionClicks", SessionClicks_IW.getInstance());
167 
168         // Static field getter
169 
170         vc.put("staticFieldGetter", StaticFieldGetter.getInstance());
171 
172         // String util
173 
174         vc.put("stringUtil", StringUtil_IW.getInstance());
175 
176         // Unicode formatter
177 
178         vc.put("unicodeFormatter", UnicodeFormatter_IW.getInstance());
179 
180         // Permissions
181 
182         vc.put(
183             "accountPermission", AccountPermissionUtil.getAccountPermission());
184         vc.put("commonPermission", CommonPermissionUtil.getCommonPermission());
185         vc.put("groupPermission", GroupPermissionUtil.getGroupPermission());
186         vc.put("layoutPermission", LayoutPermissionUtil.getLayoutPermission());
187         vc.put(
188             "locationPermission",
189             LocationPermissionUtil.getLocationPermission());
190         vc.put(
191             "organizationPermission",
192             OrganizationPermissionUtil.getOrganizationPermission());
193         vc.put(
194             "passwordPolicyPermission",
195             PasswordPolicyPermissionUtil.getPasswordPolicyPermission());
196         vc.put("portalPermission", PortalPermissionUtil.getPortalPermission());
197         vc.put(
198             "portletPermission", PortletPermissionUtil.getPortletPermission());
199         vc.put("rolePermission", RolePermissionUtil.getRolePermission());
200         vc.put(
201             "userGroupPermission",
202             UserGroupPermissionUtil.getUserGroupPermission());
203         vc.put("userPermission", UserPermissionUtil.getUserPermission());
204     }
205 
206     public static void insertVariables(
207         VelocityContext vc, HttpServletRequest req) {
208 
209         // Request
210 
211         vc.put("request", req);
212 
213         // Portlet config
214 
215         PortletConfigImpl portletConfig = (PortletConfigImpl)req.getAttribute(
216             JavaConstants.JAVAX_PORTLET_CONFIG);
217 
218         if (portletConfig != null) {
219             vc.put("portletConfig", portletConfig);
220         }
221 
222         // Render request
223 
224         PortletRequest portletRequest = (PortletRequest)req.getAttribute(
225             JavaConstants.JAVAX_PORTLET_REQUEST);
226 
227         if (portletRequest != null) {
228             if (portletRequest instanceof RenderRequest) {
229                 vc.put("renderRequest", portletRequest);
230             }
231         }
232 
233         // Render response
234 
235         PortletResponse portletResponse = (PortletResponse)req.getAttribute(
236             JavaConstants.JAVAX_PORTLET_RESPONSE);
237 
238         if (portletResponse != null) {
239             if (portletResponse instanceof RenderResponse) {
240                 vc.put("renderResponse", portletResponse);
241             }
242         }
243 
244         // Theme display
245 
246         ThemeDisplay themeDisplay =
247             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
248 
249         if (themeDisplay != null) {
250             Theme theme = themeDisplay.getTheme();
251 
252             Layout layout = themeDisplay.getLayout();
253             List layouts = themeDisplay.getLayouts();
254 
255             vc.put("themeDisplay", themeDisplay);
256             vc.put("company", themeDisplay.getCompany());
257             vc.put("user", themeDisplay.getUser());
258             vc.put("realUser", themeDisplay.getRealUser());
259             vc.put("layout", layout);
260             vc.put("layouts", layouts);
261             vc.put("plid", String.valueOf(themeDisplay.getPlid()));
262             vc.put("layoutTypePortlet", themeDisplay.getLayoutTypePortlet());
263             vc.put(
264                 "portletGroupId", new Long(themeDisplay.getPortletGroupId()));
265             vc.put("permissionChecker", themeDisplay.getPermissionChecker());
266             vc.put("locale", themeDisplay.getLocale());
267             vc.put("timeZone", themeDisplay.getTimeZone());
268             vc.put("theme", theme);
269             vc.put("colorScheme", themeDisplay.getColorScheme());
270             vc.put("portletDisplay", themeDisplay.getPortletDisplay());
271 
272             // Navigation items
273 
274             if (layout != null) {
275                 RequestVars requestVars = new RequestVars(
276                     req, themeDisplay, layout.getAncestorPlid(),
277                     layout.getAncestorLayoutId());
278 
279                 List navItems = NavItem.fromLayouts(requestVars, layouts);
280 
281                 vc.put("navItems", navItems);
282             }
283 
284             // Full css and templates path
285 
286             String ctxName = GetterUtil.getString(
287                 theme.getServletContextName());
288 
289             vc.put(
290                 "fullCssPath",
291                 ctxName + theme.getVelocityResourceListener() +
292                     theme.getCssPath());
293 
294             vc.put(
295                 "fullTemplatesPath",
296                 ctxName + theme.getVelocityResourceListener() +
297                     theme.getTemplatesPath());
298 
299             // Init
300 
301             vc.put(
302                 "init",
303                 themeDisplay.getPathContext() +
304                     VelocityResourceListener.SERVLET_SEPARATOR +
305                         "/html/themes/_unstyled/templates/init.vm");
306         }
307 
308         // Tiles attributes
309 
310         String tilesTitle = _insertTilesVariables(
311             vc, req, "tilesTitle", "title");
312         String tilesContent = _insertTilesVariables(
313             vc, req, "tilesContent", "content");
314         boolean tilesSelectable = GetterUtil.getBoolean(_insertTilesVariables(
315             vc, req, "tilesSelectable", "selectable"));
316 
317         if (themeDisplay != null) {
318             themeDisplay.setTilesTitle(tilesTitle);
319             themeDisplay.setTilesContent(tilesContent);
320             themeDisplay.setTilesSelectable(tilesSelectable);
321         }
322 
323         // Page title and subtitle
324 
325         vc.put("pageTitle", req.getAttribute(WebKeys.PAGE_TITLE));
326         vc.put("pageSubtitle", req.getAttribute(WebKeys.PAGE_SUBTITLE));
327 
328         // Helper utilities
329 
330         insertHelperUtilities(vc);
331 
332         // Insert custom vm variables
333 
334         Map vmVariables = (Map)req.getAttribute(WebKeys.VM_VARIABLES);
335 
336         if (vmVariables != null) {
337             Iterator itr = vmVariables.entrySet().iterator();
338 
339             while (itr.hasNext()) {
340                 Map.Entry entry = (Map.Entry)itr.next();
341 
342                 String key = (String)entry.getKey();
343                 Object value = entry.getValue();
344 
345                 if (Validator.isNotNull(key)) {
346                     vc.put(key, value);
347                 }
348             }
349         }
350     }
351 
352     private static String _insertTilesVariables(
353         VelocityContext vc, HttpServletRequest req, String attributeId,
354         String attributeName) {
355 
356         ComponentContext componentContext = (ComponentContext)req.getAttribute(
357             ComponentConstants.COMPONENT_CONTEXT);
358 
359         String value = null;
360 
361         if (componentContext != null) {
362             value = (String)componentContext.getAttribute(attributeName);
363 
364             if (value != null) {
365                 vc.put(attributeId, value);
366             }
367         }
368 
369         return value;
370     }
371 
372 }