1   /**
2    * Copyright (c) 2000-2006 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.util;
24  
25  import com.germinus.easyconf.Filter;
26  
27  import com.liferay.portal.NoSuchLayoutException;
28  import com.liferay.portal.NoSuchUserException;
29  import com.liferay.portal.PortalException;
30  import com.liferay.portal.SystemException;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.language.LanguageException;
33  import com.liferay.portal.language.LanguageUtil;
34  import com.liferay.portal.model.Company;
35  import com.liferay.portal.model.Group;
36  import com.liferay.portal.model.Layout;
37  import com.liferay.portal.model.LayoutSet;
38  import com.liferay.portal.model.LayoutTypePortlet;
39  import com.liferay.portal.model.Portlet;
40  import com.liferay.portal.model.User;
41  import com.liferay.portal.model.impl.GroupImpl;
42  import com.liferay.portal.model.impl.LayoutImpl;
43  import com.liferay.portal.model.impl.RoleImpl;
44  import com.liferay.portal.security.permission.ActionKeys;
45  import com.liferay.portal.security.permission.PermissionCheckerFactory;
46  import com.liferay.portal.security.permission.PermissionCheckerImpl;
47  import com.liferay.portal.service.CompanyLocalServiceUtil;
48  import com.liferay.portal.service.GroupLocalServiceUtil;
49  import com.liferay.portal.service.LayoutLocalServiceUtil;
50  import com.liferay.portal.service.LayoutServiceUtil;
51  import com.liferay.portal.service.PortletLocalServiceUtil;
52  import com.liferay.portal.service.UserLocalServiceUtil;
53  import com.liferay.portal.service.UserServiceUtil;
54  import com.liferay.portal.service.permission.UserPermission;
55  import com.liferay.portal.servlet.FriendlyURLPortletPlugin;
56  import com.liferay.portal.servlet.PortletContextPool;
57  import com.liferay.portal.servlet.PortletContextWrapper;
58  import com.liferay.portal.theme.ThemeDisplay;
59  import com.liferay.portlet.ActionRequestImpl;
60  import com.liferay.portlet.ActionResponseImpl;
61  import com.liferay.portlet.CachePortlet;
62  import com.liferay.portlet.LiferayPortletMode;
63  import com.liferay.portlet.LiferayWindowState;
64  import com.liferay.portlet.PortletConfigFactory;
65  import com.liferay.portlet.PortletPreferencesImpl;
66  import com.liferay.portlet.PortletPreferencesWrapper;
67  import com.liferay.portlet.PortletURLImpl;
68  import com.liferay.portlet.RenderRequestImpl;
69  import com.liferay.portlet.wsrp.URLGeneratorImpl;
70  import com.liferay.util.CollectionFactory;
71  import com.liferay.util.Encryptor;
72  import com.liferay.util.GetterUtil;
73  import com.liferay.util.Http;
74  import com.liferay.util.InstancePool;
75  import com.liferay.util.JS;
76  import com.liferay.util.ParamUtil;
77  import com.liferay.util.StringComparator;
78  import com.liferay.util.StringUtil;
79  import com.liferay.util.Validator;
80  import com.liferay.util.portlet.PortletRequestWrapper;
81  import com.liferay.util.servlet.DynamicServletRequest;
82  import com.liferay.util.servlet.StringServletResponse;
83  import com.liferay.util.servlet.UploadPortletRequest;
84  import com.liferay.util.servlet.UploadServletRequest;
85  
86  import java.io.IOException;
87  
88  import java.rmi.RemoteException;
89  
90  import java.util.Arrays;
91  import java.util.Calendar;
92  import java.util.Date;
93  import java.util.Enumeration;
94  import java.util.GregorianCalendar;
95  import java.util.Iterator;
96  import java.util.List;
97  import java.util.Locale;
98  import java.util.Map;
99  import java.util.Properties;
100 import java.util.ResourceBundle;
101 import java.util.Set;
102 import java.util.TimeZone;
103 
104 import javax.portlet.ActionRequest;
105 import javax.portlet.ActionResponse;
106 import javax.portlet.PortletConfig;
107 import javax.portlet.PortletMode;
108 import javax.portlet.PortletPreferences;
109 import javax.portlet.PortletRequest;
110 import javax.portlet.PortletURL;
111 import javax.portlet.PreferencesValidator;
112 import javax.portlet.RenderRequest;
113 import javax.portlet.ValidatorException;
114 import javax.portlet.WindowState;
115 
116 import javax.servlet.RequestDispatcher;
117 import javax.servlet.ServletContext;
118 import javax.servlet.ServletException;
119 import javax.servlet.ServletRequest;
120 import javax.servlet.http.HttpServletRequest;
121 import javax.servlet.http.HttpServletRequestWrapper;
122 import javax.servlet.http.HttpServletResponse;
123 import javax.servlet.http.HttpSession;
124 
125 import org.apache.commons.logging.Log;
126 import org.apache.commons.logging.LogFactory;
127 import org.apache.struts.Globals;
128 
129 import org.hibernate.util.FastHashMap;
130 
131 /**
132  * <a href="PortalUtil.java.html"><b><i>View Source</i></b></a>
133  *
134  * @author  Brian Wing Shun Chan
135  * @author  Brian Myunghun Kim
136  * @author  Jorge Ferrer
137  *
138  */
139 public class PortalUtil {
140 
141     public static void clearRequestParameters(RenderRequest req) {
142 
143         // Clear the render parameters if they were set during processAction
144 
145         boolean action = ParamUtil.getBoolean(req, "p_p_action");
146 
147         if (action) {
148             ((RenderRequestImpl)req).getRenderParameters().clear();
149         }
150     }
151 
152     public static void copyRequestParameters(
153         ActionRequest req, ActionResponse res) {
154 
155         try {
156             HttpServletRequest originalReq = getOriginalServletRequest(
157                 getHttpServletRequest(req));
158 
159             ActionResponseImpl resImpl = (ActionResponseImpl)res;
160 
161             Map renderParameters = resImpl.getRenderParameters();
162 
163             res.setRenderParameter("p_p_action", "1");
164 
165             Enumeration enu = req.getParameterNames();
166 
167             while (enu.hasMoreElements()) {
168                 String param = (String)enu.nextElement();
169                 String[] values = req.getParameterValues(param);
170 
171                 if (renderParameters.get(
172                         resImpl.getNamespace() + param) == null) {
173 
174                     res.setRenderParameter(param, values);
175                 }
176             }
177         }
178         catch (IllegalStateException ise) {
179 
180             // This should only happen if the developer called
181             // sendRedirect of javax.portlet.ActionResponse
182 
183         }
184     }
185 
186     public static String createSecureProxyURL(String url, boolean secure) {
187 
188         // Use this method to fetch external content that may not be available
189         // in secure mode. See how the Weather portlet fetches images.
190 
191         if (!secure) {
192             return url;
193         }
194         else {
195             Map params = CollectionFactory.getHashMap();
196 
197             params.put(org.apache.wsrp4j.util.Constants.URL, url);
198 
199             return URLGeneratorImpl.getResourceProxyURL(params);
200         }
201     }
202 
203     public static Company getCompany(HttpServletRequest req)
204         throws PortalException, SystemException {
205 
206         String companyId = getCompanyId(req);
207 
208         if (companyId == null) {
209             return null;
210         }
211 
212         Company company = (Company)req.getAttribute(WebKeys.COMPANY);
213 
214         if (company == null) {
215             company = CompanyLocalServiceUtil.getCompany(companyId);
216 
217             req.setAttribute(WebKeys.COMPANY, company);
218         }
219 
220         return company;
221     }
222 
223     public static Company getCompany(ActionRequest req)
224         throws PortalException, SystemException {
225 
226         return getCompany(getHttpServletRequest(req));
227     }
228 
229     public static Company getCompany(RenderRequest req)
230         throws PortalException, SystemException {
231 
232         return getCompany(getHttpServletRequest(req));
233     }
234 
235     public static String getCompanyId(HttpServletRequest req) {
236         String companyId =
237             (String)req.getSession().getAttribute(WebKeys.COMPANY_ID);
238 
239         if (companyId == null) {
240             companyId = (String)req.getAttribute(WebKeys.COMPANY_ID);
241         }
242 
243         return companyId;
244     }
245 
246     public static String getCompanyId(ActionRequest req) {
247         return getCompanyId(getHttpServletRequest(req));
248     }
249 
250     public static String getCompanyId(PortletRequest req) {
251         String companyId = null;
252 
253         if (req instanceof ActionRequest) {
254             companyId = getCompanyId((ActionRequest)req);
255         }
256         else {
257             companyId = getCompanyId((RenderRequest)req);
258         }
259 
260         return companyId;
261     }
262 
263     public static String getCompanyId(RenderRequest req) {
264         return getCompanyId(getHttpServletRequest(req));
265     }
266 
267     public static Date getDate(
268             int month, int day, int year, PortalException pe)
269         throws PortalException {
270 
271         return getDate(month, day, year, null, pe);
272     }
273 
274     public static Date getDate(
275             int month, int day, int year, TimeZone timeZone, PortalException pe)
276         throws PortalException {
277 
278         return getDate(month, day, year, -1, -1, timeZone, pe);
279     }
280 
281     public static Date getDate(
282             int month, int day, int year, int hour, int min, PortalException pe)
283         throws PortalException {
284 
285         return getDate(month, day, year, hour, min, null, pe);
286     }
287 
288     public static Date getDate(
289             int month, int day, int year, int hour, int min, TimeZone timeZone,
290             PortalException pe)
291         throws PortalException {
292 
293         if (!Validator.isGregorianDate(month, day, year)) {
294             throw pe;
295         }
296         else {
297             Calendar cal = null;
298 
299             if (timeZone == null) {
300                 cal = new GregorianCalendar();
301             }
302             else {
303                 cal = new GregorianCalendar(timeZone);
304             }
305 
306             if ((hour == -1) || (min == -1)) {
307                 cal.set(year, month, day);
308             }
309             else {
310                 cal.set(year, month, day, hour, min, 0);
311             }
312 
313             Date date = cal.getTime();
314 
315             /*if (timeZone != null &&
316                 cal.before(new GregorianCalendar(timeZone))) {
317 
318                 throw pe;
319             }*/
320 
321             return date;
322         }
323     }
324 
325     public static String getHost(HttpServletRequest req) {
326         String host = req.getHeader("Host");
327 
328         if (host != null) {
329             int pos = host.indexOf(':');
330 
331             if (pos >= 0) {
332                 host = host.substring(0, pos);
333             }
334         }
335         else {
336             host = null;
337         }
338 
339         return host;
340     }
341 
342     public static String getHost(ActionRequest req) {
343         return getHost(getHttpServletRequest(req));
344     }
345 
346     public static String getHost(RenderRequest req) {
347         return getHost(getHttpServletRequest(req));
348     }
349 
350     public static HttpServletRequest getHttpServletRequest(PortletRequest req) {
351         if (req instanceof ActionRequestImpl) {
352             ActionRequestImpl reqImpl = (ActionRequestImpl)req;
353 
354             return reqImpl.getHttpServletRequest();
355         }
356         else if (req instanceof RenderRequestImpl) {
357             RenderRequestImpl reqImpl = (RenderRequestImpl)req;
358 
359             return reqImpl.getHttpServletRequest();
360         }
361         else if (req instanceof PortletRequestWrapper) {
362             PortletRequestWrapper reqWrapper = (PortletRequestWrapper)req;
363 
364             return getHttpServletRequest(reqWrapper.getPortletRequest());
365         }
366         else {
367             throw new RuntimeException(
368                 "Unable to get the HTTP servlet request from " +
369                     req.getClass().getName());
370         }
371     }
372 
373     public static String getLayoutEditPage(Layout layout) {
374         return PropsUtil.getComponentProperties().getString(
375             PropsUtil.LAYOUT_EDIT_PAGE, Filter.by(layout.getType()));
376     }
377 
378     public static String getLayoutViewPage(Layout layout) {
379         return PropsUtil.getComponentProperties().getString(
380             PropsUtil.LAYOUT_VIEW_PAGE, Filter.by(layout.getType()));
381     }
382 
383     public static String getLayoutURL(Layout layout, ThemeDisplay themeDisplay)
384         throws PortalException, SystemException {
385 
386         return getLayoutURL(layout, themeDisplay, true);
387     }
388 
389     public static String getLayoutURL(
390             Layout layout, ThemeDisplay themeDisplay, boolean doAsUser)
391         throws PortalException, SystemException {
392 
393         String layoutFriendlyURL = getLayoutFriendlyURL(layout, themeDisplay);
394 
395         if (Validator.isNotNull(layoutFriendlyURL)) {
396             if (doAsUser && Validator.isNotNull(themeDisplay.getDoAsUserId())) {
397                 layoutFriendlyURL = Http.addParameter(
398                     layoutFriendlyURL, "doAsUserId",
399                     themeDisplay.getDoAsUserId());
400             }
401 
402             return layoutFriendlyURL;
403         }
404 
405         String layoutURL = getLayoutActualURL(layout, themeDisplay);
406 
407         if (doAsUser && Validator.isNotNull(themeDisplay.getDoAsUserId())) {
408             layoutURL = Http.addParameter(
409                 layoutURL, "doAsUserId", themeDisplay.getDoAsUserId());
410         }
411 
412         return layoutURL;
413     }
414 
415     public static String getLayoutActualURL(
416             Layout layout, ThemeDisplay themeDisplay)
417         throws PortalException, SystemException {
418 
419         return getLayoutActualURL(layout, themeDisplay.getPathMain());
420     }
421 
422     public static String getLayoutActualURL(Layout layout, String mainPath)
423         throws PortalException, SystemException {
424 
425         Map vars = new FastHashMap();
426 
427         vars.put("liferay:mainPath", mainPath);
428         vars.put("liferay:plid", layout.getPlid());
429         vars.putAll(layout.getLayoutType().getTypeSettingsProperties());
430 
431         String href = PropsUtil.getComponentProperties().getString(
432             PropsUtil.LAYOUT_URL,
433             Filter.by(layout.getType()).setVariables(vars));
434 
435         return href;
436     }
437 
438     public static String getLayoutActualURL(
439             String ownerId, String mainPath, String friendlyURL)
440         throws PortalException, SystemException {
441 
442         Layout layout = null;
443         String queryString = StringPool.BLANK;
444 
445         if (Validator.isNull(friendlyURL)) {
446             List layouts = LayoutLocalServiceUtil.getLayouts(
447                 ownerId, LayoutImpl.DEFAULT_PARENT_LAYOUT_ID);
448 
449             if (layouts.size() > 0) {
450                 layout = (Layout)layouts.get(0);
451             }
452             else {
453                 throw new NoSuchLayoutException(
454                     ownerId + " does not have any layouts");
455             }
456         }
457         else {
458             Object[] portletPlugin =
459                 getPortletFriendlyURLPlugin(ownerId, friendlyURL);
460 
461             layout = (Layout)portletPlugin[0];
462             queryString = (String)portletPlugin[1];
463         }
464 
465         String layoutActualURL =
466             PortalUtil.getLayoutActualURL(layout, mainPath);
467 
468         if (Validator.isNotNull(queryString)) {
469             layoutActualURL = layoutActualURL + queryString;
470         }
471 
472         return layoutActualURL;
473     }
474 
475     public static String getLayoutFriendlyURL(
476             Layout layout, ThemeDisplay themeDisplay)
477         throws PortalException, SystemException {
478 
479         if (!isLayoutFriendliable(layout)) {
480             return null;
481         }
482 
483         String layoutFriendlyURL = layout.getFriendlyURL();
484 
485         if (Validator.isNotNull(layoutFriendlyURL)) {
486             LayoutSet layoutSet = layout.getLayoutSet();
487 
488             if (Validator.isNotNull(layoutSet.getVirtualHost())) {
489                 String portalURL = PortalUtil.getPortalURL(
490                     layoutSet.getVirtualHost(), themeDisplay.getServerPort(),
491                     themeDisplay.isSecure());
492 
493                 return portalURL + layoutFriendlyURL;
494             }
495 
496             Group group = GroupLocalServiceUtil.getGroup(
497                 layout.getGroupId());
498 
499             String parentFriendlyURL = group.getFriendlyURL();
500 
501             if (Validator.isNotNull(parentFriendlyURL)) {
502                 String friendlyURL = null;
503 
504                 if (layout.isPrivateLayout()) {
505                     friendlyURL = themeDisplay.getPathFriendlyURLPrivate();
506                 }
507                 else {
508                     friendlyURL = themeDisplay.getPathFriendlyURLPublic();
509                 }
510 
511                 return friendlyURL + parentFriendlyURL + layoutFriendlyURL;
512             }
513         }
514 
515         return null;
516     }
517 
518     public static String getLayoutTarget(Layout layout) {
519         Properties typeSettingsProps = layout.getTypeSettingsProperties();
520 
521         String target = typeSettingsProps.getProperty("target");
522 
523         if (Validator.isNull(target)) {
524             target = StringPool.BLANK;
525         }
526         else {
527             target = "target=\"" + target + "\"";
528         }
529 
530         return target;
531     }
532 
533     public static String getJsSafePortletName(String portletName) {
534         return JS.getSafeName(portletName);
535     }
536 
537     public static Locale getLocale(HttpServletRequest req) {
538         return (Locale)req.getSession().getAttribute(Globals.LOCALE_KEY);
539     }
540 
541     public static Locale getLocale(RenderRequest req) {
542         return getLocale(getHttpServletRequest(req));
543     }
544 
545     public static HttpServletRequest getOriginalServletRequest(
546         HttpServletRequest req) {
547 
548         HttpServletRequest originalReq = req;
549 
550         while (originalReq.getClass().getName().startsWith("com.liferay.")) {
551 
552             // Get original request so that portlets inside portlets render
553             // properly
554 
555             originalReq = (HttpServletRequest)
556                 ((HttpServletRequestWrapper)originalReq).getRequest();
557         }
558 
559         return originalReq;
560     }
561 
562     public static String getPortalURL(HttpServletRequest req) {
563         return getPortalURL(req, req.isSecure());
564     }
565 
566     public static String getPortalURL(HttpServletRequest req, boolean secure) {
567         return getPortalURL(req.getServerName(), req.getServerPort(), secure);
568     }
569 
570     public static String getPortalURL(
571         String serverName, int serverPort, boolean secure) {
572 
573         StringBuffer sb = new StringBuffer();
574 
575         String serverProtocol = GetterUtil.getString(
576             PropsUtil.get(PropsUtil.WEB_SERVER_PROTOCOL));
577 
578         if (secure || Http.HTTPS.equals(serverProtocol)) {
579             sb.append(Http.HTTPS_WITH_SLASH);
580         }
581         else {
582             sb.append(Http.HTTP_WITH_SLASH);
583         }
584 
585         String serverHost = PropsUtil.get(PropsUtil.WEB_SERVER_HOST);
586 
587         if (Validator.isNull(serverHost)) {
588             sb.append(serverName);
589         }
590         else {
591             sb.append(serverHost);
592         }
593 
594         int serverHttpPort = GetterUtil.getInteger(
595             PropsUtil.get(PropsUtil.WEB_SERVER_HTTP_PORT), -1);
596 
597         if (serverHttpPort == -1) {
598             if (!secure && (serverPort != Http.HTTP_PORT)) {
599                 sb.append(StringPool.COLON);
600                 sb.append(serverPort);
601             }
602         }
603         else {
604             if (!secure && (serverPort != serverHttpPort)) {
605                 if (serverHttpPort != Http.HTTP_PORT) {
606                     sb.append(StringPool.COLON);
607                     sb.append(serverHttpPort);
608                 }
609             }
610         }
611 
612         int serverHttpsPort = GetterUtil.getInteger(
613             PropsUtil.get(PropsUtil.WEB_SERVER_HTTPS_PORT), -1);
614 
615         if (serverHttpsPort == -1) {
616             if (secure && (serverPort != Http.HTTPS_PORT)) {
617                 sb.append(StringPool.COLON);
618                 sb.append(serverPort);
619             }
620         }
621         else {
622             if (secure && (serverPort != serverHttpsPort)) {
623                 if (serverHttpsPort != Http.HTTPS_PORT) {
624                     sb.append(StringPool.COLON);
625                     sb.append(serverHttpsPort);
626                 }
627             }
628         }
629 
630         return sb.toString();
631     }
632 
633     public static Object[] getPortletFriendlyURLPlugin(
634             String ownerId, String url)
635         throws PortalException, SystemException {
636 
637         String friendlyURL = url;
638         String queryString = StringPool.BLANK;
639 
640         Map portletPlugins = PortletLocalServiceUtil.getFriendlyURLPlugins();
641 
642         Iterator itr = portletPlugins.entrySet().iterator();
643 
644         while (itr.hasNext()) {
645             Map.Entry entry = (Map.Entry)itr.next();
646 
647             String className = (String)entry.getValue();
648 
649             FriendlyURLPortletPlugin portletPlugin =
650                 (FriendlyURLPortletPlugin)InstancePool.get(className);
651 
652             if (url.endsWith(StringPool.SLASH + portletPlugin.getMapping())) {
653                 url += StringPool.SLASH;
654             }
655 
656             int pos = url.indexOf(
657                 StringPool.SLASH + portletPlugin.getMapping() +
658                     StringPool.SLASH);
659 
660             if (pos != -1) {
661                 String[] values = portletPlugin.getValues(url, pos);
662 
663                 friendlyURL = values[0];
664                 queryString = values[1];
665 
666                 break;
667             }
668         }
669 
670         friendlyURL = StringUtil.replace(friendlyURL, "//", StringPool.SLASH);
671 
672         if (friendlyURL.endsWith(StringPool.SLASH)) {
673             friendlyURL = friendlyURL.substring(0, friendlyURL.length() - 1);
674         }
675 
676         Layout layout = LayoutLocalServiceUtil.getFriendlyURLLayout(
677             ownerId, friendlyURL);
678 
679         return new Object[] {layout, queryString};
680     }
681 
682     public static String getPortletGroupId(String plid) {
683         String ownerId = LayoutImpl.getOwnerId(plid);
684 
685         return GetterUtil.getString(
686             LayoutImpl.getGroupId(ownerId), GroupImpl.DEFAULT_PARENT_GROUP_ID);
687     }
688 
689     public static String getPortletGroupId(HttpServletRequest req) {
690         Layout layout = (Layout)req.getAttribute(WebKeys.LAYOUT);
691 
692         return getPortletGroupId(layout.getPlid());
693     }
694 
695     public static String getPortletGroupId(ActionRequest req) {
696         return getPortletGroupId(getHttpServletRequest(req));
697     }
698 
699     public static String getPortletGroupId(RenderRequest req) {
700         return getPortletGroupId(getHttpServletRequest(req));
701     }
702 
703     public static String getPortletNamespace(String portletName) {
704         return StringPool.UNDERLINE + portletName + StringPool.UNDERLINE;
705     }
706 
707     public static String getPortletTitle(String portletId, User user)
708         throws LanguageException {
709 
710         return LanguageUtil.get(
711             user, WebKeys.JAVAX_PORTLET_TITLE + StringPool.PERIOD + portletId);
712     }
713 
714     public static String getPortletTitle(
715         Portlet portlet, ServletContext ctx, Locale locale) {
716 
717         PortletConfig portletConfig = PortletConfigFactory.create(portlet, ctx);
718 
719         ResourceBundle resourceBundle = portletConfig.getResourceBundle(locale);
720 
721         return resourceBundle.getString(WebKeys.JAVAX_PORTLET_TITLE);
722     }
723 
724     public static PortletPreferences getPreferences(HttpServletRequest req) {
725         RenderRequest renderRequest =
726             (RenderRequest)req.getAttribute(WebKeys.JAVAX_PORTLET_REQUEST);
727 
728         PortletPreferences prefs = null;
729 
730         if (renderRequest != null) {
731             PortletPreferencesWrapper prefsWrapper =
732                 (PortletPreferencesWrapper)renderRequest.getPreferences();
733 
734             prefs = prefsWrapper.getPreferencesImpl();
735         }
736 
737         return prefs;
738     }
739 
740     public static PreferencesValidator getPreferencesValidator(
741         Portlet portlet) {
742 
743         if (portlet.isWARFile()) {
744             PortletContextWrapper pcw =
745                 PortletContextPool.get(portlet.getRootPortletId());
746 
747             return pcw.getPreferencesValidator();
748         }
749         else {
750             PreferencesValidator prefsValidator = null;
751 
752             if (Validator.isNotNull(portlet.getPreferencesValidator())) {
753                 prefsValidator =
754                     (PreferencesValidator)InstancePool.get(
755                         portlet.getPreferencesValidator());
756             }
757 
758             return prefsValidator;
759         }
760     }
761 
762     public static User getSelectedUser(HttpServletRequest req)
763         throws PortalException, RemoteException, SystemException {
764 
765         String userId = ParamUtil.getString(req, "p_u_i_d");
766 
767         User user = null;
768 
769         try {
770             user = UserServiceUtil.getUserById(userId);
771         }
772         catch (NoSuchUserException nsue) {
773         }
774 
775         return user;
776     }
777 
778     public static User getSelectedUser(ActionRequest req)
779         throws PortalException, RemoteException, SystemException {
780 
781         return getSelectedUser(getHttpServletRequest(req));
782     }
783 
784     public static User getSelectedUser(RenderRequest req)
785         throws PortalException, RemoteException, SystemException {
786 
787         return getSelectedUser(getHttpServletRequest(req));
788     }
789 
790     public static String[] getSystemGroups() {
791         return _instance._getSystemGroups();
792     }
793 
794     public static String[] getSystemRoles() {
795         return _instance._getSystemRoles();
796     }
797 
798     public static UploadPortletRequest getUploadPortletRequest(
799         ActionRequest req) {
800 
801         ActionRequestImpl actionReq = (ActionRequestImpl)req;
802 
803         DynamicServletRequest dynamicReq =
804             (DynamicServletRequest)actionReq.getHttpServletRequest();
805 
806         HttpServletRequestWrapper reqWrapper =
807             (HttpServletRequestWrapper)dynamicReq.getRequest();
808 
809         UploadServletRequest uploadReq = getUploadServletRequest(reqWrapper);
810 
811         return new UploadPortletRequest(
812             uploadReq, getPortletNamespace(actionReq.getPortletName()));
813     }
814 
815     public static UploadServletRequest getUploadServletRequest(
816         HttpServletRequest httpReq) {
817 
818         HttpServletRequestWrapper httpReqWrapper = null;
819 
820         if (httpReq instanceof HttpServletRequestWrapper) {
821             httpReqWrapper = (HttpServletRequestWrapper)httpReq;
822         }
823 
824         UploadServletRequest uploadReq = null;
825 
826         while (uploadReq == null) {
827 
828             // Find the underlying UploadServletRequest wrapper. For example,
829             // WebSphere wraps all requests with ProtectedServletRequest.
830 
831             if (httpReqWrapper instanceof UploadServletRequest) {
832                 uploadReq = (UploadServletRequest)httpReqWrapper;
833             }
834             else {
835                 ServletRequest req = httpReqWrapper.getRequest();
836 
837                 if (!(req instanceof HttpServletRequestWrapper)) {
838                     break;
839                 }
840                 else {
841                     httpReqWrapper =
842                         (HttpServletRequestWrapper)httpReqWrapper.getRequest();
843                 }
844             }
845         }
846 
847         return uploadReq;
848     }
849 
850     public static User getUser(HttpServletRequest req)
851         throws PortalException, SystemException {
852 
853         String userId = getUserId(req);
854 
855         if (userId == null) {
856 
857             // Portlet WARs may have the correct remote user and not have the
858             // correct user id because the user id is saved in the session
859             // and may not be accessible by the portlet WAR's session. This
860             // behavior is inconsistent across different application servers.
861 
862             userId = req.getRemoteUser();
863 
864             if (userId == null) {
865                 return null;
866             }
867         }
868 
869         User user = (User)req.getAttribute(WebKeys.USER);
870 
871         if (user == null) {
872             user = UserLocalServiceUtil.getUserById(userId);
873 
874             req.setAttribute(WebKeys.USER, user);
875         }
876 
877         return user;
878     }
879 
880     public static User getUser(ActionRequest req)
881         throws PortalException, SystemException {
882 
883         return getUser(getHttpServletRequest(req));
884     }
885 
886     public static User getUser(RenderRequest req)
887         throws PortalException, SystemException {
888 
889         return getUser(getHttpServletRequest(req));
890     }
891 
892     public static String getUserId(HttpServletRequest req) {
893         String userId = (String)req.getAttribute(WebKeys.USER_ID);
894 
895         if (userId != null) {
896             return userId;
897         }
898 
899         if (!GetterUtil.getBoolean(
900                 PropsUtil.get(PropsUtil.PORTAL_JAAS_ENABLE)) &&
901             GetterUtil.getBoolean(
902                 PropsUtil.get(PropsUtil.PORTAL_IMPERSONATION_ENABLE))) {
903 
904             String doAsUserId = ParamUtil.getString(req, "doAsUserId");
905 
906             try {
907                 doAsUserId = _getDoAsUserId(req, doAsUserId);
908 
909                 if (doAsUserId != null) {
910                     return doAsUserId;
911                 }
912             }
913             catch (Exception e) {
914                 _log.error("Unable to impersonate user " + doAsUserId, e);
915             }
916         }
917 
918         HttpSession ses = req.getSession();
919 
920         userId = (String)ses.getAttribute(WebKeys.USER_ID);
921 
922         if (userId != null) {
923             req.setAttribute(WebKeys.USER_ID, userId);
924         }
925 
926         return userId;
927     }
928 
929     public static String getUserId(ActionRequest req) {
930         return getUserId(getHttpServletRequest(req));
931     }
932 
933     public static String getUserId(RenderRequest req) {
934         return getUserId(getHttpServletRequest(req));
935     }
936 
937     public static String getUserName(String userId, String defaultUserName) {
938         return getUserName(userId, defaultUserName, null);
939     }
940 
941     public static String getUserName(
942         String userId, String defaultUserName, HttpServletRequest req) {
943 
944         String userName = defaultUserName;
945 
946         try {
947             User user = UserLocalServiceUtil.getUserById(userId);
948 
949             userName = user.getFullName();
950 
951             if (req != null) {
952                 Layout layout = (Layout)req.getAttribute(WebKeys.LAYOUT);
953 
954                 PortletURL portletURL = new PortletURLImpl(
955                     req, PortletKeys.DIRECTORY, layout.getPlid(), false);
956 
957                 portletURL.setWindowState(WindowState.MAXIMIZED);
958                 portletURL.setPortletMode(PortletMode.VIEW);
959 
960                 portletURL.setParameter("struts_action", "/directory/edit_user");
961                 portletURL.setParameter("p_u_i_d", user.getUserId());
962 
963                 userName =
964                     "<a href=\"" + portletURL.toString() + "\">" + userName +
965                         "</a>";
966             }
967         }
968         catch (Exception e) {
969         }
970 
971         return userName;
972     }
973 
974     public static String getUserPassword(HttpSession ses) {
975         return (String)ses.getAttribute(WebKeys.USER_PASSWORD);
976     }
977 
978     public static String getUserPassword(HttpServletRequest req) {
979         return getUserPassword(req.getSession());
980     }
981 
982     public static String getUserPassword(ActionRequest req) {
983         return getUserPassword(getHttpServletRequest(req));
984     }
985 
986     public static String getUserPassword(RenderRequest req) {
987         return getUserPassword(getHttpServletRequest(req));
988     }
989 
990     public static boolean isLayoutFriendliable(Layout layout) {
991         return PropsUtil.getComponentProperties().getBoolean(
992             PropsUtil.LAYOUT_URL_FRIENDLIABLE,
993             Filter.by(layout.getType()), true);
994     }
995 
996     public static boolean isLayoutParentable(Layout layout) {
997         return isLayoutParentable(layout.getType());
998     }
999 
1000    public static boolean isLayoutParentable(String type) {
1001        return PropsUtil.getComponentProperties().getBoolean(
1002            PropsUtil.LAYOUT_PARENTABLE,
1003            Filter.by(type), true);
1004    }
1005
1006    public static boolean isReservedParameter(String name) {
1007        return _instance._reservedParams.contains(name);
1008    }
1009
1010    public static boolean isSystemGroup(String groupName) {
1011        return _instance._isSystemGroup(groupName);
1012    }
1013
1014    public static boolean isSystemRole(String roleName) {
1015        return _instance._isSystemRole(roleName);
1016    }
1017
1018    public static void renderPage(
1019            StringBuffer sb, ServletContext ctx, HttpServletRequest req,
1020            HttpServletResponse res, String path)
1021        throws IOException, ServletException {
1022
1023        RequestDispatcher rd = ctx.getRequestDispatcher(path);
1024
1025        StringServletResponse stringServletRes =
1026            new StringServletResponse(res);
1027
1028        rd.include(req, stringServletRes);
1029
1030        sb.append(stringServletRes.getString());
1031    }
1032
1033    public static void renderPortlet(
1034            StringBuffer sb, ServletContext ctx, HttpServletRequest req,
1035            HttpServletResponse res, Portlet portlet, String queryString)
1036        throws IOException, ServletException {
1037
1038        renderPortlet(
1039            sb, ctx, req, res, portlet, queryString, null, null, null);
1040    }
1041
1042    public static void renderPortlet(
1043            StringBuffer sb, ServletContext ctx, HttpServletRequest req,
1044            HttpServletResponse res, Portlet portlet, String queryString,
1045            String columnId, Integer columnPos, Integer columnCount)
1046        throws IOException, ServletException {
1047
1048        renderPortlet(
1049            sb, ctx, req, res, portlet, queryString, columnId, columnPos,
1050            columnCount, null);
1051    }
1052
1053    public static void renderPortlet(
1054            StringBuffer sb, ServletContext ctx, HttpServletRequest req,
1055            HttpServletResponse res, Portlet portlet, String queryString,
1056            String columnId, Integer columnPos, Integer columnCount,
1057            String path)
1058        throws IOException, ServletException {
1059
1060        queryString = GetterUtil.getString(queryString);
1061        columnId = GetterUtil.getString(columnId);
1062
1063        if (columnPos == null) {
1064            columnPos = new Integer(0);
1065        }
1066
1067        if (columnCount == null) {
1068            columnCount = new Integer(0);
1069        }
1070
1071        req.setAttribute(WebKeys.RENDER_PORTLET, portlet);
1072        req.setAttribute(WebKeys.RENDER_PORTLET_QUERY_STRING, queryString);
1073        req.setAttribute(WebKeys.RENDER_PORTLET_COLUMN_ID, columnId);
1074        req.setAttribute(WebKeys.RENDER_PORTLET_COLUMN_POS, columnPos);
1075        req.setAttribute(WebKeys.RENDER_PORTLET_COLUMN_COUNT, columnCount);
1076
1077        if (path == null) {
1078            path = "/html/portal/render_portlet.jsp";
1079        }
1080
1081        RequestDispatcher rd = ctx.getRequestDispatcher(path);
1082
1083        if (sb != null) {
1084            StringServletResponse stringServletRes =
1085                new StringServletResponse(res);
1086
1087            rd.include(req, stringServletRes);
1088
1089            sb.append(stringServletRes.getString());
1090        }
1091        else {
1092
1093            // LEP-766
1094
1095            String strutsCharEncoding =
1096                PropsUtil.get(PropsUtil.STRUTS_CHAR_ENCODING);
1097
1098            req.setCharacterEncoding(strutsCharEncoding);
1099
1100            res.setContentType(Constants.TEXT_HTML + "; charset=UTF-8");
1101
1102            rd.include(req, res);
1103        }
1104    }
1105
1106    public static void storePreferences(PortletPreferences prefs)
1107        throws IOException, ValidatorException {
1108
1109        PortletPreferencesWrapper prefsWrapper =
1110            (PortletPreferencesWrapper)prefs;
1111
1112        PortletPreferencesImpl prefsImpl =
1113            (PortletPreferencesImpl)prefsWrapper.getPreferencesImpl();
1114
1115        prefsImpl.store();
1116    }
1117
1118    public static PortletMode updatePortletMode(
1119            String portletId, User user, Layout layout, PortletMode portletMode)
1120        throws PortalException, RemoteException, SystemException {
1121
1122        LayoutTypePortlet layoutType =
1123            (LayoutTypePortlet)layout.getLayoutType();
1124
1125        if (portletMode == null || Validator.isNull(portletMode.toString())) {
1126            if (layoutType.hasModeAboutPortletId(portletId)) {
1127                return LiferayPortletMode.ABOUT;
1128            }
1129            else if (layoutType.hasModeConfigPortletId(portletId)) {
1130                return LiferayPortletMode.CONFIG;
1131            }
1132            else if (layoutType.hasModeEditPortletId(portletId)) {
1133                return PortletMode.EDIT;
1134            }
1135            else if (layoutType.hasModeEditDefaultsPortletId(portletId)) {
1136                return LiferayPortletMode.EDIT_DEFAULTS;
1137            }
1138            else if (layoutType.hasModeEditGuestPortletId(portletId)) {
1139                return LiferayPortletMode.EDIT_GUEST;
1140            }
1141            else if (layoutType.hasModeHelpPortletId(portletId)) {
1142                return PortletMode.HELP;
1143            }
1144            else if (layoutType.hasModePreviewPortletId(portletId)) {
1145                return LiferayPortletMode.PREVIEW;
1146            }
1147            else if (layoutType.hasModePrintPortletId(portletId)) {
1148                return LiferayPortletMode.PRINT;
1149            }
1150            else {
1151                return PortletMode.VIEW;
1152            }
1153        }
1154        else {
1155            boolean updateLayout = false;
1156
1157            if (portletMode.equals(LiferayPortletMode.ABOUT) &&
1158                !layoutType.hasModeAboutPortletId(portletId)) {
1159
1160                layoutType.addModeAboutPortletId(portletId);
1161
1162                updateLayout = true;
1163            }
1164            else if (portletMode.equals(LiferayPortletMode.CONFIG) &&
1165                     !layoutType.hasModeConfigPortletId(portletId)) {
1166
1167                layoutType.addModeConfigPortletId(portletId);
1168
1169                updateLayout = true;
1170            }
1171            else if (portletMode.equals(PortletMode.EDIT) &&
1172                     !layoutType.hasModeEditPortletId(portletId)) {
1173
1174                layoutType.addModeEditPortletId(portletId);
1175
1176                updateLayout = true;
1177            }
1178            else if (portletMode.equals(LiferayPortletMode.EDIT_DEFAULTS) &&
1179                     !layoutType.hasModeEditDefaultsPortletId(portletId)) {
1180
1181                layoutType.addModeEditDefaultsPortletId(portletId);
1182
1183                updateLayout = true;
1184            }
1185            else if (portletMode.equals(LiferayPortletMode.EDIT_GUEST) &&
1186                     !layoutType.hasModeEditGuestPortletId(portletId)) {
1187
1188                layoutType.addModeEditGuestPortletId(portletId);
1189
1190                updateLayout = true;
1191            }
1192            else if (portletMode.equals(PortletMode.HELP) &&
1193                     !layoutType.hasModeHelpPortletId(portletId)) {
1194
1195                layoutType.addModeHelpPortletId(portletId);
1196
1197                updateLayout = true;
1198            }
1199            else if (portletMode.equals(LiferayPortletMode.PREVIEW) &&
1200                     !layoutType.hasModePreviewPortletId(portletId)) {
1201
1202                layoutType.addModePreviewPortletId(portletId);
1203
1204                updateLayout = true;
1205            }
1206            else if (portletMode.equals(LiferayPortletMode.PRINT) &&
1207                     !layoutType.hasModePrintPortletId(portletId)) {
1208
1209                layoutType.addModePrintPortletId(portletId);
1210
1211                updateLayout = true;
1212            }
1213            else if (portletMode.equals(PortletMode.VIEW) &&
1214                     !layoutType.hasModeViewPortletId(portletId)) {
1215
1216                layoutType.removeModesPortletId(portletId);
1217
1218                updateLayout = true;
1219            }
1220
1221            if (updateLayout) {
1222                if ((user != null) && !layout.isShared()) {
1223                    LayoutServiceUtil.updateLayout(
1224                        layout.getLayoutId(), layout.getOwnerId(),
1225                        layout.getTypeSettings());
1226                }
1227            }
1228
1229            return portletMode;
1230        }
1231    }
1232
1233    public static WindowState updateWindowState(
1234            String portletId, User user, Layout layout, WindowState windowState,
1235            HttpServletRequest req)
1236        throws PortalException, RemoteException, SystemException {
1237
1238        LayoutTypePortlet layoutType =
1239            (LayoutTypePortlet)layout.getLayoutType();
1240
1241        if ((windowState == null) ||
1242            (Validator.isNull(windowState.toString()))) {
1243
1244            if (layoutType.hasStateMaxPortletId(portletId)) {
1245                return WindowState.MAXIMIZED;
1246            }
1247            else if (layoutType.hasStateMinPortletId(portletId)) {
1248                return WindowState.MINIMIZED;
1249            }
1250            else {
1251                return WindowState.NORMAL;
1252            }
1253        }
1254        else {
1255            boolean updateLayout = false;
1256
1257            if ((windowState.equals(WindowState.MAXIMIZED)) ||
1258                (windowState.equals(LiferayWindowState.EXCLUSIVE)) ||
1259                (windowState.equals(LiferayWindowState.POP_UP))) {
1260
1261                if (layoutType.hasStateMax()) {
1262                    String curMaxPortletId =
1263                        StringUtil.split(layoutType.getStateMax())[0];
1264
1265                    // Clear cache and render parameters for the previous
1266                    // portlet that had a maximum window state
1267
1268                    CachePortlet.clearResponse(
1269                        req.getSession(), layout.getPrimaryKey(),
1270                        curMaxPortletId);
1271
1272                    /*RenderParametersPool.clear(
1273                        req, layout.getPlid(), curMaxPortletId);*/
1274
1275                    if ((windowState.equals(LiferayWindowState.EXCLUSIVE)) ||
1276                        (windowState.equals(LiferayWindowState.POP_UP))) {
1277
1278                        String stateMaxPrevious =
1279                            layoutType.getStateMaxPrevious();
1280
1281                        if (stateMaxPrevious == null) {
1282                            layoutType.setStateMaxPrevious(curMaxPortletId);
1283
1284                            updateLayout = true;
1285                        }
1286                    }
1287                }
1288                else {
1289                    if ((windowState.equals(LiferayWindowState.EXCLUSIVE)) ||
1290                        (windowState.equals(LiferayWindowState.POP_UP))) {
1291
1292                        String stateMaxPrevious =
1293                            layoutType.getStateMaxPrevious();
1294
1295                        if (stateMaxPrevious == null) {
1296                            layoutType.setStateMaxPrevious(StringPool.BLANK);
1297
1298                            updateLayout = true;
1299                        }
1300                    }
1301                }
1302
1303                if (!layoutType.hasStateMaxPortletId(portletId)) {
1304                    layoutType.addStateMaxPortletId(portletId);
1305
1306                    updateLayout = true;
1307                }
1308            }
1309            else if (windowState.equals(WindowState.MINIMIZED) &&
1310                     !layoutType.hasStateMinPortletId(portletId)) {
1311
1312                layoutType.addStateMinPortletId(portletId);
1313
1314                updateLayout = true;
1315            }
1316            else if (windowState.equals(WindowState.NORMAL) &&
1317                     !layoutType.hasStateNormalPortletId(portletId)) {
1318
1319                layoutType.removeStatesPortletId(portletId);
1320
1321                updateLayout = true;
1322            }
1323
1324            if (updateLayout) {
1325                if ((user != null) && !layout.isShared()) {
1326                    LayoutServiceUtil.updateLayout(
1327                        layout.getLayoutId(), layout.getOwnerId(),
1328                        layout.getTypeSettings());
1329                }
1330            }
1331
1332            return windowState;
1333        }
1334    }
1335
1336    private static String _getDoAsUserId(
1337            HttpServletRequest req, String doAsUserId)
1338        throws Exception {
1339
1340        if (Validator.isNull(doAsUserId)) {
1341            return null;
1342        }
1343
1344        HttpSession ses = req.getSession();
1345
1346        String realUserId = (String)ses.getAttribute(WebKeys.USER_ID);
1347
1348        if (realUserId == null) {
1349            return null;
1350        }
1351
1352        Company company = getCompany(req);
1353
1354        doAsUserId = Encryptor.decrypt(company.getKeyObj(), doAsUserId);
1355
1356        User doAsUser = UserLocalServiceUtil.getUserById(doAsUserId);
1357
1358        String organizationId = doAsUser.getOrganization().getOrganizationId();
1359        String locationId = doAsUser.getLocation().getOrganizationId();
1360
1361        User realUser = UserLocalServiceUtil.getUserById(realUserId);
1362        boolean signedIn = true;
1363        boolean checkGuest = true;
1364
1365        PermissionCheckerImpl permissionChecker = null;
1366
1367        try {
1368            permissionChecker = PermissionCheckerFactory.create(
1369                realUser, signedIn, checkGuest);
1370
1371            if (doAsUser.isDefaultUser() ||
1372                UserPermission.contains(
1373                    permissionChecker, doAsUserId, organizationId, locationId,
1374                    ActionKeys.IMPERSONATE)) {
1375
1376                req.setAttribute(WebKeys.USER_ID, doAsUserId);
1377
1378                return doAsUserId;
1379            }
1380            else {
1381                _log.error(
1382                    "User " + realUserId + " does not have the permission to " +
1383                        "impersonate " + doAsUserId);
1384
1385                return null;
1386            }
1387        }
1388        finally {
1389            try {
1390                PermissionCheckerFactory.recycle(permissionChecker);
1391            }
1392            catch (Exception e) {
1393            }
1394        }
1395    }
1396
1397    private PortalUtil() {
1398
1399        // Groups
1400
1401        String customSystemGroups[] =
1402            PropsUtil.getArray(PropsUtil.SYSTEM_GROUPS);
1403
1404        if (customSystemGroups == null || customSystemGroups.length == 0) {
1405            _allSystemGroups = GroupImpl.SYSTEM_GROUPS;
1406        }
1407        else {
1408            _allSystemGroups = new String[
1409                GroupImpl.SYSTEM_GROUPS.length + customSystemGroups.length];
1410
1411            System.arraycopy(
1412                GroupImpl.SYSTEM_GROUPS, 0, _allSystemGroups, 0,
1413                GroupImpl.SYSTEM_GROUPS.length);
1414
1415            System.arraycopy(
1416                customSystemGroups, 0, _allSystemGroups,
1417                GroupImpl.SYSTEM_GROUPS.length, customSystemGroups.length);
1418        }
1419
1420        _sortedSystemGroups = new String[_allSystemGroups.length];
1421
1422        System.arraycopy(
1423            _allSystemGroups, 0, _sortedSystemGroups, 0,
1424            _allSystemGroups.length);
1425
1426        Arrays.sort(_sortedSystemGroups, new StringComparator());
1427
1428        // Roles
1429
1430        String customSystemRoles[] = PropsUtil.getArray(PropsUtil.SYSTEM_ROLES);
1431
1432        if (customSystemRoles == null || customSystemRoles.length == 0) {
1433            _allSystemRoles = RoleImpl.SYSTEM_ROLES;
1434        }
1435        else {
1436            _allSystemRoles = new String[
1437                RoleImpl.SYSTEM_ROLES.length + customSystemRoles.length];
1438
1439            System.arraycopy(
1440                RoleImpl.SYSTEM_ROLES, 0, _allSystemRoles, 0,
1441                RoleImpl.SYSTEM_ROLES.length);
1442
1443            System.arraycopy(
1444                customSystemRoles, 0, _allSystemRoles,
1445                RoleImpl.SYSTEM_ROLES.length, customSystemRoles.length);
1446        }
1447
1448        _sortedSystemRoles = new String[_allSystemRoles.length];
1449
1450        System.arraycopy(
1451            _allSystemRoles, 0, _sortedSystemRoles, 0, _allSystemRoles.length);
1452
1453        Arrays.sort(_sortedSystemRoles, new StringComparator());
1454
1455        // Reserved parameter names
1456
1457        _reservedParams = CollectionFactory.getHashSet();
1458
1459        _reservedParams.add("p_l_id");
1460        _reservedParams.add("p_p_id");
1461        _reservedParams.add("p_p_action");
1462        _reservedParams.add("p_p_state");
1463        _reservedParams.add("p_p_mode");
1464        _reservedParams.add("p_p_width");
1465        _reservedParams.add("p_p_col_id");
1466        _reservedParams.add("p_p_col_pos");
1467        _reservedParams.add("p_p_col_count");
1468    }
1469
1470    private String[] _getSystemGroups() {
1471        return _allSystemGroups;
1472    }
1473
1474    private String[] _getSystemRoles() {
1475        return _allSystemRoles;
1476    }
1477
1478    private boolean _isSystemGroup(String groupName) {
1479        if (groupName == null) {
1480            return false;
1481        }
1482
1483        groupName = groupName.trim();
1484
1485        int pos = Arrays.binarySearch(
1486            _sortedSystemGroups, groupName, new StringComparator());
1487
1488        if (pos >= 0) {
1489            return true;
1490        }
1491        else {
1492            return false;
1493        }
1494    }
1495
1496    private boolean _isSystemRole(String roleName) {
1497        if (roleName == null) {
1498            return false;
1499        }
1500
1501        roleName = roleName.trim();
1502
1503        int pos = Arrays.binarySearch(
1504            _sortedSystemRoles, roleName, new StringComparator());
1505
1506        if (pos >= 0) {
1507            return true;
1508        }
1509        else {
1510            return false;
1511        }
1512    }
1513
1514    private static Log _log = LogFactory.getLog(PortalUtil.class);
1515
1516    private static PortalUtil _instance = new PortalUtil();
1517
1518    private String[] _allSystemGroups;
1519    private String[] _allSystemRoles;
1520    private String[] _sortedSystemGroups;
1521    private String[] _sortedSystemRoles;
1522    private Set _reservedParams;
1523
1524}